Have CompletionEntryDetails source use a relative path (#19917)

* Have CompletionEntryDetails source use a relative path

* Use getCanonicalFileName from services Instead of creating a new one
This commit is contained in:
Andy 2017-11-14 14:26:49 -08:00 committed by GitHub
parent 9c51a8534e
commit 592ee00906
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 97 additions and 49 deletions

View File

@ -77,7 +77,7 @@ namespace ts {
}
export interface BuilderOptions {
getCanonicalFileName: (fileName: string) => string;
getCanonicalFileName: GetCanonicalFileName;
computeHash: (data: string) => string;
}

View File

@ -1540,7 +1540,7 @@ namespace ts {
host: ParseConfigHost,
basePath: string,
configFileName: string,
getCanonicalFileName: (fileName: string) => string,
getCanonicalFileName: GetCanonicalFileName,
resolutionStack: Path[],
errors: Push<Diagnostic>,
): ParsedTsconfig {
@ -1588,7 +1588,7 @@ namespace ts {
json: any,
host: ParseConfigHost,
basePath: string,
getCanonicalFileName: (fileName: string) => string,
getCanonicalFileName: GetCanonicalFileName,
configFileName: string | undefined,
errors: Push<Diagnostic>
): ParsedTsconfig {
@ -1619,7 +1619,7 @@ namespace ts {
sourceFile: JsonSourceFile,
host: ParseConfigHost,
basePath: string,
getCanonicalFileName: (fileName: string) => string,
getCanonicalFileName: GetCanonicalFileName,
configFileName: string | undefined,
errors: Push<Diagnostic>
): ParsedTsconfig {
@ -1688,7 +1688,7 @@ namespace ts {
extendedConfig: string,
host: ParseConfigHost,
basePath: string,
getCanonicalFileName: (fileName: string) => string,
getCanonicalFileName: GetCanonicalFileName,
errors: Push<Diagnostic>,
createDiagnostic: (message: DiagnosticMessage, arg1?: string) => Diagnostic) {
extendedConfig = normalizeSlashes(extendedConfig);
@ -1713,7 +1713,7 @@ namespace ts {
extendedConfigPath: Path,
host: ts.ParseConfigHost,
basePath: string,
getCanonicalFileName: (fileName: string) => string,
getCanonicalFileName: GetCanonicalFileName,
resolutionStack: Path[],
errors: Push<Diagnostic>,
): ParsedTsconfig | undefined {

View File

@ -2033,7 +2033,7 @@ namespace ts {
}
}
export function getRelativePathToDirectoryOrUrl(directoryPathOrUrl: string, relativeOrAbsolutePath: string, currentDirectory: string, getCanonicalFileName: (fileName: string) => string, isAbsolutePathAnUrl: boolean) {
export function getRelativePathToDirectoryOrUrl(directoryPathOrUrl: string, relativeOrAbsolutePath: string, currentDirectory: string, getCanonicalFileName: GetCanonicalFileName, isAbsolutePathAnUrl: boolean) {
const pathComponents = getNormalizedPathOrUrlComponents(relativeOrAbsolutePath, currentDirectory);
const directoryComponents = getNormalizedPathOrUrlComponents(directoryPathOrUrl, currentDirectory);
if (directoryComponents.length > 1 && lastOrUndefined(directoryComponents) === "") {
@ -2819,7 +2819,8 @@ namespace ts {
}
}
export function createGetCanonicalFileName(useCaseSensitiveFileNames: boolean): (fileName: string) => string {
export type GetCanonicalFileName = (fileName: string) => string;
export function createGetCanonicalFileName(useCaseSensitiveFileNames: boolean): GetCanonicalFileName {
return useCaseSensitiveFileNames
? ((fileName) => fileName)
: ((fileName) => fileName.toLowerCase());

View File

@ -20,7 +20,7 @@ namespace ts {
}
/* @internal */
export function computeCommonSourceDirectoryOfFilenames(fileNames: string[], currentDirectory: string, getCanonicalFileName: (fileName: string) => string): string {
export function computeCommonSourceDirectoryOfFilenames(fileNames: string[], currentDirectory: string, getCanonicalFileName: GetCanonicalFileName): string {
let commonPathComponents: string[];
const failed = forEach(fileNames, sourceFile => {
// Each file contributes into common source file path

View File

@ -867,10 +867,10 @@ namespace FourSlash {
});
}
public verifyCompletionListContains(entryId: ts.Completions.CompletionEntryIdentifier, text?: string, documentation?: string, kind?: string, spanIndex?: number, hasAction?: boolean, options?: ts.GetCompletionsAtPositionOptions) {
public verifyCompletionListContains(entryId: ts.Completions.CompletionEntryIdentifier, text?: string, documentation?: string, kind?: string, spanIndex?: number, hasAction?: boolean, options?: FourSlashInterface.VerifyCompletionListContainsOptions) {
const completions = this.getCompletionListAtCaret(options);
if (completions) {
this.assertItemInCompletionList(completions.entries, entryId, text, documentation, kind, spanIndex, hasAction);
this.assertItemInCompletionList(completions.entries, entryId, text, documentation, kind, spanIndex, hasAction, options);
}
else {
this.raiseError(`No completions at position '${this.currentCaretPosition}' when looking for '${JSON.stringify(entryId)}'.`);
@ -3071,6 +3071,7 @@ Actual: ${stringify(fullActual)}`);
kind: string | undefined,
spanIndex: number | undefined,
hasAction: boolean | undefined,
options: FourSlashInterface.VerifyCompletionListContainsOptions | undefined,
) {
for (const item of items) {
if (item.name === entryId.name && item.source === entryId.source) {
@ -3084,7 +3085,12 @@ Actual: ${stringify(fullActual)}`);
assert.equal(ts.displayPartsToString(details.displayParts), text, this.assertionMessageAtLastKnownMarker("completion item detail text for " + entryId));
}
assert.deepEqual(details.source, entryId.source === undefined ? undefined : [ts.textPart(entryId.source)]);
if (entryId.source === undefined) {
assert.equal(options && options.sourceDisplay, undefined);
}
else {
assert.deepEqual(details.source, [ts.textPart(options!.sourceDisplay)]);
}
}
if (kind !== undefined) {
@ -3811,7 +3817,7 @@ namespace FourSlashInterface {
// Verifies the completion list contains the specified symbol. The
// completion list is brought up if necessary
public completionListContains(entryId: string | ts.Completions.CompletionEntryIdentifier, text?: string, documentation?: string, kind?: string, spanIndex?: number, hasAction?: boolean, options?: ts.GetCompletionsAtPositionOptions) {
public completionListContains(entryId: string | ts.Completions.CompletionEntryIdentifier, text?: string, documentation?: string, kind?: string, spanIndex?: number, hasAction?: boolean, options?: VerifyCompletionListContainsOptions) {
if (typeof entryId === "string") {
entryId = { name: entryId, source: undefined };
}
@ -4547,6 +4553,10 @@ namespace FourSlashInterface {
isNewIdentifierLocation?: boolean;
}
export interface VerifyCompletionListContainsOptions extends ts.GetCompletionsAtPositionOptions {
sourceDisplay: string;
}
export interface NewContentOptions {
// Exactly one of these should be defined.
newFileContent?: string;

View File

@ -34,7 +34,7 @@ namespace ts.codefix {
host: LanguageServiceHost;
checker: TypeChecker;
compilerOptions: CompilerOptions;
getCanonicalFileName(fileName: string): string;
getCanonicalFileName: GetCanonicalFileName;
cachedImportDeclarations?: ImportDeclarationMap;
}
@ -313,7 +313,7 @@ namespace ts.codefix {
}
}
function getModuleSpecifierForNewImport(sourceFile: SourceFile, moduleSymbol: Symbol, options: CompilerOptions, getCanonicalFileName: (file: string) => string, host: LanguageServiceHost): string | undefined {
export function getModuleSpecifierForNewImport(sourceFile: SourceFile, moduleSymbol: Symbol, options: CompilerOptions, getCanonicalFileName: (file: string) => string, host: LanguageServiceHost): string | undefined {
const moduleFileName = moduleSymbol.valueDeclaration.getSourceFile().fileName;
const sourceDirectory = getDirectoryPath(sourceFile.fileName);
@ -523,7 +523,7 @@ namespace ts.codefix {
return state > States.NodeModules ? { topLevelNodeModulesIndex, topLevelPackageNameIndex, packageRootIndex, fileNameIndex } : undefined;
}
function getPathRelativeToRootDirs(path: string, rootDirs: ReadonlyArray<string>, getCanonicalFileName: (fileName: string) => string): string | undefined {
function getPathRelativeToRootDirs(path: string, rootDirs: ReadonlyArray<string>, getCanonicalFileName: GetCanonicalFileName): string | undefined {
return firstDefined(rootDirs, rootDir => getRelativePathIfInDirectory(path, rootDir, getCanonicalFileName));
}
@ -535,12 +535,12 @@ namespace ts.codefix {
return fileName;
}
function getRelativePathIfInDirectory(path: string, directoryPath: string, getCanonicalFileName: (fileName: string) => string): string | undefined {
function getRelativePathIfInDirectory(path: string, directoryPath: string, getCanonicalFileName: GetCanonicalFileName): string | undefined {
const relativePath = getRelativePathToDirectoryOrUrl(directoryPath, path, directoryPath, getCanonicalFileName, /*isAbsolutePathAnUrl*/ false);
return isRootedDiskPath(relativePath) || startsWith(relativePath, "..") ? undefined : relativePath;
}
function getRelativePath(path: string, directoryPath: string, getCanonicalFileName: (fileName: string) => string) {
function getRelativePath(path: string, directoryPath: string, getCanonicalFileName: GetCanonicalFileName) {
const relativePath = getRelativePathToDirectoryOrUrl(directoryPath, path, directoryPath, getCanonicalFileName, /*isAbsolutePathAnUrl*/ false);
return !pathIsRelative(relativePath) ? "./" + relativePath : relativePath;
}

View File

@ -422,8 +422,9 @@ namespace ts.Completions {
allSourceFiles: ReadonlyArray<SourceFile>,
host: LanguageServiceHost,
formatContext: formatting.FormatContext,
getCanonicalFileName: GetCanonicalFileName,
): CompletionEntryDetails {
const { name, source } = entryId;
const { name } = entryId;
// Compute all the completion symbols again.
const symbolCompletion = getSymbolCompletionFromEntryId(typeChecker, log, compilerOptions, sourceFile, position, entryId, allSourceFiles);
switch (symbolCompletion.type) {
@ -442,10 +443,10 @@ namespace ts.Completions {
}
case "symbol": {
const { symbol, location, symbolToOriginInfoMap } = symbolCompletion;
const codeActions = getCompletionEntryCodeActions(symbolToOriginInfoMap, symbol, typeChecker, host, compilerOptions, sourceFile, formatContext);
const { codeActions, sourceDisplay } = getCompletionEntryCodeActionsAndSourceDisplay(symbolToOriginInfoMap, symbol, typeChecker, host, compilerOptions, sourceFile, formatContext, getCanonicalFileName);
const kindModifiers = SymbolDisplay.getSymbolModifiers(symbol);
const { displayParts, documentation, symbolKind, tags } = SymbolDisplay.getSymbolDisplayPartsDocumentationAndSymbolKind(typeChecker, symbol, sourceFile, location, location, SemanticMeaning.All);
return { name, kindModifiers, kind: symbolKind, displayParts, documentation, tags, codeActions, source: source === undefined ? undefined : [textPart(source)] };
return { name, kindModifiers, kind: symbolKind, displayParts, documentation, tags, codeActions, source: sourceDisplay };
}
case "none": {
// Didn't find a symbol with this name. See if we can find a keyword instead.
@ -466,7 +467,7 @@ namespace ts.Completions {
}
}
function getCompletionEntryCodeActions(
function getCompletionEntryCodeActionsAndSourceDisplay(
symbolToOriginInfoMap: SymbolOriginInfoMap,
symbol: Symbol,
checker: TypeChecker,
@ -474,14 +475,17 @@ namespace ts.Completions {
compilerOptions: CompilerOptions,
sourceFile: SourceFile,
formatContext: formatting.FormatContext,
): CodeAction[] | undefined {
getCanonicalFileName: GetCanonicalFileName,
): { codeActions: CodeAction[] | undefined, sourceDisplay: SymbolDisplayPart[] | undefined } {
const symbolOriginInfo = symbolToOriginInfoMap[getSymbolId(symbol)];
if (!symbolOriginInfo) {
return undefined;
return { codeActions: undefined, sourceDisplay: undefined };
}
const { moduleSymbol, isDefaultExport } = symbolOriginInfo;
return codefix.getCodeActionForImport(moduleSymbol, {
const sourceDisplay = [textPart(codefix.getModuleSpecifierForNewImport(sourceFile, moduleSymbol, compilerOptions, getCanonicalFileName, host))];
const codeActions = codefix.getCodeActionForImport(moduleSymbol, {
host,
checker,
newLineCharacter: host.getNewLine(),
@ -489,10 +493,11 @@ namespace ts.Completions {
sourceFile,
formatContext,
symbolName: getSymbolName(symbol, symbolOriginInfo, compilerOptions.target),
getCanonicalFileName: createGetCanonicalFileName(host.useCaseSensitiveFileNames ? host.useCaseSensitiveFileNames() : false),
getCanonicalFileName,
symbolToken: undefined,
kind: isDefaultExport ? codefix.ImportKind.Default : codefix.ImportKind.Named,
});
return { sourceDisplay, codeActions };
}
export function getCompletionEntrySymbol(

View File

@ -1,6 +1,6 @@
/* @internal */
namespace ts.Rename {
export function getRenameInfo(typeChecker: TypeChecker, defaultLibFileName: string, getCanonicalFileName: (fileName: string) => string, sourceFile: SourceFile, position: number): RenameInfo {
export function getRenameInfo(typeChecker: TypeChecker, defaultLibFileName: string, getCanonicalFileName: GetCanonicalFileName, sourceFile: SourceFile, position: number): RenameInfo {
const getCanonicalDefaultLibName = memoize(() => getCanonicalFileName(ts.normalizePath(defaultLibFileName)));
const node = getTouchingWord(sourceFile, position, /*includeJsDocComment*/ true);
const renameInfo = node && nodeIsEligibleForRename(node)

View File

@ -938,7 +938,7 @@ namespace ts {
private _compilationSettings: CompilerOptions;
private currentDirectory: string;
constructor(private host: LanguageServiceHost, getCanonicalFileName: (fileName: string) => string) {
constructor(private host: LanguageServiceHost, getCanonicalFileName: GetCanonicalFileName) {
// script id => script index
this.currentDirectory = host.getCurrentDirectory();
this.fileNameToEntry = createMap<CachedHostFileInformation>();
@ -1447,7 +1447,8 @@ namespace ts {
{ name, source },
program.getSourceFiles(),
host,
formattingOptions && formatting.getFormatContext(formattingOptions));
formattingOptions && formatting.getFormatContext(formattingOptions),
getCanonicalFileName);
}
function getCompletionEntrySymbol(fileName: string, position: number, name: string, source?: string): Symbol {

View File

@ -9,7 +9,10 @@
////f/**/;
goTo.marker("");
verify.completionListContains({ name: "foo", source: "/a" }, "function foo(): void", "", "function", /*spanIndex*/ undefined, /*hasAction*/ true, { includeExternalModuleExports: true });
verify.completionListContains({ name: "foo", source: "/a" }, "function foo(): void", "", "function", /*spanIndex*/ undefined, /*hasAction*/ true, {
includeExternalModuleExports: true,
sourceDisplay: "./a",
});
verify.applyCodeActionFromCompletion("", {
name: "foo",

View File

@ -8,7 +8,10 @@
////f/**/;
goTo.marker("");
verify.completionListContains({ name: "foo", source: "/a" }, "function foo(): void", "", "function", /*spanIndex*/ undefined, /*hasAction*/ true, { includeExternalModuleExports: true });
verify.completionListContains({ name: "foo", source: "/a" }, "function foo(): void", "", "function", /*spanIndex*/ undefined, /*hasAction*/ true, {
includeExternalModuleExports: true,
sourceDisplay: "./a",
});
verify.applyCodeActionFromCompletion("", {
name: "foo",

View File

@ -8,7 +8,10 @@
////f/**/;
goTo.marker("");
verify.completionListContains({ name: "foo", source: "/a" }, "function foo(): void", "", "function", /*spanIndex*/ undefined, /*hasAction*/ true, { includeExternalModuleExports: true });
verify.completionListContains({ name: "foo", source: "/a" }, "function foo(): void", "", "function", /*spanIndex*/ undefined, /*hasAction*/ true, {
includeExternalModuleExports: true,
sourceDisplay: "./a",
});
verify.applyCodeActionFromCompletion("", {
name: "foo",

View File

@ -10,10 +10,14 @@
////fooB/*1*/
goTo.marker("0");
verify.not.completionListContains({ name: "default", source: "/src/foo-bar" }, undefined, undefined, undefined, undefined, undefined, { includeExternalModuleExports: true });
const options = {
includeExternalModuleExports: true,
sourceDisplay: "./foo-bar",
};
verify.not.completionListContains({ name: "default", source: "/src/foo-bar" }, undefined, undefined, undefined, undefined, undefined, options);
goTo.marker("1");
verify.completionListContains({ name: "fooBar", source: "/src/foo-bar" }, "(property) default: 0", "", "property", /*spanIndex*/ undefined, /*hasAction*/ true, { includeExternalModuleExports: true });
verify.completionListContains({ name: "fooBar", source: "/src/foo-bar" }, "(property) default: 0", "", "property", /*spanIndex*/ undefined, /*hasAction*/ true, options);
verify.applyCodeActionFromCompletion("1", {
name: "fooBar",
source: "/src/foo-bar",

View File

@ -7,7 +7,10 @@
////f/**/;
goTo.marker("");
verify.completionListContains({ name: "foo", source: "/a" }, "function foo(): void", "", "function", /*spanIndex*/ undefined, /*hasAction*/ true, { includeExternalModuleExports: true });
verify.completionListContains({ name: "foo", source: "/a" }, "function foo(): void", "", "function", /*spanIndex*/ undefined, /*hasAction*/ true, {
includeExternalModuleExports: true,
sourceDisplay: "./a",
});
verify.applyCodeActionFromCompletion("", {
name: "foo",

View File

@ -14,7 +14,7 @@
goTo.marker("");
const options = { includeExternalModuleExports: true };
const options = { includeExternalModuleExports: true, sourceDisplay: "./a" };
verify.not.completionListContains({ name: "abcde", source: "/a" }, undefined, undefined, undefined, undefined, undefined, options);
verify.not.completionListContains({ name: "dbf", source: "/a" }, undefined, undefined, undefined, undefined, undefined, options);

View File

@ -14,10 +14,10 @@
////fo/**/
goTo.marker("");
const options = { includeExternalModuleExports: true };
const options = { includeExternalModuleExports: true, sourceDisplay: undefined };
verify.completionListContains("foo", "var foo: number", "", "var", undefined, undefined, options);
verify.completionListContains({ name: "foo", source: "/a" }, "const foo: 0", "", "const", /*spanIndex*/ undefined, /*hasAction*/ true, options);
verify.completionListContains({ name: "foo", source: "/b" }, "const foo: 1", "", "const", /*spanIndex*/ undefined, /*hasAction*/ true, options);
verify.completionListContains({ name: "foo", source: "/a" }, "const foo: 0", "", "const", /*spanIndex*/ undefined, /*hasAction*/ true, { ...options, sourceDisplay: "./a" });
verify.completionListContains({ name: "foo", source: "/b" }, "const foo: 1", "", "const", /*spanIndex*/ undefined, /*hasAction*/ true, { ...options, sourceDisplay: "./b" });
verify.applyCodeActionFromCompletion("", {
name: "foo",

View File

@ -9,7 +9,10 @@
////f/**/;
goTo.marker("");
verify.completionListContains({ name: "foo", source: "/a" }, "function foo(): void", "", "function", /*spanIndex*/ undefined, /*hasAction*/ true, { includeExternalModuleExports: true });
verify.completionListContains({ name: "foo", source: "/a" }, "function foo(): void", "", "function", /*spanIndex*/ undefined, /*hasAction*/ true, {
includeExternalModuleExports: true,
sourceDisplay: "./a",
});
verify.applyCodeActionFromCompletion("", {
name: "foo",

View File

@ -9,8 +9,8 @@
////t/**/
goTo.marker("");
const options = { includeExternalModuleExports: true };
verify.completionListContains({ name: "Test1", source: "/a" }, "function Test1(): void", "", "function", /*spanIndex*/ undefined, /*hasAction*/ true, options);
const options = { includeExternalModuleExports: true, sourceDisplay: undefined };
verify.completionListContains({ name: "Test1", source: "/a" }, "function Test1(): void", "", "function", /*spanIndex*/ undefined, /*hasAction*/ true, { ...options, sourceDisplay: "./a" });
verify.completionListContains("Test2", "import Test2", "", "alias", /*spanIndex*/ undefined, /*hasAction*/ undefined, options);
verify.not.completionListContains({ name: "Test2", source: "/a" }, undefined, undefined, undefined, undefined, undefined, options);

View File

@ -8,7 +8,10 @@
////f/**/;
goTo.marker("");
verify.completionListContains({ name: "foo", source: "/a" }, "function foo(): void", "", "function", /*spanIndex*/ undefined, /*hasAction*/ true, { includeExternalModuleExports: true });
verify.completionListContains({ name: "foo", source: "/a" }, "function foo(): void", "", "function", /*spanIndex*/ undefined, /*hasAction*/ true, {
includeExternalModuleExports: true,
sourceDisplay: "./a",
});
verify.applyCodeActionFromCompletion("", {
name: "foo",

View File

@ -15,7 +15,7 @@
////fo/**/
goTo.marker("");
const options = { includeExternalModuleExports: true };
const options = { includeExternalModuleExports: true, sourceDisplay: "./a" };
// TODO: https://github.com/Microsoft/TypeScript/issues/14003
verify.completionListContains({ name: "foo", source: "/a" }, "import foo", "", "alias", /*spanIndex*/ undefined, /*hasAction*/ true, options);
verify.not.completionListContains({ name: "foo", source: "/a_reexport" }, undefined, undefined, undefined, undefined, undefined, options);

View File

@ -8,4 +8,7 @@
/////**/
goTo.marker("");
verify.completionListContains({ name: "foo", source: "/a" }, "function foo(): void", "", "function", /*spanIndex*/ undefined, /*hasAction*/ true, { includeExternalModuleExports: true });
verify.completionListContains({ name: "foo", source: "/a" }, "function foo(): void", "", "function", /*spanIndex*/ undefined, /*hasAction*/ true, {
includeExternalModuleExports: true,
sourceDisplay: "./a",
});

View File

@ -14,7 +14,10 @@
////fo/*c*/
goTo.marker("b");
verify.completionListContains({ name: "foo", source: "/a" }, "const foo: 0", "", "const", /*spanIndex*/ undefined, /*hasAction*/ true, { includeExternalModuleExports: true });
verify.completionListContains({ name: "foo", source: "/a" }, "const foo: 0", "", "const", /*spanIndex*/ undefined, /*hasAction*/ true, {
includeExternalModuleExports: true,
sourceDisplay: "./a",
});
verify.applyCodeActionFromCompletion("b", {
name: "foo",
@ -28,7 +31,10 @@ fo`,
});
goTo.marker("c");
verify.completionListContains({ name: "foo", source: "/a" }, "const foo: 0", "", "const", /*spanIndex*/ undefined, /*hasAction*/ true, { includeExternalModuleExports: true });
verify.completionListContains({ name: "foo", source: "/a" }, "const foo: 0", "", "const", /*spanIndex*/ undefined, /*hasAction*/ true, {
includeExternalModuleExports: true,
sourceDisplay: "./a",
});
verify.applyCodeActionFromCompletion("c", {
name: "foo",

View File

@ -8,6 +8,6 @@
////fo/**/
goTo.marker("");
const options = { includeExternalModuleExports: true };
const options = { includeExternalModuleExports: true, sourceDisplay: undefined };
verify.completionListContains("foo", "const foo: 1", "", "const", undefined, undefined, options);
verify.not.completionListContains({ name: "foo", source: "/a" }, undefined, undefined, undefined, undefined, undefined, options);

View File

@ -148,7 +148,7 @@ declare namespace FourSlashInterface {
kind?: string,
spanIndex?: number,
hasAction?: boolean,
options?: { includeExternalModuleExports: boolean },
options?: { includeExternalModuleExports: boolean, sourceDisplay: string },
): void;
completionListItemsCountIsGreaterThan(count: number): void;
completionListIsEmpty(): void;