|
| 1 | +import {QuickFix, QuickFixQueryInformation, Refactoring, CanProvideFixResponse} from "../quickFix"; |
| 2 | +import * as ast from "../astUtils"; |
| 3 | +import {EOL } from "os"; |
| 4 | +var { displayPartsToString, typeToDisplayParts } = ts; |
| 5 | +import path = require('path'); |
| 6 | +import {Project} from "../../core/project"; |
| 7 | + |
| 8 | +import {getPathCompletions} from "../../modules/getPathCompletions"; |
| 9 | + |
| 10 | +function getIdentifierAndFileNames(error: ts.Diagnostic, project: Project) { |
| 11 | + |
| 12 | + var errorText: string = <any>error.messageText; |
| 13 | + |
| 14 | + // We don't support error chains yet |
| 15 | + if (typeof errorText !== 'string') { |
| 16 | + return undefined; |
| 17 | + }; |
| 18 | + |
| 19 | + var match = errorText.match(/Cannot find name \'(\w+)\'./); |
| 20 | + |
| 21 | + // If for whatever reason the error message doesn't match |
| 22 | + if (!match) return; |
| 23 | + |
| 24 | + var [, identifierName] = match; |
| 25 | + var {files} = getPathCompletions({ |
| 26 | + project, |
| 27 | + filePath: error.file.fileName, |
| 28 | + prefix: identifierName, |
| 29 | + includeExternalModules: false |
| 30 | + }); |
| 31 | + var file = files.length > 0 ? files[0].relativePath : undefined; |
| 32 | + var basename = files.length > 0 ? files[0].name : undefined; |
| 33 | + return { identifierName, file, basename }; |
| 34 | +} |
| 35 | + |
| 36 | +export class AddImportFromStatement implements QuickFix { |
| 37 | + key = AddImportFromStatement.name; |
| 38 | + |
| 39 | + canProvideFix(info: QuickFixQueryInformation): CanProvideFixResponse { |
| 40 | + var relevantError = info.positionErrors.filter(x=> x.code == 2304)[0]; |
| 41 | + if (!relevantError) return; |
| 42 | + if (info.positionNode.kind !== ts.SyntaxKind.Identifier) return; |
| 43 | + var matches = getIdentifierAndFileNames(relevantError, info.project); |
| 44 | + if (!matches) return; |
| 45 | + |
| 46 | + var { identifierName, file} = matches; |
| 47 | + return file ? { display: `import {${identifierName}} from \"${file}\"` } : undefined; |
| 48 | + } |
| 49 | + |
| 50 | + provideFix(info: QuickFixQueryInformation): Refactoring[] { |
| 51 | + var relevantError = info.positionErrors.filter(x=> x.code == 2304)[0]; |
| 52 | + var identifier = <ts.Identifier>info.positionNode; |
| 53 | + |
| 54 | + var identifierName = identifier.text; |
| 55 | + var fileNameforFix = getIdentifierAndFileNames(relevantError, info.project); |
| 56 | + |
| 57 | + // Add stuff at the top of the file |
| 58 | + let refactorings: Refactoring[] = [{ |
| 59 | + span: { |
| 60 | + start: 0, |
| 61 | + length: 0 |
| 62 | + }, |
| 63 | + newText: `import {${identifierName}} from \"${fileNameforFix.file}\";${EOL}`, |
| 64 | + filePath: info.sourceFile.fileName |
| 65 | + }]; |
| 66 | + |
| 67 | + // Also refactor the variable name to match the file name |
| 68 | + // TODO: the following code only takes into account location |
| 69 | + // There may be other locations where this is used. |
| 70 | + // Better that they trigger a *rename* explicitly later if they want to rename the variable |
| 71 | + // if (identifierName !== fileNameforFix.basename) { |
| 72 | + // refactorings.push({ |
| 73 | + // span: { |
| 74 | + // start: identifier.getStart(), |
| 75 | + // length: identifier.end - identifier.getStart() |
| 76 | + // }, |
| 77 | + // newText: fileNameforFix.basename, |
| 78 | + // filePath: info.srcFile.fileName |
| 79 | + // }) |
| 80 | + // } |
| 81 | + |
| 82 | + return refactorings; |
| 83 | + } |
| 84 | +} |
0 commit comments