+41
-7
lines changedFilter options
+41
-7
lines changed Original file line number Diff line number Diff line change
@@ -138,6 +138,13 @@ function M.get_query(lang, query_name)
138
138
end
139
139
end
140
140
141
+
local query_cache = setmetatable({}, {
142
+
__index = function(tbl, key)
143
+
rawset(tbl, key, {})
144
+
return rawget(tbl, key)
145
+
end
146
+
})
147
+
141
148
--- Parse {query} as a string. (If the query is in a file, the caller
142
149
--- should read the contents into a string before calling).
143
150
---
@@ -151,17 +158,23 @@ end
151
158
--- -` info.captures` also points to `captures`.
152
159
--- - `info.patterns` contains information about predicates.
153
160
---
154
-
---@param lang The language
155
-
---@param query A string containing the query (s-expr syntax)
161
+
---@param lang string The language
162
+
---@param query string A string containing the query (s-expr syntax)
156
163
---
157
164
---@returns The query
158
165
function M.parse_query(lang, query)
159
166
language.require_language(lang)
160
-
local self = setmetatable({}, Query)
161
-
self.query = vim._ts_parse_query(lang, query)
162
-
self.info = self.query:inspect()
163
-
self.captures = self.info.captures
164
-
return self
167
+
local cached = query_cache[lang][query]
168
+
if cached then
169
+
return cached
170
+
else
171
+
local self = setmetatable({}, Query)
172
+
self.query = vim._ts_parse_query(lang, query)
173
+
self.info = self.query:inspect()
174
+
self.captures = self.info.captures
175
+
query_cache[lang][query] = self
176
+
return self
177
+
end
165
178
end
166
179
167
180
--- Gets the text corresponding to a given node
Original file line number Diff line number Diff line change
@@ -167,6 +167,27 @@ void ui_refresh(void)
167
167
eq('variable', ret)
168
168
end)
169
169
170
+
it("supports caching queries", function()
171
+
local long_query = query:rep(100)
172
+
local first_run = exec_lua ([[
173
+
local before = vim.loop.hrtime()
174
+
cquery = vim.treesitter.parse_query("c", ...)
175
+
local after = vim.loop.hrtime()
176
+
return after - before
177
+
]], long_query)
178
+
179
+
local subsequent_runs = exec_lua ([[
180
+
local before = vim.loop.hrtime()
181
+
for i=1,100,1 do
182
+
cquery = vim.treesitter.parse_query("c", ...)
183
+
end
184
+
local after = vim.loop.hrtime()
185
+
return after - before
186
+
]], long_query)
187
+
188
+
assert.True(1000 * subsequent_runs < first_run)
189
+
end)
190
+
170
191
it('support query and iter by capture', function()
171
192
insert(test_text)
172
193
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4