-
Notifications
You must be signed in to change notification settings - Fork 0
/
Search.lua
187 lines (147 loc) · 5.76 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
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 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 -F -i -uu -n '\\''"..firstWord.."'\\'' | "..Common.OmniFzfCmd.." "..fzfArgs..
" -q '\\''"..selectedText.."'\\''"
else
selectedText = selectedText:gsub("'", '"')
firstWord = firstWord:gsub("'", '""')
fzfArgs = Common.OmniContentArgs:gsub("'", '"')
finalCmd = "rg -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 or output == "--" then
-- micro.InfoBar():Error("Error is: ", err:Error())
else
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
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
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)
if err ~= nil or output == "" then
-- micro.InfoBar():Error("Error is: ", err:Error())
else
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
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 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
return Self