Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7c025c6

Browse files
authored
Merge pull request#134 from Hippo0o/master
Support treesitter and semantic_tokens highlighting (needs nvim 0.9.0)
2 parentsbd423cf +f7c8706 commit7c025c6

File tree

3 files changed

+134
-9
lines changed

3 files changed

+134
-9
lines changed

‎autoload/context/line.vim‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,11 @@ function! context#line#display(winid, join_parts) abort
160160
let width=0
161161
letstart= join_part.indent_chars
162162
for line_colinrange(start,start+len(join_part.text))
163-
let hlgroup=synIDattr(synIDtrans(synID(join_part.number, line_col+1,1)),'name')
163+
ifhas('nvim-0.9.0')
164+
let hlgroup=v:lua.require('context.highlight').nvim_hlgroup(a:winid, join_part.number, line_col)
165+
else
166+
let hlgroup=synIDattr(synIDtrans(synID(join_part.number, line_col+1,1)),'name')
167+
endif
164168

165169
if hlgroup== prev_hl
166170
let width+=1

‎autoload/context/popup/nvim.vim‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,10 @@ function! context#popup#nvim#redraw(winid, popup, lines) abort
4242
\'row':c.pos_y-1,
4343
\'col':c.pos_x-1,
4444
\'height':len(a:lines),
45-
\'width':c.size_w,
45+
\'width':c.size_w >0 ?c.size_w :1,
4646
\})
4747

4848
callsetwinvar(a:popup,'&list', &list)
49-
50-
" NOTE: because of some neovim limitation we have to temporarily switch to
51-
" the popup window so we can clear the highlighting
52-
" https://github.com/neovim/neovim/issues/10822
53-
execute'noautocmd'bufwinnr(buf) .'wincmd w'
54-
callclearmatches()
55-
wincmdp
5649
endfunction
5750

5851
function!context#popup#nvim#close(popup)abort

‎lua/context/highlight.lua‎

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
localM= {}
2+
localvim=vim
3+
localapi=vim.api
4+
localinspect_pos=vim.inspect_pos
5+
localcontext_update=vim.fn["context#update"]
6+
7+
functionM:fill_hl_cache(row,col)
8+
ifself.eol_col[row]==nilthen
9+
localline=api.nvim_buf_get_lines(self.buf,row-1,row,false)[1]
10+
self.eol_col[row]=lineand#lineor0
11+
end
12+
13+
-- workaround some treesitter captures not terminating at eol
14+
ifcol==self.eol_col[row]then
15+
return
16+
end
17+
18+
-- needs nvim >0.9.0
19+
localpos=inspect_pos(self.buf,row-1,col, {extmarks=false })
20+
21+
-- will fallback to treesitter if hl_group is empty
22+
if#pos.semantic_tokens>0then
23+
localhl_group=nil
24+
for_,tokeninipairs(pos.semantic_tokens)do
25+
-- TODO using nvim_get_hl() didn't work
26+
-- returns { [true] = 6 } if hl_group is empty
27+
ifapi.nvim_get_hl_by_name(token.opts.hl_group,true)[true]~=6then
28+
hl_group=token.opts.hl_group
29+
end
30+
end
31+
ifhl_groupthen
32+
self.cached[row][col]=hl_group
33+
return
34+
end
35+
end
36+
37+
if#pos.treesitter>0then
38+
-- last should have highest priority
39+
localhl_group=""
40+
for_,tokeninipairs(pos.treesitter)do
41+
ifnottoken.capture:match("^_")andnotvim.tbl_contains({"spell","conceal"},token.capture)then
42+
hl_group=token.hl_group
43+
end
44+
end
45+
self.cached[row][col]=hl_group
46+
return
47+
end
48+
49+
if#pos.syntax>0then
50+
-- last should have highest priority
51+
localhl_group=""
52+
for_,tokeninipairs(pos.syntax)do
53+
hl_group=token.hl_group
54+
end
55+
self.cached[row][col]=hl_group
56+
return
57+
end
58+
end
59+
60+
functionM:reset()
61+
self.cached= {}
62+
self.eol_col= {}
63+
end
64+
65+
functionM:get_highlight(row,col)
66+
self.cached[row]=self.cached[row]or {}
67+
ifself.cached[row][col]~=nilthen
68+
returnself.cached[row][col]
69+
end
70+
self.cached[row][col]=vim.g.context.highlight_normal
71+
72+
-- schedule to avoid stuttering
73+
self.scheduled=self.scheduled+1
74+
vim.schedule(function()
75+
self.scheduled=self.scheduled-1
76+
ifself.cached[row]==nilornotapi.nvim_buf_is_valid(self.buf)then
77+
return
78+
end
79+
self:fill_hl_cache(row,col)
80+
ifself.scheduled<1then
81+
-- last to finish triggers update
82+
self.scheduled=0
83+
context_update("OptionSet")-- need to use OptionSet to force update
84+
end
85+
end)
86+
87+
returnself.cached[row][col]
88+
end
89+
90+
functionM.new(buf)
91+
localm=setmetatable({}, {__index=M })
92+
m.buf=buf
93+
m.cached= {}
94+
m.eol_col= {}
95+
m.scheduled=0
96+
97+
returnm
98+
end
99+
100+
local_list= {}
101+
localfunctionnvim_hlgroup(winid,row,col)
102+
localbuf=api.nvim_win_get_buf(winid)
103+
localm=_list[buf]
104+
ifm==nilthen
105+
m=M.new(buf)
106+
api.nvim_buf_attach(buf,false, {
107+
on_detach=function()
108+
_list[buf]=nil
109+
end,
110+
on_lines=function()
111+
m:reset()
112+
end,
113+
})
114+
_list[buf]=m
115+
end
116+
returnm:get_highlight(row,col)
117+
end
118+
119+
return {
120+
nvim_hlgroup=nvim_hlgroup,
121+
clear_cache=function(buf)
122+
ifbufthen
123+
_list[buf]=nil
124+
else
125+
_list= {}
126+
end
127+
end,
128+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp