t9md / atom-vim-mode-plus

vim-mode improved
https://atom.io/packages/vim-mode-plus
MIT License
1.4k stars 111 forks source link

Fix 'a-f' (a-function) commands in JS when using using tree-sitter #1134

Closed jonboiser closed 4 years ago

jonboiser commented 4 years ago

This fixes the broken 'a-f' commands when editing JS/TS/JS+flow files when Tree-Sitter parsers are enabled. I think this is because of some changes to the tree-sitter parsers. Here is some output from the tree-sitter playground.

The main thing is that function_declaration is used for function foo() {} and function is used for anonymous functions. They have even added support for arrow functions, although the a-f selection range isn't perfect when the functions are formatted in weird ways. I added tests for very simple examples, and hopefully the more complicated ones can be addressed later.

Thanks and あけましておめでとう!

Input

function foo() {
    return '';
}

const foo = () => {
    return '';
}

const foo = function() {
    return '';
}

const methods = {
    foo() {
        return '';
    }
}

Output

program [0, 0] - [11, 0])

Named Function
  function_declaration [0, 0] - [2, 1])
    name: identifier [0, 9] - [0, 12])
    parameters: formal_parameters [0, 12] - [0, 14])
    body: statement_block [0, 15] - [2, 1])
      return_statement [1, 1] - [1, 11])
        string [1, 8] - [1, 10])

Arrow function
  lexical_declaration [4, 0] - [6, 1])
    variable_declarator [4, 6] - [6, 1])
      name: identifier [4, 6] - [4, 9])
      value: arrow_function [4, 12] - [6, 1])
        parameters: formal_parameters [4, 12] - [4, 14])
        body: statement_block [4, 18] - [6, 1])
          return_statement [5, 1] - [5, 11])
            string [5, 8] - [5, 10])

Anonymous function
  lexical_declaration [8, 0] - [10, 1])
    variable_declarator [8, 6] - [10, 1])
      name: identifier [8, 6] - [8, 9])
      value: function [8, 12] - [10, 1])
        parameters: formal_parameters [8, 20] - [8, 22])
        body: statement_block [8, 23] - [10, 1])
          return_statement [9, 1] - [9, 11])
            string [9, 8] - [9, 10])

Method
  lexical_declaration [12, 0] - [16, 1])
    variable_declarator [12, 6] - [16, 1])
      name: identifier [12, 6] - [12, 13])
      value: object [12, 16] - [16, 1])
        method_definition [13, 0] - [15, 1])
          name: property_identifier [13, 0] - [13, 3])
          parameters: formal_parameters [13, 3] - [13, 5])
          body: statement_block [13, 6] - [15, 1])
            return_statement [14, 0] - [14, 10])
              string [14, 7] - [14, 9])
t9md commented 4 years ago

Thank you. Happy new year.