Skip to content

Commit 666318f

Browse files
committed
Detach highlighter when switching syntax away, closes #13
1 parent 1e6e197 commit 666318f

File tree

2 files changed

+34
-21
lines changed

2 files changed

+34
-21
lines changed

lua/commonmarker/init.lua

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package.loaded["rust"] = nil -- Force module reload during dev
22
local rust = require("libvim_commonmark")
33

4-
local rustymarks = vim.api.nvim_create_namespace("rustymarks")
5-
64
-- The Lua API is verbose and repetative
75
local call_function = vim.api.nvim_call_function
86
local buf_add_highlight = vim.api.nvim_buf_add_highlight
97
local buf_attach = vim.api.nvim_buf_attach
108
local buf_get_lines = vim.api.nvim_buf_get_lines
119
local buf_clear_namespace = vim.api.nvim_buf_clear_namespace
1210

13-
local _attachments = {}
11+
local commonmarker = {
12+
_attachments = {},
13+
_namespace = vim.api.nvim_create_namespace("rustymarks")
14+
}
1415

1516
-- luacheck: ignore dump
1617
local function dump(...)
@@ -35,42 +36,49 @@ local function get_contents (buffer)
3536
return table.concat(lines)
3637
end
3738

38-
local function highlight (buffer)
39+
local function highlight (buffer, namespace)
3940
local contents = get_contents(buffer)
4041
local events = rust.get_offsets(contents)
4142
for _, event in ipairs(events) do
4243
local sline, scol = byte2pos(event.first)
4344
local eline, ecol = byte2pos(event.last)
4445
if sline < eline then
45-
buf_add_highlight(buffer, rustymarks, event.group, sline - 1, scol, -1)
46+
buf_add_highlight(buffer, namespace, event.group, sline - 1, scol, -1)
4647
sline = sline + 1
4748
while sline < eline do
48-
buf_add_highlight(buffer, rustymarks, event.group, sline - 1, 0, -1)
49+
buf_add_highlight(buffer, namespace, event.group, sline - 1, 0, -1)
4950
sline = sline + 1
5051
end
51-
buf_add_highlight(buffer, rustymarks, event.group, sline - 1, 0, ecol)
52+
buf_add_highlight(buffer, namespace, event.group, sline - 1, 0, ecol)
5253
else
53-
buf_add_highlight(buffer, rustymarks, event.group, sline - 1, scol, ecol)
54+
buf_add_highlight(buffer, namespace, event.group, sline - 1, scol, ecol)
5455
end
5556
end
5657
end
5758

58-
local function attach (buffer)
59-
if _attachments[buffer] then return end
60-
_attachments[buffer] = true
61-
highlight(buffer)
59+
commonmarker.detach = function (self, buffer)
60+
dump(self._attachments)
61+
self._attachments[buffer] = nil
62+
buf_clear_namespace(buffer, self._namespace, 0, -1)
63+
end
64+
65+
commonmarker.attach = function (self, buffer)
66+
if self._attachments[buffer] then return end
67+
self._attachments[buffer] = true
68+
highlight(buffer, self._namespace)
6269
buf_attach(buffer, false, {
6370
on_lines = function (_, _, _, _, _, _)
64-
if not _attachments[buffer] then return end
65-
buf_clear_namespace(buffer, rustymarks, 0, -1)
66-
highlight(buffer)
71+
dump(self)
72+
buf_clear_namespace(buffer, self._namespace, 0, -1)
73+
-- Returning true here detaches, we thought we should have been already
74+
if not self._attachments[buffer] then return true end
75+
highlight(buffer, self._namespace)
6776
end,
68-
on_detach = function ()
69-
_attachments[buffer] = nil
77+
on_detach = function (_)
78+
self._attachments[buffer] = nil
79+
self:detach(buffer)
7080
end
7181
})
7282
end
7383

74-
return {
75-
attach = attach,
76-
}
84+
return commonmarker

syntax/commonmark.vim

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ endif
5050

5151
lua package.loaded["commonmarker"] = nil -- Force module reload during dev
5252
lua commonmarker = require("commonmarker")
53-
lua commonmarker.attach(vim.api.nvim_get_current_buf())
53+
lua commonmarker:attach(vim.api.nvim_get_current_buf())
54+
55+
augroup commonmarker
56+
autocmd!
57+
autocmd Syntax * lua commonmarker:detach(vim.api.nvim_get_current_buf())
58+
augroup END
5459

5560
let b:current_syntax = 'commonmark'

0 commit comments

Comments
 (0)