From 6dc967b05bf2c914232ee337a126a63a0986954c Mon Sep 17 00:00:00 2001 From: Nikolas Date: Thu, 14 Mar 2019 16:02:14 -0400 Subject: [PATCH] Completion for xmlns:xsi Fixes #326 Signed-off-by: Nikolas --- .../xsi/XSICompletionParticipant.java | 4 +- .../lsp4xml/services/XSISchemaModel.java | 48 ++++++++++++------- .../XMLSchemaCompletionExtensionsTest.java | 16 ++++++- 3 files changed, 47 insertions(+), 21 deletions(-) diff --git a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/xsi/XSICompletionParticipant.java b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/xsi/XSICompletionParticipant.java index f9e345fb34..5dfef69845 100644 --- a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/xsi/XSICompletionParticipant.java +++ b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/xsi/XSICompletionParticipant.java @@ -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 diff --git a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/services/XSISchemaModel.java b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/services/XSISchemaModel.java index 9ee7fc7844..065fe1307f 100644 --- a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/services/XSISchemaModel.java +++ b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/services/XSISchemaModel.java @@ -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; @@ -64,6 +60,8 @@ public class XSISchemaModel { " " + lineSeparator + " " + 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 { @@ -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; @@ -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); + } } } @@ -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); + } + } + } } diff --git a/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/contentmodel/XMLSchemaCompletionExtensionsTest.java b/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/contentmodel/XMLSchemaCompletionExtensionsTest.java index 1e8bfc829f..97795b2e97 100644 --- a/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/contentmodel/XMLSchemaCompletionExtensionsTest.java +++ b/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/contentmodel/XMLSchemaCompletionExtensionsTest.java @@ -452,7 +452,7 @@ public void xsiCompletionNotUsingXSIName() throws BadLocationException { } @Test - public void xsiCompletionDoesntAppearSinceDoesntExist() throws BadLocationException { + public void xmlnsXSICompletion() throws BadLocationException { String xml = "\r\n" + ""; - 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 = + "\r\n" + + " \r\n" + + ""; + + XMLAssert.testCompletionFor(xml, 1, c("http://www.w3.org/2001/XMLSchema-instance", "\"http://www.w3.org/2001/XMLSchema-instance\"")); } @Test