Skip to content

Commit ff2dc0d

Browse files
Copilotanthonykim1
andauthored
Fix microsoft#258695 -> Python REPL suggestions stopping after typing '(' character (microsoft#258831)
* Initial plan * Fix Python REPL suggestions after typing '(' character Co-authored-by: anthonykim1 <[email protected]> * format * cleaner code --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: anthonykim1 <[email protected]> Co-authored-by: Anthony Kim <[email protected]>
1 parent de84d24 commit ff2dc0d

File tree

1 file changed

+41
-28
lines changed

1 file changed

+41
-28
lines changed

src/vs/workbench/contrib/terminalContrib/suggest/browser/lspCompletionProviderAddon.ts

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export class LspCompletionProviderAddon extends Disposable implements ITerminalA
3131
this._provider = provider;
3232
this._textVirtualModel = textVirtualModel;
3333
this._lspTerminalModelContentProvider = lspTerminalModelContentProvider;
34-
this.triggerCharacters = provider.triggerCharacters ? [...provider.triggerCharacters, ' '] : [' '];
34+
this.triggerCharacters = provider.triggerCharacters ? [...provider.triggerCharacters, ' ', '('] : [' ', '('];
3535
}
3636

3737
activate(terminal: Terminal): void {
@@ -83,34 +83,47 @@ export class LspCompletionProviderAddon extends Disposable implements ITerminalA
8383
}
8484
}
8585

86-
export function createCompletionItemPython(cursorPosition: number, prefix: string, kind: TerminalCompletionItemKind, label: string | CompletionItemLabel, detail: string | undefined): TerminalCompletionItem {
87-
const endsWithDot = prefix.endsWith('.');
88-
const endsWithSpace = prefix.endsWith(' ');
89-
90-
if (endsWithSpace) {
91-
// Case where user is triggering completion with space:
92-
// For example, typing `import ` to request completion for list of modules
93-
// This is similar to completions we are used to seeing in upstream shell (such as typing `ls ` inside bash).
94-
const lastWord = endsWithSpace ? '' : prefix.split(' ').at(-1) ?? '';
95-
return {
96-
label: label,
97-
detail: detail ?? detail ?? '',
98-
replacementIndex: cursorPosition - lastWord.length,
99-
replacementLength: lastWord.length,
100-
kind: kind ?? kind ?? TerminalCompletionItemKind.Method
101-
};
102-
} else {
103-
// Case where user is triggering completion with dot:
104-
// For example, typing `pathlib.` to request completion for list of methods, attributes from the pathlib module.
105-
const lastWord = endsWithDot ? '' : prefix.split('.').at(-1) ?? '';
106-
return {
107-
label,
108-
detail: detail ?? detail ?? '',
109-
replacementIndex: cursorPosition - lastWord.length,
110-
replacementLength: lastWord.length,
111-
kind: kind ?? kind ?? TerminalCompletionItemKind.Method
112-
};
86+
export function createCompletionItemPython(
87+
cursorPosition: number,
88+
prefix: string,
89+
kind: TerminalCompletionItemKind,
90+
label: string | CompletionItemLabel,
91+
detail: string | undefined
92+
): TerminalCompletionItem {
93+
const lastWord = getLastWord(prefix);
94+
95+
return {
96+
label,
97+
detail: detail ?? '',
98+
replacementIndex: cursorPosition - lastWord.length,
99+
replacementLength: lastWord.length,
100+
kind: kind ?? TerminalCompletionItemKind.Method
101+
};
102+
}
103+
104+
function getLastWord(prefix: string): string {
105+
if (prefix.endsWith(' ')) {
106+
return '';
113107
}
108+
109+
if (prefix.endsWith('.')) {
110+
return '';
111+
}
112+
113+
const lastSpaceIndex = prefix.lastIndexOf(' ');
114+
const lastDotIndex = prefix.lastIndexOf('.');
115+
const lastParenIndex = prefix.lastIndexOf('(');
116+
117+
// Get the maximum index (most recent delimiter)
118+
const lastDelimiterIndex = Math.max(lastSpaceIndex, lastDotIndex, lastParenIndex);
119+
120+
// If no delimiter found, return the entire prefix
121+
if (lastDelimiterIndex === -1) {
122+
return prefix;
123+
}
124+
125+
// Return the substring after the last delimiter
126+
return prefix.substring(lastDelimiterIndex + 1);
114127
}
115128

116129
export interface TerminalCompletionItem {

0 commit comments

Comments
 (0)