From df060261106a3570dbae04ae83879cdcbc9a9907 Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Sat, 23 May 2020 20:29:08 +0200 Subject: [PATCH 01/11] 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 02/11] 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 03/11] 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 04/11] 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 05/11] 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 06/11] 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 07/11] 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 08/11] 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 09/11] 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 10/11] 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 11/11] 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)