From fc37a3f9a294d704a91efe6db0d07988c8056b36 Mon Sep 17 00:00:00 2001 From: ray-x Date: Sat, 22 May 2021 10:54:10 +1000 Subject: [PATCH] bug fix and add error logs --- LICENSE | 2 +- README.md | 7 +++--- lua/navigator/diagnostics.lua | 37 ++++++++++++++++++++++-------- lua/navigator/lspwrapper.lua | 2 ++ lua/navigator/treesitter.lua | 43 ++++++++++++++++++++++++++++++++++- lua/navigator/util.lua | 1 + plugin/navigator.vim | 24 ------------------- 7 files changed, 77 insertions(+), 39 deletions(-) delete mode 100644 plugin/navigator.vim diff --git a/LICENSE b/LICENSE index e534b92..9b25ede 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2021 rayx +Copyright (c) 2021 Ray-X Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 9571ab0..699c87a 100644 --- a/README.md +++ b/README.md @@ -2,17 +2,16 @@ - Easy code navigation through LSP and 🌲🏑Treesitter symbols; view diagnostic errors. -- Put language server and tree sitter's parser together. Not only for better highlight but also display symbol context +- Combine LSP and treesitter parser together. Not only providing better highlight but also help you analysis symbol context and scope. Here is an example -Show javascript call tree 🌲 of a variable inside a closure. Similar to incoming&outgoing calls from LSP. This feature -is designed for the symbol analysis. +Following screen shot shows javascript call tree 🌲 of variable `browser` insides a closure. This feature is similar to incoming&outgoing calls from LSP. It is designed for the symbol analysis. ![js_closure_call_tree](https://user-images.githubusercontent.com/1681295/119120589-cee23700-ba6f-11eb-95c5-b9ac8d445c31.jpg) Explains: -- There are 3 references for the symbol *browser* in closure.js +- First line of floating windows shows there are 3 references for the symbol *browser* in closure.js - The first reference of browser is an assigement, an emoji of πŸ“ indicates the value changed in this line. In many cases, we search for reference to find out where the value changed. - The second reference of `browser` is inside function `displayName` and `displayName` sit inside `makeFunc`, So you diff --git a/lua/navigator/diagnostics.lua b/lua/navigator/diagnostics.lua index 8aed460..7d6b9e1 100644 --- a/lua/navigator/diagnostics.lua +++ b/lua/navigator/diagnostics.lua @@ -3,16 +3,21 @@ local diagnostic_list = {} local util = require "navigator.util" local log = util.log +local error = util.error diagnostic_list[vim.bo.filetype] = {} local diag_hdlr = function(err, method, result, client_id, br, config) -- log(result) vim.lsp.diagnostic.on_publish_diagnostics(err, method, result, client_id, br, config) - if err ~= nil then log(err, config) end + if err ~= nil then + log(err, config) + end local cwd = vim.fn.getcwd(0) local ft = vim.bo.filetype - if diagnostic_list[ft] == nil then diagnostic_list[vim.bo.filetype] = {} end + if diagnostic_list[ft] == nil then + diagnostic_list[vim.bo.filetype] = {} + end -- vim.lsp.diagnostic.clear(vim.fn.bufnr(), client.id, nil, nil) local uri = result.uri @@ -27,16 +32,26 @@ local diag_hdlr = function(err, method, result, client_id, br, config) item.col = v.range.start.character + 1 item.uri = uri local head = "πŸ›" - if v.severity == 1 then head = "🈲" end - if v.severity == 2 then head = "☣️" end - if v.severity > 2 then head = "πŸ‘Ž" end + if v.severity == 1 then + head = "🈲" + end + if v.severity == 2 then + head = "☣️" + end + if v.severity > 2 then + head = "πŸ‘Ž" + end local bufnr = vim.uri_to_bufnr(uri) vim.fn.bufload(bufnr) local pos = v.range.start local row = pos.line local line = (vim.api.nvim_buf_get_lines(bufnr, row, row + 1, false) or {""})[1] - item.text = head .. line .. " πŸ“› " .. v.message - table.insert(item_list, item) + if line ~= nil then + item.text = head .. line .. " πŸ“› " .. v.message + table.insert(item_list, item) + else + error("diagnostic result empty line", v, item) + end end -- local old_items = vim.fn.getqflist() diagnostic_list[ft][uri] = item_list @@ -67,7 +82,9 @@ M.show_diagnostic = function() for _, buf in ipairs(bufs) do local bname = vim.fn.bufname(buf) if #bname > 0 and not util.exclude(bname) then - if vim.api.nvim_buf_is_loaded(buf) then vim.lsp.diagnostic.get(buf, nil) end + if vim.api.nvim_buf_is_loaded(buf) then + vim.lsp.diagnostic.get(buf, nil) + end end end if diagnostic_list[vim.bo.filetype] ~= nil then @@ -76,7 +93,9 @@ M.show_diagnostic = function() local results = diagnostic_list[vim.bo.filetype] local display_items = {} for _, items in pairs(results) do - for _, it in pairs(items) do table.insert(display_items, it) end + for _, it in pairs(items) do + table.insert(display_items, it) + end end -- log(display_items) if #display_items > 0 then diff --git a/lua/navigator/lspwrapper.lua b/lua/navigator/lspwrapper.lua index 8843841..63daf89 100644 --- a/lua/navigator/lspwrapper.lua +++ b/lua/navigator/lspwrapper.lua @@ -4,6 +4,7 @@ local gutil = require "guihua.util" local lsp = require "vim.lsp" local api = vim.api local log = require"navigator.util".log +local lerr = require"navigator.util".error local trace = require"navigator.util".trace local symbol_kind = require"navigator.lspclient.lspkind".symbol_kind local cwd = vim.fn.getcwd(0) @@ -136,6 +137,7 @@ end local function ts_functions(uri) if not ts_enabled or not calltree_enabled then + lerr("ts not enabled") return nil end local ts_func = require"navigator.treesitter".buf_func diff --git a/lua/navigator/treesitter.lua b/lua/navigator/treesitter.lua index a135ecc..6a6ba2e 100644 --- a/lua/navigator/treesitter.lua +++ b/lua/navigator/treesitter.lua @@ -1,3 +1,5 @@ +--- Note: some of the functions/code coped from treesitter/refactor/navigation.lua and may be modified +-- to fit in navigator.lua local gui = require "navigator.gui" local ok, ts_locals = pcall(require, "nvim-treesitter.locals") @@ -7,14 +9,16 @@ if not ok then end local parsers = require "nvim-treesitter.parsers" -local ts_utils = require "nvim-treesitter.ts_utils" local utils = require "nvim-treesitter.utils" +local locals = require 'nvim-treesitter.locals' +local ts_utils = require 'nvim-treesitter.ts_utils' local api = vim.api local util = require "navigator.util" local M = {} local cwd = vim.fn.getcwd(0) local log = require"navigator.util".log +local lerr = require"navigator.util".error local trace = require"navigator.util".trace local match_kinds = { @@ -36,6 +40,43 @@ local get_icon = function(kind) end end +function M.goto_definition(bufnr) + bufnr = bufnr or api.nvim_get_current_buf() + local node_at_point = ts_utils.get_node_at_cursor() + + if not node_at_point then + return + end + + local definition = locals.find_definition(node_at_point, bufnr) + + if definition ~= node_at_point then + ts_utils.goto_node(definition) + end +end + +-- use lsp range to find def +function M.find_definition(range, bufnr) + if not range then + + return + end + bufnr = bufnr or api.nvim_get_current_buf() + local cursor = {range.start.line, range.start.character} -- +1 or not? + + local node_at_point = ts_utils.get_node_at_cursor() + + if not node_at_point then + lerr("no node at cursor") + return + end + + local definition = locals.find_definition(node_at_point, bufnr) + + log(definition) + return +end + --- Get definitions of bufnr (unique and sorted by order of appearance). --- This function copy from treesitter/refactor/navigation.lua local function get_definitions(bufnr) diff --git a/lua/navigator/util.lua b/lua/navigator/util.lua index 11546f5..b71eaa1 100644 --- a/lua/navigator/util.lua +++ b/lua/navigator/util.lua @@ -91,6 +91,7 @@ M._log = require("guihua.log").new({level = default_config.level}, true) -- add log to you lsp.log M.log = M._log.info +M.info = M._log.info M.trace = M._log.trace M.error = M._log.error diff --git a/plugin/navigator.vim b/plugin/navigator.vim deleted file mode 100644 index cc6d50d..0000000 --- a/plugin/navigator.vim +++ /dev/null @@ -1,24 +0,0 @@ -" built upon popfix api(https://github.com/RishabhRD/popfix) -" for parameter references see popfix readme. - -if exists('g:loaded_navigator_lsp') | finish | endif - -let s:save_cpo = &cpo -set cpo&vim - -if ! exists('g:navigator_lsp_location_opts') - let g:navigator_lsp_location_opts = v:null -endif - -if ! exists('g:navigator_lsp_symbols_opts') - let g:navigator_lsp_symbols_opts = v:null -endif - -if ! exists('g:navigator_lsp_codeaction_opts') - let g:navigator_lsp_codeaction_opts = v:null -endif - -let &cpo = s:save_cpo -unlet s:save_cpo - -let g:loaded_navigator_lsp = 1