-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Add infrastructure for refactors #14624
Changes from 1 commit
e38b98a
6cbc258
3011d16
fa5a483
b357841
e92d016
675cf03
f108fd7
b86d40a
909b436
0965223
ba6e22c
683a0a0
dfa2506
0aeff72
2e8f6c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -692,6 +692,50 @@ namespace ts.server { | |
return response.body.map(entry => this.convertCodeActions(entry, fileName)); | ||
} | ||
|
||
getRefactorDiagnostics(fileName: string, range?: TextRange): RefactorDiagnostic[] { | ||
const startLineOffset = this.positionToOneBasedLineOffset(fileName, range.pos); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this crash if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here the |
||
const endLineOffset = this.positionToOneBasedLineOffset(fileName, range.end); | ||
|
||
const args: protocol.GetRefactorsForRangeRequestArgs = { | ||
file: fileName, | ||
startLine: startLineOffset.line, | ||
startOffset: startLineOffset.offset, | ||
endLine: endLineOffset.line, | ||
endOffset: endLineOffset.offset, | ||
}; | ||
|
||
const request = this.processRequest<protocol.GetRefactorsForRangeRequest>(CommandNames.GetRefactorsForRange, args); | ||
const response = this.processResponse<protocol.GetRefactorsForRangeResponse>(request); | ||
|
||
return response.body.map(entry => { | ||
return <RefactorDiagnostic>{ | ||
code: entry.code, | ||
end: this.lineOffsetToPosition(fileName, entry.end), | ||
start: this.lineOffsetToPosition(fileName, entry.start), | ||
text: entry.text | ||
}; | ||
}); | ||
} | ||
|
||
getCodeActionsForRefactorAtPosition(fileName: string, range: TextRange, refactorCode: number): CodeAction[] { | ||
const startLineOffset = this.positionToOneBasedLineOffset(fileName, range.pos); | ||
const endLineOffset = this.positionToOneBasedLineOffset(fileName, range.end); | ||
|
||
const args: protocol.GetCodeActionsForRefactorRequestArgs = { | ||
file: fileName, | ||
startLine: startLineOffset.line, | ||
startOffset: startLineOffset.offset, | ||
endLine: endLineOffset.line, | ||
endOffset: endLineOffset.offset, | ||
refactorCode | ||
}; | ||
|
||
const request = this.processRequest<protocol.GetCodeActionsForRefactorRequest>(CommandNames.GetCodeActionsForRefactor, args); | ||
const response = this.processResponse<protocol.GetCodeActionsForRefactorResponse>(request); | ||
|
||
return response.body.map(entry => this.convertCodeActions(entry, fileName)); | ||
} | ||
|
||
convertCodeActions(entry: protocol.CodeAction, fileName: string): CodeAction { | ||
return { | ||
description: entry.description, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -95,6 +95,9 @@ namespace ts.server.protocol { | |
/* @internal */ | ||
export type GetCodeFixesFull = "getCodeFixes-full"; | ||
export type GetSupportedCodeFixes = "getSupportedCodeFixes"; | ||
|
||
export type GetRefactorsForRange = "getRefactorsForRange"; | ||
export type GetCodeActionsForRefactor = "getCodeActionsForRefactor"; | ||
} | ||
|
||
/** | ||
|
@@ -394,6 +397,55 @@ namespace ts.server.protocol { | |
position?: number; | ||
} | ||
|
||
/** | ||
* An diagnostic information suggesting refactors at applicable positions without | ||
* clients asking. | ||
*/ | ||
export interface RefactorDiagnostic { | ||
text: string; | ||
code: number; | ||
start: Location; | ||
end: Location; | ||
} | ||
|
||
export interface RefactorDiagnosticEventBody { | ||
file: string; | ||
diagnostics: RefactorDiagnostic[]; | ||
} | ||
|
||
/** | ||
* Returns a list of applicable refactors at a given position. This request does not actually | ||
* compute the refactors; instead it goes through faster checks to determine what is possible. | ||
*/ | ||
export interface GetRefactorsForRangeRequest extends Request { | ||
command: CommandTypes.GetRefactorsForRange; | ||
arguments: GetRefactorsForRangeRequestArgs; | ||
} | ||
|
||
export interface GetRefactorsForRangeRequestArgs extends TextRangeRequestArgs { | ||
} | ||
|
||
export interface GetRefactorsForRangeResponse extends Response { | ||
body?: RefactorDiagnostic[]; | ||
} | ||
|
||
/** | ||
* Computes the code actions for a given refactor. This is normally called after the user commited one | ||
* of the applicable refactors provided by the "GetApplicableRefactors" API. | ||
*/ | ||
export interface GetCodeActionsForRefactorRequest extends Request { | ||
command: CommandTypes.GetCodeActionsForRefactor; | ||
arguments: GetCodeActionsForRefactorRequestArgs; | ||
} | ||
|
||
export interface GetCodeActionsForRefactorRequestArgs extends GetRefactorsForRangeRequestArgs { | ||
refactorCode: number; | ||
} | ||
|
||
export interface GetCodeActionsForRefactorResponse extends Response { | ||
body?: CodeAction[]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do not use an array. make it an map. |
||
} | ||
|
||
/** | ||
* Request for the available codefixes at a specific position. | ||
*/ | ||
|
@@ -402,10 +454,7 @@ namespace ts.server.protocol { | |
arguments: CodeFixRequestArgs; | ||
} | ||
|
||
/** | ||
* Instances of this interface specify errorcodes on a specific location in a sourcefile. | ||
*/ | ||
export interface CodeFixRequestArgs extends FileRequestArgs { | ||
export interface TextRangeRequestArgs extends FileRequestArgs { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you name the first two members There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe |
||
/** | ||
* The line number for the request (1-based). | ||
*/ | ||
|
@@ -437,7 +486,12 @@ namespace ts.server.protocol { | |
*/ | ||
/* @internal */ | ||
endPosition?: number; | ||
} | ||
|
||
/** | ||
* Instances of this interface specify errorcodes on a specific location in a sourcefile. | ||
*/ | ||
export interface CodeFixRequestArgs extends TextRangeRequestArgs { | ||
/** | ||
* Errorcodes we want to get the fixes for. | ||
*/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"messages" should be "message"