nushell / tree-sitter-nu

A tree-sitter grammar for nu-lang, the language of nushell
MIT License
122 stars 27 forks source link

nvim queries: Highlight built-in commands #100

Closed hedyhli closed 5 months ago

hedyhli commented 5 months ago

I thought it'd be great to highlight built-in commands differently to custom commands similar to what Nu does in the prompt line. It should be fairly easy to use the oneliner included in the comments on later versions of Nu to update the list.

Reference:

Core built-ins are filtered out because they are already specified higher up in the file as keywords.

All commands that aren't in the list are still highlighted with @function

fdncred commented 5 months ago

I like the idea, but it seems like there's a lot of commands missing like the commands with spaces in the name, also plugins (unless we don't want them) and ones like registry query that only exist on Windows. I'm also not sure why core commands are excluded.

I think if we can get a complete list, this will be a nice add. I have to do the same thing for the vscode extension so I may have a script laying around somewhere.

fdncred commented 5 months ago

With the vscode-extension I just do this on windows and mac/linux, combine the files and sort uniq them for the complete list. You also have to have the plugins installed and registered when doing this, unless we don't want to include the plugins. (i'd probably include them though)

scope commands | where type == built-in | get name | to text | save mac-cmds.txt
hedyhli commented 5 months ago

I like the idea, but it seems like there's a lot of commands missing like the commands with spaces in the name

I didn't see an easy way to include them because subcommands are parsed as arguments and not part of the command itself. If we match the command and subcommand separately there will be incorrect highlighting (eg my-command split vs str split).

also plugins (unless we don't want them)

I'm not sure if that's a good idea, wouldn't that be different for everyone? The highlight file will have to be the same, at least for nvim.

and ones like registry query that only exist on Windows

Ah, good point!

I'm also not sure why core commands are excluded.

They are already specified in the highlight file and highlighted as keywords, which I've explained in the PR description :), see below:

Core built-ins are filtered out because they are already specified higher up in the file as keywords.

fdncred commented 5 months ago

ok, let's move forward with this then. thanks!

hedyhli commented 5 months ago

I've got an idea how built-in subcommands can also be highlighted, I'll PR if it works -- indeed, for completeness highlighting both words would be the ideal situation.

hedyhli commented 5 months ago

@fdncred I've gotten it to work, but it's a little verbose -- though the simplest I could find:

Here, str starts-with and str trim are the only two valid built-in commands, where function.builtin is orange and function is blue.

Query for a single command:

(command (
  (cmd_identifier) @function.builtin (#eq? @function.builtin "str")
  .
  (val_string) @function.builtin.subcommand (#eq? @function.builtin.subcommand "starts-with")
))

The .subcommand part is only for descriptive purposes in the highlight.scm file, because the two groups must be different for the comparison to work properly. It makes use of Neovim's fallback mechanism, where if a highlight group A.B.C is not found, it tries A.B, then A until a color is found.

With this method, all subcommands for all built-in commands will have to be repeated as a separate query.

If all efforts to find a simpler solution using only the default predicates (#eq?, #match? etc) fails, one fallback solution is to define our own predicate added by the plugin in init.lua.

What do we think?

fdncred commented 5 months ago

What do we think?

"we" like it. I'd go for it because it's better than what we had before.