-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.lua
135 lines (109 loc) · 2.77 KB
/
init.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
133
134
135
-- ===== Open in Neovim =====
local obj = {}
obj.__index = obj
obj.name = "OpenInNeovim"
obj.version = "1.0"
obj.author = "June Kelly"
obj.license = "MIT - https://opensource.org/licenses/MIT"
local function log(logString)
print("[OpenInNeovim] " .. logString)
end
local function validateConfig(config)
if config.token == nil then
log("ERROR: config.token is required")
return false
end
if config.token:len() < 12 then
log("ERROR: config.token must be at least 12 characters")
return false
end
if config.nvimPath == nil then
log("ERROR: config.nvimPath is required")
return false
end
if config.nvimServerPipePath == nil then
log("ERROR: config.nvimServerPipePath is required")
return false
end
if config.translateRootPath ~= nil then
if config.translateRootPath.from == nil or config.translateRootPath.to == nil then
log("ERROR: config.translateRootPath requires both 'from' and 'to' fields in present")
return false
end
end
return true
end
local function buildFilePath(filePath, config)
if config.translateRootPath == nil then
return filePath
else
return filePath:gsub("^" .. config.translateRootPath.from, config.translateRootPath.to)
end
end
local function fileExists(filePath)
return hs.fs.attributes(filePath) ~= nil
end
function obj.bind(config)
log("Bind " .. hs.inspect.inspect(config))
if not validateConfig(config) then
return
end
local eventName = config.eventName or "openInNeovim"
log("Binding to URL '" .. eventName .. "'")
hs.urlevent.bind(eventName, function(_eventName, params)
local params_json = hs.json.encode(params)
if params.token ~= config.token then
log("Invalid Token! " .. params_json)
hs.notify
.new({
title = "Open in Neovim: Invalid Token!",
informativeText = params_json,
})
:send()
return
else
local filePath = buildFilePath(params.file, config)
local lineNumber = params.line
if tonumber(lineNumber) == nil then
log("ERROR: line number is not a valid number: '" .. lineNumber .. "'")
return
end
if config.skipValidateFileExists ~= true then
if fileExists(filePath) == false then
log("ERROR: file path does not exist '" .. filePath .. "'")
return
end
end
hs.task
.new(config.nvimPath, nil, {
"--server",
config.nvimServerPipePath,
"--remote-send",
table.concat({
"<ESC><ESC><ESC><ESC>",
":e",
" ",
"+",
lineNumber,
" ",
filePath,
"<CR>",
}),
})
:start()
hs.notify
.new({
title = "Opened in Neovim",
informativeText = filePath .. ":" .. lineNumber,
})
:send()
if config.foregroundApp then
local app = hs.application.find(config.foregroundApp)
if app then
app:setFrontmost(true)
end
end
end
end)
end
return obj