From f0c5ba08f63c60f7542dfd3592c4cfd42bd579bc Mon Sep 17 00:00:00 2001 From: ivanwonder Date: Thu, 22 Jul 2021 17:54:26 +0800 Subject: [PATCH] fix(language-service): global autocomplete doesn't work when the user tries to modify the symbol (#42923) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When the user tries to trigger suggestions from an interruption, the LS should provide the global completions. For example, `[input]="t¦"`, the `t` can be the `true` or the symbol from the component context. PR Close #42923 --- .../src/ngtsc/typecheck/src/completion.ts | 15 ++++++++++++++- .../language-service/ivy/test/completions_spec.ts | 12 ++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/packages/compiler-cli/src/ngtsc/typecheck/src/completion.ts b/packages/compiler-cli/src/ngtsc/typecheck/src/completion.ts index 645a628967..48e64cb304 100644 --- a/packages/compiler-cli/src/ngtsc/typecheck/src/completion.ts +++ b/packages/compiler-cli/src/ngtsc/typecheck/src/completion.ts @@ -7,7 +7,7 @@ */ import {TmplAstReference, TmplAstTemplate} from '@angular/compiler'; -import {AST, EmptyExpr, LiteralPrimitive, MethodCall, PropertyRead, PropertyWrite, SafeMethodCall, SafePropertyRead, TmplAstNode} from '@angular/compiler/src/compiler'; +import {AST, EmptyExpr, ImplicitReceiver, LiteralPrimitive, MethodCall, PropertyRead, PropertyWrite, SafeMethodCall, SafePropertyRead, TmplAstNode} from '@angular/compiler/src/compiler'; import {TextAttribute} from '@angular/compiler/src/render3/r3_ast'; import * as ts from 'typescript'; @@ -91,6 +91,19 @@ export class CompletionEngine { } } + if (node instanceof PropertyRead && node.receiver instanceof ImplicitReceiver) { + const nodeLocation = findFirstMatchingNode(this.tcb, { + filter: ts.isPropertyAccessExpression, + withSpan: node.sourceSpan, + }); + if (nodeLocation) { + nodeContext = { + shimPath: this.shimPath, + positionInShimFile: nodeLocation.getStart(), + }; + } + } + return { componentContext: this.componentContext, templateContext, diff --git a/packages/language-service/ivy/test/completions_spec.ts b/packages/language-service/ivy/test/completions_spec.ts index 6a2a05b806..83c565119f 100644 --- a/packages/language-service/ivy/test/completions_spec.ts +++ b/packages/language-service/ivy/test/completions_spec.ts @@ -234,6 +234,18 @@ describe('completions', () => { const {templateFile} = setup(``, '', DIR_WITH_UNION_TYPE_INPUT); templateFile.moveCursorToText('dir [myInput]="¦">'); + const completions = templateFile.getCompletionsAtPosition(); + expectContain(completions, ts.ScriptElementKind.string, [`'foo'`, '42']); + expectContain(completions, ts.ScriptElementKind.keyword, ['null']); + expectContain(completions, ts.ScriptElementKind.variableElement, ['undefined']); + expectDoesNotContain(completions, ts.ScriptElementKind.parameterElement, ['ctx']); + }); + + it('should return completions of string literals, number literals, `true`, `false`, `null` and `undefined` when the user tries to modify the symbol', + () => { + const {templateFile} = setup(``, '', DIR_WITH_UNION_TYPE_INPUT); + templateFile.moveCursorToText('dir [myInput]="a¦">'); + const completions = templateFile.getCompletionsAtPosition(); expectContain(completions, ts.ScriptElementKind.string, [`'foo'`, '42']); expectContain(completions, ts.ScriptElementKind.keyword, ['null']);