|
11 | 11 | /// <reference path='jsTyping.ts' /> |
12 | 12 | /// <reference path='formatting\formatting.ts' /> |
13 | 13 | /// <reference path='formatting\smartIndenter.ts' /> |
| 14 | +/// <reference path='codefixes\references.ts' /> |
14 | 15 |
|
15 | 16 | namespace ts { |
16 | 17 | /** The version of the language service API */ |
@@ -1237,6 +1238,8 @@ namespace ts { |
1237 | 1238 |
|
1238 | 1239 | isValidBraceCompletionAtPosition(fileName: string, position: number, openingBrace: number): boolean; |
1239 | 1240 |
|
| 1241 | + getCodeFixesAtPosition(fileName: string, start: number, end: number, errorCodes: string[]): CodeAction[]; |
| 1242 | + |
1240 | 1243 | getEmitOutput(fileName: string): EmitOutput; |
1241 | 1244 |
|
1242 | 1245 | getProgram(): Program; |
@@ -1283,6 +1286,18 @@ namespace ts { |
1283 | 1286 | newText: string; |
1284 | 1287 | } |
1285 | 1288 |
|
| 1289 | + export interface FileTextChanges { |
| 1290 | + fileName: string; |
| 1291 | + textChanges: TextChange[]; |
| 1292 | + } |
| 1293 | + |
| 1294 | + export interface CodeAction { |
| 1295 | + /** Description of the code action to display in the UI of the editor */ |
| 1296 | + description: string; |
| 1297 | + /** Text changes to apply to each file as part of the code action */ |
| 1298 | + changes: FileTextChanges[]; |
| 1299 | + } |
| 1300 | + |
1286 | 1301 | export interface TextInsertion { |
1287 | 1302 | newText: string; |
1288 | 1303 | /** The position in newText the caret should point to after the insertion. */ |
@@ -1886,9 +1901,13 @@ namespace ts { |
1886 | 1901 | }; |
1887 | 1902 | } |
1888 | 1903 |
|
1889 | | - // Cache host information about script should be refreshed |
| 1904 | + export function getSupportedCodeFixes() { |
| 1905 | + return codeFix.CodeFixProvider.getSupportedErrorCodes(); |
| 1906 | + } |
| 1907 | + |
| 1908 | + // Cache host information about script Should be refreshed |
1890 | 1909 | // at each language service public entry point, since we don't know when |
1891 | | - // set of scripts handled by the host changes. |
| 1910 | + // the set of scripts handled by the host changes. |
1892 | 1911 | class HostCache { |
1893 | 1912 | private fileNameToEntry: FileMap<HostFileInformation>; |
1894 | 1913 | private _compilationSettings: CompilerOptions; |
@@ -3022,6 +3041,7 @@ namespace ts { |
3022 | 3041 | documentRegistry: DocumentRegistry = createDocumentRegistry(host.useCaseSensitiveFileNames && host.useCaseSensitiveFileNames(), host.getCurrentDirectory())): LanguageService { |
3023 | 3042 |
|
3024 | 3043 | const syntaxTreeCache: SyntaxTreeCache = new SyntaxTreeCache(host); |
| 3044 | + const codeFixProvider: codeFix.CodeFixProvider = new codeFix.CodeFixProvider(); |
3025 | 3045 | let ruleProvider: formatting.RulesProvider; |
3026 | 3046 | let program: Program; |
3027 | 3047 | let lastProjectVersion: string; |
@@ -7832,6 +7852,30 @@ namespace ts { |
7832 | 7852 | return []; |
7833 | 7853 | } |
7834 | 7854 |
|
| 7855 | + function getCodeFixesAtPosition(fileName: string, start: number, end: number, errorCodes: string[]): CodeAction[] { |
| 7856 | + synchronizeHostData(); |
| 7857 | + const sourceFile = getValidSourceFile(fileName); |
| 7858 | + const checker = program.getTypeChecker(); |
| 7859 | + let allFixes: CodeAction[] = []; |
| 7860 | + |
| 7861 | + forEach(errorCodes, error => { |
| 7862 | + const context = { |
| 7863 | + errorCode: error, |
| 7864 | + sourceFile: sourceFile, |
| 7865 | + span: { start, length: end - start }, |
| 7866 | + checker: checker, |
| 7867 | + newLineCharacter: getNewLineOrDefaultFromHost(host) |
| 7868 | + }; |
| 7869 | + |
| 7870 | + const fixes = codeFixProvider.getFixes(context); |
| 7871 | + if (fixes) { |
| 7872 | + allFixes = allFixes.concat(fixes); |
| 7873 | + } |
| 7874 | + }); |
| 7875 | + |
| 7876 | + return allFixes; |
| 7877 | + } |
| 7878 | + |
7835 | 7879 | /** |
7836 | 7880 | * Checks if position points to a valid position to add JSDoc comments, and if so, |
7837 | 7881 | * returns the appropriate template. Otherwise returns an empty string. |
@@ -8302,6 +8346,7 @@ namespace ts { |
8302 | 8346 | getFormattingEditsAfterKeystroke, |
8303 | 8347 | getDocCommentTemplateAtPosition, |
8304 | 8348 | isValidBraceCompletionAtPosition, |
| 8349 | + getCodeFixesAtPosition, |
8305 | 8350 | getEmitOutput, |
8306 | 8351 | getNonBoundSourceFile, |
8307 | 8352 | getProgram |
|
0 commit comments