Skip to content
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

Reverting changes in TypeScript language features extension #198858

Merged
merged 1 commit into from
Nov 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 * as PConst from '../tsServer/protocol/protocol.const';

function toWorkspaceEdit(client: ITypeScriptServiceClient, edits: readonly Proto.FileCodeEdits[]): vscode.WorkspaceEdit {
const workspaceEdit = new vscode.WorkspaceEdit();
Expand Down Expand Up @@ -436,30 +435,6 @@ class MoveToFileCodeAction extends vscode.CodeAction {
arguments: [<MoveToFileRefactorCommand.Args>{ action, document, range }]
};
}

private static readonly _declarationKinds = new Set([
PConst.Kind.module,
PConst.Kind.class,
PConst.Kind.interface
]);

static isOnDeclarationName(node: Proto.NavigationTree | undefined, range: vscode.Range) {
if (!node) {
return false;
}
const isRangeInSpan = (span: Proto.TextSpan) => typeConverters.Range.fromTextSpan(span).contains(range);
if (MoveToFileCodeAction._declarationKinds.has(node.kind) && node.nameSpan && isRangeInSpan(node.nameSpan)) {
return true;
}
if (node.childItems && node.spans.some(isRangeInSpan)) {
for (const child of node.childItems) {
if (MoveToFileCodeAction.isOnDeclarationName(child, range)) {
return true;
}
}
}
return false;
}
}

class SelectCodeAction extends vscode.CodeAction {
Expand Down Expand Up @@ -541,24 +516,16 @@ class TypeScriptRefactorProvider implements vscode.CodeActionProvider<TsCodeActi
return undefined;
}

const applicableRefactors = await this.convertApplicableRefactors(document, response.body, rangeOrSelection);
const actions = await Promise.all(applicableRefactors.map(async (action) => {
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
if (!context.only && action.kind?.value === 'refactor.rewrite.function.returnType') {
return false;
}
}
if (action.kind?.value === Move_NewFile.kind.value) {
const navigationTree = await this.client.execute('navtree', { file: document.uri.path }, nulToken);
if (navigationTree.type !== 'response') {
return;
}
return MoveToFileCodeAction.isOnDeclarationName(navigationTree.body, rangeOrSelection);
}
return true;
})).then((mappedRefactors) => applicableRefactors.filter((_, index) => mappedRefactors[index]));
});

if (!context.only) {
return actions;
Expand All @@ -580,44 +547,31 @@ class TypeScriptRefactorProvider implements vscode.CodeActionProvider<TsCodeActi
return context.triggerKind === vscode.CodeActionTriggerKind.Invoke ? 'invoked' : 'implicit';
}

private async convertApplicableRefactors(
private *convertApplicableRefactors(
document: vscode.TextDocument,
refactors: readonly Proto.ApplicableRefactorInfo[],
rangeOrSelection: vscode.Range | vscode.Selection
): Promise<Array<TsCodeAction>> {
const actions: Array<TsCodeAction> = [];
): Iterable<TsCodeAction> {
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 | undefined> {
): 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.isOnDeclarationName(navigationTree.body, rangeOrSelection);
if (!shouldIncludeMoveToAction) {
return;
}
codeAction = new MoveToFileCodeAction(document, action, rangeOrSelection);
} else {
let copilotRename: ((info: Proto.RefactorEditInfo) => vscode.Command) | undefined;
Expand Down