Skip to content

Commit

Permalink
Add code action to insert missing ="" for attribute value.
Browse files Browse the repository at this point in the history
  • Loading branch information
angelozerr committed Sep 26, 2018
1 parent 90bd091 commit 492212a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.apache.xerces.xni.XMLLocator;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4xml.contentmodel.participants.codeactions.ElementUnterminatedCodeAction;
import org.eclipse.lsp4xml.contentmodel.participants.codeactions.EqRequiredInAttributeCodeAction;
import org.eclipse.lsp4xml.contentmodel.participants.diagnostics.IXMLErrorCode;
import org.eclipse.lsp4xml.dom.XMLDocument;
import org.eclipse.lsp4xml.services.extensions.ICodeActionParticipant;
Expand Down Expand Up @@ -99,24 +100,27 @@ public static Range toLSPRange(XMLLocator location, XMLSyntaxErrorCode code, Obj
case ElementPrefixUnbound:
case ElementUnterminated:
return XMLPositionUtility.selectStartTag(offset, document);
case EqRequiredInAttribute: {
String attrName = (String) arguments[1];
return XMLPositionUtility.selectAttributeNameFromGivenNameAt(attrName, offset, document);
}
case EncodingDeclRequired:
case EqRequiredInAttribute:
case EqRequiredInXMLDecl:
case AttributeNotUnique:
case AttributeNSNotUnique:
return XMLPositionUtility.selectAttributeNameAt(offset, document);
case LessthanInAttValue: {
case LessthanInAttValue: {
String attrName = (String) arguments[1];
return XMLPositionUtility.selectAttributeValueAt(attrName, offset, document);
}
case QuoteRequiredInXMLDecl: {
String attrName = (String) arguments[0];
return XMLPositionUtility.selectAttributeValueAt(attrName, offset, document);
}
case EmptyPrefixedAttName: {
case EmptyPrefixedAttName: {
QName qName = (QName) arguments[0];
return XMLPositionUtility.selectAttributeValueAt(qName.rawname, offset, document);
}
}
case SDDeclInvalid:
case VersionNotSupported: {
String attrValue = (String) arguments[0];
Expand Down Expand Up @@ -171,5 +175,6 @@ public static Range toLSPRange(XMLLocator location, XMLSyntaxErrorCode code, Obj

public static void registerCodeActionParticipants(Map<String, ICodeActionParticipant> codeActions) {
codeActions.put(ElementUnterminated.getCode(), new ElementUnterminatedCodeAction());
codeActions.put(EqRequiredInAttribute.getCode(), new EqRequiredInAttributeCodeAction());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* 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 <angelo.zerr@gmail.com> - initial API and implementation
*/
package org.eclipse.lsp4xml.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.BadLocationException;
import org.eclipse.lsp4xml.commons.CodeActionFactory;
import org.eclipse.lsp4xml.dom.Element;
import org.eclipse.lsp4xml.dom.Node;
import org.eclipse.lsp4xml.dom.XMLDocument;
import org.eclipse.lsp4xml.services.extensions.ICodeActionParticipant;
import org.eclipse.lsp4xml.settings.XMLFormattingOptions;

/**
* Code action to fix EqRequiredInAttribute error.
*
*/
public class EqRequiredInAttributeCodeAction implements ICodeActionParticipant {

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

// Insert =""
try {
int offset = document.offsetAt(range.getStart());
Node node = document.findNodeAt(offset);
if (node != null && node.isElement()) {
String tagName = ((Element) node).getTagName();
if (tagName != null) {
String insertText = "=\"\"";
CodeAction insertEqualsAndQuotesAction = CodeActionFactory.insert(
"Insert '" + insertText + "'", diagnosticRange, insertText,
document.getTextDocument(), diagnostic);
codeActions.add(insertEqualsAndQuotesAction);
}
}
} catch (BadLocationException e) {
// do nothing
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ public void testEncodingDeclRequired() throws Exception {
@Test
public void testEqRequiredInAttribute() throws Exception {
String xml = "<a Ccy>123.456</a>";
testDiagnosticsFor(xml, d(0, 3, 0, 6, XMLSyntaxErrorCode.EqRequiredInAttribute));
Diagnostic d = d(0, 3, 0, 6, XMLSyntaxErrorCode.EqRequiredInAttribute);
testDiagnosticsFor(xml, d);
testCodeActionsFor(xml, d, ca(d, te(0, 6, 0, 6, "=\"\"")));
}

@Ignore("This test works on OS Windows but fails in travis, why? ")
Expand Down

0 comments on commit 492212a

Please sign in to comment.