-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
480 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
local modifiers = {} | ||
|
||
function modifiers.linewise(next) | ||
return function(state, callback) | ||
local body = vim.fn.getreg(state.register) | ||
local type = vim.fn.getregtype(state.register) | ||
|
||
if state.vmode ~= "line" then | ||
-- we add a newline at the end nly if we don't replace to the end of a | ||
-- line and if we don't replace to line mode | ||
local should_wrap = type ~= "V" and state.marks.finish.col + 1 < vim.fn.getline(state.marks.finish.row):len() | ||
vim.fn.setreg(state.register, string.format("\n%s\n%s", body, should_wrap and "\n" or ""), type) | ||
end | ||
|
||
if nil == next then | ||
callback(state) | ||
else | ||
next(state, callback) | ||
end | ||
|
||
vim.fn.setreg(state.register, body, type) | ||
end | ||
end | ||
|
||
function modifiers.trim(next) | ||
return function(state, callback) | ||
local body = vim.fn.getreg(state.register) | ||
|
||
local reformated_body = body:gsub("^%s*", ""):gsub("%s*$", "") | ||
vim.fn.setreg(state.register, reformated_body, vim.fn.getregtype(state.register)) | ||
|
||
if nil == next then | ||
callback(state) | ||
else | ||
next(state, callback) | ||
end | ||
|
||
vim.fn.setreg(state.register, body, vim.fn.getregtype(state.register)) | ||
end | ||
end | ||
|
||
function modifiers.join(next) | ||
return function(state, callback) | ||
local body = vim.fn.getreg(state.register) | ||
|
||
local reformated_body = body:gsub("%s*\r?\n%s*", " ") | ||
vim.fn.setreg(state.register, reformated_body, vim.fn.getregtype(state.register)) | ||
|
||
if nil == next then | ||
callback(state) | ||
else | ||
next(state, callback) | ||
end | ||
|
||
vim.fn.setreg(state.register, body, vim.fn.getregtype(state.register)) | ||
end | ||
end | ||
|
||
function modifiers.reindent(next) | ||
return function(state, callback) | ||
if nil == next then | ||
callback(state) | ||
else | ||
next(state, callback) | ||
end | ||
|
||
local cursor_pos = vim.api.nvim_win_get_cursor(0) | ||
vim.cmd("silent '[,']normal! ==") | ||
vim.api.nvim_win_set_cursor(0, cursor_pos) | ||
end | ||
end | ||
|
||
function modifiers.build(chain) | ||
if nil == chain then | ||
return nil | ||
end | ||
|
||
local modifier = nil | ||
for index = #chain, 1, -1 do | ||
modifier = modifiers[chain[index]](modifier) | ||
end | ||
|
||
return modifier | ||
end | ||
|
||
return modifiers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
local substitute = require("substitute") | ||
|
||
local function execute_keys(feedkeys) | ||
local keys = vim.api.nvim_replace_termcodes(feedkeys, true, false, true) | ||
vim.api.nvim_feedkeys(keys, "x", false) | ||
end | ||
|
||
local function get_buf_lines() | ||
return vim.api.nvim_buf_get_lines(0, 0, -1, true) | ||
end | ||
|
||
local buf | ||
describe("Substitute modifiers", function() | ||
before_each(function() | ||
substitute.setup() | ||
|
||
buf = vim.api.nvim_create_buf(false, true) | ||
vim.api.nvim_command("buffer " .. buf) | ||
end) | ||
|
||
it("should be taken from a function", function() | ||
vim.api.nvim_buf_set_lines(buf, 0, -1, true, { "Lorem", "ipsum", "dolor", "sit", "amet" }) | ||
|
||
vim.keymap.set({ "n", "x" }, "]s", function() | ||
require("substitute").operator({ | ||
modifiers = function(_) | ||
return { "linewise" } | ||
end, | ||
}) | ||
end, { noremap = true }) | ||
|
||
execute_keys("lly2l") | ||
execute_keys("j") | ||
execute_keys("]s2l") | ||
|
||
assert.are.same({ "Lorem", "ip", "re", "m", "dolor", "sit", "amet" }, get_buf_lines()) | ||
end) | ||
|
||
it("could be conditionnal", function() | ||
vim.api.nvim_buf_set_lines(buf, 0, -1, true, { " Lorem ", "ipsum", "dolor", "sit", "amet" }) | ||
|
||
vim.keymap.set({ "n", "x" }, "]s", function() | ||
require("substitute").operator({ | ||
modifiers = function(state) | ||
return state.vmode == "char" and { "trim" } or { "linewise" } | ||
end, | ||
}) | ||
end, { noremap = true }) | ||
|
||
execute_keys("yy") | ||
execute_keys("jll") | ||
execute_keys("]s2l") | ||
|
||
execute_keys("jV") | ||
execute_keys("]s") | ||
|
||
assert.are.same({ " Lorem ", "ipLoremm", " Lorem ", "sit", "amet" }, get_buf_lines()) | ||
end) | ||
end) |
Oops, something went wrong.