Skip to content

Commit

Permalink
Completion for xmlns:xsi
Browse files Browse the repository at this point in the history
Fixes eclipse-lemminx#326

Signed-off-by: Nikolas <[email protected]>
  • Loading branch information
NikolasKomonen committed Mar 14, 2019
1 parent 3988871 commit 6dc967b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ public class XSICompletionParticipant extends CompletionParticipantAdapter {
@Override
public void onAttributeName(boolean generateValue, Range fullRange, ICompletionRequest request,
ICompletionResponse response) throws Exception {
if (request.getXMLDocument().hasSchemaInstancePrefix()) {
//if (request.getXMLDocument().hasSchemaInstancePrefix()) {
XSISchemaModel.computeCompletionResponses(request, response, fullRange, request.getXMLDocument(),
generateValue);
}

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,14 @@
import org.eclipse.lsp4j.CompletionItem;
import org.eclipse.lsp4j.CompletionItemKind;
import org.eclipse.lsp4j.Hover;
import org.eclipse.lsp4j.InsertTextFormat;
import org.eclipse.lsp4j.MarkupContent;
import org.eclipse.lsp4j.MarkupKind;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.TextEdit;
import org.eclipse.lsp4xml.commons.BadLocationException;
import org.eclipse.lsp4xml.dom.DOMAttr;
import org.eclipse.lsp4xml.dom.DOMDocument;
import org.eclipse.lsp4xml.dom.DOMElement;
import org.eclipse.lsp4xml.dom.DOMNode;
import org.eclipse.lsp4xml.extensions.contentmodel.utils.XMLGenerator;
import org.eclipse.lsp4xml.services.extensions.ICompletionRequest;
import org.eclipse.lsp4xml.services.extensions.ICompletionResponse;
import org.eclipse.lsp4xml.services.extensions.IHoverRequest;
Expand Down Expand Up @@ -64,6 +60,8 @@ public class XSISchemaModel {
" <!-- ... --> " + lineSeparator +
"</root> " + lineSeparator +
"```" ;
public static final String XSI_WEBSITE = "http://www.w3.org/2001/XMLSchema-instance";
public static final String XSI_DOC = "The namespace that defines important attributes such as `noNamespaceSchemaLocation` and `schemaLocation`.";
public static void computeCompletionResponses(ICompletionRequest request,
ICompletionResponse response, Range editRange, DOMDocument document, boolean generateValue) throws BadLocationException {

Expand All @@ -74,8 +72,13 @@ public static void computeCompletionResponses(ICompletionRequest request,
if(rootElement.equals(nodeAtOffset)) {
inRootElement = true;
}

boolean isSnippetsSupported = request.getCompletionSettings().isCompletionSnippetsSupported();
if(inRootElement && document.hasSchemaInstancePrefix() == false) {
createCompletionItem("xmlns:xsi", isSnippetsSupported, generateValue, editRange, XSI_WEBSITE, null, XSI_DOC, response);
return;
}

String actualPrefix = document.getSchemaInstancePrefix();
String name;
String documentation;
Expand All @@ -100,14 +103,16 @@ public static void computeCompletionResponses(ICompletionRequest request,
}
//The xsi:schemaLocation and xsi:noNamespaceSchemaLocation attributes can be used in a document
//to provide hints as to the physical location of schema documents which may be used for ·assessment·.
if(inRootElement && !schemaLocationExists && !noNamespaceSchemaLocationExists) {
documentation = SCHEMA_LOCATION_DOC;
name = actualPrefix + ":schemaLocation";
createCompletionItem(name, isSnippetsSupported, generateValue, editRange, null, null, documentation, response);

documentation = NO_NAMESPACE_SCHEMA_LOCATION_DOC;
name = actualPrefix + ":noNamespaceSchemaLocation";
createCompletionItem(name, isSnippetsSupported, generateValue, editRange, null, null, documentation, response);
if(inRootElement) {
if(!schemaLocationExists && !noNamespaceSchemaLocationExists) {
documentation = SCHEMA_LOCATION_DOC;
name = actualPrefix + ":schemaLocation";
createCompletionItem(name, isSnippetsSupported, generateValue, editRange, null, null, documentation, response);

documentation = NO_NAMESPACE_SCHEMA_LOCATION_DOC;
name = actualPrefix + ":noNamespaceSchemaLocation";
createCompletionItem(name, isSnippetsSupported, generateValue, editRange, null, null, documentation, response);
}
}
}

Expand All @@ -129,12 +134,21 @@ public static void computeValueCompletionResponses(ICompletionRequest request,
int offset = document.offsetAt(editRange.getStart());
DOMElement nodeAtOffset = (DOMElement) document.findNodeAt(offset);


String actualPrefix = document.getSchemaInstancePrefix();
DOMAttr attrAtOffset = nodeAtOffset.findAttrAt(offset);
String attrName = attrAtOffset.getName();

// Value completion for 'nil' attribute
DOMAttr nilAttr = nodeAtOffset.getAttributeNode(actualPrefix, "nil");
if(nilAttr != null) {
createCompletionItemsForValues(StringUtils.TRUE_FALSE_ARRAY, editRange, document, response, settings);
if(attrName != null) {
if(attrName.equals(actualPrefix + ":nil")) { // Value completion for 'nil' attribute
createCompletionItemsForValues(StringUtils.TRUE_FALSE_ARRAY, editRange, document, response, settings);
}
else if(document.getDocumentElement().equals(nodeAtOffset)) { // if in the root element
if(attrName.equals("xmlns:xsi")) {
createSingleCompletionItemForValue(XSI_WEBSITE, editRange, document, response, settings);
}
}

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,15 +452,27 @@ public void xsiCompletionNotUsingXSIName() throws BadLocationException {
}

@Test
public void xsiCompletionDoesntAppearSinceDoesntExist() throws BadLocationException {
public void xmlnsXSICompletion() throws BadLocationException {
String xml =
"<project\r\n" +
" xmlns=\"http://maven.apache.org/POM/4.0.0\"\r\n" +
" xsi:|>\r\n" +
" <modelVersion></modelVersion>\r\n" +
"</project>";

XMLAssert.testCompletionFor(xml, 0);
XMLAssert.testCompletionFor(xml, 1, c("xmlns:xsi", "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""));
}

@Test
public void xmlnsXSIValueCompletion() throws BadLocationException {
String xml =
"<project\r\n" +
" xmlns=\"http://maven.apache.org/POM/4.0.0\"\r\n" +
" xmlns:xsi=|>\r\n" +
" <modelVersion></modelVersion>\r\n" +
"</project>";

XMLAssert.testCompletionFor(xml, 1, c("http://www.w3.org/2001/XMLSchema-instance", "\"http://www.w3.org/2001/XMLSchema-instance\""));
}

@Test
Expand Down

0 comments on commit 6dc967b

Please sign in to comment.