Skip to content

Commit 8774715

Browse files
authored
[typespec-vscode] Meta property autocomplete (microsoft#5353)
Fix microsoft#4976 metadata will prompt after '::', i.e. 'type', 'parameters', 'returnType'
1 parent 5e26257 commit 8774715

File tree

3 files changed

+118
-1
lines changed

3 files changed

+118
-1
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
3+
changeKind: fix
4+
packages:
5+
- "@typespec/compiler"
6+
---
7+
8+
Meta property are auto-completed, current only supported '::type', '::parameters', '::returnType'

packages/compiler/src/core/checker.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2928,12 +2928,30 @@ export function createChecker(program: Program, resolver: NameResolver): Checker
29282928
}
29292929
} else if (identifier.parent && identifier.parent.kind === SyntaxKind.MemberExpression) {
29302930
let base = resolver.getNodeLinks(identifier.parent.base).resolvedSymbol;
2931+
29312932
if (base) {
29322933
if (base.flags & SymbolFlags.Alias) {
29332934
base = getAliasedSymbol(base, undefined);
29342935
}
2936+
29352937
if (base) {
2936-
addCompletions(base.exports ?? base.members);
2938+
if (identifier.parent.selector === "::") {
2939+
if (base?.node === undefined && base?.declarations && base.declarations.length > 0) {
2940+
// Process meta properties separately, such as `::parameters`, `::returnType`
2941+
const nodeModels = base?.declarations[0];
2942+
if (nodeModels.kind === SyntaxKind.OperationStatement) {
2943+
const operation = nodeModels as OperationStatementNode;
2944+
addCompletion("parameters", operation.symbol);
2945+
addCompletion("returnType", operation.symbol);
2946+
}
2947+
} else if (base?.node?.kind === SyntaxKind.ModelProperty) {
2948+
// Process meta properties separately, such as `::type`
2949+
const metaProperty = base.node as ModelPropertyNode;
2950+
addCompletion("type", metaProperty.symbol);
2951+
}
2952+
} else {
2953+
addCompletions(base.exports ?? base.members);
2954+
}
29372955
}
29382956
}
29392957
} else {

packages/compiler/test/server/completion.test.ts

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,97 @@ describe("identifiers", () => {
479479
]);
480480
});
481481

482+
it("completes meta property '::type' on model property", async () => {
483+
const completions = await complete(
484+
`
485+
model A{
486+
name: string;
487+
}
488+
489+
model B{
490+
a: A;
491+
}
492+
493+
model C {
494+
...B.a::┆;
495+
}
496+
`,
497+
);
498+
499+
check(completions, [
500+
{
501+
label: "type",
502+
insertText: "type",
503+
kind: CompletionItemKind.Field,
504+
documentation: {
505+
kind: MarkupKind.Markdown,
506+
value: "(model property)\n```typespec\nB.a: A\n```",
507+
},
508+
},
509+
]);
510+
});
511+
512+
it("completes meta property '::parameters' and '::returnType' on operation", async () => {
513+
const completions = await complete(
514+
`
515+
op base(one: string): void;
516+
@@doc(base::par┆, "Override");
517+
`,
518+
);
519+
520+
check(completions, [
521+
{
522+
label: "parameters",
523+
insertText: "parameters",
524+
kind: CompletionItemKind.Method,
525+
documentation: {
526+
kind: MarkupKind.Markdown,
527+
value: "```typespec\nop base(one: string): void\n```",
528+
},
529+
},
530+
{
531+
label: "returnType",
532+
insertText: "returnType",
533+
kind: CompletionItemKind.Method,
534+
documentation: {
535+
kind: MarkupKind.Markdown,
536+
value: "```typespec\nop base(one: string): void\n```",
537+
},
538+
},
539+
]);
540+
});
541+
542+
it("completes meta property '::parameters' and '::returnType' using alias on operation", async () => {
543+
const completions = await complete(
544+
`
545+
op a(@doc("base doc") one: string): void;
546+
op b is a;
547+
@@doc(b::par┆, "override for b");
548+
`,
549+
);
550+
551+
check(completions, [
552+
{
553+
label: "parameters",
554+
insertText: "parameters",
555+
kind: CompletionItemKind.Method,
556+
documentation: {
557+
kind: MarkupKind.Markdown,
558+
value: "```typespec\nop b(one: string): void\n```",
559+
},
560+
},
561+
{
562+
label: "returnType",
563+
insertText: "returnType",
564+
kind: CompletionItemKind.Method,
565+
documentation: {
566+
kind: MarkupKind.Markdown,
567+
value: "```typespec\nop b(one: string): void\n```",
568+
},
569+
},
570+
]);
571+
});
572+
482573
it("completes partial identifiers", async () => {
483574
const completions = await complete(
484575
`

0 commit comments

Comments
 (0)