Compare commits
1 commit
0.x
...
better-del
| Author | SHA1 | Date | |
|---|---|---|---|
| ed7a97be82 |
2 changed files with 58 additions and 4 deletions
|
|
@ -1,6 +1,23 @@
|
||||||
-- The prefix that will be before the search text for the user
|
-- The prefix that will be before the search text for the user
|
||||||
local prompt_prefix = ">> "
|
local prompt_prefix = ">> "
|
||||||
|
|
||||||
|
-- Gets the suffix to delete from some text biased on what happens in a bash
|
||||||
|
-- prompt. If the text dose not end in a letter then the last word and all of
|
||||||
|
-- the tailing special characters will be returned. If the text dose end in a
|
||||||
|
-- letter then only the last word will be returned leaving the special
|
||||||
|
-- characters that are before the last word. For example
|
||||||
|
--
|
||||||
|
-- `some word` -> `some `
|
||||||
|
-- `some word` -> `some `
|
||||||
|
-- `some word ` -> `some `
|
||||||
|
local function get_delete_suffix(text)
|
||||||
|
if text:match "([A-Za-z]+)$" == nil then
|
||||||
|
return text:match "([A-Za-z]+[^A-Za-z]+)$"
|
||||||
|
end
|
||||||
|
|
||||||
|
return text:match "([A-Za-z]+)$"
|
||||||
|
end
|
||||||
|
|
||||||
local prompt = {}
|
local prompt = {}
|
||||||
|
|
||||||
prompt.suffix = ""
|
prompt.suffix = ""
|
||||||
|
|
@ -9,6 +26,7 @@ prompt.value = ""
|
||||||
prompt.text = function()
|
prompt.text = function()
|
||||||
return prompt.value .. prompt.suffix
|
return prompt.value .. prompt.suffix
|
||||||
end
|
end
|
||||||
|
|
||||||
prompt.update = function()
|
prompt.update = function()
|
||||||
vim.api.nvim_echo({
|
vim.api.nvim_echo({
|
||||||
{ prompt_prefix, "None" },
|
{ prompt_prefix, "None" },
|
||||||
|
|
@ -32,9 +50,12 @@ prompt.input = function(char)
|
||||||
prompt.suffix = prompt.suffix:sub(2, -1)
|
prompt.suffix = prompt.suffix:sub(2, -1)
|
||||||
end
|
end
|
||||||
elseif char == "DELETE_WORD" then
|
elseif char == "DELETE_WORD" then
|
||||||
prompt.value = prompt.value:match "(.*)%s+.*$"
|
local suffix = get_delete_suffix(prompt.value)
|
||||||
if prompt.value == nil then
|
|
||||||
|
if suffix == nil then
|
||||||
prompt.value = ""
|
prompt.value = ""
|
||||||
|
else
|
||||||
|
prompt.value = prompt.value:sub(1, #prompt.value - #suffix)
|
||||||
end
|
end
|
||||||
elseif char == "\\\\" then
|
elseif char == "\\\\" then
|
||||||
prompt.value = prompt.value .. "\\"
|
prompt.value = prompt.value .. "\\"
|
||||||
|
|
|
||||||
|
|
@ -51,11 +51,44 @@ end)
|
||||||
it("can delete a word", function(t)
|
it("can delete a word", function(t)
|
||||||
prompt.set "Ade Attwood"
|
prompt.set "Ade Attwood"
|
||||||
input { "DELETE_WORD" }
|
input { "DELETE_WORD" }
|
||||||
assert_prompt(t, "Ade")
|
assert_prompt(t, "Ade ")
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it("can delete a word in the middle", function(t)
|
it("can delete a word in the middle", function(t)
|
||||||
prompt.set "Ade middle A"
|
prompt.set "Ade middle A"
|
||||||
input { "LEFT", "LEFT", "DELETE_WORD" }
|
input { "LEFT", "LEFT", "DELETE_WORD" }
|
||||||
assert_prompt(t, "Ade A")
|
assert_prompt(t, "Ade A")
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it("will delete the space and the word if the last word is single space", function(t)
|
||||||
|
prompt.set "some.thing "
|
||||||
|
input { "DELETE_WORD" }
|
||||||
|
assert_prompt(t, "some.")
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("will only delete one word from path", function(t)
|
||||||
|
prompt.set "some/nested/path"
|
||||||
|
input { "DELETE_WORD" }
|
||||||
|
assert_prompt(t, "some/nested/")
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("will delete tailing space", function(t)
|
||||||
|
prompt.set "word "
|
||||||
|
input { "DELETE_WORD" }
|
||||||
|
assert_prompt(t, "")
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("will leave a random space", function(t)
|
||||||
|
prompt.set "some word "
|
||||||
|
input { "DELETE_WORD" }
|
||||||
|
assert_prompt(t, "some ")
|
||||||
|
end)
|
||||||
|
|
||||||
|
local special_characters = { ".", "/", "^" }
|
||||||
|
for _, char in ipairs(special_characters) do
|
||||||
|
it(string.format("will stop at a %s", char), function(t)
|
||||||
|
prompt.set(string.format("key%sValue", char))
|
||||||
|
input { "DELETE_WORD" }
|
||||||
|
assert_prompt(t, string.format("key%s", char))
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue