nvim-treesitter / nvim-treesitter

Nvim Treesitter configurations and abstraction layer
Apache License 2.0
10.35k stars 869 forks source link

`Makefile` highlighting some values and arguments as text #6639

Open AlejandroSuero opened 3 months ago

AlejandroSuero commented 3 months ago

Describe the highlighting problem

Example snippet that causes the problem

define style_calls
    $(eval $@_msg = $(1))
    echo ${GREEN}${$@_msg}
    echo ${RESTORE}
endef

.PHONY: lint format

lint:
    @$(call style_calls,"Linting lua files")
    @selene --display-style quiet --config ./selene.toml lua/supermaven-nvim
    @$(call style_calls,"Running stylua check")
    @stylua --color always -f ./stylua.toml --check .

Tree-sitter parsing result

(variable_assignment) ; [1:1 - 2:0]
 name: (word) ; [1:1 - 5]
 value: (text) ; [1:7 - 19]
(variable_assignment) ; [2:1 - 4:0]
 name: (word) ; [2:1 - 7]
 value: (text) ; [2:9 - 17]
(comment) ; [4:1 - 46]
(define_directive) ; [5:1 - 11:0]
 name: (word) ; [5:8 - 18]
 value: (raw_text) ; [6:1 - 9:0]
(rule) ; [11:1 - 13:0]
 (targets) ; [11:1 - 6]
  (word) ; [11:1 - 6]
 normal: (prerequisites) ; [11:9 - 19]
  (word) ; [11:9 - 12]
  (word) ; [11:14 - 19]
(rule) ; [13:1 - 19:0]
 (targets) ; [13:1 - 4]
  (word) ; [13:1 - 4]
 (recipe) ; [13:6 - 19:0]
  (recipe_line) ; [14:2 - 41]
   (shell_text) ; [14:3 - 41]
    (function_call) ; [14:3 - 41]
     (arguments) ; [14:10 - 40]
      argument: (text) ; [14:10 - 40]
  (recipe_line) ; [15:2 - 73]
   (shell_text) ; [15:3 - 73]
    (command) ; [15:3 - 73]
     name: (command_name) ; [15:3 - 8]
      (word) ; [15:3 - 8]
     argument: (word) ; [15:10 - 24]
     argument: (word) ; [15:26 - 30]
     argument: (word) ; [15:32 - 39]
     argument: (word) ; [15:41 - 53]
     argument: (word) ; [15:55 - 73]
  (recipe_line) ; [16:2 - 44]
   (shell_text) ; [16:3 - 44]
    (function_call) ; [16:3 - 44]
     (arguments) ; [16:10 - 43]
      argument: (text) ; [16:10 - 43]
  (recipe_line) ; [17:2 - 50]
   (shell_text) ; [17:3 - 50]
    (command) ; [17:3 - 50]
     name: (command_name) ; [17:3 - 8]
      (word) ; [17:3 - 8]
     argument: (word) ; [17:10 - 16]
     argument: (word) ; [17:18 - 23]
     argument: (word) ; [17:25 - 26]
     argument: (word) ; [17:28 - 40]
     argument: (word) ; [17:42 - 48]
     argument: (word) ; [17:50 - 50]
(rule) ; [19:1 - 23:0]
 (targets) ; [19:1 - 6]
  (word) ; [19:1 - 6]
 (recipe) ; [19:8 - 23:0]
  (recipe_line) ; [20:2 - 45]
   (shell_text) ; [20:3 - 45]
    (function_call) ; [20:3 - 45]
     (arguments) ; [20:10 - 44]
      argument: (text) ; [20:10 - 44]
  (recipe_line) ; [21:2 - 42]
   (shell_text) ; [21:3 - 42]
    (command) ; [21:3 - 42]
     name: (command_name) ; [21:3 - 8]
      (word) ; [21:3 - 8]
     argument: (word) ; [21:10 - 16]
     argument: (word) ; [21:18 - 23]
     argument: (word) ; [21:25 - 26]
     argument: (word) ; [21:28 - 40]
     argument: (word) ; [21:42 - 42]
(rule) ; [23:1 - 24:0]
 (targets) ; [23:1 - 3]
  (word) ; [23:1 - 3]
 normal: (prerequisites) ; [23:6 - 16]
  (word) ; [23:6 - 9]
  (word) ; [23:11 - 16]

Example screenshot

