|
12 | 12 |
|
13 | 13 | (fn build-completion-query [ts-lang cmpl-resource] |
14 | 14 | (let [query-path (string.format symbol-query-path-template cmpl-resource) |
15 | | - query-text (res.get-resource-contents query-path) ] |
16 | | - (if query-text |
| 15 | + query-text (res.get-resource-contents query-path) |
| 16 | + lang-avail (pcall vim.treesitter.language.inspect ts-lang)] |
| 17 | + (if (and query-text lang-avail) |
17 | 18 | (vim.treesitter.query.parse ts-lang query-text) |
18 | 19 | nil))) |
19 | 20 |
|
|
83 | 84 | ;; To avoid getting an invalid node when calling M.get-completions-at-cursor, ensure |
84 | 85 | ;; that the buffer is parsed. |
85 | 86 | ;; See the note for `:he treesitter.get_node` and `:he languagetree`. |
86 | | - (ts.parse!) |
87 | | - (let [buffer (vim.api.nvim_get_current_buf) |
88 | | - cursor-node (vim.treesitter.get_node) |
89 | | - (row _) (unpack (vim.api.nvim_win_get_cursor 0)) |
90 | | - scope-captures (query:iter_captures (cursor-node:root) buffer 0 row) |
91 | | - scopes (extract-scopes query scope-captures) |
92 | | - captures (query:iter_captures (cursor-node:root) buffer 0 row) |
93 | | - results []] |
94 | | - |
95 | | - (each [id n meta captures] |
96 | | - (let [captured-label (. query.captures id)] |
97 | | - (if (= :global.define captured-label) |
| 87 | + (if (not= nil (ts.parse!)) |
| 88 | + (let [buffer (vim.api.nvim_get_current_buf) |
| 89 | + cursor-node (vim.treesitter.get_node) |
| 90 | + (row _) (unpack (vim.api.nvim_win_get_cursor 0)) |
| 91 | + scope-captures (query:iter_captures (cursor-node:root) buffer 0 row) |
| 92 | + scopes (extract-scopes query scope-captures) |
| 93 | + captures (query:iter_captures (cursor-node:root) buffer 0 row) |
| 94 | + results []] |
| 95 | + |
| 96 | + (each [id n meta captures] |
| 97 | + (let [captured-label (. query.captures id)] |
| 98 | + (if (= :global.define captured-label) |
98 | 99 | (table.insert results (get-node-text n buffer meta)) |
99 | 100 |
|
100 | 101 | (and (= :local.bind captured-label) |
|
107 | 108 | (is-in-scope cursor-node (get-nth-scope-parent 2 n scopes))) |
108 | 109 | (table.insert results (get-node-text n buffer meta))))) |
109 | 110 |
|
110 | | - (util.ordered-distinct results))) |
| 111 | + (util.ordered-distinct results)) |
| 112 | + [])) |
111 | 113 |
|
112 | 114 | (fn M.get-completions-at-cursor [ts-lang cmpl-resource] |
113 | 115 | "Use tree-sitter query to find completions in scope at cursor |
|
0 commit comments