From ec839f23bf9e5ef591e72eb2f991e8fa437d23ab Mon Sep 17 00:00:00 2001 From: Aiday Marlen Kyzy Date: Tue, 21 Nov 2023 15:08:54 +0100 Subject: [PATCH] reverting changes in the typescript extension --- .../src/languageFeatures/documentSymbol.ts | 4 +- .../src/languageFeatures/refactor.ts | 80 ++----------------- 2 files changed, 10 insertions(+), 74 deletions(-) diff --git a/extensions/typescript-language-features/src/languageFeatures/documentSymbol.ts b/extensions/typescript-language-features/src/languageFeatures/documentSymbol.ts index cc924f59897..ef2d47ef942 100644 --- a/extensions/typescript-language-features/src/languageFeatures/documentSymbol.ts +++ b/extensions/typescript-language-features/src/languageFeatures/documentSymbol.ts @@ -12,7 +12,7 @@ import * as PConst from '../tsServer/protocol/protocol.const'; import * as typeConverters from '../typeConverters'; import { ITypeScriptServiceClient } from '../typescriptService'; -export const getSymbolKind = (kind: string): vscode.SymbolKind => { +const getSymbolKind = (kind: string): vscode.SymbolKind => { switch (kind) { case PConst.Kind.module: return vscode.SymbolKind.Module; case PConst.Kind.class: return vscode.SymbolKind.Class; @@ -33,7 +33,7 @@ export const getSymbolKind = (kind: string): vscode.SymbolKind => { return vscode.SymbolKind.Variable; }; -export class TypeScriptDocumentSymbolProvider implements vscode.DocumentSymbolProvider { +class TypeScriptDocumentSymbolProvider implements vscode.DocumentSymbolProvider { public constructor( private readonly client: ITypeScriptServiceClient, diff --git a/extensions/typescript-language-features/src/languageFeatures/refactor.ts b/extensions/typescript-language-features/src/languageFeatures/refactor.ts index 3d682902ff2..8fa071892f7 100644 --- a/extensions/typescript-language-features/src/languageFeatures/refactor.ts +++ b/extensions/typescript-language-features/src/languageFeatures/refactor.ts @@ -21,7 +21,6 @@ import { nulToken } from '../utils/cancellation'; import FormattingOptionsManager from './fileConfigurationManager'; import { conditionalRegistration, requireSomeCapability } from './util/dependentRegistration'; import { EditorChatFollowUp, EditorChatFollowUp_Args, CompositeCommand } from './util/copilot'; -import { getSymbolKind } from './documentSymbol'; function toWorkspaceEdit(client: ITypeScriptServiceClient, edits: readonly Proto.FileCodeEdits[]): vscode.WorkspaceEdit { const workspaceEdit = new vscode.WorkspaceEdit(); @@ -436,48 +435,6 @@ class MoveToFileCodeAction extends vscode.CodeAction { arguments: [{ action, document, range }] }; } - - private static readonly _scopesOfInterest = [ - vscode.SymbolKind.File, - vscode.SymbolKind.Module, - vscode.SymbolKind.Namespace, - vscode.SymbolKind.Package, - vscode.SymbolKind.Class, - vscode.SymbolKind.Interface, - ]; - - private static _findSmallestNavTreeContaining( - navigationTree: Proto.NavigationTree, - range: vscode.Range - ): Proto.NavigationTree { - const childTrees = navigationTree.childItems; - if (!childTrees) { - return navigationTree; - } - for (const childTree of childTrees) { - if (MoveToFileCodeAction._navTreeContainsRange(childTree, range) && MoveToFileCodeAction._scopesOfInterest.includes(getSymbolKind(childTree.kind))) { - return this._findSmallestNavTreeContaining(childTree, range); - } - } - return navigationTree; - } - - private static _navTreeContainsRange(navigationTree: Proto.NavigationTree, range: vscode.Range): boolean { - return navigationTree.spans.some(span => typeConverters.Range.fromTextSpan(span).contains(range)); - } - - public static shouldIncludeMoveToAction( - navigationTree: Proto.NavigationTree | undefined, - range: vscode.Range - ): boolean { - if (!navigationTree || !MoveToFileCodeAction._navTreeContainsRange(navigationTree, range)) { - return false; - } - const smallestScopeContaining = MoveToFileCodeAction._findSmallestNavTreeContaining(navigationTree, range); - return !!(smallestScopeContaining - && smallestScopeContaining.spans[0].start.line - 1 === range.start.line - && smallestScopeContaining.spans[smallestScopeContaining.spans.length - 1].end.line - 1 === range.end.line); - } } class SelectCodeAction extends vscode.CodeAction { @@ -559,8 +516,7 @@ class TypeScriptRefactorProvider implements vscode.CodeActionProvider { + const actions = Array.from(this.convertApplicableRefactors(document, response.body, rangeOrSelection)).filter(action => { if (this.client.apiVersion.lt(API.v430)) { // Don't show 'infer return type' refactoring unless it has been explicitly requested // https://github.com/microsoft/TypeScript/issues/42993 @@ -568,15 +524,8 @@ class TypeScriptRefactorProvider implements vscode.CodeActionProvider applicableRefactors.filter((_, index) => mappedRefactors[index])); + }); if (!context.only) { return actions; @@ -598,44 +547,31 @@ class TypeScriptRefactorProvider implements vscode.CodeActionProvider> { - const actions: Array = []; + ): Iterable { for (const refactor of refactors) { if (refactor.inlineable === false) { - actions.push(new SelectCodeAction(refactor, document, rangeOrSelection)); + yield new SelectCodeAction(refactor, document, rangeOrSelection); } else { for (const action of refactor.actions) { - const refactorAction = await this.refactorActionToCodeAction(document, refactor, action, rangeOrSelection, refactor.actions); - if (refactorAction) { - actions.push(refactorAction); - } + yield this.refactorActionToCodeAction(document, refactor, action, rangeOrSelection, refactor.actions); } } } - return actions; } - private async refactorActionToCodeAction( + private refactorActionToCodeAction( document: vscode.TextDocument, refactor: Proto.ApplicableRefactorInfo, action: Proto.RefactorActionInfo, rangeOrSelection: vscode.Range | vscode.Selection, allActions: readonly Proto.RefactorActionInfo[], - ): Promise { + ): TsCodeAction { let codeAction: TsCodeAction; if (action.name === 'Move to file') { - const navigationTree = await this.client.execute('navtree', { file: document.uri.path }, nulToken); - if (navigationTree.type !== 'response') { - return; - } - const shouldIncludeMoveToAction = MoveToFileCodeAction.shouldIncludeMoveToAction(navigationTree.body, rangeOrSelection); - if (!shouldIncludeMoveToAction) { - return; - } codeAction = new MoveToFileCodeAction(document, action, rangeOrSelection); } else { let copilotRename: ((info: Proto.RefactorEditInfo) => vscode.Command) | undefined;