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

Pvb/codeaction/api #10185

Merged
merged 29 commits into from
Oct 12, 2016
Merged

Pvb/codeaction/api #10185

merged 29 commits into from
Oct 12, 2016

Conversation

paulvanbrenk
Copy link
Contributor

Add CodeAction API

@@ -115,6 +115,15 @@ namespace ts {
return -1;
}

export function firstOrUndefined<T>(array: T[], predicate: (x: T) => boolean): T {
Copy link
Contributor

Choose a reason for hiding this comment

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

forEach

Copy link
Member

Choose a reason for hiding this comment

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

forEach is slightly different. Also this is usually called find.

const fixes = codeFixes[context.errorCode];
let allActions: CodeAction[] = [];

Debug.assert(fixes && fixes.length > 0, "No fixes found for error: '${errorCode}'.");
Copy link
Contributor

Choose a reason for hiding this comment

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

isn't it too strict?

const superCall = findSuperCall((<ConstructorDeclaration>constructor).body);
Debug.assert(!!superCall, "Failed to find super call.");

const newPosition = getOpenBraceEnd(<ConstructorDeclaration>constructor, sourceFile);
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add a test for the case where this is an argument to super

super(this.x);

@mhegazy
Copy link
Contributor

mhegazy commented Sep 1, 2016

Please add this to the tsserver, you will need to define a new request and response kinds in server\protocol.d.ts, and add a handler for it in server\session.ts

@@ -0,0 +1,3 @@
///<reference path='..\services.ts' />
Copy link
Contributor

Choose a reason for hiding this comment

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

we stoped using references for a while. they should be picked up by the tsconfig.json in this folder any ways. so please remove this.

@@ -204,6 +204,53 @@ declare namespace ts.server.protocol {
}

/**
* Request for the available codefixed at a specific position.
Copy link
Contributor

Choose a reason for hiding this comment

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

nit Code Fixes instead of codefixed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

textChanges: CodeEdit[];
}

export interface CodeActionResponse extends Response {
Copy link
Contributor

Choose a reason for hiding this comment

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

CodeFixResponse

Copy link
Contributor Author

@paulvanbrenk paulvanbrenk Oct 7, 2016

Choose a reason for hiding this comment

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

done

/** Description of the code action to display in the UI of the editor */
description: string;
/** Text changes to apply to each file as part of the code action */
changes: FileCodeEdits[];
Copy link
Contributor

Choose a reason for hiding this comment

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

CodeAction [] ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep

/**
* Changes to apply to each file as part of the code action.
*/
changes: FileTextChanges[];
Copy link
Contributor

Choose a reason for hiding this comment

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

CodeEdit [] instead.

Copy link
Contributor

Choose a reason for hiding this comment

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

see formatting for example.

textChanges: TextChange[];
}

export class TextChange {
Copy link
Contributor

Choose a reason for hiding this comment

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

not needed.

description: source.description,
changes: source.changes.map(change => ({
fileName: change.fileName,
textChanges: change.textChanges.map(textChange => ({
Copy link
Contributor

Choose a reason for hiding this comment

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

extract the common part between here and getFormattingEditsForRange in convertTextChangeToCodeEdit

@@ -630,6 +630,10 @@ namespace ts.server {
throw new Error("Not Implemented Yet.");
}

getCodeFixesAtPosition(fileName: string, start: number, end: number, errorCodes: number[]): ts.CodeAction[] {
throw new Error("Not Implemented Yet.");
Copy link
Contributor

Choose a reason for hiding this comment

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

why? add implementation here, and add a test in tests\cases\fourslash\server

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@@ -251,6 +251,8 @@ namespace ts {
*/
isValidBraceCompletionAtPosition(fileName: string, position: number, openingBrace: number): string;

getCodeFixesAtPosition(fileName: string, start: number, end: number, errorCodes: string): string;
Copy link
Contributor

Choose a reason for hiding this comment

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

we do not need to update the shims, since we moved to tsserver

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed

newLineCharacter: newLineChar
};

const fixes = codefix.getFixes(context);
Copy link
Contributor

Choose a reason for hiding this comment

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

check the cancellation token for every iteration.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

namespace ts {
export interface CodeFix {
errorCodes: number[];
getCodeActions(context: CodeFixContext): CodeAction[];
Copy link
Contributor

Choose a reason for hiding this comment

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

getCodeActions(context: CodeFixContext): CodeAction[] | undefined to allow strictNullChecks to work for users implementing it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@@ -1524,4 +1592,16 @@ declare namespace ts.server.protocol {
export interface NavBarResponse extends Response {
body?: NavigationBarItem[];
}

export interface CodeAction {
Copy link
Contributor

Choose a reason for hiding this comment

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

this is already defined above.

@@ -134,6 +135,9 @@ namespace ts.server {
export const NameOrDottedNameSpan = "nameOrDottedNameSpan";
export const BreakpointStatement = "breakpointStatement";
export const CompilerOptionsForInferredProjects = "compilerOptionsForInferredProjects";
export const GetCodeFixes = "getCodeFixes";
export const GetCodeFixesFull = "getCodeFixes-full";
export const GetSupportedCodeFixes = "getSupportedCodeFixes";
Copy link
Contributor

Choose a reason for hiding this comment

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

i do not see the definition of the request for this command in protocol.d.ts

Copy link
Contributor

Choose a reason for hiding this comment

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

can you also add a test for it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are no arguments for the command so no need for a request

@paulvanbrenk paulvanbrenk merged commit 9f73ae5 into master Oct 12, 2016
@paulvanbrenk paulvanbrenk deleted the pvb/codeaction/api branch October 12, 2016 02:51
});
}

export function getSupportedErrorCodes() {
Copy link

Choose a reason for hiding this comment

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

Any reason this returns a string[] and not a number[]? Error codes are always numbers, so you could map this with Number.parseInt.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, while we currently only return numbers for errorcodes, in the future e.g. a linter extension could prepend the errorcodes they return with a string. That way they can be differentiated and handled by separate 'fixers', even when they have the same error number.

Copy link

Choose a reason for hiding this comment

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

Maybe the definitions of errorCode and errorCodes above (in CodeFixContent and CodeFix) should use string then?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You make a good point, I'll update that.

@vvakame vvakame mentioned this pull request Nov 10, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants