-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathinit.lua
More file actions
217 lines (183 loc) · 5.21 KB
/
Copy pathinit.lua
File metadata and controls
217 lines (183 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
-- lua/yapi_nvim/init.lua
--
-- yapi Neovim integration:
-- - :YapiWatch opens pretty TUI watch mode in a terminal
-- - :YapiRun runs a single request in a split
-- - LSP support for completions and diagnostics
local M = {}
local term_buf = nil
local term_win = nil
local function open_term_window()
-- Check if window still exists and is valid
if term_win and vim.api.nvim_win_is_valid(term_win) then
vim.api.nvim_set_current_win(term_win)
return term_win
end
-- Create a vertical split on the right
vim.cmd("rightbelow vsplit")
term_win = vim.api.nvim_get_current_win()
-- Window options
vim.wo[term_win].number = false
vim.wo[term_win].relativenumber = false
vim.wo[term_win].signcolumn = "no"
vim.wo[term_win].foldcolumn = "0"
return term_win
end
local function close_term()
if term_win and vim.api.nvim_win_is_valid(term_win) then
vim.api.nvim_win_close(term_win, true)
end
term_win = nil
term_buf = nil
end
local function start_watch(filepath)
if filepath == "" then
vim.notify("[yapi] Buffer has no file name", vim.log.levels.ERROR)
return
end
if not filepath:match("%.yapi$") and
not filepath:match("%.yapi%.yml$") and
not filepath:match("%.yapi%.yaml$") and
not filepath:match("yapi%.config%.yml$") and
not filepath:match("yapi%.config%.yaml$")
then
vim.notify("[yapi] Not a yapi config file", vim.log.levels.WARN)
return
end
-- Save if modified
if vim.bo.modified then
vim.cmd("write")
end
-- Close existing term if any
close_term()
-- Open window and launch terminal with yapi watch --pretty
local win = open_term_window()
term_buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_win_set_buf(win, term_buf)
-- Start terminal with yapi watch (pretty mode if configured)
local cmd = { "yapi", "watch", filepath }
if M._opts.pretty then
table.insert(cmd, "--pretty")
end
vim.fn.termopen(cmd, {
on_exit = function()
vim.schedule(function()
close_term()
end)
end,
})
-- Stay in normal mode, don't enter insert mode in terminal
vim.cmd("stopinsert")
end
local function run_once(filepath)
if filepath == "" then
vim.notify("[yapi] Buffer has no file name", vim.log.levels.ERROR)
return
end
if not filepath:match("%.yapi$") and
not filepath:match("%.yapi%.yml$") and
not filepath:match("%.yapi%.yaml$") and
not filepath:match("yapi%.config%.yml$") and
not filepath:match("yapi%.config%.yaml$")
then
vim.notify("[yapi] Not a yapi config file", vim.log.levels.WARN)
return
end
if vim.bo.modified then
vim.cmd("write")
end
-- Close existing term if any
close_term()
-- Open window and run yapi
local win = open_term_window()
term_buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_win_set_buf(win, term_buf)
vim.fn.termopen({ "yapi", "run", filepath }, {
on_exit = function()
-- Keep the buffer open to show results
end,
})
vim.cmd("stopinsert")
end
function M.watch()
start_watch(vim.api.nvim_buf_get_name(0))
end
function M.run()
run_once(vim.api.nvim_buf_get_name(0))
end
function M.stop()
close_term()
end
function M.toggle()
if term_win and vim.api.nvim_win_is_valid(term_win) then
close_term()
else
start_watch(vim.api.nvim_buf_get_name(0))
end
end
function M.setup(opts)
opts = opts or {}
M._opts = {
lsp = opts.lsp ~= false,
pretty = opts.pretty == true, -- default false
watch_on_save = opts.watch_on_save == true, -- default false
}
-- Commands
vim.api.nvim_create_user_command("YapiWatch", function()
M.watch()
end, { desc = "Start yapi watch mode in terminal" })
vim.api.nvim_create_user_command("YapiRun", function()
M.run()
end, { desc = "Run yapi once in terminal" })
vim.api.nvim_create_user_command("YapiStop", function()
M.stop()
end, { desc = "Close yapi terminal" })
-- Auto-start watch on save if configured
if M._opts.watch_on_save then
vim.api.nvim_create_autocmd("BufWritePost", {
pattern = { "*.yapi.yml", "*.yapi.yaml", "*.yapi", "yapi.config.yml", "yapi.config.yaml" },
callback = function()
M.watch()
end,
desc = "Start yapi watch on save",
})
end
-- Setup LSP for yapi files
if M._opts.lsp then
local ok, lspconfig = pcall(require, "lspconfig")
if ok then
local configs = require("lspconfig.configs")
if not configs.yapi then
configs.yapi = {
default_config = {
cmd = { "yapi", "lsp" },
filetypes = { "yaml.yapi" },
root_dir = lspconfig.util.root_pattern("yapi.config.yml", "yapi.config.yaml", ".git"),
},
}
end
lspconfig.yapi.setup({})
end
-- Set filetype to yaml.yapi for yapi config files
vim.api.nvim_create_autocmd({ "BufReadPost", "BufNewFile" }, {
pattern = {
"*.yapi.yml",
"*.yapi.yaml",
"*.yapi",
"yapi.config.yml",
"yapi.config.yaml"
},
callback = function()
vim.bo.filetype = "yaml.yapi"
end,
desc = "Set filetype for yapi config files",
})
end
-- Clean up on vim exit
vim.api.nvim_create_autocmd("VimLeavePre", {
callback = function()
close_term()
end,
})
end
return M