Skip to content

Commit 143b48b

Browse files
Updating scripts to pass luacheck, fixing crash bug in SmartNewTab
Updating scripts to pass luacheck, Fixing crash bug in SmartNewTab caused by missing variable declaration
1 parent d533521 commit 143b48b

File tree

9 files changed

+87
-98
lines changed

9 files changed

+87
-98
lines changed

Common.lua

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ function Self.HandleOpenFile(path, bp, lineNum, gotoLineIfExists)
6565
-- Turn to relative path if possible
6666
local wd, err = os.Getwd()
6767
if err == nil then
68-
local relPath, err = filepath.Rel(wd, path)
69-
if err == nil and relPath ~= nil then
68+
local relPath, relErr = filepath.Rel(wd, path)
69+
if relErr == nil and relPath ~= nil then
7070
path = relPath
7171
end
7272
end
@@ -79,8 +79,8 @@ function Self.HandleOpenFile(path, bp, lineNum, gotoLineIfExists)
7979
if Self.OmniNewFileMethod == "newtab" then
8080
bp:NewTabCmd({path})
8181
else
82-
local buf, err = buffer.NewBufferFromFile(path)
83-
if err ~= nil then return end
82+
local buf, bufErr = buffer.NewBufferFromFile(path)
83+
if bufErr ~= nil then return end
8484

8585
if Self.OmniNewFileMethod == "vsplit" then
8686
bp:VSplitIndex(buf, true)
@@ -154,8 +154,8 @@ function Self.SmartNewTab(path, bp, lineNum, gotoLineIfExists)
154154
-- Otherwise find if there's any existing panes
155155
if Self.OpenPaneIfExist(cleanFilepath) then
156156
if gotoLineIfExists then
157-
currentPane.Cursor:ResetSelection()
158-
currentPane:GotoCmd({lineNum})
157+
micro.CurPane().Cursor:ResetSelection()
158+
micro.CurPane():GotoCmd({lineNum})
159159
end
160160
return
161161
end

Diff.lua

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ local OmniDiffTargetPanes = {}
2020
local OmniDiffDiffPanes = {}
2121

2222

23-
function createRuntimeFile(relativePath, data)
23+
local function createRuntimeFile(relativePath, data)
2424
local microOmniDir = config.ConfigDir.."/plug/MicroOmni/"
2525
if not Common.path_exists(filepath.Dir(microOmniDir..relativePath)) then
2626
local err = os.MkdirAll(filepath.Dir(microOmniDir..relativePath), os.ModePerm)
@@ -42,7 +42,7 @@ function createRuntimeFile(relativePath, data)
4242
return microOmniDir..relativePath, true
4343
end
4444

45-
function processDiffOutput(output)
45+
local function processDiffOutput(output)
4646
local outputLines = {}
4747
local currentLineIndex = 1
4848

@@ -102,7 +102,6 @@ function processDiffOutput(output)
102102

103103
-- Populate the return lines
104104
local returnLines = {}
105-
currentLineIndex = firstDiffIndex
106105
for i = firstDiffIndex, outputLinesCount do
107106
local curLine
108107
if outputLines[i] ~= nil then
@@ -144,7 +143,7 @@ function processDiffOutput(output)
144143
return table.concat(returnLines, "\n")
145144
end
146145

147-
function OnDiffFinishCallback(resp, cancelled)
146+
local function OnDiffFinishCallback(resp, cancelled)
148147
if cancelled then
149148
return
150149
end
@@ -275,19 +274,10 @@ function OnDiffFinishCallback(resp, cancelled)
275274
micro.Log("processedDiff: ", processedDiff)
276275
if err == nil or err:Error() == "exit status 1" or err:Error() == "exit status 2" then
277276
local curPane = micro.CurPane()
278-
local relPath, err = filepath.Rel(os.Getwd(), minusFile)
279-
if err == nil and relPath ~= nil then
280-
minusFile = relPath
281-
end
282-
283-
relPath, err = filepath.Rel(os.Getwd(), plusFile)
284-
if err == nil and relPath ~= nil then
285-
plusFile = relPath
286-
end
287277

