-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathmaven.lua
More file actions
176 lines (165 loc) · 5.04 KB
/
maven.lua
File metadata and controls
176 lines (165 loc) · 5.04 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
local utils = require("kide.core.utils")
local M = {}
local function maven_settings()
if vim.fn.filereadable(vim.fn.expand("~/.m2/settings.xml")) == 1 then
return vim.fn.expand("~/.m2/settings.xml")
end
local maven_home = vim.env["MAVEN_HOME"]
if maven_home and vim.fn.filereadable(maven_home .. "/conf/settings.xml") then
return maven_home .. "/conf/settings.xml"
end
end
M.get_maven_settings = function()
return vim.env["MAVEN_SETTINGS_XML"] or maven_settings()
end
local function settings_opt(settings)
if settings then
return " -s " .. settings
end
return ""
end
local function pom_file(file)
if file and vim.endswith(file, "pom.xml") then
return " -f " .. file
end
return ""
end
local exec = function(cmd, pom, opt)
opt = opt or {}
local Terminal = require("toggleterm.terminal").Terminal
-- require("toggleterm").exec(cmd .. settings_opt(M.get_maven_settings()) .. pom_file(pom))
local mvn = Terminal:new({
cmd = cmd .. settings_opt(M.get_maven_settings()) .. pom_file(pom),
close_on_exit = opt.close_on_exit,
auto_scroll = true,
on_exit = function(_)
if opt.update ~= nil and opt.update and opt.close_on_exit ~= nil and opt.close_on_exit then
vim.defer_fn(function()
local filetype = vim.api.nvim_buf_get_option(0, "filetype")
if filetype == "java" then
require("jdtls").update_project_config()
end
end, 500)
end
end,
})
mvn:toggle()
end
local function create_command(buf, name, cmd, complete, opt)
vim.api.nvim_buf_create_user_command(buf, name, function(opts)
if type(cmd) == "function" then
cmd = cmd(opts)
end
if cmd == nil then
return
end
if opts.args then
exec(cmd .. " " .. opts.args, vim.fn.expand("%"), opt)
else
exec(cmd, vim.fn.expand("%"), opt)
end
end, {
nargs = "*",
complete = complete,
})
end
local maven_args_complete = utils.command_args_complete
local function get_class_name()
if require("nvim-treesitter.query").has_query_files("java", "indents") then
local ts_utils = require("nvim-treesitter.ts_utils")
local node = ts_utils.get_node_at_cursor()
if node ~= nil and node:type() ~= "identifier" then
node = node:parent()
end
if node ~= nil and node:type() == "identifier" then
return vim.treesitter.get_node_text(node, 0)
end
end
end
M.maven_command = function(buf)
-- 判断为 java 文件
if vim.api.nvim_buf_get_option(buf, "filetype") == "java" then
create_command(buf, "MavenExecJava", function(_)
local filename = vim.fn.expand("%:p")
filename = string.gsub(filename, "^[%-/%w%s]*%/src%/main%/java%/", "")
filename = string.gsub(filename, "[/\\]", ".")
filename = string.gsub(filename, "%.java$", "")
return 'mvn exec:java -Dexec.mainClass="' .. filename .. '"'
end, nil, { update = true, close_on_exit = false })
end
create_command(
buf,
"MavenCompile",
"mvn clean compile",
maven_args_complete({ "test-compile" }, { model = "multiple" }),
{ update = true, close_on_exit = false }
)
create_command(
buf,
"MavenInstll",
"mvn clean install",
maven_args_complete({ "-DskipTests", "-Dmaven.test.skip=true" }, { model = "single" }),
{ update = true, close_on_exit = false }
)
create_command(
buf,
"MavenPackage",
"mvn clean package",
maven_args_complete({ "-DskipTests", "-Dmaven.test.skip=true" }, { model = "single" }),
{ update = true, close_on_exit = false }
)
create_command(
buf,
"MavenDependencyTree",
"mvn dependency:tree",
maven_args_complete({ "-Doutput=.dependency.txt" }, { model = "single" }),
{ close_on_exit = false }
)
create_command(buf, "MavenDependencyAnalyzeDuplicate", "mvn dependency:analyze-duplicate", nil, {
close_on_exit = false,
})
create_command(buf, "MavenDependencyAnalyzeOnly", "mvn dependency:analyze-only -Dverbose", nil, {
close_on_exit = false,
})
create_command(buf, "MavenDownloadSources", "mvn dependency:sources -DdownloadSources=true")
create_command(
buf,
"MavenTest",
"mvn test",
maven_args_complete({ "-Dtest=" }, { model = "single" }),
{ close_on_exit = false }
)
end
M.setup = function()
local group = vim.api.nvim_create_augroup("kide_jdtls_java_maven", { clear = true })
vim.api.nvim_create_autocmd({ "FileType" }, {
group = group,
pattern = { "xml", "java" },
desc = "maven_command",
callback = function(e)
if vim.endswith(e.file, "pom.xml") or vim.endswith(e.file, ".java") then
M.maven_command(e.buf)
end
end,
})
local opt = { update = true, close_on_exit = false }
vim.api.nvim_create_user_command("Maven", function(opts)
if opts.args then
exec("mvn " .. opts.args, vim.fn.expand("%"), opt)
else
exec("mvn", vim.fn.expand("%"), opt)
end
end, {
nargs = "*",
complete = maven_args_complete({
"clean",
"compile",
"test-compile",
"verify",
"package",
"install",
"deploy",
}, { model = "multiple" }),
})
end
return M