Skip to content

Commit

Permalink
Adjust range for ElementDeclUnterminated (see #225)
Browse files Browse the repository at this point in the history
  • Loading branch information
angelozerr committed Dec 1, 2018
1 parent c5768b5 commit e6f38eb
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.eclipse.lsp4j.CodeAction;
import org.eclipse.lsp4j.CodeActionKind;
import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.TextDocumentEdit;
import org.eclipse.lsp4j.TextDocumentItem;
Expand Down Expand Up @@ -51,12 +52,12 @@ public static CodeAction remove(String title, Range range, TextDocumentItem docu
* @param diagnostic
* @return
*/
public static CodeAction insert(String title, Range range, String insertText, TextDocumentItem document,
public static CodeAction insert(String title, Position position, String insertText, TextDocumentItem document,
Diagnostic diagnostic) {
CodeAction insertContentAction = new CodeAction(title);
insertContentAction.setKind(CodeActionKind.QuickFix);
insertContentAction.setDiagnostics(Arrays.asList(diagnostic));
TextEdit edit = new TextEdit(new Range(range.getEnd(), range.getEnd()), insertText);
TextEdit edit = new TextEdit(new Range(position, position), insertText);
VersionedTextDocumentIdentifier versionedTextDocumentIdentifier = new VersionedTextDocumentIdentifier(
document.getUri(), document.getVersion());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class ContentModelCodeActionParticipant implements ICodeActionParticipant
public ContentModelCodeActionParticipant() {
codeActionParticipants = new HashMap<>();
XMLSyntaxErrorCode.registerCodeActionParticipants(codeActionParticipants);
DTDErrorCode.registerCodeActionParticipants(codeActionParticipants);
XMLSchemaErrorCode.registerCodeActionParticipants(codeActionParticipants);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import org.apache.xerces.xni.XMLLocator;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4xml.dom.DOMDocument;
import org.eclipse.lsp4xml.extensions.contentmodel.participants.codeactions.ElementDeclUnterminatedCodeAction;
import org.eclipse.lsp4xml.services.extensions.ICodeActionParticipant;
import org.eclipse.lsp4xml.services.extensions.diagnostics.IXMLErrorCode;
import org.eclipse.lsp4xml.utils.XMLPositionUtility;

Expand All @@ -30,8 +32,7 @@ public enum DTDErrorCode implements IXMLErrorCode {
MSG_ELEMENT_NOT_DECLARED, MSG_CONTENT_INCOMPLETE, MSG_CONTENT_INVALID, MSG_REQUIRED_ATTRIBUTE_NOT_SPECIFIED,
MSG_ATTRIBUTE_NOT_DECLARED, MSG_ATTRIBUTE_VALUE_NOT_IN_LIST, MSG_FIXED_ATTVALUE_INVALID,

MSG_ELEMENT_TYPE_REQUIRED_IN_ELEMENTDECL,
MSG_MARKUP_NOT_RECOGNIZED_IN_DTD;
MSG_ELEMENT_TYPE_REQUIRED_IN_ELEMENTDECL, MSG_MARKUP_NOT_RECOGNIZED_IN_DTD, ElementDeclUnterminated;

private final String code;

Expand Down Expand Up @@ -92,10 +93,20 @@ public static Range toLSPRange(XMLLocator location, DTDErrorCode code, Object[]
String attrName = (String) arguments[0];
return XMLPositionUtility.selectAttributeValueAt(attrName, offset, document);
}

// ---------- DTD Doc type

case ElementDeclUnterminated: {
return XMLPositionUtility.selectDTDElementDeclAt(offset, document);
}
case MSG_ELEMENT_TYPE_REQUIRED_IN_ELEMENTDECL: {
return XMLPositionUtility.selectDTDElementDeclTagAt(offset, document);
}
}
return null;
}

public static void registerCodeActionParticipants(Map<String, ICodeActionParticipant> codeActions) {
codeActions.put(ElementDeclUnterminated.getCode(), new ElementDeclUnterminatedCodeAction());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Copyright (c) 2018 Angelo ZERR.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Angelo Zerr <[email protected]> - initial API and implementation
*/
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.Range;
import org.eclipse.lsp4xml.commons.CodeActionFactory;
import org.eclipse.lsp4xml.dom.DOMDocument;
import org.eclipse.lsp4xml.services.extensions.ICodeActionParticipant;
import org.eclipse.lsp4xml.services.extensions.IComponentProvider;
import org.eclipse.lsp4xml.settings.XMLFormattingOptions;

/**
* Code action to fix ElementDeclUnterminated error.
*
*/
public class ElementDeclUnterminatedCodeAction implements ICodeActionParticipant {

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

// Close with '>
CodeAction closeAction = CodeActionFactory.insert("Close with '>'", diagnosticRange.getEnd(), ">",
document.getTextDocument(), diagnostic);
codeActions.add(closeAction);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ public void doCodeAction(Diagnostic diagnostic, Range range, DOMDocument documen
XMLFormattingOptions formattingSettings, IComponentProvider componentProvider) {
Range diagnosticRange = diagnostic.getRange();
// Close with '/>
CodeAction autoCloseAction = CodeActionFactory.insert("Close with '/>'", diagnosticRange, "/>",
CodeAction autoCloseAction = CodeActionFactory.insert("Close with '/>'", diagnosticRange.getEnd(), "/>",
document.getTextDocument(), diagnostic);
codeActions.add(autoCloseAction);

// Close with '>
CodeAction closeAction = CodeActionFactory.insert("Close with '>'", diagnosticRange, ">",
CodeAction closeAction = CodeActionFactory.insert("Close with '>'", diagnosticRange.getEnd(), ">",
document.getTextDocument(), diagnostic);
codeActions.add(closeAction);

Expand All @@ -53,7 +53,7 @@ public void doCodeAction(Diagnostic diagnostic, Range range, DOMDocument documen
if (tagName != null) {
String insertText = "></" + tagName + ">";
CodeAction closeEndTagAction = CodeActionFactory.insert("Close with '" + insertText + "'",
diagnosticRange, insertText, document.getTextDocument(), diagnostic);
diagnosticRange.getEnd(), insertText, document.getTextDocument(), diagnostic);
codeActions.add(closeEndTagAction);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void doCodeAction(Diagnostic diagnostic, Range range, DOMDocument documen
if (tagName != null) {
String insertText = "=\"\"";
CodeAction insertEqualsAndQuotesAction = CodeActionFactory.insert("Insert '" + insertText + "'",
diagnosticRange, insertText, document.getTextDocument(), diagnostic);
diagnosticRange.getEnd(), insertText, document.getTextDocument(), diagnostic);
codeActions.add(insertEqualsAndQuotesAction);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void doCodeAction(Diagnostic diagnostic, Range range, DOMDocument documen

// Insert required attributes
CodeAction insertRequiredAttributesAction = CodeActionFactory.insert("Insert required attributes",
diagnosticRange, xmlAttributes, document.getTextDocument(), diagnostic);
diagnosticRange.getEnd(), xmlAttributes, document.getTextDocument(), diagnostic);
codeActions.add(insertRequiredAttributesAction);
} catch (Exception e) {
// Do nothing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,23 @@ public class DTDDoctypeDiagnosticsTest {

@Test
public void MSG_ELEMENT_TYPE_REQUIRED_IN_ELEMENTDECL() throws Exception {
String xml = "<?xml version=\"1.0\"?>\r\n" + //
"<!DOCTYPE student [\r\n" + //
String xml = "<?xml version=\"1.0\"?>\r\n" + //
"<!DOCTYPE student [\r\n" + //
" <!ELEMENT \r\n" + // <-- error
"]>\r\n" + //
"<student />";
XMLAssert.testDiagnosticsFor(xml, d(2, 2, 11, DTDErrorCode.MSG_ELEMENT_TYPE_REQUIRED_IN_ELEMENTDECL));
}

@Test
public void ElementDeclUnterminated() throws Exception {
String xml = "<?xml version=\"1.0\"?>\r\n" + //
"<!DOCTYPE student [\r\n" + //
" <!ELEMENT student (surname,id)>\r\n" + //
" <!ELEMENT surname (#PCDATA)\r\n" + // <- error
"]>\r\n" + //
"<student />";
XMLAssert.testDiagnosticsFor(xml, d(3, 2, 30, DTDErrorCode.ElementDeclUnterminated));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0"?>
<!DOCTYPE student [
<!ELEMENT student (surname,id)>
<!ELEMENT surname (#PCDATA)
]>
<student />

0 comments on commit e6f38eb

Please sign in to comment.