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

Adjust TS/JS regexp for Document detection, also adjust TextMate #179

Merged
merged 6 commits into from
Aug 23, 2024

Conversation

phryneas
Copy link
Member

@phryneas phryneas commented Aug 22, 2024

Before:
image
After:
image
Before:
image
After:
image
Before:
image
After:
image
Before:
image
After:
image

}
`;

// syntax highlighting cannot work in all examples after this - textmate grammars don't work over multiple lines like this
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we cannot syntax highlight anything after that line, the other features will still work. We could disable them with stricter regular expressions, but I'm not sure if we should.

Comment on lines +219 to +227
Array [
"Q1",
"Q2",
"Q3",
"Q4",
"Q6",
"Q8",
"Q9",
"Q10",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We just test that all queries in the fixture are found by looking at the name, and the documentName function also checks that there were no syntax errors in the document.

# ^^^ source.ts meta.block.ts meta.var.expr.ts
# ^^^^^^^ source.ts meta.block.ts meta.var.expr.ts markup.italic
# ^^^^ source.ts meta.block.ts meta.var.expr.ts
# ^^^^^ source.ts meta.block.ts meta.var.expr.ts meta.embedded.block.graphql keyword.operation.graphql
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shows that everything above the ^^^ here was detected as both meta.embedded.block.graphql and keyword.operation.graphql

# ^ source.ts meta.block.ts meta.var.expr.ts keyword.operator.assignment.ts
# ^ source.ts meta.block.ts meta.var.expr.ts
# ^^^ source.ts meta.block.ts meta.var.expr.ts
# ^^^^^^^ source.ts meta.block.ts meta.var.expr.ts markup.italic
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've chosen to make the word GraphQL in the /* GraphQL */ comment here italic to highlight that it does something.

Comment on lines -11 to -14
"name": "entity.name.function.tagged-template.js"
"name": "meta.embedded.block.graphql comment.line.graphql.js"
},
"2": {
"name": "punctuation.definition.string.template.begin.js"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did remove these entity.name.function.tagged-template.js and punctuation.definition.string.template.begin.js since we don't want our grammar to attack tokens and highlight those, but we want the parent language to add them.
Instead, for only this pattern, we add meta.embedded.block.graphql comment.line.graphql.js", as the source.graphql matcher will only start working between start and end pattern, but we also want the GraphQL in the start pattern to be properly highlighted.

The other patterns don't add capture names, apart from markup.italic on the word gql/graphql/GraphQL inside of comments.

@@ -3,43 +3,40 @@
"injectionSelector": "L:source -string -comment",
"patterns": [
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is split into four patterns now instead of one pattern that kinda tries to capture everything with a monster RegExp.

@@ -3,43 +3,40 @@
"injectionSelector": "L:source -string -comment",
"patterns": [
{
"name": "taggedTemplates",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That name was applied to all the captures, but didn't seem to do anything. It's optional, so I removed it.

"contentName": "meta.embedded.block.graphql",
"begin": "(Relay\\.QL|gql|graphql(\\.experimental)?)\\(?(`)",
"begin": "(?:Relay\\.QL|gql|graphql(\\.experimental)?)\\s*(?:<.*?>\\s*)?`",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We now also allow for TypeScript generics here - (?:<.*?>\\s*)?.
That might come in handy if we add those in a future version of graphql-tag

@phryneas phryneas requested a review from jerelmiller August 22, 2024 11:44
@phryneas phryneas marked this pull request as ready for review August 22, 2024 11:45
Copy link
Member

@jerelmiller jerelmiller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spotted only one minor thing, but otherwise looks great! Cool to see how some of the syntax highlighting works behind the scenes.

src/language-server/document.ts Outdated Show resolved Hide resolved
@phryneas phryneas merged commit b4687eb into main Aug 23, 2024
7 checks passed
@phryneas phryneas deleted the pr/adjust-tsjs-regexp branch August 23, 2024 16:21
@github-actions github-actions bot mentioned this pull request Aug 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants