Skip to content

Commit

Permalink
Merge pull request #159 from ember-tooling/sync-injections-with-nvim-…
Browse files Browse the repository at this point in the history
…treesitter

Sync injections with nvim treesitter
  • Loading branch information
NullVoxPopuli authored Aug 20, 2024
2 parents 5b54907 + 69b18de commit da605af
Show file tree
Hide file tree
Showing 7 changed files with 623 additions and 65 deletions.
42 changes: 42 additions & 0 deletions .github/workflows/linting.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Linting

on:
pull_request:
push:
branches:
- master
- main

concurrency:
group: ${{github.workflow}}-${{github.ref}}
cancel-in-progress: true

jobs:
js:
name: "JavaScript"
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
- run: npm install
- run: npm exec prettier --check .

format-queries:
name: Queries
runs-on: ubuntu-latest
env:
NVIM_TAG: nightly
# NVIM_TAG: stable
steps:
- uses: actions/checkout@v4
- name: Prepare
run: |
bash ./scripts/ci-install.sh
- name: Lint
run: |
nvim --version
nvim --headless -c "TSInstallSync query" -c "q"
nvim -l scripts/format-queries.lua
git diff --exit-code
5 changes: 3 additions & 2 deletions queries/glimmer/folds.scm
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[
(element_node (element_node_start))
(block_statement)
(element_node
(element_node_start))
(block_statement)
] @fold
107 changes: 68 additions & 39 deletions queries/glimmer/highlights.scm
Original file line number Diff line number Diff line change
@@ -1,84 +1,113 @@
; === Tag Names ===

; Tags that start with a lower case letter are HTML tags
; We'll also use this highlighting for named blocks (which start with `:`)
((tag_name) @tag
(#match? @tag "^(:)?[a-z]"))
(#lua-match? @tag "^:?[%l]"))

; Tags that start with a capital letter are Glimmer components
((tag_name) @constructor
(#match? @constructor "^[A-Z]"))
(#lua-match? @constructor "^%u"))

(attribute_name) @property
(attribute_name) @attribute

(string_literal) @string

(number_literal) @number

(boolean_literal) @boolean

(concat_statement) @string

; === Block Statements ===

; Highlight the brackets
(block_statement_start) @tag.delimiter

(block_statement_end) @tag.delimiter

; Highlight `if`/`each`/`let`
(block_statement_start path: (identifier) @conditional)
(block_statement_end path: (identifier) @conditional)
((mustache_statement (identifier) @conditional)
(#match? @conditional "else"))
(block_statement_start
path: (identifier) @keyword.conditional)

; == Mustache Statements ===
(block_statement_end
path: (identifier) @keyword.conditional)

((mustache_statement
(identifier) @keyword.conditional)
(#lua-match? @keyword.conditional "else"))

; Hightlight the whole statement, to color brackets and separators
; == Mustache Statements ===
; Highlight the whole statement, to color brackets and separators
(mustache_statement) @tag.delimiter

; An identifier in a mustache expression is a variable
((mustache_statement [
(path_expression (identifier) @variable)
(identifier) @variable
((mustache_statement
[
(path_expression
(identifier) @variable)
(identifier) @variable
])
(#not-match? @variable "yield|outlet|this|else"))
(#not-any-of? @variable "yield" "outlet" "this" "else"))

; As are arguments in a block statement
(block_statement_start argument: [
(path_expression (identifier) @variable)
(identifier) @variable
(block_statement_start
argument: [
(path_expression
(identifier) @variable)
(identifier) @variable
])

; As is an identifier in a block param
(block_params (identifier) @variable)
(block_params
(identifier) @variable)

; As are helper arguments
((helper_invocation argument: [
(path_expression (identifier) @variable)
(identifier) @variable
((helper_invocation
argument: [
(path_expression
(identifier) @variable)
(identifier) @variable
])
(#not-match? @variable "this"))
(#not-eq? @variable "this"))

; `this` should be highlighted as a built-in variable
((identifier) @variable.builtin
(#match? @variable.builtin "this"))
(#eq? @variable.builtin "this"))

; If the identifier is just "yield" or "outlet", it's a keyword
((mustache_statement (identifier) @keyword)
(#match? @keyword "yield|outlet"))
((mustache_statement
(identifier) @keyword)
(#any-of? @keyword "yield" "outlet"))

; Helpers are functions
((helper_invocation helper: [
(path_expression (identifier) @function)
(identifier) @function
((helper_invocation
helper: [
(path_expression
(identifier) @function)
(identifier) @function
])
(#not-match? @function "if|yield"))
((helper_invocation helper: (identifier) @conditional)
(#match? @conditional "if"))
((helper_invocation helper: (identifier) @keyword)
(#match? @keyword "yield"))
(#not-any-of? @function "if" "yield"))

((helper_invocation
helper: (identifier) @keyword.conditional)
(#eq? @keyword.conditional "if"))

((helper_invocation
helper: (identifier) @keyword)
(#eq? @keyword "yield"))

(hash_pair
key: (identifier) @property)

(hash_pair key: (identifier) @property)
(comment_statement) @comment @spell

(comment_statement) @comment
(attribute_node
"=" @operator)

(attribute_node "=" @operator)
(block_params
"as" @keyword)

(block_params "as" @keyword)
(block_params "|" @operator)
(block_params
"|" @operator)

[
"<"
Expand Down
34 changes: 23 additions & 11 deletions queries/glimmer/indents.scm
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
[
(element_node (element_node_start))
(element_node
(element_node_start))
(element_node_void)
(block_statement (block_statement_start))
(block_statement
(block_statement_start))
(mustache_statement)
] @indent.begin

(element_node (element_node_end [">"] @indent.end))
(element_node_void "/>" @indent.end)
(element_node
(element_node_end
">" @indent.end))

(element_node_void
"/>" @indent.end)

[
">"
"/>"
"</"
">"
"/>"
"</"
"{{/"
"}}"
] @indent.branch
] @indent.branch

(mustache_statement
(helper_invocation helper: (identifier) @_identifier (#lua-match? @_identifier "else"))
) @indent.branch
(mustache_statement ((identifier) @_identifier (#lua-match? @_identifier "else"))) @indent.branch
(helper_invocation
helper: (identifier) @_identifier
(#eq? @_identifier "else"))) @indent.branch

(mustache_statement
((identifier) @_identifier
(#eq? @_identifier "else"))) @indent.branch

(comment_statement) @indent.ignore
18 changes: 5 additions & 13 deletions queries/glimmer/injections.scm
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
; comments
((comment_statement) @injection.content
(#set! injection.language "comment"))
(#set! injection.language "comment"))

; <style> tags
((element_node
(element_node_start
(tag_name) @_tag_name
(#eq? @_tag_name "style")
)
)
@injection.content
(#eq? @_tag_name "style"))) @injection.content
(#offset! @injection.content 0 7 0 -8)
(#set! injection.language "css")
(#set! injection.include-children)
)
(#set! injection.include-children))

; <script> tags
((element_node
(element_node_start
(tag_name) @_tag_name
(#eq? @_tag_name "script")
)
)
@injection.content
(#eq? @_tag_name "script"))) @injection.content
(#offset! @injection.content 0 8 0 -9)
(#set! injection.language "glimmer_javascript")
(#set! injection.include-children)
)
(#set! injection.include-children))
31 changes: 31 additions & 0 deletions scripts/ci-install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash

set -e

mkdir -p /tmp/ci-linting/
cd /tmp/ci-linting/
git clone https://github.com/nvim-treesitter/nvim-treesitter.git
cd nvim-treesitter

os=$(uname -s)
if [[ $os == Linux ]]; then
wget https://github.com/neovim/neovim/releases/download/${NVIM_TAG}/nvim-linux64.tar.gz
tar -zxf nvim-linux64.tar.gz
sudo ln -s "$PWD"/nvim-linux64/bin/nvim /usr/local/bin
rm -rf "$PWD"/nvim-linux64/lib/nvim/parser
mkdir -p ~/.local/share/nvim/site/pack/nvim-treesitter/start
ln -s "$PWD" ~/.local/share/nvim/site/pack/nvim-treesitter/start
elif [[ $os == Darwin ]]; then
RELEASE_NAME="nvim-macos-$(uname -m)"
curl -L "https://github.com/neovim/neovim/releases/download/${NVIM_TAG}/$RELEASE_NAME.tar.gz" | tar -xz
sudo ln -s "$PWD/$RELEASE_NAME/bin/nvim" /usr/local/bin
rm -rf "$PWD/$RELEASE_NAME/lib/nvim/parser"
mkdir -p ~/.local/share/nvim/site/pack/nvim-treesitter/start
ln -s "$PWD" ~/.local/share/nvim/site/pack/nvim-treesitter/start
else
curl -L https://github.com/neovim/neovim/releases/download/${NVIM_TAG}/nvim-win64.zip -o nvim-win64.zip
unzip nvim-win64
mkdir -p ~/AppData/Local/nvim/pack/nvim-treesitter/start
mkdir -p ~/AppData/Local/nvim-data
cp -r "$PWD" ~/AppData/Local/nvim/pack/nvim-treesitter/start
fi
Loading

0 comments on commit da605af

Please sign in to comment.