Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
gbprod committed Nov 10, 2023
1 parent 7dd97ed commit 22fa938
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 52 deletions.
4 changes: 2 additions & 2 deletions lua/substitute.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ end

function substitute.operator_callback(vmode)
local marks = utils.get_marks(0, vmode)

-- print(vim.inspect(marks))
substitute.state.vmode = vmode
substitute.state.marks = marks

local substitued_text = utils.text(0, marks.start, marks.finish, vmode)

Expand Down
2 changes: 1 addition & 1 deletion lua/substitute/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ function utils.text(bufnr, start, finish, vmode)
local current_row_len = vim.fn.getline(row):len()

local end_col = current_row_len > finish.col and utils.get_next_char_bytecol(finish.row, finish.col)
or current_row_len
or current_row_len
if start.col > end_col then
end_col = start.col
end
Expand Down
50 changes: 8 additions & 42 deletions lua/substitute/wrappers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,13 @@ function wrappers.linewise(next)
local body = vim.fn.getreg(state.register)
local type = vim.fn.getregtype(state.register)

vim.fn.setreg(state.register, body, "l")

if nil == next then
callback(state)
else
next(state, callback)
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

vim.fn.setreg(state.register, body, type)
end
end

function wrappers.charwise(next)
return function(state, callback)
local body = vim.fn.getreg(state.register)
local type = vim.fn.getregtype(state.register)

local reformated_body = body:gsub("\n$", "")
vim.fn.setreg(state.register, reformated_body, "c")

if nil == next then
callback(state)
else
next(state, callback)
end

vim.fn.setreg(state.register, body, type)
end
end

function wrappers.blockwise(next)
return function(state, callback)
local body = vim.fn.getreg(state.register)
local type = vim.fn.getregtype(state.register)

vim.fn.setreg(state.register, body, "b")

if nil == next then
callback(state)
else
Expand Down Expand Up @@ -86,7 +56,7 @@ function wrappers.join(next)
end
end

function wrappers.change(change, next)
function wrappers.reindent(next)
return function(state, callback)
if nil == next then
callback(state)
Expand All @@ -95,7 +65,7 @@ function wrappers.change(change, next)
end

local cursor_pos = vim.api.nvim_win_get_cursor(0)
vim.cmd(string.format("silent '[,']normal! %s", change))
vim.cmd("silent '[,']normal! ==")
vim.api.nvim_win_set_cursor(0, cursor_pos)
end
end
Expand All @@ -104,11 +74,7 @@ function wrappers.build(chain)
local wrapper = nil

for index = #chain, 1, -1 do
if vim.tbl_contains({ "==", ">>", "<<" }, chain[index]) then
wrapper = wrappers.change(chain[index], wrapper)
else
wrapper = wrappers[chain[index]](wrapper)
end
wrapper = wrappers[chain[index]](wrapper)
end

return wrapper
Expand Down
14 changes: 7 additions & 7 deletions spec/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ function M.setup()

M.load("nvim-lua/plenary.nvim")

vim.keymap.set("n", "ss", "<cmd>lua require('substitute').line()<cr>", { noremap = true })
vim.keymap.set("n", "S", "<cmd>lua require('substitute').eol()<cr>", { noremap = true })
vim.keymap.set("n", "s", "<cmd>lua require('substitute').operator()<cr>", { noremap = true })
vim.keymap.set("x", "s", "<cmd>lua require('substitute').visual()<cr>", { noremap = true })
vim.keymap.set("n", "ss", require("substitute").line, { noremap = true })
vim.keymap.set("n", "S", require("substitute").eol, { noremap = true })
vim.keymap.set("n", "s", require("substitute").operator, { noremap = true })
vim.keymap.set("x", "s", require("substitute").visual, { noremap = true })

vim.keymap.set("n", "<leader>s", "<cmd>lua require('substitute.range').operator()<cr>", { noremap = true })
vim.keymap.set("n", "<leader>s", require("substitute.range").operator, { noremap = true })

vim.keymap.set("n", "sx", "<cmd>lua require('substitute.exchange').operator()<cr>", { noremap = true })
vim.keymap.set("x", "X", "<cmd>lua require('substitute.exchange').visual()<cr>", { noremap = true })
vim.keymap.set("n", "sx", require("substitute.exchange").operator, { noremap = true })
vim.keymap.set("x", "X", require("substitute.exchange").visual, { noremap = true })
end

M.setup()
75 changes: 75 additions & 0 deletions spec/substitute/wrappers/linewise_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
local substitute = require("substitute")

local function get_buf_lines()
local result = vim.api.nvim_buf_get_lines(0, 0, vim.api.nvim_buf_line_count(0), false)
return result
end

local function execute_keys(feedkeys)
local keys = vim.api.nvim_replace_termcodes(feedkeys, true, false, true)
vim.api.nvim_feedkeys(keys, "x", false)
end

describe("Substitute linewise", function()
before_each(function()
substitute.setup()

local buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_command("buffer " .. buf)

vim.keymap.set({ "n", "x" }, "]s", function()
require("substitute").operator({
wrappers = require("substitute.wrappers").build({ "linewise" }),
})
end, { noremap = true })

vim.keymap.set({ "n", "x" }, "]ss", function()
require("substitute").line({
wrappers = require("substitute.wrappers").build({ "linewise" }),
})
end, { noremap = true })

vim.api.nvim_buf_set_lines(0, 0, -1, true, { "Lorem", "ipsum", "dolor", "sit", "amet" })
end)

it("should substitute linewise", function()
execute_keys("y3l")
execute_keys("jll")
execute_keys("]s2l")

assert.are.same({ "Lorem", "ip", "Lor", "m", "dolor", "sit", "amet" }, get_buf_lines())
end)

it("should substitute linewise from linewise", function()
execute_keys("yy")
execute_keys("jll")
execute_keys("]s2l")

assert.are.same({ "Lorem", "ip", "Lorem", "m", "dolor", "sit", "amet" }, get_buf_lines())
end)

it("should substitute linewise in visual mode", function()
execute_keys("y3l")
execute_keys("jll")
execute_keys("vl")
execute_keys("]s")

assert.are.same({ "Lorem", "ip", "Lor", "m", "dolor", "sit", "amet" }, get_buf_lines())
end)

it("should substitute linewise in line mode", function()
execute_keys("y3l")
execute_keys("jll")
execute_keys("]ss")

assert.are.same({ "Lorem", "Lor", "dolor", "sit", "amet" }, get_buf_lines())
end)

it("should substitute linewise at the end of line", function()
execute_keys("y3l")
execute_keys("jll")
execute_keys("]s$")

assert.are.same({ "Lorem", "ip", "Lor", "dolor", "sit", "amet" }, get_buf_lines())
end)
end)
148 changes: 148 additions & 0 deletions spec/substitute/wrappers/reindent_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
local substitute = require("substitute")

