@@ -134,6 +134,8 @@ namespace ts.server {
134134 export const NameOrDottedNameSpan = "nameOrDottedNameSpan" ;
135135 export const BreakpointStatement = "breakpointStatement" ;
136136 export const CompilerOptionsForInferredProjects = "compilerOptionsForInferredProjects" ;
137+ export const GetCodeFixes = "getCodeFixes" ;
138+ export const GetCodeFixesFull = "getCodeFixes-full" ;
137139 }
138140
139141 export function formatMessage < T extends protocol . Message > ( msg : T , logger : server . Logger , byteLength : ( s : string , encoding : string ) => number , newLine : string ) : string {
@@ -751,7 +753,7 @@ namespace ts.server {
751753 return this . getFileAndProjectWorker ( args . file , args . projectFileName , /*refreshInferredProjects*/ false , errorOnMissingProject ) ;
752754 }
753755
754- private getFileAndProjectWorker ( uncheckedFileName : string , projectFileName : string , refreshInferredProjects : boolean , errorOnMissingProject : boolean ) {
756+ private getFileAndProjectWorker ( uncheckedFileName : string , projectFileName : string , refreshInferredProjects : boolean , errorOnMissingProject : boolean ) {
755757 const file = toNormalizedPath ( uncheckedFileName ) ;
756758 const project : Project = this . getProject ( projectFileName ) || this . projectService . getDefaultProjectForFile ( file , refreshInferredProjects ) ;
757759 if ( ! project && errorOnMissingProject ) {
@@ -1199,6 +1201,48 @@ namespace ts.server {
11991201 }
12001202 }
12011203
1204+ private getCodeFixes ( args : protocol . CodeFixRequestArgs , simplifiedResult : boolean ) : protocol . CodeAction [ ] | CodeAction [ ] {
1205+ const { file, project } = this . getFileAndProjectWithoutRefreshingInferredProjects ( args ) ;
1206+
1207+ const scriptInfo = project . getScriptInfoForNormalizedPath ( file ) ;
1208+ const startPosition = getStartPosition ( ) ;
1209+ const endPosition = getEndPosition ( ) ;
1210+
1211+ const codeActions = project . getLanguageService ( ) . getCodeFixesAtPosition ( file , startPosition , endPosition , args . errorCodes ) ;
1212+ if ( ! codeActions ) {
1213+ return undefined ;
1214+ }
1215+ if ( simplifiedResult ) {
1216+ return codeActions . map ( mapCodeAction ) ;
1217+ } else {
1218+ return codeActions ;
1219+ }
1220+
1221+ function mapCodeAction ( source : CodeAction ) : protocol . CodeAction {
1222+ return {
1223+ description : source . description ,
1224+ changes : source . changes . map ( change => ( {
1225+ fileName : change . fileName ,
1226+ textChanges : change . textChanges . map ( textChange => ( {
1227+ span : {
1228+ start : scriptInfo . positionToLineOffset ( textChange . span . start ) ,
1229+ end : scriptInfo . positionToLineOffset ( textChange . span . start + textChange . span . length )
1230+ } ,
1231+ newText : textChange . newText
1232+ } ) )
1233+ } ) )
1234+ } ;
1235+ }
1236+
1237+ function getStartPosition ( ) {
1238+ return args . startPosition !== undefined ? args . startPosition : scriptInfo . lineOffsetToPosition ( args . startLine , args . startOffset ) ;
1239+ }
1240+
1241+ function getEndPosition ( ) {
1242+ return args . endPosition !== undefined ? args . endPosition : scriptInfo . lineOffsetToPosition ( args . endLine , args . endOffset ) ;
1243+ }
1244+ }
1245+
12021246 private getBraceMatching ( args : protocol . FileLocationRequestArgs , simplifiedResult : boolean ) : protocol . TextSpan [ ] | TextSpan [ ] {
12031247 const { file, project } = this . getFileAndProjectWithoutRefreshingInferredProjects ( args ) ;
12041248
@@ -1521,6 +1565,12 @@ namespace ts.server {
15211565 [ CommandNames . ReloadProjects ] : ( request : protocol . ReloadProjectsRequest ) => {
15221566 this . projectService . reloadProjects ( ) ;
15231567 return this . notRequired ( ) ;
1568+ } ,
1569+ [ CommandNames . GetCodeFixes ] : ( request : protocol . CodeFixRequest ) => {
1570+ return this . requiredResponse ( this . getCodeFixes ( request . arguments , /*simplifiedResult*/ true ) ) ;
1571+ } ,
1572+ [ CommandNames . GetCodeFixesFull ] : ( request : protocol . CodeFixRequest ) => {
1573+ return this . requiredResponse ( this . getCodeFixes ( request . arguments , /*simplifiedResult*/ false ) ) ;
15241574 }
15251575 } ) ;
15261576
0 commit comments