tree-sitter / tree-sitter-rust

Rust grammar for tree-sitter
MIT License
350 stars 98 forks source link

tuple struct #178

Closed glepnir closed 1 year ago

glepnir commented 1 year ago

Hi thanks for this parser. for a tuple struct the tree looks like not correct

    struct Apple(i32);

    impl Phone for Apple {
        fn call(&self) -> Apple {
            Apple(10)
        }
    }

in tree it's function i know it's very special , is it possible to fix ?

[source_file](https://tree-sitter.github.io/tree-sitter/playground#) [0, 4] - [8, 0]
  [struct_item](https://tree-sitter.github.io/tree-sitter/playground#) [0, 4] - [0, 22]
    name: [type_identifier](https://tree-sitter.github.io/tree-sitter/playground#) [0, 11] - [0, 16]
    body: [ordered_field_declaration_list](https://tree-sitter.github.io/tree-sitter/playground#) [0, 16] - [0, 21]
      type: [primitive_type](https://tree-sitter.github.io/tree-sitter/playground#) [0, 17] - [0, 20]
  [impl_item](https://tree-sitter.github.io/tree-sitter/playground#) [2, 4] - [6, 5]
    trait: [type_identifier](https://tree-sitter.github.io/tree-sitter/playground#) [2, 9] - [2, 14]
    type: [type_identifier](https://tree-sitter.github.io/tree-sitter/playground#) [2, 19] - [2, 24]
    body: [declaration_list](https://tree-sitter.github.io/tree-sitter/playground#) [2, 25] - [6, 5]
      [function_item](https://tree-sitter.github.io/tree-sitter/playground#) [3, 8] - [5, 9]
        name: [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [3, 11] - [3, 15]
        parameters: [parameters](https://tree-sitter.github.io/tree-sitter/playground#) [3, 15] - [3, 22]
          [self_parameter](https://tree-sitter.github.io/tree-sitter/playground#) [3, 16] - [3, 21]
            [self](https://tree-sitter.github.io/tree-sitter/playground#) [3, 17] - [3, 21]
        return_type: [type_identifier](https://tree-sitter.github.io/tree-sitter/playground#) [3, 26] - [3, 31]
        body: [block](https://tree-sitter.github.io/tree-sitter/playground#) [3, 32] - [5, 9]
          [call_expression](https://tree-sitter.github.io/tree-sitter/playground#) [4, 12] - [4, 21]
            function: [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [4, 12] - [4, 17] -----Here!!!!
            arguments: [arguments](https://tree-sitter.github.io/tree-sitter/playground#) [4, 17] - [4, 21]
              [integer_literal](https://tree-sitter.github.io/tree-sitter/playground#) [4, 18] - [4, 20]
amaanq commented 1 year ago

it's not really possible to fix here, but it's something queries can resolve with match regexes given the convention for tuple ctor identifiers (single identifier that is in PascalCase)

glepnir commented 1 year ago

but it also might be a function.call ? what did is change priority for function.call and variable. looks like this is not correct

(call_expression
  function: (identifier) @function.call
  (#lua-match? @type "^[A-Z]*$"))
amaanq commented 1 year ago

exactly, there's no real way to discern this - that's an LSP's job and the best one can do is make assumptions based on the ident's casing

Foo() is likely a tuple ctor, foo() a function call, make of that what you will but I could easily add that change to nvim-treesitter if desired

glepnir commented 1 year ago

right. simply change property for function.call and variable make them less than type will work.

amaanq commented 1 year ago

well you could just use "^[A-Z][a-z0-9_]*$" for tuple ctors