mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-03-17 17:51:22 -05:00
* checker.ts: Remove null check on symbols * tsserverProjectSystem.ts: add two tests * client.ts, completions.ts, types.ts: Add codeActions member to CompletionEntryDetails * protocol.ts, session.ts: Add codeActions member to CompletionEntryDetails protocol * protocol.ts, session.ts, types.ts: add hasAction to CompletionEntry * session.ts, services.ts, types.ts: Add formattingOptions parameter to getCompletionEntryDetails * completions.ts: define SymbolOriginInfo type * completions.ts, services.ts: Add allSourceFiles parameter to getCompletionsAtPosition * completions.ts, services.ts: Plumb allSourceFiles into new function getSymbolsFromOtherSourceFileExports inside getCompletionData * completions.ts: add symbolToOriginInfoMap parameter to getCompletionEntriesFromSymbols and to return value of getCompletionData * utilities.ts: Add getOtherModuleSymbols, getUniqueSymbolIdAsString, getUniqueSymbolId * completions.ts: Set CompletionEntry.hasAction when symbol is found in symbolToOriginInfoMap (meaning there's an import action) * completions.ts: Populate list with possible exports (implement getSymbolsFromOtherSourceFileExports) * completions.ts, services.ts: Plumb host and rulesProvider into getCompletionEntryDetails * completions.ts: Add TODO comment * importFixes.ts: Add types ImportDeclarationMap and ImportCodeFixContext * Move getImportDeclarations into getCodeActionForImport, immediately after the implementation * importFixes.ts: Move createChangeTracker into getCodeActionForImport, immediately after getImportDeclarations * importFixes.ts: Add convertToImportCodeFixContext function and reference it from the getCodeActions lambda * importFixes.ts: Add context: ImportCodeFixContext parameter to getCodeActionForImport, update call sites, destructure it, use compilerOptions in getModuleSpecifierForNewImport * importFixes.ts: Remove moduleSymbol parameter from getImportDeclarations and use the ambient one * importFixes.ts: Use cachedImportDeclarations from context in getCodeActionForImport * importFixes.ts: Move createCodeAction out, immediately above convertToImportCodeFixContext * Move the declaration for lastImportDeclaration out of the getCodeActions lambda into getCodeActionForImport * importFixes.ts: Use symbolToken in getCodeActionForImport * importFixes.ts: Remove useCaseSensitiveFileNames altogether from getCodeActions lambda * importFixes.ts: Remove local getUniqueSymbolId function and add checker parameter to calls to it * importFixes.ts: Move getCodeActionForImport out into an export, immediately below convertToImportCodeFixContext * completions.ts: In getCompletionEntryDetails, if there's symbolOriginInfo, call getCodeActionForImport * importFixes.ts: Create and use importFixContext within getCodeActions lambda * importFixes.ts: Use local newLineCharacter instead of context.newLineCharacter in getCodeActionForImport * importFixes.ts: Use local host instead of context.host in getCodeActionForImport * importFixes.ts: Remove dummy getCanonicalFileName line * Filter symbols after gathering exports instead of before * Lint * Test, fix bugs, refactor * Suggestions from code review * Update api baseline * Fix bug if previousToken is not an Identifier * Replace `startsWith` with `stringContainsCharactersInOrder`
57 lines
1.8 KiB
TypeScript
57 lines
1.8 KiB
TypeScript
/* @internal */
|
|
namespace ts {
|
|
export interface CodeFix {
|
|
errorCodes: number[];
|
|
getCodeActions(context: CodeFixContext): CodeAction[] | undefined;
|
|
}
|
|
|
|
export interface CodeFixContext extends textChanges.TextChangesContext {
|
|
errorCode: number;
|
|
sourceFile: SourceFile;
|
|
span: TextSpan;
|
|
program: Program;
|
|
host: LanguageServiceHost;
|
|
cancellationToken: CancellationToken;
|
|
}
|
|
|
|
export namespace codefix {
|
|
const codeFixes: CodeFix[][] = [];
|
|
|
|
export function registerCodeFix(codeFix: CodeFix) {
|
|
forEach(codeFix.errorCodes, error => {
|
|
let fixes = codeFixes[error];
|
|
if (!fixes) {
|
|
fixes = [];
|
|
codeFixes[error] = fixes;
|
|
}
|
|
fixes.push(codeFix);
|
|
});
|
|
}
|
|
|
|
export function getSupportedErrorCodes() {
|
|
return Object.keys(codeFixes);
|
|
}
|
|
|
|
export function getFixes(context: CodeFixContext): CodeAction[] {
|
|
const fixes = codeFixes[context.errorCode];
|
|
const allActions: CodeAction[] = [];
|
|
|
|
forEach(fixes, f => {
|
|
const actions = f.getCodeActions(context);
|
|
if (actions && actions.length > 0) {
|
|
for (const action of actions) {
|
|
if (action === undefined) {
|
|
context.host.log(`Action for error code ${context.errorCode} added an invalid action entry; please log a bug`);
|
|
}
|
|
else {
|
|
allActions.push(action);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
return allActions;
|
|
}
|
|
}
|
|
}
|