From d9639e1e5eeb623c59949cf523681111b71475be Mon Sep 17 00:00:00 2001 From: Chinmay Date: Thu, 11 Jun 2020 22:31:45 +0530 Subject: [PATCH 01/87] Add highlights.scm and locals.scm --- queries/java/highlights.scm | 146 ++++++++++++++++++++++++++++++++++++ queries/java/locals.scm | 8 ++ 2 files changed, 154 insertions(+) create mode 100644 queries/java/highlights.scm create mode 100644 queries/java/locals.scm diff --git a/queries/java/highlights.scm b/queries/java/highlights.scm new file mode 100644 index 000000000..398cfe13f --- /dev/null +++ b/queries/java/highlights.scm @@ -0,0 +1,146 @@ +; Methods + +(method_declaration + name: (identifier) @function.method) +(method_invocation + name: (identifier) @function.method) +(super) @function.builtin + +; Annotations + +(annotation + name: (identifier) @attribute) +(marker_annotation + name: (identifier) @attribute) + +"@" @operator + +; Types + +(interface_declaration + name: (identifier) @type) +(class_declaration + name: (identifier) @type) +(enum_declaration + name: (identifier) @type) + +((field_access + object: (identifier) @type) + (#match? @type "^[A-Z]")) +((scoped_identifier + scope: (identifier) @type) + (#match? @type "^[A-Z]")) + +(constructor_declaration + name: (identifier) @type) + +(type_identifier) @type +(boolean_type) @type.builtin +(integral_type) @type.builtin +(floating_point_type) @type.builtin +(floating_point_type) @type.builtin +(void_type) @type.builtin + +; Variables + +((identifier) @constant + (#match? @constant "^_*[A-Z][A-Z\d_]+")) + +(identifier) @variable + +(this) @variable.builtin + +; Literals + +(hex_integer_literal) @number +(decimal_integer_literal) @number +(octal_integer_literal) @number +(decimal_floating_point_literal) @number +(hex_floating_point_literal) @number +(character_literal) @string +(string_literal) @string +(null_literal) @constant.builtin + +(comment) @comment + +[ +(true) +(false) +] @boolean + +; Keywords + +[ +"abstract" +"assert" +"break" +"catch" +"class" +"continue" +"default" +"do" +"enum" +"exports" +"extends" +"final" +"finally" +"implements" +"instanceof" +"interface" +"module" +"native" +"new" +"open" +"opens" +"package" +"private" +"protected" +"provides" +"public" +"requires" +"return" +"static" +"strictfp" +"synchronized" +"throw" +"throws" +"to" +"transient" +"transitive" +"try" +"uses" +"volatile" +"with" +] @keyword + +; Conditionals + +[ +"if" +"else" +"switch" +"case" +] @conditional + +; + +[ +"for" +"while" +] @repeat + +; + +"import" @include + +; Punctuation + +";" @punctuation.delimiter +"." @punctuation.delimiter +"," @punctuation.delimiter +"[" @punctuation.bracket +"]" @punctuation.bracket +"{" @punctuation.bracket +"}" @punctuation.bracket +"(" @punctuation.bracket +")" @punctuation.bracket diff --git a/queries/java/locals.scm b/queries/java/locals.scm new file mode 100644 index 000000000..9044c98d7 --- /dev/null +++ b/queries/java/locals.scm @@ -0,0 +1,8 @@ +(class_declaration + name: (identifier) @name) @class + +(method_declaration + name: (identifier) @name) @method + +(method_invocation + name: (identifier) @name) @call From 44108fe03e5c2cb761090c4abf8e7164225bf62f Mon Sep 17 00:00:00 2001 From: Steven Sojka Date: Fri, 12 Jun 2020 14:11:37 -0500 Subject: [PATCH 02/87] feat(queries): allow for user overrides --- README.md | 7 +++++++ lua/nvim-treesitter/query.lua | 15 +++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c31ce8985..c19874593 100644 --- a/README.md +++ b/README.md @@ -181,6 +181,13 @@ List of currently supported languages: - [ ] nix - [ ] markdown +## User Query Extensions + +You can add your own query files by placing a query file in vim's runtime path after `nvim-treesitter` is sourced. +If the language has a built in query file, that file will be appended to or it will be used (useful for languages not yet supported). +For example, you can add files to `/after/queries/lua/highlights.scm` to add more queries to lua highlights. +You can also manually add query paths to the runtime path by adding this to your vim config `set rtp+='path/to/queries'`. + ## Troubleshooting Before doing anything run `:checkhealth nvim_treesitter`. This will help you find where the bug might come from. diff --git a/lua/nvim-treesitter/query.lua b/lua/nvim-treesitter/query.lua index 644c33933..b914e5d7d 100644 --- a/lua/nvim-treesitter/query.lua +++ b/lua/nvim-treesitter/query.lua @@ -3,14 +3,21 @@ local ts = vim.treesitter local M = {} -local function read_query_file(fname) - return table.concat(vim.fn.readfile(fname), '\n') +local function read_query_files(filenames) + local contents = {} + + for _,filename in ipairs(filenames) do + vim.list_extend(contents, vim.fn.readfile(filename)) + end + + return table.concat(contents, '\n') end function M.get_query(ft, query_name) - local query_files = api.nvim_get_runtime_file(string.format('queries/%s/%s.scm', ft, query_name), false) + local query_files = api.nvim_get_runtime_file(string.format('queries/%s/%s.scm', ft, query_name), true) + if #query_files > 0 then - return ts.parse_query(ft, read_query_file(query_files[1])) + return ts.parse_query(ft, read_query_files(query_files)) end end From ac8ae3b1c7b5644f8317cdc88d604cdb558b6296 Mon Sep 17 00:00:00 2001 From: Steven Sojka Date: Fri, 12 Jun 2020 07:41:20 -0500 Subject: [PATCH 03/87] feat(queries): add typescript and javascript queries --- README.md | 4 +- queries/javascript/highlights.scm | 190 ++++++++++++++++++++++++++ queries/javascript/locals.scm | 35 +++++ queries/typescript/highlights.scm | 215 ++++++++++++++++++++++++++++++ queries/typescript/locals.scm | 37 +++++ 5 files changed, 479 insertions(+), 2 deletions(-) create mode 100644 queries/javascript/highlights.scm create mode 100644 queries/javascript/locals.scm create mode 100644 queries/typescript/highlights.scm create mode 100644 queries/typescript/locals.scm diff --git a/README.md b/README.md index c31ce8985..0401128b8 100644 --- a/README.md +++ b/README.md @@ -159,8 +159,8 @@ List of currently supported languages: - [ ] cpp - [ ] rust - [x] python (maintained by @theHamsta) -- [ ] javascript -- [ ] typescript +- [x] javascript (maintained by @steelsojka) +- [x] typescript (maintained by @steelsojka) - [ ] tsx - [ ] json - [x] html (maintained by @TravonteD) diff --git a/queries/javascript/highlights.scm b/queries/javascript/highlights.scm new file mode 100644 index 000000000..da88d85d0 --- /dev/null +++ b/queries/javascript/highlights.scm @@ -0,0 +1,190 @@ +; Types + +; Javascript +; Special identifiers +;-------------------- + +((identifier) @constant + (#match? @constant "^[A-Z_][A-Z\\d_]+$")) + +((shorthand_property_identifier) @constant + (#match? @constant "^[A-Z_][A-Z\\d_]+$")) + +((identifier) @constructor + (#match? @constructor "^[A-Z]")) + +((identifier) @variable.builtin + (#match? @variable.builtin "^(arguments|module|console|window|document)$")) + +((identifier) @function.builtin + (#eq? @function.builtin "require")) + +; Function and method definitions +;-------------------------------- + +(function + name: (identifier) @function) +(function_declaration + name: (identifier) @function) +(method_definition + name: (property_identifier) @function.method) + +(pair + key: (property_identifier) @function.method + value: (function)) +(pair + key: (property_identifier) @function.method + value: (arrow_function)) + +(assignment_expression + left: (member_expression + property: (property_identifier) @function.method) + right: (arrow_function)) +(assignment_expression + left: (member_expression + property: (property_identifier) @function.method) + right: (function)) + +(variable_declarator + name: (identifier) @function + value: (arrow_function)) +(variable_declarator + name: (identifier) @function + value: (function)) + +(assignment_expression + left: (identifier) @function + right: (arrow_function)) +(assignment_expression + left: (identifier) @function + right: (function)) + +; Function and method calls +;-------------------------- + +(call_expression + function: (identifier) @function) + +(call_expression + function: (member_expression + property: (property_identifier) @function.method)) + +; Variables +;---------- + +(formal_parameters (identifier) @variable.parameter) + +(identifier) @variable + +; Properties +;----------- + +(property_identifier) @property + +; Literals +;--------- + +(this) @variable.builtin +(super) @variable.builtin + +(true) @constant.builtin +(false) @constant.builtin +(null) @constant.builtin +(comment) @comment +(string) @string +(regex) @string.special +(template_string) @string +(number) @number + +; Punctuation +;------------ + +(template_substitution + "${" @punctuation.special + "}" @punctuation.special) @embedded + +";" @punctuation.delimiter +"." @punctuation.delimiter +"," @punctuation.delimiter + +"--" @operator +"-" @operator +"-=" @operator +"&&" @operator +"+" @operator +"++" @operator +"+=" @operator +"<" @operator +"<<" @operator +"=" @operator +"==" @operator +"===" @operator +"=>" @operator +">" @operator +">>" @operator +"||" @operator +"??" @operator + +"(" @punctuation.bracket +")" @punctuation.bracket +"[" @punctuation.bracket +"]" @punctuation.bracket +"{" @punctuation.bracket +"}" @punctuation.bracket + +; Keywords +;---------- + +[ +"if" +"else" +"switch" +"case" +"default" +] @conditional + +[ +"import" +"from" +"as" +] @include + +[ +"for" +"of" +"do" +"while" +"continue" +] @repeat + +[ +"async" +"await" +"break" +"catch" +"class" +"const" +"debugger" +"delete" +"export" +"extends" +"finally" +"function" +"get" +"in" +"instanceof" +"let" +"new" +"return" +"set" +"static" +"switch" +"target" +"throw" +"try" +"typeof" +"var" +"void" +"with" +"yield" +] @keyword diff --git a/queries/javascript/locals.scm b/queries/javascript/locals.scm new file mode 100644 index 000000000..165adfed9 --- /dev/null +++ b/queries/javascript/locals.scm @@ -0,0 +1,35 @@ +; Scopes +;------- + +(statement_block) @scope +(function) @scope +(arrow_function) @scope +(function_declaration) @scope +(method_definition) @scope + +; Definitions +;------------ + +(formal_parameters + (identifier) @definition) + +(formal_parameters + (object_pattern + (identifier) @definition)) + +(formal_parameters + (object_pattern + (shorthand_property_identifier) @definition)) + +(formal_parameters + (array_pattern + (identifier) @definition)) + +(variable_declarator + name: (identifier) @definition) + +; References +;------------ + +(identifier) @reference + diff --git a/queries/typescript/highlights.scm b/queries/typescript/highlights.scm new file mode 100644 index 000000000..92820105b --- /dev/null +++ b/queries/typescript/highlights.scm @@ -0,0 +1,215 @@ +; Types + +; Javascript +; Special identifiers +;-------------------- + +((identifier) @constant + (#match? @constant "^[A-Z_][A-Z\\d_]+$")) + +((shorthand_property_identifier) @constant + (#match? @constant "^[A-Z_][A-Z\\d_]+$")) + +((identifier) @constructor + (#match? @constructor "^[A-Z]")) + +((identifier) @variable.builtin + (#match? @variable.builtin "^(arguments|module|console|window|document)$")) + +((identifier) @function.builtin + (#eq? @function.builtin "require")) + +; Function and method definitions +;-------------------------------- + +(function + name: (identifier) @function) +(function_declaration + name: (identifier) @function) +(method_definition + name: (property_identifier) @function.method) + +(pair + key: (property_identifier) @function.method + value: (function)) +(pair + key: (property_identifier) @function.method + value: (arrow_function)) + +(assignment_expression + left: (member_expression + property: (property_identifier) @function.method) + right: (arrow_function)) +(assignment_expression + left: (member_expression + property: (property_identifier) @function.method) + right: (function)) + +(variable_declarator + name: (identifier) @function + value: (arrow_function)) +(variable_declarator + name: (identifier) @function + value: (function)) + +(assignment_expression + left: (identifier) @function + right: (arrow_function)) +(assignment_expression + left: (identifier) @function + right: (function)) + +; Function and method calls +;-------------------------- + +(call_expression + function: (identifier) @function) + +(call_expression + function: (member_expression + property: (property_identifier) @function.method)) + +; Variables +;---------- + +(formal_parameters (identifier) @variable.parameter) + +(identifier) @variable + +; Properties +;----------- + +(property_identifier) @property + +; Literals +;--------- + +(this) @variable.builtin +(super) @variable.builtin + +(true) @constant.builtin +(false) @constant.builtin +(null) @constant.builtin +(comment) @comment +(string) @string +(regex) @string.special +(template_string) @string +(number) @number + +; Punctuation +;------------ + +(template_substitution + "${" @punctuation.special + "}" @punctuation.special) @embedded + +";" @punctuation.delimiter +"." @punctuation.delimiter +"," @punctuation.delimiter + +"--" @operator +"-" @operator +"-=" @operator +"&&" @operator +"+" @operator +"++" @operator +"+=" @operator +"<" @operator +"<<" @operator +"=" @operator +"==" @operator +"===" @operator +"=>" @operator +">" @operator +">>" @operator +"||" @operator +"??" @operator + +"(" @punctuation.bracket +")" @punctuation.bracket +"[" @punctuation.bracket +"]" @punctuation.bracket +"{" @punctuation.bracket +"}" @punctuation.bracket + +; Keywords +;---------- + +[ +"if" +"else" +"switch" +"case" +"default" +] @conditional + +[ +"import" +"from" +"as" +] @include + +[ +"for" +"of" +"do" +"while" +"continue" +] @repeat + +[ +"async" +"await" +"break" +"catch" +"class" +"const" +"debugger" +"delete" +"export" +"extends" +"finally" +"function" +"get" +"in" +"instanceof" +"let" +"new" +"return" +"set" +"static" +"switch" +"target" +"throw" +"try" +"typeof" +"var" +"void" +"with" +"yield" +"abstract" +"declare" +"enum" +"export" +"implements" +"interface" +"keyof" +"namespace" +"private" +"protected" +"public" +"type" +] @keyword + +(readonly) @keyword +(type_identifier) @type +(predefined_type) @type.builtin + +(type_arguments + "<" @punctuation.bracket + ">" @punctuation.bracket) + +; Variables + +(required_parameter (identifier) @variable.parameter) +(optional_parameter (identifier) @variable.parameter) diff --git a/queries/typescript/locals.scm b/queries/typescript/locals.scm new file mode 100644 index 000000000..d308e9873 --- /dev/null +++ b/queries/typescript/locals.scm @@ -0,0 +1,37 @@ +; Scopes +;------- + +(statement_block) @scope +(function) @scope +(arrow_function) @scope +(function_declaration) @scope +(method_definition) @scope + +; Definitions +;------------ + +(formal_parameters + (identifier) @definition) + +(formal_parameters + (object_pattern + (identifier) @definition)) + +(formal_parameters + (object_pattern + (shorthand_property_identifier) @definition)) + +(formal_parameters + (array_pattern + (identifier) @definition)) + +(variable_declarator + name: (identifier) @definition) + +; References +;------------ + +(identifier) @reference + +(required_parameter (identifier) @definition) +(optional_parameter (identifier) @definition) From df060261106a3570dbae04ae83879cdcbc9a9907 Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Sat, 23 May 2020 20:29:08 +0200 Subject: [PATCH 04/87] Introduce base languages for queries Some treesitter grammars just extend another treesitter grammar. This enables us to use the C queries also for C++. We only need to put additional queries in the C++ files. --- lua/nvim-treesitter/query.lua | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/lua/nvim-treesitter/query.lua b/lua/nvim-treesitter/query.lua index b914e5d7d..788bfa126 100644 --- a/lua/nvim-treesitter/query.lua +++ b/lua/nvim-treesitter/query.lua @@ -13,11 +13,31 @@ local function read_query_files(filenames) return table.concat(contents, '\n') end +-- Some treesitter grammars extend others. +-- We can use that to import the queries of the base language +M.base_language_map = { + cpp = {'c'}, + typescript = {'javascript'}, + tsx = {'typescript', 'javascript'}, +} + function M.get_query(ft, query_name) local query_files = api.nvim_get_runtime_file(string.format('queries/%s/%s.scm', ft, query_name), true) + local query_string = '' if #query_files > 0 then - return ts.parse_query(ft, read_query_files(query_files)) + query_string = read_query_files(query_files)..query_string + end + + for _, base_lang in ipairs(M.base_language_map[ft] or {}) do + local base_files = api.nvim_get_runtime_file(string.format('queries/%s/%s.scm', base_lang, query_name), false) + if base_files and #base_files > 0 then + query_string = read_query_files(base_files)..query_string + end + end + + if #query_string > 0 then + return ts.parse_query(ft, query_string) end end From 167ce6339fb672af9bfd9b5e21a4d18b2f4fb253 Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Sat, 23 May 2020 21:02:43 +0200 Subject: [PATCH 05/87] Add C++ highlights.scm --- README.md | 2 +- queries/c/highlights.scm | 3 ++ queries/cpp/highlights.scm | 97 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 queries/cpp/highlights.scm diff --git a/README.md b/README.md index 1b4fe210d..95bb5ae91 100644 --- a/README.md +++ b/README.md @@ -156,7 +156,7 @@ List of currently supported languages: - [x] ruby (maintained by @TravonteD) - [x] c (maintained by @vigoux) - [x] go (maintained by @theHamsta) -- [ ] cpp +- [x] cpp (maintained by @theHamsta, extends C queries) - [ ] rust - [x] python (maintained by @theHamsta) - [ ] javascript diff --git a/queries/c/highlights.scm b/queries/c/highlights.scm index 4cfb042cc..80fb99749 100644 --- a/queries/c/highlights.scm +++ b/queries/c/highlights.scm @@ -71,6 +71,9 @@ (primitive_type) @type (sized_type_specifier) @type +((identifier) @type + (match? @type "^[A-Z]")) + ((identifier) @constant (match? @constant "^[A-Z][A-Z\\d_]+$")) diff --git a/queries/cpp/highlights.scm b/queries/cpp/highlights.scm new file mode 100644 index 000000000..e3bce91cb --- /dev/null +++ b/queries/cpp/highlights.scm @@ -0,0 +1,97 @@ +((identifier) @field + (match? @field "^_")) + +((identifier) @field + (match? @field "^m_")) + +((identifier) @field + (match? @field "_$")) + +;(field_expression) @parameter ;; How to highlight this? +(template_function + name: (identifier) @function) + +(template_method + name: (field_identifier) @method) + +(template_function + name: (scoped_identifier + name: (identifier) @function)) + +(namespace_identifier) @constant + +((namespace_identifier) @type + (match? @type "^[A-Z]")) +((namespace_identifier) @constant + (match? @constant "^[A-Z][A-Z_1-9]*$")) + +(destructor_name + name: (*) @function) + +(function_declarator + declarator: (scoped_identifier + name: (identifier) @function)) +((function_declarator + declarator: (scoped_identifier + name: (identifier) @constructor)) + (match? @constructor "^[A-Z]")) + +(call_expression + function: (scoped_identifier + name: (identifier) @function)) + +(call_expression + function: (field_expression + field: (field_identifier) @function)) + +((call_expression + function: (scoped_identifier + name: (identifier) @constructor)) +(match? @constructor "^[A-Z]")) + +((call_expression + function: (field_expression + field: (field_identifier) @function)) +(match? @function "^[A-Z]")) + +;; constructing a type in a intizializer list: Constructor (): **SuperType (1)** +((field_initializer + (field_identifier) @constructor + (argument_list)) + (match? @constructor "^[A-Z]")) + +(auto) @keyword +(class_specifier) + +; Constants + +;(this) @constant.builtin +(this) @keyword +(nullptr) @constant + +(true) @boolean +(false) @boolean + +; Keywords + +"catch" @exception +"class" @keyword +"constexpr" @keyword +"delete" @operator +"explicit" @keyword +"final" @exception +"friend" @keyword +"mutable" @keyword +"namespace" @keyword +"noexcept" @keyword +"new" @operator +"override" @keyword +"private" @keyword +"protected" @keyword +"public" @keyword +"template" @keyword +"throw" @keyword +"try" @exception +"typename" @keyword +"using" @keyword +"virtual" @keyword From b95e0af73b00dcba6e54e6f1ffa9b42e1aef5060 Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Sat, 23 May 2020 21:03:03 +0200 Subject: [PATCH 06/87] Add @error highlight to c/highlights.scm --- queries/c/highlights.scm | 2 ++ 1 file changed, 2 insertions(+) diff --git a/queries/c/highlights.scm b/queries/c/highlights.scm index 80fb99749..e758b0ed4 100644 --- a/queries/c/highlights.scm +++ b/queries/c/highlights.scm @@ -78,3 +78,5 @@ (match? @constant "^[A-Z][A-Z\\d_]+$")) (comment) @comment + +(ERROR) @error From 1badceea813d321d0d58319d5fc5dde07e98f18f Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Sat, 23 May 2020 21:23:05 +0200 Subject: [PATCH 07/87] Add cpp/locals.scm --- queries/cpp/highlights.scm | 11 ++++++--- queries/cpp/locals.scm | 49 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 queries/cpp/locals.scm diff --git a/queries/cpp/highlights.scm b/queries/cpp/highlights.scm index e3bce91cb..be79098c9 100644 --- a/queries/cpp/highlights.scm +++ b/queries/cpp/highlights.scm @@ -61,7 +61,11 @@ (match? @constructor "^[A-Z]")) (auto) @keyword -(class_specifier) + +;; Parameters +; normals +(parameter_list + (parameter_declaration) @parameter) ; Constants @@ -77,14 +81,14 @@ "catch" @exception "class" @keyword "constexpr" @keyword -"delete" @operator +"delete" @keyword "explicit" @keyword "final" @exception "friend" @keyword "mutable" @keyword "namespace" @keyword "noexcept" @keyword -"new" @operator +"new" @keyword "override" @keyword "private" @keyword "protected" @keyword @@ -95,3 +99,4 @@ "typename" @keyword "using" @keyword "virtual" @keyword +"::" @operator diff --git a/queries/cpp/locals.scm b/queries/cpp/locals.scm new file mode 100644 index 000000000..00268442c --- /dev/null +++ b/queries/cpp/locals.scm @@ -0,0 +1,49 @@ + +;; Class / struct defintions +(class_specifier) @scope +(struct_specifier) @scope + + +(struct_specifier + name: (type_identifier) @definition.type) + +(struct_specifier + name: (scoped_type_identifier + name: (type_identifier) @definition.type) ) + +(class_specifier + name: (type_identifier) @definition.type) + +(class_specifier + name: (scoped_type_identifier + name: (type_identifier) @definition.type) ) + +;; Function defintions +(template_function + name: (identifier) @definition.function) @scope + +(template_method + name: (field_identifier) @definition.method) @scope + +(template_function + name: (scoped_identifier + name: (identifier) @definition.function)) @scope + +(function_declarator + declarator: (scoped_identifier + name: (type_identifier) @definition.function)) @scope + +(field_declaration + declarator: (function_declarator + (field_identifier) @definition.method)) + +(lambda_expression) @scope + +;; Control structures +(try_statement + body: (*) @scope) + +(catch_clause) @scope + +(destructor_name + name: (*) @constructor) From 148ad160e8be09b29a2e9547f4814b85147639a6 Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Sat, 23 May 2020 21:50:52 +0200 Subject: [PATCH 08/87] Make =,~,! operators in C highlights --- queries/c/highlights.scm | 10 ++++++++++ queries/cpp/highlights.scm | 5 ----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/queries/c/highlights.scm b/queries/c/highlights.scm index e758b0ed4..307feb751 100644 --- a/queries/c/highlights.scm +++ b/queries/c/highlights.scm @@ -35,14 +35,20 @@ "->" @operator "!=" @operator "*" @operator +"/" @operator "&" @operator "&&" @operator "+" @operator "++" @operator "+=" @operator "<" @operator +"<=" @operator "==" @operator +"=" @operator +"~" @operator ">" @operator +">=" @operator +"!" @operator "||" @operator "." @delimiter @@ -79,4 +85,8 @@ (comment) @comment +;; Parameters +(parameter_list + (parameter_declaration) @parameter) + (ERROR) @error diff --git a/queries/cpp/highlights.scm b/queries/cpp/highlights.scm index be79098c9..d98099185 100644 --- a/queries/cpp/highlights.scm +++ b/queries/cpp/highlights.scm @@ -62,11 +62,6 @@ (auto) @keyword -;; Parameters -; normals -(parameter_list - (parameter_declaration) @parameter) - ; Constants ;(this) @constant.builtin From a0b912ff8ace8f58ff74843ffcfd12c0aa9700c5 Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Sat, 23 May 2020 21:59:34 +0200 Subject: [PATCH 09/87] Add punctuation.bracket/punctuation.delimiter to C highlights --- queries/c/highlights.scm | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/queries/c/highlights.scm b/queries/c/highlights.scm index 307feb751..c0df45d48 100644 --- a/queries/c/highlights.scm +++ b/queries/c/highlights.scm @@ -51,8 +51,17 @@ "!" @operator "||" @operator -"." @delimiter -";" @delimiter +"." @punctuation.delimiter +";" @punctuation.delimiter +":" @punctuation.delimiter +"," @punctuation.delimiter + +"(" @punctuation.bracket +")" @punctuation.bracket +"[" @punctuation.bracket +"]" @punctuation.bracket +"{" @punctuation.bracket +"}" @punctuation.bracket (string_literal) @string (system_lib_string) @string From 95f4e10a62f8a90ba7f7db58cb6e8c360b0f61c9 Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Sat, 23 May 2020 22:10:14 +0200 Subject: [PATCH 10/87] Add compound_statement to c queries --- queries/c/locals.scm | 1 + 1 file changed, 1 insertion(+) diff --git a/queries/c/locals.scm b/queries/c/locals.scm index b08d706ee..f7a4b84d2 100644 --- a/queries/c/locals.scm +++ b/queries/c/locals.scm @@ -36,3 +36,4 @@ (while_statement) @scope (translation_unit) @scope (function_definition) @scope +(compound_statement) @scope ; a block in curly braces From d88db9d71805bf9d759d4e0b8d571e6c952c3b0c Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Sun, 24 May 2020 13:31:13 +0200 Subject: [PATCH 11/87] Add operators /=,*=,|=,&= to C highlights --- queries/c/highlights.scm | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/queries/c/highlights.scm b/queries/c/highlights.scm index c0df45d48..d6e3a7a15 100644 --- a/queries/c/highlights.scm +++ b/queries/c/highlights.scm @@ -31,7 +31,6 @@ "--" @operator "-" @operator -"-=" @operator "->" @operator "!=" @operator "*" @operator @@ -40,7 +39,6 @@ "&&" @operator "+" @operator "++" @operator -"+=" @operator "<" @operator "<=" @operator "==" @operator @@ -51,6 +49,13 @@ "!" @operator "||" @operator +"-=" @operator +"+=" @operator +"*=" @operator +"/=" @operator +"|=" @operator +"&=" @operator + "." @punctuation.delimiter ";" @punctuation.delimiter ":" @punctuation.delimiter From e46baab8ca23855053900b8657a50e83ad76ba41 Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Sun, 24 May 2020 13:40:43 +0200 Subject: [PATCH 12/87] Add better highlighting for preprocessor functions in C highlights --- queries/c/highlights.scm | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/queries/c/highlights.scm b/queries/c/highlights.scm index d6e3a7a15..3c2123383 100644 --- a/queries/c/highlights.scm +++ b/queries/c/highlights.scm @@ -84,6 +84,8 @@ declarator: (identifier) @function) (preproc_function_def name: (identifier) @function.macro) +(preproc_arg) @function.macro +; TODO (preproc_arg) @embedded (field_identifier) @property (statement_identifier) @label @@ -103,4 +105,7 @@ (parameter_list (parameter_declaration) @parameter) +(preproc_params + (identifier)) @parameter + (ERROR) @error From a5fc7b13ccfbc189deea031e692ba14b0e686279 Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Sun, 7 Jun 2020 13:13:24 +0200 Subject: [PATCH 13/87] Update C/C++ highlights to new query syntax --- queries/c/highlights.scm | 5 +++-- queries/c/locals.scm | 2 +- queries/cpp/highlights.scm | 22 +++++++++++----------- queries/cpp/locals.scm | 4 ++-- 4 files changed, 17 insertions(+), 16 deletions(-) diff --git a/queries/c/highlights.scm b/queries/c/highlights.scm index 3c2123383..568ebaffa 100644 --- a/queries/c/highlights.scm +++ b/queries/c/highlights.scm @@ -94,10 +94,10 @@ (sized_type_specifier) @type ((identifier) @type - (match? @type "^[A-Z]")) + (#match? @type "^[A-Z]")) ((identifier) @constant - (match? @constant "^[A-Z][A-Z\\d_]+$")) + (#match? @constant "^[A-Z][A-Z\\d_]+$")) (comment) @comment @@ -109,3 +109,4 @@ (identifier)) @parameter (ERROR) @error + diff --git a/queries/c/locals.scm b/queries/c/locals.scm index f7a4b84d2..505db5b57 100644 --- a/queries/c/locals.scm +++ b/queries/c/locals.scm @@ -17,7 +17,7 @@ (declaration declarator: (identifier) @definition.var) (enum_specifier - name: (*) @definition.type + name: (_) @definition.type (enumerator_list (enumerator name: (identifier) @definition.var))) diff --git a/queries/cpp/highlights.scm b/queries/cpp/highlights.scm index d98099185..68e5047ab 100644 --- a/queries/cpp/highlights.scm +++ b/queries/cpp/highlights.scm @@ -1,11 +1,11 @@ ((identifier) @field - (match? @field "^_")) + (#match? @field "^_")) ((identifier) @field - (match? @field "^m_")) + (#match? @field "^m_")) ((identifier) @field - (match? @field "_$")) + (#match? @field "_$")) ;(field_expression) @parameter ;; How to highlight this? (template_function @@ -21,12 +21,12 @@ (namespace_identifier) @constant ((namespace_identifier) @type - (match? @type "^[A-Z]")) + (#match? @type "^[A-Z]")) ((namespace_identifier) @constant - (match? @constant "^[A-Z][A-Z_1-9]*$")) + (#match? @constant "^[A-Z][A-Z_1-9]*$")) (destructor_name - name: (*) @function) + name: (_) @function) (function_declarator declarator: (scoped_identifier @@ -34,7 +34,7 @@ ((function_declarator declarator: (scoped_identifier name: (identifier) @constructor)) - (match? @constructor "^[A-Z]")) + (#match? @constructor "^[A-Z]")) (call_expression function: (scoped_identifier @@ -47,18 +47,18 @@ ((call_expression function: (scoped_identifier name: (identifier) @constructor)) -(match? @constructor "^[A-Z]")) +(#match? @constructor "^[A-Z]")) ((call_expression function: (field_expression - field: (field_identifier) @function)) -(match? @function "^[A-Z]")) + field: (field_identifier) @constructor)) +(#match? @function "^[A-Z]")) ;; constructing a type in a intizializer list: Constructor (): **SuperType (1)** ((field_initializer (field_identifier) @constructor (argument_list)) - (match? @constructor "^[A-Z]")) + (#match? @constructor "^[A-Z]")) (auto) @keyword diff --git a/queries/cpp/locals.scm b/queries/cpp/locals.scm index 00268442c..061153c04 100644 --- a/queries/cpp/locals.scm +++ b/queries/cpp/locals.scm @@ -41,9 +41,9 @@ ;; Control structures (try_statement - body: (*) @scope) + body: (_) @scope) (catch_clause) @scope (destructor_name - name: (*) @constructor) + name: (_) @constructor) From 8bc00cf02a5bc73541ba76ad8ea70fd928fffce2 Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Sun, 14 Jun 2020 15:12:38 +0200 Subject: [PATCH 14/87] Change regexes in C/C++ highlights --- queries/c/highlights.scm | 2 +- queries/cpp/highlights.scm | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/queries/c/highlights.scm b/queries/c/highlights.scm index 568ebaffa..0d718c223 100644 --- a/queries/c/highlights.scm +++ b/queries/c/highlights.scm @@ -97,7 +97,7 @@ (#match? @type "^[A-Z]")) ((identifier) @constant - (#match? @constant "^[A-Z][A-Z\\d_]+$")) + (#match? @constant "^[A-Z][A-Z0-9_]+$")) (comment) @comment diff --git a/queries/cpp/highlights.scm b/queries/cpp/highlights.scm index 68e5047ab..92909f4d9 100644 --- a/queries/cpp/highlights.scm +++ b/queries/cpp/highlights.scm @@ -23,7 +23,7 @@ ((namespace_identifier) @type (#match? @type "^[A-Z]")) ((namespace_identifier) @constant - (#match? @constant "^[A-Z][A-Z_1-9]*$")) + (#match? @constant "^[A-Z][A-Z_0-9]*$")) (destructor_name name: (_) @function) From 0671c0c6a28bc62e5467d744d72466817d8f30e7 Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Mon, 15 Jun 2020 12:07:50 +0200 Subject: [PATCH 15/87] Fixup: Introduce base languages for queries Use same argument for nvim_get_runtime_file for base language --- lua/nvim-treesitter/query.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/nvim-treesitter/query.lua b/lua/nvim-treesitter/query.lua index 788bfa126..bd0f4c542 100644 --- a/lua/nvim-treesitter/query.lua +++ b/lua/nvim-treesitter/query.lua @@ -30,7 +30,7 @@ function M.get_query(ft, query_name) end for _, base_lang in ipairs(M.base_language_map[ft] or {}) do - local base_files = api.nvim_get_runtime_file(string.format('queries/%s/%s.scm', base_lang, query_name), false) + local base_files = api.nvim_get_runtime_file(string.format('queries/%s/%s.scm', base_lang, query_name), true) if base_files and #base_files > 0 then query_string = read_query_files(base_files)..query_string end From a600be80b68803c949d43e384c847315f5ab4155 Mon Sep 17 00:00:00 2001 From: Steven Sojka Date: Mon, 15 Jun 2020 08:53:49 -0500 Subject: [PATCH 16/87] chore(queries): use inherited queries for ts/js --- queries/typescript/highlights.scm | 188 ------------------------------ queries/typescript/locals.scm | 35 ------ 2 files changed, 223 deletions(-) diff --git a/queries/typescript/highlights.scm b/queries/typescript/highlights.scm index 92820105b..dd0dd778f 100644 --- a/queries/typescript/highlights.scm +++ b/queries/typescript/highlights.scm @@ -1,192 +1,4 @@ -; Types - -; Javascript -; Special identifiers -;-------------------- - -((identifier) @constant - (#match? @constant "^[A-Z_][A-Z\\d_]+$")) - -((shorthand_property_identifier) @constant - (#match? @constant "^[A-Z_][A-Z\\d_]+$")) - -((identifier) @constructor - (#match? @constructor "^[A-Z]")) - -((identifier) @variable.builtin - (#match? @variable.builtin "^(arguments|module|console|window|document)$")) - -((identifier) @function.builtin - (#eq? @function.builtin "require")) - -; Function and method definitions -;-------------------------------- - -(function - name: (identifier) @function) -(function_declaration - name: (identifier) @function) -(method_definition - name: (property_identifier) @function.method) - -(pair - key: (property_identifier) @function.method - value: (function)) -(pair - key: (property_identifier) @function.method - value: (arrow_function)) - -(assignment_expression - left: (member_expression - property: (property_identifier) @function.method) - right: (arrow_function)) -(assignment_expression - left: (member_expression - property: (property_identifier) @function.method) - right: (function)) - -(variable_declarator - name: (identifier) @function - value: (arrow_function)) -(variable_declarator - name: (identifier) @function - value: (function)) - -(assignment_expression - left: (identifier) @function - right: (arrow_function)) -(assignment_expression - left: (identifier) @function - right: (function)) - -; Function and method calls -;-------------------------- - -(call_expression - function: (identifier) @function) - -(call_expression - function: (member_expression - property: (property_identifier) @function.method)) - -; Variables -;---------- - -(formal_parameters (identifier) @variable.parameter) - -(identifier) @variable - -; Properties -;----------- - -(property_identifier) @property - -; Literals -;--------- - -(this) @variable.builtin -(super) @variable.builtin - -(true) @constant.builtin -(false) @constant.builtin -(null) @constant.builtin -(comment) @comment -(string) @string -(regex) @string.special -(template_string) @string -(number) @number - -; Punctuation -;------------ - -(template_substitution - "${" @punctuation.special - "}" @punctuation.special) @embedded - -";" @punctuation.delimiter -"." @punctuation.delimiter -"," @punctuation.delimiter - -"--" @operator -"-" @operator -"-=" @operator -"&&" @operator -"+" @operator -"++" @operator -"+=" @operator -"<" @operator -"<<" @operator -"=" @operator -"==" @operator -"===" @operator -"=>" @operator -">" @operator -">>" @operator -"||" @operator -"??" @operator - -"(" @punctuation.bracket -")" @punctuation.bracket -"[" @punctuation.bracket -"]" @punctuation.bracket -"{" @punctuation.bracket -"}" @punctuation.bracket - -; Keywords -;---------- - [ -"if" -"else" -"switch" -"case" -"default" -] @conditional - -[ -"import" -"from" -"as" -] @include - -[ -"for" -"of" -"do" -"while" -"continue" -] @repeat - -[ -"async" -"await" -"break" -"catch" -"class" -"const" -"debugger" -"delete" -"export" -"extends" -"finally" -"function" -"get" -"in" -"instanceof" -"let" -"new" -"return" -"set" -"static" -"switch" -"target" -"throw" -"try" -"typeof" -"var" -"void" -"with" -"yield" "abstract" "declare" "enum" diff --git a/queries/typescript/locals.scm b/queries/typescript/locals.scm index d308e9873..cb064b823 100644 --- a/queries/typescript/locals.scm +++ b/queries/typescript/locals.scm @@ -1,37 +1,2 @@ -; Scopes -;------- - -(statement_block) @scope -(function) @scope -(arrow_function) @scope -(function_declaration) @scope -(method_definition) @scope - -; Definitions -;------------ - -(formal_parameters - (identifier) @definition) - -(formal_parameters - (object_pattern - (identifier) @definition)) - -(formal_parameters - (object_pattern - (shorthand_property_identifier) @definition)) - -(formal_parameters - (array_pattern - (identifier) @definition)) - -(variable_declarator - name: (identifier) @definition) - -; References -;------------ - -(identifier) @reference - (required_parameter (identifier) @definition) (optional_parameter (identifier) @definition) From 042464c1c5c4e1a88717430b26347b84a4adcdf0 Mon Sep 17 00:00:00 2001 From: Chinmay Date: Mon, 15 Jun 2020 21:48:50 +0530 Subject: [PATCH 17/87] Fix method highlighting --- queries/java/highlights.scm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/queries/java/highlights.scm b/queries/java/highlights.scm index 398cfe13f..59b803abf 100644 --- a/queries/java/highlights.scm +++ b/queries/java/highlights.scm @@ -1,9 +1,9 @@ ; Methods (method_declaration - name: (identifier) @function.method) + name: (identifier) @method) (method_invocation - name: (identifier) @function.method) + name: (identifier) @method) (super) @function.builtin ; Annotations From d262d4101e55450e8c0092c2131f19e70dc1a261 Mon Sep 17 00:00:00 2001 From: Chinmay Date: Mon, 15 Jun 2020 22:45:56 +0530 Subject: [PATCH 18/87] Add operators --- queries/java/highlights.scm | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/queries/java/highlights.scm b/queries/java/highlights.scm index 59b803abf..71e8a74a4 100644 --- a/queries/java/highlights.scm +++ b/queries/java/highlights.scm @@ -13,7 +13,33 @@ (marker_annotation name: (identifier) @attribute) -"@" @operator +; Operators +[ +"@" +"+" +"++" +"-" +"--" +"&" +"&&" +"|" +"||" +"!=" +"==" +"*" +"/" +"%" +"<" +"<=" +">" +">=" +"=" +"-=" +"+=" +"*=" +"/=" +"%=" +] @operator ; Types From 6a7ad3f01185f1da9a033f909386eb0bca0a6922 Mon Sep 17 00:00:00 2001 From: Chinmay Dalal Date: Mon, 15 Jun 2020 23:12:20 +0530 Subject: [PATCH 19/87] Add java to supported languages --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c31ce8985..139214b44 100644 --- a/README.md +++ b/README.md @@ -166,7 +166,7 @@ List of currently supported languages: - [x] html (maintained by @TravonteD) - [ ] csharp - [ ] swift -- [ ] java +- [x] java - [ ] ocaml - [x] css (maintained by @TravonteD) - [ ] julia From a6c426b2bd60004a3b80e8727d50103747dbd4c0 Mon Sep 17 00:00:00 2001 From: Chinmay Dalal Date: Mon, 15 Jun 2020 23:23:17 +0530 Subject: [PATCH 20/87] Fix types --- queries/java/highlights.scm | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/queries/java/highlights.scm b/queries/java/highlights.scm index 71e8a74a4..9b4a9555b 100644 --- a/queries/java/highlights.scm +++ b/queries/java/highlights.scm @@ -81,9 +81,9 @@ (hex_integer_literal) @number (decimal_integer_literal) @number (octal_integer_literal) @number -(decimal_floating_point_literal) @number -(hex_floating_point_literal) @number -(character_literal) @string +(decimal_floating_point_literal) @float +(hex_floating_point_literal) @float +(character_literal) @character (string_literal) @string (null_literal) @constant.builtin From a896f5579dfc8c1852b72d7698aa84c6705a46e0 Mon Sep 17 00:00:00 2001 From: Steven Sojka Date: Mon, 15 Jun 2020 15:11:07 -0500 Subject: [PATCH 21/87] fix(javascript): set booleans to boolean highlights --- queries/javascript/highlights.scm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/queries/javascript/highlights.scm b/queries/javascript/highlights.scm index da88d85d0..dfbabc977 100644 --- a/queries/javascript/highlights.scm +++ b/queries/javascript/highlights.scm @@ -87,8 +87,8 @@ (this) @variable.builtin (super) @variable.builtin -(true) @constant.builtin -(false) @constant.builtin +(true) @boolean +(false) @boolean (null) @constant.builtin (comment) @comment (string) @string From 7bc746f3f0f2e8195c39c3ff3029379ad39d9f33 Mon Sep 17 00:00:00 2001 From: Steven Sojka Date: Mon, 15 Jun 2020 13:13:26 -0500 Subject: [PATCH 22/87] feat(lang): add json highlights --- README.md | 2 +- queries/json/highlights.scm | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 queries/json/highlights.scm diff --git a/README.md b/README.md index 6a45ebee9..3df340334 100644 --- a/README.md +++ b/README.md @@ -162,7 +162,7 @@ List of currently supported languages: - [x] javascript (maintained by @steelsojka) - [x] typescript (maintained by @steelsojka) - [ ] tsx -- [ ] json +- [x] json (maintained by @steelsojka) - [x] html (maintained by @TravonteD) - [ ] csharp - [ ] swift diff --git a/queries/json/highlights.scm b/queries/json/highlights.scm new file mode 100644 index 000000000..bdc35ad98 --- /dev/null +++ b/queries/json/highlights.scm @@ -0,0 +1,13 @@ +(true) @boolean +(false) @boolean +(null) @constant.builtin +(number) @number +(pair key: (string) @label) +(pair value: (string) @string) +(string_content (escape_sequence) @string.escape) +(ERROR) @error +"," @punctuation.delimiter +"[" @punctuation.bracket +"]" @punctuation.bracket +"{" @punctuation.bracket +"}" @punctuation.bracket From 3302dd3a5b3464ff31600ae7b4a62571090acc7a Mon Sep 17 00:00:00 2001 From: Chinmay Dalal Date: Wed, 17 Jun 2020 18:01:29 +0530 Subject: [PATCH 23/87] Give credits --- queries/java/highlights.scm | 2 ++ 1 file changed, 2 insertions(+) diff --git a/queries/java/highlights.scm b/queries/java/highlights.scm index 9b4a9555b..e51099e58 100644 --- a/queries/java/highlights.scm +++ b/queries/java/highlights.scm @@ -1,3 +1,5 @@ +; CREDITS @maxbrunsfeld (maxbrunsfeld@gmail.com) + ; Methods (method_declaration From 031e3e45c47029c758a62f487e87a920044b25fb Mon Sep 17 00:00:00 2001 From: Chinmay Dalal Date: Wed, 17 Jun 2020 18:01:53 +0530 Subject: [PATCH 24/87] Give credits --- queries/java/locals.scm | 2 ++ 1 file changed, 2 insertions(+) diff --git a/queries/java/locals.scm b/queries/java/locals.scm index 9044c98d7..56c629f4e 100644 --- a/queries/java/locals.scm +++ b/queries/java/locals.scm @@ -1,3 +1,5 @@ +; CREDITS @maxbrunsfeld (maxbrunsfeld@gmail.com) + (class_declaration name: (identifier) @name) @class From 38af29a912a607d3d703f57b840bfc61671679cc Mon Sep 17 00:00:00 2001 From: kiyan42 Date: Fri, 19 Jun 2020 13:45:33 +0200 Subject: [PATCH 25/87] refacto: remove buf_state - remove buf_state and related code - add get_node_at_cursor() - better incremental selection (code is localized) --- lua/nvim-treesitter.lua | 21 +-- lua/nvim-treesitter/configs.lua | 76 +++++------ lua/nvim-treesitter/incremental_selection.lua | 76 ++++++----- lua/nvim-treesitter/install.lua | 3 +- lua/nvim-treesitter/locals.lua | 4 +- lua/nvim-treesitter/parsers.lua | 5 +- lua/nvim-treesitter/state.lua | 122 ------------------ lua/nvim-treesitter/ts_utils.lua | 7 + lua/nvim-treesitter/utils.lua | 19 --- 9 files changed, 100 insertions(+), 233 deletions(-) delete mode 100644 lua/nvim-treesitter/state.lua diff --git a/lua/nvim-treesitter.lua b/lua/nvim-treesitter.lua index 100324e36..d3c7fa75f 100644 --- a/lua/nvim-treesitter.lua +++ b/lua/nvim-treesitter.lua @@ -2,10 +2,9 @@ local api = vim.api local install = require'nvim-treesitter.install' local utils = require'nvim-treesitter.utils' +local ts_utils = require'nvim-treesitter.ts_utils' local info = require'nvim-treesitter.info' local configs = require'nvim-treesitter.configs' -local state = require'nvim-treesitter.state' -local ts_utils = require'nvim-treesitter.ts_utils' local M = {} @@ -21,20 +20,13 @@ function M.setup() api.nvim_command(string.format("autocmd NvimTreesitter FileType %s %s", ft, cmd)) end end - local cmd = string.format("lua require'nvim-treesitter.state'.attach_to_buffer(%s)", ft) - api.nvim_command(string.format('autocmd NvimTreesitter FileType %s %s', ft, cmd)) end - - state.run_update() end function M.statusline(indicator_size) local indicator_size = indicator_size or 100 - local bufnr = api.nvim_get_current_buf() - local buf_state = state.get_buf_state(bufnr) - if not buf_state then return "" end - local current_node = buf_state.current_node + local current_node = ts_utils.get_node_at_cursor() if not current_node then return "" end local expr = current_node:parent() @@ -56,13 +48,4 @@ function M.statusline(indicator_size) end end -function M.get_buf_state() - local bufnr = api.nvim_get_current_buf() - return state.exposed_state(bufnr) -end - -function M.get_node_api() - return ts_utils -end - return M diff --git a/lua/nvim-treesitter/configs.lua b/lua/nvim-treesitter/configs.lua index f7b5ff2cf..633f9d21c 100644 --- a/lua/nvim-treesitter/configs.lua +++ b/lua/nvim-treesitter/configs.lua @@ -1,137 +1,137 @@ local api = vim.api local queries = require'nvim-treesitter.query' -local utils = require'nvim-treesitter.utils' +local parsers = require'nvim-treesitter.parsers' -local parsers = {} +local parserlist = {} -parsers.javascript = { +parserlist.javascript = { install_info = { url = "https://github.com/tree-sitter/tree-sitter-javascript", files = { "src/parser.c", "src/scanner.c" }, } } -parsers.c = { +parserlist.c = { install_info = { url = "https://github.com/tree-sitter/tree-sitter-c", files = { "src/parser.c" } } } -parsers.cpp = { +parserlist.cpp = { install_info = { url = "https://github.com/tree-sitter/tree-sitter-cpp", files = { "src/parser.c", "src/scanner.cc" } } } -parsers.rust = { +parserlist.rust = { install_info = { url = "https://github.com/tree-sitter/tree-sitter-rust", files = { "src/parser.c", "src/scanner.c" }, } } -parsers.lua = { +parserlist.lua = { install_info = { url = "https://github.com/nvim-treesitter/tree-sitter-lua", files = { "src/parser.c", "src/scanner.cc" } } } -parsers.python = { +parserlist.python = { install_info = { url = "https://github.com/tree-sitter/tree-sitter-python", files = { "src/parser.c", "src/scanner.cc" }, } } -parsers.go = { +parserlist.go = { install_info = { url = "https://github.com/tree-sitter/tree-sitter-go", files = { "src/parser.c" }, } } -parsers.ruby = { +parserlist.ruby = { install_info = { url = "https://github.com/tree-sitter/tree-sitter-ruby", files = { "src/parser.c", "src/scanner.cc" }, } } -parsers.bash = { +parserlist.bash = { install_info = { url = "https://github.com/tree-sitter/tree-sitter-bash", files = { "src/parser.c", "src/scanner.cc" }, } } -parsers.php = { +parserlist.php = { install_info = { url = "https://github.com/tree-sitter/tree-sitter-php", files = { "src/parser.c", "src/scanner.cc" }, } } -parsers.java = { +parserlist.java = { install_info = { url = "https://github.com/tree-sitter/tree-sitter-java", files = { "src/parser.c" }, } } -parsers.html = { +parserlist.html = { install_info = { url = "https://github.com/tree-sitter/tree-sitter-html", files = { "src/parser.c", "src/scanner.cc" }, } } -parsers.julia = { +parserlist.julia = { install_info = { url = "https://github.com/tree-sitter/tree-sitter-julia", files = { "src/parser.c", "src/scanner.c" }, } } -parsers.json = { +parserlist.json = { install_info = { url = "https://github.com/tree-sitter/tree-sitter-json", files = { "src/parser.c" }, } } -parsers.css = { +parserlist.css = { install_info = { url = "https://github.com/tree-sitter/tree-sitter-css", files = { "src/parser.c", "src/scanner.c" }, } } -parsers.ocaml = { +parserlist.ocaml = { install_info = { url = "https://github.com/tree-sitter/tree-sitter-ocaml", files = { "src/parser.c", "src/scanner.cc" }, } } -parsers.swift = { +parserlist.swift = { install_info = { url = "https://github.com/tree-sitter/tree-sitter-swift", files = { "src/parser.c" }, } } -parsers.csharp = { +parserlist.csharp = { install_info = { url = "https://github.com/tree-sitter/tree-sitter-c-sharp", files = { "src/parser.c", "src/scanner.c" }, } } -parsers.typescript = { +parserlist.typescript = { install_info = { url = "https://github.com/tree-sitter/tree-sitter-typescript", files = { "src/parser.c", "src/scanner.c" }, @@ -139,7 +139,7 @@ parsers.typescript = { } } -parsers.tsx = { +parserlist.tsx = { install_info = { url = "https://github.com/tree-sitter/tree-sitter-typescript", files = { "src/parser.c", "src/scanner.c" }, @@ -147,63 +147,63 @@ parsers.tsx = { } } -parsers.scala = { +parserlist.scala = { install_info = { url = "https://github.com/tree-sitter/tree-sitter-scala", files = { "src/parser.c", "src/scanner.c" }, } } -parsers.haskell = { +parserlist.haskell = { install_info = { url = "https://github.com/tree-sitter/tree-sitter-haskell", files = { "src/parser.c", "src/scanner.cc" }, } } -parsers.markdown = { +parserlist.markdown = { install_info = { url = "https://github.com/ikatyang/tree-sitter-markdown", files = { "src/parser.c", "src/scanner.cc" }, } } -parsers.toml = { +parserlist.toml = { install_info = { url = "https://github.com/ikatyang/tree-sitter-toml", files = { "src/parser.c", "src/scanner.c" }, } } -parsers.vue = { +parserlist.vue = { install_info = { url = "https://github.com/ikatyang/tree-sitter-vue", files = { "src/parser.c", "src/scanner.cc" }, } } -parsers.elm = { +parserlist.elm = { install_info = { url = "https://github.com//razzeee/tree-sitter-elm", files = { "src/parser.c", "src/scanner.cc" }, } } -parsers.yaml = { +parserlist.yaml = { install_info = { url = "https://github.com/ikatyang/tree-sitter-yaml", files = { "src/parser.c", "src/scanner.cc" }, } } -parsers.nix = { +parserlist.nix = { install_info = { url = "https://github.com/cstrahan/tree-sitter-nix", files = { "src/parser.c", "src/scanner.cc" }, } } -parsers.regex = { +parserlist.regex = { install_info = { url = "https://github.com/tree-sitter/tree-sitter-regex", files = { "src/parser.c" } @@ -245,7 +245,7 @@ local M = {} local function enable_module(mod, bufnr, ft) local bufnr = bufnr or api.nvim_get_current_buf() local ft = ft or api.nvim_buf_get_option(bufnr, 'ft') - if not parsers[ft] or not config.modules[mod] then + if not parserlist[ft] or not config.modules[mod] then return end @@ -275,12 +275,12 @@ local function enable_all(mod, ft) end end if ft then - if utils.has_parser(ft) then + if parsers.has_parser(ft) then enable_mod_conf_autocmd(mod, ft) end else for _, ft in pairs(M.available_parsers()) do - if utils.has_parser(ft) then + if parsers.has_parser(ft) then enable_mod_conf_autocmd(mod, ft) end end @@ -291,7 +291,7 @@ end local function disable_module(mod, bufnr, ft) local bufnr = bufnr or api.nvim_get_current_buf() local ft = ft or api.nvim_buf_get_option(bufnr, 'ft') - if not parsers[ft] or not config.modules[mod] then + if not parserlist[ft] or not config.modules[mod] then return end @@ -360,7 +360,7 @@ M.commands = { -- @param mod: module (string) -- @param ft: filetype (string) function M.is_enabled(mod, ft) - if not M.get_parser_configs()[ft] or not utils.has_parser(ft) then + if not M.get_parser_configs()[ft] or not parsers.has_parser(ft) then return false end @@ -404,11 +404,11 @@ function M.setup(user_data) end function M.get_parser_configs() - return parsers + return parserlist end function M.available_parsers() - return vim.tbl_keys(parsers) + return vim.tbl_keys(parserlist) end function M.available_modules() diff --git a/lua/nvim-treesitter/incremental_selection.lua b/lua/nvim-treesitter/incremental_selection.lua index 9876f2e01..6a9999303 100644 --- a/lua/nvim-treesitter/incremental_selection.lua +++ b/lua/nvim-treesitter/incremental_selection.lua @@ -1,11 +1,13 @@ local api = vim.api -local state = require'nvim-treesitter.state' local configs = require'nvim-treesitter.configs' local ts_utils = require'nvim-treesitter.ts_utils' +local parsers = require'nvim-treesitter.parsers' local M = {} +local selections = {} + local function update_selection(buf, node) local start_row, start_col, end_row, end_col = node:range() @@ -18,32 +20,49 @@ local function update_selection(buf, node) vim.fn.setpos(".", { buf, end_row+1, end_col+1, 0 }) end +function M.init_selection() + local buf = api.nvim_get_current_buf() + local node = ts_utils.get_node_at_cursor() + selections[buf] = { [1] = node } + update_selection(buf, node) +end + +local function visual_selection_range() + local _, csrow, cscol, _ = unpack(vim.fn.getpos("'<")) + local _, cerow, cecol, _ = unpack(vim.fn.getpos("'>")) + if csrow < cerow then + return csrow-1, cscol-1, cerow-1, cecol-1 + else + return cerow-1, cecol-1, csrow-1, cscol-1 + end +end + +local function range_matches(node) + local csrow, cscol, cerow, cecol = visual_selection_range() + local srow, scol, erow, ecol = node:range() + return srow == csrow and scol == cscol and erow == cerow and ecol == cecol +end + local function select_incremental(get_parent) return function() local buf = api.nvim_get_current_buf() - local buf_state = state.get_buf_state(buf) + local nodes = selections[buf] - local node - -- initialize incremental selection with current range - if #buf_state.selection.nodes == 0 then - local cur_range = buf_state.selection.range - if not cur_range then - local _, cursor_row, cursor_col, _ = unpack(vim.fn.getpos(".")) - cur_range = { cursor_row, cursor_col, cursor_row, cursor_col + 1 } - end - - local root = buf_state.parser.tree:root() - if not root then return end - - node = root:named_descendant_for_range(cur_range[1]-1, cur_range[2]-1, cur_range[3]-1, cur_range[4]-1) - else - node = get_parent(buf_state.selection.nodes[#buf_state.selection.nodes]) + -- initialize incremental selection with current selection + if not nodes or #nodes == 0 or not range_matches(nodes[#nodes]) then + local csrow, cscol, cerow, cecol = visual_selection_range() + local root = parsers.get_parser().tree:root() + local node = root:named_descendant_for_range(csrow, cscol, cerow, cecol) + update_selection(buf, node) + selections[buf] = { [1] = node } + return end + node = get_parent(nodes[#nodes]) if not node then return end - if node ~= buf_state.selection.nodes[#buf_state.selection.nodes] then - state.insert_selection_node(buf, node) + if node ~= nodes[#nodes] then + table.insert(nodes, node) end update_selection(buf, node) @@ -60,13 +79,10 @@ end) function M.node_decremental() local buf = api.nvim_get_current_buf() - local buf_state = state.get_buf_state(buf) - - local nodes = buf_state.selection.nodes - if #nodes < 2 then return end - - state.pop_selection_node(buf) + local nodes = selections[buf] + if not nodes or #nodes < 2 then return end + table.remove(selections[buf]) local node = nodes[#nodes] update_selection(buf, node) end @@ -76,14 +92,14 @@ function M.attach(bufnr) local config = configs.get_module('incremental_selection') for funcname, mapping in pairs(config.keymaps) do - + local mode if funcname == "init_selection" then - local cmd = ":lua require'nvim-treesitter.incremental_selection'.node_incremental()" - api.nvim_buf_set_keymap(buf, 'n', mapping, cmd, { silent = true }) + mode = 'n' else - local cmd = string.format(":lua require'nvim-treesitter.incremental_selection'.%s()", funcname) - api.nvim_buf_set_keymap(buf, 'v', mapping, cmd, { silent = true }) + mode = 'v' end + local cmd = string.format(":lua require'nvim-treesitter.incremental_selection'.%s()", funcname) + api.nvim_buf_set_keymap(buf, mode, mapping, cmd, { silent = true }) end end diff --git a/lua/nvim-treesitter/install.lua b/lua/nvim-treesitter/install.lua index 637f1dedb..381afaa4a 100644 --- a/lua/nvim-treesitter/install.lua +++ b/lua/nvim-treesitter/install.lua @@ -4,6 +4,7 @@ local luv = vim.loop local configs = require'nvim-treesitter.configs' local utils = require'nvim-treesitter.utils' +local parsers = require'nvim-treesitter.parsers' local M = {} @@ -128,7 +129,7 @@ M.ensure_installed = function(languages) end for _, ft in ipairs(languages) do - if not utils.has_parser(ft) then + if not parsers.has_parser(ft) then install(ft) end end diff --git a/lua/nvim-treesitter/locals.lua b/lua/nvim-treesitter/locals.lua index 313d7655d..cbea89108 100644 --- a/lua/nvim-treesitter/locals.lua +++ b/lua/nvim-treesitter/locals.lua @@ -5,7 +5,7 @@ local api = vim.api local ts = vim.treesitter local queries = require'nvim-treesitter.query' -local utils = require'nvim-treesitter.utils' +local parsers = require'nvim-treesitter.parsers' local M = { locals = {} @@ -18,7 +18,7 @@ function M.collect_locals(bufnr) local query = queries.get_query(ft, 'locals') if not query then return end - local parser = utils.get_parser(bufnr, ft) + local parser = parsers.get_parser(bufnr, ft) if not parser then return end local root = parser:parse():root() diff --git a/lua/nvim-treesitter/parsers.lua b/lua/nvim-treesitter/parsers.lua index 07f3e9d34..3789b7afe 100644 --- a/lua/nvim-treesitter/parsers.lua +++ b/lua/nvim-treesitter/parsers.lua @@ -10,9 +10,10 @@ function M.has_parser(lang) end function M.get_parser(bufnr, lang) + local buf = bufnr or api.nvim_get_current_buf() + local lang = lang or api.nvim_buf_get_option(buf, 'ft') + if M.has_parser(lang) then - local buf = bufnr or api.nvim_get_current_buf() - local lang = lang or api.nvim_buf_get_option(buf, 'ft') if not M[buf] then M[buf] = {} end diff --git a/lua/nvim-treesitter/state.lua b/lua/nvim-treesitter/state.lua deleted file mode 100644 index adff7eeaf..000000000 --- a/lua/nvim-treesitter/state.lua +++ /dev/null @@ -1,122 +0,0 @@ -local api = vim.api - -local utils = require'nvim-treesitter.utils' - -local M = {} - -local buffers = {} - -local g_mode = api.nvim_get_mode().mode - -local function get_selection_range() - local _, vstart_row, vstart_col, _ = unpack(vim.fn.getpos("v")) - local _, cursor_row, cursor_col, _ = unpack(vim.fn.getpos(".")) - if vstart_row < cursor_row then - return vstart_row, vstart_col, cursor_row, cursor_col - else - return cursor_row, cursor_col, vstart_row, vstart_col - end -end - -function M.update() - local bufnr = api.nvim_get_current_buf() - local buf_config = buffers[bufnr] - if not buf_config then return end - - local mode = api.nvim_get_mode().mode - local cursor = api.nvim_win_get_cursor(0) - local row = cursor[1] - local col = cursor[2] - if row == buf_config.cursor_pos.row - and col == buf_config.cursor_pos.col - and mode == g_mode - then - return - end - - local root = buf_config.parser.tree:root() - if not root then return end - - local new_node = root:named_descendant_for_range(row - 1, col, row - 1, col) - - if new_node ~= buf_config.current_node then - buf_config.current_node = new_node - end - - -- We only want to update the range when the incremental selection has not started yet - if mode == "v" and #buf_config.selection.nodes == 0 then - local row_start, col_start, row_end, col_end = get_selection_range() - buf_config.selection.range = { row_start, col_start, row_end, col_end } - elseif mode ~= "v" then - buf_config.selection.nodes = {} - buf_config.selection.range = nil - end - - g_mode = mode - buf_config.cursor_pos.row = row - buf_config.cursor_pos.col = col -end - -function M.insert_selection_node(bufnr, range) - local buf_config = buffers[bufnr] - if not buf_config then return end - - table.insert(buffers[bufnr].selection.nodes, range) -end - -function M.pop_selection_node(bufnr) - local buf_config = buffers[bufnr] - if not buf_config then return end - - table.remove( - buffers[bufnr].selection.nodes, - #buffers[bufnr].selection.nodes - ) -end - -function M.run_update() - local cmd = "lua require'nvim-treesitter.state'.update()" - api.nvim_command('autocmd NvimTreesitter CursorMoved * '..cmd) -end - -function M.attach_to_buffer(ft) - local bufnr = api.nvim_get_current_buf() - local ft = ft or api.nvim_buf_get_option(bufnr, 'ft') - - if buffers[bufnr] then return end - - local parser = utils.get_parser(bufnr, ft) - if not parser then return end - - buffers[bufnr] = { - cursor_pos = {}, - current_node = nil, - selection = { - range = nil, - nodes = {} - }, - parser = parser, - } - - M.update() - api.nvim_buf_attach(bufnr, false, { - -- TODO(kyazdani): on lines should only parse the changed content - -- TODO(kyazdani): add a timer to avoid too frequent updates - on_lines = function(_, buf) buffers[buf].parser:parse() end, - on_detach = function(bufnr) buffers[bufnr] = nil end, - }) -end - -function M.get_buf_state(bufnr) - return buffers[bufnr] -end - -function M.exposed_state(bufnr) - local buf_state = buffers[bufnr] - return { - cursor_pos = buf_state.cursor_pos, - current_node = buf_state.current_node - } -end - -return M diff --git a/lua/nvim-treesitter/ts_utils.lua b/lua/nvim-treesitter/ts_utils.lua index 1445418e0..a85934c0b 100644 --- a/lua/nvim-treesitter/ts_utils.lua +++ b/lua/nvim-treesitter/ts_utils.lua @@ -1,6 +1,7 @@ local api = vim.api local locals = require'nvim-treesitter.locals' +local parsers = require'nvim-treesitter.parsers' local M = {} @@ -205,4 +206,10 @@ function M.previous_scope(node) end end +function M.get_node_at_cursor() + local cursor = api.nvim_win_get_cursor(0) + local root = parsers.get_parser().tree:root() + return root:named_descendant_for_range(cursor[1]-1,cursor[2],cursor[1]-1,cursor[2]) +end + return M diff --git a/lua/nvim-treesitter/utils.lua b/lua/nvim-treesitter/utils.lua index 9dc5d17bd..6fa159817 100644 --- a/lua/nvim-treesitter/utils.lua +++ b/lua/nvim-treesitter/utils.lua @@ -45,23 +45,4 @@ function M.get_cache_dir() return nil, 'Invalid cache rights, $XDG_CACHE_HOME or /tmp should be read/write' end -function M.has_parser(lang) - local lang = lang or api.nvim_buf_get_option(0, 'filetype') - return #api.nvim_get_runtime_file('parser/' .. lang .. '.so', false) > 0 -end - -function M.get_parser(bufnr, lang) - if M.has_parser() then - local buf = bufnr or api.nvim_get_current_buf() - local lang = lang or api.nvim_buf_get_option(buf, 'ft') - if not M[buf] then - M[buf] = {} - end - if not M[buf][lang] then - M[buf][lang] = ts.get_parser(buf, lang) - end - return M[buf][lang] - end -end - return M From cabe61ac19a2d0377a46ef48499eb292e12ae5fb Mon Sep 17 00:00:00 2001 From: kiyan42 Date: Fri, 19 Jun 2020 13:51:09 +0200 Subject: [PATCH 26/87] update docs --- README.md | 14 ++++--------- doc/nvim-treesitter.txt | 46 +++++++++++++++-------------------------- doc/tags | 23 +++++++++++---------- 3 files changed, 33 insertions(+), 50 deletions(-) diff --git a/README.md b/README.md index 33ce5d979..b2193103a 100644 --- a/README.md +++ b/README.md @@ -131,19 +131,13 @@ Some of these features are : You can find the roadmap [here](https://github.com/nvim-treesitter/nvim-treesitter/projects/1). The roadmap and all features of this plugin are open to change, and any suggestion will be highly appreciated! -## Api +## Utils -Nvim-treesitter exposes an api to extend node capabilites. You can retrieve the api like this: +you can get some utility functions with ```lua -local ts_node_api = require 'nvim-treesitter'.get_node_api() +local ts_utils = require 'nvim-treesitter.ts_utils' ``` - -You can also retrieve the current state of the current buffer with: -```lua -local buf_state = require'nvim-treesitter'.get_buf_state() -``` - -More information is available in neovim documentation (`:help nvim-treesitter-api`). +More information is available in neovim documentation (`:help nvim-treesitter-utils`). ## Supported Languages diff --git a/doc/nvim-treesitter.txt b/doc/nvim-treesitter.txt index f1c278427..27e4f0bb5 100644 --- a/doc/nvim-treesitter.txt +++ b/doc/nvim-treesitter.txt @@ -91,26 +91,28 @@ A list of languages can be found at |:TSInstallInfo| List modules state for the current session. ============================================================================== -API *nvim-treesitter-api* +UTILS *nvim-treesitter-utils* -Nvim treesitter exposes extended functions to use on nodes and scopes. -you can retrieve the api with: +Nvim treesitter has some wrapper functions that you can retrieve with: > - local ts_node_api = require 'nvim-treesitter'.get_node_api() + local ts_utils = require 'nvim-treesitter.ts_utils' < Methods -get_node_text(node, bufnr) *ts_api.get_node_text* +get_node_at_cursor() *ts_utils.get_node_at_cursor* + returns the node under the cursor + +get_node_text(node, bufnr) *ts_utils.get_node_text* return the text content of a node -is_parent(dest, source) *ts_api.is_parent* +is_parent(dest, source) *ts_utils.is_parent* determines wether `dest` is a parent of `source` return a boolean -get_named_children(node) *ts_api.get_named_children* +get_named_children(node) *ts_utils.get_named_children* return a table of named children of `node` -get_next_node(node, allow_switch_parent, allow_next_parent) *ts_api.get_next_node* +get_next_node(node, allow_switch_parent, allow_next_parent) *ts_utils.get_next_node* return the next node within the same parent. if no node is found, returns `nil`. if `allow_switch_parent` is true, it will allow switching parent @@ -118,42 +120,28 @@ get_next_node(node, allow_switch_parent, allow_next_parent) *ts_api.get_next_nod if `allow_next_parent` is true, it will allow next parent if the node is the last node and the next parent doesn't have children. -get_previous_node(node, allow_switch_parents, allow_prev_parent) *ts_api.get_previous_node* +get_previous_node(node, allow_switch_parents, allow_prev_parent) *ts_utils.get_previous_node* return the previous node within the same parent. `allow_switch_parent` and `allow_prev_parent` follow the same rule - as |ts_api.get_next_node| but if the node is the first node. + as |ts_utils.get_next_node| but if the node is the first node. -containing_scope(node) *ts_api.containing_scope* +containing_scope(node) *ts_utils.containing_scope* return the smallest scope containing the node -parent_scope(node, cursor_pos) *ts_api.parent_scope* +parent_scope(node, cursor_pos) *ts_utils.parent_scope* return the parent scope of the current scope that contains the node. `cursor_pos` should be `{ row = number, col = number }` - you can retrieve the cursor_pos with the buffer state -nested_scope(node, cursor_pos) *ts_api.nested_scope* +nested_scope(node, cursor_pos) *ts_utils.nested_scope* return the first scope within current scope that contains the node. `cursor_pos` should be `{ row = number, col = number }` - you can retrieve the cursor_pos with the buffer state -next_scope(node) *ts_api.next_scope* +next_scope(node) *ts_utils.next_scope* return the neighbour scope of the current node -previous_scope(node) *ts_api.previous_scope* +previous_scope(node) *ts_utils.previous_scope* return the previous neighbour scope of the current node -Nvim-treesitter also provides access to the state of the current buffer: -> - local cur_buf_state = require'nvim-treesitter'.get_buf_state() - print(vim.inspect(cur_buf_state)) - --[[ - { - cursor_pos = { row = number, col = number }, (current cursor pos in the buffer) - current_node = tsnode (smallest node the cursor is on) - } - ]]-- -< - ============================================================================== FUNCTIONS *nvim-treesitter-functions* diff --git a/doc/tags b/doc/tags index 0a12b18d5..df33c4d43 100644 --- a/doc/tags +++ b/doc/tags @@ -6,20 +6,21 @@ :TSInstallInfo nvim-treesitter.txt /*:TSInstallInfo* :TSModuleInfo nvim-treesitter.txt /*:TSModuleInfo* nvim-treesitter nvim-treesitter.txt /*nvim-treesitter* -nvim-treesitter-api nvim-treesitter.txt /*nvim-treesitter-api* nvim-treesitter-commands nvim-treesitter.txt /*nvim-treesitter-commands* nvim-treesitter-functions nvim-treesitter.txt /*nvim-treesitter-functions* nvim-treesitter-intro nvim-treesitter.txt /*nvim-treesitter-intro* nvim-treesitter-quickstart nvim-treesitter.txt /*nvim-treesitter-quickstart* +nvim-treesitter-utils nvim-treesitter.txt /*nvim-treesitter-utils* nvim_treesitter#foldexpr() nvim-treesitter.txt /*nvim_treesitter#foldexpr()* nvim_treesitter#statusline() nvim-treesitter.txt /*nvim_treesitter#statusline()* -ts_api.containing_scope nvim-treesitter.txt /*ts_api.containing_scope* -ts_api.get_named_children nvim-treesitter.txt /*ts_api.get_named_children* -ts_api.get_next_node nvim-treesitter.txt /*ts_api.get_next_node* -ts_api.get_node_text nvim-treesitter.txt /*ts_api.get_node_text* -ts_api.get_previous_node nvim-treesitter.txt /*ts_api.get_previous_node* -ts_api.is_parent nvim-treesitter.txt /*ts_api.is_parent* -ts_api.nested_scope nvim-treesitter.txt /*ts_api.nested_scope* -ts_api.next_scope nvim-treesitter.txt /*ts_api.next_scope* -ts_api.parent_scope nvim-treesitter.txt /*ts_api.parent_scope* -ts_api.previous_scope nvim-treesitter.txt /*ts_api.previous_scope* +ts_utils.containing_scope nvim-treesitter.txt /*ts_utils.containing_scope* +ts_utils.get_named_children nvim-treesitter.txt /*ts_utils.get_named_children* +ts_utils.get_next_node nvim-treesitter.txt /*ts_utils.get_next_node* +ts_utils.get_node_at_cursor nvim-treesitter.txt /*ts_utils.get_node_at_cursor* +ts_utils.get_node_text nvim-treesitter.txt /*ts_utils.get_node_text* +ts_utils.get_previous_node nvim-treesitter.txt /*ts_utils.get_previous_node* +ts_utils.is_parent nvim-treesitter.txt /*ts_utils.is_parent* +ts_utils.nested_scope nvim-treesitter.txt /*ts_utils.nested_scope* +ts_utils.next_scope nvim-treesitter.txt /*ts_utils.next_scope* +ts_utils.parent_scope nvim-treesitter.txt /*ts_utils.parent_scope* +ts_utils.previous_scope nvim-treesitter.txt /*ts_utils.previous_scope* From cf72524b2f7e3868ea74037004913af6f935f126 Mon Sep 17 00:00:00 2001 From: kiyan42 Date: Fri, 19 Jun 2020 14:13:23 +0200 Subject: [PATCH 27/87] add winnr to get_node_at_cursor --- README.md | 2 +- doc/nvim-treesitter.txt | 3 ++- lua/nvim-treesitter/ts_utils.lua | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index b2193103a..787a2a200 100644 --- a/README.md +++ b/README.md @@ -137,7 +137,7 @@ you can get some utility functions with ```lua local ts_utils = require 'nvim-treesitter.ts_utils' ``` -More information is available in neovim documentation (`:help nvim-treesitter-utils`). +More information is available in the help file (`:help nvim-treesitter-utils`). ## Supported Languages diff --git a/doc/nvim-treesitter.txt b/doc/nvim-treesitter.txt index 27e4f0bb5..bb53e182e 100644 --- a/doc/nvim-treesitter.txt +++ b/doc/nvim-treesitter.txt @@ -99,7 +99,8 @@ Nvim treesitter has some wrapper functions that you can retrieve with: < Methods -get_node_at_cursor() *ts_utils.get_node_at_cursor* +get_node_at_cursor(winnr) *ts_utils.get_node_at_cursor* + winnr will be 0 if nil returns the node under the cursor get_node_text(node, bufnr) *ts_utils.get_node_text* diff --git a/lua/nvim-treesitter/ts_utils.lua b/lua/nvim-treesitter/ts_utils.lua index a85934c0b..687a3c539 100644 --- a/lua/nvim-treesitter/ts_utils.lua +++ b/lua/nvim-treesitter/ts_utils.lua @@ -206,8 +206,8 @@ function M.previous_scope(node) end end -function M.get_node_at_cursor() - local cursor = api.nvim_win_get_cursor(0) +function M.get_node_at_cursor(winnr) + local cursor = api.nvim_win_get_cursor(winnr or 0) local root = parsers.get_parser().tree:root() return root:named_descendant_for_range(cursor[1]-1,cursor[2],cursor[1]-1,cursor[2]) end From ad636f4f5306b7741bc55ad63e6b12f9379a2515 Mon Sep 17 00:00:00 2001 From: kiyan42 Date: Fri, 19 Jun 2020 14:22:43 +0200 Subject: [PATCH 28/87] fix: declare parse names as their appropriate filetype and change clone url --- lua/nvim-treesitter/configs.lua | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lua/nvim-treesitter/configs.lua b/lua/nvim-treesitter/configs.lua index 633f9d21c..4942fac78 100644 --- a/lua/nvim-treesitter/configs.lua +++ b/lua/nvim-treesitter/configs.lua @@ -61,9 +61,9 @@ parserlist.ruby = { } } -parserlist.bash = { +parserlist.sh = { install_info = { - url = "https://github.com/tree-sitter/tree-sitter-bash", + url = "https://github.com/nvim-treesitter/tree-sitter-bash", files = { "src/parser.c", "src/scanner.cc" }, } } @@ -124,9 +124,9 @@ parserlist.swift = { } } -parserlist.csharp = { +parserlist.cs = { install_info = { - url = "https://github.com/tree-sitter/tree-sitter-c-sharp", + url = "https://github.com/nvim-treesitter/tree-sitter-c-sharp", files = { "src/parser.c", "src/scanner.c" }, } } @@ -139,9 +139,9 @@ parserlist.typescript = { } } -parserlist.tsx = { +parserlist.typescriptreact = { install_info = { - url = "https://github.com/tree-sitter/tree-sitter-typescript", + url = "https://github.com/nvim-treesitter/tree-sitter-typescript", files = { "src/parser.c", "src/scanner.c" }, location = "tree-sitter-tsx/tsx" } From df17a48c85b8e47bc4982b640dbb686e372cd81e Mon Sep 17 00:00:00 2001 From: kiyan42 Date: Sat, 20 Jun 2020 12:21:42 +0200 Subject: [PATCH 29/87] refactor: parser list and lang->ft/ft->lang - move parser list in `parsers.lua` - most `ft` variable where changed to `lang`, `ft` is only used on autocmd binding, and lang is used for everything else. Functions have been defined to make the switch between `ft` and `lang` --- lua/nvim-treesitter.lua | 6 +- lua/nvim-treesitter/configs.lua | 295 +++++------------------------- lua/nvim-treesitter/health.lua | 4 +- lua/nvim-treesitter/highlight.lua | 11 +- lua/nvim-treesitter/info.lua | 7 +- lua/nvim-treesitter/install.lua | 11 +- lua/nvim-treesitter/parsers.lua | 244 +++++++++++++++++++++++- lua/nvim-treesitter/query.lua | 8 +- plugin/nvim-treesitter.vim | 2 +- 9 files changed, 311 insertions(+), 277 deletions(-) diff --git a/lua/nvim-treesitter.lua b/lua/nvim-treesitter.lua index d3c7fa75f..dde4ac0fd 100644 --- a/lua/nvim-treesitter.lua +++ b/lua/nvim-treesitter.lua @@ -5,6 +5,7 @@ local utils = require'nvim-treesitter.utils' local ts_utils = require'nvim-treesitter.ts_utils' local info = require'nvim-treesitter.info' local configs = require'nvim-treesitter.configs' +local parsers = require'nvim-treesitter.parsers' local M = {} @@ -13,9 +14,10 @@ function M.setup() utils.setup_commands('info', info.commands) utils.setup_commands('configs', configs.commands) - for _, ft in pairs(configs.available_parsers()) do + for _, lang in pairs(parsers.available_parsers()) do for _, mod in pairs(configs.available_modules()) do - if configs.is_enabled(mod, ft) then + if configs.is_enabled(mod, lang) then + local ft = parsers.lang_to_ft(lang) local cmd = string.format("lua require'nvim-treesitter.%s'.attach()", mod) api.nvim_command(string.format("autocmd NvimTreesitter FileType %s %s", ft, cmd)) end diff --git a/lua/nvim-treesitter/configs.lua b/lua/nvim-treesitter/configs.lua index 4942fac78..ecad20136 100644 --- a/lua/nvim-treesitter/configs.lua +++ b/lua/nvim-treesitter/configs.lua @@ -3,213 +3,6 @@ local api = vim.api local queries = require'nvim-treesitter.query' local parsers = require'nvim-treesitter.parsers' -local parserlist = {} - -parserlist.javascript = { - install_info = { - url = "https://github.com/tree-sitter/tree-sitter-javascript", - files = { "src/parser.c", "src/scanner.c" }, - } -} - -parserlist.c = { - install_info = { - url = "https://github.com/tree-sitter/tree-sitter-c", - files = { "src/parser.c" } - } -} - -parserlist.cpp = { - install_info = { - url = "https://github.com/tree-sitter/tree-sitter-cpp", - files = { "src/parser.c", "src/scanner.cc" } - } -} - -parserlist.rust = { - install_info = { - url = "https://github.com/tree-sitter/tree-sitter-rust", - files = { "src/parser.c", "src/scanner.c" }, - } -} - -parserlist.lua = { - install_info = { - url = "https://github.com/nvim-treesitter/tree-sitter-lua", - files = { "src/parser.c", "src/scanner.cc" } - } -} - -parserlist.python = { - install_info = { - url = "https://github.com/tree-sitter/tree-sitter-python", - files = { "src/parser.c", "src/scanner.cc" }, - } -} - -parserlist.go = { - install_info = { - url = "https://github.com/tree-sitter/tree-sitter-go", - files = { "src/parser.c" }, - } -} - -parserlist.ruby = { - install_info = { - url = "https://github.com/tree-sitter/tree-sitter-ruby", - files = { "src/parser.c", "src/scanner.cc" }, - } -} - -parserlist.sh = { - install_info = { - url = "https://github.com/nvim-treesitter/tree-sitter-bash", - files = { "src/parser.c", "src/scanner.cc" }, - } -} - -parserlist.php = { - install_info = { - url = "https://github.com/tree-sitter/tree-sitter-php", - files = { "src/parser.c", "src/scanner.cc" }, - } -} - -parserlist.java = { - install_info = { - url = "https://github.com/tree-sitter/tree-sitter-java", - files = { "src/parser.c" }, - } -} - -parserlist.html = { - install_info = { - url = "https://github.com/tree-sitter/tree-sitter-html", - files = { "src/parser.c", "src/scanner.cc" }, - } -} - -parserlist.julia = { - install_info = { - url = "https://github.com/tree-sitter/tree-sitter-julia", - files = { "src/parser.c", "src/scanner.c" }, - } -} - -parserlist.json = { - install_info = { - url = "https://github.com/tree-sitter/tree-sitter-json", - files = { "src/parser.c" }, - } -} - -parserlist.css = { - install_info = { - url = "https://github.com/tree-sitter/tree-sitter-css", - files = { "src/parser.c", "src/scanner.c" }, - } -} - -parserlist.ocaml = { - install_info = { - url = "https://github.com/tree-sitter/tree-sitter-ocaml", - files = { "src/parser.c", "src/scanner.cc" }, - } -} - -parserlist.swift = { - install_info = { - url = "https://github.com/tree-sitter/tree-sitter-swift", - files = { "src/parser.c" }, - } -} - -parserlist.cs = { - install_info = { - url = "https://github.com/nvim-treesitter/tree-sitter-c-sharp", - files = { "src/parser.c", "src/scanner.c" }, - } -} - -parserlist.typescript = { - install_info = { - url = "https://github.com/tree-sitter/tree-sitter-typescript", - files = { "src/parser.c", "src/scanner.c" }, - location = "tree-sitter-typescript/typescript" - } -} - -parserlist.typescriptreact = { - install_info = { - url = "https://github.com/nvim-treesitter/tree-sitter-typescript", - files = { "src/parser.c", "src/scanner.c" }, - location = "tree-sitter-tsx/tsx" - } -} - -parserlist.scala = { - install_info = { - url = "https://github.com/tree-sitter/tree-sitter-scala", - files = { "src/parser.c", "src/scanner.c" }, - } -} - -parserlist.haskell = { - install_info = { - url = "https://github.com/tree-sitter/tree-sitter-haskell", - files = { "src/parser.c", "src/scanner.cc" }, - } -} - -parserlist.markdown = { - install_info = { - url = "https://github.com/ikatyang/tree-sitter-markdown", - files = { "src/parser.c", "src/scanner.cc" }, - } -} - -parserlist.toml = { - install_info = { - url = "https://github.com/ikatyang/tree-sitter-toml", - files = { "src/parser.c", "src/scanner.c" }, - } -} - -parserlist.vue = { - install_info = { - url = "https://github.com/ikatyang/tree-sitter-vue", - files = { "src/parser.c", "src/scanner.cc" }, - } -} - -parserlist.elm = { - install_info = { - url = "https://github.com//razzeee/tree-sitter-elm", - files = { "src/parser.c", "src/scanner.cc" }, - } -} - -parserlist.yaml = { - install_info = { - url = "https://github.com/ikatyang/tree-sitter-yaml", - files = { "src/parser.c", "src/scanner.cc" }, - } -} - -parserlist.nix = { - install_info = { - url = "https://github.com/cstrahan/tree-sitter-nix", - files = { "src/parser.c", "src/scanner.cc" }, - } -} - -parserlist.regex = { - install_info = { - url = "https://github.com/tree-sitter/tree-sitter-regex", - files = { "src/parser.c" } - } -} - -- @enable can be true or false -- @disable is a list of languages, only relevant if enable is true -- @keymaps list of user mappings for a given module if relevant @@ -219,8 +12,8 @@ local config = { highlight = { enable = false, disable = {}, - is_supported = function(ft) - return queries.get_query(ft, 'highlights') ~= nil + is_supported = function(lang) + return queries.get_query(lang, 'highlights') ~= nil end }, incremental_selection = { @@ -232,8 +25,8 @@ local config = { scope_incremental="grc", node_decremental="grm" }, - is_supported = function(ft) - return queries.get_query(ft, 'locals') + is_supported = function(lang) + return queries.get_query(lang, 'locals') end } }, @@ -242,56 +35,62 @@ local config = { local M = {} -local function enable_module(mod, bufnr, ft) +local function enable_module(mod, bufnr, lang) local bufnr = bufnr or api.nvim_get_current_buf() - local ft = ft or api.nvim_buf_get_option(bufnr, 'ft') - if not parserlist[ft] or not config.modules[mod] then + local lang = lang or parsers.ft_to_lang(api.nvim_buf_get_option(bufnr, 'ft')) + if not parsers.list[lang] or not config.modules[mod] then return end local loaded_mod = require(string.format("nvim-treesitter.%s", mod)) - loaded_mod.attach(bufnr, ft) + loaded_mod.attach(bufnr, lang) end -local function enable_mod_conf_autocmd(mod, ft) - if not config.modules[mod] or M.is_enabled(mod, ft) then return end +local function enable_mod_conf_autocmd(mod, lang) + if not config.modules[mod] or M.is_enabled(mod, lang) then return end + local ft = parsers.lang_to_ft(lang) local cmd = string.format("lua require'nvim-treesitter.%s'.attach()", mod) api.nvim_command(string.format("autocmd FileType %s %s", ft, cmd)) for i, parser in pairs(config.modules[mod].disable) do - if parser == ft then + if parser == lang then table.remove(config.modules[mod].disable, i) break end end end -local function enable_all(mod, ft) +local function enable_all(mod, lang) if not config.modules[mod] then return end for _, bufnr in pairs(api.nvim_list_bufs()) do - if not ft or api.nvim_buf_get_option(bufnr, 'ft') == ft then - enable_module(mod, bufnr, ft) + local ft = api.nvim_buf_get_option(bufnr, 'ft') + if not lang or ft == parsers.lang_to_ft(lang) then + enable_module(mod, bufnr, lang) end end - if ft then - if parsers.has_parser(ft) then - enable_mod_conf_autocmd(mod, ft) + if lang then + if parsers.has_parser(lang) then + enable_mod_conf_autocmd(mod, lang) end else - for _, ft in pairs(M.available_parsers()) do - if parsers.has_parser(ft) then - enable_mod_conf_autocmd(mod, ft) + for _, lang in pairs(parsers.available_parsers()) do + if parsers.has_parser(lang) then + enable_mod_conf_autocmd(mod, lang) end end end config.modules[mod].enable = true end -local function disable_module(mod, bufnr, ft) +local function disable_module(mod, bufnr, lang) local bufnr = bufnr or api.nvim_get_current_buf() - local ft = ft or api.nvim_buf_get_option(bufnr, 'ft') - if not parserlist[ft] or not config.modules[mod] then + local lang = lang or parsers.ft_to_lang(api.nvim_buf_get_option(bufnr, 'ft')) + if not lang then + return + end + + if not parsers.list[lang] or not config.modules[mod] then return end @@ -299,24 +98,26 @@ local function disable_module(mod, bufnr, ft) loaded_mod.detach(bufnr, ft) end -local function disable_mod_conf_autocmd(mod, ft) - if not config.modules[mod] or not M.is_enabled(mod, ft) then return end +local function disable_mod_conf_autocmd(mod, lang) + if not config.modules[mod] or not M.is_enabled(mod, lang) then return end + local ft = parsers.lang_to_ft(lang) api.nvim_command(string.format("autocmd! FileType %s", ft)) - table.insert(config.modules[mod].disable, ft) + table.insert(config.modules[mod].disable, lang) end -local function disable_all(mod, ft) +local function disable_all(mod, lang) for _, bufnr in pairs(api.nvim_list_bufs()) do - if not ft or api.nvim_buf_get_option(bufnr, 'ft') == ft then - disable_module(mod, bufnr, ft) + local ft = api.nvim_buf_get_option(bufnr, 'ft') + if not lang or ft == parsers.lang_to_ft(lang) then + disable_module(mod, bufnr, lang) end end - if ft then - disable_mod_conf_autocmd(mod, ft) + if lang then + disable_mod_conf_autocmd(mod, lang) else - for _, ft in pairs(M.available_parsers()) do - disable_mod_conf_autocmd(mod, ft) + for _, lang in pairs(parsers.available_parsers()) do + disable_mod_conf_autocmd(mod, lang) end config.modules[mod].enable = false end @@ -359,15 +160,15 @@ M.commands = { -- @param mod: module (string) -- @param ft: filetype (string) -function M.is_enabled(mod, ft) - if not M.get_parser_configs()[ft] or not parsers.has_parser(ft) then +function M.is_enabled(mod, lang) + if not parsers.list[lang] or not parsers.has_parser(lang) then return false end local module_config = config.modules[mod] if not module_config then return false end - if not module_config.enable or not module_config.is_supported(ft) then + if not module_config.enable or not module_config.is_supported(lang) then return false end @@ -403,14 +204,6 @@ function M.setup(user_data) end end -function M.get_parser_configs() - return parserlist -end - -function M.available_parsers() - return vim.tbl_keys(parserlist) -end - function M.available_modules() return vim.tbl_keys(config.modules) end diff --git a/lua/nvim-treesitter/health.lua b/lua/nvim-treesitter/health.lua index ca1e765c7..e73ce7fb5 100644 --- a/lua/nvim-treesitter/health.lua +++ b/lua/nvim-treesitter/health.lua @@ -4,7 +4,7 @@ local fn = vim.fn local queries = require'nvim-treesitter.query' local locals = require'nvim-treesitter.locals' local highlight = require'nvim-treesitter.highlight' -local configs = require'nvim-treesitter.configs' +local parsers = require'nvim-treesitter.parsers' local health_start = vim.fn["health#report_start"] local health_ok = vim.fn['health#report_ok'] @@ -62,7 +62,7 @@ function M.checkhealth() local missing_parsers = {} -- Parser installation checks - for _, parser_name in pairs(configs.available_parsers()) do + for _, parser_name in pairs(parsers.available_parsers()) do local installed = #api.nvim_get_runtime_file('parser/'..parser_name..'.so', false) -- Only print informations about installed parsers diff --git a/lua/nvim-treesitter/highlight.lua b/lua/nvim-treesitter/highlight.lua index ac46010bb..a6c3275f7 100644 --- a/lua/nvim-treesitter/highlight.lua +++ b/lua/nvim-treesitter/highlight.lua @@ -2,6 +2,7 @@ local api = vim.api local ts = vim.treesitter local queries = require'nvim-treesitter.query' +local parsers = require'nvim-treesitter.parsers' local M = { highlighters = {} @@ -49,14 +50,14 @@ hlmap["type.builtin"] = "Type" hlmap["structure"] = "Structure" hlmap["include"] = "Include" -function M.attach(bufnr, ft) - local buf = bufnr or api.nvim_get_current_buf() - local ft = ft or api.nvim_buf_get_option(buf, 'ft') +function M.attach(bufnr, lang) + local bufnr = bufnr or api.nvim_get_current_buf() + local lang = lang or parsers.ft_to_lang(api.nvim_buf_get_option(bufnr, 'ft')) - local query = queries.get_query(ft, "highlights") + local query = queries.get_query(lang, "highlights") if not query then return end - M.highlighters[buf] = ts.TSHighlighter.new(query, buf, ft) + M.highlighters[bufnr] = ts.TSHighlighter.new(query, bufnr, lang) end function M.detach(bufnr) diff --git a/lua/nvim-treesitter/info.lua b/lua/nvim-treesitter/info.lua index 18b1b611f..ba1812e42 100644 --- a/lua/nvim-treesitter/info.lua +++ b/lua/nvim-treesitter/info.lua @@ -1,15 +1,16 @@ local api = vim.api local configs = require'nvim-treesitter.configs' +local parsers = require'nvim-treesitter.parsers' local M = {} local function install_info() local max_len = 0 - for _, ft in pairs(configs.available_parsers()) do + for _, ft in pairs(parsers.available_parsers()) do if #ft > max_len then max_len = #ft end end - for _, ft in pairs(configs.available_parsers()) do + for _, ft in pairs(parsers.available_parsers()) do local is_installed = #api.nvim_get_runtime_file('parser/'..ft..'.so', false) > 0 api.nvim_out_write(ft..string.rep(' ', max_len - #ft + 1)) if is_installed then @@ -66,7 +67,7 @@ end local function module_info(mod) if mod and not configs.get_config()[mod] then return end - local ft_by_len = configs.available_parsers() + local ft_by_len = parsers.available_parsers() table.sort(ft_by_len, function(a, b) return #a > #b end) if mod then print_info_module(ft_by_len, mod) diff --git a/lua/nvim-treesitter/install.lua b/lua/nvim-treesitter/install.lua index 381afaa4a..c2eeaf763 100644 --- a/lua/nvim-treesitter/install.lua +++ b/lua/nvim-treesitter/install.lua @@ -2,7 +2,6 @@ local api = vim.api local fn = vim.fn local luv = vim.loop -local configs = require'nvim-treesitter.configs' local utils = require'nvim-treesitter.utils' local parsers = require'nvim-treesitter.parsers' @@ -103,7 +102,7 @@ local function install(...) if not string.match(yesno, '^y.*') then return end end - local parser_config = configs.get_parser_configs()[ft] + local parser_config = parsers.get_parser_configs()[ft] if not parser_config then return api.nvim_err_writeln('Parser not available for language '..ft) end @@ -122,15 +121,15 @@ end M.ensure_installed = function(languages) if type(languages) == 'string' then if languages == 'all' then - languages = configs.available_parsers() + languages = parsers.available_parsers() else languages = {languages} end end - for _, ft in ipairs(languages) do - if not parsers.has_parser(ft) then - install(ft) + for _, lang in ipairs(languages) do + if not parsers.has_parser(lang) then + install(lang) end end end diff --git a/lua/nvim-treesitter/parsers.lua b/lua/nvim-treesitter/parsers.lua index 3789b7afe..ae0fce277 100644 --- a/lua/nvim-treesitter/parsers.lua +++ b/lua/nvim-treesitter/parsers.lua @@ -1,17 +1,255 @@ local api = vim.api local ts = vim.treesitter -local M = {} +local list = {} + +list.javascript = { + install_info = { + url = "https://github.com/tree-sitter/tree-sitter-javascript", + files = { "src/parser.c", "src/scanner.c" }, + } +} + +list.c = { + install_info = { + url = "https://github.com/tree-sitter/tree-sitter-c", + files = { "src/parser.c" } + } +} + +list.cpp = { + install_info = { + url = "https://github.com/tree-sitter/tree-sitter-cpp", + files = { "src/parser.c", "src/scanner.cc" } + } +} + +list.rust = { + install_info = { + url = "https://github.com/tree-sitter/tree-sitter-rust", + files = { "src/parser.c", "src/scanner.c" }, + } +} + +list.lua = { + install_info = { + url = "https://github.com/nvim-treesitter/tree-sitter-lua", + files = { "src/parser.c", "src/scanner.cc" } + } +} + +list.python = { + install_info = { + url = "https://github.com/tree-sitter/tree-sitter-python", + files = { "src/parser.c", "src/scanner.cc" }, + } +} + +list.go = { + install_info = { + url = "https://github.com/tree-sitter/tree-sitter-go", + files = { "src/parser.c" }, + } +} + +list.ruby = { + install_info = { + url = "https://github.com/tree-sitter/tree-sitter-ruby", + files = { "src/parser.c", "src/scanner.cc" }, + } +} + +list.bash = { + install_info = { + url = "https://github.com/nvim-treesitter/tree-sitter-bash", + files = { "src/parser.c", "src/scanner.cc" }, + }, + filetype = 'sh' +} + +list.php = { + install_info = { + url = "https://github.com/tree-sitter/tree-sitter-php", + files = { "src/parser.c", "src/scanner.cc" }, + } +} + +list.java = { + install_info = { + url = "https://github.com/tree-sitter/tree-sitter-java", + files = { "src/parser.c" }, + } +} + +list.html = { + install_info = { + url = "https://github.com/tree-sitter/tree-sitter-html", + files = { "src/parser.c", "src/scanner.cc" }, + } +} + +list.julia = { + install_info = { + url = "https://github.com/tree-sitter/tree-sitter-julia", + files = { "src/parser.c", "src/scanner.c" }, + } +} + +list.json = { + install_info = { + url = "https://github.com/tree-sitter/tree-sitter-json", + files = { "src/parser.c" }, + } +} + +list.css = { + install_info = { + url = "https://github.com/tree-sitter/tree-sitter-css", + files = { "src/parser.c", "src/scanner.c" }, + } +} + +list.ocaml = { + install_info = { + url = "https://github.com/tree-sitter/tree-sitter-ocaml", + files = { "src/parser.c", "src/scanner.cc" }, + } +} + +list.swift = { + install_info = { + url = "https://github.com/tree-sitter/tree-sitter-swift", + files = { "src/parser.c" }, + } +} + +list.c_sharp = { + install_info = { + url = "https://github.com/tree-sitter/tree-sitter-c-sharp", + files = { "src/parser.c", "src/scanner.c" }, + }, + filetype = 'cs' +} + +list.typescript = { + install_info = { + url = "https://github.com/tree-sitter/tree-sitter-typescript", + files = { "src/parser.c", "src/scanner.c" }, + location = "tree-sitter-typescript/typescript" + } +} + +list.tsx = { + install_info = { + url = "https://github.com/tree-sitter/tree-sitter-typescript", + files = { "src/parser.c", "src/scanner.c" }, + location = "tree-sitter-tsx/tsx" + }, + filetype = 'typescriptreact' +} + +list.scala = { + install_info = { + url = "https://github.com/tree-sitter/tree-sitter-scala", + files = { "src/parser.c", "src/scanner.c" }, + } +} + +list.haskell = { + install_info = { + url = "https://github.com/tree-sitter/tree-sitter-haskell", + files = { "src/parser.c", "src/scanner.cc" }, + } +} + +list.markdown = { + install_info = { + url = "https://github.com/ikatyang/tree-sitter-markdown", + files = { "src/parser.c", "src/scanner.cc" }, + } +} + +list.toml = { + install_info = { + url = "https://github.com/ikatyang/tree-sitter-toml", + files = { "src/parser.c", "src/scanner.c" }, + } +} + +list.vue = { + install_info = { + url = "https://github.com/ikatyang/tree-sitter-vue", + files = { "src/parser.c", "src/scanner.cc" }, + } +} + +list.elm = { + install_info = { + url = "https://github.com//razzeee/tree-sitter-elm", + files = { "src/parser.c", "src/scanner.cc" }, + } +} + +list.yaml = { + install_info = { + url = "https://github.com/ikatyang/tree-sitter-yaml", + files = { "src/parser.c", "src/scanner.cc" }, + } +} + +list.nix = { + install_info = { + url = "https://github.com/cstrahan/tree-sitter-nix", + files = { "src/parser.c", "src/scanner.cc" }, + } +} + +list.regex = { + install_info = { + url = "https://github.com/tree-sitter/tree-sitter-regex", + files = { "src/parser.c" } + } +} + +local M = { + list = list +} + +local ft_to_parsername = {} +for name, obj in pairs(M.list) do + if obj.filetype then + ft_to_parsername[obj.filetype] = name + else + ft_to_parsername[name] = name + end +end + +function M.ft_to_lang(ft) + return ft_to_parsername[ft] +end + +function M.lang_to_ft(lang) + return M.list[lang].filetype or lang +end + +function M.available_parsers() + return vim.tbl_keys(M.list) +end + +function M.get_parser_configs() + return M.list +end function M.has_parser(lang) - local lang = lang or api.nvim_buf_get_option(0, 'filetype') + local buf = api.nvim_get_current_buf() + local lang = lang or M.ft_to_lang(api.nvim_buf_get_option(buf, 'ft')) if not lang or #lang == 0 then return false end return #api.nvim_get_runtime_file('parser/' .. lang .. '.*', false) > 0 end function M.get_parser(bufnr, lang) local buf = bufnr or api.nvim_get_current_buf() - local lang = lang or api.nvim_buf_get_option(buf, 'ft') + local lang = lang or M.ft_to_lang(api.nvim_buf_get_option(buf, 'ft')) if M.has_parser(lang) then if not M[buf] then diff --git a/lua/nvim-treesitter/query.lua b/lua/nvim-treesitter/query.lua index bd0f4c542..97a82fc46 100644 --- a/lua/nvim-treesitter/query.lua +++ b/lua/nvim-treesitter/query.lua @@ -21,15 +21,15 @@ M.base_language_map = { tsx = {'typescript', 'javascript'}, } -function M.get_query(ft, query_name) - local query_files = api.nvim_get_runtime_file(string.format('queries/%s/%s.scm', ft, query_name), true) +function M.get_query(lang, query_name) + local query_files = api.nvim_get_runtime_file(string.format('queries/%s/%s.scm', lang, query_name), true) local query_string = '' if #query_files > 0 then query_string = read_query_files(query_files)..query_string end - for _, base_lang in ipairs(M.base_language_map[ft] or {}) do + for _, base_lang in ipairs(M.base_language_map[lang] or {}) do local base_files = api.nvim_get_runtime_file(string.format('queries/%s/%s.scm', base_lang, query_name), true) if base_files and #base_files > 0 then query_string = read_query_files(base_files)..query_string @@ -37,7 +37,7 @@ function M.get_query(ft, query_name) end if #query_string > 0 then - return ts.parse_query(ft, query_string) + return ts.parse_query(lang, query_string) end end diff --git a/plugin/nvim-treesitter.vim b/plugin/nvim-treesitter.vim index 30ee3241d..4769680a3 100644 --- a/plugin/nvim-treesitter.vim +++ b/plugin/nvim-treesitter.vim @@ -11,7 +11,7 @@ let g:loaded_nvim_treesitter = 1 lua << EOF ts_installable_parsers = function() - return table.concat(require'nvim-treesitter.configs'.available_parsers(), '\n') + return table.concat(require'nvim-treesitter.parsers'.available_parsers(), '\n') end ts_available_modules = function() return table.concat(require'nvim-treesitter.configs'.available_modules(), '\n') From 0f836e060266c73e77871bd85b91f4159556035d Mon Sep 17 00:00:00 2001 From: TravonteD Date: Fri, 19 Jun 2020 09:59:27 -0400 Subject: [PATCH 30/87] refactor: use newly introduced consolidated syntax --- CONTRIBUTING.md | 2 + queries/css/highlights.scm | 125 ++++++++++++++---------- queries/html/highlights.scm | 11 ++- queries/ruby/highlights.scm | 188 ++++++++++++++++++++++-------------- queries/ruby/locals.scm | 6 +- 5 files changed, 197 insertions(+), 135 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 5a8c74ee6..568a47e3b 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -29,6 +29,7 @@ Here are some global advices : - If not, you should consider installing the [tree-sitter cli](https://github.com/tree-sitter/tree-sitter/tree/master/cli), you should then be able to open a local playground using `tree-sitter build-wasm && tree-sitter web-ui` within the parsers repo. + - An Example of somewhat complex highlight queries can be found in queries/ruby/highlights.scm (Maintained by @TravonteD) ### Highlights @@ -36,6 +37,7 @@ As languages differ quite a lot, here is a set of captures available to you when One important thing to note is that many of these capture groups are not supported by `neovim` for now, and will not have any effect on highlighting. We will work on improving highlighting in the near future though. + #### Misc `@comment` `@error` for error `(ERROR)` nodes. diff --git a/queries/css/highlights.scm b/queries/css/highlights.scm index 72009e765..1b876c4b1 100644 --- a/queries/css/highlights.scm +++ b/queries/css/highlights.scm @@ -1,72 +1,91 @@ -"@media" @keyword -"@import" @include -"@charset" @keyword -"@namespace" @keyword -"@supports" @keyword -"@keyframes" @keyword -(at_keyword) @keyword -(to) @keyword -(from) @keyword -(important) @keyword +[ + "@media" + "@import" + "@charset" + "@namespace" + "@supports" + "@keyframes" + (at_keyword) + (to) + (from) + (important) + ] @keyword (comment) @comment -(tag_name) @type -(nesting_selector) @type -(universal_selector) @type +[ + (tag_name) + (nesting_selector) + (universal_selector) + ] @type (function_name) @function -"~" @operator -">" @operator -"+" @operator -"-" @operator -"*" @operator -"/" @operator -"=" @operator -"^=" @operator -"|=" @operator -"~=" @operator -"$=" @operator -"*=" @operator +[ + "~" + ">" + "+" + "-" + "*" + "/" + "=" + "^=" + "|=" + "~=" + "$=" + "*=" + "and" + "or" + "not" + "only" + ] @operator -"and" @operator -"or" @operator -"not" @operator -"only" @operator (attribute_selector (plain_value) @string) (pseudo_element_selector (tag_name) @property) (pseudo_class_selector (class_name) @property) -(class_name) @property -(id_name) @property -(namespace_name) @property -(property_name) @property -(feature_name) @property -(attribute_name) @property +[ + (class_name) + (id_name) + (namespace_name) + (property_name) + (feature_name) + (attribute_name) + ] @property ((property_name) @type - (#match? @type "^--")) + (#match? @type "^--")) ((plain_value) @type - (#match? @type "^--")) + (#match? @type "^--")) -(string_value) @string -(color_value) @string -(identifier) @string +[ + (string_value) + (color_value) + (identifier) + (unit) + ] @string -(integer_value) @number -(float_value) @number -(unit) @string +[ + (integer_value) + (float_value) + ] @number -"#" @punctuation.delimiter -"," @punctuation.delimiter -"." @punctuation.delimiter -":" @punctuation.delimiter -"::" @punctuation.delimiter -";" @punctuation.delimiter -"{" @punctuation.bracket -")" @punctuation.bracket -"(" @punctuation.bracket -"}" @punctuation.bracket +[ + "#" + "," + "." + ":" + "::" + ";" + ] @punctuation.delimiter + +[ + "{" + ")" + "(" + "}" + ] @punctuation.bracket + +(ERROR) @error diff --git a/queries/html/highlights.scm b/queries/html/highlights.scm index 582e1e46f..1e83ecf4c 100644 --- a/queries/html/highlights.scm +++ b/queries/html/highlights.scm @@ -8,8 +8,9 @@ "=" @operator -"<" @punctuation.bracket -">" @punctuation.bracket -"" @punctuation.bracket - +[ + "<" + ">" + "" + ] @punctuation.bracket diff --git a/queries/ruby/highlights.scm b/queries/ruby/highlights.scm index 9e5852cdf..21afbe809 100644 --- a/queries/ruby/highlights.scm +++ b/queries/ruby/highlights.scm @@ -1,32 +1,41 @@ ; Keywords -"alias" @keyword -"and" @keyword -"begin" @keyword -"break" @keyword -"case" @conditional -"class" @keyword -"def" @keyword -"do" @keyword -"else" @conditional -"elsif" @conditional -"end" @keyword -"ensure" @keyword -"for" @repeat -"if" @conditional -"in" @keyword -"module" @keyword -"next" @keyword -"or" @keyword -"rescue" @keyword -"retry" @keyword -"return" @keyword -"then" @keyword -"unless" @conditional -"until" @repeat -"when" @conditional -"while" @repeat -"yield" @keyword +[ + "alias" + "and" + "begin" + "break" + "class" + "def" + "do" + "end" + "ensure" + "in" + "module" + "next" + "or" + "rescue" + "retry" + "return" + "then" + "yield" + ] @keyword + +[ + "case" + "else" + "elsif" + "if" + "unless" + "when" + ] @conditional + +[ + "for" + "until" + "while" + ] @repeat + ((identifier) @keyword (#match? @keyword "^(private|protected|public)$")) @@ -39,32 +48,45 @@ "defined?" @function (call - receiver: (constant) @constant) + [ + receiver: (constant) @constant + method: [ + (identifier) + (constant) + ] @function + ]) + (method_call - receiver: (constant) @constant) -(call - method: (identifier) @function) -(method_call - method: (identifier) @function) -(call - method: (constant) @function) -(method_call - method: (constant) @function) + [ + receiver: (constant) @constant + method: [ + (identifier) + (constant) + ] @function + ]) ; Function definitions (alias (identifier) @function) (setter (identifier) @function) -(method name: (identifier) @function) -(method name: (constant) @constant) + +(method name: [ + (identifier) @function + (constant) @constant + ]) + +(singleton_method name: [ + (identifier) @function + (constant) @constant + ]) + (class name: (constant) @constant) -(singleton_method name: (identifier) @function) -(singleton_method name: (constant) @constant) ; Identifiers - -(class_variable) @label -(instance_variable) @label +[ + (class_variable) + (instance_variable) + ] @label ((identifier) @constant.builtin (#match? @constant.builtin "^__(FILE|LINE|ENCODING)__$")) @@ -74,8 +96,10 @@ (constant) @constant -(self) @constant.builtin -(super) @constant.builtin +[ + (self) + (super) + ] @constant.builtin (method_parameters (identifier) @parameter) (lambda_parameters (identifier) @parameter) @@ -93,22 +117,30 @@ ; Literals -(string) @string -(bare_string) @string -(bare_symbol) @constant -(subshell) @string -(heredoc_beginning) @constant -(heredoc_body) @string -(heredoc_end) @constant -(symbol) @constant +[ + (string) + (bare_string) + (subshell) + (heredoc_body) + ] @string + +[ + (bare_symbol) + (heredoc_beginning) + (heredoc_end) + (symbol) + ] @constant + (regex) @string.regex (escape_sequence) @string.escape (integer) @number (float) @float -(nil) @boolean -(true) @boolean -(false) @boolean +[ + (nil) + (true) + (false) + ] @boolean (interpolation "#{" @punctuation.bracket @@ -118,25 +150,31 @@ ; Operators -"=" @operator -"=>" @operator -"->" @operator -"+" @operator -"-" @operator -"*" @operator -"/" @operator +[ + "=" + "=>" + "->" + "+" + "-" + "*" + "/" + ] @operator -"," @punctuation.delimiter -";" @punctuation.delimiter -"." @punctuation.delimiter +[ + "," + ";" + "." + ] @punctuation.delimiter -"(" @punctuation.bracket -")" @punctuation.bracket -"[" @punctuation.bracket -"]" @punctuation.bracket -"{" @punctuation.bracket -"}" @punctuation.bracket -"%w(" @punctuation.bracket -"%i(" @punctuation.bracket +[ + "(" + ")" + "[" + "]" + "{" + "}" + "%w(" + "%i(" + ] @punctuation.bracket (ERROR) @error diff --git a/queries/ruby/locals.scm b/queries/ruby/locals.scm index f50c2cc99..fe5122d2d 100644 --- a/queries/ruby/locals.scm +++ b/queries/ruby/locals.scm @@ -24,8 +24,10 @@ ((method) @scope (set! scope-inherits false)) -(block) @scope -(do_block) @scope +[ + (block) + (do_block) + ] @scope (method_parameters (identifier) @definition.function) (lambda_parameters (identifier) @definition.function) From 0b4d4b41408ff027888232f951000ca9d2e01fe4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=81uczy=C5=84ski?= Date: Tue, 2 Jun 2020 15:59:13 +0200 Subject: [PATCH 31/87] Updated python highlights * allow for digits in constant names * removed redundant/conflicting rules * added missing hlmap * fixed escape_sequence * more explicit @constructor assignment * added rules for function decoration identifiers --- lua/nvim-treesitter/highlight.lua | 1 + queries/python/highlights.scm | 76 +++++++++++++------------------ 2 files changed, 32 insertions(+), 45 deletions(-) diff --git a/lua/nvim-treesitter/highlight.lua b/lua/nvim-treesitter/highlight.lua index ac46010bb..0086fcb0d 100644 --- a/lua/nvim-treesitter/highlight.lua +++ b/lua/nvim-treesitter/highlight.lua @@ -13,6 +13,7 @@ local hlmap = vim.treesitter.TSHighlighter.hl_map hlmap.error = "Error" hlmap["punctuation.delimiter"] = "Delimiter" hlmap["punctuation.bracket"] = "Delimiter" +hlmap["punctuation.special"] = "Delimiter" -- Constants hlmap["constant"] = "Constant" diff --git a/queries/python/highlights.scm b/queries/python/highlights.scm index 0b6e30033..cc11a5bd2 100644 --- a/queries/python/highlights.scm +++ b/queries/python/highlights.scm @@ -3,31 +3,32 @@ ; Identifier naming conventions - -((import_from_statement - name: (dotted_name - (identifier)) @type) - (match? @type "^[A-Z]")) - +(identifier) @Normal ((identifier) @type (match? @type "^[A-Z]")) - ((identifier) @constant - (match? @constant "^[A-Z][A-Z_]*$")) + (match? @constant "^[A-Z][A-Z_0-9]*$")) ; Function calls (decorator) @function +((decorator (dotted_name (identifier) @function)) + (match? @function "^([A-Z])@!.*$")) + +(call + function: (identifier) @function) (call function: (attribute attribute: (identifier) @method)) -(call - function: (identifier) @function) +((call + function: (identifier) @constructor) + (match? @constructor "^[A-Z]")) ((call - (identifier) @constructor) + function: (attribute + attribute: (identifier) @constructor)) (match? @constructor "^[A-Z]")) ;; Builtin functions @@ -43,10 +44,9 @@ (function_definition name: (identifier) @function) -(identifier) @variable -(attribute attribute: (identifier) @property) (type (identifier) @type) -((call + +((call function: (identifier) @isinstance arguments: (argument_list (*) @@ -55,7 +55,7 @@ ; Normal parameters (parameters - (identifier) @parameter) + (identifier) @parameter) ; Default parameters (keyword_argument name: (identifier) @parameter) @@ -65,10 +65,10 @@ ; Variadic parameters *args, **kwargs (parameters (list_splat ; *args - (identifier) @parameter)) + (identifier) @parameter)) (parameters (dictionary_splat ; **kwargs - (identifier) @parameter)) + (identifier) @parameter)) ; Literals @@ -84,11 +84,7 @@ (comment) @comment (string) @string -(escape_sequence) @escape - -(interpolation - "{" @punctuation.special - "}" @punctuation.special) @embedded +(escape_sequence) @string.escape ; Tokens @@ -164,46 +160,36 @@ ")" @punctuation.bracket "[" @punctuation.bracket "]" @punctuation.bracket +"{" @punctuation.bracket +"}" @punctuation.bracket + +(interpolation + "{" @punctuation.special + "}" @punctuation.special) @embedded "," @punctuation.delimiter "." @punctuation.delimiter ":" @punctuation.delimiter +; Class definitions + (class_definition name: (identifier) @type) (class_definition - superclasses: (argument_list + superclasses: (argument_list (identifier) @type)) -(attribute +((attribute attribute: (identifier) @field) - -((attribute - attribute: (identifier) @constant) - (match? @constant "^[A-Z][A-Z_]*$")) - -((attribute - attribute: (identifier) @type) - (match? @type "^[A-Z][a-z_]+")) - -((attribute - object: (identifier) @type) - (match? @type "^[A-Z][a-z_]+")) - -(class_definition - body: (block - (expression_statement - (assignment - left: (expression_list - (identifier) @field))))) + (match? @field "^([A-Z])@!.*$")) ((class_definition body: (block (expression_statement (assignment left: (expression_list - (identifier) @constant))))) - (match? @constant "^[A-Z][A-Z_]*$")) + (identifier) @field))))) + (match? @field "^([A-Z])@!.*$")) ;; Error (ERROR) @error From a85cf1a47c0acc6b22ed99b3228c827f5fb46927 Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Sun, 21 Jun 2020 17:00:17 +0200 Subject: [PATCH 32/87] Add highlighting of dunder identifiers (e.g. __all__, __add__) to Python highlights --- queries/python/highlights.scm | 3 +++ 1 file changed, 3 insertions(+) diff --git a/queries/python/highlights.scm b/queries/python/highlights.scm index cc11a5bd2..c56897804 100644 --- a/queries/python/highlights.scm +++ b/queries/python/highlights.scm @@ -9,6 +9,9 @@ ((identifier) @constant (match? @constant "^[A-Z][A-Z_0-9]*$")) +((identifier) @constant.builtin + (match? @constant.builtin "^__[a-zA-Z0-9_]*__$")) + ; Function calls (decorator) @function From ec903ac99d07e0960d0acc71e70ba9809742bfbf Mon Sep 17 00:00:00 2001 From: Thomas Vigouroux Date: Sun, 21 Jun 2020 19:50:24 +0200 Subject: [PATCH 33/87] fix(statusline): don't call if no parser --- lua/nvim-treesitter.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/lua/nvim-treesitter.lua b/lua/nvim-treesitter.lua index dde4ac0fd..9b3ddce89 100644 --- a/lua/nvim-treesitter.lua +++ b/lua/nvim-treesitter.lua @@ -26,6 +26,7 @@ function M.setup() end function M.statusline(indicator_size) + if not parsers.has_parser() then return end local indicator_size = indicator_size or 100 local current_node = ts_utils.get_node_at_cursor() From 171c7e4bcfd43ca8aef7f841013f0de47c76defd Mon Sep 17 00:00:00 2001 From: Santos Gallegos Date: Sun, 21 Jun 2020 13:47:56 -0500 Subject: [PATCH 34/87] Fix bash parser url Not sure if this was on purpose or if it was a mistake --- lua/nvim-treesitter/parsers.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/nvim-treesitter/parsers.lua b/lua/nvim-treesitter/parsers.lua index ae0fce277..9097d335d 100644 --- a/lua/nvim-treesitter/parsers.lua +++ b/lua/nvim-treesitter/parsers.lua @@ -61,7 +61,7 @@ list.ruby = { list.bash = { install_info = { - url = "https://github.com/nvim-treesitter/tree-sitter-bash", + url = "https://github.com/tree-sitter/tree-sitter-bash", files = { "src/parser.c", "src/scanner.cc" }, }, filetype = 'sh' From 895216014b83defa41f15118c1347b789d70e6fc Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Sun, 21 Jun 2020 17:39:58 +0200 Subject: [PATCH 35/87] Add disclaimer to README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 787a2a200..787a9adc9 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,9 @@ # nvim-treesitter Treesitter configurations and abstraction layer for Neovim. +**Warning: Treesitter and Treesitter highlighting are an experimental feature of nightly versions of Neovim. +Please consider the experience with this plug-in as experimental until Neovim 0.5 is released!** + # Quickstart ## Requirements From 3000b878fe7dc99f0bab9192a86e227000814895 Mon Sep 17 00:00:00 2001 From: Thomas Vigouroux Date: Sun, 21 Jun 2020 23:06:13 +0200 Subject: [PATCH 36/87] fix: separate queries with newlines --- lua/nvim-treesitter/query.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/nvim-treesitter/query.lua b/lua/nvim-treesitter/query.lua index 97a82fc46..42e835824 100644 --- a/lua/nvim-treesitter/query.lua +++ b/lua/nvim-treesitter/query.lua @@ -26,13 +26,13 @@ function M.get_query(lang, query_name) local query_string = '' if #query_files > 0 then - query_string = read_query_files(query_files)..query_string + query_string = read_query_files(query_files) .. "\n" .. query_string end for _, base_lang in ipairs(M.base_language_map[lang] or {}) do local base_files = api.nvim_get_runtime_file(string.format('queries/%s/%s.scm', base_lang, query_name), true) if base_files and #base_files > 0 then - query_string = read_query_files(base_files)..query_string + query_string = read_query_files(base_files) .. "\n" .. query_string end end From def8c20dd710d7ee6337af21012535cb860607ae Mon Sep 17 00:00:00 2001 From: kiyan42 Date: Mon, 22 Jun 2020 11:56:55 +0200 Subject: [PATCH 37/87] rename and finish ft->lang migration --- lua/nvim-treesitter/configs.lua | 4 ++-- lua/nvim-treesitter/info.lua | 32 ++++++++++++++++---------------- lua/nvim-treesitter/install.lua | 28 ++++++++++++++-------------- lua/nvim-treesitter/locals.lua | 8 ++++---- 4 files changed, 36 insertions(+), 36 deletions(-) diff --git a/lua/nvim-treesitter/configs.lua b/lua/nvim-treesitter/configs.lua index ecad20136..f542e2609 100644 --- a/lua/nvim-treesitter/configs.lua +++ b/lua/nvim-treesitter/configs.lua @@ -95,7 +95,7 @@ local function disable_module(mod, bufnr, lang) end local loaded_mod = require(string.format("nvim-treesitter.%s", mod)) - loaded_mod.detach(bufnr, ft) + loaded_mod.detach(bufnr) end local function disable_mod_conf_autocmd(mod, lang) @@ -173,7 +173,7 @@ function M.is_enabled(mod, lang) end for _, parser in pairs(module_config.disable) do - if ft == parser then return false end + if lang == parser then return false end end return true diff --git a/lua/nvim-treesitter/info.lua b/lua/nvim-treesitter/info.lua index ba1812e42..c768595c4 100644 --- a/lua/nvim-treesitter/info.lua +++ b/lua/nvim-treesitter/info.lua @@ -21,14 +21,14 @@ local function install_info() end end -local function print_info_module(sorted_filetypes, mod) - local max_str_len = #sorted_filetypes[1] +local function print_info_module(sorted_languages, mod) + local max_str_len = #sorted_languages[1] local header = string.format('%s%s', string.rep(' ', max_str_len + 2), mod) api.nvim_out_write(header..'\n') - for _, ft in pairs(sorted_filetypes) do - local padding = string.rep(' ', max_str_len - #ft + #mod / 2 + 1) - api.nvim_out_write(ft..":"..padding) - if configs.is_enabled(mod, ft) then + for _, lang in pairs(sorted_languages) do + local padding = string.rep(' ', max_str_len - #lang + #mod / 2 + 1) + api.nvim_out_write(lang..":"..padding) + if configs.is_enabled(mod, lang) then api.nvim_out_write('✓') else api.nvim_out_write('✗') @@ -37,23 +37,23 @@ local function print_info_module(sorted_filetypes, mod) end end -local function print_info_modules(sorted_filetypes) - local max_str_len = #sorted_filetypes[1] +local function print_info_modules(sorted_languages) + local max_str_len = #sorted_languages[1] local header = string.rep(' ', max_str_len + 2) for _, mod in pairs(configs.available_modules()) do header = string.format('%s%s ', header, mod) end api.nvim_out_write(header..'\n') - for _, ft in pairs(sorted_filetypes) do - local padding = string.rep(' ', max_str_len - #ft) - api.nvim_out_write(ft..":"..padding) + for _, lang in pairs(sorted_languages) do + local padding = string.rep(' ', max_str_len - #lang) + api.nvim_out_write(lang..":"..padding) for _, mod in pairs(configs.available_modules()) do local pad_len = #mod / 2 + 1 api.nvim_out_write(string.rep(' ', pad_len)) - if configs.is_enabled(mod, ft) then + if configs.is_enabled(mod, lang) then api.nvim_out_write('✓') else api.nvim_out_write('✗') @@ -67,12 +67,12 @@ end local function module_info(mod) if mod and not configs.get_config()[mod] then return end - local ft_by_len = parsers.available_parsers() - table.sort(ft_by_len, function(a, b) return #a > #b end) + local parserlist = parsers.available_parsers() + table.sort(parserlist, function(a, b) return #a > #b end) if mod then - print_info_module(ft_by_len, mod) + print_info_module(parserlist, mod) else - print_info_modules(ft_by_len) + print_info_modules(parserlist) end end diff --git a/lua/nvim-treesitter/install.lua b/lua/nvim-treesitter/install.lua index c2eeaf763..2d33dafda 100644 --- a/lua/nvim-treesitter/install.lua +++ b/lua/nvim-treesitter/install.lua @@ -7,8 +7,8 @@ local parsers = require'nvim-treesitter.parsers' local M = {} -local function iter_cmd(cmd_list, i, ft) - if i == #cmd_list + 1 then return print('Treesitter parser for '..ft..' has been installed') end +local function iter_cmd(cmd_list, i, lang) + if i == #cmd_list + 1 then return print('Treesitter parser for '..lang..' has been installed') end local attr = cmd_list[i] if attr.info then print(attr.info) end @@ -18,16 +18,16 @@ local function iter_cmd(cmd_list, i, ft) handle = luv.spawn(attr.cmd, attr.opts, vim.schedule_wrap(function(code) handle:close() if code ~= 0 then return api.nvim_err_writeln(attr.err) end - iter_cmd(cmd_list, i + 1, ft) + iter_cmd(cmd_list, i + 1, lang) end)) end -local function run_install(cache_folder, package_path, ft, repo) - local project_name = 'tree-sitter-'..ft +local function run_install(cache_folder, package_path, lang, repo) + local project_name = 'tree-sitter-'..lang local project_repo = cache_folder..'/'..project_name -- compile_location only needed for typescript installs. local compile_location = cache_folder..'/'..(repo.location or project_name) - local parser_lib_name = package_path.."/parser/"..ft..".so" + local parser_lib_name = package_path.."/parser/"..lang..".so" local command_list = { { cmd = 'rm', @@ -76,7 +76,7 @@ local function run_install(cache_folder, package_path, ft, repo) } } - iter_cmd(command_list, 1, ft) + iter_cmd(command_list, 1, lang) end -- TODO(kyazdani): this should work on windows too @@ -95,16 +95,16 @@ local function install(...) local cache_folder, err = utils.get_cache_dir() if err then return api.nvim_err_writeln(err) end - for _, ft in ipairs({ ... }) do - if #api.nvim_get_runtime_file('parser/'..ft..'.so', false) > 0 then - local yesno = fn.input(ft .. ' parser already available: would you like to reinstall ? y/n: ') + for _, lang in ipairs({ ... }) do + if #api.nvim_get_runtime_file('parser/'..lang..'.so', false) > 0 then + local yesno = fn.input(lang .. ' parser already available: would you like to reinstall ? y/n: ') print('\n ') -- mandatory to avoid messing up command line if not string.match(yesno, '^y.*') then return end end - local parser_config = parsers.get_parser_configs()[ft] + local parser_config = parsers.get_parser_configs()[lang] if not parser_config then - return api.nvim_err_writeln('Parser not available for language '..ft) + return api.nvim_err_writeln('Parser not available for language '..lang) end local install_info = parser_config.install_info @@ -113,7 +113,7 @@ local function install(...) files={ install_info.files, 'table' } } - run_install(cache_folder, package_path, ft, install_info) + run_install(cache_folder, package_path, lang, install_info) end end @@ -142,7 +142,7 @@ M.commands = { "-nargs=+", "-complete=custom,v:lua.ts_installable_parsers" }, - description = '`:TSInstall {ft}` installs a parser under nvim-treesitter/parser/{name}.so' + description = '`:TSInstall {lang}` installs a parser under nvim-treesitter/parser/{lang}.so' } } diff --git a/lua/nvim-treesitter/locals.lua b/lua/nvim-treesitter/locals.lua index cbea89108..7ace347a3 100644 --- a/lua/nvim-treesitter/locals.lua +++ b/lua/nvim-treesitter/locals.lua @@ -12,13 +12,13 @@ local M = { } function M.collect_locals(bufnr) - local ft = api.nvim_buf_get_option(bufnr, "ft") - if not ft then return end + local lang = parsers.ft_to_lang(api.nvim_buf_get_option(bufnr, "ft")) + if not lang then return end - local query = queries.get_query(ft, 'locals') + local query = queries.get_query(lang, 'locals') if not query then return end - local parser = parsers.get_parser(bufnr, ft) + local parser = parsers.get_parser(bufnr, lang) if not parser then return end local root = parser:parse():root() From 09c2e6e0a73721128b723b9668a8316aebb3a590 Mon Sep 17 00:00:00 2001 From: TravonteD Date: Mon, 22 Jun 2020 11:18:34 -0400 Subject: [PATCH 38/87] Fix the ":" in symbols when used in pairs This fixes the colon not being highlighted when a symbol is represented in a pair like so: symbol: true --- queries/ruby/highlights.scm | 1 + 1 file changed, 1 insertion(+) diff --git a/queries/ruby/highlights.scm b/queries/ruby/highlights.scm index 21afbe809..d7d880043 100644 --- a/queries/ruby/highlights.scm +++ b/queries/ruby/highlights.scm @@ -131,6 +131,7 @@ (symbol) ] @constant +(pair key: (symbol) ":" @constant) (regex) @string.regex (escape_sequence) @string.escape (integer) @number From 35dfd7e1f471c5f470a6c51577e11b7c3b72bb60 Mon Sep 17 00:00:00 2001 From: Thomas Vigouroux <39092278+vigoux@users.noreply.github.com> Date: Tue, 23 Jun 2020 14:30:01 +0200 Subject: [PATCH 39/87] Update CONTRIBUTING.md This makes it more readable --- CONTRIBUTING.md | 108 ++++++++++++++++++++++++++---------------------- 1 file changed, 59 insertions(+), 49 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 568a47e3b..af48cdc62 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -39,64 +39,73 @@ effect on highlighting. We will work on improving highlighting in the near futur #### Misc -`@comment` -`@error` for error `(ERROR)` nodes. -`@punctuation.delimiter` for `;` `.` `,` -`@punctuation.bracket` for `()` or `{}` +``` +@comment +@error for error (ERROR` nodes. +@punctuation.delimiter for `;` `.` `,` +@punctuation.bracket for `()` or `{}` +``` Some captures are related to language injection (like markdown code blocks). As this is not supported by neovim yet, these are optional and will not have any effect for now. -`@embedded` -`@injection` - `language` - `content` - +``` +@embedded +@injection + language + content +``` #### Constants -`@constant` - `builtin` - `macro` -`@string` - `regex` - `escape` -`@character` -`@number` -`@boolean` -`@float` +``` +@constant + builtin + macro +@string + regex + escape +@character +@number +@boolean +@float +``` #### Functions -`@function` - `builtin` - `macro` -`@parameter` +``` +@function + builtin + macro +@parameter -`@method` -`@field` or `@property` +@method +@field or @property -`@constructor` +@constructor +``` #### Keywords -`@conditional` -`@repeat` -`@label` for C/Lua-like labels -`@operator` -`@keyword` -`@exception` -`@include` keywords for including modules (e.g. import/from in Python) - -`@type` - `builtin` -`@structure` +``` +@conditional +@repeat +@label for C/Lua-like labels +@operator +@keyword +@exception +@include keywords for including modules (e.g. import/from in Python) +@type + builtin +@structure +``` ### Locals - -`@definition` for various definitions - `function` - `method` - `var` - `macro` - `type` - `field` - `doc` for documentation adjecent to a definition. E.g. +``` +@definition for various definitions + function + method + var + macro + type + field + doc for documentation adjecent to a definition. E.g. +``` ```scheme (comment)* @definition.doc @@ -104,7 +113,8 @@ are optional and will not have any effect for now. name: (field_identifier) @definition.method) ``` -`@scope` - -`@reference` +``` +@scope +@reference +``` From 51a52b85cf9fdc4d67619e0d71c57c1e6bb0f988 Mon Sep 17 00:00:00 2001 From: TravonteD Date: Mon, 22 Jun 2020 17:39:54 -0400 Subject: [PATCH 40/87] add highlight queries for bash --- README.md | 2 +- queries/bash/highlights.scm | 110 ++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 queries/bash/highlights.scm diff --git a/README.md b/README.md index 787a9adc9..9c8a97d33 100644 --- a/README.md +++ b/README.md @@ -168,7 +168,7 @@ List of currently supported languages: - [x] css (maintained by @TravonteD) - [ ] julia - [ ] php -- [ ] bash +- [x] bash (maintained by @TravonteD) - [ ] scala - [ ] haskell - [ ] toml diff --git a/queries/bash/highlights.scm b/queries/bash/highlights.scm new file mode 100644 index 000000000..c0ec8d698 --- /dev/null +++ b/queries/bash/highlights.scm @@ -0,0 +1,110 @@ +[ + "(" + ")" + "{" + "}" + "[" + "]" + ] @punctuation.bracket + +[ + ";" + ";;" + (heredoc_start) + ] @punctuation.delimiter + +[ + ">" + "<" + "&" + "&&" + "|" + "||" + "=" + "==" + "!=" + ] @operator + +[ + (string) + (raw_string) + (heredoc_body) +] @string + +[ + "if" + "then" + "else" + "elif" + "fi" + "case" + "in" + "esac" + ] @conditional + +[ + "for" + "do" + "done" + "while" + ] @repeat + +[ + "declare" + "export" + "local" + "readonly" + "unset" + ] @keyword + +[ + (special_variable_name) + ("$" (special_variable_name)) + ] @constant + +((word) @constant + (#match? @constant "SIG(INT|TERM|QUIT|TIN|TOU|STP|HUP)")) + +((word) @boolean + (#match? @boolean "true|false")) + +((word) @number + (#match? @number "^\d*$")) + +(comment) @comment +(test_operator) @string. + +(command_substitution + [ "$(" ")" ] @punctuation.bracket) + + +(function_definition + name: (word) @function) + +(command_name (word)) @function + +(command + argument: [ + (word) @parameter + ((word) @number + (#match? @number "^\d*$")) + (concatenation (word) @parameter) + ]) + +(file_redirect + descriptor: (file_descriptor) @operator + destination: (word) @parameter) + + +("$" (variable_name)) @identifier + +(expansion + [ "${" "}" ] @punctuation.bracket) + +(variable_name) @identifier + +(case_item + value: (word) @parameter) + +(concatenation (word) @parameter) + From dfead6cacab1a9f94396169166c9de8f8d4efdb7 Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Sun, 21 Jun 2020 16:32:25 +0200 Subject: [PATCH 41/87] Adapt Python highlights to new query syntax --- queries/python/highlights.scm | 139 ++++++++++++++++------------------ queries/python/locals.scm | 8 +- 2 files changed, 66 insertions(+), 81 deletions(-) diff --git a/queries/python/highlights.scm b/queries/python/highlights.scm index c56897804..739ab81cf 100644 --- a/queries/python/highlights.scm +++ b/queries/python/highlights.scm @@ -77,8 +77,7 @@ ; Literals (none) @constant.builtin -(true) @boolean -(false) @boolean +[(true) (false)] @boolean ((identifier) @constant.builtin (match? @constant.builtin "self")) @@ -91,88 +90,78 @@ ; Tokens -"-" @operator -"->" @operator -"-=" @operator -"!=" @operator -"*" @operator -"**" @operator -"**=" @operator -"*=" @operator -"/" @operator -"//" @operator -"//=" @operator -"/=" @operator -"&" @operator -"%" @operator -"%=" @operator -"^" @operator -"+" @operator -"+=" @operator -"<" @operator -"<<" @operator -"<=" @operator -"<>" @operator -"=" @operator -"==" @operator -">" @operator -">=" @operator -">>" @operator -"|" @operator -"~" @operator -"and" @operator -"in" @operator -"is" @operator -"not" @operator -"or" @operator +[ + "-" + "-=" + "!=" + "*" + "**" + "**=" + "*=" + "/" + "//" + "//=" + "/=" + "&" + "%" + "%=" + "^" + "+" + "+=" + "<" + "<<" + "<=" + "<>" + "=" + "==" + ">" + ">=" + ">>" + "|" + "~" + "and" + "in" + "is" + "not" + "or" +] @operator ; Keywords -"as" @include -"assert" @keyword -"async" @keyword -"await" @keyword -"break" @repeat -"class" @keyword -"continue" @repeat -"def" @keyword -"del" @keyword -"elif" @conditional -"else" @conditional -"except" @keyword -"exec" @keyword -"finally" @keyword -"for" @repeat -"from" @include -"global" @keyword -"if" @conditional -"import" @include -"lambda" @keyword -"nonlocal" @keyword -"pass" @keyword -"print" @keyword -"raise" @keyword -"return" @keyword -"try" @keyword -"while" @repeat -"with" @keyword -"yield" @keyword +[ + "assert" + "await" + "class" + "def" + "del" + "except" + "exec" + "finally" + "global" + "lambda" + "nonlocal" + "pass" + "print" + "raise" + "return" + "try" + "with" + "yield" +] @keyword -; Additions for nvim-treesitter -"(" @punctuation.bracket -")" @punctuation.bracket -"[" @punctuation.bracket -"]" @punctuation.bracket -"{" @punctuation.bracket -"}" @punctuation.bracket +[ "as" "from" "import"] @include + +[ "if" "elif" "else" ] @conditional + +[ "for" "while" "break" "continue" ] @repeat + +[ "(" ")" "[" "]" "{" "}"] @punctuation.bracket (interpolation "{" @punctuation.special "}" @punctuation.special) @embedded -"," @punctuation.delimiter -"." @punctuation.delimiter -":" @punctuation.delimiter +[ "," "." ":" ] @punctuation.delimiter ; Class definitions diff --git a/queries/python/locals.scm b/queries/python/locals.scm index 735271663..cdf4d4117 100644 --- a/queries/python/locals.scm +++ b/queries/python/locals.scm @@ -30,12 +30,8 @@ ; Function defines function and scope (function_definition - name: (identifier) @definition.function) @scope - -;; Should be extended to when syntax supported -;(function_definition - ;name: (identifier) @definition.function - ;body: (block (expression_statement (string) @definition.function.doc)?)) @scope + name: (identifier) @definition.function + body: (block (expression_statement (string) @definition.doc)?)) @scope (class_definition From d04a833e2090aab9899071021c0a959886029247 Mon Sep 17 00:00:00 2001 From: Steven Sojka Date: Wed, 24 Jun 2020 07:46:40 -0500 Subject: [PATCH 42/87] fix(locals): compute locals after later tick --- lua/nvim-treesitter/locals.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/nvim-treesitter/locals.lua b/lua/nvim-treesitter/locals.lua index 7ace347a3..8c7d400eb 100644 --- a/lua/nvim-treesitter/locals.lua +++ b/lua/nvim-treesitter/locals.lua @@ -40,7 +40,7 @@ end function M.get_locals(bufnr) local bufnr = bufnr or api.nvim_get_current_buf() local cached_local = M.locals[bufnr] - if not cached_local or api.nvim_buf_get_changedtick(bufnr) < cached_local.tick then + if not cached_local or api.nvim_buf_get_changedtick(bufnr) > cached_local.tick then update_cached_locals(bufnr,api.nvim_buf_get_changedtick(bufnr)) end From 5e91955b6afd450899e6fd4a10ffaa6601be53b0 Mon Sep 17 00:00:00 2001 From: kiyan42 Date: Thu, 25 Jun 2020 12:02:06 +0200 Subject: [PATCH 43/87] add command to install all parsers --- lua/nvim-treesitter/install.lua | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lua/nvim-treesitter/install.lua b/lua/nvim-treesitter/install.lua index 2d33dafda..2a9257f73 100644 --- a/lua/nvim-treesitter/install.lua +++ b/lua/nvim-treesitter/install.lua @@ -143,6 +143,13 @@ M.commands = { "-complete=custom,v:lua.ts_installable_parsers" }, description = '`:TSInstall {lang}` installs a parser under nvim-treesitter/parser/{lang}.so' + }, + TSInstallAll = { + run = function() + for _, lang in pairs(parsers.available_parsers()) do + install(lang) + end + end } } From 3e4ea3d890d3914d1fe56b21363308470c700678 Mon Sep 17 00:00:00 2001 From: kiyan42 Date: Thu, 25 Jun 2020 12:37:01 +0200 Subject: [PATCH 44/87] install can take 'all' as parameter --- lua/nvim-treesitter/install.lua | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/lua/nvim-treesitter/install.lua b/lua/nvim-treesitter/install.lua index 2a9257f73..24f6fbd63 100644 --- a/lua/nvim-treesitter/install.lua +++ b/lua/nvim-treesitter/install.lua @@ -95,11 +95,20 @@ local function install(...) local cache_folder, err = utils.get_cache_dir() if err then return api.nvim_err_writeln(err) end - for _, lang in ipairs({ ... }) do - if #api.nvim_get_runtime_file('parser/'..lang..'.so', false) > 0 then - local yesno = fn.input(lang .. ' parser already available: would you like to reinstall ? y/n: ') - print('\n ') -- mandatory to avoid messing up command line - if not string.match(yesno, '^y.*') then return end + local languages = { ... } + local check_installed = true + if ... == 'all' then + languages = parsers.available_parsers() + check_installed = false + end + + for _, lang in ipairs(languages) do + if check_installed then + if #api.nvim_get_runtime_file('parser/'..lang..'.so', false) > 0 then + local yesno = fn.input(lang .. ' parser already available: would you like to reinstall ? y/n: ') + print('\n ') -- mandatory to avoid messing up command line + if not string.match(yesno, '^y.*') then return end + end end local parser_config = parsers.get_parser_configs()[lang] @@ -143,13 +152,6 @@ M.commands = { "-complete=custom,v:lua.ts_installable_parsers" }, description = '`:TSInstall {lang}` installs a parser under nvim-treesitter/parser/{lang}.so' - }, - TSInstallAll = { - run = function() - for _, lang in pairs(parsers.available_parsers()) do - install(lang) - end - end } } From 0ed1fbf9093a7dda9aadda145c1b35dc5343cc8b Mon Sep 17 00:00:00 2001 From: kiyan42 Date: Thu, 25 Jun 2020 12:39:59 +0200 Subject: [PATCH 45/87] continue installing if not reinstalling one parser --- lua/nvim-treesitter/install.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lua/nvim-treesitter/install.lua b/lua/nvim-treesitter/install.lua index 24f6fbd63..686688bf7 100644 --- a/lua/nvim-treesitter/install.lua +++ b/lua/nvim-treesitter/install.lua @@ -107,7 +107,7 @@ local function install(...) if #api.nvim_get_runtime_file('parser/'..lang..'.so', false) > 0 then local yesno = fn.input(lang .. ' parser already available: would you like to reinstall ? y/n: ') print('\n ') -- mandatory to avoid messing up command line - if not string.match(yesno, '^y.*') then return end + if not string.match(yesno, '^y.*') then goto continue end end end @@ -123,6 +123,7 @@ local function install(...) } run_install(cache_folder, package_path, lang, install_info) + ::continue:: end end From 88c8b70560a0286c325a507e0d498219ee43c20f Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Wed, 24 Jun 2020 16:42:55 +0200 Subject: [PATCH 46/87] Python highlights: Add walrus operator --- queries/python/highlights.scm | 1 + 1 file changed, 1 insertion(+) diff --git a/queries/python/highlights.scm b/queries/python/highlights.scm index 739ab81cf..e238a9a8e 100644 --- a/queries/python/highlights.scm +++ b/queries/python/highlights.scm @@ -93,6 +93,7 @@ [ "-" "-=" + ":=" "!=" "*" "**" From 75bce1dc93293f4f1dfd1541386afb607f09d726 Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Wed, 24 Jun 2020 16:45:17 +0200 Subject: [PATCH 47/87] Python highlights: Reset highlighting in f-string interpolation This solution is preferable to `(identifier) @Normal` since otherwise everything without highlight will use string-highlighting. --- queries/python/highlights.scm | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/queries/python/highlights.scm b/queries/python/highlights.scm index e238a9a8e..ca0f84387 100644 --- a/queries/python/highlights.scm +++ b/queries/python/highlights.scm @@ -1,9 +1,10 @@ ;; From tree-sitter-python licensed under MIT License ; Copyright (c) 2016 Max Brunsfeld -; Identifier naming conventions +; Reset highlighing in f-string interpolations +(interpolation) @Normal -(identifier) @Normal +; Identifier naming conventions ((identifier) @type (match? @type "^[A-Z]")) ((identifier) @constant From 2d224ac3bfd0f3ac4afd458a7f2fb55955ebc38c Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Fri, 26 Jun 2020 17:12:10 +0200 Subject: [PATCH 48/87] Fix cpp-highlights: wrong capture name used in regex --- queries/cpp/highlights.scm | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/queries/cpp/highlights.scm b/queries/cpp/highlights.scm index 92909f4d9..0d8295a1d 100644 --- a/queries/cpp/highlights.scm +++ b/queries/cpp/highlights.scm @@ -52,7 +52,7 @@ ((call_expression function: (field_expression field: (field_identifier) @constructor)) -(#match? @function "^[A-Z]")) +(#match? @constructor "^[A-Z]")) ;; constructing a type in a intizializer list: Constructor (): **SuperType (1)** ((field_initializer @@ -64,8 +64,7 @@ ; Constants -;(this) @constant.builtin -(this) @keyword +(this) @constant.builtin (nullptr) @constant (true) @boolean From 282e18edd18fdd84e8436440789c0ecb2bc83f82 Mon Sep 17 00:00:00 2001 From: Thomas Vigouroux Date: Sun, 21 Jun 2020 19:43:02 +0200 Subject: [PATCH 49/87] highlight: use custom highlight groups --- doc/nvim-treesitter.txt | 131 ++++++++++++++++++++++++++++++ doc/tags | 32 ++++++++ lua/nvim-treesitter/highlight.lua | 64 +++++++-------- plugin/nvim-treesitter.vim | 41 +++++++++- 4 files changed, 235 insertions(+), 33 deletions(-) diff --git a/doc/nvim-treesitter.txt b/doc/nvim-treesitter.txt index bb53e182e..5a3a4ce11 100644 --- a/doc/nvim-treesitter.txt +++ b/doc/nvim-treesitter.txt @@ -166,4 +166,135 @@ Note: This is highly experimental, and folding can break on some types of edits. If you encounter such breakage, hiting `zx` should fix folding. In any case, feel free to open an issue with the reproducing steps. +============================================================================== +HIGHLIGHTS *nvim-treesitter-highlights* + +TSError + +`TSPunctDelimiter` + *hl-TSPunctDelimiter* +For delimiters ie: `.` + +`TSPunctBracket` + *hl-TSPunctBracket* +For brackets and parens. + +`TSPunctSpecial` + *hl-TSPunctSpecial* +For special punctutation that does not fall in the catagories before. + +`TSConstant` + *hl-TSConstant* +For constants + +`TSConstBuiltin` + *hl-TSConstBuiltin* +For constant that are built in the language: `nil` in Lua. + +`TSConstMacro` + *hl-TSConstMacro* +For constants that are defined by macros: `NULL` in C. + +`TSString` + *hl-TSString* +For strings. + +`TSStringRegex` + *hl-TSStringRegex* +For regexes. + +`TSStringEscape` + *hl-TSStringEscape* +For escape characters within a string. + +`TSCharacter` + *hl-TSCharacter* +For characters. + +`TSNumber` + *hl-TSNumber* +For integers. + +`TSBoolean` + *hl-TSBoolean* +For booleans. + +`TSFloat` + *hl-TSFloat* +For floats. + +`TSFunction` + *hl-TSFunction* +For function (calls and definitions). + +`TSFuncBuiltin` + *hl-TSFuncBuiltin* +For builtin functions: `table.insert` in Lua. + +`TSFuncMacro` + *hl-TSFuncMacro* +For macro defined fuctions (calls and definitions): each `macro_rules` in +Rust. + +`TSParameter` + *hl-TSParameter* +For parameters of a function. + +`TSMethod` + *hl-TSMethod* +For method calls and definitions. + +`TSField` + *hl-TSField* +For fields. + +`TSProperty` + *hl-TSProperty* +Same as `TSField`. + +`TSConstructor` + *hl-TSConstructor* +For constructor calls and definitions: `{}` in Lua, and Java constructors. + +`TSConditional` + *hl-TSConditional* +For keywords related to conditionnals. + +`TSRepeat` + *hl-TSRepeat* +For keywords related to loops. + +`TSLabel` + *hl-TSLabel* +For labels: `labe:` in C and `:label:` in Lua. + +`TSOperator` + *hl-TSOperator* +For any operator: `+`, but also `->` and `*` in C. + +`TSKeyword` + *hl-TSKeyword* +For keywords that don't fall in previous categories. + +`TSException` + *hl-TSException* +For exception related keywords. + +`TSType` + *hl-TSType* +For types. + +`TSTypeBuiltin` + *hl-TSTypeBuiltin* +For builtin types (you guessed it, right ?). + +`TSStructure` + *hl-TSStructure* +This is left as an exercise for the reader. + +`TSInclude` + *hl-TSInclude* +For includes: `#include` in C, `use` or `extern crate` in Rust, or `require` +in Lua + vim:tw=78:ts=8:noet:ft=help:norl: diff --git a/doc/tags b/doc/tags index df33c4d43..7cab44634 100644 --- a/doc/tags +++ b/doc/tags @@ -5,9 +5,41 @@ :TSInstall nvim-treesitter.txt /*:TSInstall* :TSInstallInfo nvim-treesitter.txt /*:TSInstallInfo* :TSModuleInfo nvim-treesitter.txt /*:TSModuleInfo* +hl-TSBoolean nvim-treesitter.txt /*hl-TSBoolean* +hl-TSCharacter nvim-treesitter.txt /*hl-TSCharacter* +hl-TSConditional nvim-treesitter.txt /*hl-TSConditional* +hl-TSConstBuiltin nvim-treesitter.txt /*hl-TSConstBuiltin* +hl-TSConstMacro nvim-treesitter.txt /*hl-TSConstMacro* +hl-TSConstant nvim-treesitter.txt /*hl-TSConstant* +hl-TSConstructor nvim-treesitter.txt /*hl-TSConstructor* +hl-TSException nvim-treesitter.txt /*hl-TSException* +hl-TSField nvim-treesitter.txt /*hl-TSField* +hl-TSFloat nvim-treesitter.txt /*hl-TSFloat* +hl-TSFuncBuiltin nvim-treesitter.txt /*hl-TSFuncBuiltin* +hl-TSFuncMacro nvim-treesitter.txt /*hl-TSFuncMacro* +hl-TSFunction nvim-treesitter.txt /*hl-TSFunction* +hl-TSInclude nvim-treesitter.txt /*hl-TSInclude* +hl-TSKeyword nvim-treesitter.txt /*hl-TSKeyword* +hl-TSLabel nvim-treesitter.txt /*hl-TSLabel* +hl-TSMethod nvim-treesitter.txt /*hl-TSMethod* +hl-TSNumber nvim-treesitter.txt /*hl-TSNumber* +hl-TSOperator nvim-treesitter.txt /*hl-TSOperator* +hl-TSParameter nvim-treesitter.txt /*hl-TSParameter* +hl-TSProperty nvim-treesitter.txt /*hl-TSProperty* +hl-TSPunctBracket nvim-treesitter.txt /*hl-TSPunctBracket* +hl-TSPunctDelimiter nvim-treesitter.txt /*hl-TSPunctDelimiter* +hl-TSPunctSpecial nvim-treesitter.txt /*hl-TSPunctSpecial* +hl-TSRepeat nvim-treesitter.txt /*hl-TSRepeat* +hl-TSString nvim-treesitter.txt /*hl-TSString* +hl-TSStringEscape nvim-treesitter.txt /*hl-TSStringEscape* +hl-TSStringRegex nvim-treesitter.txt /*hl-TSStringRegex* +hl-TSStructure nvim-treesitter.txt /*hl-TSStructure* +hl-TSType nvim-treesitter.txt /*hl-TSType* +hl-TSTypeBuiltin nvim-treesitter.txt /*hl-TSTypeBuiltin* nvim-treesitter nvim-treesitter.txt /*nvim-treesitter* nvim-treesitter-commands nvim-treesitter.txt /*nvim-treesitter-commands* nvim-treesitter-functions nvim-treesitter.txt /*nvim-treesitter-functions* +nvim-treesitter-highlights nvim-treesitter.txt /*nvim-treesitter-highlights* nvim-treesitter-intro nvim-treesitter.txt /*nvim-treesitter-intro* nvim-treesitter-quickstart nvim-treesitter.txt /*nvim-treesitter-quickstart* nvim-treesitter-utils nvim-treesitter.txt /*nvim-treesitter-utils* diff --git a/lua/nvim-treesitter/highlight.lua b/lua/nvim-treesitter/highlight.lua index 717601462..1b4722435 100644 --- a/lua/nvim-treesitter/highlight.lua +++ b/lua/nvim-treesitter/highlight.lua @@ -11,45 +11,45 @@ local M = { local hlmap = vim.treesitter.TSHighlighter.hl_map -- Misc -hlmap.error = "Error" -hlmap["punctuation.delimiter"] = "Delimiter" -hlmap["punctuation.bracket"] = "Delimiter" -hlmap["punctuation.special"] = "Delimiter" +hlmap.error = "TSError" +hlmap["punctuation.delimiter"] = "TSPunctDelimiter" +hlmap["punctuation.bracket"] = "TSPunctBracket" +hlmap["punctuation.special"] = "TSPunctSpecial" -- Constants -hlmap["constant"] = "Constant" -hlmap["constant.builtin"] = "Special" -hlmap["constant.macro"] = "Define" -hlmap["string"] = "String" -hlmap["string.regex"] = "String" -hlmap["string.escape"] = "SpecialChar" -hlmap["character"] = "Character" -hlmap["number"] = "Number" -hlmap["boolean"] = "Boolean" -hlmap["float"] = "Float" +hlmap["constant"] = "TSConstant" +hlmap["constant.builtin"] = "TSConstBuiltin" +hlmap["constant.macro"] = "TSConstMacro" +hlmap["string"] = "TSString" +hlmap["string.regex"] = "TSStringRegex" +hlmap["string.escape"] = "TSStringEscape" +hlmap["character"] = "TSCharacter" +hlmap["number"] = "TSNumber" +hlmap["boolean"] = "TSBoolean" +hlmap["float"] = "TSFloat" -- Functions -hlmap["function"] = "Function" -hlmap["function.builtin"] = "Special" -hlmap["function.macro"] = "Macro" -hlmap["parameter"] = "Identifier" -hlmap["method"] = "Function" -hlmap["field"] = "Identifier" -hlmap["property"] = "Identifier" -hlmap["constructor"] = "Special" +hlmap["function"] = "TSFunction" +hlmap["function.builtin"] = "TSFuncBuiltin" +hlmap["function.macro"] = "TSFuncMacro" +hlmap["parameter"] = "TSIdentifier" +hlmap["method"] = "TSMethod" +hlmap["field"] = "TSField" +hlmap["property"] = "TSProperty" +hlmap["constructor"] = "TSConstructor" -- Keywords -hlmap["conditional"] = "Conditional" -hlmap["repeat"] = "Repeat" -hlmap["label"] = "Label" -hlmap["operator"] = "Operator" -hlmap["keyword"] = "Keyword" -hlmap["exception"] = "Exception" +hlmap["conditional"] = "TSConditional" +hlmap["repeat"] = "TSRepeat" +hlmap["label"] = "TSLabel" +hlmap["operator"] = "TSOperator" +hlmap["keyword"] = "TSKeyword" +hlmap["exception"] = "TSException" -hlmap["type"] = "Type" -hlmap["type.builtin"] = "Type" -hlmap["structure"] = "Structure" -hlmap["include"] = "Include" +hlmap["type"] = "TSType" +hlmap["type.builtin"] = "TSTypeBuiltin" +hlmap["structure"] = "TSStructure" +hlmap["include"] = "TSInclude" function M.attach(bufnr, lang) local bufnr = bufnr or api.nvim_get_current_buf() diff --git a/plugin/nvim-treesitter.vim b/plugin/nvim-treesitter.vim index 4769680a3..cbd08a9bf 100644 --- a/plugin/nvim-treesitter.vim +++ b/plugin/nvim-treesitter.vim @@ -1,4 +1,4 @@ -" Last Change: 2020 avril 25 +" Last Change: 2020 juin 21 if exists('g:loaded_nvim_treesitter') finish @@ -18,3 +18,42 @@ ts_available_modules = function() end require'nvim-treesitter'.setup() EOF + +highlight link TSError Error + +highlight link TSPunctDelimiter Delimiter +highlight link TSPunctBracket Delimiter +highlight link TSPunctSpecial Delimiter + +highlight link TSConstant Constant +highlight link TSConstBuiltin Special +highlight link TSConstMacro Define +highlight link TSString String +highlight link TSStringRegex String +highlight link TSStringEscape SpecialChar +highlight link TSCharacter Character +highlight link TSNumber Number +highlight link TSBoolean Boolean +highlight link TSFloat TSFloat + +highlight link TSFunction Function +highlight link TSFuncBuiltin Special +highlight link TSFuncMacro Macro +highlight link TSParameter Identifier +highlight link TSMethod Function +highlight link TSField Identifier +highlight link TSProperty Identifier +highlight link TSConstructor Special + +highlight link TSConditional Conditional +highlight link TSRepeat Repeat +highlight link TSLabel Label +highlight link TSOperator Operator +highlight link TSKeyword Keyword +highlight link TSException Exception + +highlight link TSType Type +highlight link TSTypeBuiltin Type +highlight link TSStructure Structure +highlight link TSInclude Include + From ddb41c36e8de2c716ebbcb13aff6f50a9b0c4e2f Mon Sep 17 00:00:00 2001 From: kiyan42 Date: Sat, 27 Jun 2020 12:43:19 +0200 Subject: [PATCH 50/87] update docs for TSInstall --- README.md | 2 +- doc/nvim-treesitter.txt | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9c8a97d33..54d17abce 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ $ git clone https://github.com/nvim-treesitter/nvim-treesitter.git Treesitter is using a different _parser_ for every language. It can be quite a pain to install, but fortunately `nvim-treesitter` provides two command to tackle this issue: - - `TSInstall` to install a given parser. + - `TSInstall` to install one or more parser. You can use `TSInstall all` to download all parsers. - `TSInstallInfo` to know which parser is installed. Let's say you need parsers for `lua`, `c`, and `python`, this is how you do with these commands: diff --git a/doc/nvim-treesitter.txt b/doc/nvim-treesitter.txt index 5a3a4ce11..67e9f7581 100644 --- a/doc/nvim-treesitter.txt +++ b/doc/nvim-treesitter.txt @@ -52,9 +52,10 @@ By default, everything is disabled. To enable support for features, in your `ini ============================================================================== COMMANDS *nvim-treesitter-commands* -|:TSInstall| {language} *:TSInstall* +|:TSInstall| {language} ... *:TSInstall* -Download, compile and install a parser for {language} +Install one or more treesitter parsers. +You can use |:TSInstall| `all` to install all parsers. |:TSInstallInfo| *:TSInstallInfo* From c27d35adc5bb8bd8c2ca58883cda6e9ec297106d Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Sat, 27 Jun 2020 20:33:55 +0200 Subject: [PATCH 51/87] Fix typo in documentation: `labe:` -> `label:` --- doc/nvim-treesitter.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/nvim-treesitter.txt b/doc/nvim-treesitter.txt index 67e9f7581..b287b56e3 100644 --- a/doc/nvim-treesitter.txt +++ b/doc/nvim-treesitter.txt @@ -267,7 +267,7 @@ For keywords related to loops. `TSLabel` *hl-TSLabel* -For labels: `labe:` in C and `:label:` in Lua. +For labels: `label:` in C and `:label:` in Lua. `TSOperator` *hl-TSOperator* From 54438439e6e05a52eaca34d815211e61231c9da8 Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Sat, 27 Jun 2020 21:06:11 +0200 Subject: [PATCH 52/87] C highlights: Add highlight for #elif --- queries/c/highlights.scm | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/queries/c/highlights.scm b/queries/c/highlights.scm index 0d718c223..b3889dbd8 100644 --- a/queries/c/highlights.scm +++ b/queries/c/highlights.scm @@ -21,13 +21,16 @@ "while" @repeat "#define" @constant.macro -"#else" @keyword -"#endif" @keyword -"#if" @keyword -"#ifdef" @keyword -"#ifndef" @keyword -"#include" @keyword -(preproc_directive) @keyword +[ + "#if" + "#ifdef" + "#ifndef" + "#else" + "#elif" + "#endif" + "#include" + (preproc_directive) +] @keyword "--" @operator "-" @operator From b184f1cafbe7316ae8671b7eafec5a11383b4fd6 Mon Sep 17 00:00:00 2001 From: Chinmay Dalal Date: Mon, 29 Jun 2020 14:18:13 +0530 Subject: [PATCH 53/87] Java highlights: Capture parameters in declaration (#93) * Capture parameters in declaration * Refactor (new syntax) * Fix spaces * Fix capture (java method parameters) * Improve java parameter capture * Fix bracket and whitespace * Fix java query * Fix java query Co-authored-by: Thomas Vigouroux <39092278+vigoux@users.noreply.github.com> * Add capture for lambda parameters, remove redundant captures, add -> operator * Previous commit wasn't saved properly * Fix formatting * Changes suggested in PR https://github.com/nvim-treesitter/nvim-treesitter/pull/93#pullrequestreview-435630553 * Add bitwise operators * Re-add builtin operators * remove left shift, add ^= * Add &= and |= * remove @variable * Remove duplicate Co-authored-by: Thomas Vigouroux <39092278+vigoux@users.noreply.github.com> --- queries/java/highlights.scm | 95 +++++++++++++++++++++++++------------ 1 file changed, 64 insertions(+), 31 deletions(-) diff --git a/queries/java/highlights.scm b/queries/java/highlights.scm index e51099e58..8d50ebfd6 100644 --- a/queries/java/highlights.scm +++ b/queries/java/highlights.scm @@ -2,20 +2,32 @@ ; Methods + (method_declaration name: (identifier) @method) (method_invocation name: (identifier) @method) + (super) @function.builtin +; Parameters +(formal_parameter + name: (identifier) @parameter) + +(inferred_parameters (identifier) @parameter) + + ; Annotations + (annotation name: (identifier) @attribute) (marker_annotation name: (identifier) @attribute) + ; Operators + [ "@" "+" @@ -41,6 +53,15 @@ "*=" "/=" "%=" +"->" +"^" +"^=" +"&=" +"|=" +"~" +">>" +">>>" +"<<" ] @operator ; Types @@ -51,40 +72,46 @@ name: (identifier) @type) (enum_declaration name: (identifier) @type) +(constructor_declaration + name: (identifier) @type) +(type_identifier) @type + + ((field_access object: (identifier) @type) - (#match? @type "^[A-Z]")) + (#match? @type "^[A-Z]")) ((scoped_identifier scope: (identifier) @type) - (#match? @type "^[A-Z]")) + (#match? @type "^[A-Z]")) -(constructor_declaration - name: (identifier) @type) - -(type_identifier) @type -(boolean_type) @type.builtin -(integral_type) @type.builtin -(floating_point_type) @type.builtin -(floating_point_type) @type.builtin -(void_type) @type.builtin +[ +(boolean_type) +(integral_type) +(floating_point_type) +(void_type) +] @type.builtin ; Variables ((identifier) @constant - (#match? @constant "^_*[A-Z][A-Z\d_]+")) + (#match? @constant "^_*[A-Z][A-Z\d_]+")) -(identifier) @variable -(this) @variable.builtin ; Literals -(hex_integer_literal) @number -(decimal_integer_literal) @number -(octal_integer_literal) @number -(decimal_floating_point_literal) @float -(hex_floating_point_literal) @float +[ +(hex_integer_literal) +(decimal_integer_literal) +(octal_integer_literal) +] @number + +[ +(decimal_floating_point_literal) +(hex_floating_point_literal) +] @float + (character_literal) @character (string_literal) @string (null_literal) @constant.builtin @@ -106,7 +133,6 @@ "class" "continue" "default" -"do" "enum" "exports" "extends" @@ -150,25 +176,32 @@ "case" ] @conditional -; +; [ "for" "while" +"do" ] @repeat -; +; Includes "import" @include +"package" @include ; Punctuation -";" @punctuation.delimiter -"." @punctuation.delimiter -"," @punctuation.delimiter -"[" @punctuation.bracket -"]" @punctuation.bracket -"{" @punctuation.bracket -"}" @punctuation.bracket -"(" @punctuation.bracket -")" @punctuation.bracket +[ +";" +"." +"," +] @punctuation.delimiter + +[ +"[" +"]" +"{" +"}" +"(" +")" +] @punctuation.bracket From f4b87d3364d8ef08aad4fd7aebf59450f2c06d67 Mon Sep 17 00:00:00 2001 From: Thomas Vigouroux Date: Mon, 29 Jun 2020 09:00:06 +0000 Subject: [PATCH 54/87] highlights: declare links as defaults --- plugin/nvim-treesitter.vim | 66 +++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/plugin/nvim-treesitter.vim b/plugin/nvim-treesitter.vim index cbd08a9bf..24a8eceb6 100644 --- a/plugin/nvim-treesitter.vim +++ b/plugin/nvim-treesitter.vim @@ -1,4 +1,4 @@ -" Last Change: 2020 juin 21 +" Last Change: 2020 Jun 29 if exists('g:loaded_nvim_treesitter') finish @@ -19,41 +19,41 @@ end require'nvim-treesitter'.setup() EOF -highlight link TSError Error +highlight default link TSError Error -highlight link TSPunctDelimiter Delimiter -highlight link TSPunctBracket Delimiter -highlight link TSPunctSpecial Delimiter +highlight default link TSPunctDelimiter Delimiter +highlight default link TSPunctBracket Delimiter +highlight default link TSPunctSpecial Delimiter -highlight link TSConstant Constant -highlight link TSConstBuiltin Special -highlight link TSConstMacro Define -highlight link TSString String -highlight link TSStringRegex String -highlight link TSStringEscape SpecialChar -highlight link TSCharacter Character -highlight link TSNumber Number -highlight link TSBoolean Boolean -highlight link TSFloat TSFloat +highlight default link TSConstant Constant +highlight default link TSConstBuiltin Special +highlight default link TSConstMacro Define +highlight default link TSString String +highlight default link TSStringRegex String +highlight default link TSStringEscape SpecialChar +highlight default link TSCharacter Character +highlight default link TSNumber Number +highlight default link TSBoolean Boolean +highlight default link TSFloat TSFloat -highlight link TSFunction Function -highlight link TSFuncBuiltin Special -highlight link TSFuncMacro Macro -highlight link TSParameter Identifier -highlight link TSMethod Function -highlight link TSField Identifier -highlight link TSProperty Identifier -highlight link TSConstructor Special +highlight default link TSFunction Function +highlight default link TSFuncBuiltin Special +highlight default link TSFuncMacro Macro +highlight default link TSParameter Identifier +highlight default link TSMethod Function +highlight default link TSField Identifier +highlight default link TSProperty Identifier +highlight default link TSConstructor Special -highlight link TSConditional Conditional -highlight link TSRepeat Repeat -highlight link TSLabel Label -highlight link TSOperator Operator -highlight link TSKeyword Keyword -highlight link TSException Exception +highlight default link TSConditional Conditional +highlight default link TSRepeat Repeat +highlight default link TSLabel Label +highlight default link TSOperator Operator +highlight default link TSKeyword Keyword +highlight default link TSException Exception -highlight link TSType Type -highlight link TSTypeBuiltin Type -highlight link TSStructure Structure -highlight link TSInclude Include +highlight default link TSType Type +highlight default link TSTypeBuiltin Type +highlight default link TSStructure Structure +highlight default link TSInclude Include From a2f09312540822e9c579fee4534832fefd492250 Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Sat, 27 Jun 2020 20:43:41 +0200 Subject: [PATCH 55/87] Extend documentation for TSError --- doc/nvim-treesitter.txt | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/doc/nvim-treesitter.txt b/doc/nvim-treesitter.txt index b287b56e3..c9adf93b2 100644 --- a/doc/nvim-treesitter.txt +++ b/doc/nvim-treesitter.txt @@ -170,7 +170,13 @@ Note: This is highly experimental, and folding can break on some types of ============================================================================== HIGHLIGHTS *nvim-treesitter-highlights* -TSError +`TSError` + *hl-TSError* +For syntax/parser errors. + +You can deactivate highlighting of syntax errors by adding this to your +init.vim: > + highlight link TSError Normal `TSPunctDelimiter` *hl-TSPunctDelimiter* From 7f6f7596dac5a45ae914336dc986ebb30881b1a3 Mon Sep 17 00:00:00 2001 From: kiyan42 Date: Mon, 29 Jun 2020 14:46:25 +0200 Subject: [PATCH 56/87] add used_by key to parsers Enables the use of multiple filetypes for one parser. --- lua/nvim-treesitter.lua | 5 +++-- lua/nvim-treesitter/configs.lua | 16 ++++++++++------ lua/nvim-treesitter/parsers.lua | 31 ++++++++++++++++++++++--------- 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/lua/nvim-treesitter.lua b/lua/nvim-treesitter.lua index 9b3ddce89..553ec3f65 100644 --- a/lua/nvim-treesitter.lua +++ b/lua/nvim-treesitter.lua @@ -17,9 +17,10 @@ function M.setup() for _, lang in pairs(parsers.available_parsers()) do for _, mod in pairs(configs.available_modules()) do if configs.is_enabled(mod, lang) then - local ft = parsers.lang_to_ft(lang) local cmd = string.format("lua require'nvim-treesitter.%s'.attach()", mod) - api.nvim_command(string.format("autocmd NvimTreesitter FileType %s %s", ft, cmd)) + for _, ft in pairs(parsers.lang_to_ft(lang)) do + api.nvim_command(string.format("autocmd NvimTreesitter FileType %s %s", ft, cmd)) + end end end end diff --git a/lua/nvim-treesitter/configs.lua b/lua/nvim-treesitter/configs.lua index f542e2609..13eb93675 100644 --- a/lua/nvim-treesitter/configs.lua +++ b/lua/nvim-treesitter/configs.lua @@ -49,9 +49,10 @@ end local function enable_mod_conf_autocmd(mod, lang) if not config.modules[mod] or M.is_enabled(mod, lang) then return end - local ft = parsers.lang_to_ft(lang) local cmd = string.format("lua require'nvim-treesitter.%s'.attach()", mod) - api.nvim_command(string.format("autocmd FileType %s %s", ft, cmd)) + for _, ft in pairs(parsers.lang_to_ft(lang)) do + api.nvim_command(string.format("autocmd NvimTreesitter FileType %s %s", ft, cmd)) + end for i, parser in pairs(config.modules[mod].disable) do if parser == lang then table.remove(config.modules[mod].disable, i) @@ -65,7 +66,7 @@ local function enable_all(mod, lang) for _, bufnr in pairs(api.nvim_list_bufs()) do local ft = api.nvim_buf_get_option(bufnr, 'ft') - if not lang or ft == parsers.lang_to_ft(lang) then + if not lang or parsers.lang_match_ft(lang, ft) then enable_module(mod, bufnr, lang) end end @@ -101,15 +102,18 @@ end local function disable_mod_conf_autocmd(mod, lang) if not config.modules[mod] or not M.is_enabled(mod, lang) then return end - local ft = parsers.lang_to_ft(lang) - api.nvim_command(string.format("autocmd! FileType %s", ft)) + local cmd = string.format("lua require'nvim-treesitter.%s'.attach()", mod) + -- TODO(kyazdani): detach the correct autocmd... doesn't work when using %s, cmd + for _, ft in pairs(parsers.lang_to_ft(lang)) do + api.nvim_command(string.format("autocmd! NvimTreesitter FileType %s", ft)) + end table.insert(config.modules[mod].disable, lang) end local function disable_all(mod, lang) for _, bufnr in pairs(api.nvim_list_bufs()) do local ft = api.nvim_buf_get_option(bufnr, 'ft') - if not lang or ft == parsers.lang_to_ft(lang) then + if not lang or parsers.lang_match_ft(lang, ft) then disable_module(mod, bufnr, lang) end end diff --git a/lua/nvim-treesitter/parsers.lua b/lua/nvim-treesitter/parsers.lua index 9097d335d..7fa09df91 100644 --- a/lua/nvim-treesitter/parsers.lua +++ b/lua/nvim-treesitter/parsers.lua @@ -64,6 +64,7 @@ list.bash = { url = "https://github.com/tree-sitter/tree-sitter-bash", files = { "src/parser.c", "src/scanner.cc" }, }, + used_by = { "zsh" }, filetype = 'sh' } @@ -205,10 +206,10 @@ list.nix = { } list.regex = { - install_info = { - url = "https://github.com/tree-sitter/tree-sitter-regex", - files = { "src/parser.c" } - } + install_info = { + url = "https://github.com/tree-sitter/tree-sitter-regex", + files = { "src/parser.c" } + } } local M = { @@ -216,12 +217,14 @@ local M = { } local ft_to_parsername = {} + for name, obj in pairs(M.list) do - if obj.filetype then - ft_to_parsername[obj.filetype] = name - else - ft_to_parsername[name] = name + if type(obj.used_by) == 'table' then + for _, ft in pairs(obj.used_by) do + ft_to_parsername[ft] = name + end end + ft_to_parsername[obj.filetype or name] = name end function M.ft_to_lang(ft) @@ -229,7 +232,17 @@ function M.ft_to_lang(ft) end function M.lang_to_ft(lang) - return M.list[lang].filetype or lang + local obj = M.list[lang] + return vim.tbl_flatten({{obj.filetype or lang}, obj.used_by or {}}) +end + +function M.lang_match_ft(lang, ft) + for _, f in pairs(M.lang_to_ft(lang)) do + if ft == f then + return true + end + end + return false end function M.available_parsers() From 65b3a8e6d7b3777bbea326dd96d21d17d937fd7d Mon Sep 17 00:00:00 2001 From: Chinmay Dalal Date: Mon, 29 Jun 2020 21:55:52 +0530 Subject: [PATCH 57/87] Add method reference operator --- queries/java/highlights.scm | 1 + 1 file changed, 1 insertion(+) diff --git a/queries/java/highlights.scm b/queries/java/highlights.scm index 8d50ebfd6..ac582c042 100644 --- a/queries/java/highlights.scm +++ b/queries/java/highlights.scm @@ -62,6 +62,7 @@ ">>" ">>>" "<<" +"::" ] @operator ; Types From 180ad9a1a8c1212d9839bdbe97c11137b48a7064 Mon Sep 17 00:00:00 2001 From: kiyan42 Date: Tue, 30 Jun 2020 08:14:47 +0200 Subject: [PATCH 58/87] add python async --- queries/python/highlights.scm | 1 + 1 file changed, 1 insertion(+) diff --git a/queries/python/highlights.scm b/queries/python/highlights.scm index ca0f84387..fb316bea5 100644 --- a/queries/python/highlights.scm +++ b/queries/python/highlights.scm @@ -132,6 +132,7 @@ [ "assert" + "async" "await" "class" "def" From 058e8d2296515041be982c6f23c119ec6b6d1ba9 Mon Sep 17 00:00:00 2001 From: Steven Sojka Date: Thu, 25 Jun 2020 13:26:31 -0500 Subject: [PATCH 59/87] feat(refactor): highlight usages module --- lua/nvim-treesitter/configs.lua | 133 +++++++++++++----- lua/nvim-treesitter/info.lua | 2 +- lua/nvim-treesitter/locals.lua | 4 +- lua/nvim-treesitter/query.lua | 9 ++ .../refactor/highlight_definitions.lua | 93 ++++++++++++ lua/nvim-treesitter/ts_utils.lua | 51 +++++++ lua/nvim-treesitter/utils.lua | 17 +++ queries/javascript/locals.scm | 3 + 8 files changed, 275 insertions(+), 37 deletions(-) create mode 100644 lua/nvim-treesitter/refactor/highlight_definitions.lua diff --git a/lua/nvim-treesitter/configs.lua b/lua/nvim-treesitter/configs.lua index 13eb93675..30c2114ce 100644 --- a/lua/nvim-treesitter/configs.lua +++ b/lua/nvim-treesitter/configs.lua @@ -2,6 +2,7 @@ local api = vim.api local queries = require'nvim-treesitter.query' local parsers = require'nvim-treesitter.parsers' +local utils = require'nvim-treesitter.utils' -- @enable can be true or false -- @disable is a list of languages, only relevant if enable is true @@ -12,9 +13,7 @@ local config = { highlight = { enable = false, disable = {}, - is_supported = function(lang) - return queries.get_query(lang, 'highlights') ~= nil - end + is_supported = queries.has_highlights }, incremental_selection = { enable = false, @@ -25,9 +24,14 @@ local config = { scope_incremental="grc", node_decremental="grm" }, - is_supported = function(lang) - return queries.get_query(lang, 'locals') - end + is_supported = queries.has_locals + }, + refactor = { + highlight_definitions = { + enable = false, + disable = {}, + is_supported = queries.has_locals + } } }, ensure_installed = nil @@ -38,7 +42,8 @@ local M = {} local function enable_module(mod, bufnr, lang) local bufnr = bufnr or api.nvim_get_current_buf() local lang = lang or parsers.ft_to_lang(api.nvim_buf_get_option(bufnr, 'ft')) - if not parsers.list[lang] or not config.modules[mod] then + + if not parsers.list[lang] or not M.get_module(mod) then return end @@ -47,22 +52,26 @@ local function enable_module(mod, bufnr, lang) end local function enable_mod_conf_autocmd(mod, lang) - if not config.modules[mod] or M.is_enabled(mod, lang) then return end + local config_mod = M.get_module(mod) + + if not config_mod or M.is_enabled(mod, lang) then return end local cmd = string.format("lua require'nvim-treesitter.%s'.attach()", mod) for _, ft in pairs(parsers.lang_to_ft(lang)) do api.nvim_command(string.format("autocmd NvimTreesitter FileType %s %s", ft, cmd)) end - for i, parser in pairs(config.modules[mod].disable) do + for i, parser in pairs(config_mod.disable) do if parser == lang then - table.remove(config.modules[mod].disable, i) + table.remove(config_mod.disable, i) break end end end local function enable_all(mod, lang) - if not config.modules[mod] then return end + local config_mod = M.get_module(mod) + + if not config_mod then return end for _, bufnr in pairs(api.nvim_list_bufs()) do local ft = api.nvim_buf_get_option(bufnr, 'ft') @@ -81,7 +90,7 @@ local function enable_all(mod, lang) end end end - config.modules[mod].enable = true + config_mod.enable = true end local function disable_module(mod, bufnr, lang) @@ -91,7 +100,7 @@ local function disable_module(mod, bufnr, lang) return end - if not parsers.list[lang] or not config.modules[mod] then + if not parsers.list[lang] or not M.get_module(mod) then return end @@ -100,14 +109,16 @@ local function disable_module(mod, bufnr, lang) end local function disable_mod_conf_autocmd(mod, lang) - if not config.modules[mod] or not M.is_enabled(mod, lang) then return end + local config_mod = M.get_module(mod) + + if not config_mod or not M.is_enabled(mod, lang) then return end local cmd = string.format("lua require'nvim-treesitter.%s'.attach()", mod) -- TODO(kyazdani): detach the correct autocmd... doesn't work when using %s, cmd for _, ft in pairs(parsers.lang_to_ft(lang)) do api.nvim_command(string.format("autocmd! NvimTreesitter FileType %s", ft)) end - table.insert(config.modules[mod].disable, lang) + table.insert(config_mod.disable, lang) end local function disable_all(mod, lang) @@ -123,7 +134,30 @@ local function disable_all(mod, lang) for _, lang in pairs(parsers.available_parsers()) do disable_mod_conf_autocmd(mod, lang) end - config.modules[mod].enable = false + + local config_mod = M.get_module(mod) + + if config_mod then + config_mod.enable = false + end + end +end + +-- Recurses trough all modules including submodules +-- @param accumulator function called for each module +-- @param root root configuration table to start at +-- @param path prefix path +local function recurse_modules(accumulator, root, path) + local root = root or config.modules + + for name, module in pairs(root) do + local new_path = path and (path..'.'..name) or name + + if M.is_module(module) then + accumulator(name, module, new_path) + elseif type(module) == 'table' then + recurse_modules(accumulator, module, new_path) + end end end @@ -169,7 +203,7 @@ function M.is_enabled(mod, lang) return false end - local module_config = config.modules[mod] + local module_config = M.get_module(mod) if not module_config then return false end if not module_config.enable or not module_config.is_supported(lang) then @@ -188,19 +222,7 @@ function M.setup(user_data) for mod, data in pairs(user_data) do if config.modules[mod] then - if type(data.enable) == 'boolean' then - config.modules[mod].enable = data.enable - end - if type(data.disable) == 'table' then - config.modules[mod].disable = data.disable - end - if config.modules[mod].keymaps and type(data.keymaps) == 'table' then - for f, map in pairs(data.keymaps) do - if config.modules[mod].keymaps[f] then - config.modules[mod].keymaps[f] = map - end - end - end + M.setup_module(config.modules[mod], data) elseif mod == 'ensure_installed' then config.ensure_installed = data require'nvim-treesitter.install'.ensure_installed(data) @@ -208,12 +230,55 @@ function M.setup(user_data) end end -function M.available_modules() - return vim.tbl_keys(config.modules) +--- Sets up a single module or all submodules of a group +-- @param mod the module or group of modules +-- @param data user defined configuration for the module +function M.setup_module(mod, data) + if M.is_module(mod) then + if type(data.enable) == 'boolean' then + mod.enable = data.enable + end + if type(data.disable) == 'table' then + mod.disable = data.disable + end + if mod.keymaps and type(data.keymaps) == 'table' then + for f, map in pairs(data.keymaps) do + if mod.keymaps[f] then + mod.keymaps[f] = map + end + end + end + elseif type(data) == 'table' and type(mod) == 'table' then + for key, value in pairs(data) do + M.setup_module(mod[key], value) + end + end end -function M.get_module(mod) - return config.modules[mod] +function M.available_modules() + local modules = {} + + recurse_modules(function(_, _, path) + table.insert(modules, path) + end) + + return modules +end + +-- Gets a module config by path +-- @param mod_path path to the module +-- @returns the module or nil +function M.get_module(mod_path) + local mod = utils.get_at_path(config.modules, mod_path) + + return M.is_module(mod) and mod or nil +end + +-- Determines whether the provided table is a module. +-- A module should contain an 'is_supported' function. +-- @param mod the module table +function M.is_module(mod) + return type(mod) == 'table' and type(mod.is_supported) == 'function' end return M diff --git a/lua/nvim-treesitter/info.lua b/lua/nvim-treesitter/info.lua index c768595c4..0c99f3ff2 100644 --- a/lua/nvim-treesitter/info.lua +++ b/lua/nvim-treesitter/info.lua @@ -65,7 +65,7 @@ local function print_info_modules(sorted_languages) end local function module_info(mod) - if mod and not configs.get_config()[mod] then return end + if mod and not configs.get_module(mod) then return end local parserlist = parsers.available_parsers() table.sort(parserlist, function(a, b) return #a > #b end) diff --git a/lua/nvim-treesitter/locals.lua b/lua/nvim-treesitter/locals.lua index 8c7d400eb..fae8195ea 100644 --- a/lua/nvim-treesitter/locals.lua +++ b/lua/nvim-treesitter/locals.lua @@ -11,7 +11,7 @@ local M = { locals = {} } -function M.collect_locals(bufnr) +function M.collect_locals(bufnr, root) local lang = parsers.ft_to_lang(api.nvim_buf_get_option(bufnr, "ft")) if not lang then return end @@ -21,7 +21,7 @@ function M.collect_locals(bufnr) local parser = parsers.get_parser(bufnr, lang) if not parser then return end - local root = parser:parse():root() + local root = root or parser:parse():root() local start_row, _, end_row, _ = root:range() local locals = {} diff --git a/lua/nvim-treesitter/query.lua b/lua/nvim-treesitter/query.lua index 42e835824..d04e368a6 100644 --- a/lua/nvim-treesitter/query.lua +++ b/lua/nvim-treesitter/query.lua @@ -13,6 +13,12 @@ local function read_query_files(filenames) return table.concat(contents, '\n') end +local function get_query_gaurd(query) + return function(lang) + return M.get_query(lang, query) ~= nil + end +end + -- Some treesitter grammars extend others. -- We can use that to import the queries of the base language M.base_language_map = { @@ -21,6 +27,9 @@ M.base_language_map = { tsx = {'typescript', 'javascript'}, } +M.has_locals = get_query_gaurd('locals') +M.has_highlights = get_query_gaurd('highlights') + function M.get_query(lang, query_name) local query_files = api.nvim_get_runtime_file(string.format('queries/%s/%s.scm', lang, query_name), true) local query_string = '' diff --git a/lua/nvim-treesitter/refactor/highlight_definitions.lua b/lua/nvim-treesitter/refactor/highlight_definitions.lua new file mode 100644 index 000000000..ef415bb77 --- /dev/null +++ b/lua/nvim-treesitter/refactor/highlight_definitions.lua @@ -0,0 +1,93 @@ +-- This module highlights reference usages and the corresponding +-- definition on cursor hold. + +local parsers = require'nvim-treesitter.parsers' +local ts_utils = require'nvim-treesitter.ts_utils' +local locals = require'nvim-treesitter.locals' +local api = vim.api +local cmd = api.nvim_command + +local M = {} + +local usage_namespace = api.nvim_create_namespace('nvim-treesitter-usages') + +local function find_usages(node, scope_node) + local usages = {} + local node_text = ts_utils.get_node_text(node)[1] + + if not node_text or #node_text < 1 then return end + + for _, def in ipairs(locals.collect_locals(bufnr, scope_node)) do + if def.reference + and def.reference.node + and ts_utils.get_node_text(def.reference.node)[1] == node_text then + + table.insert(usages, def.reference.node) + end + end + + return usages +end + +function M.highlight_usages(bufnr) + M.clear_usage_highlights(bufnr) + + local node_at_point = ts_utils.get_node_at_cursor() + + if not node_at_point then return end + + local def_node, scope = ts_utils.find_definition(node_at_point, bufnr) + local usages = find_usages(node_at_point, scope) + + for _, usage_node in ipairs(usages) do + local start_row, start_col, _, end_col = usage_node:range() + + if usage_node ~= node_at_point then + api.nvim_buf_add_highlight( + bufnr, + usage_namespace, + 'Visual', + start_row, + start_col, + end_col) + end + end + + if def_node then + local start_row, start_col, _, end_col = def_node:range() + + if def_node ~= node_at_point then + api.nvim_buf_add_highlight( + bufnr, + usage_namespace, + 'Search', + start_row, + start_col, + end_col) + end + end +end + +function M.clear_usage_highlights(bufnr) + api.nvim_buf_clear_namespace(bufnr, usage_namespace, 0, -1) +end + +function M.attach(bufnr) + local bufnr = bufnr or api.nvim_get_current_buf() + + cmd(string.format('augroup NvimTreesitterUsages_%d', bufnr)) + cmd 'au!' + cmd(string.format([[autocmd CursorHold lua require'nvim-treesitter.refactor.highlight_definitions'.highlight_usages(%d)]], bufnr, bufnr)) + cmd(string.format([[autocmd CursorMoved lua require'nvim-treesitter.refactor.highlight_definitions'.clear_usage_highlights(%d)]], bufnr, bufnr)) + cmd(string.format([[autocmd InsertEnter lua require'nvim-treesitter.refactor.highlight_definitions'.clear_usage_highlights(%d)]], bufnr, bufnr)) + cmd 'augroup END' +end + +function M.detach(bufnr) + M.clear_usage_highlights(bufnr) + cmd(string.format('autocmd! NvimTreesitterUsages_%d CursorHold', bufnr)) + cmd(string.format('autocmd! NvimTreesitterUsages_%d CursorMoved', bufnr)) + cmd(string.format('autocmd! NvimTreesitterUsages_%d InsertEnter', bufnr)) +end + +return M diff --git a/lua/nvim-treesitter/ts_utils.lua b/lua/nvim-treesitter/ts_utils.lua index 687a3c539..363d26374 100644 --- a/lua/nvim-treesitter/ts_utils.lua +++ b/lua/nvim-treesitter/ts_utils.lua @@ -212,4 +212,55 @@ function M.get_node_at_cursor(winnr) return root:named_descendant_for_range(cursor[1]-1,cursor[2],cursor[1]-1,cursor[2]) end +-- Finds the definition node and it's scope node of a node +-- @param node starting node +-- @param bufnr buffer +-- @returns the definition node and the definition nodes scope node +function M.find_definition(node, bufnr) + local bufnr = bufnr or api.nvim_get_current_buf() + local node_text = M.get_node_text(node)[1] + local current_scope = M.containing_scope(node) + local _, _, node_start = node:start() + + -- If a scope wasn't found then use the root node + if current_scope == node then + current_scope = parsers.get_parser(bufnr).tree:root() + end + + while current_scope ~= nil and current_scope ~= node do + for _, def in ipairs(locals.collect_locals(bufnr, current_scope)) do + if def.definition then + for _, def_node in ipairs(M.get_local_nodes(def.definition)) do + local _, _, def_start = def_node:start() + + if M.get_node_text(def_node)[1] == node_text and def_start < node_start then + return def_node, current_scope + end + end + end + end + + current_scope = M.containing_scope(current_scope:parent()) + end + + return nil, nil +end + +-- Gets all nodes from a local list result. +-- @param local_def the local list result +-- @returns a list of nodes +function M.get_local_nodes(local_def) + if local_def.node then + return { local_def.node } + else + local result = {} + + for _, def in pairs(local_def) do + vim.list_extend(result, M.get_local_nodes(def)) + end + + return result + end +end + return M diff --git a/lua/nvim-treesitter/utils.lua b/lua/nvim-treesitter/utils.lua index 6fa159817..41833f8cf 100644 --- a/lua/nvim-treesitter/utils.lua +++ b/lua/nvim-treesitter/utils.lua @@ -45,4 +45,21 @@ function M.get_cache_dir() return nil, 'Invalid cache rights, $XDG_CACHE_HOME or /tmp should be read/write' end +--- Gets a property at path +-- @param tbl the table to access +-- @param path the '.' seperated path +-- @returns the value at path or nil +function M.get_at_path(tbl, path) + local segments = vim.split(path, '.', true) + local result = tbl + + for _, segment in ipairs(segments) do + if type(result) == 'table' then + result = result[segment] + end + end + + return result +end + return M diff --git a/queries/javascript/locals.scm b/queries/javascript/locals.scm index 165adfed9..d56000d5a 100644 --- a/queries/javascript/locals.scm +++ b/queries/javascript/locals.scm @@ -28,6 +28,9 @@ (variable_declarator name: (identifier) @definition) +(import_specifier + (identifier) @definition) + ; References ;------------ From 64838e51c0fcb9def4be912391a1544b4d9a9d27 Mon Sep 17 00:00:00 2001 From: Steven Sojka Date: Fri, 26 Jun 2020 11:11:21 -0500 Subject: [PATCH 60/87] feat(refactor): add smart rename module --- lua/nvim-treesitter/configs.lua | 8 ++ lua/nvim-treesitter/query.lua | 8 +- .../refactor/highlight_definitions.lua | 20 +---- lua/nvim-treesitter/refactor/smart_rename.lua | 75 +++++++++++++++++++ lua/nvim-treesitter/ts_utils.lua | 28 ++++++- 5 files changed, 115 insertions(+), 24 deletions(-) create mode 100644 lua/nvim-treesitter/refactor/smart_rename.lua diff --git a/lua/nvim-treesitter/configs.lua b/lua/nvim-treesitter/configs.lua index 30c2114ce..595c2e759 100644 --- a/lua/nvim-treesitter/configs.lua +++ b/lua/nvim-treesitter/configs.lua @@ -31,6 +31,14 @@ local config = { enable = false, disable = {}, is_supported = queries.has_locals + }, + smart_rename = { + enable = false, + disable = {}, + is_supported = queries.has_locals, + keymaps = { + smart_rename = "grr" + } } } }, diff --git a/lua/nvim-treesitter/query.lua b/lua/nvim-treesitter/query.lua index d04e368a6..6c76fc237 100644 --- a/lua/nvim-treesitter/query.lua +++ b/lua/nvim-treesitter/query.lua @@ -13,7 +13,9 @@ local function read_query_files(filenames) return table.concat(contents, '\n') end -local function get_query_gaurd(query) +-- Creates a function that checks whether a certain query exists +-- for a specific language. +local function get_query_guard(query) return function(lang) return M.get_query(lang, query) ~= nil end @@ -27,8 +29,8 @@ M.base_language_map = { tsx = {'typescript', 'javascript'}, } -M.has_locals = get_query_gaurd('locals') -M.has_highlights = get_query_gaurd('highlights') +M.has_locals = get_query_guard('locals') +M.has_highlights = get_query_guard('highlights') function M.get_query(lang, query_name) local query_files = api.nvim_get_runtime_file(string.format('queries/%s/%s.scm', lang, query_name), true) diff --git a/lua/nvim-treesitter/refactor/highlight_definitions.lua b/lua/nvim-treesitter/refactor/highlight_definitions.lua index ef415bb77..6279e4708 100644 --- a/lua/nvim-treesitter/refactor/highlight_definitions.lua +++ b/lua/nvim-treesitter/refactor/highlight_definitions.lua @@ -11,24 +11,6 @@ local M = {} local usage_namespace = api.nvim_create_namespace('nvim-treesitter-usages') -local function find_usages(node, scope_node) - local usages = {} - local node_text = ts_utils.get_node_text(node)[1] - - if not node_text or #node_text < 1 then return end - - for _, def in ipairs(locals.collect_locals(bufnr, scope_node)) do - if def.reference - and def.reference.node - and ts_utils.get_node_text(def.reference.node)[1] == node_text then - - table.insert(usages, def.reference.node) - end - end - - return usages -end - function M.highlight_usages(bufnr) M.clear_usage_highlights(bufnr) @@ -37,7 +19,7 @@ function M.highlight_usages(bufnr) if not node_at_point then return end local def_node, scope = ts_utils.find_definition(node_at_point, bufnr) - local usages = find_usages(node_at_point, scope) + local usages = ts_utils.find_usages(node_at_point, scope) for _, usage_node in ipairs(usages) do local start_row, start_col, _, end_col = usage_node:range() diff --git a/lua/nvim-treesitter/refactor/smart_rename.lua b/lua/nvim-treesitter/refactor/smart_rename.lua new file mode 100644 index 000000000..5b7562eb4 --- /dev/null +++ b/lua/nvim-treesitter/refactor/smart_rename.lua @@ -0,0 +1,75 @@ +-- Binds a keybinding to smart rename definitions and usages. +-- Can be used directly using the `smart_rename` function. + +local ts_utils = require'nvim-treesitter.ts_utils' +local configs = require'nvim-treesitter.configs' +local api = vim.api + +local M = {} + +function M.smart_rename(bufnr) + local bufnr = bufnr or api.nvim_get_current_buf() + local node_at_point = ts_utils.get_node_at_cursor() + + if not node_at_point then + print('No node to rename!') + return + end + + local node_text = ts_utils.get_node_text(node_at_point)[1] + local new_name = vim.fn.input('New name: ', node_text or '') + + -- Empty name cancels the interaction or ESC + if not new_name or #new_name < 1 then return end + + local definition, scope = ts_utils.find_definition(node_at_point, bufnr) + local nodes_to_rename = ts_utils.find_usages(node_at_point, scope) + + if not vim.tbl_contains(nodes_to_rename, node_at_point) then + table.insert(nodes_to_rename, node_at_point) + end + + if definition and not vim.tbl_contains(nodes_to_rename, definition) then + table.insert(nodes_to_rename, definition) + end + + if #nodes_to_rename < 1 then + print('No nodes to rename!') + return + end + + for _, node in ipairs(nodes_to_rename) do + local start_row, start_col, end_row, end_col = node:range() + + local line = api.nvim_buf_get_lines(bufnr, start_row, start_row + 1, false)[1] + + if line then + local new_line = line:sub(1, start_col) .. new_name .. line:sub(end_col + 1, -1) + + api.nvim_buf_set_lines(bufnr, start_row, start_row + 1, false, { new_line }) + end + end +end + +function M.attach(bufnr) + local bufnr = bufnr or api.nvim_get_current_buf() + + local config = configs.get_module('refactor.smart_rename') + + for fn_name, mapping in pairs(config.keymaps) do + local cmd = string.format([[:lua require'nvim-treesitter.refactor.smart_rename'.%s(%d)]], fn_name, bufnr) + + api.nvim_buf_set_keymap(bufnr, 'n', mapping, cmd, { silent = true }) + end +end + +function M.detach(bufnr) + local buf = bufnr or api.nvim_get_current_buf() + local config = configs.get_module('refactor.smart_rename') + + for fn_name, mapping in pairs(config.keymaps) do + api.nvim_buf_del_keymap(bufnr, 'n', mapping) + end +end + +return M diff --git a/lua/nvim-treesitter/ts_utils.lua b/lua/nvim-treesitter/ts_utils.lua index 363d26374..90dc3d262 100644 --- a/lua/nvim-treesitter/ts_utils.lua +++ b/lua/nvim-treesitter/ts_utils.lua @@ -15,14 +15,16 @@ function M.get_node_text(node, bufnr) -- We have to remember that end_col is end-exclusive local start_row, start_col, end_row, end_col = node:range() + if start_row ~= end_row then local lines = api.nvim_buf_get_lines(bufnr, start_row, end_row+1, false) lines[1] = string.sub(lines[1], start_col+1) lines[#lines] = string.sub(lines[#lines], 1, end_col) return lines else - local line = api.nvim_buf_get_lines(bufnr, start_row, start_row+1, true)[1] - return { string.sub(line, start_col+1, end_col) } + local line = api.nvim_buf_get_lines(bufnr, start_row, start_row+1, false)[1] + -- If line is nil then the line is empty + return line and { string.sub(line, start_col+1, end_col) } or {} end end @@ -263,4 +265,26 @@ function M.get_local_nodes(local_def) end end +-- Finds usages of a node in a particula scope +-- @param node the node to find usages for +-- @param scope_node the node to look within +-- @returns a list of nodes +function M.find_usages(node, scope_node) + local usages = {} + local node_text = M.get_node_text(node)[1] + + if not node_text or #node_text < 1 then return {} end + + for _, def in ipairs(locals.collect_locals(bufnr, scope_node)) do + if def.reference + and def.reference.node + and M.get_node_text(def.reference.node)[1] == node_text then + + table.insert(usages, def.reference.node) + end + end + + return usages +end + return M From 6f8e4c97a4f99b1a04cca5c41c333ffb5337d84a Mon Sep 17 00:00:00 2001 From: Steven Sojka Date: Fri, 26 Jun 2020 13:57:23 -0500 Subject: [PATCH 61/87] feat(refactor): add definition navigation module --- lua/nvim-treesitter/configs.lua | 9 +++ lua/nvim-treesitter/refactor/navigation.lua | 87 +++++++++++++++++++++ lua/nvim-treesitter/ts_utils.lua | 34 ++++++-- 3 files changed, 123 insertions(+), 7 deletions(-) create mode 100644 lua/nvim-treesitter/refactor/navigation.lua diff --git a/lua/nvim-treesitter/configs.lua b/lua/nvim-treesitter/configs.lua index 595c2e759..1d3e1c75e 100644 --- a/lua/nvim-treesitter/configs.lua +++ b/lua/nvim-treesitter/configs.lua @@ -39,6 +39,15 @@ local config = { keymaps = { smart_rename = "grr" } + }, + navigation = { + enable = false, + disable = {}, + is_supported = queries.has_locals, + keymaps = { + goto_definition = "gnd", + list_definitions = "gnD" + } } } }, diff --git a/lua/nvim-treesitter/refactor/navigation.lua b/lua/nvim-treesitter/refactor/navigation.lua new file mode 100644 index 000000000..9c439179c --- /dev/null +++ b/lua/nvim-treesitter/refactor/navigation.lua @@ -0,0 +1,87 @@ +-- Definition based navigation module + +local ts_utils = require'nvim-treesitter.ts_utils' +local locals = require'nvim-treesitter.locals' +local configs = require'nvim-treesitter.configs' +local api = vim.api + +local M = {} + +local function node_to_qf(node, kind) + local lnum, col, _ = def.node:start() + + return { + bufnr = bufnr, + lnum = lnum + 1, + col = col + 1, + text = ts_utils.get_node_text(def.node)[1] or '', + kind = kind + } +end + +function M.goto_definition(bufnr) + local bufnr = bufnr or api.nvim_get_current_buf() + local node_at_point = ts_utils.get_node_at_cursor() + + if not node_at_point then return end + + local definition, _ = ts_utils.find_definition(node_at_point, bufnr) + + if not definition then + print('No definition found') + return + end + + local start_row, start_col, _ = definition:start() + + api.nvim_win_set_cursor(0, { start_row + 1, start_col }) +end + +function M.list_definitions(bufnr) + local bufnr = bufnr or api.nvim_get_current_buf() + local definitions = locals.get_definitions(bufnr) + + if #definitions < 1 then return end + + local qf_list = {} + + for _, def in ipairs(definitions) do + ts_utils.recurse_local_nodes(def, function(_, node, _, match) + local lnum, col, _ = node:start() + + table.insert(qf_list, { + bufnr = bufnr, + lnum = lnum + 1, + col = col + 1, + text = ts_utils.get_node_text(node)[1] or "", + kind = match and match:sub(1, 1) or "" + }) + end) + end + + vim.fn.setqflist(qf_list, 'r') + api.nvim_command('copen') +end + +function M.attach(bufnr) + local bufnr = bufnr or api.nvim_get_current_buf() + + local config = configs.get_module('refactor.navigation') + + for fn_name, mapping in pairs(config.keymaps) do + local cmd = string.format([[:lua require'nvim-treesitter.refactor.navigation'.%s(%d)]], fn_name, bufnr) + + api.nvim_buf_set_keymap(bufnr, 'n', mapping, cmd, { silent = true }) + end +end + +function M.detach(bufnr) + local buf = bufnr or api.nvim_get_current_buf() + local config = configs.get_module('refactor.navigation') + + for fn_name, mapping in pairs(config.keymaps) do + api.nvim_buf_del_keymap(bufnr, 'n', mapping) + end +end + +return M diff --git a/lua/nvim-treesitter/ts_utils.lua b/lua/nvim-treesitter/ts_utils.lua index 90dc3d262..9720be7bf 100644 --- a/lua/nvim-treesitter/ts_utils.lua +++ b/lua/nvim-treesitter/ts_utils.lua @@ -252,16 +252,36 @@ end -- @param local_def the local list result -- @returns a list of nodes function M.get_local_nodes(local_def) + local result = {} + + M.recurse_local_nodes(local_def, function(_, node) + table.insert(result, node) + end) + + return result +end + +-- Recurse locals results until a node is found. +-- The accumulator function is given +-- * The table of the node +-- * The node +-- * The full definition match `@definition.var.something` -> 'var.something' +-- * The last definition match `@definition.var.something` -> 'something' +-- @param The locals result +-- @param The accumulator function +-- @param The full match path to append to +-- @param The last match +function M.recurse_local_nodes(local_def, accumulator, full_match, last_match) if local_def.node then - return { local_def.node } + accumulator(local_def, local_def.node, full_match, last_match) else - local result = {} - - for _, def in pairs(local_def) do - vim.list_extend(result, M.get_local_nodes(def)) + for match_key, def in pairs(local_def) do + M.recurse_local_nodes( + def, + accumulator, + full_match and (full_match..'.'..match_key) or match_key, + match_key) end - - return result end end From d73500eaa6b25edf476d73d0d1a47c65043b6e88 Mon Sep 17 00:00:00 2001 From: Steven Sojka Date: Mon, 29 Jun 2020 09:58:51 -0500 Subject: [PATCH 62/87] refactor(refactor): use higher local apis and some cleanup --- README.md | 26 +++++++- lua/nvim-treesitter/configs.lua | 22 +++---- lua/nvim-treesitter/locals.lua | 4 +- .../refactor/highlight_definitions.lua | 25 ++++---- lua/nvim-treesitter/refactor/navigation.lua | 6 -- lua/nvim-treesitter/refactor/smart_rename.lua | 14 +---- lua/nvim-treesitter/ts_utils.lua | 61 ++++++++++++------- lua/nvim-treesitter/utils.lua | 8 ++- plugin/nvim-treesitter.vim | 3 + queries/javascript/locals.scm | 3 - 10 files changed, 103 insertions(+), 69 deletions(-) diff --git a/README.md b/README.md index 54d17abce..3ea15b95f 100644 --- a/README.md +++ b/README.md @@ -102,9 +102,23 @@ require'nvim-treesitter.configs'.setup { init_selection = 'gnn', -- maps in normal mode to init the node/scope selection node_incremental = "grn", -- increment to the upper named parent scope_incremental = "grc", -- increment to the upper scope (as defined in locals.scm) - node_decremental = "grm", -- decrement to the previous node + node_decremental = "grm", -- decrement to the previous node } }, + refactor = { + highlight_defintions = { + enable = true + }, + smart_rename = { + enable = true, + smart_rename = "grr" -- mapping to rename reference under cursor + }, + navigation = { + enable = true, + goto_definition = "gnd", -- mapping to go to definition of symbol under cursor + list_definitions = "gnD" -- mapping to list all definitions in current file + } + }, ensure_installed = 'all' -- one of 'all', 'language', or a list of languages } EOF @@ -134,6 +148,16 @@ Some of these features are : You can find the roadmap [here](https://github.com/nvim-treesitter/nvim-treesitter/projects/1). The roadmap and all features of this plugin are open to change, and any suggestion will be highly appreciated! +## Available Modules + +- `highlight`: Consistent syntax highlighting. +- `incremental_selection`: Syntax based selection. +- `refactor.highlight_definitions`: Syntax based definition and usage highlighting. +- `refactor.smart_rename`: Syntax based definition and usage renaming. +- `refactor.navigation`: Syntax based definition listing and navigation. + * List all definitions + * Go to definition + ## Utils you can get some utility functions with diff --git a/lua/nvim-treesitter/configs.lua b/lua/nvim-treesitter/configs.lua index 1d3e1c75e..da80c818d 100644 --- a/lua/nvim-treesitter/configs.lua +++ b/lua/nvim-treesitter/configs.lua @@ -237,21 +237,19 @@ end function M.setup(user_data) if not user_data then return end - for mod, data in pairs(user_data) do - if config.modules[mod] then - M.setup_module(config.modules[mod], data) - elseif mod == 'ensure_installed' then - config.ensure_installed = data - require'nvim-treesitter.install'.ensure_installed(data) - end - end + M.setup_module(config.modules, user_data) end ---- Sets up a single module or all submodules of a group +-- Sets up a single module or all submodules of a group. +-- Note, this method is recursive. -- @param mod the module or group of modules -- @param data user defined configuration for the module -function M.setup_module(mod, data) - if M.is_module(mod) then +-- @param mod_name name of the module if it exists +function M.setup_module(mod, data, mod_name) + if mod_name == 'ensure_installed' then + config.ensure_installed = data + require'nvim-treesitter.install'.ensure_installed(data) + elseif M.is_module(mod) then if type(data.enable) == 'boolean' then mod.enable = data.enable end @@ -267,7 +265,7 @@ function M.setup_module(mod, data) end elseif type(data) == 'table' and type(mod) == 'table' then for key, value in pairs(data) do - M.setup_module(mod[key], value) + M.setup_module(mod[key], value, key) end end end diff --git a/lua/nvim-treesitter/locals.lua b/lua/nvim-treesitter/locals.lua index fae8195ea..8c7d400eb 100644 --- a/lua/nvim-treesitter/locals.lua +++ b/lua/nvim-treesitter/locals.lua @@ -11,7 +11,7 @@ local M = { locals = {} } -function M.collect_locals(bufnr, root) +function M.collect_locals(bufnr) local lang = parsers.ft_to_lang(api.nvim_buf_get_option(bufnr, "ft")) if not lang then return end @@ -21,7 +21,7 @@ function M.collect_locals(bufnr, root) local parser = parsers.get_parser(bufnr, lang) if not parser then return end - local root = root or parser:parse():root() + local root = parser:parse():root() local start_row, _, end_row, _ = root:range() local locals = {} diff --git a/lua/nvim-treesitter/refactor/highlight_definitions.lua b/lua/nvim-treesitter/refactor/highlight_definitions.lua index 6279e4708..bdbec1588 100644 --- a/lua/nvim-treesitter/refactor/highlight_definitions.lua +++ b/lua/nvim-treesitter/refactor/highlight_definitions.lua @@ -15,8 +15,11 @@ function M.highlight_usages(bufnr) M.clear_usage_highlights(bufnr) local node_at_point = ts_utils.get_node_at_cursor() + local references = locals.get_references(bufnr) - if not node_at_point then return end + if not node_at_point or not vim.tbl_contains(references, node_at_point) then + return + end local def_node, scope = ts_utils.find_definition(node_at_point, bufnr) local usages = ts_utils.find_usages(node_at_point, scope) @@ -28,25 +31,23 @@ function M.highlight_usages(bufnr) api.nvim_buf_add_highlight( bufnr, usage_namespace, - 'Visual', + 'TSDefinitionUsage', start_row, start_col, end_col) end end - if def_node then + if def_node ~= node_at_point then local start_row, start_col, _, end_col = def_node:range() - if def_node ~= node_at_point then - api.nvim_buf_add_highlight( - bufnr, - usage_namespace, - 'Search', - start_row, - start_col, - end_col) - end + api.nvim_buf_add_highlight( + bufnr, + usage_namespace, + 'TSDefinition', + start_row, + start_col, + end_col) end end diff --git a/lua/nvim-treesitter/refactor/navigation.lua b/lua/nvim-treesitter/refactor/navigation.lua index 9c439179c..5fd25e474 100644 --- a/lua/nvim-treesitter/refactor/navigation.lua +++ b/lua/nvim-treesitter/refactor/navigation.lua @@ -26,12 +26,6 @@ function M.goto_definition(bufnr) if not node_at_point then return end local definition, _ = ts_utils.find_definition(node_at_point, bufnr) - - if not definition then - print('No definition found') - return - end - local start_row, start_col, _ = definition:start() api.nvim_win_set_cursor(0, { start_row + 1, start_col }) diff --git a/lua/nvim-treesitter/refactor/smart_rename.lua b/lua/nvim-treesitter/refactor/smart_rename.lua index 5b7562eb4..8aab9538d 100644 --- a/lua/nvim-treesitter/refactor/smart_rename.lua +++ b/lua/nvim-treesitter/refactor/smart_rename.lua @@ -3,6 +3,7 @@ local ts_utils = require'nvim-treesitter.ts_utils' local configs = require'nvim-treesitter.configs' +local utils = require'nvim-treesitter.utils' local api = vim.api local M = {} @@ -12,7 +13,7 @@ function M.smart_rename(bufnr) local node_at_point = ts_utils.get_node_at_cursor() if not node_at_point then - print('No node to rename!') + utils.print_warning("No node to rename!") return end @@ -29,23 +30,16 @@ function M.smart_rename(bufnr) table.insert(nodes_to_rename, node_at_point) end - if definition and not vim.tbl_contains(nodes_to_rename, definition) then + if not vim.tbl_contains(nodes_to_rename, definition) then table.insert(nodes_to_rename, definition) end - if #nodes_to_rename < 1 then - print('No nodes to rename!') - return - end - for _, node in ipairs(nodes_to_rename) do local start_row, start_col, end_row, end_col = node:range() - local line = api.nvim_buf_get_lines(bufnr, start_row, start_row + 1, false)[1] if line then local new_line = line:sub(1, start_col) .. new_name .. line:sub(end_col + 1, -1) - api.nvim_buf_set_lines(bufnr, start_row, start_row + 1, false, { new_line }) end end @@ -53,12 +47,10 @@ end function M.attach(bufnr) local bufnr = bufnr or api.nvim_get_current_buf() - local config = configs.get_module('refactor.smart_rename') for fn_name, mapping in pairs(config.keymaps) do local cmd = string.format([[:lua require'nvim-treesitter.refactor.smart_rename'.%s(%d)]], fn_name, bufnr) - api.nvim_buf_set_keymap(bufnr, 'n', mapping, cmd, { silent = true }) end end diff --git a/lua/nvim-treesitter/ts_utils.lua b/lua/nvim-treesitter/ts_utils.lua index 9720be7bf..06f92c885 100644 --- a/lua/nvim-treesitter/ts_utils.lua +++ b/lua/nvim-treesitter/ts_utils.lua @@ -223,29 +223,33 @@ function M.find_definition(node, bufnr) local node_text = M.get_node_text(node)[1] local current_scope = M.containing_scope(node) local _, _, node_start = node:start() + local matching_def_nodes = {} -- If a scope wasn't found then use the root node if current_scope == node then current_scope = parsers.get_parser(bufnr).tree:root() end - - while current_scope ~= nil and current_scope ~= node do - for _, def in ipairs(locals.collect_locals(bufnr, current_scope)) do - if def.definition then - for _, def_node in ipairs(M.get_local_nodes(def.definition)) do - local _, _, def_start = def_node:start() - if M.get_node_text(def_node)[1] == node_text and def_start < node_start then - return def_node, current_scope - end - end + -- Get all definitions that match the node text + for _, def in ipairs(locals.get_definitions(bufnr)) do + for _, def_node in ipairs(M.get_local_nodes(def)) do + if M.get_node_text(def_node)[1] == node_text then + table.insert(matching_def_nodes, def_node) end end + end + -- Continue up each scope until we find the scope that contains the definition + while current_scope do + for _, def_node in ipairs(matching_def_nodes) do + if M.is_parent(current_scope, def_node) then + return def_node, current_scope + end + end current_scope = M.containing_scope(current_scope:parent()) end - return nil, nil + return node, parsers.get_parser(bufnr).tree:root() end -- Gets all nodes from a local list result. @@ -285,26 +289,41 @@ function M.recurse_local_nodes(local_def, accumulator, full_match, last_match) end end --- Finds usages of a node in a particula scope +-- Finds usages of a node in a given scope -- @param node the node to find usages for -- @param scope_node the node to look within -- @returns a list of nodes -function M.find_usages(node, scope_node) - local usages = {} +function M.find_usages(node, scope_node, bufnr) + local bufnr = bufnr or api.nvim_get_current_buf() local node_text = M.get_node_text(node)[1] if not node_text or #node_text < 1 then return {} end - for _, def in ipairs(locals.collect_locals(bufnr, scope_node)) do - if def.reference - and def.reference.node - and M.get_node_text(def.reference.node)[1] == node_text then - - table.insert(usages, def.reference.node) + local scope_node = scope_node or parsers.get_parser(bufnr).tree:root() + local references = locals.get_references(bufnr) + local usages = {} + + M.recurse_tree(scope_node, function(iter_node, _, next) + if vim.tbl_contains(references, iter_node) and M.get_node_text(iter_node)[1] == node_text then + table.insert(usages, iter_node) end - end + next() + end) return usages end +-- Recurses all child nodes of a tree. +-- The callback is provided the child node, parent_node, and a callback to recurse into +-- the child node. This allows for the ability to short circuit the recursion +-- if we found what we are looking for, we can then stop the recursion or skip a node +-- if need be. +-- @param tree the node root +-- @param cb the callback for each node +function M.recurse_tree(tree, cb) + for _, child in ipairs(M.get_named_children(tree)) do + cb(child, tree, function(next_node) M.recurse_tree(next_node or child, cb) end) + end +end + return M diff --git a/lua/nvim-treesitter/utils.lua b/lua/nvim-treesitter/utils.lua index 41833f8cf..f5c65a0a7 100644 --- a/lua/nvim-treesitter/utils.lua +++ b/lua/nvim-treesitter/utils.lua @@ -45,7 +45,7 @@ function M.get_cache_dir() return nil, 'Invalid cache rights, $XDG_CACHE_HOME or /tmp should be read/write' end ---- Gets a property at path +-- Gets a property at path -- @param tbl the table to access -- @param path the '.' seperated path -- @returns the value at path or nil @@ -62,4 +62,10 @@ function M.get_at_path(tbl, path) return result end +-- Prints a warning message +-- @param text the text message +function M.print_warning(text) + api.nvim_command(string.format([[echohl WarningMsg | echo "%s" | echohl None]], text)) +end + return M diff --git a/plugin/nvim-treesitter.vim b/plugin/nvim-treesitter.vim index 24a8eceb6..3c115bf77 100644 --- a/plugin/nvim-treesitter.vim +++ b/plugin/nvim-treesitter.vim @@ -57,3 +57,6 @@ highlight default link TSTypeBuiltin Type highlight default link TSStructure Structure highlight default link TSInclude Include +highlight default link TSDefinitionUsage Visual +highlight default link TSDefinition Search + diff --git a/queries/javascript/locals.scm b/queries/javascript/locals.scm index d56000d5a..165adfed9 100644 --- a/queries/javascript/locals.scm +++ b/queries/javascript/locals.scm @@ -28,9 +28,6 @@ (variable_declarator name: (identifier) @definition) -(import_specifier - (identifier) @definition) - ; References ;------------ From fa7a270da14f30eaa407a1813d1dfef78d6c031e Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Tue, 30 Jun 2020 07:44:35 +0200 Subject: [PATCH 63/87] Java highlights: Add parameter highlight for single-parameter lambdas --- queries/java/highlights.scm | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/queries/java/highlights.scm b/queries/java/highlights.scm index ac582c042..ed019775b 100644 --- a/queries/java/highlights.scm +++ b/queries/java/highlights.scm @@ -14,7 +14,10 @@ (formal_parameter name: (identifier) @parameter) -(inferred_parameters (identifier) @parameter) +;; Lambda parameter +(inferred_parameters (identifier) @parameter) ; (x,y) -> ... +(lambda_expression + parameters: (identifier) @parameter) ; x -> ... ; Annotations From 2b6b1e3e6159bd689dad677c50a5ab0189df0e08 Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Tue, 30 Jun 2020 07:48:55 +0200 Subject: [PATCH 64/87] Java highlights: Add operators ":" "?" --- queries/java/highlights.scm | 2 ++ 1 file changed, 2 insertions(+) diff --git a/queries/java/highlights.scm b/queries/java/highlights.scm index ed019775b..5f942a296 100644 --- a/queries/java/highlights.scm +++ b/queries/java/highlights.scm @@ -34,6 +34,8 @@ [ "@" "+" +"?" +":" "++" "-" "--" From ce119de2e3a85ed7f0b081c6733ae167c2fc5599 Mon Sep 17 00:00:00 2001 From: kiyan42 Date: Mon, 29 Jun 2020 16:07:16 +0200 Subject: [PATCH 65/87] update installer with sync and some fixes - add sync method for installing using `system` - remove `descriptions` in command configs - use install(lang) in ensure_installed and make it compatible --- lua/nvim-treesitter/configs.lua | 12 +-- lua/nvim-treesitter/info.lua | 6 +- lua/nvim-treesitter/install.lua | 139 ++++++++++++++++++++------------ 3 files changed, 92 insertions(+), 65 deletions(-) diff --git a/lua/nvim-treesitter/configs.lua b/lua/nvim-treesitter/configs.lua index da80c818d..c31ca4f29 100644 --- a/lua/nvim-treesitter/configs.lua +++ b/lua/nvim-treesitter/configs.lua @@ -184,32 +184,28 @@ M.commands = { args = { "-nargs=1", "-complete=custom,v:lua.ts_available_modules" - }, - description = '`:TSBufEnable module_name` enable a specified module on the current buffer' + } }, TSBufDisable = { run = disable_module, args = { "-nargs=1", "-complete=custom,v:lua.ts_available_modules" - }, - description = '`:TSBufDisable module_name` disable a specified module on the current buffer' + } }, TSEnableAll = { run = enable_all, args = { "-nargs=+", "-complete=custom,v:lua.ts_available_modules" - }, - description = '`:TSEnableAll module_name (filetype)` enables a specified module on all buffers. If filetype is specified, enable only for specified filetype' + } }, TSDisableAll = { run = disable_all, args = { "-nargs=+", "-complete=custom,v:lua.ts_available_modules" - }, - description = '`:TSDisableAll module_name (filetype)` disables a specified module on all buffers. If filetype is specified, disable only for specified filetype' + } }, } diff --git a/lua/nvim-treesitter/info.lua b/lua/nvim-treesitter/info.lua index 0c99f3ff2..98b759518 100644 --- a/lua/nvim-treesitter/info.lua +++ b/lua/nvim-treesitter/info.lua @@ -81,16 +81,14 @@ M.commands = { run = install_info, args = { "-nargs=0", - }, - description = '`:TSInstallInfo` print installation state for every filetype' + } }, TSModuleInfo = { run = module_info, args = { "-nargs=?", "-complete=custom,v:lua.ts_available_modules" - }, - description = '`:TSModuleInfo` print module state for every filetype, if module is specified, only for current module' + } } } diff --git a/lua/nvim-treesitter/install.lua b/lua/nvim-treesitter/install.lua index 686688bf7..e91987120 100644 --- a/lua/nvim-treesitter/install.lua +++ b/lua/nvim-treesitter/install.lua @@ -22,7 +22,42 @@ local function iter_cmd(cmd_list, i, lang) end)) end -local function run_install(cache_folder, package_path, lang, repo) +local function get_command(cmd) + local ret = '' + if cmd.opts and cmd.opts.cwd then + ret = string.format('cd %s;\n', cmd.opts.cwd) + end + + ret = string.format('%s%s ', ret, cmd.cmd) + + local options = "" + if cmd.opts and cmd.opts.args then + for _, opt in ipairs(cmd.opts.args) do + options = string.format("%s %s", options, opt) + end + end + + return string.format('%s%s', ret, options) +end + +local function iter_cmd_sync(cmd_list) + for _, cmd in ipairs(cmd_list) do + if cmd.info then + print(cmd.info) + end + + vim.fn.system(get_command(cmd)) + if vim.v.shell_error ~= 0 then + api.nvim_err_writeln(cmd.err) + return false + end + + end + + return true +end + +local function run_install(cache_folder, package_path, lang, repo, with_sync) local project_name = 'tree-sitter-'..lang local project_repo = cache_folder..'/'..project_name -- compile_location only needed for typescript installs. @@ -33,7 +68,7 @@ local function run_install(cache_folder, package_path, lang, repo) cmd = 'rm', opts = { args = { '-rf', project_repo }, - } + }, }, { cmd = 'git', @@ -76,83 +111,81 @@ local function run_install(cache_folder, package_path, lang, repo) } } - iter_cmd(command_list, 1, lang) + if with_sync then + if iter_cmd_sync(command_list, lang) == true then + print('Treesitter parser for '..lang..' has been installed') + end + else + iter_cmd(command_list, 1, lang) + end end -- TODO(kyazdani): this should work on windows too -local function install(...) - if fn.has('win32') == 1 then - return api.nvim_err_writeln('This command is not available on windows at the moment.') - end +local function install(with_sync) + return function (...) + if fn.has('win32') == 1 then + return api.nvim_err_writeln('This command is not available on windows at the moment.') + end - if fn.executable('git') == 0 then - return api.nvim_err_writeln('Git is required on your system to run this command') - end + if fn.executable('git') == 0 then + return api.nvim_err_writeln('Git is required on your system to run this command') + end - local package_path, err = utils.get_package_path() - if err then return api.nvim_err_writeln(err) end + local package_path, err = utils.get_package_path() + if err then return api.nvim_err_writeln(err) end - local cache_folder, err = utils.get_cache_dir() - if err then return api.nvim_err_writeln(err) end + local cache_folder, err = utils.get_cache_dir() + if err then return api.nvim_err_writeln(err) end - local languages = { ... } - local check_installed = true - if ... == 'all' then - languages = parsers.available_parsers() - check_installed = false - end + local languages = vim.tbl_flatten({...}) + local ask_reinstall = true + if ... == 'all' then + languages = parsers.available_parsers() + ask_reinstall = false + end - for _, lang in ipairs(languages) do - if check_installed then + for _, lang in ipairs(languages) do if #api.nvim_get_runtime_file('parser/'..lang..'.so', false) > 0 then + if not ask_reinstall then goto continue end + local yesno = fn.input(lang .. ' parser already available: would you like to reinstall ? y/n: ') print('\n ') -- mandatory to avoid messing up command line if not string.match(yesno, '^y.*') then goto continue end end - end - local parser_config = parsers.get_parser_configs()[lang] - if not parser_config then - return api.nvim_err_writeln('Parser not available for language '..lang) - end + local parser_config = parsers.get_parser_configs()[lang] + if not parser_config then + return api.nvim_err_writeln('Parser not available for language '..lang) + end - local install_info = parser_config.install_info - vim.validate { - url={ install_info.url, 'string' }, - files={ install_info.files, 'table' } - } + local install_info = parser_config.install_info + vim.validate { + url={ install_info.url, 'string' }, + files={ install_info.files, 'table' } + } - run_install(cache_folder, package_path, lang, install_info) - ::continue:: - end -end - - -M.ensure_installed = function(languages) - if type(languages) == 'string' then - if languages == 'all' then - languages = parsers.available_parsers() - else - languages = {languages} - end - end - - for _, lang in ipairs(languages) do - if not parsers.has_parser(lang) then - install(lang) + run_install(cache_folder, package_path, lang, install_info, with_sync) + ::continue:: end end end +M.ensure_installed = install(false) M.commands = { TSInstall = { - run = install, + run = install(false), args = { "-nargs=+", "-complete=custom,v:lua.ts_installable_parsers" - }, - description = '`:TSInstall {lang}` installs a parser under nvim-treesitter/parser/{lang}.so' + } + }, + TSInstallSync = { + run = install(true), + args = { + "-nargs=+", + "-complete=custom,v:lua.ts_installable_parsers" + } } } From 2a20484a1454d7ba91644b00505df1f4457b94d6 Mon Sep 17 00:00:00 2001 From: kiyan42 Date: Mon, 29 Jun 2020 16:16:18 +0200 Subject: [PATCH 66/87] fix: remove goto statements --- lua/nvim-treesitter/install.lua | 45 ++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/lua/nvim-treesitter/install.lua b/lua/nvim-treesitter/install.lua index e91987120..3805750b4 100644 --- a/lua/nvim-treesitter/install.lua +++ b/lua/nvim-treesitter/install.lua @@ -120,6 +120,29 @@ local function run_install(cache_folder, package_path, lang, repo, with_sync) end end +local function install_lang(lang, ask_reinstall, cache_folder, package_path, with_sync) + if #api.nvim_get_runtime_file('parser/'..lang..'.so', false) > 0 then + if not ask_reinstall then return end + + local yesno = fn.input(lang .. ' parser already available: would you like to reinstall ? y/n: ') + print('\n ') -- mandatory to avoid messing up command line + if not string.match(yesno, '^y.*') then return end + end + + local parser_config = parsers.get_parser_configs()[lang] + if not parser_config then + return api.nvim_err_writeln('Parser not available for language '..lang) + end + + local install_info = parser_config.install_info + vim.validate { + url={ install_info.url, 'string' }, + files={ install_info.files, 'table' } + } + + run_install(cache_folder, package_path, lang, install_info, with_sync) +end + -- TODO(kyazdani): this should work on windows too local function install(with_sync) return function (...) @@ -145,27 +168,7 @@ local function install(with_sync) end for _, lang in ipairs(languages) do - if #api.nvim_get_runtime_file('parser/'..lang..'.so', false) > 0 then - if not ask_reinstall then goto continue end - - local yesno = fn.input(lang .. ' parser already available: would you like to reinstall ? y/n: ') - print('\n ') -- mandatory to avoid messing up command line - if not string.match(yesno, '^y.*') then goto continue end - end - - local parser_config = parsers.get_parser_configs()[lang] - if not parser_config then - return api.nvim_err_writeln('Parser not available for language '..lang) - end - - local install_info = parser_config.install_info - vim.validate { - url={ install_info.url, 'string' }, - files={ install_info.files, 'table' } - } - - run_install(cache_folder, package_path, lang, install_info, with_sync) - ::continue:: + install_lang(lang, ask_reinstall, cache_folder, package_path, with_sync) end end end From 0611f432aae7794a23f2a4ee5d2d035e7d490b3e Mon Sep 17 00:00:00 2001 From: kiyan42 Date: Mon, 29 Jun 2020 16:58:33 +0200 Subject: [PATCH 67/87] if/else in defining languages --- lua/nvim-treesitter/install.lua | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lua/nvim-treesitter/install.lua b/lua/nvim-treesitter/install.lua index 3805750b4..7be01b6ee 100644 --- a/lua/nvim-treesitter/install.lua +++ b/lua/nvim-treesitter/install.lua @@ -160,11 +160,14 @@ local function install(with_sync) local cache_folder, err = utils.get_cache_dir() if err then return api.nvim_err_writeln(err) end - local languages = vim.tbl_flatten({...}) - local ask_reinstall = true + local languages + local ask_reinstall if ... == 'all' then languages = parsers.available_parsers() ask_reinstall = false + else + languages = vim.tbl_flatten({...}) + ask_reinstall = true end for _, lang in ipairs(languages) do From 2bb6374c344e76ca6cafc38696dd67e641a02876 Mon Sep 17 00:00:00 2001 From: kiyan42 Date: Mon, 29 Jun 2020 16:51:38 +0200 Subject: [PATCH 68/87] Define multiple query for a language Allows using another query file for a language, or use a query file from another language --- lua/nvim-treesitter/query.lua | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lua/nvim-treesitter/query.lua b/lua/nvim-treesitter/query.lua index 6c76fc237..72cead9dd 100644 --- a/lua/nvim-treesitter/query.lua +++ b/lua/nvim-treesitter/query.lua @@ -29,6 +29,11 @@ M.base_language_map = { tsx = {'typescript', 'javascript'}, } +M.query_extensions = { + javascript = { 'jsx' }, + tsx = {'javascript.jsx'} +} + M.has_locals = get_query_guard('locals') M.has_highlights = get_query_guard('highlights') @@ -47,6 +52,20 @@ function M.get_query(lang, query_name) end end + local extensions = M.query_extensions[lang] + for _, ext in ipairs(extensions or {}) do + local l = lang + local e = ext + if e:match('%.') ~= nil then + l = e:match('.*%.'):sub(0, -2) + e = e:match('%..*'):sub(2, -1) + end + local ext_files = api.nvim_get_runtime_file(string.format('queries/%s/%s.scm', l, e), true) + if ext_files and #ext_files > 0 then + query_string = read_query_files(ext_files) .. "\n" .. query_string + end + end + if #query_string > 0 then return ts.parse_query(lang, query_string) end From c70a8242eb346e77f2b554c7fa207a767583ffb4 Mon Sep 17 00:00:00 2001 From: kiyan42 Date: Tue, 30 Jun 2020 08:51:52 +0200 Subject: [PATCH 69/87] fix ask install --- lua/nvim-treesitter/install.lua | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lua/nvim-treesitter/install.lua b/lua/nvim-treesitter/install.lua index 7be01b6ee..f2972005c 100644 --- a/lua/nvim-treesitter/install.lua +++ b/lua/nvim-treesitter/install.lua @@ -144,7 +144,7 @@ local function install_lang(lang, ask_reinstall, cache_folder, package_path, wit end -- TODO(kyazdani): this should work on windows too -local function install(with_sync) +local function install(with_sync, ask_reinstall) return function (...) if fn.has('win32') == 1 then return api.nvim_err_writeln('This command is not available on windows at the moment.') @@ -161,33 +161,33 @@ local function install(with_sync) if err then return api.nvim_err_writeln(err) end local languages - local ask_reinstall + local ask if ... == 'all' then languages = parsers.available_parsers() - ask_reinstall = false + ask = false else languages = vim.tbl_flatten({...}) - ask_reinstall = true + ask = ask_reinstall end for _, lang in ipairs(languages) do - install_lang(lang, ask_reinstall, cache_folder, package_path, with_sync) + install_lang(lang, ask, cache_folder, package_path, with_sync) end end end -M.ensure_installed = install(false) +M.ensure_installed = install(false, false) M.commands = { TSInstall = { - run = install(false), + run = install(false, true), args = { "-nargs=+", "-complete=custom,v:lua.ts_installable_parsers" } }, TSInstallSync = { - run = install(true), + run = install(true, true), args = { "-nargs=+", "-complete=custom,v:lua.ts_installable_parsers" From d7240734011cd31cf4fbb95b996c9d407fb57d28 Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Fri, 26 Jun 2020 17:28:23 +0200 Subject: [PATCH 70/87] C highlights: Make ? an operator in c highlights --- queries/c/highlights.scm | 1 + 1 file changed, 1 insertion(+) diff --git a/queries/c/highlights.scm b/queries/c/highlights.scm index b3889dbd8..4bb3ff745 100644 --- a/queries/c/highlights.scm +++ b/queries/c/highlights.scm @@ -51,6 +51,7 @@ ">=" @operator "!" @operator "||" @operator +"?" @operator "-=" @operator "+=" @operator From 255d1756b0e72840ccc5782ff2df2d30f17b33f5 Mon Sep 17 00:00:00 2001 From: Thomas Vigouroux <39092278+vigoux@users.noreply.github.com> Date: Tue, 30 Jun 2020 08:54:24 +0200 Subject: [PATCH 71/87] highlights(c): highlight ternary as conditional --- queries/c/highlights.scm | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/queries/c/highlights.scm b/queries/c/highlights.scm index 4bb3ff745..5a79a2b5e 100644 --- a/queries/c/highlights.scm +++ b/queries/c/highlights.scm @@ -51,7 +51,7 @@ ">=" @operator "!" @operator "||" @operator -"?" @operator +(conditional_expression [ "?" ":" ] @conditional) "-=" @operator "+=" @operator @@ -113,4 +113,3 @@ (identifier)) @parameter (ERROR) @error - From 4757636995ce2c31a5b11b16068efbd25ff0adee Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Tue, 30 Jun 2020 17:30:18 +0200 Subject: [PATCH 72/87] Rgenerate doc/tags --- doc/tags | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/tags b/doc/tags index 7cab44634..5d8e88196 100644 --- a/doc/tags +++ b/doc/tags @@ -12,6 +12,7 @@ hl-TSConstBuiltin nvim-treesitter.txt /*hl-TSConstBuiltin* hl-TSConstMacro nvim-treesitter.txt /*hl-TSConstMacro* hl-TSConstant nvim-treesitter.txt /*hl-TSConstant* hl-TSConstructor nvim-treesitter.txt /*hl-TSConstructor* +hl-TSError nvim-treesitter.txt /*hl-TSError* hl-TSException nvim-treesitter.txt /*hl-TSException* hl-TSField nvim-treesitter.txt /*hl-TSField* hl-TSFloat nvim-treesitter.txt /*hl-TSFloat* From 817abb3688301510d0399c4f95fc07b71269cbf6 Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Wed, 1 Jul 2020 11:47:12 +0200 Subject: [PATCH 73/87] Fix #136: Highlight of TSFloat should be Float not TSFloat --- plugin/nvim-treesitter.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/nvim-treesitter.vim b/plugin/nvim-treesitter.vim index 3c115bf77..2bebbba99 100644 --- a/plugin/nvim-treesitter.vim +++ b/plugin/nvim-treesitter.vim @@ -34,7 +34,7 @@ highlight default link TSStringEscape SpecialChar highlight default link TSCharacter Character highlight default link TSNumber Number highlight default link TSBoolean Boolean -highlight default link TSFloat TSFloat +highlight default link TSFloat Float highlight default link TSFunction Function highlight default link TSFuncBuiltin Special From 61803eb523cc935797715c90241b6b8b024c8008 Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Wed, 1 Jul 2020 11:42:38 +0200 Subject: [PATCH 74/87] Java highlights: Add binary integer literals --- queries/java/highlights.scm | 1 + 1 file changed, 1 insertion(+) diff --git a/queries/java/highlights.scm b/queries/java/highlights.scm index 5f942a296..2b41f1d53 100644 --- a/queries/java/highlights.scm +++ b/queries/java/highlights.scm @@ -111,6 +111,7 @@ (hex_integer_literal) (decimal_integer_literal) (octal_integer_literal) +(binary_integer_literal) ] @number [ From 337756d2f632fa94461dbfddd73898568f46c43d Mon Sep 17 00:00:00 2001 From: Thomas Vigouroux <39092278+vigoux@users.noreply.github.com> Date: Wed, 1 Jul 2020 13:24:25 +0200 Subject: [PATCH 75/87] docs: add apache Licence This is to match neovim's licence. --- LICENSE | 201 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 201 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 000000000..261eeb9e9 --- /dev/null +++ b/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. From af8192f0878b32d781f2eb405d2e94cf604551b9 Mon Sep 17 00:00:00 2001 From: Sainnhepark Date: Wed, 1 Jul 2020 08:32:38 +0800 Subject: [PATCH 76/87] remove doc/tags --- .gitignore | 1 + doc/tags | 59 ------------------------------------------------------ 2 files changed, 1 insertion(+), 59 deletions(-) create mode 100644 .gitignore delete mode 100644 doc/tags diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..926ccaaf9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +doc/tags diff --git a/doc/tags b/doc/tags deleted file mode 100644 index 5d8e88196..000000000 --- a/doc/tags +++ /dev/null @@ -1,59 +0,0 @@ -:TSBufDisable nvim-treesitter.txt /*:TSBufDisable* -:TSBufDisableAll nvim-treesitter.txt /*:TSBufDisableAll* -:TSBufEnable nvim-treesitter.txt /*:TSBufEnable* -:TSBufEnableAll nvim-treesitter.txt /*:TSBufEnableAll* -:TSInstall nvim-treesitter.txt /*:TSInstall* -:TSInstallInfo nvim-treesitter.txt /*:TSInstallInfo* -:TSModuleInfo nvim-treesitter.txt /*:TSModuleInfo* -hl-TSBoolean nvim-treesitter.txt /*hl-TSBoolean* -hl-TSCharacter nvim-treesitter.txt /*hl-TSCharacter* -hl-TSConditional nvim-treesitter.txt /*hl-TSConditional* -hl-TSConstBuiltin nvim-treesitter.txt /*hl-TSConstBuiltin* -hl-TSConstMacro nvim-treesitter.txt /*hl-TSConstMacro* -hl-TSConstant nvim-treesitter.txt /*hl-TSConstant* -hl-TSConstructor nvim-treesitter.txt /*hl-TSConstructor* -hl-TSError nvim-treesitter.txt /*hl-TSError* -hl-TSException nvim-treesitter.txt /*hl-TSException* -hl-TSField nvim-treesitter.txt /*hl-TSField* -hl-TSFloat nvim-treesitter.txt /*hl-TSFloat* -hl-TSFuncBuiltin nvim-treesitter.txt /*hl-TSFuncBuiltin* -hl-TSFuncMacro nvim-treesitter.txt /*hl-TSFuncMacro* -hl-TSFunction nvim-treesitter.txt /*hl-TSFunction* -hl-TSInclude nvim-treesitter.txt /*hl-TSInclude* -hl-TSKeyword nvim-treesitter.txt /*hl-TSKeyword* -hl-TSLabel nvim-treesitter.txt /*hl-TSLabel* -hl-TSMethod nvim-treesitter.txt /*hl-TSMethod* -hl-TSNumber nvim-treesitter.txt /*hl-TSNumber* -hl-TSOperator nvim-treesitter.txt /*hl-TSOperator* -hl-TSParameter nvim-treesitter.txt /*hl-TSParameter* -hl-TSProperty nvim-treesitter.txt /*hl-TSProperty* -hl-TSPunctBracket nvim-treesitter.txt /*hl-TSPunctBracket* -hl-TSPunctDelimiter nvim-treesitter.txt /*hl-TSPunctDelimiter* -hl-TSPunctSpecial nvim-treesitter.txt /*hl-TSPunctSpecial* -hl-TSRepeat nvim-treesitter.txt /*hl-TSRepeat* -hl-TSString nvim-treesitter.txt /*hl-TSString* -hl-TSStringEscape nvim-treesitter.txt /*hl-TSStringEscape* -hl-TSStringRegex nvim-treesitter.txt /*hl-TSStringRegex* -hl-TSStructure nvim-treesitter.txt /*hl-TSStructure* -hl-TSType nvim-treesitter.txt /*hl-TSType* -hl-TSTypeBuiltin nvim-treesitter.txt /*hl-TSTypeBuiltin* -nvim-treesitter nvim-treesitter.txt /*nvim-treesitter* -nvim-treesitter-commands nvim-treesitter.txt /*nvim-treesitter-commands* -nvim-treesitter-functions nvim-treesitter.txt /*nvim-treesitter-functions* -nvim-treesitter-highlights nvim-treesitter.txt /*nvim-treesitter-highlights* -nvim-treesitter-intro nvim-treesitter.txt /*nvim-treesitter-intro* -nvim-treesitter-quickstart nvim-treesitter.txt /*nvim-treesitter-quickstart* -nvim-treesitter-utils nvim-treesitter.txt /*nvim-treesitter-utils* -nvim_treesitter#foldexpr() nvim-treesitter.txt /*nvim_treesitter#foldexpr()* -nvim_treesitter#statusline() nvim-treesitter.txt /*nvim_treesitter#statusline()* -ts_utils.containing_scope nvim-treesitter.txt /*ts_utils.containing_scope* -ts_utils.get_named_children nvim-treesitter.txt /*ts_utils.get_named_children* -ts_utils.get_next_node nvim-treesitter.txt /*ts_utils.get_next_node* -ts_utils.get_node_at_cursor nvim-treesitter.txt /*ts_utils.get_node_at_cursor* -ts_utils.get_node_text nvim-treesitter.txt /*ts_utils.get_node_text* -ts_utils.get_previous_node nvim-treesitter.txt /*ts_utils.get_previous_node* -ts_utils.is_parent nvim-treesitter.txt /*ts_utils.is_parent* -ts_utils.nested_scope nvim-treesitter.txt /*ts_utils.nested_scope* -ts_utils.next_scope nvim-treesitter.txt /*ts_utils.next_scope* -ts_utils.parent_scope nvim-treesitter.txt /*ts_utils.parent_scope* -ts_utils.previous_scope nvim-treesitter.txt /*ts_utils.previous_scope* From d15210b22fa926ac8d21f8ad95269cad89492d3d Mon Sep 17 00:00:00 2001 From: Thomas Vigouroux Date: Tue, 30 Jun 2020 08:59:34 +0200 Subject: [PATCH 77/87] highlights(lua): update query to new syntax --- queries/lua/highlights.scm | 97 +++++++++++++++++++++----------------- 1 file changed, 55 insertions(+), 42 deletions(-) diff --git a/queries/lua/highlights.scm b/queries/lua/highlights.scm index a674638e8..28247491d 100644 --- a/queries/lua/highlights.scm +++ b/queries/lua/highlights.scm @@ -2,57 +2,70 @@ ;;; Builtins ;; Keywords -"local" @keyword -"if" @conditional -"then" @conditional -"else" @conditional -"elseif" @conditional -"end" @keyword -"return" @keyword -"do" @repeat -"while" @repeat -"repeat" @repeat -"for" @repeat -(break_statement) @keyword -"goto" @keyword + +[ +"if" +"then" +"else" +"elseif" + ] @conditional + +[ +"do" +"while" +"repeat" +"for" +"in" +] @repeat + +[ +"local" +"end" +"return" +(break_statement) +"goto" +] @keyword ;; Operators -"~=" @operator -"==" @operator -"<=" @operator -">=" @operator -"not" @operator -"and" @operator -"or" @operator -"<" @operator -">" @operator +[ +"~=" +"==" +"<=" +">=" +"not" +"and" +"or" +"<" +">" +"+" +"-" +"%" +"/" +"//" +"*" +"^" +"&" +"~" +"|" +">>" +"<<" +".." +"#" + ] @operator -"+" @operator -"-" @operator -"%" @operator -"/" @operator -"//" @operator -"*" @operator -"^" @operator -"&" @operator -"~" @operator -"|" @operator -">>" @operator -"<<" @operator -".." @operator -"#" @operator ;; Constants -(false) @boolean -(true) @boolean +[ +(false) +(true) +] @boolean (nil) @constant.builtin (spread) @constant ;; "..." ;; Nodes -(function "function" @function "end" @function) -(function_definition "function" @function "end" @function) -(local_function "function" @function "end" @function) -(table "{" @constructor "}" @constructor) +(_ "function" @function "end" @function) ;; Any node that has both funtion and end in it + +(table ["{" "}"] @constructor) (comment) @comment (string) @string (number) @number From e8af62c84379d9cf90a8a15cba38a687b5265c93 Mon Sep 17 00:00:00 2001 From: Thomas Vigouroux Date: Tue, 30 Jun 2020 09:18:07 +0200 Subject: [PATCH 78/87] locals(lua): capture function definition params --- queries/lua/locals.scm | 2 ++ 1 file changed, 2 insertions(+) diff --git a/queries/lua/locals.scm b/queries/lua/locals.scm index ee2927328..ca15119e3 100644 --- a/queries/lua/locals.scm +++ b/queries/lua/locals.scm @@ -12,6 +12,8 @@ (parameters (identifier) @definition.var))) ((function (parameters (identifier) @definition.var))) +((function_definition + (parameters (identifier) @definition.var))) ;; Loops ((loop_expression From e4cd88baa48968c4bae5a8f0948d9a451cb1c2ae Mon Sep 17 00:00:00 2001 From: Steven Sojka Date: Wed, 1 Jul 2020 10:40:17 -0500 Subject: [PATCH 79/87] feat(javascript): add import specifier as definition --- queries/javascript/locals.scm | 3 +++ 1 file changed, 3 insertions(+) diff --git a/queries/javascript/locals.scm b/queries/javascript/locals.scm index 165adfed9..d56000d5a 100644 --- a/queries/javascript/locals.scm +++ b/queries/javascript/locals.scm @@ -28,6 +28,9 @@ (variable_declarator name: (identifier) @definition) +(import_specifier + (identifier) @definition) + ; References ;------------ From 07cd0b4d8c712303e404e1becbb8d189e7bd2384 Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Sat, 4 Jul 2020 19:06:53 +0200 Subject: [PATCH 80/87] C++ locals: Add reference_declarator (analogous to pointer_declarator for C locals) --- queries/cpp/locals.scm | 2 ++ 1 file changed, 2 insertions(+) diff --git a/queries/cpp/locals.scm b/queries/cpp/locals.scm index 061153c04..e2e32a140 100644 --- a/queries/cpp/locals.scm +++ b/queries/cpp/locals.scm @@ -3,6 +3,8 @@ (class_specifier) @scope (struct_specifier) @scope +(reference_declarator + (identifier) @definition.var) (struct_specifier name: (type_identifier) @definition.type) From 1ce1c73249cf5620726f9b00fd58cac1747b1e70 Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Sat, 4 Jul 2020 18:04:32 +0200 Subject: [PATCH 81/87] Add Travis CI with luacheck --- .gitignore | 1 + .luacheckrc | 16 ++++++++++++++++ .travis.yml | 11 +++++++++++ 3 files changed, 28 insertions(+) create mode 100644 .luacheckrc create mode 100644 .travis.yml diff --git a/.gitignore b/.gitignore index 926ccaaf9..7ac6d5b04 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ doc/tags +.luacheckcache diff --git a/.luacheckrc b/.luacheckrc new file mode 100644 index 000000000..cb61432f9 --- /dev/null +++ b/.luacheckrc @@ -0,0 +1,16 @@ +-- Rerun tests only if their modification time changed. +cache = true + +-- Glorious list of warnings: https://luacheck.readthedocs.io/en/stable/warnings.html +ignore = { + "212", -- Unused argument, In the case of callback function, _arg_name is easier to understand than _, so this option is set to off. + "411", -- Redefining a local variable. + "412", -- Redefining an argument. + "422", -- Shadowing an argument + "122" -- Indirectly setting a readonly global +} + +-- Global objects defined by the C code +read_globals = { + "vim", +} diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 000000000..a6749df1e --- /dev/null +++ b/.travis.yml @@ -0,0 +1,11 @@ + +language: c +dist: bionic +before_install: + - sudo apt-get update + - sudo add-apt-repository universe + - sudo apt install luarocks -y + - sudo luarocks install luacheck + +script: + - luacheck **/**/*.lua --codes From 5642507f6a35d2e25b1588a36d8ddfc525f1464d Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Sat, 4 Jul 2020 18:28:17 +0200 Subject: [PATCH 82/87] Make luacheck happy --- lua/nvim-treesitter/configs.lua | 6 +++--- lua/nvim-treesitter/fold.lua | 3 +-- lua/nvim-treesitter/health.lua | 3 --- lua/nvim-treesitter/incremental_selection.lua | 2 +- lua/nvim-treesitter/locals.lua | 1 - .../refactor/highlight_definitions.lua | 17 +++++++++-------- lua/nvim-treesitter/refactor/navigation.lua | 15 +-------------- lua/nvim-treesitter/refactor/smart_rename.lua | 5 ++--- lua/nvim-treesitter/ts_utils.lua | 13 ++++++------- lua/nvim-treesitter/utils.lua | 1 - 10 files changed, 23 insertions(+), 43 deletions(-) diff --git a/lua/nvim-treesitter/configs.lua b/lua/nvim-treesitter/configs.lua index c31ca4f29..43dc944bf 100644 --- a/lua/nvim-treesitter/configs.lua +++ b/lua/nvim-treesitter/configs.lua @@ -130,7 +130,7 @@ local function disable_mod_conf_autocmd(mod, lang) if not config_mod or not M.is_enabled(mod, lang) then return end - local cmd = string.format("lua require'nvim-treesitter.%s'.attach()", mod) + --local cmd = string.format("lua require'nvim-treesitter.%s'.attach()", mod) -- TODO(kyazdani): detach the correct autocmd... doesn't work when using %s, cmd for _, ft in pairs(parsers.lang_to_ft(lang)) do api.nvim_command(string.format("autocmd! NvimTreesitter FileType %s", ft)) @@ -260,7 +260,7 @@ function M.setup_module(mod, data, mod_name) end end elseif type(data) == 'table' and type(mod) == 'table' then - for key, value in pairs(data) do + for key, value in pairs(data) do M.setup_module(mod[key], value, key) end end @@ -289,7 +289,7 @@ end -- A module should contain an 'is_supported' function. -- @param mod the module table function M.is_module(mod) - return type(mod) == 'table' and type(mod.is_supported) == 'function' + return type(mod) == 'table' and type(mod.is_supported) == 'function' end return M diff --git a/lua/nvim-treesitter/fold.lua b/lua/nvim-treesitter/fold.lua index 6a39461a7..401f52606 100644 --- a/lua/nvim-treesitter/fold.lua +++ b/lua/nvim-treesitter/fold.lua @@ -1,4 +1,3 @@ -local api = vim.api local parsers = require'nvim-treesitter.parsers' local M = {} @@ -21,7 +20,7 @@ function M.get_fold_indic(lnum) local parser = parsers.get_parser() - local multiline_here, level = smallest_multiline_containing(parser:parse():root(), 0) + local _, level = smallest_multiline_containing(parser:parse():root(), 0) return tostring(level) end diff --git a/lua/nvim-treesitter/health.lua b/lua/nvim-treesitter/health.lua index e73ce7fb5..d3083e77b 100644 --- a/lua/nvim-treesitter/health.lua +++ b/lua/nvim-treesitter/health.lua @@ -2,13 +2,10 @@ local api = vim.api local fn = vim.fn local queries = require'nvim-treesitter.query' -local locals = require'nvim-treesitter.locals' -local highlight = require'nvim-treesitter.highlight' local parsers = require'nvim-treesitter.parsers' local health_start = vim.fn["health#report_start"] local health_ok = vim.fn['health#report_ok'] -local health_info = vim.fn['health#report_info'] local health_warn = vim.fn['health#report_warn'] local health_error = vim.fn['health#report_error'] diff --git a/lua/nvim-treesitter/incremental_selection.lua b/lua/nvim-treesitter/incremental_selection.lua index 6a9999303..3b76b25f2 100644 --- a/lua/nvim-treesitter/incremental_selection.lua +++ b/lua/nvim-treesitter/incremental_selection.lua @@ -58,7 +58,7 @@ local function select_incremental(get_parent) return end - node = get_parent(nodes[#nodes]) + local node = get_parent(nodes[#nodes]) if not node then return end if node ~= nodes[#nodes] then diff --git a/lua/nvim-treesitter/locals.lua b/lua/nvim-treesitter/locals.lua index 8c7d400eb..d30953348 100644 --- a/lua/nvim-treesitter/locals.lua +++ b/lua/nvim-treesitter/locals.lua @@ -2,7 +2,6 @@ -- Locals are a generalization of definition and scopes -- its the way nvim-treesitter uses to "understand" the code local api = vim.api -local ts = vim.treesitter local queries = require'nvim-treesitter.query' local parsers = require'nvim-treesitter.parsers' diff --git a/lua/nvim-treesitter/refactor/highlight_definitions.lua b/lua/nvim-treesitter/refactor/highlight_definitions.lua index bdbec1588..a581e37d5 100644 --- a/lua/nvim-treesitter/refactor/highlight_definitions.lua +++ b/lua/nvim-treesitter/refactor/highlight_definitions.lua @@ -1,7 +1,6 @@ -- This module highlights reference usages and the corresponding -- definition on cursor hold. -local parsers = require'nvim-treesitter.parsers' local ts_utils = require'nvim-treesitter.ts_utils' local locals = require'nvim-treesitter.locals' local api = vim.api @@ -17,7 +16,7 @@ function M.highlight_usages(bufnr) local node_at_point = ts_utils.get_node_at_cursor() local references = locals.get_references(bufnr) - if not node_at_point or not vim.tbl_contains(references, node_at_point) then + if not node_at_point or not vim.tbl_contains(references, node_at_point) then return end @@ -29,9 +28,9 @@ function M.highlight_usages(bufnr) if usage_node ~= node_at_point then api.nvim_buf_add_highlight( - bufnr, - usage_namespace, - 'TSDefinitionUsage', + bufnr, + usage_namespace, + 'TSDefinitionUsage', start_row, start_col, end_col) @@ -42,9 +41,9 @@ function M.highlight_usages(bufnr) local start_row, start_col, _, end_col = def_node:range() api.nvim_buf_add_highlight( - bufnr, - usage_namespace, - 'TSDefinition', + bufnr, + usage_namespace, + 'TSDefinition', start_row, start_col, end_col) @@ -60,9 +59,11 @@ function M.attach(bufnr) cmd(string.format('augroup NvimTreesitterUsages_%d', bufnr)) cmd 'au!' + -- luacheck: push ignore 631 cmd(string.format([[autocmd CursorHold lua require'nvim-treesitter.refactor.highlight_definitions'.highlight_usages(%d)]], bufnr, bufnr)) cmd(string.format([[autocmd CursorMoved lua require'nvim-treesitter.refactor.highlight_definitions'.clear_usage_highlights(%d)]], bufnr, bufnr)) cmd(string.format([[autocmd InsertEnter lua require'nvim-treesitter.refactor.highlight_definitions'.clear_usage_highlights(%d)]], bufnr, bufnr)) + -- luacheck: pop cmd 'augroup END' end diff --git a/lua/nvim-treesitter/refactor/navigation.lua b/lua/nvim-treesitter/refactor/navigation.lua index 5fd25e474..f2eda367c 100644 --- a/lua/nvim-treesitter/refactor/navigation.lua +++ b/lua/nvim-treesitter/refactor/navigation.lua @@ -7,18 +7,6 @@ local api = vim.api local M = {} -local function node_to_qf(node, kind) - local lnum, col, _ = def.node:start() - - return { - bufnr = bufnr, - lnum = lnum + 1, - col = col + 1, - text = ts_utils.get_node_text(def.node)[1] or '', - kind = kind - } -end - function M.goto_definition(bufnr) local bufnr = bufnr or api.nvim_get_current_buf() local node_at_point = ts_utils.get_node_at_cursor() @@ -70,10 +58,9 @@ function M.attach(bufnr) end function M.detach(bufnr) - local buf = bufnr or api.nvim_get_current_buf() local config = configs.get_module('refactor.navigation') - for fn_name, mapping in pairs(config.keymaps) do + for _, mapping in pairs(config.keymaps) do api.nvim_buf_del_keymap(bufnr, 'n', mapping) end end diff --git a/lua/nvim-treesitter/refactor/smart_rename.lua b/lua/nvim-treesitter/refactor/smart_rename.lua index 8aab9538d..e5ee37ac1 100644 --- a/lua/nvim-treesitter/refactor/smart_rename.lua +++ b/lua/nvim-treesitter/refactor/smart_rename.lua @@ -35,7 +35,7 @@ function M.smart_rename(bufnr) end for _, node in ipairs(nodes_to_rename) do - local start_row, start_col, end_row, end_col = node:range() + local start_row, start_col, _, end_col = node:range() local line = api.nvim_buf_get_lines(bufnr, start_row, start_row + 1, false)[1] if line then @@ -56,10 +56,9 @@ function M.attach(bufnr) end function M.detach(bufnr) - local buf = bufnr or api.nvim_get_current_buf() local config = configs.get_module('refactor.smart_rename') - for fn_name, mapping in pairs(config.keymaps) do + for _, mapping in pairs(config.keymaps) do api.nvim_buf_del_keymap(bufnr, 'n', mapping) end end diff --git a/lua/nvim-treesitter/ts_utils.lua b/lua/nvim-treesitter/ts_utils.lua index 06f92c885..7d5e97cdd 100644 --- a/lua/nvim-treesitter/ts_utils.lua +++ b/lua/nvim-treesitter/ts_utils.lua @@ -219,10 +219,9 @@ end -- @param bufnr buffer -- @returns the definition node and the definition nodes scope node function M.find_definition(node, bufnr) - local bufnr = bufnr or api.nvim_get_current_buf() + local bufnr = bufnr or api.nvim_get_current_buf() local node_text = M.get_node_text(node)[1] local current_scope = M.containing_scope(node) - local _, _, node_start = node:start() local matching_def_nodes = {} -- If a scope wasn't found then use the root node @@ -234,7 +233,7 @@ function M.find_definition(node, bufnr) for _, def in ipairs(locals.get_definitions(bufnr)) do for _, def_node in ipairs(M.get_local_nodes(def)) do if M.get_node_text(def_node)[1] == node_text then - table.insert(matching_def_nodes, def_node) + table.insert(matching_def_nodes, def_node) end end end @@ -258,7 +257,7 @@ end function M.get_local_nodes(local_def) local result = {} - M.recurse_local_nodes(local_def, function(_, node) + M.recurse_local_nodes(local_def, function(_, node) table.insert(result, node) end) @@ -281,9 +280,9 @@ function M.recurse_local_nodes(local_def, accumulator, full_match, last_match) else for match_key, def in pairs(local_def) do M.recurse_local_nodes( - def, - accumulator, - full_match and (full_match..'.'..match_key) or match_key, + def, + accumulator, + full_match and (full_match..'.'..match_key) or match_key, match_key) end end diff --git a/lua/nvim-treesitter/utils.lua b/lua/nvim-treesitter/utils.lua index f5c65a0a7..c87cee2d8 100644 --- a/lua/nvim-treesitter/utils.lua +++ b/lua/nvim-treesitter/utils.lua @@ -1,7 +1,6 @@ local api = vim.api local fn = vim.fn local luv = vim.loop -local ts = vim.treesitter local M = {} From bf015c168f13995a99cc5eac3a794b375ba5ed56 Mon Sep 17 00:00:00 2001 From: Thomas Vigouroux <39092278+vigoux@users.noreply.github.com> Date: Sun, 5 Jul 2020 19:27:56 +0200 Subject: [PATCH 83/87] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 3ea15b95f..5499de391 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ [![Gitter](https://badges.gitter.im/nvim-treesitter/community.svg)](https://gitter.im/nvim-treesitter/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) +[![Build Status](https://travis-ci.com/nvim-treesitter/nvim-treesitter.svg?branch=master)](https://travis-ci.com/nvim-treesitter/nvim-treesitter) # nvim-treesitter Treesitter configurations and abstraction layer for Neovim. From 63c1853674f8f6c81bc8883383ad550499410568 Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Sat, 4 Jul 2020 22:00:59 +0200 Subject: [PATCH 84/87] C/C++ highlights: update to new syntax This changed the highlight of "switch" to conditional and of "break" to repeat. --- queries/c/highlights.scm | 117 +++++++++++++++++++------------------ queries/c/locals.scm | 14 +++-- queries/cpp/highlights.scm | 51 +++++++++------- queries/cpp/locals.scm | 4 +- 4 files changed, 99 insertions(+), 87 deletions(-) diff --git a/queries/c/highlights.scm b/queries/c/highlights.scm index 5a79a2b5e..3fbf4ffa9 100644 --- a/queries/c/highlights.scm +++ b/queries/c/highlights.scm @@ -1,24 +1,34 @@ -"break" @keyword -"case" @conditional -"const" @keyword -"continue" @repeat -"default" @keyword -"do" @repeat -"else" @conditional -"enum" @keyword -"extern" @keyword -"for" @repeat -"if" @conditional -"inline" @keyword -"return" @keyword -"sizeof" @keyword -"static" @keyword -"struct" @keyword -"switch" @keyword -"typedef" @keyword -"union" @keyword -"volatile" @keyword -"while" @repeat +[ + "const" + "default" + "enum" + "extern" + "inline" + "return" + "sizeof" + "static" + "struct" + "typedef" + "union" + "volatile" +] @keyword + +[ + "while" + "for" + "do" + "continue" + "break" +] @repeat + +[ + "if" + "else" + "case" + "switch" +] @conditional + +(conditional_expression [ "?" ":" ] @conditional) "#define" @constant.macro [ @@ -32,45 +42,38 @@ (preproc_directive) ] @keyword -"--" @operator -"-" @operator -"->" @operator -"!=" @operator -"*" @operator -"/" @operator -"&" @operator -"&&" @operator -"+" @operator -"++" @operator -"<" @operator -"<=" @operator -"==" @operator -"=" @operator -"~" @operator -">" @operator -">=" @operator -"!" @operator -"||" @operator -(conditional_expression [ "?" ":" ] @conditional) +[ + "--" + "-" + "->" + "!=" + "*" + "/" + "&" + "&&" + "+" + "++" + "<" + "<=" + "==" + "=" + "~" + ">" + ">=" + "!" + "||" -"-=" @operator -"+=" @operator -"*=" @operator -"/=" @operator -"|=" @operator -"&=" @operator + "-=" + "+=" + "*=" + "/=" + "|=" + "&=" +] @operator -"." @punctuation.delimiter -";" @punctuation.delimiter -":" @punctuation.delimiter -"," @punctuation.delimiter +[ "." ";" ":" "," ] @punctuation.delimiter -"(" @punctuation.bracket -")" @punctuation.bracket -"[" @punctuation.bracket -"]" @punctuation.bracket -"{" @punctuation.bracket -"}" @punctuation.bracket +[ "(" ")" "[" "]" "{" "}"] @punctuation.bracket (string_literal) @string (system_lib_string) @string diff --git a/queries/c/locals.scm b/queries/c/locals.scm index 505db5b57..06b5a4c95 100644 --- a/queries/c/locals.scm +++ b/queries/c/locals.scm @@ -31,9 +31,11 @@ (identifier) @reference ;; Scope -(for_statement) @scope -(if_statement) @scope -(while_statement) @scope -(translation_unit) @scope -(function_definition) @scope -(compound_statement) @scope ; a block in curly braces +[ + (for_statement) + (if_statement) + (while_statement) + (translation_unit) + (function_definition) + (compound_statement) ; a block in curly braces +] @scope diff --git a/queries/cpp/highlights.scm b/queries/cpp/highlights.scm index 0d8295a1d..49382bdf7 100644 --- a/queries/cpp/highlights.scm +++ b/queries/cpp/highlights.scm @@ -60,7 +60,6 @@ (argument_list)) (#match? @constructor "^[A-Z]")) -(auto) @keyword ; Constants @@ -72,25 +71,33 @@ ; Keywords -"catch" @exception -"class" @keyword -"constexpr" @keyword -"delete" @keyword -"explicit" @keyword -"final" @exception -"friend" @keyword -"mutable" @keyword -"namespace" @keyword -"noexcept" @keyword -"new" @keyword -"override" @keyword -"private" @keyword -"protected" @keyword -"public" @keyword -"template" @keyword -"throw" @keyword -"try" @exception -"typename" @keyword -"using" @keyword -"virtual" @keyword +[ + "try" + "catch" + "noexcept" + "throw" +] @exception + + +[ + "class" + "constexpr" + "delete" + "explicit" + "final" + "friend" + "mutable" + "namespace" + "new" + "override" + "private" + "protected" + "public" + "template" + "typename" + "using" + "virtual" + (auto) +] @keyword + "::" @operator diff --git a/queries/cpp/locals.scm b/queries/cpp/locals.scm index e2e32a140..47b69c866 100644 --- a/queries/cpp/locals.scm +++ b/queries/cpp/locals.scm @@ -11,14 +11,14 @@ (struct_specifier name: (scoped_type_identifier - name: (type_identifier) @definition.type) ) + name: (type_identifier) @definition.type)) (class_specifier name: (type_identifier) @definition.type) (class_specifier name: (scoped_type_identifier - name: (type_identifier) @definition.type) ) + name: (type_identifier) @definition.type)) ;; Function defintions (template_function From ab1916f3854c8181c7d68f1a4801bc5f217dd4be Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Sun, 5 Jul 2020 17:04:54 +0200 Subject: [PATCH 85/87] Allow arbitrary query files in locals.lua This is a preparation for `textobject` queries. --- lua/nvim-treesitter/locals.lua | 59 ++++++++++++++++++++++++++++------ 1 file changed, 49 insertions(+), 10 deletions(-) diff --git a/lua/nvim-treesitter/locals.lua b/lua/nvim-treesitter/locals.lua index d30953348..b52d42b3f 100644 --- a/lua/nvim-treesitter/locals.lua +++ b/lua/nvim-treesitter/locals.lua @@ -5,16 +5,30 @@ local api = vim.api local queries = require'nvim-treesitter.query' local parsers = require'nvim-treesitter.parsers' +local utils = require'nvim-treesitter.utils' -local M = { - locals = {} +local default_dict = { + __index = function(table, key) + local exists = rawget(table, key) + if not exists then + table[key] = {} + end + return rawget(table, key) + end } -function M.collect_locals(bufnr) +local query_cache = {} +setmetatable(query_cache, default_dict) + +local M = {} + +function M.collect_locals(bufnr, query_kind) + query_kind = query_kind or 'locals' + local lang = parsers.ft_to_lang(api.nvim_buf_get_option(bufnr, "ft")) if not lang then return end - local query = queries.get_query(lang, 'locals') + local query = queries.get_query(lang, query_kind) if not query then return end local parser = parsers.get_parser(bufnr, lang) @@ -32,18 +46,20 @@ function M.collect_locals(bufnr) return locals end -local function update_cached_locals(bufnr, changed_tick) - M.locals[bufnr] = {tick=changed_tick, cache=( M.collect_locals(bufnr) or {} )} +local function update_cached_locals(bufnr, changed_tick, query_kind) + query_cache[query_kind][bufnr] = {tick=changed_tick, cache=( M.collect_locals(bufnr, query_kind) or {} )} end -function M.get_locals(bufnr) +function M.get_locals(bufnr, query_kind) + query_kind = query_kind or 'locals' + local bufnr = bufnr or api.nvim_get_current_buf() - local cached_local = M.locals[bufnr] + local cached_local = query_cache[query_kind][bufnr] if not cached_local or api.nvim_buf_get_changedtick(bufnr) > cached_local.tick then - update_cached_locals(bufnr,api.nvim_buf_get_changedtick(bufnr)) + update_cached_locals(bufnr,api.nvim_buf_get_changedtick(bufnr), query_kind) end - return M.locals[bufnr].cache + return query_cache[query_kind][bufnr].cache end function M.get_definitions(bufnr) @@ -88,4 +104,27 @@ function M.get_references(bufnr) return refs end +--- Return all nodes in locals corresponding to a specific capture (like @scope, @reference) +-- Works like M.get_references or M.get_scopes except you can choose the capture +-- Can also be a nested capture like @definition.function to get all nodes defining a function +function M.get_capture_matches(bufnr, capture_string, query_kind) + if not string.sub(capture_string, 1,2) == '@' then + print('capture_string must start with "@"') + return + end + + --remove leading "@" + capture_string = string.sub(capture_string, 2) + + local matches = {} + for _, match in pairs(M.get_locals(bufnr, query_kind)) do + local insert = utils.get_at_path(match, capture_string..'.node') + + if insert then + table.insert(matches, insert) + end + end + return matches +end + return M From 27d7a0ffff1623572a642f3e45db3fc556dafec3 Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Sun, 5 Jul 2020 20:45:46 +0200 Subject: [PATCH 86/87] Add style-check script --- .travis.yml | 2 +- CONTRIBUTING.md | 12 ++++++++++++ scripts/pre-push | 7 +++++++ scripts/style-check.sh | 3 +++ 4 files changed, 23 insertions(+), 1 deletion(-) create mode 100755 scripts/pre-push create mode 100755 scripts/style-check.sh diff --git a/.travis.yml b/.travis.yml index a6749df1e..a3d4dfe86 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,4 +8,4 @@ before_install: - sudo luarocks install luacheck script: - - luacheck **/**/*.lua --codes + - ./scripts/style-check.sh diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index af48cdc62..47dcb9f83 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -11,6 +11,18 @@ As you know, `nvim-treesitter` is roughly splitted in two parts : Depending on which part of the plugin you want to contribute to, please read the appropriate section. +## Style Checks and Tests + +We haven't implemented any functionality tests yet. Feel free to contribute. +However, we check code style with `luacheck`! +Please install luacheck and activate our `pre-push` hook to automatically check style before +every push: + +```bash +luarocks install luacheck +ln -s ../../scripts/pre-push .git/hooks/pre-push +``` + ## Parser configurations Contributing to parser configurations is basically modifying one of the `queries/*/*.scm`. diff --git a/scripts/pre-push b/scripts/pre-push new file mode 100755 index 000000000..00d4c5071 --- /dev/null +++ b/scripts/pre-push @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +# Can be used as a pre-push hook +# Just symlink this file to .git/hooks/pre-push + +echo "Running style check..." +./scripts/style-check.sh diff --git a/scripts/style-check.sh b/scripts/style-check.sh new file mode 100755 index 000000000..181ab4581 --- /dev/null +++ b/scripts/style-check.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +luacheck `find -name "*.lua"` --codes From 3e8b5a30e2640d47ce07e7b32af07b1e4f5e6e33 Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Mon, 6 Jul 2020 19:57:58 +0200 Subject: [PATCH 87/87] Use
for checkhealth output in bug report template The output of checkhealth can be quite lengthy. Plus fix position of *** to make text render bold --- .github/ISSUE_TEMPLATE/bug_report.md | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md index 06596fe11..8290ee650 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -20,10 +20,15 @@ Steps to reproduce the behavior: **Expected behavior** A clear and concise description of what you expected to happen. -**Output of `:checkhealth nvim_treesitter` *** -``` +**Output of `:checkhealth nvim_treesitter`** + +
+ + Paste the output here -``` + + +
**Output of `nvim --version`** ```