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.
If the declared namespace of the root element is not the same as the namespace declared in the schema, the `xmlns` attribute of the root element is highlighted. If there is no namespace declared in the root element, but one is declared in the schema, the root element is highlighted. Added a CodeAction attached to `TargetNamespace.1` that replaces the namespace declared in the instance with the namespace declared in the schema. Fixes eclipse-lemminx#704, Fixes eclipse-lemminx#703 Signed-off-by: David Thompson <[email protected]>
- Loading branch information
Showing
7 changed files
with
136 additions
and
1 deletion.
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
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
55 changes: 55 additions & 0 deletions
55
...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,55 @@ | ||
/** | ||
* 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.EnforceQuoteStyle; | ||
import org.eclipse.lemminx.settings.SharedSettings; | ||
import org.eclipse.lsp4j.CodeAction; | ||
import org.eclipse.lsp4j.Diagnostic; | ||
import org.eclipse.lsp4j.Range; | ||
|
||
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) { | ||
|
||
Matcher namespaceMatcher = NAMESPACE_EXTRACTOR.matcher(diagnostic.getMessage()); | ||
if (namespaceMatcher.find()){ | ||
String correctNamespace = namespaceMatcher.group(1); | ||
String quote = "\""; | ||
if (sharedSettings.getFormattingSettings().getEnforceQuoteStyle() == EnforceQuoteStyle.preferred) { | ||
quote = sharedSettings.getPreferences().getQuotationAsString(); | ||
} | ||
// @formatter:off | ||
CodeAction replaceNamespace = CodeActionFactory.replace( | ||
"Replace with '" + correctNamespace + "'", | ||
diagnostic.getRange(), | ||
quote + correctNamespace + quote, | ||
document.getTextDocument(), diagnostic); | ||
// @formatter:on | ||
codeActions.add(replaceNamespace); | ||
} | ||
} | ||
|
||
} |
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
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
10 changes: 10 additions & 0 deletions
10
org.eclipse.lemminx/src/test/resources/xsd/two-letter-name.xsd
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,10 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://two-letter-name"> | ||
<xs:element name="two-letter-name"> | ||
<xs:simpleType> | ||
<xs:restriction base="xs:token"> | ||
<xs:pattern value="[A-Z][a-z]"></xs:pattern> | ||
</xs:restriction> | ||
</xs:simpleType> | ||
</xs:element> | ||
</xs:schema> |