aboutsummaryrefslogtreecommitdiffstats
path: root/lua
diff options
context:
space:
mode:
authorSteven Sojka <steelsojka@users.noreply.github.com>2020-08-22 09:10:28 -0500
committerGitHub <noreply@github.com>2020-08-22 09:10:28 -0500
commit3b2cb65d4cfc6e86026f8cc6bf63c180d271ebe3 (patch)
tree462836d0e88a0672ccf538d81ddf80011493d56b /lua
parentfix(smart_rename): fix usages call (diff)
parentfix(modules): do not reattach if already attached (diff)
downloadnvim-treesitter-3b2cb65d4cfc6e86026f8cc6bf63c180d271ebe3.tar
nvim-treesitter-3b2cb65d4cfc6e86026f8cc6bf63c180d271ebe3.tar.gz
nvim-treesitter-3b2cb65d4cfc6e86026f8cc6bf63c180d271ebe3.tar.bz2
nvim-treesitter-3b2cb65d4cfc6e86026f8cc6bf63c180d271ebe3.tar.lz
nvim-treesitter-3b2cb65d4cfc6e86026f8cc6bf63c180d271ebe3.tar.xz
nvim-treesitter-3b2cb65d4cfc6e86026f8cc6bf63c180d271ebe3.tar.zst
nvim-treesitter-3b2cb65d4cfc6e86026f8cc6bf63c180d271ebe3.zip
Merge pull request #330 from steelsojka/fix-do-not-reattach
fix(modules): do not reattach if already attached
Diffstat (limited to 'lua')
-rw-r--r--lua/nvim-treesitter/caching.lua48
-rw-r--r--lua/nvim-treesitter/configs.lua12
-rw-r--r--lua/nvim-treesitter/highlight.lua10
-rw-r--r--lua/nvim-treesitter/incremental_selection.lua10
-rw-r--r--lua/nvim-treesitter/query.lua19
-rw-r--r--lua/nvim-treesitter/refactor/highlight_current_scope.lua2
-rw-r--r--lua/nvim-treesitter/refactor/highlight_definitions.lua2
-rw-r--r--lua/nvim-treesitter/refactor/navigation.lua2
-rw-r--r--lua/nvim-treesitter/refactor/smart_rename.lua1
-rw-r--r--lua/nvim-treesitter/textobjects/attach.lua12
10 files changed, 76 insertions, 42 deletions
diff --git a/lua/nvim-treesitter/caching.lua b/lua/nvim-treesitter/caching.lua
new file mode 100644
index 000000000..bfda39622
--- /dev/null
+++ b/lua/nvim-treesitter/caching.lua
@@ -0,0 +1,48 @@
+local api = vim.api
+
+local M = {}
+
+--- Creates a cache table for buffers keyed by a type name.
+--- Cache entries attach to the buffer and cleanup entries
+--- as buffers are detached.
+function M.create_buffer_cache()
+ local cache = {}
+
+ local items = setmetatable({}, {
+ __index = function(tbl, key)
+ rawset(tbl, key, {})
+ return rawget(tbl, key)
+ end
+ })
+
+ function cache.set(type_name, bufnr, value)
+ if not cache.has(type_name, bufnr) then
+ -- Clean up the cache if the buffer is detached
+ -- to avoid memory leaks
+ api.nvim_buf_attach(bufnr, false, {
+ on_detach = function()
+ cache.remove(type_name, bufnr)
+ return true
+ end
+ })
+ end
+
+ items[type_name][bufnr] = value
+ end
+
+ function cache.get(type_name, bufnr)
+ return items[type_name][bufnr]
+ end
+
+ function cache.has(type_name, bufnr)
+ return cache.get(type_name, bufnr) ~= nil
+ end
+
+ function cache.remove(type_name, bufnr)
+ items[type_name][bufnr] = nil
+ end
+
+ return cache
+end
+
+return M
diff --git a/lua/nvim-treesitter/configs.lua b/lua/nvim-treesitter/configs.lua
index ba4ae3f8d..51d982c79 100644
--- a/lua/nvim-treesitter/configs.lua
+++ b/lua/nvim-treesitter/configs.lua
@@ -3,6 +3,7 @@ local api = vim.api
local queries = require'nvim-treesitter.query'
local parsers = require'nvim-treesitter.parsers'
local utils = require'nvim-treesitter.utils'
+local caching = require'nvim-treesitter.caching'
local M = {}
@@ -109,6 +110,8 @@ local builtin_modules = {
}
}
+local attached_buffers_by_module = caching.create_buffer_cache()
+
-- Resolves a module by requiring the `module_path` or using the module definition.
local function resolve_module(mod_name)
local config_mod = M.get_module(mod_name)
@@ -367,8 +370,13 @@ end
-- @param lang the language of the buffer
function M.attach_module(mod_name, bufnr, lang)
local resolved_mod = resolve_module(mod_name)
+ local bufnr = bufnr or api.nvim_get_current_buf()
- if resolved_mod and parsers.has_parser(lang) then
+ if resolved_mod
+ and parsers.has_parser(lang)
+ and not attached_buffers_by_module.has(mod_name, bufnr)
+ then
+ attached_buffers_by_module.set(mod_name, bufnr, true)
resolved_mod.attach(bufnr, lang)
end
end
@@ -378,8 +386,10 @@ end
-- @param bufnr the bufnr
function M.detach_module(mod_name, bufnr)
local resolved_mod = resolve_module(mod_name)
+ local bufnr = bufnr or api.nvim_get_current_buf()
if resolved_mod then
+ attached_buffers_by_module.remove(mod_name, bufnr)
resolved_mod.detach(bufnr)
end
end
diff --git a/lua/nvim-treesitter/highlight.lua b/lua/nvim-treesitter/highlight.lua
index 1fbc6621d..a9549fc06 100644
--- a/lua/nvim-treesitter/highlight.lua
+++ b/lua/nvim-treesitter/highlight.lua
@@ -69,7 +69,6 @@ hlmap["text.literal"] = "TSLiteral"
hlmap["text.uri"] = "TSURI"
function M.attach(bufnr, lang)
- local bufnr = bufnr or api.nvim_get_current_buf()
local lang = lang or parsers.get_buf_lang(bufnr)
local config = configs.get_module('highlight')
@@ -84,12 +83,11 @@ function M.attach(bufnr, lang)
end
function M.detach(bufnr)
- local buf = bufnr or api.nvim_get_current_buf()
- if M.highlighters[buf] then
- M.highlighters[buf]:set_query("")
- M.highlighters[buf] = nil
+ if M.highlighters[bufnr] then
+ M.highlighters[bufnr]:set_query("")
+ M.highlighters[bufnr] = nil
end
- api.nvim_buf_set_option(buf, 'syntax', 'on')
+ api.nvim_buf_set_option(bufnr, 'syntax', 'on')
end
return M
diff --git a/