Better sematic highlighter
This commit is contained in:
parent
161cdef318
commit
2efef18008
4 changed files with 72 additions and 53 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
doc/tags
|
||||||
|
|
@ -3,7 +3,40 @@ if exists("s:loaded")
|
||||||
endif
|
endif
|
||||||
let s:loaded = 1
|
let s:loaded = 1
|
||||||
|
|
||||||
let s:NimHighlighter = {}
|
let s:highlights = {
|
||||||
|
\ 'skProc': "Function",
|
||||||
|
\ 'skTemplate': "PreProc",
|
||||||
|
\ 'skType': "Type",
|
||||||
|
\ 'skMacro': "Macro",
|
||||||
|
\ 'skMethod': "Function",
|
||||||
|
\ 'skField': "Identifier",
|
||||||
|
\ 'skAlias': "Type",
|
||||||
|
\ 'skConditional': "Conditional",
|
||||||
|
\ 'skConst': "Constant",
|
||||||
|
\ 'skConverter': "Function",
|
||||||
|
\ 'skDynLib': "Include",
|
||||||
|
\ 'skEnumField': "Identifier",
|
||||||
|
\ 'skForVar': "Special",
|
||||||
|
\ 'skGenericParam': "Typedef",
|
||||||
|
\ 'skGlobalVar': "Constant",
|
||||||
|
\ 'skGlobalLet': "Constant",
|
||||||
|
\ 'skIterator': "Keyword",
|
||||||
|
\ 'skLabel': "Identifier",
|
||||||
|
\ 'skLet': "Constant",
|
||||||
|
\ 'skModule': "Include",
|
||||||
|
\ 'skPackage': "Define",
|
||||||
|
\ 'skParam': "Identifier",
|
||||||
|
\ 'skResult': "Keyword",
|
||||||
|
\ 'skStub': "PreCondit",
|
||||||
|
\ 'skTemp': "Identifier",
|
||||||
|
\ 'skUnknown': "Error",
|
||||||
|
\ 'skVar': "Constant",
|
||||||
|
\ }
|
||||||
|
|
||||||
|
|
||||||
|
let s:NimHighlighter = {
|
||||||
|
\ }
|
||||||
|
" \ 'pty': 1
|
||||||
|
|
||||||
function! s:NimHighlighter.on_stdout(job, chunk)
|
function! s:NimHighlighter.on_stdout(job, chunk)
|
||||||
if len(a:chunk[0]) != 0 && !(a:chunk[0] =~ "^usage")
|
if len(a:chunk[0]) != 0 && !(a:chunk[0] =~ "^usage")
|
||||||
|
|
@ -19,35 +52,16 @@ function! s:NimHighlighter.on_exit()
|
||||||
return
|
return
|
||||||
endif
|
endif
|
||||||
|
|
||||||
let highlights = {
|
for m in b:highlights
|
||||||
\ 'skProc': ["Function", [], 0],
|
call matchdelete(m)
|
||||||
\ 'skTemplate': ["PreProc", [], 0],
|
endfor
|
||||||
\ 'skType': ["Type", [], 0],
|
|
||||||
\ 'skMacro': ["Macro", [], 0],
|
let b:highlights = []
|
||||||
\ 'skMethod': ["Function", [], 0],
|
let semantics_set = {}
|
||||||
\ 'skField': ["Identifier", [], 0],
|
|
||||||
\ 'skAlias': ["Type", [], 0],
|
for ctype in g:nvim_nim_highlighter_semantics
|
||||||
\ 'skConditional': ["Conditional", [], 0],
|
let semantics_set[ctype] = 1
|
||||||
\ 'skConst': ["Constant", [], 1],
|
endfor
|
||||||
\ 'skConverter': ["Function", [], 0],
|
|
||||||
\ 'skDynLib': ["Include", [], 0],
|
|
||||||
\ 'skEnumField': ["Identifier", [], 0],
|
|
||||||
\ 'skForVar': ["Special", [], 1],
|
|
||||||
\ 'skGenericParam': ["Typedef", [], 0],
|
|
||||||
\ 'skGlobalVar': ["Constant", [], 1],
|
|
||||||
\ 'skGlobalLet': ["Constant", [], 1],
|
|
||||||
\ 'skIterator': ["Keyword", [], 0],
|
|
||||||
\ 'skLabel': ["Identifier", [], 0],
|
|
||||||
\ 'skLet': ["Constant", [], 1],
|
|
||||||
\ 'skModule': ["Include", [], 1],
|
|
||||||
\ 'skPackage': ["Define", [], 0],
|
|
||||||
\ 'skParam': ["Identifier", [], 1],
|
|
||||||
\ 'skResult': ["Keyword", [], 0],
|
|
||||||
\ 'skStub': ["PreCondit", [], 0],
|
|
||||||
\ 'skTemp': ["Identifier", [], 1],
|
|
||||||
\ 'skUnknown': ["Error", [], 0],
|
|
||||||
\ 'skVar': ["Constant", [], 1]
|
|
||||||
\ }
|
|
||||||
|
|
||||||
for line in self.lines
|
for line in self.lines
|
||||||
if len(line) == 0
|
if len(line) == 0
|
||||||
|
|
@ -59,34 +73,28 @@ function! s:NimHighlighter.on_exit()
|
||||||
continue
|
continue
|
||||||
endif
|
endif
|
||||||
|
|
||||||
let line = p[2] + 0
|
let ctype = p[1]
|
||||||
let c = p[3] + 1
|
let line = p[2] + 0
|
||||||
let s = p[4] + 0
|
let c = p[3] + 1
|
||||||
if getline(line)[c - 1] == '*'
|
let s = p[4] + 0
|
||||||
|
let str = getline(line)
|
||||||
|
|
||||||
|
" Hack because nimsuggest gives wrong place
|
||||||
|
if str[c - 1] == '*'
|
||||||
let c -= s
|
let c -= s
|
||||||
endif
|
endif
|
||||||
call add(highlights[p[1]][1], [line, c, s])
|
|
||||||
endfor
|
|
||||||
|
|
||||||
let new_highlights = []
|
if has_key(s:highlights, ctype)
|
||||||
for [k, v] in items(highlights)
|
if has_key(semantics_set, ctype)
|
||||||
if g:nvim_nim_highlighter_semantic && v[2]
|
call add(b:highlights, matchaddpos("Semantic" . util#djb(strpart(str, c - 1, s)) % 20, [[line, c, s]]))
|
||||||
for pos in v[1]
|
else
|
||||||
let l = getline(pos[0])
|
call add(b:highlights, matchaddpos(s:highlights[ctype], [[line, c, s]]))
|
||||||
let c = pos[1]
|
endif
|
||||||
let class = (char2nr(l[c]) * char2nr(l[c + 1])) % 20
|
|
||||||
call add(new_highlights, matchaddpos("Semantic" . class, [pos]))
|
|
||||||
endfor
|
|
||||||
else
|
else
|
||||||
call add(new_highlights, matchaddpos(v[0], v[1]))
|
echoerr "No such key: " . p[1]
|
||||||
endif
|
endif
|
||||||
endfor
|
endfor
|
||||||
|
|
||||||
for m in b:old_highlights
|
|
||||||
call matchdelete(m)
|
|
||||||
endfor
|
|
||||||
|
|
||||||
let b:old_highlights = new_highlights
|
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function highlighter#New()
|
function highlighter#New()
|
||||||
|
|
@ -97,8 +105,8 @@ function highlighter#New()
|
||||||
let tempfile = result.file . ".temp"
|
let tempfile = result.file . ".temp"
|
||||||
call writefile(getline(1, '$'), tempfile)
|
call writefile(getline(1, '$'), tempfile)
|
||||||
call jobsend(result.job, "highlight " . result.file . ";" . tempfile . ":1:1\nquit\n")
|
call jobsend(result.job, "highlight " . result.file . ";" . tempfile . ":1:1\nquit\n")
|
||||||
if !exists("b:old_highlights")
|
if !exists("b:highlights")
|
||||||
let b:old_highlights = []
|
let b:highlights = []
|
||||||
endif
|
endif
|
||||||
return result
|
return result
|
||||||
endfunction
|
endfunction
|
||||||
|
|
|
||||||
|
|
@ -187,3 +187,11 @@ function! util#SignatureStr(input)
|
||||||
let tinfo = util#ParseSignature(a:input)
|
let tinfo = util#ParseSignature(a:input)
|
||||||
return join(tinfo.params, " -> ") . (tinfo.reval != "" ? (" => " . tinfo.reval) : "")
|
return join(tinfo.params, " -> ") . (tinfo.reval != "" ? (" => " . tinfo.reval) : "")
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
function! util#djb(str)
|
||||||
|
let hash = 0
|
||||||
|
for s:char in split(a:str, '\zs')
|
||||||
|
let hash = (hash * 11) + char2nr(s:char)
|
||||||
|
endfor
|
||||||
|
return float2nr(hash)
|
||||||
|
endfunction
|
||||||
|
|
|
||||||
|
|
@ -13,3 +13,5 @@ let g:nvim_nim_highlight_builtin = 1
|
||||||
let g:nvim_nim_highlight_use_unite = 0
|
let g:nvim_nim_highlight_use_unite = 0
|
||||||
let g:nvim_nim_autocomplete = "omni" " omni, deoplete, ycm
|
let g:nvim_nim_autocomplete = "omni" " omni, deoplete, ycm
|
||||||
let g:nvim_nim_outline = "quickfix" " quickfix, outline
|
let g:nvim_nim_outline = "quickfix" " quickfix, outline
|
||||||
|
|
||||||
|
let g:nvim_nim_highlighter_semantics = ["skConst", "skForVar", "skGlobalVar", "skGlobalLet", "skLet", "skModule", "skParam", "skTemp", "skVar"]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue