Skip to content

Commit

Permalink
Trigger chat variable completion on word start (#224174)
Browse files Browse the repository at this point in the history
* And to `computeCompletionRanges` support for allowing completions only on word start

* use word start column instead of cursor position

* Fix wrong column number
  • Loading branch information
bsShoham authored Aug 1, 2024
1 parent 3af2def commit deecca1
Showing 1 changed file with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ class BuiltinDynamicCompletions extends Disposable {
return null;
}

const range = computeCompletionRanges(model, position, BuiltinDynamicCompletions.VariableNameDef);
const range = computeCompletionRanges(model, position, BuiltinDynamicCompletions.VariableNameDef, true);
if (!range) {
return null;
}
Expand All @@ -344,12 +344,19 @@ class BuiltinDynamicCompletions extends Disposable {

Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench).registerWorkbenchContribution(BuiltinDynamicCompletions, LifecyclePhase.Eventually);

function computeCompletionRanges(model: ITextModel, position: Position, reg: RegExp): { insert: Range; replace: Range; varWord: IWordAtPosition | null } | undefined {
function computeCompletionRanges(model: ITextModel, position: Position, reg: RegExp, onlyOnWordStart = false): { insert: Range; replace: Range; varWord: IWordAtPosition | null } | undefined {
const varWord = getWordAtText(position.column, reg, model.getLineContent(position.lineNumber), 0);
if (!varWord && model.getWordUntilPosition(position).word) {
// inside a "normal" word
return;
}
if (varWord && onlyOnWordStart) {
const wordBefore = model.getWordUntilPosition({ lineNumber: position.lineNumber, column: varWord.startColumn });
if (wordBefore.word) {
// inside a word
return;
}
}

let insert: Range;
let replace: Range;
Expand Down Expand Up @@ -394,7 +401,7 @@ class VariableCompletions extends Disposable {
return null;
}

const range = computeCompletionRanges(model, position, VariableCompletions.VariableNameDef);
const range = computeCompletionRanges(model, position, VariableCompletions.VariableNameDef, true);
if (!range) {
return null;
}
Expand Down

0 comments on commit deecca1

Please sign in to comment.