Skip to content

Commit

Permalink
xsd:enumeration autocomplete don't work for text node
Browse files Browse the repository at this point in the history
  • Loading branch information
angelozerr committed Apr 10, 2020
1 parent 565e869 commit 6e81dba
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2018 Angelo ZERR
* Copyright (c) 2018-2020 Angelo ZERR
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand All @@ -13,6 +13,7 @@
package org.eclipse.lemminx.extensions.contentmodel.participants;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

Expand All @@ -35,6 +36,7 @@
import org.eclipse.lsp4j.CompletionItemKind;
import org.eclipse.lsp4j.InsertTextFormat;
import org.eclipse.lsp4j.MarkupContent;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.TextEdit;
import org.w3c.dom.Document;
Expand Down Expand Up @@ -334,4 +336,54 @@ private void fillAttributeValuesWithCMAttributeDeclarations(CMElementDeclaration
}
}

@Override
public void onXMLContent(ICompletionRequest request, ICompletionResponse response) throws Exception {
try {
ContentModelManager contentModelManager = request.getComponent(ContentModelManager.class);
DOMElement parentElement = request.getParentElement();
if (parentElement != null) {
CMElementDeclaration elementDeclaration = contentModelManager.findCMElement(parentElement);
Collection<String> values = elementDeclaration != null ? elementDeclaration.getEnumerationValues()
: Collections.emptyList();
if (!values.isEmpty()) {
DOMDocument document = parentElement.getOwnerDocument();
int startOffset = parentElement.getStartTagCloseOffset() + 1;
Position start = parentElement.getOwnerDocument().positionAt(startOffset);
Position end = request.getPosition();
int endOffset = parentElement.getEndTagOpenOffset();
if (endOffset > 0) {
end = document.positionAt(endOffset);
}
int completionOffset = request.getOffset();
StringBuilder tok = new StringBuilder();
for (int i = startOffset; i < completionOffset; i++) {
char ch = document.getText().charAt(i);
if (Character.isWhitespace(ch)) {
tok.append(ch);
} else {
break;
}

}
String tokenStart = tok.toString();
Range fullRange = new Range(start, end);
values.forEach(value -> {
CompletionItem item = new CompletionItem();
item.setLabel(value);
String insertText = value; //request.getInsertAttrValue(value);
item.setLabel(value);
item.setKind(CompletionItemKind.Value);
item.setFilterText(tokenStart + insertText);
item.setTextEdit(new TextEdit(fullRange, insertText));
MarkupContent documentation = XMLGenerator.createMarkupContent(elementDeclaration, value,
request);
item.setDocumentation(documentation);
response.addCompletionItem(item);
});
}
}
} catch (CacheResourceDownloadingException e) {
// XML Schema, DTD is loading, ignore this error
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2018 Angelo ZERR
* Copyright (c) 2018-2020 Angelo ZERR
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -323,4 +323,14 @@ public static MarkupContent createMarkupContent(CMAttributeDeclaration cmAttribu
}
return null;
}

public static MarkupContent createMarkupContent(CMElementDeclaration ownerElement, String attributeValue,
IMarkupKindSupport support) {
String documentation = XMLGenerator.generateDocumentation("TODO", ownerElement.getDocumentURI(),
support.canSupportMarkupKind(MarkupKind.MARKDOWN));
if (documentation != null) {
return MarkupContentFactory.createMarkupContent(documentation, MarkupKind.MARKDOWN, support);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,14 @@ public boolean isEmpty() {
@Override
public Collection<String> getEnumerationValues() {
XSTypeDefinition typeDefinition = elementDeclaration.getTypeDefinition();
if (typeDefinition != null && typeDefinition.getTypeCategory() == XSTypeDefinition.SIMPLE_TYPE) {
return CMXSDDocument.getEnumerationValues((XSSimpleTypeDefinition) typeDefinition);
if (typeDefinition != null) {
XSSimpleTypeDefinition simpleDefinition = null;
if (typeDefinition.getTypeCategory() == XSTypeDefinition.SIMPLE_TYPE) {
simpleDefinition = (XSSimpleTypeDefinition) typeDefinition;
} else if (typeDefinition.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE) {
simpleDefinition = ((XSComplexTypeDefinition) typeDefinition).getSimpleType();
}
return CMXSDDocument.getEnumerationValues(simpleDefinition);
}
return Collections.emptyList();
}
Expand Down

0 comments on commit 6e81dba

Please sign in to comment.