From c8b1e454acf56b1c1390f38a6fadd9ff81f81d9a Mon Sep 17 00:00:00 2001 From: TypeScript Bot Date: Tue, 10 Aug 2021 11:48:39 -0700 Subject: [PATCH] Cherry-pick PR #45394 into release-4.4 (#45396) Component commits: 61dd78f65d Use getFileAndProject in session provideInlayHints to ensure language service updates are applied Co-authored-by: Wesley Wigham --- src/server/session.ts | 4 +- src/testRunner/tsconfig.json | 1 + .../unittests/tsserver/inlayHints.ts | 63 +++++++++++++++++++ 3 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 src/testRunner/unittests/tsserver/inlayHints.ts diff --git a/src/server/session.ts b/src/server/session.ts index a359847865545..f08d809a171d3 100644 --- a/src/server/session.ts +++ b/src/server/session.ts @@ -1453,9 +1453,9 @@ namespace ts.server { } private provideInlayHints(args: protocol.InlayHintsRequestArgs) { - const { file, languageService } = this.getFileAndLanguageServiceForSyntacticOperation(args); + const { file, project } = this.getFileAndProject(args); const scriptInfo = this.projectService.getScriptInfoForNormalizedPath(file)!; - const hints = languageService.provideInlayHints(file, args, this.getPreferences(file)); + const hints = project.getLanguageService().provideInlayHints(file, args, this.getPreferences(file)); return hints.map(hint => ({ ...hint, diff --git a/src/testRunner/tsconfig.json b/src/testRunner/tsconfig.json index e4274fabe1b11..d5aa42ef79bbd 100644 --- a/src/testRunner/tsconfig.json +++ b/src/testRunner/tsconfig.json @@ -191,6 +191,7 @@ "unittests/tsserver/getExportReferences.ts", "unittests/tsserver/getFileReferences.ts", "unittests/tsserver/importHelpers.ts", + "unittests/tsserver/inlayHints.ts", "unittests/tsserver/inferredProjects.ts", "unittests/tsserver/jsdocTag.ts", "unittests/tsserver/languageService.ts", diff --git a/src/testRunner/unittests/tsserver/inlayHints.ts b/src/testRunner/unittests/tsserver/inlayHints.ts new file mode 100644 index 0000000000000..837bc9c452e6a --- /dev/null +++ b/src/testRunner/unittests/tsserver/inlayHints.ts @@ -0,0 +1,63 @@ +namespace ts.projectSystem { + describe("unittests:: tsserver:: inlayHints", () => { + const configFile: File = { + path: "/a/b/tsconfig.json", + content: "{}" + }; + const app: File = { + path: "/a/b/app.ts", + content: "declare function foo(param: any): void;\nfoo(12);" + }; + + it("with updateOpen request does not corrupt documents", () => { + const host = createServerHost([app, commonFile1, commonFile2, libFile, configFile]); + const session = createSession(host); + session.executeCommandSeq({ + command: protocol.CommandTypes.Open, + arguments: { file: app.path } + }); + session.executeCommandSeq({ + command: protocol.CommandTypes.Configure, + arguments: { + preferences: { + includeInlayParameterNameHints: "all" + } as UserPreferences + } + }); + verifyInlayHintResponse(session); + session.executeCommandSeq({ + command: protocol.CommandTypes.UpdateOpen, + arguments: { + changedFiles: [{ fileName: app.path, textChanges: [{ start: { line: 1, offset: 39 }, end: { line: 1, offset: 39 }, newText: "//" }] }] + } + }); + verifyInlayHintResponse(session); + session.executeCommandSeq({ + command: protocol.CommandTypes.UpdateOpen, + arguments: { + changedFiles: [{ fileName: app.path, textChanges: [{ start: { line: 1, offset: 41 }, end: { line: 1, offset: 41 }, newText: "c" }] }] + } + }); + verifyInlayHintResponse(session); + + function verifyInlayHintResponse(session: TestSession) { + verifyParamInlayHint(session.executeCommandSeq({ + command: protocol.CommandTypes.ProvideInlayHints, + arguments: { + file: app.path, + start: 0, + length: app.content.length, + } + }).response as protocol.InlayHintItem[] | undefined); + } + + function verifyParamInlayHint(response: protocol.InlayHintItem[] | undefined) { + Debug.assert(response); + Debug.assert(response[0]); + Debug.assertEqual(response[0].text, "param:"); + Debug.assertEqual(response[0].position.line, 2); + Debug.assertEqual(response[0].position.offset, 5); + } + }); + }); +}