forked from eclipse-lemminx/lemminx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Document links for xsi:schemaLocation
Every second token for the value of xsi:schemaLocation is turned into a document link to the referenced schema file. Closes eclipse-lemminx#666 Signed-off-by: David Thompson <[email protected]>
- Loading branch information
Showing
9 changed files
with
368 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/dom/SchemaLocationHint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* Copyright (c) 2018 Red Hat Inc. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v2.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.lemminx.dom; | ||
|
||
public class SchemaLocationHint implements DOMRange { | ||
|
||
private final int start, end; | ||
|
||
private final String hint; | ||
|
||
private final DOMDocument document; | ||
|
||
public SchemaLocationHint(int start, int end, String hint, DOMDocument document) { | ||
this.start = start; | ||
this.end = end; | ||
this.hint = hint; | ||
this.document = document; | ||
} | ||
|
||
/** | ||
* Get the loation hint that was assigned to this | ||
*/ | ||
public String getHint() { | ||
return this.hint; | ||
} | ||
|
||
/** | ||
* Get the start character of this SchemaLocationHint in the document. | ||
*/ | ||
@Override | ||
public int getStart() { | ||
return this.start; | ||
} | ||
|
||
/** | ||
* Get the end character of this SchemaLocationHint in the document. | ||
*/ | ||
@Override | ||
public int getEnd() { | ||
return this.end; | ||
} | ||
|
||
@Override | ||
public DOMDocument getOwnerDocument() { | ||
return document; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...lemminx/extensions/contentmodel/participants/codeactions/TargetNamespace_1CodeAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* Copyright (c) 2020 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v2.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.lemminx.extensions.contentmodel.participants.codeactions; | ||
|
||
import java.util.List; | ||
import java.util.regex.Pattern; | ||
import java.util.regex.Matcher; | ||
|
||
import org.eclipse.lemminx.commons.CodeActionFactory; | ||
import org.eclipse.lemminx.dom.DOMDocument; | ||
import org.eclipse.lemminx.services.extensions.ICodeActionParticipant; | ||
import org.eclipse.lemminx.services.extensions.IComponentProvider; | ||
import org.eclipse.lemminx.settings.SharedSettings; | ||
import org.eclipse.lemminx.utils.StringUtils; | ||
import org.eclipse.lsp4j.CodeAction; | ||
import org.eclipse.lsp4j.Diagnostic; | ||
import org.eclipse.lsp4j.Range; | ||
|
||
/** | ||
* CodeAction to replace an incorrect namespace in an .xml document. | ||
* | ||
* Changes the value of the xmlns attribute of the root element of the .xml | ||
* document to the declared namespace of the referenced .xsd document. | ||
*/ | ||
public class TargetNamespace_1CodeAction implements ICodeActionParticipant { | ||
|
||
private static final Pattern NAMESPACE_EXTRACTOR = Pattern.compile("'([^']+)'\\."); | ||
|
||
@Override | ||
public void doCodeAction(Diagnostic diagnostic, Range range, DOMDocument document, List<CodeAction> codeActions, | ||
SharedSettings sharedSettings, IComponentProvider componentProvider) { | ||
|
||
String namespace = extractNamespace(diagnostic.getMessage()); | ||
if (StringUtils.isEmpty(namespace)) { | ||
return; | ||
} | ||
String quote = sharedSettings.getPreferences().getQuotationAsString(); | ||
// @formatter:off | ||
CodeAction replaceNamespace = CodeActionFactory.replace( | ||
"Replace with '" + namespace + "'", | ||
diagnostic.getRange(), | ||
quote + namespace + quote, | ||
document.getTextDocument(), | ||
diagnostic); | ||
// @formatter:on | ||
codeActions.add(replaceNamespace); | ||
} | ||
|
||
private static String extractNamespace(String diagnosticMessage) { | ||
// The error message has this form: | ||
// TargetNamespace.1: Expecting namespace 'NaN', but the target namespace of the | ||
// schema document is 'http://two-letter-name'. | ||
Matcher nsMatcher = NAMESPACE_EXTRACTOR.matcher(diagnosticMessage); | ||
if (nsMatcher.find()) { | ||
return nsMatcher.group(1); | ||
} | ||
return null; | ||
} | ||
|
||
} |
79 changes: 79 additions & 0 deletions
79
...lemminx/extensions/contentmodel/participants/codeactions/TargetNamespace_2CodeAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/** | ||
* Copyright (c) 2020 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v2.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.lemminx.extensions.contentmodel.participants.codeactions; | ||
|
||
import java.util.List; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import org.eclipse.lemminx.commons.CodeActionFactory; | ||
import org.eclipse.lemminx.dom.DOMDocument; | ||
import org.eclipse.lemminx.dom.DOMNode; | ||
import org.eclipse.lemminx.services.extensions.ICodeActionParticipant; | ||
import org.eclipse.lemminx.services.extensions.IComponentProvider; | ||
import org.eclipse.lemminx.settings.SharedSettings; | ||
import org.eclipse.lemminx.utils.StringUtils; | ||
import org.eclipse.lemminx.utils.XMLPositionUtility; | ||
import org.eclipse.lsp4j.CodeAction; | ||
import org.eclipse.lsp4j.Diagnostic; | ||
import org.eclipse.lsp4j.Position; | ||
import org.eclipse.lsp4j.Range; | ||
|
||
/** | ||
* Code action to add the missing xmlns declaration to the root element in an | ||
* .xml document. | ||
* | ||
* Finds the namespace of the referenced .xsd. Adds the xmlns attribute to the | ||
* root of .xml, and sets its value to the .xsd namespace. | ||
*/ | ||
public class TargetNamespace_2CodeAction implements ICodeActionParticipant { | ||
|
||
private static final Pattern NAMESPACE_EXTRACTOR = Pattern.compile("'([^']+)'\\."); | ||
|
||
@Override | ||
public void doCodeAction(Diagnostic diagnostic, Range range, DOMDocument document, List<CodeAction> codeActions, | ||
SharedSettings sharedSettings, IComponentProvider componentProvider) { | ||
|
||
String namespace = extractNamespace(diagnostic.getMessage()); | ||
if (StringUtils.isEmpty(namespace)) { | ||
return; | ||
} | ||
DOMNode root = document.getDocumentElement(); | ||
if (root == null) { | ||
return; | ||
} | ||
Position tagEnd = XMLPositionUtility.selectStartTagName(root).getEnd(); | ||
String quote = sharedSettings.getPreferences().getQuotationAsString(); | ||
// @formatter:off | ||
CodeAction addNamespaceDecl = CodeActionFactory.insert( | ||
"Declare '" + namespace + "' as the namespace", | ||
tagEnd, | ||
" xmlns=" + quote + namespace + quote, | ||
document.getTextDocument(), | ||
diagnostic); | ||
// @formatter:on | ||
codeActions.add(addNamespaceDecl); | ||
} | ||
|
||
private static String extractNamespace(String diagnosticMessage) { | ||
// The error message has this form: | ||
// TargetNamespace.2: Expecting no namespace, but the schema document has a | ||
// target namespace of 'http://docbook.org/ns/docbook'. | ||
Matcher nsMatcher = NAMESPACE_EXTRACTOR.matcher(diagnosticMessage); | ||
if (nsMatcher.find()) { | ||
return nsMatcher.group(1); | ||
} | ||
return null; | ||
} | ||
|
||
} |
Oops, something went wrong.