Skip to content

Commit 88db99b

Browse files
committed
👍 Add listDecorations function in buffer/decoration.ts
1 parent f38feeb commit 88db99b

File tree

2 files changed

+119
-1
lines changed

2 files changed

+119
-1
lines changed

buffer/decoration.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,20 @@ export function undecorate(
133133
}
134134
}
135135

136+
export function listDecorations(
137+
denops: Denops,
138+
bufnr: number,
139+
): Promise<Decoration[]> {
140+
switch (denops.meta.host) {
141+
case "vim":
142+
return vimListDecorations(denops, bufnr);
143+
case "nvim":
144+
return nvimListDecorations(denops, bufnr);
145+
default:
146+
unreachable(denops.meta.host);
147+
}
148+
}
149+
136150
function uniq<T>(array: readonly T[]): T[] {
137151
return [...new Set(array)];
138152
}
@@ -192,6 +206,33 @@ async function vimUndecorate(
192206
});
193207
}
194208

209+
async function vimListDecorations(
210+
denops: Denops,
211+
bufnr: number,
212+
): Promise<Decoration[]> {
213+
const props = await vimFn.prop_list(denops, 1, {
214+
bufnr,
215+
end_lnum: -1,
216+
}) as {
217+
col: number;
218+
end: number;
219+
id: number;
220+
length: number;
221+
lnum: number;
222+
start: number;
223+
type: string;
224+
type_bufnr: number;
225+
}[];
226+
return props
227+
.filter((prop) => prop.type.startsWith(`${prefix}:`))
228+
.map((prop) => ({
229+
line: prop.lnum,
230+
column: prop.col,
231+
length: prop.length,
232+
highlight: prop.type.split(":").pop() as string,
233+
}));
234+
}
235+
195236
async function nvimDecorate(
196237
denops: Denops,
197238
bufnr: number,
@@ -224,3 +265,38 @@ async function nvimUndecorate(
224265
const ns = await nvimFn.nvim_create_namespace(denops, prefix);
225266
await nvimFn.nvim_buf_clear_namespace(denops, bufnr, ns, start, end);
226267
}
268+
269+
async function nvimListDecorations(
270+
denops: Denops,
271+
bufnr: number,
272+
): Promise<Decoration[]> {
273+
const ns = await nvimFn.nvim_create_namespace(denops, prefix);
274+
const extmarks = await nvimFn.nvim_buf_get_extmarks(
275+
denops,
276+
bufnr,
277+
ns,
278+
0,
279+
-1,
280+
{ details: true },
281+
) as [
282+
extmark_id: number,
283+
row: number,
284+
col: number,
285+
{
286+
hl_group: string;
287+
hl_eol: boolean;
288+
end_right_gravity: boolean;
289+
priority: number;
290+
right_gravity: boolean;
291+
end_col: number;
292+
ns_id: number;
293+
end_row: number;
294+
},
295+
][];
296+
return extmarks.map((extmark) => ({
297+
line: extmark[1] + 1,
298+
column: extmark[2] + 1,
299+
length: extmark[3].end_col - extmark[2],
300+
highlight: extmark[3].hl_group,
301+
}));
302+
}

buffer/decoration_test.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as fn from "../function/mod.ts";
44
import * as vimFn from "../function/vim/mod.ts";
55
import * as nvimFn from "../function/nvim/mod.ts";
66
import * as buffer from "./buffer.ts";
7-
import { decorate } from "./decoration.ts";
7+
import { decorate, listDecorations } from "./decoration.ts";
88

99
test({
1010
mode: "vim",
@@ -107,3 +107,45 @@ test({
107107
);
108108
},
109109
});
110+
111+
test({
112+
mode: "all",
113+
name: "listDecorations list decorations defined in the buffer",
114+
fn: async (denops) => {
115+
const bufnr = await fn.bufnr(denops);
116+
await buffer.replace(denops, bufnr, [
117+
"Hello",
118+
"Darkness",
119+
"My",
120+
"Old friend",
121+
]);
122+
await decorate(denops, bufnr, [
123+
{
124+
line: 1,
125+
column: 1,
126+
length: 5,
127+
highlight: "Title",
128+
},
129+
{
130+
line: 2,
131+
column: 2,
132+
length: 3,
133+
highlight: "Search",
134+
},
135+
]);
136+
assertEquals(await listDecorations(denops, bufnr), [
137+
{
138+
line: 1,
139+
column: 1,
140+
length: 5,
141+
highlight: "Title",
142+
},
143+
{
144+
line: 2,
145+
column: 2,
146+
length: 3,
147+
highlight: "Search",
148+
},
149+
]);
150+
},
151+
});

0 commit comments

Comments
 (0)