Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grammar: Fix comments in template params not tokenized #3018

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: fix
packages:
- "@typespec/compiler"
---

Grammar: Fix comments in template params not tokenized
9 changes: 9 additions & 0 deletions grammars/typespec.json
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,9 @@
{
"include": "#token"
},
{
"include": "#type-parameters"
},
{
"include": "#interface-heritage"
},
Expand Down Expand Up @@ -1155,6 +1158,9 @@
},
"end": "(?=>)|(?=,|;|@|\\)|\\}|\\b(?:extern)\\b|\\b(?:namespace|model|op|using|import|enum|alias|union|interface|dec|fn)\\b)",
"patterns": [
{
"include": "#token"
},
{
"include": "#type-parameter-constraint"
},
Expand Down Expand Up @@ -1229,6 +1235,9 @@
{
"include": "#token"
},
{
"include": "#type-parameters"
},
{
"include": "#expression"
}
Expand Down
5 changes: 3 additions & 2 deletions packages/compiler/src/server/tmlanguage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ const typeParameter: BeginEndRule = {
"1": { scope: "entity.name.type.tsp" },
},
end: `(?=>)|${universalEnd}`,
patterns: [typeParameterConstraint, typeParameterDefault],
patterns: [token, typeParameterConstraint, typeParameterDefault],
};

const typeParameters: BeginEndRule = {
Expand Down Expand Up @@ -522,7 +522,7 @@ const unionStatement: BeginEndRule = {
"1": { scope: "keyword.other.tsp" },
},
end: `(?<=\\})|${universalEnd}`,
patterns: [token, expression],
patterns: [token, typeParameters, expression],
};

const aliasStatement: BeginEndRule = {
Expand Down Expand Up @@ -663,6 +663,7 @@ const interfaceStatement: BeginEndRule = {
end: `(?<=\\})|${universalEnd}`,
patterns: [
token,
typeParameters,
interfaceHeritage, // before expression or extends will look like type name
interfaceBody, // before expression or { will match model expression
expression, // enough to match name and type parameters
Expand Down
44 changes: 44 additions & 0 deletions packages/compiler/test/server/colorization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { TypeSpecScope } from "../../src/server/tmlanguage.js";
import { SemanticToken, SemanticTokenKind } from "../../src/server/types.js";
import { createTestServerHost } from "../../src/testing/test-server-host.js";
import { findTestPackageRoot } from "../../src/testing/test-utils.js";
import { deepEquals } from "../../src/utils/index.js";

const { parseRawGrammar, Registry } = vscode_textmate;
const { createOnigScanner, createOnigString, loadWASM } = vscode_oniguruma;
Expand Down Expand Up @@ -1029,6 +1030,49 @@ function testColorization(description: string, tokenize: Tokenize) {
Token.comment.block("*/"),
]);
});

describe("in template parameters", () => {
it.each([
[
"alias",
`alias Foo<T // comment
> = T`,
],
[
"model",
`model Foo<T // comment
> {}`,
],
[
"union",
`union Foo<T // comment
> {}`,
],
[
"interface",
`interface Foo<T // comment
> {}`,
],
[
"operation",
`op foo<T // comment
>(): void;`,
],
])("%s", async () => {
const tokens = await tokenize(`alias Foo<T // comment
> = T`);

const index = tokens.findIndex((x) =>
deepEquals(x, Token.punctuation.typeParameters.begin)
);
deepStrictEqual(tokens.slice(index, index + 4), [
Token.punctuation.typeParameters.begin,
Token.identifiers.type("T"),
Token.comment.line("// comment"),
Token.punctuation.typeParameters.end,
]);
});
});
});
}

Expand Down
Loading