Skip to content

Commit

Permalink
Adjust error for cvc-complex-type.4 + code action to add required
Browse files Browse the repository at this point in the history
attributes (see #71)
  • Loading branch information
angelozerr committed Sep 8, 2018
1 parent ee6621e commit 41fb0a7
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

import org.apache.xerces.xni.XMLLocator;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4xml.contentmodel.participants.codeactions.CVCComplexType23CodeAction;
import org.eclipse.lsp4xml.contentmodel.participants.codeactions.cvc_complex_type_2_3CodeAction;
import org.eclipse.lsp4xml.contentmodel.participants.codeactions.cvc_complex_type_4CodeAction;
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 @@ -93,18 +94,12 @@ public static Range toLSPRange(XMLLocator location, XMLSchemaErrorCode code, Obj
case cvc_complex_type_2_4_a:
case cvc_complex_type_2_4_d:
case cvc_elt_1_a:
case cvc_complex_type_4:
return XMLPositionUtility.selectStartTag(offset, document);
case cvc_complex_type_3_2_2: {
String attrName = (String) arguments[1];
return XMLPositionUtility.selectAttributeName(attrName, offset, document);
}
case cvc_complex_type_4: {
// String tag = (String) arguments[0];
// String attrName = (String) arguments[1];
// startOffset = findOffsetOfStartTag(document.ge.getText(), offset, tag);
// endOffset = startOffset + tag.length();
break;
}
case cvc_type_3_1_1:
return XMLPositionUtility.selectAllAttributes(offset, document);
}
Expand All @@ -113,6 +108,7 @@ public static Range toLSPRange(XMLLocator location, XMLSchemaErrorCode code, Obj
}

public static void registerCodeActionParticipants(Map<String, ICodeActionParticipant> codeActions) {
codeActions.put(cvc_complex_type_2_3.getCode(), new CVCComplexType23CodeAction());
codeActions.put(cvc_complex_type_2_3.getCode(), new cvc_complex_type_2_3CodeAction());
codeActions.put(cvc_complex_type_4.getCode(), new cvc_complex_type_4CodeAction());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* Code action to fix cvc-complex-type.2.3 error.
*
*/
public class CVCComplexType23CodeAction implements ICodeActionParticipant {
public class cvc_complex_type_2_3CodeAction implements ICodeActionParticipant {

@Override
public void doCodeAction(Diagnostic diagnostic, Range range, XMLDocument document, List<CodeAction> codeActions) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* 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 java.util.stream.Collectors;

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.contentmodel.model.CMAttributeDeclaration;
import org.eclipse.lsp4xml.contentmodel.model.CMElementDeclaration;
import org.eclipse.lsp4xml.contentmodel.model.ContentModelManager;
import org.eclipse.lsp4xml.contentmodel.utils.XMLGenerator;
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;

/**
* Code action to fix cvc-complex-type.2.3 error.
*
*/
public class cvc_complex_type_4CodeAction implements ICodeActionParticipant {

@Override
public void doCodeAction(Diagnostic diagnostic, Range range, XMLDocument document, List<CodeAction> codeActions) {
try {
int offset = document.offsetAt(range.getStart());
Node node = document.findNodeAt(offset);
if (!node.isElement()) {
return;
}
Element element = (Element) node;
CMElementDeclaration elementDeclaration = ContentModelManager.getInstance().findCMElement(element);
if (elementDeclaration == null) {
return;
}

List<CMAttributeDeclaration> requiredAttributes = elementDeclaration.getAttributes().stream().filter(CMAttributeDeclaration::isRequired).collect(Collectors.toList());
if (requiredAttributes.isEmpty()) {

}
XMLGenerator generator = new XMLGenerator(null, "", "", true, 0);
String xmlAttributes = generator.generate(requiredAttributes);

// Insert content
CodeAction removeContentAction = CodeActionFactory.insert("Insert required attributes", range,
xmlAttributes,
document.getTextDocument(), diagnostic);
codeActions.add(removeContentAction);
} catch (Exception e) {
// Do nothing
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,7 @@ private int generate(CMElementDeclaration elementDeclaration, String prefix, int
xml.startElement(prefix, elementDeclaration.getName(), false);
// Attributes
Collection<CMAttributeDeclaration> attributes = elementDeclaration.getAttributes();
int attributeIndex = 0;
for (CMAttributeDeclaration attributeDeclaration : attributes) {
if (attributeDeclaration.isRequired()) {
String value = "";
if (canSupportSnippets) {
snippetIndex++;
value = ("$" + snippetIndex);
}
xml.addAttribute(attributeDeclaration.getName(), value, attributeIndex, level);
attributeIndex++;
}
}
snippetIndex = generate(attributes, level, snippetIndex, xml);
// Elements children
Collection<CMElementDeclaration> children = elementDeclaration.getElements();
if (children.size() > 0) {
Expand Down Expand Up @@ -125,4 +114,26 @@ private int generate(CMElementDeclaration elementDeclaration, String prefix, int
return snippetIndex;
}

public String generate(Collection<CMAttributeDeclaration> attributes) {
XMLBuilder xml = new XMLBuilder(formattingOptions, whitespacesIndent, lineDelimiter);
generate(attributes, 0, 0, xml);
return xml.toString();
}

private int generate(Collection<CMAttributeDeclaration> attributes, int level, int snippetIndex, XMLBuilder xml) {
int attributeIndex = 0;
for (CMAttributeDeclaration attributeDeclaration : attributes) {
if (attributeDeclaration.isRequired()) {
String value = "";
if (canSupportSnippets) {
snippetIndex++;
value = ("$" + snippetIndex);
}
xml.addAttribute(attributeDeclaration.getName(), value, attributeIndex, level);
attributeIndex++;
}
}
return snippetIndex;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
*/
public class XMLBuilder {

private final XMLFormattingOptions clientFormats;
private final XMLFormattingOptions formattingOptions;
private final String lineDelimiter;
private final StringBuilder xml;
private final String whitespacesIndent;

public XMLBuilder(XMLFormattingOptions clientFormats, String whitespacesIndent, String lineDelimiter) {
public XMLBuilder(XMLFormattingOptions formattingOptions, String whitespacesIndent, String lineDelimiter) {
this.whitespacesIndent = whitespacesIndent;
this.clientFormats = clientFormats;
this.formattingOptions = formattingOptions;
this.lineDelimiter = lineDelimiter;
this.xml = new StringBuilder();
}
Expand Down Expand Up @@ -74,7 +74,7 @@ public XMLBuilder endElement() {
}

public XMLBuilder addAttribute(String name, String value, int index, int level) {
if (index > 0 && clientFormats.isSplitAttributes()) {
if (index > 0 && isSplitAttributes()) {
linefeed();
indent(level);
}
Expand Down Expand Up @@ -102,8 +102,8 @@ public XMLBuilder addContent(String text) {

public XMLBuilder indent(int level) {
for (int i = 0; i < level; i++) {
if (clientFormats.isInsertSpaces()) {
for (int j = 0; j < clientFormats.getTabSize(); j++) {
if (isInsertSpaces()) {
for (int j = 0; j < getTabSize(); j++) {
xml.append(" ");
}
} else {
Expand Down Expand Up @@ -142,7 +142,7 @@ public XMLBuilder startCDATA() {
}

public XMLBuilder addContentCDATA(String content) {
if (clientFormats.isJoinCDATALines()) {
if (isJoinCDATALines()) {
content = normalizeSpace(content);
}
xml.append(content);
Expand Down Expand Up @@ -178,7 +178,7 @@ public XMLBuilder startComment(Comment comment) {
}

public XMLBuilder addContentComment(String content) {
if (clientFormats.isJoinCommentLines()) {
if (isJoinCommentLines()) {
xml.append(" ");
xml.append(normalizeSpace(content));
} else {
Expand Down Expand Up @@ -206,4 +206,24 @@ public XMLBuilder endDoctype() {
xml.append(">");
return this;
}

private boolean isJoinCommentLines() {
return formattingOptions != null && formattingOptions.isJoinCommentLines();
}

private boolean isJoinCDATALines() {
return formattingOptions != null && formattingOptions.isJoinCDATALines();
}

private boolean isSplitAttributes() {
return formattingOptions != null && formattingOptions.isSplitAttributes();
}

private boolean isInsertSpaces() {
return formattingOptions != null && formattingOptions.isInsertSpaces();
}

private int getTabSize() {
return formattingOptions != null ? formattingOptions.getTabSize() : 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ public void cvc_complex_type_2_3() throws Exception {
testCodeActionsFor(xml, d, ca(d, te(3, 2, 3, 15, "")));
}

@Test
public void cvc_complex_type_4() throws Exception {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + //
"<beans xmlns=\"http://www.springframework.org/schema/beans\" xsi:schemaLocation=\"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\r\n"
+ //
" <bean>\r\n" + //
" <property></property>\r\n" + //
" </bean>\r\n" + //
"</beans>";
Diagnostic d = d(3, 3, 3, 11, XMLSchemaErrorCode.cvc_complex_type_4);
testDiagnosticsFor(xml, d);
testCodeActionsFor(xml, d, ca(d, te(3, 11, 3, 11, " name=\"$1\"")));
}

@Test
public void cvc_complex_type_2_4_a() throws Exception {
String xml = "<project xmlns=\"http://maven.apache.org/POM/4.0.0\"\r\n" + //
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<bean>
<property></property>
</bean>
</beans>

0 comments on commit 41fb0a7

Please sign in to comment.