|
| 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 | +} |