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

Add Vlang tree-sitter in the languages.toml #2526

Merged
merged 3 commits into from
May 28, 2022

Conversation

trufae
Copy link
Contributor

@trufae trufae commented May 21, 2022

No description provided.

@the-mikedavis the-mikedavis linked an issue May 21, 2022 that may be closed by this pull request
@the-mikedavis the-mikedavis self-requested a review May 21, 2022 16:19
@the-mikedavis
Copy link
Member

If you run cargo xtask docgen and commit the results, it will clear up the ❌ from the Docs CI.

With just the changes to the languages.toml, this will add the tree-sitter parser so you will be able to do syntax-tree based motion A-p/A-o/A-i/A-n but for syntax highlighting you'll need to add a runtime/queries/v/highlights.scm. The vls repo has some: https://github.com/vlang/vls/blob/master/tree_sitter_v/queries/highlights.scm but they're in nvim-treesitter format so they'll need some changes:

  • the precedence for stanzas is bottom-to-top in nvim-treesitter but top-to-bottom in all other tree-sitter consumers including helix. For example the (identifier) @variable needs to be below the other stanzas that pattern-match on the (identifier) node in helix queries.
  • the captures will need to be adjusted to the ones helix uses. For example, @property should become @variable.other.member. See also https://docs.helix-editor.com/master/guides/adding_languages.html#queries.

That being said, this could be merged with just cargo xtask docgen changes if you just want basic syntax tree features like syntax-based navigation.

@the-mikedavis the-mikedavis added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label May 23, 2022
@trufae trufae force-pushed the vlang branch 2 times, most recently from 907437d to ba23734 Compare May 24, 2022 16:50
@trufae
Copy link
Contributor Author

trufae commented May 24, 2022

I have updated the PR with the vanila highlight.scm. i'm mainly interested in highlighting and code navigation (as well as language server support), for indenting i would prefer to use v fmt. like it should be done for rust or go programs too. With this patch i get somehow highlighted v code in helix, and i checked your comments and i didnt found what to change as it seems to be pretty close to how's described in the go.scm.

Feel free to merge this PR and tweak the .scm if it looks good to you.

thanks for the neat review!

@the-mikedavis the-mikedavis removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label May 24, 2022
@the-mikedavis
Copy link
Member

You can use this highlights.scm which has the scopes adjusted:
(parameter_declaration
  name: (identifier) @variable.parameter)
(function_declaration
  name: (identifier) @function)
(function_declaration
  receiver: (parameter_list)
  name: (identifier) @function.method)

(call_expression
  function: (identifier) @function)
(call_expression
  function: (selector_expression
    field: (identifier) @function.method))

(field_identifier) @variable.other.member
(selector_expression
  field: (identifier) @variable.other.member)

(int_literal) @constant.numeric.integer
(interpreted_string_literal) @string
(rune_literal) @string
(escape_sequence) @constant.character.escape

[
  (type_identifier)
  (builtin_type)
  (pointer_type)
  (array_type)
] @type

[
  (identifier)
  (module_identifier)
  (import_path)
] @variable

[
 "as"
 "asm"
 "assert"
 ;"atomic"
 ;"break"
 "const"
 ;"continue"
 "defer"
 "else"
 "enum"
 "fn"
 "for"
 "$for"
 "go"
 "goto"
 "if"
 "$if"
 "import"
 "in"
 "!in"
 "interface"
 "is"
 "!is"
 "lock"
 "match"
 "module"
 "mut"
 "or"
 "pub"
 "return"
 "rlock"
 "select"
 ;"shared"
 ;"static"
 "struct"
 "type"
 ;"union"
 "unsafe"
] @keyword

[
 (true)
 (false)
] @boolean

[
 "."
 ","
 ":"
 ";"
] @punctuation.delimiter

[
 "("
 ")"
 "{"
 "}"
 "["
 "]"
] @punctuation.bracket

(array) @punctuation.bracket

[
 "++"
 "--"

 "+"
 "-"
 "*"
 "/"
 "%"

 "~"
 "&"
 "|"
 "^"

 "!"
 "&&"
 "||"
 "!="

 "<<"
 ">>"

 "<"
 ">"
 "<="
 ">="

 "+="
 "-="
 "*="
 "/="
 "&="
 "|="
 "^="
 "<<="
 ">>="

 "="
 ":="
 "=="

 "?"
 "<-"
 "$"
 ".."
 "..."
] @operator

(comment) @comment

Just highlight queries are fine - indent queries can be good if you want to tune indents but the default behavior is fine you're using a formatter as you say. Code navigation and format-on-save are done through the language server so you'll need to configure VLS in order to get those to work.

languages.toml Outdated Show resolved Hide resolved
languages.toml Outdated Show resolved Hide resolved
* $ cargo xtask docgen
* Name it v instead of vlang and add highlights.scm
* Setup tabstop and vls
file-types = ["v", "vv"]
shebangs = ["v run"]
roots = ["v.mod"]
language-server = { command = "vls", args = [] }
Copy link
Contributor

Choose a reason for hiding this comment

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

One note (not sure what we can do about this though):

There's already the command vls for vue

Copy link
Member

Choose a reason for hiding this comment

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

Hmm yeah I don't think there's much we can do about it on our end. I guess you can't develop in v and vue at the same time 😄

I think in other editors plugins might solve this if they install the language server binary local to the plugin. I'm not too worried about this though.

@trufae
Copy link
Contributor Author

trufae commented May 27, 2022

ready to merge?

@the-mikedavis
Copy link
Member

the-mikedavis commented May 28, 2022

There's a snag with this that verilog is configured to use the same v file extension and verilog seems to win when opening a .v file. You can overwrite the file-types setting for verilog in your ~/.config/helix/languages.toml to prefer v:

[[language]]
name = "verilog"
file-types = ["sv", "svh"]

Ideally the use-grammars feature in languages.toml should help with this but it doesn't currently affect which grammars may be loaded if they already exist. I'll write up an issue for it.

Edit: hmm on second thought I don't think use-grammars is ideal since it's tied to grammars rather than languages.

Copy link
Member

@the-mikedavis the-mikedavis left a comment

Choose a reason for hiding this comment

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

Thanks for working on this!

@the-mikedavis the-mikedavis merged commit 7706a4a into helix-editor:master May 28, 2022
mtoohey31 pushed a commit to mtoohey31/helix that referenced this pull request Jun 15, 2022
mtoohey31 pushed a commit to mtoohey31/helix that referenced this pull request Jun 15, 2022
@AnthonySmithDev
Copy link

Also accepts the ".vsh" extension:
https://github.com/vlang/v/blob/master/doc/docs.md#cross-platform-shell-scripts-in-v

On the line of code:

file-types = ["v", "vv"]

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.

Add v (vlang) language support
5 participants