Skip to content

Commit

Permalink
Completion for XML Prolog Improved
Browse files Browse the repository at this point in the history
Fixes eclipse#85

Signed-off-by: Nikolas Komonen <[email protected]>
  • Loading branch information
NikolasKomonen committed Oct 9, 2018
1 parent 6bd29e7 commit d2a25d6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,34 @@ public CompletionList doComplete(XMLDocument xmlDocument, Position position, Com
return completionResponse;
}
break;
case PrologName:
if (offset <= scanner.getTokenEnd()) {
collectPrologSuggestion(scanner.getTokenEnd(), scanner.getTokenText(), completionRequest,
completionResponse);
return completionResponse;
case StartPrologOrPI: {
try {
boolean isFirstNode;
isFirstNode = xmlDocument.positionAt(scanner.getTokenOffset()).getLine() == 0;
if (isFirstNode && offset <= scanner.getTokenEnd()) {
collectPrologSuggestion(scanner.getTokenEnd(), scanner.getTokenText(), completionRequest,
completionResponse, false);
return completionResponse;
}
} catch (BadLocationException e) {
LOGGER.log(Level.SEVERE, "In XMLCompletions, StartPrologOrPI position error", e);
}
break;
}
case PrologName: {
try {
boolean isFirstNode;
isFirstNode = xmlDocument.positionAt(scanner.getTokenOffset()).getLine() == 0;
if (isFirstNode && offset <= scanner.getTokenEnd()) {
collectPrologSuggestion(scanner.getTokenEnd(), scanner.getTokenText(), completionRequest,
completionResponse, true);
return completionResponse;
}
} catch (BadLocationException e) {
LOGGER.log(Level.SEVERE, "In XMLCompletions, PrologName position error", e);
}
break;
}
default:
if (offset <= scanner.getTokenEnd()) {
return completionResponse;
Expand Down Expand Up @@ -364,20 +386,23 @@ private void collectOpenTagSuggestions(boolean hasOpenBracket, Range replaceRang
* @param response
*/
private void collectPrologSuggestion(int tagNameEnd, String tag, CompletionRequest request,
CompletionResponse response) {
CompletionResponse response, boolean includesXML) {


String prologName = includesXML ? "" : "xml";
XMLDocument document = request.getXMLDocument();
CompletionItem item = new CompletionItem();
item.setLabel("<?xml ... ?>");
item.setKind(CompletionItemKind.Property);
item.setFilterText("version=\"1.0\" encoding=\"UTF-8\"?>");
item.setFilterText(prologName + " version=\"1.0\" encoding=\"UTF-8\"?>");
item.setInsertTextFormat(InsertTextFormat.Snippet);
int closingBracketOffset = getOffsetFollowedBy(document.getText(), tagNameEnd, ScannerState.WithinTag,
TokenType.StartTagClose);
int end = closingBracketOffset != -1 ? closingBracketOffset - 1 : tagNameEnd;
String closeTag = closingBracketOffset != -1 ? "" : ">";
try {
Range editRange = getReplaceRange(tagNameEnd, end, request);
item.setTextEdit(new TextEdit(editRange, " version=\"1.0\" encoding=\"UTF-8\"?" + closeTag + "$0"));
item.setTextEdit(new TextEdit(editRange, prologName + " version=\"1.0\" encoding=\"UTF-8\"?" + closeTag + "$0"));
} catch (BadLocationException e) {
LOGGER.log(Level.SEVERE, "While performing getReplaceRange for prolog completion.", e);
}
Expand Down Expand Up @@ -683,5 +708,4 @@ private static String getLineIndent(int offset, String text) {
private boolean isEmptyElement(String tag) {
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private ServerCapabilitiesConstants() {
public static final String WORKSPACE_CHANGE_FOLDERS_ID = UUID.randomUUID().toString();
public static final String WORKSPACE_WATCHED_FILES_ID = UUID.randomUUID().toString();

public static final CompletionOptions DEFAULT_COMPLETION_OPTIONS = new CompletionOptions(false, Arrays.asList(".", ":", "<", "\"", "=", "/"));
public static final CompletionOptions DEFAULT_COMPLETION_OPTIONS = new CompletionOptions(false, Arrays.asList(".", ":", "<", "\"", "=", "/", "?"));
public static final TextDocumentSyncKind DEFAULT_SYNC_OPTION = TextDocumentSyncKind.Full;
public static final DocumentLinkOptions DEFAULT_LINK_OPTIONS = new DocumentLinkOptions(true);
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,21 @@ public void testAutoCloseEnabledDisabled() throws BadLocationException {
}

@Test
public void testAutoCompletionProlog() throws BadLocationException {
public void testAutoCompletionPrologWithXML() throws BadLocationException {
//With 'xml' label
testCompletionFor("<?xml|", false, c("<?xml ... ?>", " version=\"1.0\" encoding=\"UTF-8\"?>$0", r(0, 5, 0, 5),
"version=\"1.0\" encoding=\"UTF-8\"?>"));
" version=\"1.0\" encoding=\"UTF-8\"?>"));
testCompletionFor("<?xml|>", true, c("<?xml ... ?>", " version=\"1.0\" encoding=\"UTF-8\"?$0", r(0, 5, 0, 5),
"version=\"1.0\" encoding=\"UTF-8\"?>"));
" version=\"1.0\" encoding=\"UTF-8\"?>"));
}

@Test
public void testAutoCompletionPrologWithoutXML() throws BadLocationException {
//No 'xml' label
testCompletionFor("<?|", false, c("<?xml ... ?>", "xml version=\"1.0\" encoding=\"UTF-8\"?>$0", r(0, 2, 0, 2),
"xml version=\"1.0\" encoding=\"UTF-8\"?>"));
testCompletionFor("<?|>", true, c("<?xml ... ?>", "xml version=\"1.0\" encoding=\"UTF-8\"?$0", r(0, 2, 0, 2),
"xml version=\"1.0\" encoding=\"UTF-8\"?>"));
}

// -------------------Tools----------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
import org.eclipse.lsp4j.DocumentHighlight;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4xml.commons.BadLocationException;
import org.eclipse.lsp4xml.dom.Element;
import org.eclipse.lsp4xml.dom.Node;
import org.eclipse.lsp4xml.dom.XMLDocument;
import org.eclipse.lsp4xml.dom.XMLParser;
import org.junit.Assert;
Expand Down

0 comments on commit d2a25d6

Please sign in to comment.