Skip to content

Commit

Permalink
Updating scripts to pass luacheck, fixing crash bug in SmartNewTab
Browse files Browse the repository at this point in the history
Updating scripts to pass luacheck,
Fixing crash bug in SmartNewTab caused by missing variable declaration
  • Loading branch information
Neko-Box-Coder committed Sep 10, 2024
1 parent d533521 commit 143b48b
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 98 deletions.
12 changes: 6 additions & 6 deletions Common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
24 changes: 7 additions & 17 deletions Diff.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -355,7 +345,7 @@ function Self.UpdateDiffView()
end


function OnDiffPlusCallback(yes, cancelled)
local function OnDiffPlusCallback(yes, cancelled)
if cancelled then
return
end
Expand Down
11 changes: 7 additions & 4 deletions Highlight.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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

Expand Down
6 changes: 3 additions & 3 deletions History.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
84 changes: 37 additions & 47 deletions MicroOmni.lua
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -114,33 +89,32 @@ end



function OmniCopyRelativePath(bp)
local function OmniCopyRelativePath(bp)
if bp.Buf == nil then return end

-- clipboard.Write(bp.Buf.Path, clipboard.ClipboardReg)
WriteToClipboardWorkaround(bp.Buf.Path)
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)
WriteToClipboardWorkaround(bp.Buf.AbsPath)
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
bp.Cursor:GotoLoc(Common.LocBoundCheck(bp.Buf, buffer.Loc(bp.Cursor.Loc.X, targetLineY)))
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)
Expand All @@ -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)))
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Loading

0 comments on commit 143b48b

Please sign in to comment.