-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommon.lua
197 lines (162 loc) · 6.59 KB
/
Common.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
local micro = import("micro")
local config = import("micro/config")
local buffer = import("micro/buffer")
local util = import("micro/util")
local os = import("os")
local filepath = import("path/filepath")
local Self = {}
Self.OmniContentArgs = config.GetGlobalOption("OmniGlobalSearchArgs")
Self.OmniLocalSearchArgs = config.GetGlobalOption("OmniLocalSearchArgs")
Self.OmniGotoFileArgs = config.GetGlobalOption("OmniGotoFileArgs")
Self.OmniSelectType = config.GetGlobalOption("OmniSelectType")
Self.OmniHistoryLineDiff = config.GetGlobalOption("OmniHistoryLineDiff")
Self.OmniCanUseNewCursor = config.GetGlobalOption("OmniCanUseNewCursor")
-- TODO: Allow setting highlight to use regex or not
Self.OmniFzfCmd = config.GetGlobalOption("OmniFzfCmd")
Self.OmniNewFileMethod = config.GetGlobalOption("OmniNewFileMethod")
Self.OmniMinimapMaxIndent = config.GetGlobalOption("OmniMinimapMaxIndent")
Self.OmniMinimapContextNumLines = config.GetGlobalOption("OmniMinimapContextNumLines")
Self.OmniMinimapMinDistance = config.GetGlobalOption("OmniMinimapMinDistance")
Self.OmniMinimapMaxColumns = config.GetGlobalOption("OmniMinimapMaxColumns")
Self.OmniMinimapTargetNumLines = config.GetGlobalOption("OmniMinimapTargetNumLines")
Self.OmniMinimapScrollContent = config.GetGlobalOption("OmniMinimapScrollContent")
function Self.IsPathDir(path)
-- Stat the file/dir path we created
-- file_stat should be non-nil, and stat_err should be nil on success
local file_stat, stat_err = os.Stat(path)
if stat_err ~= nil then
return false
elseif file_stat ~= nil then
-- Assume it exists if no errors
return file_stat:IsDir()
end
return false
end
-- Grabbed from filemanager2
-- Stat a path to check if it exists, returning true/false
function Self.path_exists(path)
-- Stat the file/dir path we created
-- file_stat should be non-nil, and stat_err should be nil on success
local file_stat, stat_err = os.Stat(path)
-- Check if what we tried to create exists
if stat_err ~= nil then
-- true/false if the file/dir exists
return os.IsExist(stat_err)
elseif file_stat ~= nil then
-- Assume it exists if no errors
return true
end
return false
end
function Self.HandleOpenFile(path, bp, lineNum, gotoLineIfExists)
-- Turn to relative path if possible
local wd, err = os.Getwd()
if err == nil then
local relPath, relErr = filepath.Rel(wd, path)
if relErr == nil and relPath ~= nil then
path = relPath
end
end
if Self.OmniNewFileMethod == "smart_newtab" then
Self.SmartNewTab(path, bp, lineNum, gotoLineIfExists)
return
end
if Self.OmniNewFileMethod == "newtab" then
bp:NewTabCmd({path})
else
local buf, bufErr = buffer.NewBufferFromFile(path)
if bufErr ~= nil then return end
if Self.OmniNewFileMethod == "vsplit" then
bp:VSplitIndex(buf, true)
elseif Self.OmniNewFileMethod == "hsplit" then
bp:HSplitIndex(buf, true)
else
bp:OpenBuffer(buf)
end
end
-- micro.Log("fzfParseOutput new buffer")
micro.CurPane().Cursor:ResetSelection()
micro.CurPane():GotoCmd({lineNum})
end
function Self.OpenPaneIfExist(path)
local cleanFilepath = filepath.Clean(path)
local wd, wdErr = os.Getwd()
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
-- micro.Log("cleanFilepath:", cleanFilepath)
-- micro.Log("currentBuf.AbsPath:", currentBuf.AbsPath)
-- micro.Log("currentBuf.Path:", currentBuf.Path)
-- end
if currentBuf ~= nil and currentBuf.AbsPath ~= "" and currentBuf.AbsPath ~= nil then
local calculatedAbsPath = filepath.Abs(cleanFilepath)
if not filepath.IsAbs(cleanFilepath) and wdErr == nil then
local absPath, absErr = filepath.Abs(filepath.Join(wd, cleanFilepath))
-- micro.Log("absPath:", absPath)
if absErr == nil then
calculatedAbsPath = absPath
end
end
-- micro.Log("calculatedAbsPath:", calculatedAbsPath)
if filepath.Clean(currentBuf.AbsPath) == calculatedAbsPath or
filepath.Clean(currentBuf.AbsPath) == cleanFilepath or
filepath.Clean(currentBuf.Path) == cleanFilepath then
-- NOTE: SetActive functions has index starting at 0 instead lol
micro.Tabs():SetActive(i - 1)
micro.Tabs().List[i]:SetActive(j - 1)
return true
end
end
end
end
return false
end
-- NOTE: lineNum is string
function Self.SmartNewTab(path, bp, lineNum, gotoLineIfExists)
local cleanFilepath = filepath.Clean(path)
-- micro.Log("cleanFilepath:", cleanFilepath)
-- If current pane is empty, we can open in it
if not Self.path_exists(micro.CurPane().Buf.AbsPath) or Self.IsPathDir(micro.CurPane().Buf.AbsPath) then
if #micro.CurPane().Buf:Bytes() == 0 then
bp:OpenCmd({cleanFilepath})
micro.CurPane():GotoCmd({lineNum})
return
end
end
-- Otherwise find if there's any existing panes
if Self.OpenPaneIfExist(cleanFilepath) then
if gotoLineIfExists then
micro.CurPane().Cursor:ResetSelection()
micro.CurPane():GotoCmd({lineNum})
end
return
end
-- currentPane:Relocate()
-- If not just open it
local currentActiveIndex = micro.Tabs():Active()
bp:NewTabCmd({cleanFilepath})
bp:TabMoveCmd({tostring(currentActiveIndex + 2)})
micro.CurPane():GotoCmd({lineNum})
end
function Self.LocBoundCheck(buf, loc)
local totalNumOfLines = buf:LinesNum()
local returnLoc = buffer.Loc(loc.X, loc.Y)
if loc.Y >= totalNumOfLines then
returnLoc = buffer.Loc(returnLoc.X, totalNumOfLines - 1)
end
if loc.Y < 0 then
returnLoc = buffer.Loc(returnLoc.X, 0)
end
local lineLength = util.CharacterCountInString(buf:Line(returnLoc.Y))
if lineLength == 0 then
returnLoc = buffer.Loc(0, returnLoc.Y)
elseif loc.X >= lineLength then
returnLoc = buffer.Loc(lineLength, returnLoc.Y)
else
returnLoc = buffer.Loc(loc.X, returnLoc.Y)
end
return returnLoc
end
return Self