Support completions inside JSDoc before EndOfFileToken (#25568)

This commit is contained in:
Andy 2018-09-05 11:34:27 -07:00 committed by GitHub
parent 62d8b85f1d
commit 1eb3082387
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 22 deletions

View file

@ -755,7 +755,7 @@ namespace ts {
return result;
function find(n: Node): Node | undefined {
if (isNonWhitespaceToken(n)) {
if (isNonWhitespaceToken(n) && n.kind !== SyntaxKind.EndOfFileToken) {
return n;
}
@ -786,16 +786,14 @@ namespace ts {
}
}
Debug.assert(startNode !== undefined || n.kind === SyntaxKind.SourceFile || isJSDocCommentContainingNode(n));
Debug.assert(startNode !== undefined || n.kind === SyntaxKind.SourceFile || n.kind === SyntaxKind.EndOfFileToken || isJSDocCommentContainingNode(n));
// Here we know that none of child token nodes embrace the position,
// the only known case is when position is at the end of the file.
// Try to find the rightmost token in the file without filtering.
// Namely we are skipping the check: 'position < node.end'
if (children.length) {
const candidate = findRightmostChildNodeWithTokens(children, /*exclusiveStartPosition*/ children.length, sourceFile);
return candidate && findRightmostToken(candidate, sourceFile);
}
const candidate = findRightmostChildNodeWithTokens(children, /*exclusiveStartPosition*/ children.length, sourceFile);
return candidate && findRightmostToken(candidate, sourceFile);
}
}
@ -1048,7 +1046,7 @@ namespace ts {
function nodeHasTokens(n: Node, sourceFile: SourceFileLike): boolean {
// If we have a token or node that has a non-zero width, it must have tokens.
// Note: getWidth() does not take trivia into account.
return n.getWidth(sourceFile) !== 0;
return n.kind === SyntaxKind.EndOfFileToken ? !!(n as EndOfFileToken).jsDoc : n.getWidth(sourceFile) !== 0;
}
export function getNodeModifiers(node: Node): string {

View file

@ -23,5 +23,7 @@
// @Filename: /src/folder/4.ts
////const foo = require(`x//*4*/`);
verify.completionsAt("1", ["y", "x"], { isNewIdentifierLocation: true });
verify.completionsAt(["2", "3", "4"], ["bar", "foo"], { isNewIdentifierLocation: true });
verify.completions(
{ marker: "1", exact: ["y", "x"], isNewIdentifierLocation: true },
{ marker: ["2", "3", "4"], exact: ["bar", "foo"], isNewIdentifierLocation: true },
);

View file

@ -0,0 +1,23 @@
/// <reference path="fourslash.ts" />
// @allowJs: true
// @moduleResolution: node
// @Filename: /ns.ts
////file content not read
// @Filename: /node_modules/package/index.ts
////file content not read
// @Filename: /usage.ts
////type A = typeof import("p/*1*/");
////type B = import(".//*2*/");
// @Filename: /user.js
/////** @type {import("/*3*/")} */
verify.completions(
{ marker: "1", exact: "package", isNewIdentifierLocation: true },
{ marker: "2", exact: ["lib", "ns", "user", "node_modules"], isNewIdentifierLocation: true },
{ marker: "3", exact: ["package"], isNewIdentifierLocation: true },
);

View file

@ -1,13 +0,0 @@
/// <reference path="fourslash.ts" />
// @moduleResolution: node
// @Filename: /ns.ts
////file content not read
// @Filename: /node_modules/package/index.ts
////file content not read
// @Filename: /usage.ts
////type A = typeof import("p/*1*/");
////type B = typeof import(".//*2*/");
verify.completionsAt("1", ["package"], { isNewIdentifierLocation: true });
verify.completionsAt("2", ["lib", "ns", "node_modules"], { isNewIdentifierLocation: true });