Skip to content
This repository has been archived by the owner on May 30, 2024. It is now read-only.

Improve GraphQL regex to match 'GQL' #1020

Merged
merged 1 commit into from
Feb 5, 2024

Conversation

vitorascorrea
Copy link
Contributor

Motivation

Background

Using a heredoc to syntax highlight GraphQL snippets currently breaks the rest of the file's highlighting: image

I investigated it briefly and apparently, there is a conflict between the GraphQL Syntax highlighting extension and Ruby LSP. Two evidences that this is the case:

  1. When we inspect the tokens, we see that the token that is being displayed is the one in the GraphQL extension grammar.

image

  1. Ruby LSP grammar for GraphQL has an additional match for the backtick symbol (`) that is not present in the GraphQL Syntax extension. If we try to use it, the highlighting works as expected:

image

I'm not how to make Ruby LSP grammar take precedence over the GraphQL extension grammar (maybe using injectionSelector).

Alternative

As I've seen in other codebases, there is also the common usage of GQL to delimit the GraphQL syntax highlight block. Since the Ruby LSP regex nor the GraphQL Syntax highlight regex matches it, if we add it here we gain precedence and are able to correctly highlight our whole file:

image

Implementation

Added the keyword GQL to be matched by the GraphQL grammar block begin regex, the same syntax that is used to match both JS|JAVASCRIPT.

Automated Tests

Updated how we test for GraphQL, moving it to an object that contains both delimiters ["GQL", "GRAPHQL"]

Manual Tests

Writing a GraphQL snippet that starts and ends with GQL should highlight only the block between them and keep the rest of the file syntax highlight working.

@vitorascorrea vitorascorrea requested a review from a team as a code owner February 2, 2024 22:11
Copy link
Member

@vinistock vinistock left a comment

Choose a reason for hiding this comment

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

Thank you for the contribution!

@vinistock vinistock merged commit 476ac47 into main Feb 5, 2024
10 checks passed
@vinistock vinistock deleted the vitorascorrea/add-gql-as-heredoc-match branch February 5, 2024 21:08
@vinistock vinistock added the enhancement New feature or request label Feb 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants