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

Unexpected result of getSelectText() #47

Merged
merged 38 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
e2ce146
fixed
AgatZan Oct 16, 2024
2c885de
fix: select text confirm
AgatZan Oct 20, 2024
0a39831
ci: introduce
AgatZan Oct 25, 2024
54a8a51
ci: introduce
AgatZan Oct 25, 2024
e860077
ci: introduce
AgatZan Oct 25, 2024
9397259
ci: introduce
AgatZan Oct 25, 2024
97208f3
ci: macos add
AgatZan Oct 25, 2024
bac2a4b
ci: introduce mini.test
AgatZan Oct 25, 2024
fb6fff6
ci: test fail
AgatZan Oct 25, 2024
ec91580
Merge pull request #51 from AgatZan/ci-test_mini.test
LintaoAmons Oct 28, 2024
7df3daa
fixed
AgatZan Oct 16, 2024
1867e9d
fix: select text confirm
AgatZan Oct 20, 2024
c382a79
test(utils): add test for `get_selected`
AgatZan Nov 3, 2024
f9b8651
test(utils): Add test to get_select
AgatZan Nov 7, 2024
5a340ef
Merge branch 'issue-wrong_select' of https://github.com/AgatZan/scrat…
AgatZan Nov 7, 2024
13a1fe0
remove useless test
AgatZan Nov 7, 2024
b0e7afb
fix(utils): Add backward compatibility
AgatZan Nov 7, 2024
330b19e
fix(utils): not found getregion
AgatZan Nov 7, 2024
94c1b39
fix(utils/get_selected): Fix indexes
AgatZan Nov 7, 2024
5c6022b
test(utils): Add test for command mode
AgatZan Nov 7, 2024
b729f0f
fix(utils): Remove getregion
AgatZan Nov 8, 2024
8eace59
fix(test): Selector work with raw termcode
AgatZan Nov 8, 2024
e4af357
test(utils): remove test for old realisation
AgatZan Nov 8, 2024
7be4324
fix(init): Make reaction to range
AgatZan Nov 9, 2024
a305d28
fix(init): Choose line at `:Scratch` correct
AgatZan Nov 9, 2024
c3d5628
test(utils): `get_select` to empty file
AgatZan Nov 9, 2024
993924e
test(utils/get_selected_text): coord an array
AgatZan Nov 9, 2024
a35bfef
test(utils/get_selected_text): correct left comparer
AgatZan Nov 9, 2024
b95f545
test(utils/get_selected_text): coord an array with empty string
AgatZan Nov 9, 2024
52dbf4c
test(utils/get_selected_text): empty file is array of 1 empty string
AgatZan Nov 9, 2024
2b3572e
test(utils/get_selected_text): select mark is `.`
AgatZan Nov 9, 2024
7344ebd
test(init): introduce test of `:Scratch`
AgatZan Nov 9, 2024
a172984
test(init): fullfile mock of scratch_api
AgatZan Nov 9, 2024
a7c9d1c
test(init): Fix set keymap
AgatZan Nov 9, 2024
d36cbee
fix(init): add NOTE to `:Scratch`
AgatZan Nov 9, 2024
ec95906
test(init): log `_G.scratch_opts`
AgatZan Nov 9, 2024
012fef0
revert(test/log)
AgatZan Nov 9, 2024
71c2aca
ci(test): Add timeout
AgatZan Nov 9, 2024
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
27 changes: 27 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: default

on: [push, pull_request]

jobs:
run_tests:
name: unit tests
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
neovim_version: ['v0.8.3', 'v0.9.5', 'v0.10.2', 'nightly']
include:
- os: macos-latest
neovim_version: v0.10.1
- os: windows-latest
neovim_version: v0.10.1
steps:
- uses: actions/checkout@v4
- name: Setup neovim
uses: rhysd/action-setup-vim@v1
with:
neovim: true
version: ${{ matrix.neovim_version }}
- name: Run tests
run: make test
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
test: deps/mini.nvim
nvim --headless --noplugin -u ./scripts/minimal_init.lua -c "lua MiniTest.run()"

# Run test from file at `$FILE` environment variable
test_file: deps/mini.nvim
nvim --headless --noplugin -u ./scripts/minimal_init.lua -c "lua MiniTest.run_file('$(FILE)')"

# Download 'mini.nvim' to use its 'mini.test' testing module
deps/mini.nvim:
@mkdir -p deps
git clone --filter=blob:none https://github.com/echasnovski/mini.nvim $@
32 changes: 22 additions & 10 deletions lua/scratch/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,29 @@ local table_length = function(T)
end

---@return string[]
local function getSelectedText()
local _, csrow, cscol, _ = unpack(vim.fn.getpos("'<"))
local _, cerow, cecol, _ = unpack(vim.fn.getpos("'>"))

local lines = vim.fn.getline(csrow, cerow)
local n = table_length(lines)
if n <= 0 then
return {}
local function getSelectedText(mark, selection_mode)
local pos1 = vim.fn.getpos("v")
local pos2 = vim.fn.getpos(mark)
local lines = {}
local start_row, start_col, end_row, end_col = pos1[2], pos1[3], pos2[2], pos2[3]
local text = vim.api.nvim_buf_get_lines(0, start_row - 1, end_row, true)
end_row = end_row - start_row + 1
start_row = 1
if selection_mode == "v" then
table.insert(lines, text[1]:sub(start_col))
for i = start_row + 1, end_row do
table.insert(lines, text[i])
end
lines[end_row] = lines[end_row]:sub(1, end_col)
elseif selection_mode == "V" then
for i = start_row, end_row do
table.insert(lines, text[i])
end
elseif selection_mode == vim.api.nvim_replace_termcodes("<C-V>", true, true, true) then
for i = start_row, end_row do
table.insert(lines, text[i]:sub(start_col, end_col))
end
end
lines[n] = string.sub(lines[n], 1, cecol)
lines[1] = string.sub(lines[1], cscol)
return lines
end

Expand Down
11 changes: 7 additions & 4 deletions plugin/scratch_plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ local scratch_main = require("scratch")
scratch_main.setup()

vim.api.nvim_create_user_command("Scratch", function(args)
if args.range > 0 then
scratch_api.scratch({ content = utils.getSelectedText() })
else
scratch_api.scratch()
local mode = vim.api.nvim_get_mode().mode
local opts
if mode ~= "n" then
opts = { content = utils.getSelectedText(".", mode) }
elseif args.range > 0 then
opts = { content = utils.getSelectedText("'>", "v") }
end
scratch_api.scratch(opts)
end, { range = true })

vim.api.nvim_create_user_command("ScratchOpen", scratch_api.openScratch, {})
Expand Down
12 changes: 12 additions & 0 deletions scripts/minimal_init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- Add current directory to 'runtimepath' to be able to use 'lua' files
vim.cmd([[let &rtp.=','.getcwd()]])

-- Set up 'mini.test' only when calling headless Neovim (like with `make test`)
if #vim.api.nvim_list_uis() == 0 then
-- Add 'mini.nvim' to 'runtimepath' to be able to use 'mini.test'
-- Assumed that 'mini.nvim' is stored in 'deps/mini.nvim'
vim.cmd("set rtp+=deps/mini.nvim")

-- Set up 'mini.test'
require("mini.test").setup()
end
144 changes: 144 additions & 0 deletions tests/test_select.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
local child = MiniTest.new_child_neovim()
local getSelectedText
local function table_select(text, coord, selection_mode)
local lines = {}
local start_row, start_col, end_row, end_col = coord[1], coord[2], coord[3], coord[4]
if selection_mode == "v" then
table.insert(lines, text[start_row]:sub(start_col))
for i = start_row + 1, end_row do
table.insert(lines, text[i])
end
local ind = end_row - start_row + 1
lines[ind] = lines[ind]:sub(1, end_col)
elseif selection_mode == "V" then
for i = start_row, end_row do
table.insert(lines, text[i])
end
elseif selection_mode == vim.api.nvim_replace_termcodes("<C-V>", true, true, true) then
for i = start_row, end_row do
table.insert(lines, text[i]:sub(start_col, end_col))
end
end
return lines
end
local select_wise = function(coord, selection_mode)
local vim = child
local start_row, start_col, end_row, end_col = coord[1], coord[2], coord[3], coord[4]
local mode = vim.api.nvim_get_mode()
if mode.mode ~= "n" then
local esc = vim.api.nvim_replace_termcodes("<ESC>", true, true, true)
vim.api.nvim_cmd({ cmd = "normal", bang = true, args = { esc } }, {})
vim.api.nvim_cmd({ cmd = "normal", bang = true, args = { esc } }, {})
end
selection_mode = vim.api.nvim_replace_termcodes(selection_mode, true, true, true)
vim.api.nvim_cmd({ cmd = "normal", bang = true, args = { selection_mode } }, {})

vim.api.nvim_win_set_cursor(0, { start_row, start_col - 1 })
vim.cmd("normal! o")
vim.api.nvim_win_set_cursor(0, { end_row, end_col - 1 })
end

-- local function old_real(mark, mode)
-- local _, csrow, cscol, _ = unpack(vim.fn.getpos("'<"))
-- local _, cerow, cecol, _ = unpack(vim.fn.getpos("'>"))
--
-- local lines = vim.fn.getline(csrow, cerow)
-- local n = #lines
-- if n <= 0 then
-- return {}
-- end
-- lines[n] = string.sub(lines[n], 1, cecol)
-- lines[1] = string.sub(lines[1], cscol)
-- return lines
-- end

local new_set = MiniTest.new_set
local T = new_set({
parametrize = { { "v" }, { "V" }, { vim.api.nvim_replace_termcodes("<C-V>", true, true, true) } },
})
T["param"] = new_set({
parametrize = {
{
{ 1, 1, 1, 4 },
},
{
{ 1, 2, 1, 4 },
},
{
{ 2, 1, 3, 4 },
},
{
{ 1, 1, 2, 2 },
},
{
{ 1, 1, 1, 1 },
},
{
{ 1, 1, 4, 4 },
},
},
})
local BUFFER_TEXT = {
"some",
"text here will be",
"inserted",
"now",
}
-- T["param"]["old"] = new_set({
-- hooks = {
-- pre_case = function()
-- child.restart({ "-u", "scripts/minimal_init.lua" })
-- child.api.nvim_buf_set_lines(0, 0, -1, false, BUFFER_TEXT)
-- end,
-- post_case = function()
-- child.stop()
-- end,
-- },
-- })
T["param"]["new"] = new_set({
hooks = {
pre_case = function()
child.restart({ "-u", "scripts/minimal_init.lua" })
child.api.nvim_buf_set_lines(0, 0, -1, false, BUFFER_TEXT)
getSelectedText = require("scratch.utils").getSelectedText
end,
post_case = function()
child.stop()
end,
},
n_retry = 2,
})
-- T["param"]["old"]["not_workd"] = function(selection_mode, coord)
-- select_wise(coord, selection_mode)
-- MiniTest.expect.no_equality(
-- table_select(BUFFER_TEXT, coord, selection_mode),
-- child.lua_func(old_real)
-- )
-- end
T["param"]["new"]["workd"] = function(selection_mode, coord)
select_wise(coord, selection_mode)
MiniTest.expect.equality(
table_select(BUFFER_TEXT, coord, selection_mode),
child.lua_func(getSelectedText, ".", selection_mode)
)
end
-- T["param"]["old"]["at_command"] = function(selection_mode, coord)
Copy link
Contributor Author

@AgatZan AgatZan Nov 8, 2024

Choose a reason for hiding this comment

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

Sometimes, that test suits works, sometimes it doesn't, but I have refactored the function for that use case, so this doesn't matter.

-- select_wise(coord, selection_mode)
-- child.type_keys(":")
-- MiniTest.expect.equality(
-- table_select(BUFFER_TEXT, coord, selection_mode),
-- child.lua_func(old_real)
-- )
-- end
T["param"]["new"]["at_command"] = function(selection_mode, coord)
select_wise(coord, selection_mode)
child.type_keys(":")
MiniTest.add_note("|1>--- " .. vim.inspect(child.fn.getpos("'>")))
MiniTest.add_note("|2>--- " .. vim.inspect(child.fn.getpos(".")))
MiniTest.expect.equality(
table_select(BUFFER_TEXT, coord, selection_mode),
child.lua_func(getSelectedText, "'>", selection_mode)
)
end

return T