Skip to content

Commit

Permalink
CodeAction for missing root end tag
Browse files Browse the repository at this point in the history
Fixes eclipse#588

Signed-off-by: Nikolas Komonen <[email protected]>
  • Loading branch information
NikolasKomonen committed Nov 15, 2019
1 parent bc9f68b commit e026c08
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ public List<DOMNode> getRoots() {
return super.getChildren();
}

public Position positionAt(int position) throws BadLocationException {
public Position positionAt(int offset) throws BadLocationException {
checkCanceled();
return textDocument.positionAt(position);
return textDocument.positionAt(offset);
}

public int offsetAt(Position position) throws BadLocationException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.eclipse.lsp4xml.dom.DOMDocumentType;
import org.eclipse.lsp4xml.extensions.contentmodel.participants.codeactions.ElementUnterminatedCodeAction;
import org.eclipse.lsp4xml.extensions.contentmodel.participants.codeactions.EqRequiredInAttributeCodeAction;
import org.eclipse.lsp4xml.extensions.contentmodel.participants.codeactions.MarkupEntityMismatchCodeAction;
import org.eclipse.lsp4xml.extensions.contentmodel.participants.codeactions.OpenQuoteExpectedCodeAction;
import org.eclipse.lsp4xml.services.extensions.ICodeActionParticipant;
import org.eclipse.lsp4xml.services.extensions.diagnostics.IXMLErrorCode;
Expand Down Expand Up @@ -211,5 +212,6 @@ public static void registerCodeActionParticipants(Map<String, ICodeActionPartici
codeActions.put(ElementUnterminated.getCode(), new ElementUnterminatedCodeAction());
codeActions.put(EqRequiredInAttribute.getCode(), new EqRequiredInAttributeCodeAction());
codeActions.put(OpenQuoteExpected.getCode(), new OpenQuoteExpectedCodeAction());
codeActions.put(MarkupEntityMismatch.getCode(), new MarkupEntityMismatchCodeAction());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.eclipse.lsp4xml.extensions.contentmodel.participants.codeactions;

import java.util.List;

import org.eclipse.lsp4j.CodeAction;
import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4xml.commons.BadLocationException;
import org.eclipse.lsp4xml.commons.CodeActionFactory;
import org.eclipse.lsp4xml.dom.DOMDocument;
import org.eclipse.lsp4xml.dom.DOMElement;
import org.eclipse.lsp4xml.dom.DOMNode;
import org.eclipse.lsp4xml.services.extensions.ICodeActionParticipant;
import org.eclipse.lsp4xml.services.extensions.IComponentProvider;
import org.eclipse.lsp4xml.settings.XMLFormattingOptions;

/**
* MarkupEntityMismatchCodeAction
*/
public class MarkupEntityMismatchCodeAction implements ICodeActionParticipant {

@Override
public void doCodeAction(Diagnostic diagnostic, Range range, DOMDocument document, List<CodeAction> codeActions,
XMLFormattingOptions formattingSettings, IComponentProvider componentProvider) {


try {
int offset = document.offsetAt(diagnostic.getRange().getStart());
DOMNode node = document.findNodeAt(offset);
if(!node.isElement()) {
return;
}

DOMElement element = (DOMElement) node;
int startOffset = element.getStartTagOpenOffset();
Position startPosition = document.positionAt(startOffset);
Position endPosition = document.positionAt(document.getEnd());
endPosition.setCharacter(startPosition.getCharacter());
String elementName = element.getTagName();
CodeAction action = CodeActionFactory.insert("Close with '</" + elementName + ">'", endPosition, "</" + elementName + ">", document.getTextDocument(), diagnostic);
codeActions.add(action);
} catch (BadLocationException e) {

}


}


}
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,12 @@ public void testLessThanAttValue() throws Exception {
public void testMarkupEntityMismatch() throws Exception {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
+ "<Document xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:iso:std:iso:20022:tech:xsd:pain.001.001.03\">\r\n"
+ "<CstmrCdtTrfInitn>\r\n" + //
"</CstmrCdtTrfInitn>";
testDiagnosticsFor(xml, d(1, 1, 1, 9, XMLSyntaxErrorCode.MarkupEntityMismatch));
+ "<CstmrCdtTrfInitn>\r\n"
+ "</CstmrCdtTrfInitn>";

Diagnostic d = d(1, 1, 1, 9, XMLSyntaxErrorCode.MarkupEntityMismatch);
testDiagnosticsFor(xml, d);
testCodeActionsFor(xml, d, ca(d, te(3, 0, 3, 0, "</Document>")));
}

@Test
Expand Down

0 comments on commit e026c08

Please sign in to comment.