diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 5b4f453..5cfd8c2 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -16,3 +16,14 @@ jobs: sudo luarocks install luacheck - name: Run Luacheck run: bash scripts/style-check.sh + + stylua: + name: StyLua + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Lint with stylua + uses: JohnnyMorganz/stylua-action@1.0.0 + with: + token: ${{ secrets.GITHUB_TOKEN }} + args: --check . diff --git a/.stylua.toml b/.stylua.toml new file mode 100644 index 0000000..364ef9c --- /dev/null +++ b/.stylua.toml @@ -0,0 +1,3 @@ +indent_type = "Spaces" +indent_width = 2 +no_call_parentheses = true diff --git a/lua/nvim-treesitter-refactor.lua b/lua/nvim-treesitter-refactor.lua index f2f79e2..2c6b68b 100644 --- a/lua/nvim-treesitter-refactor.lua +++ b/lua/nvim-treesitter-refactor.lua @@ -3,31 +3,31 @@ local queries = require "nvim-treesitter.query" local M = {} function M.init() - require "nvim-treesitter".define_modules { + require("nvim-treesitter").define_modules { refactor = { highlight_definitions = { - module_path = 'nvim-treesitter-refactor.highlight_definitions', + module_path = "nvim-treesitter-refactor.highlight_definitions", enable = false, disable = {}, - is_supported = queries.has_locals + is_supported = queries.has_locals, }, highlight_current_scope = { - module_path = 'nvim-treesitter-refactor.highlight_current_scope', + module_path = "nvim-treesitter-refactor.highlight_current_scope", enable = false, disable = {}, is_supported = queries.has_locals, }, smart_rename = { - module_path = 'nvim-treesitter-refactor.smart_rename', + module_path = "nvim-treesitter-refactor.smart_rename", enable = false, disable = {}, is_supported = queries.has_locals, keymaps = { - smart_rename = "grr" - } + smart_rename = "grr", + }, }, navigation = { - module_path = 'nvim-treesitter-refactor.navigation', + module_path = "nvim-treesitter-refactor.navigation", enable = false, disable = {}, is_supported = queries.has_locals, @@ -37,9 +37,9 @@ function M.init() list_definitions_toc = "gO", goto_next_usage = "", goto_previous_usage = "", - } - } - } + }, + }, + }, } end diff --git a/lua/nvim-treesitter-refactor/highlight_current_scope.lua b/lua/nvim-treesitter-refactor/highlight_current_scope.lua index 02aa7ef..fc26187 100644 --- a/lua/nvim-treesitter-refactor/highlight_current_scope.lua +++ b/lua/nvim-treesitter-refactor/highlight_current_scope.lua @@ -1,13 +1,13 @@ -- This module highlights the current scope of at the cursor position -local ts_utils = require'nvim-treesitter.ts_utils' -local locals = require'nvim-treesitter.locals' +local ts_utils = require "nvim-treesitter.ts_utils" +local locals = require "nvim-treesitter.locals" local api = vim.api local cmd = api.nvim_command local M = {} -local current_scope_namespace = api.nvim_create_namespace('nvim-treesitter-current-scope') +local current_scope_namespace = api.nvim_create_namespace "nvim-treesitter-current-scope" function M.highlight_current_scope(bufnr) M.clear_highlights(bufnr) @@ -19,7 +19,7 @@ function M.highlight_current_scope(bufnr) local start_line = current_scope:start() if start_line ~= 0 then - ts_utils.highlight_node(current_scope, bufnr, current_scope_namespace, 'TSCurrentScope') + ts_utils.highlight_node(current_scope, bufnr, current_scope_namespace, "TSCurrentScope") end end end @@ -29,19 +29,31 @@ function M.clear_highlights(bufnr) end function M.attach(bufnr) - cmd(string.format('augroup NvimTreesitterCurrentScope_%d', bufnr)) - cmd 'au!' + cmd(string.format("augroup NvimTreesitterCurrentScope_%d", bufnr)) + cmd "au!" -- luacheck: push ignore 631 - cmd(string.format([[autocmd CursorMoved lua require'nvim-treesitter-refactor.highlight_current_scope'.highlight_current_scope(%d)]], bufnr, bufnr)) - cmd(string.format([[autocmd BufLeave lua require'nvim-treesitter-refactor.highlight_current_scope'.clear_highlights(%d)]], bufnr, bufnr)) + cmd( + string.format( + [[autocmd CursorMoved lua require'nvim-treesitter-refactor.highlight_current_scope'.highlight_current_scope(%d)]], + bufnr, + bufnr + ) + ) + cmd( + string.format( + [[autocmd BufLeave lua require'nvim-treesitter-refactor.highlight_current_scope'.clear_highlights(%d)]], + bufnr, + bufnr + ) + ) -- luacheck: pop - cmd 'augroup END' + cmd "augroup END" end function M.detach(bufnr) M.clear_highlights(bufnr) - cmd(string.format('autocmd! NvimTreesitterCurrentScope_%d CursorMoved', bufnr)) - cmd(string.format('autocmd! NvimTreesitterCurrentScope_%d BufLeave', bufnr)) + cmd(string.format("autocmd! NvimTreesitterCurrentScope_%d CursorMoved", bufnr)) + cmd(string.format("autocmd! NvimTreesitterCurrentScope_%d BufLeave", bufnr)) end return M diff --git a/lua/nvim-treesitter-refactor/highlight_definitions.lua b/lua/nvim-treesitter-refactor/highlight_definitions.lua index 432fb71..f13f65f 100644 --- a/lua/nvim-treesitter-refactor/highlight_definitions.lua +++ b/lua/nvim-treesitter-refactor/highlight_definitions.lua @@ -1,14 +1,14 @@ -- This module highlights reference usages and the corresponding -- definition on cursor hold. -local ts_utils = require'nvim-treesitter.ts_utils' -local locals = require'nvim-treesitter.locals' +local ts_utils = require "nvim-treesitter.ts_utils" +local locals = require "nvim-treesitter.locals" local api = vim.api local cmd = api.nvim_command local M = {} -local usage_namespace = api.nvim_create_namespace('nvim-treesitter-usages') +local usage_namespace = api.nvim_create_namespace "nvim-treesitter-usages" function M.highlight_usages(bufnr) M.clear_usage_highlights(bufnr) @@ -25,12 +25,12 @@ function M.highlight_usages(bufnr) for _, usage_node in ipairs(usages) do if usage_node ~= node_at_point then - ts_utils.highlight_node(usage_node, bufnr, usage_namespace, 'TSDefinitionUsage') + ts_utils.highlight_node(usage_node, bufnr, usage_namespace, "TSDefinitionUsage") end end if def_node ~= node_at_point then - ts_utils.highlight_node(def_node, bufnr, usage_namespace, 'TSDefinition') + ts_utils.highlight_node(def_node, bufnr, usage_namespace, "TSDefinition") end end @@ -39,21 +39,39 @@ function M.clear_usage_highlights(bufnr) end function M.attach(bufnr) - cmd(string.format('augroup NvimTreesitterUsages_%d', bufnr)) - cmd 'au!' + cmd(string.format("augroup NvimTreesitterUsages_%d", bufnr)) + cmd "au!" -- luacheck: push ignore 631 - cmd(string.format([[autocmd CursorHold lua require'nvim-treesitter-refactor.highlight_definitions'.highlight_usages(%d)]], bufnr, bufnr)) - cmd(string.format([[autocmd CursorMoved lua require'nvim-treesitter-refactor.highlight_definitions'.clear_usage_highlights(%d)]], bufnr, bufnr)) - cmd(string.format([[autocmd InsertEnter lua require'nvim-treesitter-refactor.highlight_definitions'.clear_usage_highlights(%d)]], bufnr, bufnr)) + cmd( + string.format( + [[autocmd CursorHold lua require'nvim-treesitter-refactor.highlight_definitions'.highlight_usages(%d)]], + bufnr, + bufnr + ) + ) + cmd( + string.format( + [[autocmd CursorMoved lua require'nvim-treesitter-refactor.highlight_definitions'.clear_usage_highlights(%d)]], + bufnr, + bufnr + ) + ) + cmd( + string.format( + [[autocmd InsertEnter lua require'nvim-treesitter-refactor.highlight_definitions'.clear_usage_highlights(%d)]], + bufnr, + bufnr + ) + ) -- luacheck: pop - cmd 'augroup END' + cmd "augroup END" end function M.detach(bufnr) M.clear_usage_highlights(bufnr) - cmd(string.format('autocmd! NvimTreesitterUsages_%d CursorHold', bufnr)) - cmd(string.format('autocmd! NvimTreesitterUsages_%d CursorMoved', bufnr)) - cmd(string.format('autocmd! NvimTreesitterUsages_%d InsertEnter', bufnr)) + cmd(string.format("autocmd! NvimTreesitterUsages_%d CursorHold", bufnr)) + cmd(string.format("autocmd! NvimTreesitterUsages_%d CursorMoved", bufnr)) + cmd(string.format("autocmd! NvimTreesitterUsages_%d InsertEnter", bufnr)) end return M diff --git a/lua/nvim-treesitter-refactor/navigation.lua b/lua/nvim-treesitter-refactor/navigation.lua index 743d496..f0ca5d6 100644 --- a/lua/nvim-treesitter-refactor/navigation.lua +++ b/lua/nvim-treesitter-refactor/navigation.lua @@ -1,9 +1,9 @@ -- Definition based navigation module -local ts_utils = require'nvim-treesitter.ts_utils' -local utils = require'nvim-treesitter.utils' -local locals = require'nvim-treesitter.locals' -local configs = require'nvim-treesitter.configs' +local ts_utils = require "nvim-treesitter.ts_utils" +local utils = require "nvim-treesitter.utils" +local locals = require "nvim-treesitter.locals" +local configs = require "nvim-treesitter.configs" local api = vim.api local M = {} @@ -12,7 +12,9 @@ function M.goto_definition(bufnr, fallback_function) local 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 + if not node_at_point then + return + end local definition = locals.find_definition(node_at_point, bufnr) @@ -23,7 +25,9 @@ function M.goto_definition(bufnr, fallback_function) end end -function M.goto_definition_lsp_fallback(bufnr) M.goto_definition(bufnr, vim.lsp.buf.definition) end +function M.goto_definition_lsp_fallback(bufnr) + M.goto_definition(bufnr, vim.lsp.buf.definition) +end --- Get definitions of bufnr (unique and sorted by order of appearance). local function get_definitions(bufnr) @@ -37,14 +41,14 @@ local function get_definitions(bufnr) -- lua doesn't compare tables by value, -- use the value from byte count instead. local _, _, start = node:start() - nodes_set[start] = {node = node, type = match or ""} + nodes_set[start] = { node = node, type = match or "" } end) end end -- Sort by order of appearance. local definition_nodes = vim.tbl_values(nodes_set) - table.sort(definition_nodes, function (a, b) + table.sort(definition_nodes, function(a, b) local _, _, start_a = a.node:start() local _, _, start_b = b.node:start() return start_a < start_b @@ -57,7 +61,9 @@ function M.list_definitions(bufnr) local bufnr = bufnr or api.nvim_get_current_buf() local definitions = get_definitions(bufnr) - if #definitions < 1 then return end + if #definitions < 1 then + return + end local qf_list = {} @@ -74,8 +80,8 @@ function M.list_definitions(bufnr) }) end - vim.fn.setqflist(qf_list, 'r') - api.nvim_command('copen') + vim.fn.setqflist(qf_list, "r") + api.nvim_command "copen" end function M.list_definitions_toc() @@ -83,16 +89,18 @@ function M.list_definitions_toc() local bufnr = api.nvim_win_get_buf(winnr) local definitions = get_definitions(bufnr) - if #definitions < 1 then return end + if #definitions < 1 then + return + end local loc_list = {} -- Force some types to act like they are parents -- instead of neighbors of the next nodes. local containers = { - ['function'] = true, - ['type'] = true, - ['method'] = true, + ["function"] = true, + ["type"] = true, + ["method"] = true, } local parents = {} @@ -101,11 +109,13 @@ function M.list_definitions_toc() -- Get indentation level by putting all parents in a stack. -- The length of the stack minus one is the current level of indentation. local n = #parents - for i=1, n do + for i = 1, n do local index = n + 1 - i local parent_def = parents[index] - if ts_utils.is_parent(parent_def.node, def.node) - or (containers[parent_def.type] and ts_utils.is_parent(parent_def.node:parent(), def.node)) then + if + ts_utils.is_parent(parent_def.node, def.node) + or (containers[parent_def.type] and ts_utils.is_parent(parent_def.node:parent(), def.node)) + then break else parents[index] = nil @@ -120,51 +130,59 @@ function M.list_definitions_toc() bufnr = bufnr, lnum = lnum + 1, col = col + 1, - text = string.rep(' ', #parents - 1) .. text, + text = string.rep(" ", #parents - 1) .. text, type = type, }) end - vim.fn.setloclist(winnr, loc_list, 'r') + vim.fn.setloclist(winnr, loc_list, "r") -- The title needs to end with `TOC`, -- so Neovim displays it like a TOC instead of an error list. - vim.fn.setloclist(winnr, {}, 'a', {title = 'Definitions TOC'}) - api.nvim_command('lopen') + vim.fn.setloclist(winnr, {}, "a", { title = "Definitions TOC" }) + api.nvim_command "lopen" end function M.goto_adjacent_usage(bufnr, delta) local 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 + if not node_at_point then + return + end local def_node, scope = locals.find_definition(node_at_point, bufnr) local usages = locals.find_usages(def_node, scope, bufnr) local index = utils.index_of(usages, node_at_point) - if not index then return end + if not index then + return + end local target_index = (index + delta + #usages - 1) % #usages + 1 ts_utils.goto_node(usages[target_index]) end -function M.goto_next_usage(bufnr) return M.goto_adjacent_usage(bufnr, 1) end -function M.goto_previous_usage(bufnr) return M.goto_adjacent_usage(bufnr, -1) end +function M.goto_next_usage(bufnr) + return M.goto_adjacent_usage(bufnr, 1) +end +function M.goto_previous_usage(bufnr) + return M.goto_adjacent_usage(bufnr, -1) +end function M.attach(bufnr) - local config = configs.get_module('refactor.navigation') + local config = configs.get_module "refactor.navigation" for fn_name, mapping in pairs(config.keymaps) do local cmd = string.format([[:lua require'nvim-treesitter-refactor.navigation'.%s(%d)]], fn_name, bufnr) - api.nvim_buf_set_keymap(bufnr, 'n', mapping, cmd, { silent = true, noremap = true }) + api.nvim_buf_set_keymap(bufnr, "n", mapping, cmd, { silent = true, noremap = true }) end end function M.detach(bufnr) - local config = configs.get_module('refactor.navigation') + local config = configs.get_module "refactor.navigation" for _, mapping in pairs(config.keymaps) do - api.nvim_buf_del_keymap(bufnr, 'n', mapping) + api.nvim_buf_del_keymap(bufnr, "n", mapping) end end diff --git a/lua/nvim-treesitter-refactor/smart_rename.lua b/lua/nvim-treesitter-refactor/smart_rename.lua index db6e93f..f99b6f4 100644 --- a/lua/nvim-treesitter-refactor/smart_rename.lua +++ b/lua/nvim-treesitter-refactor/smart_rename.lua @@ -1,10 +1,10 @@ -- Binds a keybinding to smart rename definitions and usages. -- Can be used directly using the `smart_rename` function. -local ts_utils = require'nvim-treesitter.ts_utils' -local locals = require'nvim-treesitter.locals' -local configs = require'nvim-treesitter.configs' -local utils = require'nvim-treesitter.utils' +local ts_utils = require "nvim-treesitter.ts_utils" +local locals = require "nvim-treesitter.locals" +local configs = require "nvim-treesitter.configs" +local utils = require "nvim-treesitter.utils" local api = vim.api local M = {} @@ -14,15 +14,17 @@ function M.smart_rename(bufnr) local node_at_point = ts_utils.get_node_at_cursor() if not node_at_point then - utils.print_warning("No node to rename!") + utils.print_warning "No node to rename!" return end local node_text = ts_utils.get_node_text(node_at_point)[1] - local new_name = vim.fn.input('New name: ', node_text or '') + local new_name = vim.fn.input("New name: ", node_text or "") -- Empty name cancels the interaction or ESC - if not new_name or #new_name < 1 then return end + if not new_name or #new_name < 1 then + return + end local definition, scope = locals.find_definition(node_at_point, bufnr) local nodes_to_rename = {} @@ -44,19 +46,19 @@ function M.smart_rename(bufnr) end function M.attach(bufnr) - local config = configs.get_module('refactor.smart_rename') + local config = configs.get_module "refactor.smart_rename" for fn_name, mapping in pairs(config.keymaps) do local cmd = string.format([[:lua require'nvim-treesitter-refactor.smart_rename'.%s(%d)]], fn_name, bufnr) - api.nvim_buf_set_keymap(bufnr, 'n', mapping, cmd, { silent = true, noremap = true }) + api.nvim_buf_set_keymap(bufnr, "n", mapping, cmd, { silent = true, noremap = true }) end end function M.detach(bufnr) - local config = configs.get_module('refactor.smart_rename') + local config = configs.get_module "refactor.smart_rename" for _, mapping in pairs(config.keymaps) do - api.nvim_buf_del_keymap(bufnr, 'n', mapping) + api.nvim_buf_del_keymap(bufnr, "n", mapping) end end