-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearch.lua
278 lines (224 loc) · 9.47 KB
/
Search.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
local micro = import("micro")
local util = import("micro/util")
local filepath = import("path/filepath")
local shell = import("micro/shell")
local config = import("micro/config")
local buffer = import("micro/buffer")
local os = import("os")
local runtime = import("runtime")
local fmt = import('fmt')
package.path = fmt.Sprintf('%s;%s/plug/MicroOmni/?.lua', package.path, config.ConfigDir)
local Common = require("Common")
local OmniContentFindPath = ""
local OmniSearchText = ""
local Self = {}
-- NOTE: lineNum is string
local function fzfParseOutput(output, bp, lineNum, gotoLineIfExists)
micro.Log("fzfParseOutput called")
if output ~= "" then
local file = string.gsub(output, "[\n\r]", "")
if file == nil then
return
end
Common.HandleOpenFile(file, bp, lineNum, gotoLineIfExists)
end
end
local function getOS()
if runtime.GOOS == "windows" then
return "Windows"
else
return "Unix"
end
end
local function FindContent(str, searchLoc)
micro.Log("Find Content called")
local bp = micro.CurPane()
local selectedText = str
local fzfArgs
-- micro.Log("selectedText before: ", selectedText)
-- micro.Log("Common.OmniContentArgs before: ", common.OmniContentArgs)
local firstWord, _ = selectedText:match("^(.[^%s]*)%s-(.*)$")
if firstWord == nil or firstWord == "" then
micro.InfoBar():Error("Failed to extract first word... str: ", str)
return
end
local currentOS = getOS()
local finalCmd;
if currentOS == "Unix" then
selectedText = selectedText:gsub("'", "'\\''")
firstWord = firstWord:gsub("'", "'\\''")
fzfArgs = Common.OmniContentArgs:gsub("'", "'\\''")
finalCmd = "rg --glob=!.git/ -F -i -uu -n '\\''"..firstWord.."'\\'' | "..Common.OmniFzfCmd.." "..fzfArgs..
" -q '\\''"..selectedText.."'\\''"
else
selectedText = selectedText:gsub("'", '"')
firstWord = firstWord:gsub("'", '""')
fzfArgs = Common.OmniContentArgs:gsub("'", '"')
finalCmd = "rg --glob=!.git/ -F -i -uu -n \""..firstWord.."\" | "..Common.OmniFzfCmd.." "..fzfArgs..
" -q \""..selectedText.."\""
end
if currentOS == "Unix" then
finalCmd = "sh -c \'"..finalCmd.."\'"
else
finalCmd = "cmd /s /v /c "..finalCmd..""
end
micro.Log("Running search cmd: ", finalCmd)
local currentLoc = os.Getwd()
if searchLoc ~= nil and searchLoc ~= "" then
if not Common.path_exists(searchLoc) then
micro.InfoBar():Error("", searchLoc, " doesn't exist")
return
end
if not Common.IsPathDir(searchLoc) then
micro.InfoBar():Error("", searchLoc, " is not a directory")
return
end
bp:CdCmd({searchLoc})
end
local output, err = shell.RunInteractiveShell(finalCmd, false, true)
if searchLoc ~= nil and searchLoc ~= "" then
bp:CdCmd({currentLoc})
end
if err ~= nil then
-- micro.InfoBar():Error("Error is: ", err:Error())
else
local _, outputLinesCount = output:gsub('\n', '\n')
if outputLinesCount > 1 then
local buf, _ = buffer.NewBuffer(output, "")
local splitBp = bp:VSplitIndex(buf, true)
splitBp:SetLocalCmd({"filetype", bp.Buf.Settings["filetype"]})
elseif output ~= "--" and output ~= "" and outputLinesCount == 1 then
local path, lineNumber = output:match("^(.-):%s*(%d+):")
if searchLoc ~= nil and searchLoc ~= "" then
-- micro.InfoBar():Message("Open path is ", filepath.Abs(OmniContentFindPath.."/"..path))
path = OmniContentFindPath.."/"..path
end
fzfParseOutput(path, bp, lineNumber, true)
end
end
end
local function OnFindPromptDone(resp, cancelled)
if cancelled then return end
FindContent(resp, OmniContentFindPath)
return
end
local function OnSearchDirSetDone(resp, cancelled)
if cancelled then return end
local bp = micro.CurPane()
if bp == nil then return end
OmniContentFindPath = resp:gsub("{fileDir}", filepath.Dir(bp.Buf.AbsPath))
micro.InfoBar():Prompt("Content to find > ", OmniSearchText, "", nil, OnFindPromptDone)
end
function Self.OmniContent(bp)
OmniSearchText = ""
if bp.Cursor:HasSelection() then
OmniSearchText = bp.Cursor:GetSelection()
OmniSearchText = util.String(OmniSearchText)
end
micro.InfoBar():Prompt( "Search Directory ({fileDir} for current file directory) > ",
"",
"",
nil,
OnSearchDirSetDone)
end
-- local function TermTest(outStr, userArgs)
-- userArgs[1]:Quit()
-- end
function Self.OmniLocalSearch(bp, args)
local localSearchArgs = Common.OmniLocalSearchArgs:gsub("{filePath}", "\""..bp.buf.AbsPath.."\"")
if bp.Cursor:HasSelection() then
localSearchArgs = localSearchArgs.." -q '"..util.String(bp.Cursor:GetSelection()).."'"
end
local output, err = shell.RunInteractiveShell(Common.OmniFzfCmd.." "..localSearchArgs, false, true)
-- -- Test code for running fzf in term pane, but it has no color :/
-- local buf, bufErr = buffer.NewBuffer("", "")
-- if bufErr ~= nil then
-- micro.InfoBar():Error("Error when creating new buffer: ", err:Error())
-- return
-- end
-- local splitBp = bp:VSplitIndex(buf, true)
-- shell.RunTermEmulator(splitBp,
-- -- Common.OmniFzfCmd.." "..localSearchArgs,
-- "fzf", false, true,
-- TermTest, {splitBp})
-- -- callback func(out string, userargs []interface{}),
-- -- userargs []interface{}) error
if err ~= nil or output == "" then
-- micro.InfoBar():Error("Error is: ", err:Error())
else
local _, outputLinesCount = output:gsub('\n', '\n')
if outputLinesCount > 1 then
local buf, _ = buffer.NewBuffer(output, "")
local splitBp = bp:VSplitIndex(buf, true)
splitBp:SetLocalCmd({"filetype", bp.Buf.Settings["filetype"]})
elseif outputLinesCount == 1 then
local lineNumber = output:match("^%s*(.-)%s.*")
-- micro.InfoBar():Message("Output is ", output, " and extracted lineNumber is ", lineNumber)
micro.CurPane().Cursor:ResetSelection()
micro.CurPane():GotoCmd({lineNumber})
micro.CurPane():Center()
end
end
end
function Self.OmniGotoFile(bp)
local localGotoFileArgs = Common.OmniGotoFileArgs
if bp.Cursor:HasSelection() then
localGotoFileArgs = localGotoFileArgs.." -q '"..util.String(bp.Cursor:GetSelection()).."'"
end
local output, err = shell.RunInteractiveShell(Common.OmniFzfCmd.." "..localGotoFileArgs, false, true)
if err ~= nil or output == "" then
-- micro.InfoBar():Error("Error is: ", err:Error())
else
local _, outputLinesCount = output:gsub('\n', '\n')
if outputLinesCount > 1 then
local buf, _ = buffer.NewBuffer(output, "")
local splitBp = bp:VSplitIndex(buf, true)
splitBp:SetLocalCmd({"filetype", bp.Buf.Settings["filetype"]})
elseif outputLinesCount == 1 then
-- local lineNumber = output:match("^%s*(.-)%s.*")
-- local path, lineNumber = output:match("^(.-):%s*(%d+):")
-- micro.InfoBar():Message("Output is ", output, " and extracted lineNumber is ", lineNumber)
fzfParseOutput(output, bp, "1", false)
end
end
end
function Self.OmniTabSearch(bp)
local buffersStr = ""
for i = 1, #micro.Tabs().List do
for j = 1, #micro.Tabs().List[i].Panes do
local currentPane = micro.Tabs().List[i].Panes[j]
local currentBuf = currentPane.Buf
if currentBuf ~= nil then
local currentText = ""
if currentBuf.Path ~= nil and currentBuf.Path ~= "" then
currentText = currentBuf.Path
elseif currentBuf.AbsPath ~= nil and currentBuf.AbsPath ~= "" then
currentText = currentBuf.AbsPath
end
if currentPane.Cursor ~= nil then
currentText = currentText..":"..tostring(currentPane.Cursor.Loc.Y + 1)
end
buffersStr = buffersStr..currentText.."\n"
end
end
end
local createdPath, success =
Common.CreateRuntimeFile("./temp/tabSearch.txt", buffersStr)
local fzfArgs = Common.OmniTabSearchArgs:gsub("{filePath}", "\""..createdPath.."\"")
local finalCmd = Common.OmniFzfCmd.." "..fzfArgs
local output, err = shell.RunInteractiveShell(finalCmd, false, true)
if err ~= nil then
-- micro.InfoBar():Error("Error is: ", err:Error())
else
local _, outputLinesCount = output:gsub('\n', '\n')
if outputLinesCount > 1 then
local buf, _ = buffer.NewBuffer(output, "")
local splitBp = bp:VSplitIndex(buf, true)
splitBp:SetLocalCmd({"filetype", bp.Buf.Settings["filetype"]})
elseif output ~= "--" and output ~= "" and outputLinesCount == 1 then
local path, lineNumber = output:match("^(.-):%s*(%d+)")
fzfParseOutput(path, bp, lineNumber, true)
end
end
end
return Self