Skip to content

Commit

Permalink
Indication in description of completion item that includes
Browse files Browse the repository at this point in the history
the file name of the schema they come from.

Fixes eclipse-lemminx#210

Signed-off-by: Nikolas Komonen <[email protected]>
  • Loading branch information
NikolasKomonen committed May 27, 2019
1 parent b69c38e commit 82c7ea6
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ public interface CMDocument {
*/
CMElementDeclaration findCMElement(DOMElement element, String namespace);

String getURI();

String setURI(String uri);

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*/
package org.eclipse.lsp4xml.extensions.contentmodel.participants;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;

import org.eclipse.lsp4j.CompletionItem;
Expand Down Expand Up @@ -42,26 +44,34 @@ public class ContentModelCompletionParticipant extends CompletionParticipantAdap
public void onTagOpen(ICompletionRequest request, ICompletionResponse response) throws Exception {
try {
DOMDocument document = request.getXMLDocument();
String schemaURI;
String fileURI;
ContentModelManager contentModelManager = request.getComponent(ContentModelManager.class);
DOMElement parentElement = request.getParentElement();
CMDocument cmDocument;
if (parentElement == null) {
// XML is empty, in case of XML file associations, a XMl Schema/DTD can be bound
// check if it's root element (in the case of XML file associations, the link to
// XML Schema is done with pattern and not with XML root element)
CMDocument cmDocument = contentModelManager.findCMDocument(document, null);
cmDocument = contentModelManager.findCMDocument(document, null);
if (cmDocument != null) {
fillWithChildrenElementDeclaration(null, cmDocument.getElements(), null, false, request, response);
schemaURI = cmDocument.getURI();
fillWithChildrenElementDeclaration(null, cmDocument.getElements(), null, false, request, response, schemaURI);
}
return;
}
// Try to retrieve XML Schema/DTD element declaration for the parent element
// where completion was triggered.
cmDocument = contentModelManager.findCMDocument(parentElement, parentElement.getNamespaceURI());

schemaURI = cmDocument != null ? cmDocument.getURI() : null;
CMElementDeclaration cmElement = contentModelManager.findCMElement(parentElement);
String defaultPrefix = null;

if (cmElement != null) {
defaultPrefix = parentElement.getPrefix();
fillWithChildrenElementDeclaration(parentElement, cmElement.getElements(), defaultPrefix, false,
request, response);
request, response, schemaURI);
}
if (parentElement.isDocumentElement()) {
// root document element
Expand All @@ -71,10 +81,10 @@ public void onTagOpen(ICompletionRequest request, ICompletionResponse response)
continue;
}
String namespaceURI = parentElement.getNamespaceURI(prefix);
CMDocument cmDocument = contentModelManager.findCMDocument(parentElement, namespaceURI);
cmDocument = contentModelManager.findCMDocument(parentElement, namespaceURI);
if (cmDocument != null) {
fillWithChildrenElementDeclaration(parentElement, cmDocument.getElements(), prefix, true,
request, response);
request, response, cmDocument.getURI());
}
}
}
Expand All @@ -84,25 +94,51 @@ public void onTagOpen(ICompletionRequest request, ICompletionResponse response)
if (cmInternalElement != null) {
defaultPrefix = parentElement.getPrefix();
fillWithChildrenElementDeclaration(parentElement, cmInternalElement.getElements(), defaultPrefix, false,
request, response);
request, response, schemaURI);
}
} catch (CacheResourceDownloadingException e) {
// XML Schema, DTD is loading, ignore this error
}
}

private void fillWithChildrenElementDeclaration(DOMElement element, Collection<CMElementDeclaration> cmElements,
String p, boolean forceUseOfPrefix, ICompletionRequest request, ICompletionResponse response)
String p, boolean forceUseOfPrefix, ICompletionRequest request, ICompletionResponse response, String schemaURI)
throws BadLocationException {
XMLGenerator generator = request.getXMLGenerator();
for (CMElementDeclaration child : cmElements) {
String prefix = forceUseOfPrefix ? p : (element != null ? element.getPrefix(child.getNamespace()) : null);
String label = child.getName(prefix);
String fileTypeLabel = "";
// if(schemaURI != null) {
// if(schemaURI.endsWith("xsd")) {
// fileTypeLabel = " - XSD";
// }
// else if (schemaURI.endsWith("dtd")) {
// fileTypeLabel = " - DTD";
// }
// }
String label = child.getName(prefix) + fileTypeLabel;
CompletionItem item = new CompletionItem(label);
item.setFilterText(request.getFilterForStartTagName(label));
item.setKind(CompletionItemKind.Property);
String documentation = child.getDocumentation();
if (documentation != null) {
StringBuilder sb = new StringBuilder();
String documentation;
String tempDoc = child.getDocumentation();
boolean tempDocHasContent = tempDoc != null && !tempDoc.isEmpty();
if(tempDocHasContent) {
sb.append(tempDoc);
}

if(schemaURI != null) {
if(tempDocHasContent) {
String lineSeparator = System.getProperty("line.separator");
sb.append(lineSeparator + lineSeparator);
}
Path schemaPath = Paths.get(schemaURI);
sb.append("File: ");
sb.append(schemaPath.getFileName().toString());
}
documentation = sb.toString();
if (documentation != null && !documentation.isEmpty()) {
item.setDetail(documentation);
}
String xml = generator.generate(child, prefix);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public String getSystemId(DOMDocument xmlDocument, String namespaceURI) {
public CMDocument createCMDocument(String key) {
try {
CMDTDDocument document = new CMDTDDocument();
document.setURI(key);
document.setEntityResolver(resolverExtensionManager);
Grammar grammar = document.loadGrammar(new XMLInputSource(null, key, null));
if (grammar != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class CMDTDDocument extends XMLDTDLoader implements CMDocument {
private List<CMElementDeclaration> elements;
private DTDGrammar grammar;
private List<String> hierachies;
private String uri;


@Override
public Collection<CMElementDeclaration> getElements() {
Expand All @@ -58,6 +60,20 @@ public Collection<CMElementDeclaration> getElements() {
return elements;
}

@Override
/**
* Returns the URI of this document, is none was provided this
* returns null.
*/
public String getURI() {
return uri;
}

@Override
public String setURI(String uri) {
return this.uri;
}

@Override
public CMElementDeclaration findCMElement(DOMElement element, String namespace) {
List<DOMElement> paths = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ public CMDocument createCMDocument(String key) {
XSModel model = getLoader().loadURI(key);
if (model != null) {
// XML Schema can be loaded
return new CMXSDDocument(model);
CMXSDDocument document = new CMXSDDocument(model);
document.setURI(key);
return document;
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,27 @@ public class CMXSDDocument implements CMDocument {

private Collection<CMElementDeclaration> elements;

private String uri;

public CMXSDDocument(XSModel model) {
this.model = model;
this.elementMappings = new HashMap<>();
}

@Override
public String getURI() {
return uri;
}

@Override
/**
* Returns the URI of this document, is none was provided this
* returns null.
*/
public String setURI(String uri) {
return this.uri = uri;
}

@Override
public Collection<CMElementDeclaration> getElements() {
if (elements == null) {
Expand Down

0 comments on commit 82c7ea6

Please sign in to comment.