From 143b48bad79e02ef662a9769ddea670ae714f4c0 Mon Sep 17 00:00:00 2001 From: Neko Box Coder Date: Tue, 10 Sep 2024 20:23:00 +0100 Subject: [PATCH] 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 --- Common.lua | 12 ++++---- Diff.lua | 24 +++++---------- Highlight.lua | 11 ++++--- History.lua | 6 ++-- MicroOmni.lua | 84 +++++++++++++++++++++++---------------------------- Minimap.lua | 13 ++++---- Search.lua | 14 ++++----- WordJump.lua | 14 ++++----- repo.json | 7 +++++ 9 files changed, 87 insertions(+), 98 deletions(-) diff --git a/Common.lua b/Common.lua index 5f37247..873edec 100644 --- a/Common.lua +++ b/Common.lua @@ -65,8 +65,8 @@ function Self.HandleOpenFile(path, bp, lineNum, gotoLineIfExists) -- Turn to relative path if possible local wd, err = os.Getwd() if err == nil then - local relPath, err = filepath.Rel(wd, path) - if err == nil and relPath ~= nil then + local relPath, relErr = filepath.Rel(wd, path) + if relErr == nil and relPath ~= nil then path = relPath end end @@ -79,8 +79,8 @@ function Self.HandleOpenFile(path, bp, lineNum, gotoLineIfExists) if Self.OmniNewFileMethod == "newtab" then bp:NewTabCmd({path}) else - local buf, err = buffer.NewBufferFromFile(path) - if err ~= nil then return end + local buf, bufErr = buffer.NewBufferFromFile(path) + if bufErr ~= nil then return end if Self.OmniNewFileMethod == "vsplit" then bp:VSplitIndex(buf, true) @@ -154,8 +154,8 @@ function Self.SmartNewTab(path, bp, lineNum, gotoLineIfExists) -- Otherwise find if there's any existing panes if Self.OpenPaneIfExist(cleanFilepath) then if gotoLineIfExists then - currentPane.Cursor:ResetSelection() - currentPane:GotoCmd({lineNum}) + micro.CurPane().Cursor:ResetSelection() + micro.CurPane():GotoCmd({lineNum}) end return end diff --git a/Diff.lua b/Diff.lua index 686b529..d5aa5fb 100644 --- a/Diff.lua +++ b/Diff.lua @@ -20,7 +20,7 @@ local OmniDiffTargetPanes = {} local OmniDiffDiffPanes = {} -function createRuntimeFile(relativePath, data) +local function createRuntimeFile(relativePath, data) local microOmniDir = config.ConfigDir.."/plug/MicroOmni/" if not Common.path_exists(filepath.Dir(microOmniDir..relativePath)) then local err = os.MkdirAll(filepath.Dir(microOmniDir..relativePath), os.ModePerm) @@ -42,7 +42,7 @@ function createRuntimeFile(relativePath, data) return microOmniDir..relativePath, true end -function processDiffOutput(output) +local function processDiffOutput(output) local outputLines = {} local currentLineIndex = 1 @@ -102,7 +102,6 @@ function processDiffOutput(output) -- Populate the return lines local returnLines = {} - currentLineIndex = firstDiffIndex for i = firstDiffIndex, outputLinesCount do local curLine if outputLines[i] ~= nil then @@ -144,7 +143,7 @@ function processDiffOutput(output) return table.concat(returnLines, "\n") end -function OnDiffFinishCallback(resp, cancelled) +local function OnDiffFinishCallback(resp, cancelled) if cancelled then return end @@ -275,19 +274,10 @@ function OnDiffFinishCallback(resp, cancelled) micro.Log("processedDiff: ", processedDiff) if err == nil or err:Error() == "exit status 1" or err:Error() == "exit status 2" then local curPane = micro.CurPane() - local relPath, err = filepath.Rel(os.Getwd(), minusFile) - if err == nil and relPath ~= nil then - minusFile = relPath - end - - relPath, err = filepath.Rel(os.Getwd(), plusFile) - if err == nil and relPath ~= nil then - plusFile = relPath - end - local buf, err = buffer.NewBuffer(processedDiff, "diff"..#OmniDiffTargetPanes) - if err ~= nil then - micro.InfoBar():Error(err) + local buf, bufErr = buffer.NewBuffer(processedDiff, "diff"..#OmniDiffTargetPanes) + if bufErr ~= nil then + micro.InfoBar():Error(bufErr) return end @@ -355,7 +345,7 @@ function Self.UpdateDiffView() end -function OnDiffPlusCallback(yes, cancelled) +local function OnDiffPlusCallback(yes, cancelled) if cancelled then return end diff --git a/Highlight.lua b/Highlight.lua index fbe74a4..f0e6094 100644 --- a/Highlight.lua +++ b/Highlight.lua @@ -12,7 +12,7 @@ local Self = {} local OmniFirstMultiCursorSpawned = false -function OnTypingHighlight(msg) +local function OnTypingHighlight(msg) if micro.CurPane() == nil or micro.CurPane().Buf == nil then return end local bp = micro.CurPane() @@ -21,7 +21,7 @@ function OnTypingHighlight(msg) bp.Buf.HighlightSearch = true end -function OnSubmitHighlightFind(msg, cancelled) +local function OnSubmitHighlightFind(msg, cancelled) if micro.CurPane() == nil or micro.CurPane().Buf == nil or msg == nil or msg == "" then return end local bp = micro.CurPane() @@ -95,7 +95,7 @@ function Self.OmniHighlightOnly(bp) end -function PerformMultiCursor(bp, forceMove) +local function PerformMultiCursor(bp, forceMove) -- Check highlight if not bp.Buf.HighlightSearch or bp.Buf.LastSearch == "" then bp:SpawnMultiCursor() @@ -117,8 +117,11 @@ function PerformMultiCursor(bp, forceMove) if moveCursor then OmniFirstMultiCursorSpawned = true + bp:Deselect() local currentLoc = lastCursor.Loc - searchStart = buffer.Loc(currentLoc.X, currentLoc.Y) + -- searchStart = bp.Buf, buffer.Loc(currentLoc.X, currentLoc.Y) + searchStart = Common.LocBoundCheck(bp.Buf, buffer.Loc(currentLoc.X, currentLoc.Y - 1)) + -- bp.Cursor:Deselect(false) else OmniFirstMultiCursorSpawned = false diff --git a/History.lua b/History.lua index 56b8969..4d34ca4 100644 --- a/History.lua +++ b/History.lua @@ -122,7 +122,7 @@ function Self.RecordCursorHistory() end -function GoToHistoryEntry(bp, entry) +local function GoToHistoryEntry(bp, entry) micro.Log("GoToHistoryEntry called") micro.Log( "Goto Entry: ", OmniCursorFilePathMap[entry.FileId], ", ", entry.CursorLoc.X, ", ", entry.CursorLoc.Y) @@ -132,8 +132,8 @@ function GoToHistoryEntry(bp, entry) -- micro.Log("os.Getwd():", os.Getwd()) local wd, err = os.Getwd() if err == nil then - local relPath, err = filepath.Rel(wd, entryFilePath) - if err == nil and relPath ~= nil then + local relPath, relErr = filepath.Rel(wd, entryFilePath) + if relErr == nil and relPath ~= nil then entryFilePath = relPath end end diff --git a/MicroOmni.lua b/MicroOmni.lua index f88cc21..49cc64e 100644 --- a/MicroOmni.lua +++ b/MicroOmni.lua @@ -1,11 +1,13 @@ -VERSION = "0.2.2" +VERSION = "0.2.3" + +-- luacheck . --globals import VERSION preQuit onAnyEvent init --ignore 212 542 611 612 613 614 local micro = import("micro") local buffer = import("micro/buffer") local shell = import("micro/shell") local util = import("micro/util") local strings = import("strings") -local os = import("os") +-- local os = import("os") local config = import("micro/config") local fmt = import('fmt') @@ -19,35 +21,9 @@ local Highlight = require("Highlight") local Diff = require("Diff") local Minimap = require("Minimap") -function LuaCopy(obj, seen) - if type(obj) ~= 'table' then return obj end - if seen and seen[obj] then return seen[obj] end - local s = seen or {} - local res = setmetatable({}, getmetatable(obj)) - s[obj] = res - for k, v in pairs(obj) do res[copy(k, s)] = copy(v, s) end - return res -end - -function preQuit(bp) - Diff.CheckAndQuitDiffView(bp) - Minimap.CheckAndQuitMinimap(bp) - return true -end - -function onAnyEvent() - micro.Log("onAnyEvent called") - local bpToCenter = Diff.UpdateDiffView() - if bpToCenter ~= nil then - OmniCenter(bpToCenter) - end - History.RecordCursorHistory() - Minimap.UpdateMinimapView() -end - -- See issue https://github.com/zyedidia/micro/issues/3320 -- Modified from https://github.com/kaarrot/microgrep/blob/e1a32e8b95397a40e5dda0fb43e7f8d17469b88c/microgrep.lua#L118 -function WriteToClipboardWorkaround(content) +local function WriteToClipboardWorkaround(content) if micro.CurPane() == nil then return end local curTab = micro.CurPane():Tab() @@ -57,7 +33,7 @@ function WriteToClipboardWorkaround(content) -- Split pane in half and add some text micro.CurPane():HSplitAction() - local buf,err = buffer.NewBuffer(content, "") + local buf, _ = buffer.NewBuffer(content, "") -- Workaround to copy path to clioboard micro.CurPane():OpenBuffer(buf) micro.CurPane():SelectAll() @@ -67,18 +43,17 @@ function WriteToClipboardWorkaround(content) curTab:SetActive(curPaneIndex) end -function CheckCommand(command) +local function CheckCommand(command) local _, error = shell.RunCommand(command) if error ~= nil then return false end return true end -function OmniSelect(bp, args) +local function OmniSelect(bp, args) if #args < 1 then return end local buf = bp.Buf local cursor = buf:GetActiveCursor() - local currentLoc = cursor.Loc local targetLine = cursor.Loc.Y if Common.OmniSelectType == nil or Common.OmniSelectType == "" then @@ -114,7 +89,7 @@ end -function OmniCopyRelativePath(bp) +local function OmniCopyRelativePath(bp) if bp.Buf == nil then return end -- clipboard.Write(bp.Buf.Path, clipboard.ClipboardReg) @@ -122,7 +97,7 @@ function OmniCopyRelativePath(bp) micro.InfoBar():Message(bp.Buf.Path, " copied into clipboard") end -function OmniCopyAbsolutePath(bp) +local function OmniCopyAbsolutePath(bp) if bp.Buf == nil then return end -- clipboard.Write(bp.Buf.AbsPath, clipboard.ClipboardReg) @@ -130,9 +105,8 @@ function OmniCopyAbsolutePath(bp) micro.InfoBar():Message(bp.Buf.AbsPath, " copied into clipboard") end -function OmniCenter(bp) +local function OmniCenter(bp) local view = bp:GetView() - local oriX = bp.Cursor.Loc.X bp.Cursor:ResetSelection() bp.Buf:ClearCursors() local targetLineY = view.StartLine.Line + view.Height / 2 @@ -140,7 +114,7 @@ function OmniCenter(bp) end -- Testing auto complete for commands -function TestCompleter(buf) +local function TestCompleter(buf) local activeCursor = buf:GetActiveCursor() local input, argstart = buf:GetArg() -- micro.Log("input:", input) @@ -165,7 +139,7 @@ function TestCompleter(buf) -- sort.Strings(suggestions) table.sort(suggestions, function(a, b) return a:upper() < b:upper() end) -- completions := make([]string, len(suggestions)) - completions = {} + local completions = {} for _, suggestion in ipairs(suggestions) do local offset = activeCursor.X - argstart table.insert(completions, string.sub(suggestion, offset + 1, string.len(suggestion))) @@ -174,23 +148,23 @@ function TestCompleter(buf) -- return {"test", "test2"}, {"test", "test A"} end -function OmniTest(bp, args) +local function OmniTest(bp, args) -- micro.InfoBar():Prompt("Test prompt", "Test Message", "Test", TestECB, TestDoneCB) bp:CdCmd(args) end -function TestDoneCB(msg, cancelled) +local function TestDoneCB(msg, cancelled) -- git diff --output=test.diff -U5 --no-color ".\DefaultUserConfig.yaml" ".\DefaultUserConfig - Copy.yaml" local output, err = shell.RunInteractiveShell(msg, false, true) if err == nil or err:Error() == "exit status 1" then - OmniNewTabRight(micro.CurPane()) + -- OmniNewTabRight(micro.CurPane()) micro.CurPane().Buf:Insert(buffer.Loc(0, 0), output) else micro.InfoBar():Error(err) end end -function OmniTest2(bp, args) +local function OmniTest2(bp, args) -- micro.InfoBar():Prompt("Test prompt", "Test Message", "Test", TestECB, TestDoneCB) -- local wd = os.Getwd() -- micro.InfoBar():Message("Getwd: ", wd) @@ -202,25 +176,25 @@ function OmniTest2(bp, args) -- micro.InfoBar():Prompt("Test prompt", "Test Message", "Test", nil, OnWordJump) end -function OmniTest3(bp, args) +local function OmniTest3(bp, args) -- micro.InfoBar():Prompt("Test prompt", "Test Message", "Test", TestECB, TestDoneCB) -- local wd = os.Getwd() -- local path = bp.buf.AbsPath end -function OmniNewTabRight(bp) +local function OmniNewTabRight(bp) local currentActiveIndex = micro.Tabs():Active() bp:NewTabCmd({}) bp:TabMoveCmd({tostring(currentActiveIndex + 2)}) end -function OmniNewTabLeft(bp) +local function OmniNewTabLeft(bp) local currentActiveIndex = micro.Tabs():Active() bp:NewTabCmd({}) bp:TabMoveCmd({tostring(currentActiveIndex + 1)}) end -function InitializeSettings() +local function InitializeSettings() -- Convert history line diff to integer in the beginning if Common.OmniHistoryLineDiff == nil or Common.OmniHistoryLineDiff == "" then Common.OmniHistoryLineDiff = 5 @@ -327,6 +301,22 @@ function InitializeSettings() end end +function preQuit(bp) + Diff.CheckAndQuitDiffView(bp) + Minimap.CheckAndQuitMinimap(bp) + return true +end + +function onAnyEvent() + micro.Log("onAnyEvent called") + local bpToCenter = Diff.UpdateDiffView() + if bpToCenter ~= nil then + OmniCenter(bpToCenter) + end + History.RecordCursorHistory() + Minimap.UpdateMinimapView() +end + function init() config.MakeCommand("OmniGlobalSearch", Search.OmniContent, config.NoComplete) config.MakeCommand("OmniLocalSearch", Search.OmniLocalSearch, config.NoComplete) diff --git a/Minimap.lua b/Minimap.lua index 6ce3d43..e547256 100644 --- a/Minimap.lua +++ b/Minimap.lua @@ -14,7 +14,7 @@ local OmniMinimapPanes = {} local OmniMinimapRecords = {} -function GetIndentLevel(str, tabSize) +local function GetIndentLevel(str, tabSize) local indentLevel = 0 local continuousSpace = 0 local nonWhiteSpaceChar = nil @@ -38,7 +38,7 @@ function GetIndentLevel(str, tabSize) return indentLevel, nonWhiteSpaceChar end -function RemoveMinimapIfExist(targetBp) +local function RemoveMinimapIfExist(targetBp) for i, val in ipairs(OmniMinimapTargetPanes) do if targetBp == val then OmniMinimapPanes[i]:Quit() @@ -146,18 +146,15 @@ function Self.OmniMinimap(bp) local minimapMaxLines = Common.OmniMinimapTargetNumLines local indentBudget = {} local firstIndent = -1 - local maxIndent = indentCutoff for i = 0, indentCutoff do if indentCount[i] ~= nil and indentCount[i] ~= 0 then if indentCount[i] > minimapMaxLines and firstIndent == -1 then indentBudget[i] = indentCount[i] - maxIndent = i break else if indentCount[i] > minimapMaxLines then indentBudget[i] = minimapMaxLines - maxIndent = i break else indentBudget[i] = indentCount[i] @@ -168,7 +165,6 @@ function Self.OmniMinimap(bp) if firstIndent == -1 then firstIndent = i end - maxIndent = i end end @@ -266,7 +262,10 @@ function Self.OmniMinimap(bp) -- micro.Log("currentLineStr[", curLineNum, "]:", currentLineStr) -- currentLineStr = string.gsub(currentLineStr, "[\n\r]", "") if #currentLineStr > minimapShowLength then - table.insert(outputLines, string.format("%-5d %s", curLineNum, currentLineStr:sub(1, minimapShowLength).."...")) + table.insert( outputLines, + string.format( "%-5d %s", + curLineNum, + currentLineStr:sub(1, minimapShowLength).."...")) else table.insert(outputLines, string.format("%-5d %s", curLineNum, currentLineStr)) end diff --git a/Search.lua b/Search.lua index 00b2359..3876327 100644 --- a/Search.lua +++ b/Search.lua @@ -18,7 +18,7 @@ local OmniSearchText = "" local Self = {} -- NOTE: lineNum is string -function fzfParseOutput(output, bp, lineNum, gotoLineIfExists) +local function fzfParseOutput(output, bp, lineNum, gotoLineIfExists) micro.Log("fzfParseOutput called") if output ~= "" then local file = string.gsub(output, "[\n\r]", "") @@ -29,7 +29,7 @@ function fzfParseOutput(output, bp, lineNum, gotoLineIfExists) end end -function getOS() +local function getOS() if runtime.GOOS == "windows" then return "Windows" else @@ -37,15 +37,15 @@ function getOS() end end -function FindContent(str, searchLoc) +local function FindContent(str, searchLoc) micro.Log("Find Content called") local bp = micro.CurPane() local selectedText = str - local fzfArgs = "" + local fzfArgs -- micro.Log("selectedText before: ", selectedText) -- micro.Log("Common.OmniContentArgs before: ", common.OmniContentArgs) - local firstWord, otherWords = selectedText:match("^(.[^%s]*)%s-(.*)$") + local firstWord, _ = selectedText:match("^(.[^%s]*)%s-(.*)$") if firstWord == nil or firstWord == "" then micro.InfoBar():Error("Failed to extract first word... str: ", str) @@ -111,13 +111,13 @@ function FindContent(str, searchLoc) end end -function OnFindPromptDone(resp, cancelled) +local function OnFindPromptDone(resp, cancelled) if cancelled then return end FindContent(resp, OmniContentFindPath) return end -function OnSearchDirSetDone(resp, cancelled) +local function OnSearchDirSetDone(resp, cancelled) if cancelled then return end local bp = micro.CurPane() diff --git a/WordJump.lua b/WordJump.lua index 879ac8f..29f0d84 100644 --- a/WordJump.lua +++ b/WordJump.lua @@ -21,7 +21,7 @@ local OmniOriginalHighlightSearch = true local OmniJumpWordsRecords = {} local OmniOriginalWordsRecords = {} -function AssignJumpWords(majorChars, minorChars, rowIndexStart, rowIndexEnd, currentJumpChar) +local function AssignJumpWords(majorChars, minorChars, rowIndexStart, rowIndexEnd, currentJumpChar) micro.Log("rowIndexStart:", rowIndexStart) micro.Log("rowIndexEnd:", rowIndexEnd) micro.Log("string.len(majorChars):", string.len(majorChars)) @@ -56,7 +56,7 @@ function AssignJumpWords(majorChars, minorChars, rowIndexStart, rowIndexEnd, cur table.insert(runeBuffer, currentLineBytes[k]) end - local rune, size = utf8.DecodeRune(runeBuffer) + local _, size = utf8.DecodeRune(runeBuffer) runeBuffer = {} for k = j, j + size - 1 do table.insert(runeBuffer, currentLineBytes[k]) @@ -118,7 +118,7 @@ function AssignJumpWords(majorChars, minorChars, rowIndexStart, rowIndexEnd, cur return rcToOriWord, jumpWordToRc end -function AssignJumpWordsToView(msg) +local function AssignJumpWordsToView(msg) local bp = micro.CurPane() local view = bp:GetView() local numberOfLines = bp.Buf:LinesNum() @@ -178,7 +178,7 @@ function AssignJumpWordsToView(msg) -- bp.Buf:UpdateRules() end -function RestoreOriginalWords(rcToOriWord, exclusionRow) +local function RestoreOriginalWords(rcToOriWord, exclusionRow) local bp = micro.CurPane() for row, rowValues in pairs(rcToOriWord) do if exclusionRow == nil or row ~= exclusionRow then @@ -191,7 +191,7 @@ function RestoreOriginalWords(rcToOriWord, exclusionRow) end end -function OnTypingJump(msg) +local function OnTypingJump(msg) local bp = micro.CurPane() if string.len(msg) > 2 then @@ -219,7 +219,7 @@ function OnTypingJump(msg) end -function OnWordJump(msg, cancelled) +local function OnWordJump(msg, cancelled) RestoreOriginalWords(OmniOriginalWordsRecords, nil) local bp = micro.CurPane() @@ -246,7 +246,7 @@ function Self.OmniJump(bp) bp.Cursor:ResetSelection() bp.Buf:ClearCursors() - AssignJumpWordsToView(msg) + AssignJumpWordsToView("") -- Store the original search related settings OmniOriginalSearchIgnoreCase = bp.Buf.Settings["ignorecase"] diff --git a/repo.json b/repo.json index 65153f6..630d0bb 100644 --- a/repo.json +++ b/repo.json @@ -11,6 +11,13 @@ "history" ], "Versions": [ + { + "Version": "0.2.3", + "Url": "https://github.com/Neko-Box-Coder/MicroOmni/releases/download/v0.2.3/release.zip", + "Require": { + "micro": ">=2.0.13" + } + }, { "Version": "0.2.2", "Url": "https://github.com/Neko-Box-Coder/MicroOmni/releases/download/v0.2.2/release.zip",