288-
local buf, err = buffer.NewBuffer(processedDiff, "diff"..#OmniDiffTargetPanes)
289-
if err ~= nil then
290-
micro.InfoBar():Error(err)
278+
local buf, bufErr = buffer.NewBuffer(processedDiff, "diff"..#OmniDiffTargetPanes)
279+
if bufErr ~= nil then
280+
micro.InfoBar():Error(bufErr)
291281
return
292282
end
293283

@@ -355,7 +345,7 @@ function Self.UpdateDiffView()
355345
end
356346

357347

358-
function OnDiffPlusCallback(yes, cancelled)
348+
local function OnDiffPlusCallback(yes, cancelled)
359349
if cancelled then
360350
return
361351
end

Highlight.lua

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ local Self = {}
1212

1313
local OmniFirstMultiCursorSpawned = false
1414

15-
function OnTypingHighlight(msg)
15+
local function OnTypingHighlight(msg)
1616
if micro.CurPane() == nil or micro.CurPane().Buf == nil then return end
1717

1818
local bp = micro.CurPane()
@@ -21,7 +21,7 @@ function OnTypingHighlight(msg)
2121
bp.Buf.HighlightSearch = true
2222
end
2323

24-
function OnSubmitHighlightFind(msg, cancelled)
24+
local function OnSubmitHighlightFind(msg, cancelled)
2525
if micro.CurPane() == nil or micro.CurPane().Buf == nil or msg == nil or msg == "" then return end
2626

2727
local bp = micro.CurPane()
@@ -95,7 +95,7 @@ function Self.OmniHighlightOnly(bp)
9595
end
9696

9797

98-
function PerformMultiCursor(bp, forceMove)
98+
local function PerformMultiCursor(bp, forceMove)
9999
-- Check highlight
100100
if not bp.Buf.HighlightSearch or bp.Buf.LastSearch == "" then
101101
bp:SpawnMultiCursor()
@@ -117,8 +117,11 @@ function PerformMultiCursor(bp, forceMove)
117117

118118
if moveCursor then
119119
OmniFirstMultiCursorSpawned = true
120+
bp:Deselect()
120121
local currentLoc = lastCursor.Loc
121-
searchStart = buffer.Loc(currentLoc.X, currentLoc.Y)
122+
-- searchStart = bp.Buf, buffer.Loc(currentLoc.X, currentLoc.Y)
123+
searchStart = Common.LocBoundCheck(bp.Buf, buffer.Loc(currentLoc.X, currentLoc.Y - 1))
124+
-- bp.Cursor:Deselect(false)
122125
else
123126
OmniFirstMultiCursorSpawned = false
124127

History.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ function Self.RecordCursorHistory()
122122
end
123123

124124

125-
function GoToHistoryEntry(bp, entry)
125+
local function GoToHistoryEntry(bp, entry)
126126
micro.Log("GoToHistoryEntry called")
127127
micro.Log( "Goto Entry: ", OmniCursorFilePathMap[entry.FileId],
128128
", ", entry.CursorLoc.X, ", ", entry.CursorLoc.Y)
@@ -132,8 +132,8 @@ function GoToHistoryEntry(bp, entry)
132132
-- micro.Log("os.Getwd():", os.Getwd())
133133
local wd, err = os.Getwd()
134134
if err == nil then
135-
local relPath, err = filepath.Rel(wd, entryFilePath)
136-
if err == nil and relPath ~= nil then
135+
local relPath, relErr = filepath.Rel(wd, entryFilePath)
136+
if relErr == nil and relPath ~= nil then
137137
entryFilePath = relPath
138138
end
139139
end

MicroOmni.lua

Lines changed: 37 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
VERSION = "0.2.2"
1+
VERSION = "0.2.3"
2+
3+
-- luacheck . --globals import VERSION preQuit onAnyEvent init --ignore 212 542 611 612 613 614
24

35
local micro = import("micro")
46
local buffer = import("micro/buffer")
57
local shell = import("micro/shell")
68
local util = import("micro/util")
79
local strings = import("strings")
8-
local os = import("os")
10+
-- local os = import("os")
911

1012
local config = import("micro/config")
1113
local fmt = import('fmt')
@@ -19,35 +21,9 @@ local Highlight = require("Highlight")
1921
local Diff = require("Diff")
2022
local Minimap = require("Minimap")
2123

22-
function LuaCopy(obj, seen)
23-
if type(obj) ~= 'table' then return obj end
24-
if seen and seen[obj] then return seen[obj] end
25-
local s = seen or {}
26-
local res = setmetatable({}, getmetatable(obj))
27-
s[obj] = res
28-
for k, v in pairs(obj) do res[copy(k, s)] = copy(v, s) end
29-
return res
30-
end
31-
32-
function preQuit(bp)
33-
Diff.CheckAndQuitDiffView(bp)
34-
Minimap.CheckAndQuitMinimap(bp)
35-
return true
36-
end
37-
38-
function onAnyEvent()
39-
micro.Log("onAnyEvent called")
40-
local bpToCenter = Diff.UpdateDiffView()
41-
if bpToCenter ~= nil then
42-
OmniCenter(bpToCenter)
43-
end
44-
History.RecordCursorHistory()
45-
Minimap.UpdateMinimapView()
46-
end
47-
4824
-- See issue https://github.com/zyedidia/micro/issues/3320
4925
-- Modified from https://github.com/kaarrot/microgrep/blob/e1a32e8b95397a40e5dda0fb43e7f8d17469b88c/microgrep.lua#L118
50-
function WriteToClipboardWorkaround(content)
26+
local function WriteToClipboardWorkaround(content)
5127
if micro.CurPane() == nil then return end
5228

5329
local curTab = micro.CurPane():Tab()
@@ -57,7 +33,7 @@ function WriteToClipboardWorkaround(content)
5733
-- Split pane in half and add some text
5834
micro.CurPane():HSplitAction()
5935

60-
local buf,err = buffer.NewBuffer(content, "")
36+
local buf, _ = buffer.NewBuffer(content, "")
6137
-- Workaround to copy path to clioboard
6238
micro.CurPane():OpenBuffer(buf)
6339
micro.CurPane():SelectAll()
@@ -67,18 +43,17 @@ function WriteToClipboardWorkaround(content)
6743
curTab:SetActive(curPaneIndex)
6844
end
6945

70-
function CheckCommand(command)
46+
local function CheckCommand(command)
7147
local _, error = shell.RunCommand(command)
7248
if error ~= nil then return false end
7349
return true
7450
end
7551

76-
function OmniSelect(bp, args)
52+
local function OmniSelect(bp, args)
7753
if #args < 1 then return end
7854

7955
local buf = bp.Buf
8056
local cursor = buf:GetActiveCursor()
81-
local currentLoc = cursor.Loc
8257
local targetLine = cursor.Loc.Y
8358

8459
if Common.OmniSelectType == nil or Common.OmniSelectType == "" then
@@ -114,33 +89,32 @@ end
11489

11590

11691

117-
function OmniCopyRelativePath(bp)
92+
local function OmniCopyRelativePath(bp)
11893
if bp.Buf == nil then return end
11994

12095
-- clipboard.Write(bp.Buf.Path, clipboard.ClipboardReg)
12196
WriteToClipboardWorkaround(bp.Buf.Path)
12297
micro.InfoBar():Message(bp.Buf.Path, " copied into clipboard")
12398
end
12499

125-
function OmniCopyAbsolutePath(bp)
100+
local function OmniCopyAbsolutePath(bp)
126101
if bp.Buf == nil then return end
127102

128103
-- clipboard.Write(bp.Buf.AbsPath, clipboard.ClipboardReg)
129104
WriteToClipboardWorkaround(bp.Buf.AbsPath)
130105
micro.InfoBar():Message(bp.Buf.AbsPath, " copied into clipboard")
131106
end
132107

133-
function OmniCenter(bp)
108+
local function OmniCenter(bp)
134109
local view = bp:GetView()
135-
local oriX = bp.Cursor.Loc.X
136110
bp.Cursor:ResetSelection()
137111
bp.Buf:ClearCursors()
138112
local targetLineY = view.StartLine.Line + view.Height / 2
139113
bp.Cursor:GotoLoc(Common.LocBoundCheck(bp.Buf, buffer.Loc(bp.Cursor.Loc.X, targetLineY)))
140114
end
141115

142116
-- Testing auto complete for commands
143-
function TestCompleter(buf)
117+
local function TestCompleter(buf)
144118
local activeCursor = buf:GetActiveCursor()
145119
local input, argstart = buf:GetArg()
146120
-- micro.Log("input:", input)
@@ -165,7 +139,7 @@ function TestCompleter(buf)
165139
-- sort.Strings(suggestions)
166140
table.sort(suggestions, function(a, b) return a:upper() < b:upper() end)
167141
-- completions := make([]string, len(suggestions))
168-
completions = {}
142+
local completions = {}
169143
for _, suggestion in ipairs(suggestions) do
170144
local offset = activeCursor.X - argstart
171145
table.insert(completions, string.sub(suggestion, offset + 1, string.len(suggestion)))
@@ -174,23 +148,23 @@ function TestCompleter(buf)
174148
-- return {"test", "test2"}, {"test", "test A"}
175149
end
176150

177-
function OmniTest(bp, args)
151+
local function OmniTest(bp, args)
178152
-- micro.InfoBar():Prompt("Test prompt", "Test Message", "Test", TestECB, TestDoneCB)
179153
bp:CdCmd(args)
180154
end
181155

182-
function TestDoneCB(msg, cancelled)
156+
local function TestDoneCB(msg, cancelled)
183157
-- git diff --output=test.diff -U5 --no-color ".\DefaultUserConfig.yaml" ".\DefaultUserConfig - Copy.yaml"
184158
local output, err = shell.RunInteractiveShell(msg, false, true)
185159
if err == nil or err:Error() == "exit status 1" then
186-
OmniNewTabRight(micro.CurPane())
160+
-- OmniNewTabRight(micro.CurPane())
187161
micro.CurPane().Buf:Insert(buffer.Loc(0, 0), output)
188162
else
189163
micro.InfoBar():Error(err)
190164
end
191165
end
192166

193-
function OmniTest2(bp, args)
167+
local function OmniTest2(bp, args)
194168
-- micro.InfoBar():Prompt("Test prompt", "Test Message", "Test", TestECB, TestDoneCB)
195169
-- local wd = os.Getwd()
196170
-- micro.InfoBar():Message("Getwd: ", wd)
@@ -202,25 +176,25 @@ function OmniTest2(bp, args)
202176
-- micro.InfoBar():Prompt("Test prompt", "Test Message", "Test", nil, OnWordJump)
203177
end
204178

205-
function OmniTest3(bp, args)
179+
local function OmniTest3(bp, args)
206180
-- micro.InfoBar():Prompt("Test prompt", "Test Message", "Test", TestECB, TestDoneCB)
207181
-- local wd = os.Getwd()
208182
-- local path = bp.buf.AbsPath
209183
end
210184

211-
function OmniNewTabRight(bp)
185+
local function OmniNewTabRight(bp)
212186
local currentActiveIndex = micro.Tabs():Active()
213187
bp:NewTabCmd({})
214188
bp:TabMoveCmd({tostring(currentActiveIndex + 2)})
215189
end
216190

217-
function OmniNewTabLeft(bp)
191+
local function OmniNewTabLeft(bp)
218192
local currentActiveIndex = micro.Tabs():Active()
219193
bp:NewTabCmd({})
220194
bp:TabMoveCmd({tostring(currentActiveIndex + 1)})
221195
end
222196

223-
function InitializeSettings()
197+
local function InitializeSettings()
224198
-- Convert history line diff to integer in the beginning
225199
if Common.OmniHistoryLineDiff == nil or Common.OmniHistoryLineDiff == "" then
226200
Common.OmniHistoryLineDiff = 5
@@ -327,6 +301,22 @@ function InitializeSettings()
327301
end
328302
end
329303

304+
function preQuit(bp)
305+
Diff.CheckAndQuitDiffView(bp)
306+
Minimap.CheckAndQuitMinimap(bp)
307+
return true
308+
end
309+
310+
function onAnyEvent()
311+
micro.Log("onAnyEvent called")
312+
local bpToCenter = Diff.UpdateDiffView()
313+
if bpToCenter ~= nil then
314+
OmniCenter(bpToCenter)
315+
end
316+
History.RecordCursorHistory()
317+
Minimap.UpdateMinimapView()
318+
end
319+
330320
function init()
331321
config.MakeCommand("OmniGlobalSearch", Search.OmniContent, config.NoComplete)
332322
config.MakeCommand("OmniLocalSearch", Search.OmniLocalSearch, config.NoComplete)

0 commit comments

Comments
 (0)