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

nvim queries: Highlight built-in commands #100

Merged
merged 1 commit into from
Jun 13, 2024

Conversation

hedyhli
Copy link
Contributor

@hedyhli hedyhli commented Jun 12, 2024

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

@hedyhli hedyhli changed the title Highlight built-in commands nvim queries: Highlight built-in commands Jun 12, 2024
@fdncred
Copy link
Collaborator

fdncred commented Jun 12, 2024

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
Copy link
Collaborator

fdncred commented Jun 12, 2024

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
Copy link
Contributor Author

hedyhli commented Jun 13, 2024

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
Copy link
Collaborator

fdncred commented Jun 13, 2024

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

@fdncred fdncred merged commit 781c859 into nushell:main Jun 13, 2024
3 checks passed
@hedyhli hedyhli deleted the highlight-builtins branch June 13, 2024 02:37
@hedyhli
Copy link
Contributor Author

hedyhli commented Jun 13, 2024

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
Copy link
Contributor Author

hedyhli commented Jun 13, 2024

@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 pushed a commit that referenced this pull request Jun 13, 2024
Apologies, in #100 I hadn't realized the where command is special-cased
in the parser and hence requires a separate capture to highlight
properly. This ensures the `where` command is also highlighted as
built-in.
@fdncred
Copy link
Collaborator

fdncred commented Jun 13, 2024

What do we think?

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

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.

None yet

2 participants