local function get_buf_lines()
local result = vim.api.nvim_buf_get_lines(0, 0, vim.api.nvim_buf_line_count(0), false)
return result
end

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 buf = nil
describe("Substitute reindent", function()
before_each(function()
substitute.setup()

buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_command("buffer " .. buf)

vim.keymap.set({ "n", "x" }, "=s", function()
require("substitute").operator({
wrappers = require("substitute.wrappers").build({ "reindent" }),
})
end, { noremap = true, buffer = buf })

vim.keymap.set({ "n", "x" }, "=ss", function()
require("substitute").line({
wrappers = require("substitute.wrappers").build({ "reindent" }),
})
end, { noremap = true, buffer = buf })

vim.api.nvim_set_option_value("shiftwidth", 4, { buf = buf })
end)

it("should substitute and reindent line", function()
vim.api.nvim_buf_set_lines(buf, 0, -1, true, { " Lorem", "ipsum", " dolor", " sit", " amet" })

execute_keys("wy3l")
execute_keys("j0ll")
execute_keys("=s2l")

assert.are.same({ " Lorem", " ipLorm", " dolor", " sit", " amet" }, get_buf_lines())
end)

it("should substitute and reindent multiple lines", function()
vim.api.nvim_buf_set_lines(buf, 0, -1, true, { " Lorem", "ipsum", " dolor", " sit", " amet" })

execute_keys("wy3l")
execute_keys("j0ll")
execute_keys("=s2w")

assert.are.same({ " Lorem", " ipLor", " sit", " amet" }, get_buf_lines())
end)

it("should substitute line and reindent line", function()
vim.api.nvim_buf_set_lines(buf, 0, -1, true, { " Lorem", "ipsum", " dolor", " sit", " amet" })

execute_keys("wy3l")
execute_keys("j0ll")
execute_keys("=ss")

assert.are.same({ " Lorem", " Lor", " dolor", " sit", " amet" }, get_buf_lines())
end)

it("should substitute lines and reindent lines", function()
vim.api.nvim_buf_set_lines(buf, 0, -1, true, { " Lorem", "ipsum", " dolor", " sit", " amet" })

execute_keys("wy3l")
execute_keys("j0ll")
execute_keys("2=ss")

assert.are.same({ " Lorem", " Lor", " sit", " amet" }, get_buf_lines())
end)
end)

describe("Substitute linewise and reindent", function()
before_each(function()
substitute.setup()

buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_command("buffer " .. buf)

vim.keymap.set({ "n", "x" }, "=s", function()
require("substitute").operator({
wrappers = require("substitute.wrappers").build({ "linewise", "reindent" }),
})
end, { noremap = true, buffer = buf })

vim.keymap.set({ "n", "x" }, "=ss", function()
require("substitute").line({
wrappers = require("substitute.wrappers").build({ "linewise", "reindent" }),
})
end, { noremap = true, buffer = buf })

vim.api.nvim_set_option_value("shiftwidth", 4, { buf = buf })
end)

it("should substitute and reindent line", function()
vim.api.nvim_buf_set_lines(buf, 0, -1, true, { " Lorem", "ipsum", " dolor", " sit", " amet" })

execute_keys("wy3l")
execute_keys("j0ll")
execute_keys("=s2l")

assert.are.same({ " Lorem", " ip", " Lor", " m", " dolor", " sit", " amet" }, get_buf_lines())
end)

it("should substitute and reindent multiple lines", function()
vim.api.nvim_buf_set_lines(buf, 0, -1, true, { " Lorem", "ipsum", " dolor", " sit", " amet" })

execute_keys("wy3l")
execute_keys("j0ll")
execute_keys("=s2w")

assert.are.same({ " Lorem", " ip", " Lor", " sit", " amet" }, get_buf_lines())
end)

it("should substitute line and reindent line", function()
vim.api.nvim_buf_set_lines(buf, 0, -1, true, { " Lorem", "ipsum", " dolor", " sit", " amet" })

execute_keys("wy3l")
execute_keys("j")
execute_keys("=ss")

assert.are.same({ " Lorem", " Lor", " dolor", " sit", " amet" }, get_buf_lines())
end)

it("should substitute lines and reindent lines", function()
vim.api.nvim_buf_set_lines(buf, 0, -1, true, { " Lorem", "ipsum", " dolor", " sit", " amet" })

execute_keys("wy3l")
execute_keys("j0ll")
execute_keys("2=ss")

assert.are.same({ " Lorem", " Lor", " sit", " amet" }, get_buf_lines())
end)

it("should reindent until eol", function()
vim.api.nvim_buf_set_lines(buf, 0, -1, true, { " Lorem", "ipsum", " dolor", " sit", " amet" })

execute_keys("wy3l")
execute_keys("j0ll")
execute_keys("=s$")

assert.are.same({ " Lorem", " ip", " Lor", " dolor", " sit", " amet" }, get_buf_lines())
end)
end)

0 comments on commit 22fa938

Please sign in to comment.