Skip to content
Prev Previous commit
Next Next commit
Rename things
  • Loading branch information
Andy Hanson committed Nov 30, 2017
commit be19542dd650d12c811f9bea4af438fbe1eb1856
10 changes: 5 additions & 5 deletions src/harness/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2425,10 +2425,10 @@ Actual: ${stringify(fullActual)}`);
}

public verifyCodeFixAll(options: FourSlashInterface.VerifyCodeFixAllOptions): void {
const { groupId, newFileContent } = options;
const groupIds = ts.mapDefined(this.getCodeFixes(this.activeFile.fileName), a => a.groupId);
ts.Debug.assert(ts.contains(groupIds, groupId), "No available code fix has that group id.", () => `Expected '${groupId}'. Available group ids: ${groupIds}`);
const { changes, commands } = this.languageService.getCombinedCodeFix(this.activeFile.fileName, groupId, this.formatCodeSettings);
const { actionId, newFileContent } = options;
const actionIds = ts.mapDefined(this.getCodeFixes(this.activeFile.fileName), a => a.actionId);
ts.Debug.assert(ts.contains(actionIds, actionId), "No available code fix has that group id.", () => `Expected '${actionId}'. Available action ids: ${actionIds}`);
const { changes, commands } = this.languageService.getCombinedCodeFix(this.activeFile.fileName, actionId, this.formatCodeSettings);
assert.deepEqual(commands, options.commands);
this.applyChanges(changes);
this.verifyCurrentFileContent(newFileContent);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will this always be currentFile. Api call returns TextChangeRange which has file name per change.. So wouldnt it need to verify all those files?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added an assertion that all changes are for the current file and a TODO for if we need to test changes affecting multiple files.

Expand Down Expand Up @@ -4595,7 +4595,7 @@ namespace FourSlashInterface {
}

export interface VerifyCodeFixAllOptions {
groupId: string;
actionId: string;
newFileContent: string;
commands: ReadonlyArray<{}>;
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ namespace ts.server {
const request = this.processRequest<protocol.CodeFixRequest>(CommandNames.GetCodeFixes, args);
const response = this.processResponse<protocol.CodeFixResponse>(request);

return response.body.map(({ description, changes, groupId }) => ({ description, changes: this.convertChanges(changes, file), groupId }));
return response.body.map(({ description, changes, actionId }) => ({ description, changes: this.convertChanges(changes, file), actionId }));
}

getCombinedCodeFix = notImplemented;
Expand Down
14 changes: 9 additions & 5 deletions src/server/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ namespace ts.server.protocol {
GetCodeFixesFull = "getCodeFixes-full",
GetCombinedCodeFix = "getCombinedCodeFix",
/* @internal */
GetCombinedCodeFixFull = "getCombinedCodeFix",
GetCombinedCodeFixFull = "getCombinedCodeFix-full",
ApplyCodeActionCommand = "applyCodeActionCommand",
GetSupportedCodeFixes = "getSupportedCodeFixes",

Expand Down Expand Up @@ -541,6 +541,10 @@ namespace ts.server.protocol {
arguments: GetCombinedCodeFixRequestArgs;
}

export interface GetCombinedCodeFixResponse extends Response {
body: CodeActionAll;
}

export interface ApplyCodeActionCommandRequest extends Request {
command: CommandTypes.ApplyCodeActionCommand;
arguments: ApplyCodeActionCommandRequestArgs;
Expand Down Expand Up @@ -594,7 +598,7 @@ namespace ts.server.protocol {
}

export interface GetCombinedCodeFixRequestArgs extends FileRequestArgs {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a GetCombinedCodeFixResponse definition

groupId: {};
actionId: {};
}

export interface ApplyCodeActionCommandRequestArgs {
Expand Down Expand Up @@ -1594,12 +1598,12 @@ namespace ts.server.protocol {

export interface CodeActionAll {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CombinedCodeActions?

changes: FileCodeEdits[];
commands: {}[] | undefined;
commands?: {}[];
}

export interface CodeFix extends CodeAction {
/** If present, one may call 'getAllCodeFixesInGroup' with this groupId. */
groupId: {} | undefined;
/** If present, one may call 'getAllCodeFixesInGroup' with this actionId. */
actionId?: {};
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/server/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1570,7 +1570,7 @@ namespace ts.server {
private getCombinedCodeFix(args: protocol.GetCombinedCodeFixRequestArgs, simplifiedResult: boolean): protocol.CodeActionAll | CodeActionAll {
const { file, project } = this.getFileAndProject(args);
const formatOptions = this.projectService.getFormatCodeOptions(file);
const res = project.getLanguageService().getCombinedCodeFix(file, args.groupId, formatOptions);
const res = project.getLanguageService().getCombinedCodeFix(file, args.actionId, formatOptions);
if (simplifiedResult) {
return { changes: this.mapTextChangesToCodeEdits(project, res.changes), commands: res.commands };
}
Expand Down
14 changes: 7 additions & 7 deletions src/services/codeFixProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ namespace ts {
export interface CodeFixRegistration {
errorCodes: number[];
getCodeActions(context: CodeFixContext): CodeFix[] | undefined;
groupIds: string[];
fixAllInGroup(context: CodeFixAllContext): CodeActionAll;
actionIds: string[];
getAllCodeActions(context: CodeFixAllContext): CodeActionAll;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should make this one optional

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or make CodeFixRegisraction a union type

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not all codeFixes need to provide a list of applicable actionIds, and not all of them need to provide a getAllCodeActions feature and not all of them need to return an actionId in their output.

}

export interface CodeFixContextBase extends textChanges.TextChangesContext {
Expand All @@ -15,7 +15,7 @@ namespace ts {
}

export interface CodeFixAllContext extends CodeFixContextBase {
groupId: {};
actionId: {};
}

export interface CodeFixContext extends CodeFixContextBase {
Expand All @@ -36,8 +36,8 @@ namespace ts {
}
fixes.push(codeFix);
}
if (codeFix.groupIds) {
for (const gid of codeFix.groupIds) {
if (codeFix.actionIds) {
for (const gid of codeFix.actionIds) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gid is out of date

Debug.assert(!groups.has(gid));
groups.set(gid, codeFix);
}
Expand Down Expand Up @@ -70,8 +70,8 @@ namespace ts {
}

export function getAllFixes(context: CodeFixAllContext): CodeActionAll {
// Currently groupId is always a string.
return groups.get(cast(context.groupId, isString)).fixAllInGroup!(context);
// Currently actionId is always a string.
return groups.get(cast(context.actionId, isString)).getAllCodeActions!(context);
}

function createCodeActionAll(changes: FileTextChanges[], commands?: CodeActionCommand[]): CodeActionAll {
Expand Down
8 changes: 4 additions & 4 deletions src/services/codefixes/addMissingInvocationForDecorator.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/* @internal */
namespace ts.codefix {
const groupId = "addMissingInvocationForDecorator";
const actionId = "addMissingInvocationForDecorator";
const errorCodes = [Diagnostics._0_accepts_too_few_arguments_to_be_used_as_a_decorator_here_Did_you_mean_to_call_it_first_and_write_0.code];
registerCodeFix({
errorCodes,
getCodeActions: (context) => {
const changes = textChanges.ChangeTracker.with(context, t => makeChange(t, context.sourceFile, context.span.start));
return [{ description: getLocaleSpecificMessage(Diagnostics.Call_decorator_expression), changes, groupId }];
return [{ description: getLocaleSpecificMessage(Diagnostics.Call_decorator_expression), changes, actionId }];
},
groupIds: [groupId],
fixAllInGroup: context => codeFixAll(context, errorCodes, (changes, diag) => makeChange(changes, diag.file!, diag.start!)),
actionIds: [actionId],
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => makeChange(changes, diag.file!, diag.start!)),
});

function makeChange(changeTracker: textChanges.ChangeTracker, sourceFile: SourceFile, pos: number) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* @internal */
namespace ts.codefix {
const groupId = "correctQualifiedNameToIndexedAccessType";
const actionId = "correctQualifiedNameToIndexedAccessType";
const errorCodes = [Diagnostics.Cannot_access_0_1_because_0_is_a_type_but_not_a_namespace_Did_you_mean_to_retrieve_the_type_of_the_property_1_in_0_with_0_1.code];
registerCodeFix({
errorCodes,
Expand All @@ -9,10 +9,10 @@ namespace ts.codefix {
if (!qualifiedName) return undefined;
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, context.sourceFile, qualifiedName));
const description = formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Rewrite_as_the_indexed_access_type_0), [`${qualifiedName.left.text}["${qualifiedName.right.text}"]`]);
return [{ description, changes, groupId }];
return [{ description, changes, actionId }];
},
groupIds: [groupId],
fixAllInGroup: (context) => codeFixAll(context, errorCodes, (changes, diag) => {
actionIds: [actionId],
getAllCodeActions: (context) => codeFixAll(context, errorCodes, (changes, diag) => {
const q = getQualifiedName(diag.file, diag.start);
if (q) {
doChange(changes, diag.file, q);
Expand Down
12 changes: 6 additions & 6 deletions src/services/codefixes/disableJsDiagnostics.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* @internal */
namespace ts.codefix {
const groupId = "disableJsDiagnostics";
const actionId = "disableJsDiagnostics";
const errorCodes = mapDefined(Object.keys(Diagnostics), key => {
const diag = (Diagnostics as MapLike<DiagnosticMessage>)[key];
return diag.category === DiagnosticCategory.Error ? diag.code : undefined;
Expand All @@ -18,7 +18,7 @@ namespace ts.codefix {
return [{
description: getLocaleSpecificMessage(Diagnostics.Ignore_this_error_message),
changes: [createFileTextChanges(sourceFile.fileName, [getIgnoreCommentLocationForLocation(sourceFile, span.start, newLineCharacter)])],
groupId,
actionId,
},
{
description: getLocaleSpecificMessage(Diagnostics.Disable_checking_for_this_file),
Expand All @@ -29,12 +29,12 @@ namespace ts.codefix {
},
newText: `// @ts-nocheck${newLineCharacter}`
}])],
// groupId unnecessary because adding `// @ts-nocheck` even once will ignore every error in the file.
groupId: undefined,
// actionId unnecessary because adding `// @ts-nocheck` even once will ignore every error in the file.
actionId: undefined,
}];
},
groupIds: [groupId], // No point applying as a group, doing it once will fix all errors
fixAllInGroup: context => codeFixAllWithTextChanges(context, errorCodes, (changes, err) => {
actionIds: [actionId], // No point applying as a group, doing it once will fix all errors
getAllCodeActions: context => codeFixAllWithTextChanges(context, errorCodes, (changes, err) => {
if (err.start !== undefined) {
changes.push(getIgnoreCommentLocationForLocation(err.file!, err.start, context.newLineCharacter));
}
Expand Down
16 changes: 8 additions & 8 deletions src/services/codefixes/fixAddMissingMember.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace ts.codefix {
Diagnostics.Property_0_does_not_exist_on_type_1.code,
Diagnostics.Property_0_does_not_exist_on_type_1_Did_you_mean_2.code,
];
const groupId = "addMissingMember";
const actionId = "addMissingMember";
registerCodeFix({
errorCodes,
getCodeActions(context) {
Expand All @@ -17,8 +17,8 @@ namespace ts.codefix {
getActionsForAddMissingMemberInTypeScriptFile(context, classDeclarationSourceFile, classOpenBrace, token, classDeclaration, makeStatic);
return concatenate(singleElementArray(methodCodeAction), addMember);
},
groupIds: [groupId],
fixAllInGroup: context => {
actionIds: [actionId],
getAllCodeActions: context => {
const seenNames = createMap<true>();
return codeFixAll(context, errorCodes, (changes, diag) => {
const { newLineCharacter, program } = context;
Expand Down Expand Up @@ -100,7 +100,7 @@ namespace ts.codefix {
const changes = textChanges.ChangeTracker.with(context, t => addMissingMemberInJs(t, classDeclarationSourceFile, classDeclaration, tokenName, makeStatic, context.newLineCharacter));
if (changes.length === 0) return undefined;
const description = formatStringFromArgs(getLocaleSpecificMessage(makeStatic ? Diagnostics.Initialize_static_property_0 : Diagnostics.Initialize_property_0_in_the_constructor), [tokenName]);
return { description, changes, groupId };
return { description, changes, actionId };
}

function addMissingMemberInJs(changeTracker: textChanges.ChangeTracker, classDeclarationSourceFile: SourceFile, classDeclaration: ClassLikeDeclaration, tokenName: string, makeStatic: boolean, newLineCharacter: string): void {
Expand Down Expand Up @@ -146,7 +146,7 @@ namespace ts.codefix {
function createAddPropertyDeclarationAction(context: CodeFixContext, classDeclarationSourceFile: SourceFile, classOpenBrace: Node, makeStatic: boolean, tokenName: string, typeNode: TypeNode): CodeFix {
const description = formatStringFromArgs(getLocaleSpecificMessage(makeStatic ? Diagnostics.Declare_static_property_0 : Diagnostics.Declare_property_0), [tokenName]);
const changes = textChanges.ChangeTracker.with(context, t => addPropertyDeclaration(t, classDeclarationSourceFile, classOpenBrace, tokenName, typeNode, makeStatic, context.newLineCharacter));
return { description, changes, groupId };
return { description, changes, actionId };
}

function addPropertyDeclaration(changeTracker: textChanges.ChangeTracker, classDeclarationSourceFile: SourceFile, classOpenBrace: Node, tokenName: string, typeNode: TypeNode, makeStatic: boolean, newLineCharacter: string): void {
Expand Down Expand Up @@ -178,14 +178,14 @@ namespace ts.codefix {
typeNode);

const changes = textChanges.ChangeTracker.with(context, t => t.insertNodeAfter(classDeclarationSourceFile, classOpenBrace, indexSignature, { suffix: context.newLineCharacter }));
// No groupId here because code-fix-all currently only works on adding individual named properties.
return { description: formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Add_index_signature_for_property_0), [tokenName]), changes, groupId: undefined };
// No actionId here because code-fix-all currently only works on adding individual named properties.
return { description: formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Add_index_signature_for_property_0), [tokenName]), changes, actionId: undefined };
}

function getActionForMethodDeclaration(context: CodeFixContext, classDeclarationSourceFile: SourceFile, classOpenBrace: Node, token: Identifier, callExpression: CallExpression, makeStatic: boolean, inJs: boolean): CodeFix | undefined {
const description = formatStringFromArgs(getLocaleSpecificMessage(makeStatic ? Diagnostics.Declare_static_method_0 : Diagnostics.Declare_method_0), [token.text]);
const changes = textChanges.ChangeTracker.with(context, t => addMethodDeclaration(t, classDeclarationSourceFile, classOpenBrace, token, callExpression, context.newLineCharacter, makeStatic, inJs));
return { description, changes, groupId };
return { description, changes, actionId };
}

function addMethodDeclaration(changeTracker: textChanges.ChangeTracker, classDeclarationSourceFile: SourceFile, classOpenBrace: Node, token: Identifier, callExpression: CallExpression, newLineCharacter: string, makeStatic: boolean, inJs: boolean) {
Expand Down
8 changes: 4 additions & 4 deletions src/services/codefixes/fixCannotFindModule.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/* @internal */
namespace ts.codefix {
const groupId = "fixCannotFindModule";
const actionId = "fixCannotFindModule";
const errorCodes = [Diagnostics.Could_not_find_a_declaration_file_for_module_0_1_implicitly_has_an_any_type.code];
registerCodeFix({
errorCodes,
getCodeActions: context => [
{ groupId, ...tryGetCodeActionForInstallPackageTypes(context.host, context.sourceFile.fileName, getModuleName(context.sourceFile, context.span.start)) }
{ actionId, ...tryGetCodeActionForInstallPackageTypes(context.host, context.sourceFile.fileName, getModuleName(context.sourceFile, context.span.start)) }
],
groupIds: [groupId],
fixAllInGroup: context => codeFixAll(context, errorCodes, (_, diag, commands) => {
actionIds: [actionId],
getAllCodeActions: context => codeFixAll(context, errorCodes, (_, diag, commands) => {
const pkg = getTypesPackageNameToInstall(context.host, getModuleName(diag.file, diag.start));
if (pkg) {
commands.push(getCommand(diag.file.fileName, pkg));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ namespace ts.codefix {
Diagnostics.Non_abstract_class_0_does_not_implement_inherited_abstract_member_1_from_class_2.code,
Diagnostics.Non_abstract_class_expression_does_not_implement_inherited_abstract_member_0_from_class_1.code,
];
const groupId = "fixClassDoesntImplementInheritedAbstractMember";
const actionId = "fixClassDoesntImplementInheritedAbstractMember";
registerCodeFix({
errorCodes,
getCodeActions(context) {
const { program, sourceFile, span } = context;
const changes = textChanges.ChangeTracker.with(context, t =>
addMissingMembers(getClass(sourceFile, span.start), sourceFile, program.getTypeChecker(), context.newLineCharacter, t));
return changes.length === 0 ? undefined : [{ description: getLocaleSpecificMessage(Diagnostics.Implement_inherited_abstract_class), changes, groupId }];
return changes.length === 0 ? undefined : [{ description: getLocaleSpecificMessage(Diagnostics.Implement_inherited_abstract_class), changes, actionId }];
},
groupIds: [groupId],
fixAllInGroup: context => codeFixAll(context, errorCodes, (changes, diag) => {
actionIds: [actionId],
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => {
addMissingMembers(getClass(diag.file!, diag.start!), context.sourceFile, context.program.getTypeChecker(), context.newLineCharacter, changes);
}),
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* @internal */
namespace ts.codefix {
const errorCodes = [Diagnostics.Class_0_incorrectly_implements_interface_1.code];
const groupId = "fixClassIncorrectlyImplementsInterface"; // TODO: share a group with fixClassDoesntImplementInheritedAbstractMember?
const actionId = "fixClassIncorrectlyImplementsInterface"; // TODO: share a group with fixClassDoesntImplementInheritedAbstractMember?
registerCodeFix({
errorCodes,
getCodeActions(context) {
Expand All @@ -12,11 +12,11 @@ namespace ts.codefix {
const changes = textChanges.ChangeTracker.with(context, t => addMissingDeclarations(checker, implementedTypeNode, sourceFile, classDeclaration, newLineCharacter, t));
if (changes.length === 0) return undefined;
const description = formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Implement_interface_0), [implementedTypeNode.getText()]);
return { description, changes, groupId };
return { description, changes, actionId };
});
},
groupIds: [groupId],
fixAllInGroup(context) {
actionIds: [actionId],
getAllCodeActions(context) {
const seenClassDeclarations: true[] = [];
return codeFixAll(context, errorCodes, (changes, diag) => {
const classDeclaration = getClass(diag.file!, diag.start!);
Expand Down
Loading