Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comment inccommand #112

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions lua/textcase/plugin/plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ function M.register_replace_command(command, method_keys)
table.insert(M.state.methods_by_command[command], method)
end

-- The registered command for Subs replacements depends on the
-- availability of the "incremental command preview" feature
-- https://github.com/neovim/neovim/pull/18194
if flag_incremental_preview then
vim.api.nvim_create_user_command(
command,
Expand All @@ -117,13 +120,30 @@ function M.clear_match(command_namespace)
]])
end

-- This method is called for previewing the result and also
-- for computing the final result and to modify the buffer
-- with the replacements

-- Sample for preview mode:
-- :Subs/source/dest
-- In this case preview ~= nil and preview_buf ~= nil

-- Sample for final replacement
-- :Subs/source/dest<CR>
-- In this case preview_ns == nil and preview_buf == nil

-- <cmd>h command-preview
function M.incremental_substitute(opts, preview_ns, preview_buf)
local command = "Subs"
-- Equivalent to the method TextCaseSubstituteLauncher (start.vim) computing
-- if it is a multiline selection to use either normal or visual mode
local mode = (opts.range < 2 or opts.line1 == opts.line2) and "n" or "\22"
local params = vim.split(opts.args, "/")
local source, dest = params[2], params[3]
source = MixStringConectors(source)
dest = MixStringConectors(dest or "")

-- preview_ns and preview_buf indicates the buffer to be modified
local buf = (preview_ns ~= nil) and preview_buf or vim.api.nvim_get_current_buf()

local cursor_pos = vim.fn.getpos(".")
Expand Down Expand Up @@ -169,6 +189,7 @@ function M.incremental_substitute(opts, preview_ns, preview_buf)
vim.fn.setpos(".", cursor_pos)

if preview_ns ~= nil then
-- 2: Preview is shown and preview window is opened
return 2
end
end
Expand Down
7 changes: 7 additions & 0 deletions plugin/start.vim
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@ command! -range TextCaseStartReplacingCommand <line1>,<line2>lua require("textca


function! TextCaseSubstituteLauncher(...) range
" Stores the first argument as a global variable
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if the non-inccommand version is working. perhaps it is time to drop its support.
The feature appears at version 'nvim-0.8-dev+374-ge13dcdf16'

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then we could definitely get rid of it because I guess there are not many people using 0.7 or less nowadays 👍

let g:TextCaseSubsArgs = a:1

" Then it invokes the dispatcher in visual mode if multiple lines
" are selected, otherwise normal mode is passed

if a:firstline == a:lastline
" if a single line is selected
lua require'textcase'.dispatcher('n', vim.g.TextCaseSubsArgs)
elseif a:firstline == 1 && a:lastline == line("$")
" if all the file is selected
lua require'textcase'.dispatcher('n', vim.g.TextCaseSubsArgs)
else
" A range of multiple lines is selected
lua require'textcase'.dispatcher('\22', vim.g.TextCaseSubsArgs)
endif
endfunction
Expand Down