diff options
| author | Steven Sojka <Steven.Sojka@tdameritrade.com> | 2020-06-15 07:05:20 -0500 |
|---|---|---|
| committer | Steven Sojka <Steven.Sojka@tdameritrade.com> | 2020-06-15 07:05:20 -0500 |
| commit | 4551b0e1c914f8f0dabdec4010c86c163c08fee2 (patch) | |
| tree | 09f781e5970756fb5a092a37fcbc4274f9664b10 | |
| parent | ac8ae3b1c7b5644f8317cdc88d604cdb558b6296 (diff) | |
| parent | c452d4a91c341a1057b638d520e7cc75999a6b3b (diff) | |
Merge branch 'master' into feat/typescript-queries
* master:
Change regexes in C/C++ highlights
Update C/C++ highlights to new query syntax
Add better highlighting for preprocessor functions in C highlights
Add operators /=,*=,|=,&= to C highlights
Add compound_statement to c queries
Add punctuation.bracket/punctuation.delimiter to C highlights
Make =,~,! operators in C highlights
Add cpp/locals.scm
Add @error highlight to c/highlights.scm
Add C++ highlights.scm
Introduce base languages for queries
Add tree-sitter-regex
feat(queries): allow for user overrides
Update issue templates
| -rw-r--r-- | .github/ISSUE_TEMPLATE/bug_report.md | 34 | ||||
| -rw-r--r-- | .github/ISSUE_TEMPLATE/feature_request.md | 20 | ||||
| -rw-r--r-- | .github/ISSUE_TEMPLATE/language-request.md | 12 | ||||
| -rw-r--r-- | README.md | 10 | ||||
| -rw-r--r-- | lua/nvim-treesitter/configs.lua | 7 | ||||
| -rw-r--r-- | lua/nvim-treesitter/query.lua | 35 | ||||
| -rw-r--r-- | queries/c/highlights.scm | 45 | ||||
| -rw-r--r-- | queries/c/locals.scm | 3 | ||||
| -rw-r--r-- | queries/cpp/highlights.scm | 97 | ||||
| -rw-r--r-- | queries/cpp/locals.scm | 49 | ||||
| -rw-r--r-- | queries/regex/highlights.scm | 29 |
11 files changed, 330 insertions, 11 deletions
diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 00000000..06596fe1 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,34 @@ +--- +name: Bug report +about: Create a report to help us improve +title: '' +labels: bug +assignees: '' + +--- + +**Describe the bug** +A clear and concise description of what the bug is. + +**To Reproduce** +Steps to reproduce the behavior: +1. Go to '...' +2. Click on '....' +3. Scroll down to '....' +4. See error + +**Expected behavior** +A clear and concise description of what you expected to happen. + +**Output of `:checkhealth nvim_treesitter` *** +``` +Paste the output here +``` + +**Output of `nvim --version`** +``` +Paste your output here +``` + +**Additional context** +Add any other context about the problem here. diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md new file mode 100644 index 00000000..11fc491e --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -0,0 +1,20 @@ +--- +name: Feature request +about: Suggest an idea for this project +title: '' +labels: enhancement +assignees: '' + +--- + +**Is your feature request related to a problem? Please describe.** +A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] + +**Describe the solution you'd like** +A clear and concise description of what you want to happen. + +**Describe alternatives you've considered** +A clear and concise description of any alternative solutions or features you've considered. + +**Additional context** +Add any other context or screenshots about the feature request here. diff --git a/.github/ISSUE_TEMPLATE/language-request.md b/.github/ISSUE_TEMPLATE/language-request.md new file mode 100644 index 00000000..b10287ea --- /dev/null +++ b/.github/ISSUE_TEMPLATE/language-request.md @@ -0,0 +1,12 @@ +--- +name: Language request +about: Request for a new language to be supported +title: '' +labels: enhancement, good first issue, help wanted +assignees: '' + +--- + +**Language informations** + +Please paste any useful informations here ! @@ -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) - [x] javascript (maintained by @steelsojka) @@ -180,6 +180,14 @@ List of currently supported languages: - [ ] yaml - [ ] nix - [ ] markdown +- [x] regex (maintained by @theHamsta) + +## 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 `<vim-config-dir>/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/configs.lua b/lua/nvim-treesitter/configs.lua index f79fed00..f7b5ff2c 100644 --- a/lua/nvim-treesitter/configs.lua +++ b/lua/nvim-treesitter/configs.lua @@ -203,6 +203,13 @@ parsers.nix = { } } +parsers.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 diff --git a/lua/nvim-treesitter/query.lua b/lua/nvim-treesitter/query.lua index 644c3393..788bfa12 100644 --- a/lua/nvim-treesitter/query.lua +++ b/lua/nvim-treesitter/query.lua @@ -3,14 +3,41 @@ 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 +-- 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), false) + 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_file(query_files[1])) + 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 diff --git a/queries/c/highlights.scm b/queries/c/highlights.scm index 4cfb042c..0d718c22 100644 --- a/queries/c/highlights.scm +++ b/queries/c/highlights.scm @@ -31,22 +31,42 @@ "--" @operator "-" @operator -"-=" @operator "->" @operator "!=" @operator "*" @operator +"/" @operator "&" @operator "&&" @operator "+" @operator "++" @operator -"+=" @operator "<" @operator +"<=" @operator "==" @operator +"=" @operator +"~" @operator ">" @operator +">=" @operator +"!" @operator "||" @operator -"." @delimiter -";" @delimiter +"-=" @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 (string_literal) @string (system_lib_string) @string @@ -64,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 @@ -71,7 +93,20 @@ (primitive_type) @type (sized_type_specifier) @type +((identifier) @type + (#match? @type "^[A-Z]")) + ((identifier) @constant - (match? @constant "^[A-Z][A-Z\\d_]+$")) + (#match? @constant "^[A-Z][A-Z0-9_]+$")) (comment) @comment + +;; Parameters +(parameter_list + (parameter_declaration) @parameter) + +(preproc_params + (identifier)) @parameter + +(ERROR) @error + diff --git a/queries/c/locals.scm b/queries/c/locals.scm index b08d706e..505db5b5 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))) @@ -36,3 +36,4 @@ (while_statement) @scope (translation_unit) @scope (function_definition) @scope +(compound_statement) @scope ; a block in curly braces diff --git a/queries/cpp/highlights.scm b/queries/cpp/highlights.scm new file mode 100644 index 00000000..92909f4d --- /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_0-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) @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]")) + +(auto) @keyword + +; Constants + +;(this) @constant.builtin +(this) @keyword +(nullptr) @constant + +(true) @boolean +(false) @boolean + +; 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 +"::" @operator diff --git a/queries/cpp/locals.scm b/queries/cpp/locals.scm new file mode 100644 index 00000000..061153c0 --- /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) diff --git a/queries/regex/highlights.scm b/queries/regex/highlights.scm new file mode 100644 index 00000000..48242543 --- /dev/null +++ b/queries/regex/highlights.scm @@ -0,0 +1,29 @@ +;; Forked from tree-sitter-regex +;; The MIT License (MIT) Copyright (c) 2014 Max Brunsfeld +[ + "(" + ")" + "(?" + "(?:" + "(?<" + ">" + "[" + "]" + "{" + "}" +] @punctuation.bracket + +(group_name) @property + +[ + (identity_escape) + (control_letter_escape) + (character_class_escape) + (control_escape) + (start_assertion) + (end_assertion) + (boundary_assertion) + (non_boundary_assertion) +] @escape + +[ "*" "+" "|" "=" "<=" "!" "<!" ] @operator |