define call values as raw_text arguments of call in target as text :Inspect define :Inspect call
-- with lazy
return {
  "rose-pine/neovim",
  name = "rose-pine",
  priority = 1000,
  lazy = false,
  config = function()
    require("rose-pine").setup({
      variant = "moon", -- auto, main, moon, or dawn
      dark_variant = "main", -- main, moon, or dawn
      dim_inactive_windows = true,

      enable = {
        terminal = true,
        legacy_highlights = true, -- Enabling or disabling this takes no effect on `Makefile`
        migrations = true, -- Handle deprecated options automatically
      },

      styles = {
        bold = true,
        italic = false,
        transparency = vim.g.transparency,
      },
    })
  end,
}

Expected behavior

To have highlights other than raw_text or text, at least the strings.

Output of :checkhealth nvim-treesitter

nvim-treesitter: require("nvim-treesitter.health").check()

Installation ~
- OK `tree-sitter` found 0.22.6 (parser generator, only needed for :TSInstallFromGrammar)
- OK `node` found v20.13.1 (only needed for :TSInstallFromGrammar)
- OK `git` executable found.
- OK `cc` executable found. Selected from { vim.NIL, "cc", "gcc", "clang", "cl", "zig" }
  Version: Apple clang version 15.0.0 (clang-1500.3.9.4)
- OK Neovim was compiled with tree-sitter runtime ABI version 14 (required >=13). Parsers must be compatible with runtime ABI.

OS Info:
{
  machine = "arm64",
  release = "23.4.0",
  sysname = "Darwin",
  version = "Darwin Kernel Version 23.4.0: Fri Mar 15 00:12:41 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T8103"
} ~

Parser/Features         H L F I J
  - astro               ✓ ✓ ✓ ✓ ✓
  - bash                ✓ ✓ ✓ . ✓
  - c                   ✓ ✓ ✓ ✓ ✓
  - css                 ✓ . ✓ ✓ ✓
  - csv                 ✓ . . . .
  - dockerfile          ✓ . . . ✓
  - git_config          ✓ . ✓ . ✓
  - git_rebase          ✓ . . . ✓
  - gitattributes       ✓ ✓ . . ✓
  - gitcommit           ✓ . . . ✓
  - gitignore           ✓ . . . .
  - go                  ✓ ✓ ✓ ✓ ✓
  - graphql             ✓ . . ✓ ✓
  - html                ✓ ✓ ✓ ✓ ✓
  - http                ✓ . . . ✓
  - javascript          ✓ ✓ ✓ ✓ ✓
  - jsdoc               ✓ . . . .
  - json                ✓ ✓ ✓ ✓ .
  - jsonc               ✓ ✓ ✓ ✓ ✓
  - lua                 ✓ ✓ ✓ ✓ ✓
  - make                ✓ . ✓ . ✓
  - markdown            ✓ . ✓ ✓ ✓
  - markdown_inline     ✓ . . . ✓
  - php                 ✓ ✓ ✓ ✓ ✓
  - prisma              ✓ . ✓ . .
  - python              ✓ ✓ ✓ ✓ ✓
  - query               ✓ ✓ ✓ ✓ ✓
  - rust                ✓ ✓ ✓ ✓ ✓
  - svelte              ✓ ✓ ✓ ✓ ✓
  - templ               ✓ . . . ✓
  - tmux                ✓ . . . ✓
  - toml                ✓ ✓ ✓ ✓ ✓
  - tsx                 ✓ ✓ ✓ ✓ ✓
  - typescript          ✓ ✓ ✓ ✓ ✓
  - vim                 ✓ ✓ ✓ . ✓
  - vimdoc              ✓ . . . ✓
  - xml                 ✓ ✓ ✓ ✓ ✓
  - yaml                ✓ ✓ ✓ ✓ ✓

  Legend: H[ighlight], L[ocals], F[olds], I[ndents], In[j]ections
         +) multiple parsers found, only one will be used
         x) errors found in the query, try to run :TSUpdate {lang} ~

Output of nvim --version

NVIM v0.9.5
Build type: RelWithDebInfo
LuaJIT 2.1.1692716794
Compilation: /Library/Developer/CommandLineTools/usr/bin/cc -O2 -g -Og -g -Wall -Wextra -pedantic -Wno-unused-parameter -Wstrict-prototypes -std=gnu99 -Wshadow -Wconversion -Wvla -Wdouble-promotion -Wmissing-noreturn -Wmissing-format-attribute -Wmissing-prototypes -Wimplicit-fallthrough -fdiagnostics-color=always -fstack-protector-strong -DUNIT_TESTING -DINCLUDE_GENERATED_DECLARATIONS -I/Users/aome/neovim/.deps/usr/include/luajit-2.1 -I/opt/homebrew/opt/gettext/include -I/Users/aome/neovim/.deps/usr/include -I/Users/aome/neovim/build/src/nvim/auto -I/Users/aome/neovim/build/include -I/Users/aome/neovim/build/cmake.config -I/Users/aome/neovim/src -I/Library/Developer/CommandLineTools/SDKs/MacOSX14.4.sdk/usr/include -I/Users/aome/neovim/.deps/usr/include -I/Users/aome/neovim/.deps/usr/include -I/Users/aome/neovim/.deps/usr/include -I/Users/aome/neovim/.deps/usr/include -I/Users/aome/neovim/.deps/usr/include -I/Users/aome/neovim/.deps/usr/include

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/usr/local/share/nvim"

