-
Notifications
You must be signed in to change notification settings - Fork 0
/
kotlin.lua
132 lines (118 loc) · 3.11 KB
/
kotlin.lua
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
local ok, dap = pcall(require, "dap")
if not ok then
return
end
local util = require("lspconfig.util")
local root_files = {
-- Single-module projects
{
"build.xml", -- Ant
"pom.xml", -- Maven
"settings.gradle", -- Gradle
"settings.gradle.kts", -- Gradle
},
-- Multi-module projects
-- { "build.gradle", "build.gradle.kts" },
}
local contains = function(t, e)
if next(t) == nil then
return false
end
for _, v in ipairs(t) do
if v == e then
return true
end
end
return false
end
local function resolve_classname()
--[[ if (vim.bo.filetype ~= "kotlin") then
print("filetype")
print(vim.bo.filetype)
return
end ]]
local root_dir = util.root_pattern(root_files)(vim.fn.fnamemodify(vim.fn.expand("%"), ":p:h"))
if root_dir == nil then
return
end
local grep_res = vim.api.nvim_exec('! grep "fun main(arg[sv]: Array<String>)" -r ' .. root_dir, true)
if not grep_res or string.match(grep_res, "shell returned 1") then
vim.notify("Unable to find main func", vim.log.levels.WARN)
return
end
local files = {}
local mainfile, pkgname
for f in string.gmatch(grep_res, "([%w+%p]+[%w+.]kt)") do
if not contains(files, f) then
table.insert(files, f)
end
end
if #files > 1 then
-- for _, f in ipairs(files) do
-- if string.find(f, "Main") then
-- mainfile = f
-- vim.print(mainfile)
-- break
-- end
-- end
--
-- if not mainfile then
-- for _, f in ipairs(files) do
-- if string.find(f, "Application") then
-- mainfile = f
-- break
-- end
-- end
-- end
-- if not mainfile then
-- vim.notify("Multiple files contain 'fun main'", vim.log.levels.ERROR)
-- return
-- end
return
else
mainfile = files[1]
end
assert(mainfile, "Could not find a file matching \'fun main(args: Array<String>)\'")
for line in io.lines(mainfile) do
local match = line:match("package ([a-z\\.]+)")
if match then
pkgname = match
break
end
end
assert(pkgname, "Could not find package name for current class")
return pkgname .. "." .. vim.fn.fnamemodify(mainfile, ":t:r") .. "Kt"
end
-- resolve_classname()
dap.defaults.kotlin.auto_continue_if_many_stopped = false
dap.adapters.kotlin = {
type = "executable",
command = os.getenv("XDG_DATA_HOME")
.. "/kotlin-debug-adapter/adapter/build/install/adapter/bin/kotlin-debug-adapter",
options = {
initialize_timeout_sec = 60,
disconnect_timeout_sec = 60,
},
}
dap.configurations.kotlin = {
{
name = "kotlin: Launch",
type = "kotlin",
request = "launch",
projectRoot = util.root_pattern(root_files)(vim.fn.fnamemodify(vim.fn.expand("%"), ":p:h")),
mainClass = function() resolve_classname() end,
},
{
name = "kotlin: Attach",
type = "kotlin",
request = "attach",
hostName = "127.0.0.1",
port = 5005,
timeout = 20,
projectRoot = util.root_pattern(root_files)(vim.fn.fnamemodify(vim.fn.expand("%"), ":p:h")),
mainClass = function() resolve_classname() end,
}
}
-- debug
-- dap.configurations.kotlin = {
-- }