Skip to content

Commit bec14a1

Browse files
authored
fix: LSCompletionProposal#getContextInformationPosition (#1345)
For some odd reason `LSCompletionProposal.getContextInformationPosition` currently returns `SWT.RIGHT` hardcoded which translates to int value `131072`. I changed the information to return the beginning of the text edit range.
1 parent 49cdacb commit bec14a1

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/completion/CompleteCompletionTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,4 +576,28 @@ public void testCancellation() throws Exception {
576576
CompletableFuture.runAsync(() -> contentAssistProcessor.computeCompletionProposals(viewer, 1));
577577
DisplayHelper.waitAndAssertCondition(viewer.getTextWidget().getDisplay(), () -> assertEquals(1, MockConnectionProvider.cancellations.size()));
578578
}
579+
580+
/**
581+
* Verifies context information position returned by LSCompletionProposal uses
582+
* the start of the edit range when available.
583+
*/
584+
@Test
585+
public void testContextInformationPositionUsesEditRangeStart() throws CoreException {
586+
final int editOffset = 2;
587+
CompletionItem completionItem = createCompletionItem( //
588+
"hello", //
589+
CompletionItemKind.Text, //
590+
new Range(new Position(0, editOffset), new Position(0, editOffset + 1)));
591+
MockLanguageServer.INSTANCE.setCompletionList(new CompletionList(true, List.of(completionItem)));
592+
593+
ITextViewer viewer = TestUtils.openTextViewer(TestUtils.createUniqueTestFile(project, "ABCDE"));
594+
595+
// Invoke completion at the start of the TextEdit range (offset 2)
596+
ICompletionProposal[] proposals = contentAssistProcessor.computeCompletionProposals(viewer, editOffset);
597+
assertEquals(1, proposals.length);
598+
599+
int pos = ((LSCompletionProposal) proposals[0]).getContextInformationPosition();
600+
// TextEdit starts at (0,2) so offset should be 0
601+
assertEquals(editOffset, pos);
602+
}
579603
}

org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/completion/LSCompletionProposal.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@
8383
import org.eclipse.lsp4j.ServerCapabilities;
8484
import org.eclipse.lsp4j.TextEdit;
8585
import org.eclipse.lsp4j.jsonrpc.messages.Either;
86-
import org.eclipse.swt.SWT;
8786
import org.eclipse.swt.graphics.Image;
8887
import org.eclipse.swt.graphics.Point;
8988
import org.eclipse.swt.graphics.TextStyle;
@@ -875,6 +874,11 @@ public void apply(IDocument document) {
875874

876875
@Override
877876
public int getContextInformationPosition() {
878-
return SWT.RIGHT;
877+
try {
878+
return LSPEclipseUtils.toOffset(getTextEditRange().getStart(), document);
879+
} catch (BadLocationException e) {
880+
LanguageServerPlugin.logWarning(e.getMessage(), e);
881+
}
882+
return bestOffset;
879883
}
880884
}

0 commit comments

Comments
 (0)