From f74c30e08fc2b698a707bef80b63d29d42bc0ad9 Mon Sep 17 00:00:00 2001 From: Andrew Obuchowicz Date: Fri, 7 Aug 2020 12:22:55 -0400 Subject: [PATCH] Extension completion should use node text range Fix #723 Signed-off-by: Andrew Obuchowicz --- .../lemminx/services/XMLCompletions.java | 13 ++++++++++ .../HTMLCompletionExtensionsTest.java | 24 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/XMLCompletions.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/XMLCompletions.java index 420b6cbd7..e9d2ecff2 100644 --- a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/XMLCompletions.java +++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/XMLCompletions.java @@ -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; @@ -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 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); diff --git a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/extensions/HTMLCompletionExtensionsTest.java b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/extensions/HTMLCompletionExtensionsTest.java index 5528eca96..a1338a0d0 100644 --- a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/extensions/HTMLCompletionExtensionsTest.java +++ b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/extensions/HTMLCompletionExtensionsTest.java @@ -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; @@ -118,6 +119,19 @@ public void testHTMLAttributeNameCompletion() throws BadLocationException { public void testHTMLAttributeValueCompletion() throws BadLocationException { testCompletionFor("some extisti|ng text", 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 { @@ -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,