Skip to content

Commit c45dae5

Browse files
committed
init
0 parents  commit c45dae5

12 files changed

+826
-0
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 windwp
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
test:
2+
nvim --headless --noplugin -u tests/minimal.vim -c "PlenaryBustedDirectory tests/ {minimal_init = 'tests/minimal.vim'}"

README.md

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# nvim-ts-autotag
2+
3+
Use treesitter to **autoclose** and **autorename** html tag
4+
5+
It work with html,tsx,vue,svelte.
6+
7+
## Usage
8+
9+
``` text
10+
Before Input After
11+
------------------------------------
12+
<div > <div></div>
13+
<div></div> ciwspan <span></span>
14+
------------------------------------
15+
```
16+
17+
18+
## Setup
19+
Neovim 0.5 with and nvim-treesitter to work
20+
21+
User treesitter setup
22+
```lua
23+
require'nvim-treesitter.configs'.setup {
24+
autotag = {
25+
enable = true,
26+
}
27+
}
28+
29+
```
30+
or you can use a set up function
31+
32+
``` lua
33+
require('nvim-ts-autotag').setup()
34+
35+
```
36+
37+
## Default values
38+
39+
``` lua
40+
local filetypes = {
41+
'html', 'javascript', 'javascriptreact', 'typescriptreact', 'svelte', 'vue'
42+
}
43+
local skip_tags = {
44+
'area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'slot',
45+
'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr','menuitem'
46+
}
47+
48+
```
49+
50+
### Override default values
51+
52+
``` lua
53+
54+
require'nvim-treesitter.configs'.setup {
55+
autotag = {
56+
enable = true,
57+
filetypes = { "html" , "xml" },
58+
}
59+
}
60+
-- OR
61+
require('nvim-ts-autotag').setup({
62+
filetypes = { "html" , "xml" },
63+
})
64+
65+
```

lua/spectre/actions.lua

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
local Job = require("plenary.job")
2+
local api = vim.api
3+
local utils = import('spectre.utils')
4+
local _config = import('spectre.config')
5+
local config, state=_config.config, _config.state
6+
local M = {}
7+
8+
local open_file = function(filename, lnum, col, winid)
9+
if winid ~= nil then
10+
vim.fn.win_gotoid(winid)
11+
end
12+
vim.api.nvim_command[[execute "normal! m` "]]
13+
vim.cmd("e " .. filename)
14+
api.nvim_win_set_cursor(0,{lnum, col})
15+
end
16+
17+
M.goto_file = function()
18+
local t = M.get_current_entries()
19+
if t == nil then return nil end
20+
if config.is_open_target_win and state.target_winid ~= nil then
21+
open_file(t.filename, t.lnum, t.col, state.target_winid)
22+
else
23+
open_file(t.filename, t.lnum, t.col)
24+
end
25+
end
26+
27+
M.get_current_entries = function ()
28+
local lnum = vim.fn.getpos('.')[2]
29+
local line = ""
30+
local check = false
31+
local start = lnum
32+
repeat
33+
line = vim.fn.getline(start)
34+
check = string.find(line, "^[^%s]*%:%d*:%d*:")
35+
if check then
36+
local t = utils.parse_line_grep(line)
37+
if t ~= nil and t.filename ~= nil then
38+
return t
39+
end
40+
end
41+
start = start -1
42+
until check or lnum - start > 3
43+
end
44+
45+
M.get_all_entries = function()
46+
local lines = api.nvim_buf_get_lines(state.bufnr, config.line_result -1, -1, false)
47+
local entries = {}
48+
for index, line in pairs(lines) do
49+
local grep = utils.parse_line_grep(line)
50+
if grep ~= nil and line:match("^%w") ~= nil then
51+
grep.lnum_result = config.line_result + index -2
52+
table.insert(entries, grep)
53+
end
54+
end
55+
return entries
56+
end
57+
58+
M.send_to_qf = function ()
59+
local entries = M.get_all_entries()
60+
vim.cmd[[copen]]
61+
vim.fn.setqflist(entries,"r")
62+
return entries
63+
end
64+
65+
M.replace_cmd = function()
66+
M.send_to_qf()
67+
local keyquery=''
68+
if #state.query.search_query > 2 then
69+
if state.query.is_file == true then
70+
vim.fn.win_gotoid(state.target_winid)
71+
keyquery = string.format(
72+
':%%s/\\v%s/%s/g',
73+
state.query.search_query,
74+
state.query.replace_query
75+
)
76+
else
77+
keyquery = string.format(
78+
':%s %%s/\\v%s/%s/g | update',
79+
config.replace_cmd,
80+
state.query.search_query,
81+
state.query.replace_query
82+
)
83+
end
84+
end
85+
if keyquery then
86+
vim.api.nvim_feedkeys( keyquery, 'n', true)
87+
end
88+
end
89+
90+
M.replace_tool = function()
91+
local entries = M.get_all_entries()
92+
for _, value in pairs(entries) do
93+
local t_sed = string.format(
94+
"%s,%ss/%s/%s/g",
95+
value.lnum,
96+
value.lnum,
97+
utils.escape_chars(state.query.search_query),
98+
utils.escape_chars(state.query.replace_query)
99+
)
100+
101+
local args={
102+
'-i',
103+
'-E',
104+
t_sed,
105+
value.filename,
106+
}
107+
108+
local job = Job:new({
109+
command = "sed",
110+
args = args,
111+
on_stderr = function(error,status)
112+
pcall(vim.schedule_wrap( function()
113+
value.text = " ERROR"
114+
vim.fn.setqflist(entries, 'r')
115+
end))
116+
end,
117+
on_exit = function(_,status)
118+
if status == 0 then
119+
pcall(vim.schedule_wrap( function()
120+
value.text = " DONE"
121+
vim.fn.setqflist(entries, 'r')
122+
api.nvim_buf_set_extmark(M.bufnr, config.namespace, value.lnum_result, 0, { virt_text = {{" DONE", "String"}}, virt_text_pos = 'eol'})
123+
end))
124+
end
125+
end,
126+
})
127+
job:sync()
128+
end
129+
end
130+
131+
132+
M.undo=function()
133+
134+
end
135+
136+
M.undo_all=function()
137+
138+
end
139+
return M

