Skip to content

Commit 6615540

Browse files
committed
Add rudimentry tracking to not return data after the last line changed
1 parent 6b34c3a commit 6615540

File tree

4 files changed

+20
-11
lines changed

4 files changed

+20
-11
lines changed

lua/commonmarker/init.lua

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ local function get_contents (buffer)
3535
return table.concat(lines)
3636
end
3737

38-
local function highlight (buffer, namespace, firstline)
38+
local function highlight (buffer, namespace, firstline, lastline)
3939
local contents = get_contents(buffer)
4040
local firstbyte = call_function("line2byte", { firstline })
41+
local lastbyte = call_function("line2byte", { lastline + 1 }) - 1
4142
local events = rust.get_offsets(contents, firstbyte)
4243
for _, event in ipairs(events) do
4344
repeat -- Allow continue in for loop
@@ -67,13 +68,13 @@ end
6768
function commonmarker:attach (buffer)
6869
if self._attachments[buffer] then return end
6970
self._attachments[buffer] = true
70-
highlight(buffer, self._namespace, 0)
71+
highlight(buffer, self._namespace, 0, -1)
7172
buf_attach(buffer, false, {
72-
on_lines = function (_, _, _, firstline, _, _)
73-
buf_clear_namespace(buffer, self._namespace, firstline, -1)
73+
on_lines = function (_, _, _, firstline, lastline, _)
74+
buf_clear_namespace(buffer, self._namespace, firstline, lastline)
7475
-- Returning true here detaches, we thought we should have been already
7576
if not self._attachments[buffer] then return true end
76-
highlight(buffer, self._namespace, firstline)
77+
highlight(buffer, self._namespace, firstline, lastline)
7778
end,
7879
on_detach = function (_)
7980
self._attachments[buffer] = nil

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ struct MdTag {
2727

2828
type Events = Vec<MdTag>;
2929

30-
fn get_offsets(buffer: String, firstbyte: usize) -> Result<Events> {
30+
fn get_offsets(buffer: String, firstbyte: usize, lastbyte: usize) -> Result<Events> {
3131
let options = Options::all();
3232
let parser = Parser::new_ext(buffer.as_str(), options);
3333
let mut events = Events::new();
3434
for (event, range) in parser.into_offset_iter() {
3535
let first = range.start + 1;
3636
let last = range.end + 1;
3737
let mut lang = None;
38-
if first < firstbyte {
38+
if first < firstbyte || first > lastbyte {
3939
continue;
4040
}
4141
let group = match event {

src/lua.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ fn to_html(_: &Lua, buffer: String) -> LuaResult<String> {
55
Ok(super::to_html(buffer).unwrap())
66
}
77

8-
fn get_offsets(lua: &Lua, (buffer, firstbyte): (String, usize)) -> LuaResult<LuaTable> {
8+
fn get_offsets(
9+
lua: &Lua,
10+
(buffer, firstbyte, lastbyte): (String, usize, usize),
11+
) -> LuaResult<LuaTable> {
912
let table = lua.create_table().unwrap();
10-
let events = super::get_offsets(buffer, firstbyte).unwrap();
13+
let events = super::get_offsets(buffer, firstbyte, lastbyte).unwrap();
1114
for (i, event) in events.iter().enumerate() {
1215
let info = lua.create_table().unwrap();
1316
info.set("group", event.group.as_str()).unwrap();

src/python.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@ fn to_html(_py: Python, buffer: String) -> PyResult<String> {
88
}
99

1010
#[pyfunction]
11-
fn get_offsets(_py: Python, buffer: String, firstbyte: usize) -> PyResult<&PyDict> {
12-
let events = super::get_offsets(buffer, firstbyte).unwrap();
11+
fn get_offsets(
12+
_py: Python,
13+
buffer: String,
14+
firstbyte: usize,
15+
lastbyte: usize,
16+
) -> PyResult<&PyDict> {
17+
let events = super::get_offsets(buffer, firstbyte, lastbyte).unwrap();
1318
let pyevents = PyDict::new(_py);
1419
let mut i: u32 = 1;
1520
for event in events.iter() {

0 commit comments

Comments
 (0)