diff --git a/lua/commonmarker/init.lua b/lua/commonmarker/init.lua index e235009..de2d1a8 100644 --- a/lua/commonmarker/init.lua +++ b/lua/commonmarker/init.lua @@ -25,7 +25,6 @@ end local function byte2pos (byte) local line = call_function("byte2line", { byte }) - -- local col = byte - vim.api.nvim_buf_get_offset(buffer, line) local col = byte - call_function("line2byte", { line }) return line, col end @@ -36,9 +35,11 @@ local function get_contents (buffer) return table.concat(lines) end -local function highlight (buffer, namespace) +local function highlight (buffer, namespace, firstline, lastline) local contents = get_contents(buffer) - local events = rust.get_offsets(contents) + local firstbyte = call_function("line2byte", { firstline }) + local lastbyte = call_function("line2byte", { lastline + 1 }) - 1 + local events = rust.get_offsets(contents, firstbyte, lastbyte) for _, event in ipairs(events) do local sline, scol = byte2pos(event.first) local eline, ecol = byte2pos(event.last) @@ -57,7 +58,6 @@ local function highlight (buffer, namespace) end function commonmarker:detach (buffer) - dump(self._attachments) self._attachments[buffer] = nil buf_clear_namespace(buffer, self._namespace, 0, -1) end @@ -65,14 +65,13 @@ end function commonmarker:attach (buffer) if self._attachments[buffer] then return end self._attachments[buffer] = true - highlight(buffer, self._namespace) + highlight(buffer, self._namespace, 1, call_function("line", { "$" })) buf_attach(buffer, false, { - on_lines = function (_, _, _, _, _, _) - dump(self) - buf_clear_namespace(buffer, self._namespace, 0, -1) + on_lines = function (_, _, _, firstline, lastline, _) + buf_clear_namespace(buffer, self._namespace, firstline, lastline) -- Returning true here detaches, we thought we should have been already if not self._attachments[buffer] then return true end - highlight(buffer, self._namespace) + highlight(buffer, self._namespace, firstline, lastline) end, on_detach = function (_) self._attachments[buffer] = nil diff --git a/src/lib.rs b/src/lib.rs index e2e9e68..e630d00 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,7 +27,7 @@ struct MdTag { type Events = Vec; -fn get_offsets(buffer: String) -> Result { +fn get_offsets(buffer: String, firstbyte: usize, lastbyte: usize) -> Result { let options = Options::all(); let parser = Parser::new_ext(buffer.as_str(), options); let mut events = Events::new(); @@ -35,6 +35,9 @@ fn get_offsets(buffer: String) -> Result { let first = range.start + 1; let last = range.end + 1; let mut lang = None; + if firstbyte > last || lastbyte < first { + continue; + } let group = match event { Event::Start(tag) => match tag { Tag::Heading(level) => Some(format!("cmarkHeading{}", level)), diff --git a/src/lua.rs b/src/lua.rs index bfc4fce..63444f3 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -5,9 +5,12 @@ fn to_html(_: &Lua, buffer: String) -> LuaResult { Ok(super::to_html(buffer).unwrap()) } -fn get_offsets(lua: &Lua, buffer: String) -> LuaResult { +fn get_offsets( + lua: &Lua, + (buffer, firstbyte, lastbyte): (String, usize, usize), +) -> LuaResult { let table = lua.create_table().unwrap(); - let events = super::get_offsets(buffer).unwrap(); + let events = super::get_offsets(buffer, firstbyte, lastbyte).unwrap(); for (i, event) in events.iter().enumerate() { let info = lua.create_table().unwrap(); info.set("group", event.group.as_str()).unwrap(); diff --git a/src/python.rs b/src/python.rs index 5d42f51..77f4a6d 100644 --- a/src/python.rs +++ b/src/python.rs @@ -8,8 +8,13 @@ fn to_html(_py: Python, buffer: String) -> PyResult { } #[pyfunction] -fn get_offsets(_py: Python, buffer: String) -> PyResult<&PyDict> { - let events = super::get_offsets(buffer).unwrap(); +fn get_offsets( + _py: Python, + buffer: String, + firstbyte: usize, + lastbyte: usize, +) -> PyResult<&PyDict> { + let events = super::get_offsets(buffer, firstbyte, lastbyte).unwrap(); let pyevents = PyDict::new(_py); let mut i: u32 = 1; for event in events.iter() {