Skip to content

Commit

Permalink
Use diagnostic range for src-import.1.2 code action
Browse files Browse the repository at this point in the history
Signed-off-by: David Kwon <dakwon@redhat.com>
  • Loading branch information
dkwon17 authored and NikolasKomonen committed Nov 8, 2019
1 parent b0dbc6b commit bc9f68b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private CodeAction createNamespaceCodeAction(Diagnostic diagnostic, Range range,
message = "Insert 'namespace' attribute in 'import' element";
}

return CodeActionFactory.insert(message, range.getEnd(), " namespace=\"\"", document.getTextDocument(), diagnostic);
return CodeActionFactory.insert(message, diagnostic.getRange().getEnd(), " namespace=\"\"", document.getTextDocument(), diagnostic);
}

private CodeAction createTargetNamespaceCodeAction(Diagnostic diagnostic, DOMDocument document, String prefix) throws BadLocationException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,12 +449,13 @@ public static PublishDiagnosticsParams pd(String uri, Diagnostic... diagnostics)

// ------------------- CodeAction assert

public static void testCodeActionsFor(String xml, Diagnostic diagnostic, CodeAction... expected) {
public static void testCodeActionsFor(String xml, Diagnostic diagnostic,
CodeAction... expected) throws BadLocationException {
testCodeActionsFor(xml, diagnostic, null, expected);
}

public static void testCodeActionsFor(String xml, Diagnostic diagnostic, String catalogPath,
CodeAction... expected) {
CodeAction... expected) throws BadLocationException {
SharedSettings settings = new SharedSettings();
settings.getFormattingSettings().setTabSize(4);
settings.getFormattingSettings().setInsertSpaces(false);
Expand All @@ -463,9 +464,23 @@ public static void testCodeActionsFor(String xml, Diagnostic diagnostic, String
}

public static void testCodeActionsFor(String xml, Diagnostic diagnostic, String catalogPath,
SharedSettings sharedSettings, CodeAction... expected) {
SharedSettings sharedSettings, CodeAction... expected) throws BadLocationException {

int offset = xml.indexOf('|');
Range range = null;

if (offset != -1) {
xml = xml.substring(0, offset) + xml.substring(offset + 1);
}
TextDocument document = new TextDocument(xml.toString(), FILE_URI);

if (offset != -1) {
Position position = document.positionAt(offset);
range = new Range(position, position);
} else {
range = diagnostic.getRange();
}

XMLLanguageService xmlLanguageService = new XMLLanguageService();

ContentModelSettings cmSettings = new ContentModelSettings();
Expand All @@ -478,7 +493,6 @@ public static void testCodeActionsFor(String xml, Diagnostic diagnostic, String

CodeActionContext context = new CodeActionContext();
context.setDiagnostics(Arrays.asList(diagnostic));
Range range = diagnostic.getRange();
DOMDocument xmlDoc = DOMParser.getInstance().parse(document, xmlLanguageService.getResolverExtensionManager());

XMLFormattingOptions formattingSettings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import static org.eclipse.lsp4xml.XMLAssert.testCodeActionsFor;

import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4xml.XMLAssert;
import org.eclipse.lsp4xml.commons.BadLocationException;
import org.eclipse.lsp4xml.extensions.xsd.participants.XSDErrorCode;
Expand Down Expand Up @@ -335,6 +336,17 @@ public void src_import_1_2() throws BadLocationException {
testCodeActionsFor(xml, d, ca(d, te(2, 8, 2, 8, " namespace=\"\"")), ca(d, te(1, 48, 1, 48, " targetNamespace=\"\"")));
}

@Test
public void src_import_1_2_different_range() throws BadLocationException {
String xml = "<?xml version=\'1.0\'?>\r\n" +
"<xs:schema xmlns:xs=\'http://www.w3.org/2001/XMLSchema\'>\r\n" +
" <xs:imp|ort></xs:import>\r\n" +
"</xs:schema>";

Diagnostic d = d(2, 2, 2, 11, XSDErrorCode.src_import_1_2);
testCodeActionsFor(xml, d, ca(d, te(2, 11, 2, 11, " namespace=\"\"")), ca(d, te(1, 54, 1, 54, " targetNamespace=\"\"")));
}

private static void testDiagnosticsFor(String xml, Diagnostic... expected) throws BadLocationException {
XMLAssert.testDiagnosticsFor(xml, null, null, "test.xsd", expected);
}
Expand Down

0 comments on commit bc9f68b

Please sign in to comment.