Skip to content

Commit

Permalink
Fixed wrong autocompletion behavior with user typing part of an external
Browse files Browse the repository at this point in the history
autocompletion proposal after generating and before accepting it
  • Loading branch information
jploski committed Jul 29, 2023
1 parent 3a31ca4 commit 3030872
Showing 1 changed file with 26 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,6 @@ public PositionBasedCompletionProposal(
}

public void apply(IDocument document) {
try {
document.replace(
fReplacementPosition.getOffset(),
fReplacementPosition.getLength(),
fReplacementString);
} catch (BadLocationException x) {
// ignore
}
}

public Point getSelection(IDocument document) {
Expand All @@ -113,7 +105,32 @@ public IContextInformation getContextInformation() {

@Override
public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
apply(viewer.getDocument());
try {
// We have to deal here with a (typical) situation in which the user has triggered
// autocompletion and then continued to type in a prefix of the proposal (to narrow
// down the proposals list) before accepting a proposal by hitting Enter.
// In this case the replacement length returned by our helper script, which is only aware
// of what was in the document at trigger time, is not valid. Rather, we must instead
// replace the entire length of the proposal's prefix, including those characters
// typed in between generating and accepting the autocompletion proposal.

IDocument doc = viewer.getDocument();
String prefix = doc.get(fReplacementPosition.getOffset(), offset - fReplacementPosition.getOffset());
if (prefix.length() > 0 && fReplacementString.startsWith(prefix)) {
viewer.getDocument().replace(
fReplacementPosition.getOffset(),
prefix.length(),
fReplacementString);
}
else {
viewer.getDocument().replace(
fReplacementPosition.getOffset(),
fReplacementPosition.getLength(),
fReplacementString);
}
} catch (BadLocationException x) {
// ignore
}
}

@Override
Expand Down

0 comments on commit 3030872

Please sign in to comment.