Run :checkhealth for more info

Additional context

No response

clason commented 3 months ago

How would you expect them to be highlighted?

AlejandroSuero commented 3 months ago

@clason I am not sure, inside the define call it could treat them as bash|zsh|sh|... commands when in an .{bash|zsh|sh|...} files. For example:

define call inside sh
  (command) ; [31:3 - 20]
   name: (command_name) ; [31:3 - 8]
    (word) ; [31:3 - 8]
   argument: (word) ; [31:10 - 20]
  (command) ; [32:4 - 24]
   name: (command_name) ; [32:4 - 24]
    (command_substitution) ; [32:4 - 24]
     (command) ; [32:6 - 23]
      name: (command_name) ; [32:6 - 9]
       (word) ; [32:6 - 9]
      argument: (concatenation) ; [32:11 - 16]
       (simple_expansion) ; [32:11 - 12]
        (special_variable_name) ; [32:12 - 12]
       (word) ; [32:13 - 16]
      argument: (word) ; [32:18 - 18]
      argument: (command_substitution) ; [32:20 - 23]
       (command) ; [32:22 - 22]
        name: (command_name) ; [32:22 - 22]
         (number) ; [32:22 - 22]
  (command) ; [33:4 - 25]
   name: (command_name) ; [33:4 - 7]
    (word) ; [33:4 - 7]
   argument: (concatenation) ; [33:9 - 25]
    (expansion) ; [33:9 - 16]
     (variable_name) ; [33:11 - 15]
    (ERROR) ; [33:17 - 20]
    (word) ; [33:21 - 24]
    (word) ; [33:25 - 25]
  (command) ; [34:4 - 18]
   name: (command_name) ; [34:4 - 7]
    (word) ; [34:4 - 7]
   argument: (expansion) ; [34:9 - 18]
    (variable_name) ; [34:11 - 17]
  (command) ; [35:3 - 7]
   name: (command_name) ; [35:3 - 7]
    (word) ; [35:3 - 7]

:Inspect: style_calls as @variable.parameter and numbers and Uppercase variables as @string.

target call install sh
  (command) ; [31:3 - 7]
   name: (command_name) ; [31:3 - 7]
    (word) ; [31:3 - 7]
  (command) ; [32:4 - 43]
   name: (command_name) ; [32:4 - 43]
    (concatenation) ; [32:4 - 43]
     (word) ; [32:4 - 4]
     (command_substitution) ; [32:5 - 43]
      (command) ; [32:7 - 42]
       name: (command_name) ; [32:7 - 10]
        (word) ; [32:7 - 10]
       argument: (concatenation) ; [32:12 - 42]
        (word) ; [32:12 - 23]
        (string) ; [32:24 - 42]
         (string_content) ; [32:25 - 41]

:Inspect: style_calls as @variable.parameter and "..." as @string

clason commented 3 months ago

That's not possible, though. The make query can't know what file it is in. Feel free to make a PR adding a bash injection; maybe that is good enough. But if it isn't, then the current state is the best we can do.

AlejandroSuero commented 3 months ago

@clason it will be my first time touching this code base, do you know where should I do it?

clason commented 3 months ago

The relevant query is make/injections.scm.

AlejandroSuero commented 3 months ago

@clason one question I have, what should I do to put the injection?

((comment) @injection.content
  (#set! injection.language "comment"))

((shell_text) @injection.content
  (#set! injection.language "bash"))

((shell_command) @injection.content
  (#set! injection.language "bash"))
(define_directive
  "define" @keyword
  name: (word) @string.special.symbol
  [
    "="
    ":="
    "::="
    ; ":::="
    "?="
    "!="
  ]? @operator
  "endef" @keyword)

My question is, wouldn't be better to assign to highlights.scm value: (...) and try to match it there? Seems that the problem that I have is that value: is not set and therefore it takes it as raw_text as default.

clason commented 3 months ago

No, since we can only assign single captures to single nodes exposed by the parser.

If you are not (yet) sufficiently familiar with treesitter queries, no worries; but then you'll have to wait for some else to do this. (Queries are community contributed.)

AlejandroSuero commented 3 months ago

@clason I haven't written a single one yet 😥 but I will give it a try, in the meantime if anyone does it will be nice too ✌️.