Nerd font v3 for treesitter

pull/293/head
ray-x 7 months ago
parent 452eead83b
commit 50973a602a

@ -556,7 +556,7 @@ LspDiagnosticsXXX are used for diagnostic. Please check highlight.lua and dochig
The plugin can be loaded lazily (packer `opt = true` ), And it will check if optional plugins existance and load those plugins only if they existed. The plugin can be loaded lazily (packer `opt = true` ), And it will check if optional plugins existance and load those plugins only if they existed.
The terminal will need to be able to output nerdfont and emoji correctly. I am using Kitty with nerdfont (Victor Mono). The terminal will need to be able to output nerdfont(v.3.0+) and emoji correctly. I am using Kitty with nerdfont (Victor Mono).
## Integrate with mason (williamboman/mason.nvim) ## Integrate with mason (williamboman/mason.nvim)

@ -188,6 +188,7 @@ _NgConfigValues = {
-- Values (floating window) -- Values (floating window)
value_definition = '🐶🍡', -- identifier defined value_definition = '🐶🍡', -- identifier defined
value_changed = '📝', -- identifier modified value_changed = '📝', -- identifier modified
context_separator = '', -- separator between text and value
-- Formatting for Side Panel -- Formatting for Side Panel
side_panel = { side_panel = {

@ -89,7 +89,8 @@ function M.find_definition(range, bufnr)
if not root then if not root then
return return
end end
local node_at_point = root:named_descendant_for_range(symbolpos[1], symbolpos[2], symbolpos[1], symbolpos[2]) local node_at_point =
root:named_descendant_for_range(symbolpos[1], symbolpos[2], symbolpos[1], symbolpos[2])
if not node_at_point then if not node_at_point then
lerr('no node at cursor') lerr('no node at cursor')
return return
@ -108,7 +109,10 @@ function M.find_definition(range, bufnr)
return { start = { line = r, character = c } } return { start = { line = r, character = c } }
else else
if definition then if definition then
trace('error: def not found in ', bufnr, definition:range(), definition:type(), definition:parent():type())
-- stylua: ignore start
trace( 'error: def not found in ', bufnr, definition:range(), definition:type(), definition:parent():type())
-- stylua: ignore end
end end
end end
end end
@ -142,19 +146,24 @@ function M.get_tsnode_at_pos(pos, bufnr, ignore_injected_langs)
return return
end end
return root:named_descendant_for_range(cursor_range[1], cursor_range[2], cursor_range[1], cursor_range[2]) return root:named_descendant_for_range(
cursor_range[1],
cursor_range[2],
cursor_range[1],
cursor_range[2]
)
end end
-- Trim spaces and opening brackets from end -- Trim spaces and opening brackets from end
local transform_line = function(line) local transform_line = function(line)
line = line:gsub("%s*[%[%(%{]*%s*$", "") line = line:gsub('%s*[%[%(%{]*%s*$', '')
line = line:gsub("function", "") line = line:gsub('function', '')
line = line:gsub("func%w*%s+", "") line = line:gsub('func%w*%s+', '')
if _NgConfigValues.treesitter_analysis_condense then if _NgConfigValues.treesitter_analysis_condense then
line = line:gsub("%([%a%.,%s%[%]%*]+%)", "()") line = line:gsub('%([%a%.,%s%[%]%*]+%)', '()')
-- this is for multi return -- this is for multi return
line = line:gsub("%([%a%.,%s%[%]%*]+%)", "()") line = line:gsub('%([%a%.,%s%[%]%*]+%)', '()')
line = line:gsub("%(%)%s*%(%)", "()") line = line:gsub('%(%)%s*%(%)', '()')
end end
return line return line
end end
@ -168,17 +177,17 @@ function M.ref_context(opts)
local bufnr = options.bufnr or 0 local bufnr = options.bufnr or 0
local pos = options.pos local pos = options.pos
if not pos then if not pos then
pos = {start = vim.lsp.util.make_position_params().position} pos = { start = vim.lsp.util.make_position_params().position }
end end
local indicator_size = options.indicator_size or 100 local indicator_size = options.indicator_size or 100
local type_patterns = options.type_patterns or { "class", "function", "method" } local type_patterns = options.type_patterns or { 'class', 'function', 'method' }
local transform_fn = options.transform_fn or transform_line local transform_fn = options.transform_fn or transform_line
local separator = options.separator or "" local separator = options.separator or _NgConfigValues.icons.context_separator
local current_node = M.get_tsnode_at_pos(pos, bufnr) local current_node = M.get_tsnode_at_pos(pos, bufnr)
if not current_node then if not current_node then
log('no node at pos', bufnr, pos) log('no node at pos', bufnr, pos)
return "" return ''
end end
local lines = {} local lines = {}
@ -187,7 +196,7 @@ function M.ref_context(opts)
while expr do while expr do
local line = ts_utils._get_line_for_node(expr, type_patterns, transform_fn, bufnr) local line = ts_utils._get_line_for_node(expr, type_patterns, transform_fn, bufnr)
log(line) log(line)
if line ~= "" and not vim.tbl_contains(lines, line) then if line ~= '' and not vim.tbl_contains(lines, line) then
table.insert(lines, 1, line) table.insert(lines, 1, line)
end end
expr = expr:parent() expr = expr:parent()
@ -223,7 +232,11 @@ local function get_definitions(bufnr)
trace(node, match) trace(node, match)
trace(row, col, erow, offset, node:parent(), node:parent():start(), node:parent():type()) trace(row, col, erow, offset, node:parent(), node:parent():start(), node:parent():type())
if node and node:parent() and string.find(node:parent():type(), 'parameter_declaration') then if
node
and node:parent()
and string.find(node:parent():type(), 'parameter_declaration')
then
log('parameter_declaration skip') log('parameter_declaration skip')
return return
end end
@ -234,7 +247,10 @@ local function get_definitions(bufnr)
if loc.method then -- for go if loc.method then -- for go
ts_locals.recurse_local_nodes(loc.method, function(def, node, full_match, match) ts_locals.recurse_local_nodes(loc.method, function(def, node, full_match, match)
local row, col, start = node:start() local row, col, start = node:start()
trace(row, col, start, def, node, full_match, match, node:parent(), node:parent():start(), node:parent():type()) -- stylua: ignore start
trace(row, col, start, def, node, full_match, match,
node:parent(), node:parent():start(), node:parent():type())
-- stylua: ignore end
if node:type() == 'field_identifier' and nodes_set[start] == nil then if node:type() == 'field_identifier' and nodes_set[start] == nil then
nodes_set[start] = { node = node, type = 'method' } nodes_set[start] = { node = node, type = 'method' }
end end
@ -243,7 +259,10 @@ local function get_definitions(bufnr)
if loc.interface then -- for go using interface can output full method definition if loc.interface then -- for go using interface can output full method definition
ts_locals.recurse_local_nodes(loc.interface, function(def, node, full_match, match) ts_locals.recurse_local_nodes(loc.interface, function(def, node, full_match, match)
local k, l, start = node:start() local k, l, start = node:start()
trace(k, l, start, def, node, full_match, match, node:parent(), node:parent():start(), node:parent():type()) -- stylua: ignore start
trace( k, l, start, def, node, full_match,
match, node:parent(), node:parent():start(), node:parent():type())
-- stylua: ignore end
if nodes_set[start] == nil then if nodes_set[start] == nil then
nodes_set[start] = { node = node, type = match or '' } nodes_set[start] = { node = node, type = match or '' }
end end
@ -265,7 +284,9 @@ local function get_definitions(bufnr)
p3 = p2:parent() p3 = p2:parent()
p3t = p2:parent():type() p3t = p2:parent():type()
end end
trace(row, col, start, def, node, full_match, match, p1t, p1, node:parent():start(), node:parent():type(), p2, p2t, p3, p3t) -- stylua: ignore start
trace( row, col, start, def, node, full_match, match, p1t, p1, node:parent():start(), node:parent():type(), p2, p2t, p3, p3t)
-- stylua: ignore end
if p1t == 'arrow_function' then if p1t == 'arrow_function' then
row, col, start = p1:start() row, col, start = p1:start()
trace('arrow_function 1', row, col) trace('arrow_function 1', row, col)
@ -349,7 +370,11 @@ local function get_scope(type, source)
end end
if type == 'var' and next ~= nil then if type == 'var' and next ~= nil then
if next:type() == 'function' or next:type() == 'arrow_function' or next:type() == 'function_definition' then if
next:type() == 'function'
or next:type() == 'arrow_function'
or next:type() == 'function_definition'
then
trace(current:type(), current:range()) trace(current:type(), current:range())
return next, true return next, true
elseif parent:type() == 'function_declaration' then elseif parent:type() == 'function_declaration' then
@ -412,7 +437,9 @@ function M.goto_adjacent_usage(bufnr, delta)
if type(en) == 'table' then if type(en) == 'table' then
en = vim.tbl_contains(en, vim.o.ft) en = vim.tbl_contains(en, vim.o.ft)
end end
if en == false then return lsp_reference(opt) end if en == false then
return lsp_reference(opt)
end
bufnr = bufnr or api.nvim_get_current_buf() bufnr = bufnr or api.nvim_get_current_buf()
local node_at_point = ts_utils.get_node_at_cursor() local node_at_point = ts_utils.get_node_at_cursor()
@ -488,17 +515,18 @@ local function get_all_nodes(bufnr, filter, summary)
local display_filename = fname:gsub(cwd .. path_sep, path_cur, 1) local display_filename = fname:gsub(cwd .. path_sep, path_cur, 1)
local all_nodes = {} local all_nodes = {}
local containers = filter or { local containers = filter
['function'] = true, or {
['local_function'] = true, ['function'] = true,
['arrow_function'] = true, ['local_function'] = true,
['type'] = true, ['arrow_function'] = true,
['class'] = true, ['type'] = true,
['call_expression'] = true, ['class'] = true,
-- ['var'] = true, ['call_expression'] = true,
['struct'] = true, -- ['var'] = true,
['method'] = true, ['struct'] = true,
} ['method'] = true,
}
-- check and load buff -- check and load buff
@ -578,7 +606,9 @@ local function get_all_nodes(bufnr, filter, summary)
-- hack for lua and maybe other language aswell -- hack for lua and maybe other language aswell
local parent = tsdata:parent() local parent = tsdata:parent()
if parent ~= nil and _NgConfigValues.debug == 'trace' then -- for github action failure if parent ~= nil and _NgConfigValues.debug == 'trace' then -- for github action failure
trace(parent:type(), vim.treesitter.get_node_text(parent, bufnr):sub(1, 30), item.node_text, item.type) -- stylua: ignore start
trace( parent:type(), vim.treesitter.get_node_text(parent, bufnr):sub(1, 30), item.node_text, item.type)
-- stylua: ignore end
end end
if if
parent ~= nil parent ~= nil
@ -617,15 +647,10 @@ local function get_all_nodes(bufnr, filter, summary)
end end
if item.node_scope then if item.node_scope then
trace( -- stylua: ignore start
item.type, trace( item.type, tsdata:type(), item.node_text, item.kind, 'range',
tsdata:type(), item.node_scope.start.line, item.node_scope['end'].line) -- set to log if need to trace result
item.node_text, -- stylua: ignore end
item.kind,
'range',
item.node_scope.start.line,
item.node_scope['end'].line
) -- set to log if need to trace result
end end
goto continue goto continue
end end
@ -633,7 +658,8 @@ local function get_all_nodes(bufnr, filter, summary)
item.range = ts_utils.node_to_lsp_range(tsdata) item.range = ts_utils.node_to_lsp_range(tsdata)
local start_line_node, _, _ = tsdata:start() local start_line_node, _, _ = tsdata:start()
local line_text = api.nvim_buf_get_lines(bufnr, start_line_node, start_line_node + 1, false)[1] or '' local line_text = api.nvim_buf_get_lines(bufnr, start_line_node, start_line_node + 1, false)[1]
or ''
item.full_text = vim.trim(line_text) item.full_text = vim.trim(line_text)
item.full_text = item.full_text:gsub('%s*[%[%(%{]*%s*$', '') item.full_text = item.full_text:gsub('%s*[%[%(%{]*%s*$', '')
@ -670,7 +696,8 @@ local function get_all_nodes(bufnr, filter, summary)
all_nodes[#all_nodes].next_indent_level = #parents all_nodes[#all_nodes].next_indent_level = #parents
end end
item.text = string.format(' %s %s%-10s\t %s', item.kind, indent, item.node_text, item.full_text) item.text =
string.format(' %s %s%-10s\t %s', item.kind, indent, item.node_text, item.full_text)
if #item.text > length then if #item.text > length then
length = #item.text length = #item.text
end end
@ -770,7 +797,7 @@ function M.side_panel()
end end
return require('navigator.treesitter').all_ts_nodes(b) return require('navigator.treesitter').all_ts_nodes(b)
end, end,
scope = 'node_scope' scope = 'node_scope',
}) })
panel:open(true) panel:open(true)
end end
@ -917,7 +944,12 @@ function M.get_node_at_pos(pos, parser)
return return
end end
local node = root:named_descendant_for_range(cursor_range[1], cursor_range[2], cursor_range[1], cursor_range[2]) local node = root:named_descendant_for_range(
cursor_range[1],
cursor_range[2],
cursor_range[1],
cursor_range[2]
)
log(node, node:range()) log(node, node:range())
return node return node
end end

Loading…
Cancel
Save