Skip to content

Start polishing lsp terminal completions #252594

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { Position } from '../../../../../editor/common/core/position.js';
import { CompletionItemLabel, CompletionItemProvider, CompletionTriggerKind } from '../../../../../editor/common/languages.js';
import { LspTerminalModelContentProvider } from './lspTerminalModelContentProvider.js';
import { MarkdownString } from '../../../../../base/common/htmlContent.js';
import { ITerminalInstance } from '../../../terminal/browser/terminal.js';
import { LSP_SUPPORTED_SHELLS } from './lspTerminalUtil.js';

export class LspCompletionProviderAddon extends Disposable implements ITerminalAddon, ITerminalCompletionProvider {
readonly id = 'lsp';
Expand All @@ -21,17 +23,20 @@ export class LspCompletionProviderAddon extends Disposable implements ITerminalA
private _provider: CompletionItemProvider;
private _textVirtualModel: IReference<IResolvedTextEditorModel>;
private _lspTerminalModelContentProvider: LspTerminalModelContentProvider;
private _terminal: ITerminalInstance;

constructor(
provider: CompletionItemProvider,
textVirtualModel: IReference<IResolvedTextEditorModel>,
lspTerminalModelContentProvider: LspTerminalModelContentProvider,
terminal: ITerminalInstance
) {
super();
this._provider = provider;
this._textVirtualModel = textVirtualModel;
this._lspTerminalModelContentProvider = lspTerminalModelContentProvider;
this.triggerCharacters = provider.triggerCharacters ? [...provider.triggerCharacters, ' '] : [' '];
this._terminal = terminal;
}

activate(terminal: Terminal): void {
Expand All @@ -40,8 +45,12 @@ export class LspCompletionProviderAddon extends Disposable implements ITerminalA

async provideCompletions(value: string, cursorPosition: number, allowFallbackCompletions: false, token: CancellationToken): Promise<ITerminalCompletion[] | TerminalCompletionList<ITerminalCompletion> | undefined> {

if (!this._terminal.shellType || !LSP_SUPPORTED_SHELLS.includes(this._terminal.shellType)) {
return undefined;
}

// Apply edit for non-executed current commandline --> Pretend we are typing in the real-document.
this._lspTerminalModelContentProvider.trackPromptInputToVirtualFile(value);
this._lspTerminalModelContentProvider.setCommandLineVirtualFile(value);

const textBeforeCursor = value.substring(0, cursorPosition);
const lines = textBeforeCursor.split('\n');
Expand All @@ -54,22 +63,24 @@ export class LspCompletionProviderAddon extends Disposable implements ITerminalA

// TODO: Scan back to start of nearest word like other providers? Is this needed for `ILanguageFeaturesService`?
const completions: ITerminalCompletion[] = [];
// TODO: Why are we ignoring these? Do we just need to give them a penalty in the completion item?
if (this._provider && this._provider._debugDisplayName !== 'wordbasedCompletions') {

const result = await this._provider.provideCompletionItems(this._textVirtualModel.object.textEditorModel, positionVirtualDocument, { triggerKind: CompletionTriggerKind.TriggerCharacter }, token);

// TODO: Do not use any
completions.push(...(result?.suggestions || []).map((e: any) => {
// TODO: Support more terminalCompletionItemKind for [different LSP providers](https://github.com/microsoft/vscode/issues/249479)
const convertedKind = e.kind ? mapLspKindToTerminalKind(e.kind) : TerminalCompletionItemKind.Method;
const completionItemTemp = createCompletionItemPython(cursorPosition, textBeforeCursor, convertedKind, 'lspCompletionItem', undefined);
const { replacementIndex, replacementLength } = createCompletionItemPython(cursorPosition, textBeforeCursor, convertedKind, 'lspCompletionItem', undefined);

return {
label: e.insertText,
provider: `lsp:${this._provider._debugDisplayName}`,
detail: e.detail,
kind: convertedKind,
replacementIndex: completionItemTemp.replacementIndex,
replacementLength: completionItemTemp.replacementLength,
replacementIndex,
replacementLength,
};
}));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,26 @@ export class LspTerminalModelContentProvider extends Disposable implements ILspT
* We want to track the input and update the virtual document.
* Note: This is for non-executed command.
*/
trackPromptInputToVirtualFile(content: string): void {
setCommandLineVirtualFile(content: string): void {
this._commandDetection = this._capabilitiesStore.get(TerminalCapability.CommandDetection);
const model = this._modelService.getModel(this._virtualTerminalDocumentUri);
// TODO: Hardcoded python stuff
if (content !== 'exit()' && this._shellType === GeneralShellType.Python) {
if (model) {
const existingContent = model.getValue();
const delimiterIndex = existingContent.lastIndexOf(VSCODE_LSP_TERMINAL_PROMPT_TRACKER);
if (!model) {
return;
}
const existingContent = model.getValue();
const delimiterIndex = existingContent.lastIndexOf(VSCODE_LSP_TERMINAL_PROMPT_TRACKER);

// Keep content only up to delimiter
const sanitizedExistingContent = delimiterIndex !== -1 ?
existingContent.substring(0, delimiterIndex) :
existingContent;
// Keep content only up to delimiter
const sanitizedExistingContent = delimiterIndex !== -1 ?
existingContent.substring(0, delimiterIndex) :
existingContent;

// Combine base content with new content
const newContent = sanitizedExistingContent + VSCODE_LSP_TERMINAL_PROMPT_TRACKER + content;
// Combine base content with new content
const newContent = sanitizedExistingContent + VSCODE_LSP_TERMINAL_PROMPT_TRACKER + content;

model.setValue(newContent);
}
model.setValue(newContent);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { GeneralShellType, PosixShellType, WindowsShellType } from '../../../../../platform/terminal/common/terminal.js';

export const VSCODE_LSP_TERMINAL_PROMPT_TRACKER = 'vscode_lsp_terminal_prompt_tracker= {}\n';
export const PYLANCE_DEBUG_DISPLAY_NAME = `ms-python.python(.["')`;
export const PYTHON_LANGUAGE_ID = 'python';
export const LSP_SUPPORTED_SHELLS: (GeneralShellType | WindowsShellType | PosixShellType)[] = [GeneralShellType.Python];
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { createTerminalLanguageVirtualUri, LspTerminalModelContentProvider } fro
import { ITextModelService } from '../../../../../editor/common/services/resolverService.js';
import { ILanguageFeaturesService } from '../../../../../editor/common/services/languageFeatures.js';
import { env } from '../../../../../base/common/process.js';
import { PYLANCE_DEBUG_DISPLAY_NAME } from './lspTerminalUtil.js';
import { LSP_SUPPORTED_SHELLS, PYLANCE_DEBUG_DISPLAY_NAME } from './lspTerminalUtil.js';


registerSingleton(ITerminalCompletionService, TerminalCompletionService, InstantiationType.Delayed);
Expand Down Expand Up @@ -160,6 +160,7 @@ class TerminalSuggestContribution extends DisposableStore implements ITerminalCo

// TODO: Eventually support multiple LSP providers for [non-Python REPLs](https://github.com/microsoft/vscode/issues/249479)
private async _loadLspCompletionAddon(xterm: RawXtermTerminal): Promise<void> {
// TODO: It should work in wsl
const isWsl =
isLinux &&
(
Expand All @@ -172,7 +173,7 @@ class TerminalSuggestContribution extends DisposableStore implements ITerminalCo
return;
}

if (this._ctx.instance.shellType !== GeneralShellType.Python) {
if (!this._ctx.instance.shellType || !LSP_SUPPORTED_SHELLS.includes(this._ctx.instance.shellType)) {
this._lspAddon.clear();
return;
}
Expand All @@ -189,17 +190,18 @@ class TerminalSuggestContribution extends DisposableStore implements ITerminalCo
const virtualProviders = this._languageFeaturesService.completionProvider.all(textVirtualModel.object.textEditorModel);
const provider = virtualProviders.find(p => p._debugDisplayName === PYLANCE_DEBUG_DISPLAY_NAME);

if (provider) {
const lspCompletionProviderAddon = this._lspAddon.value = this._instantiationService.createInstance(LspCompletionProviderAddon, provider, textVirtualModel, this._lspModelProvider.value);
xterm.loadAddon(lspCompletionProviderAddon);
this.add(lspCompletionProviderAddon);
this.add(this._terminalCompletionService.registerTerminalCompletionProvider(
'lsp',
lspCompletionProviderAddon.id,
lspCompletionProviderAddon,
...(lspCompletionProviderAddon.triggerCharacters ?? [])
));
if (!provider) {
return;
}
const lspCompletionProviderAddon = this._lspAddon.value = this._instantiationService.createInstance(LspCompletionProviderAddon, provider, textVirtualModel, this._lspModelProvider.value, this._ctx.instance);
xterm.loadAddon(lspCompletionProviderAddon);
this.add(lspCompletionProviderAddon);
this.add(this._terminalCompletionService.registerTerminalCompletionProvider(
'lsp',
lspCompletionProviderAddon.id,
lspCompletionProviderAddon,
...(lspCompletionProviderAddon.triggerCharacters ?? [])
));
}

private _loadAddons(xterm: RawXtermTerminal): void {
Expand Down
Loading