lua/spectre/config.lua

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
local api = vim.api
2+
3+
local config = {
4+
filetype = "search_panel",
5+
namespace = api.nvim_create_namespace("SEARCH_PANEL"),
6+
namespace_status = api.nvim_create_namespace("SEARCH_PANEL_STATUS"),
7+
namespace_result = api.nvim_create_namespace("SEARCH_PANEL_RESULT"),
8+
line_sep = '--------------------------------------',
9+
highlight={
10+
ui = "String",
11+
search = "DiffChange",
12+
replace = "DiffAdd",
13+
},
14+
mapping = {
15+
['delete_line']={
16+
map = "dd",
17+
cmd = "<cmd>lua import('spectre').delete()<CR>",
18+
desc="delete current item"
19+
},
20+
['enter_file'] = {
21+
map = "<cr>",
22+
cmd = "<cmd>lua import('spectre.actions').goto_file()<CR>",
23+
desc = "goto current file"
24+
},
25+
['send_to_qf']={
26+
map="rq" ,
27+
cmd="<cmd>lua import('spectre.actions').send_to_qf()<CR>",
28+
desc = "send all item to quickfix"
29+
},
30+
['replace_cmd']={
31+
map = "rc",
32+
cmd = "<cmd>lua import('spectre.actions').replace_cmd()<CR>",
33+
desc = "input replace command vim"
34+
},
35+
['replace_tool'] = {
36+
map = "rs",
37+
cmd = "<cmd>lua import('spectre.actions').replace_tool()<CR>",
38+
desc = "replace all"
39+
},
40+
-- ["undo_file"] = {
41+
-- map = "ru",
42+
-- cmd = "<cmd>lua import('spectre.actions').undo_file()<CR>",
43+
-- desc="Goto file"
44+
-- },
45+
46+
-- ["undo_all"] = {
47+
-- map = "rU",
48+
-- cmd = "<cmd>lua import('spectre.actions').undo_all()<CR>",
49+
-- desc="Goto file"
50+
-- },
51+
},
52+
lnum_UI = 8, -- total line for ui you can edit it
53+
line_result = 10, -- line begin result
54+
replace_cmd = "cfdo",
55+
is_open_target_win = true
56+
}
57+
58+
local state = {
59+
query = { },
60+
vt = { }
61+
}
62+
_G.__search_state = _G.__search_state or state
63+
state = _G.__search_state
64+
65+
return {
66+
state = state,
67+
config = config
68+
}

0 commit comments

Comments
 (0)