diff --git a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/contentmodel/model/CMDocument.java b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/contentmodel/model/CMDocument.java index 7f15d7b933..89ed9d6a58 100644 --- a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/contentmodel/model/CMDocument.java +++ b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/contentmodel/model/CMDocument.java @@ -32,4 +32,8 @@ public interface CMDocument { */ CMElementDeclaration findCMElement(DOMElement element, String namespace); + String getURI(); + + String setURI(String uri); + } diff --git a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/contentmodel/participants/ContentModelCompletionParticipant.java b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/contentmodel/participants/ContentModelCompletionParticipant.java index ddc046895a..efb509c259 100644 --- a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/contentmodel/participants/ContentModelCompletionParticipant.java +++ b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/contentmodel/participants/ContentModelCompletionParticipant.java @@ -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; @@ -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 @@ -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()); } } } @@ -84,7 +94,7 @@ 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 @@ -92,17 +102,43 @@ public void onTagOpen(ICompletionRequest request, ICompletionResponse response) } private void fillWithChildrenElementDeclaration(DOMElement element, Collection 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); diff --git a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/dtd/contentmodel/CMDTDContentModelProvider.java b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/dtd/contentmodel/CMDTDContentModelProvider.java index d8b7710fa9..479942ec68 100644 --- a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/dtd/contentmodel/CMDTDContentModelProvider.java +++ b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/dtd/contentmodel/CMDTDContentModelProvider.java @@ -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) { diff --git a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/dtd/contentmodel/CMDTDDocument.java b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/dtd/contentmodel/CMDTDDocument.java index f0be9c4ca3..b8adfa10bc 100644 --- a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/dtd/contentmodel/CMDTDDocument.java +++ b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/dtd/contentmodel/CMDTDDocument.java @@ -41,6 +41,8 @@ public class CMDTDDocument extends XMLDTDLoader implements CMDocument { private List elements; private DTDGrammar grammar; private List hierachies; + private String uri; + @Override public Collection getElements() { @@ -58,6 +60,20 @@ public Collection 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 paths = new ArrayList<>(); diff --git a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/xsd/contentmodel/CMXSDContentModelProvider.java b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/xsd/contentmodel/CMXSDContentModelProvider.java index f8951dde92..fd735055a0 100644 --- a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/xsd/contentmodel/CMXSDContentModelProvider.java +++ b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/xsd/contentmodel/CMXSDContentModelProvider.java @@ -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; } diff --git a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/xsd/contentmodel/CMXSDDocument.java b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/xsd/contentmodel/CMXSDDocument.java index 5f8ed0232d..33b9776656 100644 --- a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/xsd/contentmodel/CMXSDDocument.java +++ b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/xsd/contentmodel/CMXSDDocument.java @@ -43,11 +43,27 @@ public class CMXSDDocument implements CMDocument { private Collection 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 getElements() { if (elements == null) {