Skip to content

Commit

Permalink
Grammar: Fix comments in template params not tokenized (#3018)
Browse files Browse the repository at this point in the history
fix #3017

---------

Co-authored-by: Mark Cowlishaw <[email protected]>
  • Loading branch information
timotheeguerin and markcowl authored Mar 22, 2024
1 parent 06f514d commit e0dfe6f
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
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

0 comments on commit e0dfe6f

Please sign in to comment.