forked from eclipse-lemminx/lemminx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
XSI attributes are provided as completion items
Fixes eclipse-lemminx#163 Signed-off-by: Nikolas Komonen <nikolaskomonen@gmail.com>
- Loading branch information
1 parent
0b15ba1
commit f198ac9
Showing
4 changed files
with
183 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/services/XSISchemaModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* Copyright (c) 2018 Red Hat, Inc. and others. | ||
* 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: | ||
* Red Hat Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.lsp4xml.services; | ||
|
||
import org.eclipse.lsp4j.CompletionItem; | ||
import org.eclipse.lsp4j.CompletionItemKind; | ||
import org.eclipse.lsp4j.InsertTextFormat; | ||
import org.eclipse.lsp4j.Range; | ||
import org.eclipse.lsp4j.TextEdit; | ||
import org.eclipse.lsp4xml.dom.Element; | ||
import org.eclipse.lsp4xml.dom.XMLDocument; | ||
import org.eclipse.lsp4xml.services.extensions.ICompletionRequest; | ||
import org.eclipse.lsp4xml.services.extensions.ICompletionResponse; | ||
|
||
/** | ||
* This class holds values that represent the XSI xsd. | ||
* Can be seen at https://www.w3.org/2001/XMLSchema-instance | ||
*/ | ||
public class XSISchemaModel { | ||
|
||
public static void computeCompletionResponses(ICompletionRequest request, | ||
ICompletionResponse response, Range editRange, XMLDocument document) { | ||
String snippet = ""; | ||
boolean isSnippetsSupported = request.getCompletionSettings().isCompletionSnippetsSupported(); | ||
if(request.getCompletionSettings().isCompletionSnippetsSupported()) { | ||
snippet = "$0"; | ||
} | ||
String actualPrefix = document.getSchemaInstancePrefix(); | ||
String name; | ||
Element root = document.getDocumentElement(); | ||
boolean schemaLocationExists = document.hasSchemaLocation(); | ||
boolean noNamespaceSchemaLocationExists = document.hasNoNamespaceSchemaLocation(); | ||
//Indicates that no values are allowed inside an XML element | ||
if(!attributeAlreadyExists(root, actualPrefix, "nil")) { | ||
name = actualPrefix + ":nil"; | ||
String nilSnippet = "true" + snippet; | ||
createCompletionItem(name, isSnippetsSupported, nilSnippet, editRange, response); | ||
} | ||
//Signals that an element should be accepted as ·valid· when it has no content despite | ||
//a content type which does not require or even necessarily allow empty content. | ||
//An element may be ·valid· without content if it has the attribute xsi:nil with | ||
//the value true. | ||
if(!attributeAlreadyExists(root, actualPrefix, "type")) { | ||
name = actualPrefix + ":type"; | ||
createCompletionItem(name, isSnippetsSupported, snippet, editRange, response); | ||
} | ||
//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(!schemaLocationExists && !noNamespaceSchemaLocationExists) { | ||
name = actualPrefix + ":schemaLocation"; | ||
createCompletionItem(name, isSnippetsSupported, snippet, editRange, response); | ||
|
||
name = actualPrefix + ":noNamespaceSchemaLocation"; | ||
createCompletionItem(name, isSnippetsSupported, snippet, editRange, response); } | ||
} | ||
|
||
|
||
private static void createCompletionItem(String text, boolean isCompletionSnippetsSupported, | ||
String defaultAndSnippet, Range editRange, ICompletionResponse response) { | ||
CompletionItem item = new CompletionItem(); | ||
item.setLabel(text); | ||
item.setKind(CompletionItemKind.Value); | ||
item.setFilterText(text); | ||
item.setInsertTextFormat(isCompletionSnippetsSupported ? InsertTextFormat.Snippet : InsertTextFormat.PlainText); | ||
item.setTextEdit(new TextEdit(editRange, text + "=\"" + defaultAndSnippet + "\"")); | ||
response.addCompletionItem(item); | ||
} | ||
|
||
private static boolean attributeAlreadyExists(Element root, String actualPrefix, String suffix) { | ||
return root.getAttributeNode(actualPrefix + ":" + suffix) != null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters