Skip to content

Commit

Permalink
Extension completion should use node text range
Browse files Browse the repository at this point in the history
Fix eclipse-lemminx#723

Signed-off-by: Andrew Obuchowicz <[email protected]>
  • Loading branch information
AObuchow committed Aug 11, 2020
1 parent 2aad97f commit f74c30e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lemminx.dom.DOMElement;
import org.eclipse.lemminx.dom.DOMNode;
import org.eclipse.lemminx.dom.DOMText;
import org.eclipse.lemminx.dom.DTDDeclParameter;
import org.eclipse.lemminx.dom.parser.Scanner;
import org.eclipse.lemminx.dom.parser.ScannerState;
Expand Down Expand Up @@ -753,6 +754,18 @@ private void collectInsideContent(CompletionRequest request, CompletionResponse
collectCloseTagSuggestions(tagNameRange, true, true, false, request, response);
}
// Participant completion on XML content
if (request.getNode() instanceof DOMElement) {
List<DOMNode> children = ((DOMElement) request.getNode()).getChildren();
if (!children.isEmpty() && children.get(0) instanceof DOMText) {
Range textRange = XMLPositionUtility.selectText((DOMText) children.get(0));
request.setReplaceRange(textRange);
}
}
if (request.getNode() instanceof DOMText) {
Range textRange = XMLPositionUtility.selectText((DOMText) request.getNode());
request.setReplaceRange(textRange);
}

for (ICompletionParticipant participant : getCompletionParticipants()) {
try {
participant.onXMLContent(request, response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.eclipse.lsp4j.CompletionItem;
import org.eclipse.lsp4j.CompletionItemKind;
import org.eclipse.lsp4j.InsertTextFormat;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.TextEdit;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
Expand Down Expand Up @@ -118,6 +119,19 @@ public void testHTMLAttributeNameCompletion() throws BadLocationException {
public void testHTMLAttributeValueCompletion() throws BadLocationException {
testCompletionFor("<input type=|", c("text", "\"text\""/* "<input type=\"text\"" */), //
c("checkbox", "\"checkbox\"" /* "<input type=\"checkbox\"" */));

// TODO: Move below to testHTMLOnXMLContentCompletion()
CompletionItem completion = new CompletionItem("Test replace range");
Position start = new Position(0, 7);
Position end = new Position(0, 26);
TextEdit edit = new TextEdit(new Range(start, end), "replacement text");
completion.setTextEdit(edit);
testCompletionFor("<input>some extisti|ng text</input>", completion );
}

@Test
private void testHTMLOnXMLContentCompletion() throws BadLocationException {
// TODO: This isin't getting called by Junit
}

public static void testCompletionFor(String value, CompletionItem... expectedItems) throws BadLocationException {
Expand Down Expand Up @@ -178,6 +192,16 @@ public void onAttributeName(boolean generateValue, ICompletionRequest completion
}
}
}

@Override
public void onXMLContent(ICompletionRequest request, ICompletionResponse response) {
CompletionItem completion = new CompletionItem("Test replace range");
TextEdit edit = new TextEdit();
edit.setNewText("replacement text");
edit.setRange(request.getReplaceRange());
completion.setTextEdit(edit);
response.addCompletionItem(completion);
}

@Override
public void onAttributeValue(String valuePrefix,
Expand Down

0 comments on commit f74c30e

Please sign in to comment.