From 3712ff0cdf4f9f877d9ca708d835a877d9a0abaf Mon Sep 17 00:00:00 2001 From: zztrieuzz Date: Mon, 19 Feb 2024 20:30:34 +0700 Subject: [PATCH] style: reformat by stylua --- .github/workflows/ci.yml | 12 ++ lua/spectre/_log.lua | 4 +- lua/spectre/actions.lua | 111 ++++++----- lua/spectre/config.lua | 175 +++++++++-------- lua/spectre/highlight.lua | 18 +- lua/spectre/init.lua | 368 ++++++++++++++++------------------- lua/spectre/regex/rust.lua | 4 +- lua/spectre/regex/vim.lua | 24 +-- lua/spectre/replace/base.lua | 17 +- lua/spectre/replace/init.lua | 10 +- lua/spectre/replace/oxi.lua | 4 +- lua/spectre/replace/sed.lua | 21 +- lua/spectre/search/ag.lua | 8 +- lua/spectre/search/base.lua | 67 +++---- lua/spectre/search/init.lua | 10 +- lua/spectre/search/rg.lua | 5 +- lua/spectre/state.lua | 11 +- lua/spectre/state_utils.lua | 11 +- lua/spectre/ui.lua | 123 +++++++----- lua/spectre/utils.lua | 83 ++++---- style.toml => stylua.toml | 2 +- 21 files changed, 547 insertions(+), 541 deletions(-) rename style.toml => stylua.toml (79%) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a2b5d1c..603f263 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -51,3 +51,15 @@ jobs: export VIM="${PWD}/_neovim/share/nvim/runtime" nvim --version make test + + stylua: + name: stylua + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v3 + - uses: JohnnyMorganz/stylua-action@v3 + with: + token: ${{ secrets.GITHUB_TOKEN }} + version: latest + # CLI arguments + args: --color always --check lua/ diff --git a/lua/spectre/_log.lua b/lua/spectre/_log.lua index 77c421e..4eff841 100644 --- a/lua/spectre/_log.lua +++ b/lua/spectre/_log.lua @@ -5,9 +5,9 @@ local empty = { error = function(_) end, } if _G.__spectre_log then - return require('plenary.log').new { + return require('plenary.log').new({ plugin = 'nvim-spectre', level = (_G.__spectre_log == true and 'debug') or 'warn', - } or empty + }) or empty end return empty diff --git a/lua/spectre/actions.lua b/lua/spectre/actions.lua index cdb58be..eb5e46b 100644 --- a/lua/spectre/actions.lua +++ b/lua/spectre/actions.lua @@ -11,17 +11,17 @@ local open_file = function(filename, lnum, col, winid) if winid ~= nil then vim.fn.win_gotoid(winid) end - vim.api.nvim_command [[execute "normal! m` "]] + vim.api.nvim_command([[execute "normal! m` "]]) local escaped_filename = vim.fn.fnameescape(filename) - vim.cmd("e " .. escaped_filename) + vim.cmd('e ' .. escaped_filename) api.nvim_win_set_cursor(0, { lnum, col }) end local is_absolute = function(filename) - if vim.loop.os_uname().sysname == "Windows_NT" then - return string.find(filename, "%a:\\") == 1 + if vim.loop.os_uname().sysname == 'Windows_NT' then + return string.find(filename, '%a:\\') == 1 end - return string.sub(filename, 1, 1) == "/" + return string.sub(filename, 1, 1) == '/' end local get_file_path = function(filename) @@ -31,7 +31,7 @@ local get_file_path = function(filename) end -- use default current working directory if state.cwd is nil or empty string -- - if state.cwd == nil or state.cwd == "" then + if state.cwd == nil or state.cwd == '' then state.cwd = vim.fn.getcwd() end @@ -40,7 +40,9 @@ end M.select_entry = function() local t = M.get_current_entry() - if t == nil then return nil end + if t == nil then + return nil + end if config.is_open_target_win and state.target_winid ~= nil then open_file(t.filename, t.lnum, t.col, state.target_winid) else @@ -52,7 +54,7 @@ M.get_state = function() local result = { query = state.query, cwd = state.cwd, - options = state.options + options = state.options, } return vim.deepcopy(result) end @@ -91,10 +93,10 @@ end M.send_to_qf = function() local entries = M.get_all_entries() - vim.cmd [[copen]] - vim.fn.setqflist(entries, "r") + vim.cmd([[copen]]) + vim.fn.setqflist(entries, 'r') vim.fn.setqflist({}, 'r', { - title = string.format("Result Search: [%s]", state.query.search_query) + title = string.format('Result Search: [%s]', state.query.search_query), }) return entries end @@ -111,12 +113,7 @@ M.replace_cmd = function() end if state.query.is_file == true then vim.fn.win_gotoid(state.target_winid) - replace_cmd = string.format( - ':%%s/\\v%s/%s/g%s', - search_regex, - state.query.replace_query, - ignore_case - ) + replace_cmd = string.format(':%%s/\\v%s/%s/g%s', search_regex, state.query.replace_query, ignore_case) else replace_cmd = string.format( ':%s %%s/\\v%s/%s/g%s | update', @@ -137,7 +134,7 @@ M.run_current_replace = function() if entry then M.run_replace({ entry }) else - vim.notify("Not found any entry to replace.") + vim.notify('Not found any entry to replace.') end end @@ -145,7 +142,7 @@ local is_running = false M.run_replace = function(entries) if is_running == true then - print("it is already running") + print('it is already running') return end is_running = true @@ -154,37 +151,46 @@ M.run_replace = function(entries) local done_item = 0 local error_item = 0 state.status_line = 'Run Replace.' - local replacer = replacer_creator:new( - state_utils.get_replace_engine_config(), { - on_done = function(result) - if (result.ref) then - done_item = done_item + 1 - state.status_line = "Replace: " .. done_item .. " Error:" .. error_item - M.set_entry_finish(result.ref.display_lnum) - local value = result.ref - value.text = " DONE" - vim.fn.setqflist(entries, 'r') - api.nvim_buf_set_extmark(state.bufnr, config.namespace, value.display_lnum, 0, - { virt_text = { { "󰄲 DONE", "String" } }, virt_text_pos = 'eol' }) - end - end, - on_error = function(result) - if type(result.value) == "string" then - for line in result.value:gmatch("[^\r\n]+") do - print(line) - end - end - if (result.ref) then - error_item = error_item + 1 - local value = result.ref - value.text = "ERROR" - vim.fn.setqflist(entries, 'r') - state.status_line = "Replace: " .. done_item .. " Error:" .. error_item - api.nvim_buf_set_extmark(state.bufnr, config.namespace, value.display_lnum, 0, - { virt_text = { { "󰄱 ERROR", "Error" } }, virt_text_pos = 'eol' }) + local replacer = replacer_creator:new(state_utils.get_replace_engine_config(), { + on_done = function(result) + if result.ref then + done_item = done_item + 1 + state.status_line = 'Replace: ' .. done_item .. ' Error:' .. error_item + M.set_entry_finish(result.ref.display_lnum) + local value = result.ref + value.text = ' DONE' + vim.fn.setqflist(entries, 'r') + api.nvim_buf_set_extmark( + state.bufnr, + config.namespace, + value.display_lnum, + 0, + { virt_text = { { '󰄲 DONE', 'String' } }, virt_text_pos = 'eol' } + ) + end + end, + on_error = function(result) + if type(result.value) == 'string' then + for line in result.value:gmatch('[^\r\n]+') do + print(line) end end - }) + if result.ref then + error_item = error_item + 1 + local value = result.ref + value.text = 'ERROR' + vim.fn.setqflist(entries, 'r') + state.status_line = 'Replace: ' .. done_item .. ' Error:' .. error_item + api.nvim_buf_set_extmark( + state.bufnr, + config.namespace, + value.display_lnum, + 0, + { virt_text = { { '󰄱 ERROR', 'Error' } }, virt_text_pos = 'eol' } + ) + end + end, + }) for _, value in pairs(entries) do if not value.is_replace_finish then replacer:replace({ @@ -209,12 +215,13 @@ M.select_template = function() end local target_bufnr = state.target_bufnr local target_winid = state.target_winid - local is_spectre = vim.api.nvim_buf_get_option(0, 'filetype') == "spectre_panel" + local is_spectre = vim.api.nvim_buf_get_option(0, 'filetype') == 'spectre_panel' vim.ui.select(state.user_config.open_template, { prompt = 'Select template', - format_item = function(item) return item.search_text end - } - , function(item) + format_item = function(item) + return item.search_text + end, + }, function(item) require('spectre').open(vim.tbl_extend('force', state.query, item)) if is_spectre and target_bufnr and target_winid then state.target_bufnr = target_bufnr diff --git a/lua/spectre/config.lua b/lua/spectre/config.lua index 17eb7d8..98b7266 100644 --- a/lua/spectre/config.lua +++ b/lua/spectre/config.lua @@ -2,12 +2,12 @@ local api = vim.api ---@class SpectreConfig local config = { - filetype = "spectre_panel", - namespace = api.nvim_create_namespace("SEARCH_PANEL"), - namespace_ui = api.nvim_create_namespace("SEARCH_PANEL_UI"), - namespace_header = api.nvim_create_namespace("SEARCH_PANEL_HEADER"), - namespace_status = api.nvim_create_namespace("SEARCH_PANEL_STATUS"), - namespace_result = api.nvim_create_namespace("SEARCH_PANEL_RESULT"), + filetype = 'spectre_panel', + namespace = api.nvim_create_namespace('SEARCH_PANEL'), + namespace_ui = api.nvim_create_namespace('SEARCH_PANEL_UI'), + namespace_header = api.nvim_create_namespace('SEARCH_PANEL_HEADER'), + namespace_status = api.nvim_create_namespace('SEARCH_PANEL_STATUS'), + namespace_result = api.nvim_create_namespace('SEARCH_PANEL_RESULT'), lnum_UI = 8, -- total line for ui you can edit it line_result = 10, -- line begin result @@ -20,102 +20,102 @@ local config = { line_sep_start = '┌──────────────────────────────────────────────────────', result_padding = '│ ', - line_sep = '└──────────────────────────────────────────────────────', - color_devicons = true, - open_cmd = 'vnew', - live_update = false, - lnum_for_results = false, -- show line number for search/replace results - highlight = { - headers = "SpectreHeader", - ui = "SpectreBody", - filename = "SpectreFile", - filedirectory = "SpectreDir", - search = "SpectreSearch", - border = "SpectreBorder", - replace = "SpectreReplace" + line_sep = '└──────────────────────────────────────────────────────', + color_devicons = true, + open_cmd = 'vnew', + live_update = false, + lnum_for_results = false, -- show line number for search/replace results + highlight = { + headers = 'SpectreHeader', + ui = 'SpectreBody', + filename = 'SpectreFile', + filedirectory = 'SpectreDir', + search = 'SpectreSearch', + border = 'SpectreBorder', + replace = 'SpectreReplace', }, - mapping = { + mapping = { ['toggle_line'] = { - map = "dd", + map = 'dd', cmd = "lua require('spectre').toggle_line()", - desc = "toggle item" + desc = 'toggle item', }, ['enter_file'] = { - map = "", + map = '', cmd = "lua require('spectre.actions').select_entry()", - desc = "open file" + desc = 'open file', }, ['send_to_qf'] = { - map = "q", + map = 'q', cmd = "lua require('spectre.actions').send_to_qf()", - desc = "send all items to quickfix" + desc = 'send all items to quickfix', }, ['replace_cmd'] = { - map = "c", + map = 'c', cmd = "lua require('spectre.actions').replace_cmd()", - desc = "input replace command" + desc = 'input replace command', }, ['show_option_menu'] = { - map = "o", + map = 'o', cmd = "lua require('spectre').show_options()", - desc = "show options" + desc = 'show options', }, ['run_current_replace'] = { - map = "rc", + map = 'rc', cmd = "lua require('spectre.actions').run_current_replace()", - desc = "replace item" + desc = 'replace item', }, ['run_replace'] = { - map = "R", + map = 'R', cmd = "lua require('spectre.actions').run_replace()", - desc = "replace all" + desc = 'replace all', }, -- only show replace text in result UI ['change_view_mode'] = { - map = "v", + map = 'v', cmd = "lua require('spectre').change_view()", - desc = "change result view mode" + desc = 'change result view mode', }, ['change_replace_sed'] = { - map = "trs", + map = 'trs', cmd = "lua require('spectre').change_engine_replace('sed')", - desc = "use sed to replace" + desc = 'use sed to replace', }, ['change_replace_oxi'] = { - map = "tro", + map = 'tro', cmd = "lua require('spectre').change_engine_replace('oxi')", - desc = "use oxi to replace" + desc = 'use oxi to replace', }, ['toggle_live_update'] = { - map = "tu", + map = 'tu', cmd = "lua require('spectre').toggle_live_update()", - desc = "update when vim writes to file" + desc = 'update when vim writes to file', }, -- only work if the find_engine following have that option ['toggle_ignore_case'] = { - map = "ti", + map = 'ti', cmd = "lua require('spectre').change_options('ignore-case')", - desc = "toggle ignore case" + desc = 'toggle ignore case', }, ['toggle_ignore_hidden'] = { - map = "th", + map = 'th', cmd = "lua require('spectre').change_options('hidden')", - desc = "toggle search hidden" + desc = 'toggle search hidden', }, ['resume_last_search'] = { - map = "l", + map = 'l', cmd = "lua require('spectre').resume_last_search()", - desc = "repeat last search" + desc = 'repeat last search', }, ['select_template'] = { - map = "rp", + map = 'rp', cmd = "lua require('spectre.actions').select_template()", - desc = "pick template" + desc = 'pick template', }, }, - find_engine = { + find_engine = { ['rg'] = { - cmd = "rg", + cmd = 'rg', -- default args args = { '--color=never', @@ -126,86 +126,85 @@ local config = { }, options = { ['ignore-case'] = { - value = "--ignore-case", - icon = "[I]", - desc = "ignore case" + value = '--ignore-case', + icon = '[I]', + desc = 'ignore case', }, ['hidden'] = { - value = "--hidden", - desc = "hidden file", - icon = "[H]" - + value = '--hidden', + desc = 'hidden file', + icon = '[H]', }, -- you can put any option you want here it can toggle with -- show_option function - } + }, }, ['ag'] = { - cmd = "ag", + cmd = 'ag', -- default args args = { '--vimgrep', - '-s' + '-s', }, options = { ['ignore-case'] = { - value = "-i", - icon = "[I]", - desc = "ignore case" + value = '-i', + icon = '[I]', + desc = 'ignore case', }, ['hidden'] = { - value = "--hidden", - desc = "hidden file", - icon = "[H]" + value = '--hidden', + desc = 'hidden file', + icon = '[H]', }, }, }, }, - replace_engine = { + replace_engine = { ['sed'] = { - cmd = "sed", + cmd = 'sed', args = { '-i', '-E', }, options = { ['ignore-case'] = { - value = "--ignore-case", - icon = "[I]", - desc = "ignore case" + value = '--ignore-case', + icon = '[I]', + desc = 'ignore case', }, - } + }, }, ['oxi'] = { cmd = 'oxi', args = {}, options = { ['ignore-case'] = { - value = "i", - icon = "[I]", - desc = "ignore case" + value = 'i', + icon = '[I]', + desc = 'ignore case', }, - } - } + }, + }, }, - default = { + default = { find = { - cmd = "rg", - options = { "ignore-case" } + cmd = 'rg', + options = { 'ignore-case' }, }, replace = { - cmd = "sed" - } + cmd = 'sed', + }, }, - replace_vim_cmd = "cdo", + replace_vim_cmd = 'cdo', is_open_target_win = true, - is_insert_mode = false, + is_insert_mode = false, is_block_ui_break = false, - open_template = {} + open_template = {}, } if vim.loop.os_uname().sysname == 'Darwin' then - config.replace_engine.sed.cmd = "gsed" + config.replace_engine.sed.cmd = 'gsed' if vim.fn.executable('gsed') == 0 then print("You need to install gnu sed 'brew install gnu-sed'") end diff --git a/lua/spectre/highlight.lua b/lua/spectre/highlight.lua index aa0f520..837a5cd 100644 --- a/lua/spectre/highlight.lua +++ b/lua/spectre/highlight.lua @@ -1,18 +1,18 @@ local M = {} M.set_hl = function() - vim.api.nvim_set_hl(0, 'SpectreHeader', { link = "Comment" }) - vim.api.nvim_set_hl(0, 'SpectreBody', { link = "String" }) - vim.api.nvim_set_hl(0, 'SpectreFile', { link = "Keyword" }) - vim.api.nvim_set_hl(0, 'SpectreDir', { link = "Comment" }) - vim.api.nvim_set_hl(0, 'SpectreSearch', { link = "DiffChange" }) - vim.api.nvim_set_hl(0, 'SpectreBorder', { link = "Comment" }) - vim.api.nvim_set_hl(0, 'SpectreReplace', { link = "DiffDelete" }) + vim.api.nvim_set_hl(0, 'SpectreHeader', { link = 'Comment' }) + vim.api.nvim_set_hl(0, 'SpectreBody', { link = 'String' }) + vim.api.nvim_set_hl(0, 'SpectreFile', { link = 'Keyword' }) + vim.api.nvim_set_hl(0, 'SpectreDir', { link = 'Comment' }) + vim.api.nvim_set_hl(0, 'SpectreSearch', { link = 'DiffChange' }) + vim.api.nvim_set_hl(0, 'SpectreBorder', { link = 'Comment' }) + vim.api.nvim_set_hl(0, 'SpectreReplace', { link = 'DiffDelete' }) end vim.api.nvim_create_autocmd('ColorScheme', { pattern = '*', - callback = M.set_hl + callback = M.set_hl, }) -return M; +return M diff --git a/lua/spectre/init.lua b/lua/spectre/init.lua index d097150..44a7cc7 100644 --- a/lua/spectre/init.lua +++ b/lua/spectre/init.lua @@ -53,15 +53,15 @@ end M.open_file_search = function(opts) opts = opts or {} if opts.select_word then - opts.search_text = vim.fn.expand("") + opts.search_text = vim.fn.expand('') else opts.search_text = utils.get_visual_selection() end - opts.path = vim.fn.fnameescape(vim.fn.expand("%:p:.")) + opts.path = vim.fn.fnameescape(vim.fn.expand('%:p:.')) - if vim.loop.os_uname().sysname == "Windows_NT" then - opts.path = vim.fn.substitute(opts.path, "\\", "/", "g") + if vim.loop.os_uname().sysname == 'Windows_NT' then + opts.path = vim.fn.substitute(opts.path, '\\', '/', 'g') end M.open(opts) @@ -70,16 +70,18 @@ end M.close = function() if state.bufnr ~= nil then local wins = vim.fn.win_findbuf(state.bufnr) - if not wins then return end + if not wins then + return + end for _, win_id in pairs(wins) do - vim.api.nvim_win_close(win_id, true); + vim.api.nvim_win_close(win_id, true) end state.is_open = false end end M.open = function(opts) - log.debug("Start") + log.debug('Start') if state.user_config == nil then M.setup() end @@ -92,7 +94,7 @@ M.open = function(opts) path = '', is_close = false, -- close an exists instance of spectre then open new is_file = false, - begin_line_num = 3 + begin_line_num = 3, }, opts or {}) or {} state.is_open = true @@ -119,10 +121,7 @@ M.open = function(opts) if state.bufnr == nil or is_new then vim.cmd(state.user_config.open_cmd) else - if state.query.path ~= nil - and #state.query.path > 1 - and opts.path == '' - then + if state.query.path ~= nil and #state.query.path > 1 and opts.path == '' then opts.path = state.query.path end end @@ -130,8 +129,8 @@ M.open = function(opts) vim.wo.foldenable = false vim.bo.buftype = 'nofile' vim.bo.buflisted = false - state.bufnr = api.nvim_get_current_buf(); - vim.cmd(string.format("file %s/spectre", state.bufnr)) + state.bufnr = api.nvim_get_current_buf() + vim.cmd(string.format('file %s/spectre', state.bufnr)) vim.bo.filetype = config.filetype api.nvim_buf_clear_namespace(state.bufnr, config.namespace_status, 0, -1) api.nvim_buf_clear_namespace(state.bufnr, config.namespace_result, 0, -1) @@ -142,10 +141,9 @@ M.open = function(opts) }) ui.render_text_query(opts) - state.cwd = opts.cwd state.search_paths = opts.search_paths - M.change_view("reset") + M.change_view('reset') ui.render_search_ui() if opts.is_insert_mode == true then @@ -174,16 +172,16 @@ end function M.mapping_buffer(bufnr) _G.__spectre_fold = M.get_fold - vim.cmd [[augroup spectre_panel + vim.cmd([[augroup spectre_panel au! au InsertEnter lua require"spectre".on_insert_enter() au InsertLeave lua require"spectre".on_search_change() au BufLeave lua require("spectre").on_leave() au BufUnload lua require("spectre").on_close() - augroup END ]] + augroup END ]]) vim.opt_local.wrap = false - vim.opt_local.foldexpr = "spectre#foldexpr()" - vim.opt_local.foldmethod = "expr" + vim.opt_local.foldexpr = 'spectre#foldexpr()' + vim.opt_local.foldmethod = 'expr' local map_opt = { noremap = true, silent = _G.__is_dev == nil } api.nvim_buf_set_keymap(bufnr, 'n', 'x', 'xlua require("spectre").on_search_change()', map_opt) api.nvim_buf_set_keymap(bufnr, 'n', 'p', "plua require('spectre').on_search_change()", map_opt) @@ -193,31 +191,37 @@ function M.mapping_buffer(bufnr) api.nvim_buf_set_keymap(bufnr, 'v', 'd', 'lua require("spectre").toggle_checked()', map_opt) api.nvim_buf_set_keymap(bufnr, 'n', 'o', 'ji', map_opt) -- don't append line on can make the UI wrong api.nvim_buf_set_keymap(bufnr, 'n', 'O', 'ki', map_opt) - api.nvim_buf_set_keymap(bufnr, 'n', 'u', "", map_opt) -- disable undo, It breaks the UI. + api.nvim_buf_set_keymap(bufnr, 'n', 'u', '', map_opt) -- disable undo, It breaks the UI. api.nvim_buf_set_keymap(bufnr, 'n', '', "lua require('spectre').tab()", map_opt) api.nvim_buf_set_keymap(bufnr, 'n', '', "lua require('spectre').tab_shift()", map_opt) api.nvim_buf_set_keymap(bufnr, 'n', 'yy', "lua require('spectre.actions').copy_current_line()", map_opt) api.nvim_buf_set_keymap(bufnr, 'n', '?', "lua require('spectre').show_help()", map_opt) for _, map in pairs(state.user_config.mapping) do - api.nvim_buf_set_keymap(bufnr, 'n', map.map, map.cmd, vim.tbl_deep_extend("force", map_opt, { desc = map.desc })) + api.nvim_buf_set_keymap( + bufnr, + 'n', + map.map, + map.cmd, + vim.tbl_deep_extend('force', map_opt, { desc = map.desc }) + ) end - vim.api.nvim_create_autocmd("BufWritePost", { - group = vim.api.nvim_create_augroup("SpectrePanelWrite", { clear = true }), - pattern = "*", + vim.api.nvim_create_autocmd('BufWritePost', { + group = vim.api.nvim_create_augroup('SpectrePanelWrite', { clear = true }), + pattern = '*', callback = require('spectre').on_write, - desc = "spectre write autocmd" + desc = 'spectre write autocmd', }) - vim.api.nvim_create_autocmd("WinClosed", { - group = vim.api.nvim_create_augroup("SpectreStateOpened", { clear = true }), + vim.api.nvim_create_autocmd('WinClosed', { + group = vim.api.nvim_create_augroup('SpectreStateOpened', { clear = true }), buffer = 0, callback = function() - if vim.api.nvim_buf_get_option(vim.api.nvim_get_current_buf(), 'filetype') == "spectre_panel" then + if vim.api.nvim_buf_get_option(vim.api.nvim_get_current_buf(), 'filetype') == 'spectre_panel' then state.is_open = false end end, - desc = "Ensure spectre state when its window is closed by any mean" + desc = 'Ensure spectre state when its window is closed by any mean', }) if state.user_config.is_block_ui_break then @@ -226,49 +230,43 @@ function M.mapping_buffer(bufnr) -- * If the user passes over a forbidden line on insert mode: leave insert mode. -- * Disable backspace jumping lines. local backspace = vim.api.nvim_get_option('backspace') - local anti_insert_breakage_group = vim.api.nvim_create_augroup("SpectreAntiInsertBreakage", { clear = true }) - vim.api.nvim_create_autocmd({ "InsertEnter", "CursorMovedI" }, { + local anti_insert_breakage_group = vim.api.nvim_create_augroup('SpectreAntiInsertBreakage', { clear = true }) + vim.api.nvim_create_autocmd({ 'InsertEnter', 'CursorMovedI' }, { group = anti_insert_breakage_group, buffer = 0, callback = function() local current_filetype = vim.bo.filetype - if current_filetype == "spectre_panel" then - vim.cmd("set backspace=indent,start") + if current_filetype == 'spectre_panel' then + vim.cmd('set backspace=indent,start') local line = vim.api.nvim_win_get_cursor(0)[1] - if line == 1 or line == 2 or line == 4 or line == 6 or line >= 8 - then - vim.api.nvim_feedkeys( - vim.api.nvim_replace_termcodes('', true, false, true), - 'n', true - ) + if line == 1 or line == 2 or line == 4 or line == 6 or line >= 8 then + vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('', true, false, true), 'n', true) end end end, - desc = "spectre anti-insert-breakage → protect the user from breaking the UI while on insert mode." + desc = 'spectre anti-insert-breakage → protect the user from breaking the UI while on insert mode.', }) - vim.api.nvim_create_autocmd({ "WinLeave" }, { + vim.api.nvim_create_autocmd({ 'WinLeave' }, { group = anti_insert_breakage_group, buffer = 0, callback = function() local current_filetype = vim.bo.filetype - if current_filetype == "spectre_panel" then - vim.cmd("set backspace=" .. backspace) + if current_filetype == 'spectre_panel' then + vim.cmd('set backspace=' .. backspace) end end, - desc = "spectre anti-insert-breakage → restore the 'backspace' option." + desc = "spectre anti-insert-breakage → restore the 'backspace' option.", }) - api.nvim_buf_set_keymap(bufnr, 'i', '', "", map_opt) -- disable ENTER on insert mode, it breaks the UI. + api.nvim_buf_set_keymap(bufnr, 'i', '', '', map_opt) -- disable ENTER on insert mode, it breaks the UI. end end local function hl_match(opts) if #opts.search_query > 0 then - api.nvim_buf_add_highlight(state.bufnr, config.namespace, - state.user_config.highlight.search, 2, 0, -1) + api.nvim_buf_add_highlight(state.bufnr, config.namespace, state.user_config.highlight.search, 2, 0, -1) end if #opts.replace_query > 0 then - api.nvim_buf_add_highlight(state.bufnr, config.namespace, - state.user_config.highlight.replace, 4, 0, -1) + api.nvim_buf_add_highlight(state.bufnr, config.namespace, state.user_config.highlight.replace, 4, 0, -1) end end @@ -281,21 +279,24 @@ local function can_edit_line() end M.on_insert_enter = function() - if can_edit_line() then return end - local key = api.nvim_replace_termcodes("", true, false, true) - api.nvim_feedkeys(key, "m", true) + if can_edit_line() then + return + end + local key = api.nvim_replace_termcodes('', true, false, true) + api.nvim_feedkeys(key, 'm', true) print("You can't make changes in results.") end - M.on_search_change = function() - if not can_edit_line() then return end + if not can_edit_line() then + return + end local lines = api.nvim_buf_get_lines(state.bufnr, 0, config.lnum_UI, false) local query = { - replace_query = "", - search_query = "", - path = "", + replace_query = '', + search_query = '', + path = '', } for index, line in pairs(lines) do @@ -342,12 +343,12 @@ end M.on_close = function() M.stop() - vim.api.nvim_create_augroup("SpectrePanelWrite", { clear = true }) - state.query_backup = vim.tbl_extend("force", state.query, {}) + vim.api.nvim_create_augroup('SpectrePanelWrite', { clear = true }) + state.query_backup = vim.tbl_extend('force', state.query, {}) end M.on_leave = function() - state.query_backup = vim.tbl_extend("force", state.query, {}) + state.query_backup = vim.tbl_extend('force', state.query, {}) end M.resume_last_search = function() @@ -358,7 +359,7 @@ M.resume_last_search = function() ui.render_text_query({ replace_text = state.query_backup.replace_query, search_text = state.query_backup.search_query, - path = state.query_backup.path + path = state.query_backup.path, }) ui.render_search_ui() M.search(state.query_backup) @@ -381,26 +382,20 @@ M.do_replace_text = function(opts, async_id) if state.async_id ~= async_id then return end - ui.render_line( - state.bufnr, - config.namespace, - { - search_query = state.query.search_query, - replace_query = state.query.replace_query, - search_text = item.search_text, - lnum = item.display_lnum, - item_line = item.lnum, - is_replace = true, - }, - { - is_disable = item.disable, - padding_text = state.user_config.result_padding, - padding = #state.user_config.result_padding, - show_search = state.view.show_search, - show_replace = state.view.show_replace, - }, - state.regex - ) + ui.render_line(state.bufnr, config.namespace, { + search_query = state.query.search_query, + replace_query = state.query.replace_query, + search_text = item.search_text, + lnum = item.display_lnum, + item_line = item.lnum, + is_replace = true, + }, { + is_disable = item.disable, + padding_text = state.user_config.result_padding, + padding = #state.user_config.result_padding, + show_search = state.view.show_search, + show_replace = state.view.show_replace, + }, state.regex) count = count + 1 -- delay to next scheduler after 100 time if count > 100 then @@ -412,18 +407,18 @@ end M.change_view = function(reset) if reset then - state.view.mode = "" + state.view.mode = '' end if state.view.mode == 'replace' then - state.view.mode = "search" + state.view.mode = 'search' state.view.show_search = true state.view.show_replace = false elseif state.view.mode == 'both' then - state.view.mode = "replace" + state.view.mode = 'replace' state.view.show_search = false state.view.show_replace = true else - state.view.mode = "both" + state.view.mode = 'both' state.view.show_search = true state.view.show_replace = true end @@ -443,66 +438,57 @@ end M.toggle_line = function(line_visual) if can_edit_line() then -- delete line content - vim.cmd [[:normal! ^d$]] + vim.cmd([[:normal! ^d$]]) return false end local lnum = line_visual or unpack(vim.api.nvim_win_get_cursor(0)) local item = state.total_item[lnum] if item ~= nil and item.display_lnum == lnum - 1 then item.disable = not item.disable - ui.render_line( - state.bufnr, - config.namespace, - { - search_query = state.query.search_query, - replace_query = state.query.replace_query, - search_text = item.search_text, - lnum = item.display_lnum, - item_line = item.lnum, - is_replace = true - }, - { - is_disable = item.disable, - padding_text = state.user_config.result_padding, - padding = #state.user_config.result_padding, - show_search = state.view.show_search, - show_replace = state.view.show_replace - }, - state.regex - ) + ui.render_line(state.bufnr, config.namespace, { + search_query = state.query.search_query, + replace_query = state.query.replace_query, + search_text = item.search_text, + lnum = item.display_lnum, + item_line = item.lnum, + is_replace = true, + }, { + is_disable = item.disable, + padding_text = state.user_config.result_padding, + padding = #state.user_config.result_padding, + show_search = state.view.show_search, + show_replace = state.view.show_replace, + }, state.regex) return elseif not line_visual then -- delete all item in 1 file local line = vim.fn.getline(lnum) - local check = string.find(line, "([^%s]*%:%d*:%d*:)$") + local check = string.find(line, '([^%s]*%:%d*:%d*:)$') if check then check = state.total_item[lnum + 1] - if check == nil then return end + if check == nil then + return + end local disable = not check.disable item = check local index = lnum + 1 while item ~= nil and check.filename == item.filename do item.disable = disable - ui.render_line( - state.bufnr, - config.namespace, - { - search_query = state.query.search_query, - replace_query = state.query.replace_query, - search_text = item.search_text, - lnum = item.display_lnum, - item_line = item.lnum, - is_replace = true - }, - { - is_disable = item.disable, - padding_text = state.user_config.result_padding, - padding = #state.user_config.result_padding, - show_search = state.view.show_search, - show_replace = state.view.show_replace - }, state.regex - ) + ui.render_line(state.bufnr, config.namespace, { + search_query = state.query.search_query, + replace_query = state.query.replace_query, + search_text = item.search_text, + lnum = item.display_lnum, + item_line = item.lnum, + is_replace = true, + }, { + is_disable = item.disable, + padding_text = state.user_config.result_padding, + padding = #state.user_config.result_padding, + show_search = state.view.show_search, + show_replace = state.view.show_replace, + }, state.regex) index = index + 1 item = state.total_item[index] end @@ -510,7 +496,6 @@ M.toggle_line = function(line_visual) end end - M.search_handler = function() local c_line = 0 local total = 0 @@ -522,80 +507,67 @@ M.search_handler = function() on_start = function() state.total_item = {} state.is_running = true - state.status_line = "Start search" + state.status_line = 'Start search' c_line = config.line_result total = 0 start_time = vim.loop.hrtime() end, on_result = function(item) - if not state.is_running then return end + if not state.is_running then + return + end item.replace_text = '' if string.match(item.filename, '^%.%/') then item.filename = item.filename:sub(3, #item.filename) end item.search_text = utils.truncate(utils.trim(item.text), 255) if #state.query.replace_query > 1 then - item.replace_text = state.regex.replace_all( - state.query.search_query, - state.query.replace_query, - item.search_text - ) + item.replace_text = + state.regex.replace_all(state.query.search_query, state.query.replace_query, item.search_text) end if last_filename ~= item.filename then - ui.render_filename( - state.bufnr, - config.namespace, - c_line, - item - ) + ui.render_filename(state.bufnr, config.namespace, c_line, item) c_line = c_line + 1 last_filename = item.filename end item.display_lnum = c_line - ui.render_line( - state.bufnr, - config.namespace, - { - search_query = state.query.search_query, - replace_query = state.query.replace_query, - search_text = item.search_text, - lnum = item.display_lnum, - item_line = item.lnum, - is_replace = false - }, - { - is_disable = item.disable, - padding_text = cfg.result_padding, - padding = padding, - show_search = state.view.show_search, - show_replace = state.view.show_replace - }, - state.regex - ) + ui.render_line(state.bufnr, config.namespace, { + search_query = state.query.search_query, + replace_query = state.query.replace_query, + search_text = item.search_text, + lnum = item.display_lnum, + item_line = item.lnum, + is_replace = false, + }, { + is_disable = item.disable, + padding_text = cfg.result_padding, + padding = padding, + show_search = state.view.show_search, + show_replace = state.view.show_replace, + }, state.regex) c_line = c_line + 1 total = total + 1 - state.status_line = "Item " .. total + state.status_line = 'Item ' .. total state.total_item[c_line] = item end, on_error = function(error_msg) - api.nvim_buf_set_lines(state.bufnr, c_line, c_line + 1, false, - { cfg.result_padding .. error_msg }) - api.nvim_buf_add_highlight(state.bufnr, config.namespace, - cfg.highlight.border, c_line, 0, padding) + api.nvim_buf_set_lines(state.bufnr, c_line, c_line + 1, false, { cfg.result_padding .. error_msg }) + api.nvim_buf_add_highlight(state.bufnr, config.namespace, cfg.highlight.border, c_line, 0, padding) c_line = c_line + 1 state.finder_instance = nil end, on_finish = function() - if not state.is_running then return end + if not state.is_running then + return + end local end_time = (vim.loop.hrtime() - start_time) / 1E9 - state.status_line = string.format("Total: %s match, time: %ss", total, end_time) + state.status_line = string.format('Total: %s match, time: %ss', total, end_time) api.nvim_buf_set_lines(state.bufnr, c_line, c_line, false, { cfg.line_sep, }) - api.nvim_buf_add_highlight(state.bufnr, config.namespace, - cfg.highlight.border, c_line, 0, -1) + api.nvim_buf_add_highlight(state.bufnr, config.namespace, cfg.highlight.border, c_line, 0, -1) state.vt.status_id = utils.write_virtual_text( state.bufnr, @@ -605,11 +577,10 @@ M.search_handler = function() ) state.finder_instance = nil state.is_running = false - end + end, } end - M.stop = function() state.is_running = false log.debug('spectre stop') @@ -623,10 +594,7 @@ M.search = function(opts) M.stop() opts = opts or state.query local finder_creator = state_utils.get_finder_creator() - state.finder_instance = finder_creator:new( - state_utils.get_search_engine_config(), - M.search_handler() - ) + state.finder_instance = finder_creator:new(state_utils.get_search_engine_config(), M.search_handler()) if not opts.search_query or #opts.search_query < 2 then return end @@ -636,13 +604,8 @@ M.search = function(opts) api.nvim_buf_set_lines(state.bufnr, config.line_result - 1, -1, false, {}) hl_match(opts) local c_line = config.line_result - api.nvim_buf_set_lines(state.bufnr, - c_line - 1, c_line - 1, - false, - { state.user_config.line_sep_start } - ) - api.nvim_buf_add_highlight(state.bufnr, config.namespace, - state.user_config.highlight.border, c_line - 1, 0, -1) + api.nvim_buf_set_lines(state.bufnr, c_line - 1, c_line - 1, false, { state.user_config.line_sep_start }) + api.nvim_buf_add_highlight(state.bufnr, config.namespace, state.user_config.highlight.border, c_line - 1, 0, -1) state.total_item = {} state.finder_instance:search({ cwd = state.cwd, @@ -663,7 +626,6 @@ M.init_regex = function() state.regex.change_options(replace_config.options_value) end - M.show_help = function() ui.show_help() end @@ -672,12 +634,12 @@ M.change_engine_replace = function(engine_name) if state.user_config.replace_engine[engine_name] then state.user_config.default.replace.cmd = engine_name M.init_regex() - vim.notify("change replace engine to: " .. engine_name) + vim.notify('change replace engine to: ' .. engine_name) ui.render_header(state.user_config) M.search() return else - vim.notify(string.format("engine %s not found " .. engine_name)) + vim.notify(string.format('engine %s not found ' .. engine_name)) end end @@ -686,7 +648,9 @@ M.change_options = function(key) state.options[key] = false end state.options[key] = not state.options[key] - if state.regex == nil then return end + if state.regex == nil then + return + end state.regex.change_options(state_utils.get_replace_engine_config().options_value) if state.query.search_query ~= nil then ui.render_search_ui() @@ -710,12 +674,16 @@ M.get_fold = function(lnum) return '0' end local line = vim.fn.getline(lnum) - local check = string.find(line, "([^%s]*%:%d*:%d*:)$") - if check then return '>1' end + local check = string.find(line, '([^%s]*%:%d*:%d*:)$') + if check then + return '>1' + end local nextline = vim.fn.getline(lnum + 1) - local nextcheck = string.find(nextline, "([^%s]*%:%d*:%d*:)$") - if nextcheck then return '<1' end + local nextcheck = string.find(nextline, '([^%s]*%:%d*:%d*:)$') + if nextcheck then + return '<1' + end local item = state.total_item[lnum] if item ~= nil then return '1' @@ -725,14 +693,22 @@ end M.tab = function() local line = vim.api.nvim_win_get_cursor(0)[1] - if line == 3 then vim.api.nvim_win_set_cursor(vim.api.nvim_get_current_win(), { 5, 1 }) end - if line == 5 then vim.api.nvim_win_set_cursor(vim.api.nvim_get_current_win(), { 7, 1 }) end + if line == 3 then + vim.api.nvim_win_set_cursor(vim.api.nvim_get_current_win(), { 5, 1 }) + end + if line == 5 then + vim.api.nvim_win_set_cursor(vim.api.nvim_get_current_win(), { 7, 1 }) + end end M.tab_shift = function() local line = vim.api.nvim_win_get_cursor(0)[1] - if line == 5 then vim.api.nvim_win_set_cursor(vim.api.nvim_get_current_win(), { 3, 1 }) end - if line == 7 then vim.api.nvim_win_set_cursor(vim.api.nvim_get_current_win(), { 5, 1 }) end + if line == 5 then + vim.api.nvim_win_set_cursor(vim.api.nvim_get_current_win(), { 3, 1 }) + end + if line == 7 then + vim.api.nvim_win_set_cursor(vim.api.nvim_get_current_win(), { 5, 1 }) + end end return M diff --git a/lua/spectre/regex/rust.lua b/lua/spectre/regex/rust.lua index 99cb00a..59d6eb7 100644 --- a/lua/spectre/regex/rust.lua +++ b/lua/spectre/regex/rust.lua @@ -14,7 +14,7 @@ rust.change_options = function(options_value) rust.flag = rust.flag .. v end if #rust.flag > 0 then - rust.flag = string.format("(?%s)", rust.flag) + rust.flag = string.format('(?%s)', rust.flag) end end end @@ -24,7 +24,7 @@ rust.matchstr = function(search_text, search_query) if not ok then log.debug(search_text) log.debug(result) - return "" + return '' end return result end diff --git a/lua/spectre/regex/vim.lua b/lua/spectre/regex/vim.lua index 0fdc9e6..4f92be0 100644 --- a/lua/spectre/regex/vim.lua +++ b/lua/spectre/regex/vim.lua @@ -6,31 +6,26 @@ local vim_regex = {} vim_regex.change_options = function(_) end vim_regex.matchstr = function(search_text, search_query) - local ok, match = pcall( - vim.fn.matchstr, - search_text, - "\\v" .. utils.escape_vim_magic(search_query) - ) + local ok, match = pcall(vim.fn.matchstr, search_text, '\\v' .. utils.escape_vim_magic(search_query)) if ok then return match end end vim_regex.replace_all = function(search_query, replace_query, text) - local result = vim.fn.substitute( - text, - "\\v" .. utils.escape_vim_magic(search_query), - replace_query, - 'g' - ) + local result = vim.fn.substitute(text, '\\v' .. utils.escape_vim_magic(search_query), replace_query, 'g') return result end --- get all position of text match in string ---@return table col{{start1, end1},{start2, end2}} math in line vim_regex.match_text_line = function(match, str, padding) - if match == nil or str == nil then return {} end - if match == "" or str == "" then return {} end + if match == nil or str == nil then + return {} + end + if match == '' or str == '' then + return {} + end padding = padding or 0 local index = 0 local len = string.len(str) @@ -48,7 +43,6 @@ vim_regex.match_text_line = function(match, str, padding) return col_tbl end -vim_regex.replace_file = function(filepath, lnum, search_query, replace_query) -end +vim_regex.replace_file = function(filepath, lnum, search_query, replace_query) end return vim_regex diff --git a/lua/spectre/replace/base.lua b/lua/spectre/replace/base.lua index 1904e61..edc2616 100644 --- a/lua/spectre/replace/base.lua +++ b/lua/spectre/replace/base.lua @@ -7,17 +7,17 @@ base.on_error = function(self, value, ref) pcall(vim.schedule_wrap(function() self.handler.on_error({ value = value, - ref = ref + ref = ref, }) end)) end end -base.on_done= function(self, value, ref) +base.on_done = function(self, value, ref) if value == 0 or value == true then pcall(vim.schedule_wrap(function() self.handler.on_done({ - ref = ref + ref = ref, }) end)) else @@ -25,22 +25,19 @@ base.on_done= function(self, value, ref) end end - local function extend(child) local creator = {} creator.__index = creator function creator:new(config, handler) - assert(config ~= nil, "replace config not nil") + assert(config ~= nil, 'replace config not nil') handler = vim.tbl_extend('force', { - on_error = function() - end, - on_done = function() - end, + on_error = function() end, + on_done = function() end, }, handler or {}) local replace_state = child:init(config) local replace = { state = replace_state, - handler = handler + handler = handler, } local meta = {} meta.__index = vim.tbl_extend('force', base, child) diff --git a/lua/spectre/replace/init.lua b/lua/spectre/replace/init.lua index f8a4ed5..5afba68 100644 --- a/lua/spectre/replace/init.lua +++ b/lua/spectre/replace/init.lua @@ -1,11 +1,11 @@ local base = require('spectre.replace.base') local r = {} r.get = function(key) - assert(key ~= nil, "key no nil") - local ok, engine = pcall(require, "spectre.replace." .. key) + assert(key ~= nil, 'key no nil') + local ok, engine = pcall(require, 'spectre.replace.' .. key) if not ok then - print("No replace engine" .. key) - engine = require("spectre.replace.sed") + print('No replace engine' .. key) + engine = require('spectre.replace.sed') end engine.name = key return base.extend(engine) @@ -14,5 +14,5 @@ end return setmetatable(r, { __index = function(self, key) return self.get(key) - end + end, }) diff --git a/lua/spectre/replace/oxi.lua b/lua/spectre/replace/oxi.lua index 1c9bd08..92d4110 100644 --- a/lua/spectre/replace/oxi.lua +++ b/lua/spectre/replace/oxi.lua @@ -1,13 +1,13 @@ local oxi = {} local Path = require('plenary.path') -local regex = require("spectre.regex.rust") +local regex = require('spectre.regex.rust') oxi.init = function(_, config) return config end oxi.replace = function(self, value) local cwd = value.cwd or vim.loop.cwd() - if not value.filename:match("^%/") then + if not value.filename:match('^%/') then value.filename = Path:new(cwd):joinpath(value.filename) end diff --git a/lua/spectre/replace/sed.lua b/lua/spectre/replace/sed.lua index 3e59ec8..e81ca9e 100644 --- a/lua/spectre/replace/sed.lua +++ b/lua/spectre/replace/sed.lua @@ -1,14 +1,13 @@ -local Job = require("plenary.job") +local Job = require('plenary.job') local utils = require('spectre.utils') local log = require('spectre._log') local sed = {} - sed.init = function(_, config) config = vim.tbl_extend('force', { - cmd = "sed", - pattern = "%s,%ss/%s/%s/g", + cmd = 'sed', + pattern = '%s,%ss/%s/%s/g', args = { '-i', '-E', @@ -22,7 +21,7 @@ sed.replace = function(self, value) if self.state.options_value ~= nil then for _, v in pairs(self.state.options_value) do if v == '--ignore-case' then - pattern = pattern .. "i" + pattern = pattern .. 'i' end end end @@ -40,10 +39,12 @@ sed.replace = function(self, value) value.filename, }) - log.debug("replace cwd " .. (value.cwd or '')) - log.debug("replace cmd: " .. self.state.cmd .. ' ' .. table.concat(args, ' ')) + log.debug('replace cwd ' .. (value.cwd or '')) + log.debug('replace cmd: ' .. self.state.cmd .. ' ' .. table.concat(args, ' ')) - if value.cwd == "" then value.cwd = nil end + if value.cwd == '' then + value.cwd = nil + end local job = Job:new({ command = self.state.cmd, cwd = value.cwd, @@ -53,7 +54,9 @@ sed.replace = function(self, value) v = self.state.cmd .. ' "' .. table.concat(args, '" "') .. '"\n' .. v self:on_error(v, value) end, - on_exit = function(_, v) self:on_done(v, value) end + on_exit = function(_, v) + self:on_done(v, value) + end, }) job:sync() end diff --git a/lua/spectre/search/ag.lua b/lua/spectre/search/ag.lua index 6bc284a..0ca1ae5 100644 --- a/lua/spectre/search/ag.lua +++ b/lua/spectre/search/ag.lua @@ -2,10 +2,10 @@ local ag = {} ag.init = function(_, config) config = vim.tbl_extend('force', { - cmd = "ag", + cmd = 'ag', args = { '--vimgrep', - '-s' + '-s', }, }, config or {}) return config @@ -16,9 +16,9 @@ ag.get_path_args = function(_, paths) return {} end - local pattern = "" + local pattern = '' for _, path in ipairs(paths) do - pattern = pattern .. path .. "|" + pattern = pattern .. path .. '|' end return { '-G', pattern:sub(1, #pattern - 1) } end diff --git a/lua/spectre/search/base.lua b/lua/spectre/search/base.lua index 987aaaf..1ac3889 100644 --- a/lua/spectre/search/base.lua +++ b/lua/spectre/search/base.lua @@ -1,10 +1,10 @@ ---@diagnostic disable: param-type-mismatch local flatten = vim.tbl_flatten -local Job = require("plenary.job") +local Job = require('plenary.job') local log = require('spectre._log') local MAX_LINE_CHARS = 255 local utils = require('spectre.utils') -local is_win = vim.api.nvim_call_function("has", { "win32" }) == 1 +local is_win = vim.api.nvim_call_function('has', { 'win32' }) == 1 local base = {} base.__index = base @@ -13,8 +13,8 @@ base.__index = base ---@return string[] local function scan_paths(s_path) local paths = {} - local path = "" - local escape_char = is_win and "^" or "\\" + local path = '' + local escape_char = is_win and '^' or '\\' local i = 1 while i <= #s_path do @@ -25,13 +25,13 @@ local function scan_paths(s_path) i = i + 1 path = path .. s_path:sub(i, i) end - elseif char:match("%s") then + elseif char:match('%s') then -- Unescaped whitespace: split here. - if path ~= "" then + if path ~= '' then table.insert(paths, path) end - path = "" - i = i + s_path:sub(i, -1):match("^%s+()") - 2 + path = '' + i = i + s_path:sub(i, -1):match('^%s+()') - 2 else path = path .. char end @@ -47,14 +47,15 @@ local function scan_paths(s_path) end base.get_path_args = function(self, path) - print("[spectre] should implement path_args for ", self.state.cmd) + print('[spectre] should implement path_args for ', self.state.cmd) return { path } end - base.on_output = function(self, output_text) pcall(vim.schedule_wrap(function() - if output_text == nil then return end + if output_text == nil then + return + end -- it make vim broken with (min.js) file has a long line if string.len(output_text) > MAX_LINE_CHARS then output_text = string.sub(output_text, 0, MAX_LINE_CHARS) @@ -69,7 +70,7 @@ end base.on_error = function(self, output_text) if output_text ~= nil then - log.debug("search error " .. output_text) + log.debug('search error ' .. output_text) pcall(vim.schedule_wrap(function() self.handler.on_error(output_text) end)) @@ -83,10 +84,10 @@ base.on_exit = function(self, value) end base.search = function(self, query) - local args = flatten { + local args = flatten({ -- query.search_text, self.state.args, - } + }) if query.path then local args_path = self:get_path_args(scan_paths(query.path)) table.insert(args, args_path) @@ -97,10 +98,10 @@ base.search = function(self, query) end -- no more args - table.insert(args, "--") + table.insert(args, '--') args = flatten(args) - if query.cwd == "" then + if query.cwd == '' then query.cwd = nil end table.insert(args, query.search_text) @@ -114,10 +115,8 @@ base.search = function(self, query) table.insert(args, '.') end - - log.debug("search cwd " .. (query.cwd or '')) - log.debug("search: " .. self.state.cmd .. ' ' .. table.concat(args, ' ')) - + log.debug('search cwd ' .. (query.cwd or '')) + log.debug('search: ' .. self.state.cmd .. ' ' .. table.concat(args, ' ')) self.handler.on_start() self.job = Job:new({ @@ -125,9 +124,15 @@ base.search = function(self, query) command = self.state.cmd, cwd = query.cwd, args = args, - on_stdout = function(_, value) self:on_output(value) end, - on_stderr = function(_, value) self:on_error(value) end, - on_exit = function(_, value) self:on_exit(value) end + on_stdout = function(_, value) + self:on_output(value) + end, + on_stderr = function(_, value) + self:on_error(value) + end, + on_exit = function(_, value) + self:on_exit(value) + end, }) self.job:start() @@ -145,21 +150,17 @@ local function extend(child) local creator = {} creator.__index = creator function creator:new(config, handler) - assert(config ~= nil, "search config not nil") + assert(config ~= nil, 'search config not nil') handler = vim.tbl_extend('force', { - on_start = function() - end, - on_result = function() - end, - on_error = function() - end, - on_finish = function() - end + on_start = function() end, + on_result = function() end, + on_error = function() end, + on_finish = function() end, }, handler or {}) local engine_state = child:init(config) local search = { state = engine_state, - handler = handler + handler = handler, } local meta = {} -- if child already have function then it will call child function diff --git a/lua/spectre/search/init.lua b/lua/spectre/search/init.lua index a8ee77d..7874a49 100644 --- a/lua/spectre/search/init.lua +++ b/lua/spectre/search/init.lua @@ -1,11 +1,11 @@ local base = require('spectre.search.base') local s = {} s.get = function(key) - assert(key ~= nil, "key no nil") - local ok, engine = pcall(require, "spectre.search." .. key) + assert(key ~= nil, 'key no nil') + local ok, engine = pcall(require, 'spectre.search.' .. key) if not ok then - print("No search engine " .. key) - engine = require("spectre.search.rg") + print('No search engine ' .. key) + engine = require('spectre.search.rg') end engine.name = key return base.extend(engine) @@ -13,5 +13,5 @@ end return setmetatable(s, { __index = function(self, key) return self.get(key) - end + end, }) diff --git a/lua/spectre/search/rg.lua b/lua/spectre/search/rg.lua index 0cfca4d..c90ea4d 100644 --- a/lua/spectre/search/rg.lua +++ b/lua/spectre/search/rg.lua @@ -2,7 +2,7 @@ local rg = {} rg.init = function(_, config) config = vim.tbl_extend('force', { - cmd = "rg", + cmd = 'rg', args = { '--color=never', '--no-heading', @@ -21,11 +21,10 @@ rg.get_path_args = function(_, paths) local args = {} for _, path in ipairs(paths) do - table.insert(args, "-g") + table.insert(args, '-g') table.insert(args, path) end return args end - return rg diff --git a/lua/spectre/state.lua b/lua/spectre/state.lua index 81d7578..1c9f7b6 100644 --- a/lua/spectre/state.lua +++ b/lua/spectre/state.lua @@ -4,7 +4,6 @@ ---@field path string ---@field is_file boolean - ---@class SpectreState ---@field user_config SpectreConfig ---@field status_line string @@ -27,21 +26,21 @@ local state = { search_query = '', replace_query = '', path = '', - is_file = false -- search in current file + is_file = false, -- search in current file }, query_backup = nil, -- display text and highlight on result view = { - mode = "both", + mode = 'both', search = true, - replace = true + replace = true, }, -- virtual text namespace vt = {}, --for options options = { ['ignore-case'] = false, - ['hidden'] = false + ['hidden'] = false, }, regex = nil, user_config = nil, @@ -50,7 +49,7 @@ local state = { target_winid = nil, total_item = {}, is_running = false, - is_open = false + is_open = false, } if _G.__is_dev then diff --git a/lua/spectre/state_utils.lua b/lua/spectre/state_utils.lua index b37fc07..0279f0b 100644 --- a/lua/spectre/state_utils.lua +++ b/lua/spectre/state_utils.lua @@ -45,8 +45,8 @@ end M.status_line = function(opt) opt = opt or {} - local slant_right = opt.seprator or ''; - local main_color = opt.main_color or 'black'; + local slant_right = opt.seprator or '' + local main_color = opt.main_color or 'black' local spectre = { filetypes = { 'spectre_panel' }, active = { @@ -57,7 +57,6 @@ M.status_line = function(opt) text = { 'black', 'white' }, sep_left = { main_color, 'white' }, sep_right = { 'white', 'NormalBg' }, - }, text = function() if state.status_line == '' or state.status_line == nil then @@ -66,12 +65,12 @@ M.status_line = function(opt) return { { slant_right, 'sep_left' }, { state.status_line, 'text' }, - { slant_right, 'sep_right' } + { slant_right, 'sep_right' }, } end - end + end, }, - { "%=", '' }, + { '%=', '' }, { slant_right, { 'NormalBg', main_color } }, { ' Spectre ', { 'white', main_color, 'bold' } }, }, diff --git a/lua/spectre/ui.lua b/lua/spectre/ui.lua index dc668e4..072981e 100644 --- a/lua/spectre/ui.lua +++ b/lua/spectre/ui.lua @@ -6,12 +6,11 @@ local utils = require('spectre.utils') local Path = require('plenary.path') -local popup = require "plenary.popup" +local popup = require('plenary.popup') local api = vim.api local M = {} - ---@param regex RegexEngine M.render_line = function(bufnr, namespace, text_opts, view_opts, regex) local cfg = state.user_config @@ -28,7 +27,7 @@ M.render_line = function(bufnr, namespace, text_opts, view_opts, regex) if cfg.lnum_for_results == true then item_line_len = string.len(text_opts.item_line) + 1 api.nvim_buf_set_lines(bufnr, text_opts.lnum, end_lnum, false, { - view_opts.padding_text .. text_opts.item_line .. " " .. diff.text, + view_opts.padding_text .. text_opts.item_line .. ' ' .. diff.text, }) else api.nvim_buf_set_lines(bufnr, text_opts.lnum, end_lnum, false, { @@ -38,20 +37,35 @@ M.render_line = function(bufnr, namespace, text_opts, view_opts, regex) if not view_opts.is_disable then for _, value in pairs(diff.search) do - api.nvim_buf_add_highlight(bufnr, namespace, + api.nvim_buf_add_highlight( + bufnr, + namespace, cfg.highlight.search, - text_opts.lnum, value[1] + view_opts.padding + item_line_len, value[2] + view_opts.padding + item_line_len) + text_opts.lnum, + value[1] + view_opts.padding + item_line_len, + value[2] + view_opts.padding + item_line_len + ) end for _, value in pairs(diff.replace) do - api.nvim_buf_add_highlight(bufnr, namespace, + api.nvim_buf_add_highlight( + bufnr, + namespace, cfg.highlight.replace, - text_opts.lnum, value[1] + view_opts.padding + item_line_len, value[2] + view_opts.padding + item_line_len) + text_opts.lnum, + value[1] + view_opts.padding + item_line_len, + value[2] + view_opts.padding + item_line_len + ) end - api.nvim_buf_add_highlight(state.bufnr, config.namespace, - cfg.highlight.border, text_opts.lnum, 0, view_opts.padding) + api.nvim_buf_add_highlight( + state.bufnr, + config.namespace, + cfg.highlight.border, + text_opts.lnum, + 0, + view_opts.padding + ) else - api.nvim_buf_add_highlight(state.bufnr, config.namespace, - cfg.highlight.border, text_opts.lnum, 0, -1) + api.nvim_buf_add_highlight(state.bufnr, config.namespace, cfg.highlight.border, text_opts.lnum, 0, -1) end end @@ -77,51 +91,43 @@ end)() M.render_filename = function(bufnr, namespace, line, entry) local u_config = state.user_config - local filename = vim.fn.fnamemodify(entry.filename, ":t") - local directory = vim.fn.fnamemodify(entry.filename, ":h") - if directory == "." then - directory = "" + local filename = vim.fn.fnamemodify(entry.filename, ':t') + local directory = vim.fn.fnamemodify(entry.filename, ':h') + if directory == '.' then + directory = '' else directory = directory .. Path.path.sep end local icon_length = state.user_config.color_devicons and 4 or 2 - local icon, icon_highlight = get_devicons( - filename, - state.user_config.color_devicons, '+') + local icon, icon_highlight = get_devicons(filename, state.user_config.color_devicons, '+') api.nvim_buf_set_lines(state.bufnr, line, line, false, { - string.format("%s %s%s:", icon, directory, filename), + string.format('%s %s%s:', icon, directory, filename), }) local width = vim.api.nvim_strwidth(filename) local hl = { - { { 0, icon_length }, icon_highlight }, + { { 0, icon_length }, icon_highlight }, { { 0, vim.api.nvim_strwidth(directory) }, u_config.highlight.filedirectory }, - { { 0, width + 1 }, u_config.highlight.filename }, + { { 0, width + 1 }, u_config.highlight.filename }, } - if icon == "" then + if icon == '' then table.remove(hl, 1) end local pos = 0 for _, value in pairs(hl) do pcall(function() - api.nvim_buf_add_highlight(bufnr, - namespace, - value[2], - line, - pos + value[1][1], - pos + value[1][2] - ) - pos = value[1][2] + pos - end) + api.nvim_buf_add_highlight(bufnr, namespace, value[2], line, pos + value[1][1], pos + value[1][2]) + pos = value[1][2] + pos + end) end end function M.render_search_ui() api.nvim_buf_clear_namespace(state.bufnr, config.namespace_ui, 0, config.lnum_UI) local details_ui = {} - local search_message = "Search: " + local search_message = 'Search: ' local cfg = state_utils.get_search_engine_config() for key, value in pairs(state.options) do if value == true and cfg.options[key] then @@ -130,10 +136,10 @@ function M.render_search_ui() end table.insert(details_ui, { { search_message, state.user_config.highlight.ui } }) - table.insert(details_ui, { { "Replace: ", state.user_config.highlight.ui } }) - local path_message = "Path:" + table.insert(details_ui, { { 'Replace: ', state.user_config.highlight.ui } }) + local path_message = 'Path:' if state.cwd then - path_message = path_message .. string.format(" cwd=%s", state.cwd) + path_message = path_message .. string.format(' cwd=%s', state.cwd) end table.insert(details_ui, { { path_message, state.user_config.highlight.ui } }) @@ -148,36 +154,45 @@ end function M.render_header(opts) api.nvim_buf_clear_namespace(state.bufnr, config.namespace_header, 0, config.lnum_UI) local help_text = string.format( - "[Nvim Spectre] (Search by %s) %s (Replace by %s) (Press ? for mappings)", + '[Nvim Spectre] (Search by %s) %s (Replace by %s) (Press ? for mappings)', state.user_config.default.find.cmd, opts.live_update and '(Auto update)' or '', state.user_config.default.replace.cmd ) - utils.write_virtual_text(state.bufnr, config.namespace_header, 0, - { { help_text, state.user_config.highlight.headers } }) + utils.write_virtual_text( + state.bufnr, + config.namespace_header, + 0, + { { help_text, state.user_config.highlight.headers } } + ) end M.show_menu_options = function(title, content) local win_width, win_height = vim.lsp.util._make_floating_popup_size(content, {}) local bufnr = vim.api.nvim_create_buf(false, true) - api.nvim_buf_set_option(bufnr, "bufhidden", "wipe") + api.nvim_buf_set_option(bufnr, 'bufhidden', 'wipe') api.nvim_buf_set_lines(bufnr, 0, -1, true, content) local help_win = vim.api.nvim_open_win(bufnr, false, { - style = "minimal", - title = " " .. title .. " ", + style = 'minimal', + title = ' ' .. title .. ' ', title_pos = 'center', relative = 'cursor', width = win_width, height = win_height, col = 0, row = 1, - border = "rounded" + border = 'rounded', }) api.nvim_win_set_option(help_win, 'winblend', 0) - api.nvim_buf_set_keymap(bufnr, 'n', '', 'lua vim.api.nvim_win_close(' .. help_win .. ', true)', - { noremap = true }) + api.nvim_buf_set_keymap( + bufnr, + 'n', + '', + 'lua vim.api.nvim_win_close(' .. help_win .. ', true)', + { noremap = true } + ) api.nvim_create_autocmd({ 'CursorMovedI', @@ -197,7 +212,7 @@ end M.show_help = function() local help_msg = {} - local map_tbl = {}; + local map_tbl = {} for _, map in pairs(state.user_config.mapping) do table.insert(map_tbl, map) end @@ -207,37 +222,37 @@ M.show_help = function() end) for _, map in pairs(map_tbl) do - table.insert(help_msg, string.format("%9s : %s", map.map, map.desc)) + table.insert(help_msg, string.format('%9s : %s', map.map, map.desc)) end - M.show_menu_options("Mappings", help_msg) + M.show_menu_options('Mappings', help_msg) end M.show_options = function() local cfg = state_utils.get_search_engine_config() - local help_msg = { " Press number to select option." } + local help_msg = { ' Press number to select option.' } local option_cmd = {} local i = 1 for key, option in pairs(cfg.options) do - table.insert(help_msg, string.format(" %s : toggle %s", i, option.desc or ' ')) + table.insert(help_msg, string.format(' %s : toggle %s', i, option.desc or ' ')) table.insert(option_cmd, key) i = i + 1 end - M.show_menu_options("Options", help_msg) + M.show_menu_options('Options', help_msg) return option_cmd end M.show_find_engine = function() - local engines = state.user_config.find_engine; + local engines = state.user_config.find_engine - local help_msg = { " Press number to select option." } + local help_msg = { ' Press number to select option.' } local option_cmd = {} local i = 1 for key, option in pairs(engines) do - table.insert(help_msg, string.format(" %s : engine %s", i, option.desc or ' ')) + table.insert(help_msg, string.format(' %s : engine %s', i, option.desc or ' ')) table.insert(option_cmd, key) i = i + 1 end @@ -248,7 +263,7 @@ M.render_text_query = function(opts) local lines = {} local length = config.lnum_UI for _ = 1, length, 1 do - table.insert(lines, "") + table.insert(lines, '') end api.nvim_buf_set_lines(state.bufnr, 0, 0, false, lines) api.nvim_buf_set_lines(state.bufnr, 2, 2, false, { opts.search_text }) diff --git a/lua/spectre/utils.lua b/lua/spectre/utils.lua index 438ecd9..4c6c116 100644 --- a/lua/spectre/utils.lua +++ b/lua/spectre/utils.lua @@ -1,7 +1,7 @@ local api = vim.api local M = {} -local Job = require("plenary.job") +local Job = require('plenary.job') local config = require('spectre.config') local state = require('spectre.state') @@ -11,12 +11,18 @@ M.parse_line_grep = function(query) local t = { text = query } local _, _, filename, lnum, col, text = string.find(t.text, _regex_file_line) - if filename == nil then return nil end + if filename == nil then + return nil + end local ok ok, lnum = pcall(tonumber, lnum) - if not ok then return nil end + if not ok then + return nil + end ok, col = pcall(tonumber, col) - if not ok then return nil end + if not ok then + return nil + end t.filename = filename t.lnum = lnum @@ -31,30 +37,22 @@ end M.escape_vim_magic = function(query) query = string.gsub(query, '@', '\\@') local regex = [=[(\\)@<=](\\)@!]=] - return vim.fn.substitute( - query, - "\\v" .. regex, - [[\\\0]], - 'g' - ) + return vim.fn.substitute(query, '\\v' .. regex, [[\\\0]], 'g') end -- escape_chars but don't escape it if have slash before or after ! M.escape_chars = function(query) local regex = [=[(\\)@