- Offizieller Beitrag
In AutoIt können wir mit <Alt + I> die Includedatei aus der Zeile, in der der Cursor steht, öffnen.
Das habe ich nun auch für Lua umgesetzt. Geöffnet werden natürlich nur Bibliotheken *.lua. Sind die Bibliotheken *.dll wird ein Hinweis ausgegeben.
Die shell.dll ist erforderlich!
Wir können hier denselben Hotkey nutzen, dann brauchen wir nicht umdenken:
# 23 Open Require
command.23.$(file.patterns.lua)=dofile $(Lua.User.Scripts.Path)/open_require.lua
command.name.23.$(file.patterns.lua)=Require Datei Öffnen
command.mode.23.$(file.patterns.lua)=subsystem:lua,savebefore:no
command.is.filter.23.$(file.patterns.lua)=1
command.shortcut.23.$(file.patterns.lua)=Alt+I
EDIT: v0.1 hatte mögliche Punkte im Bibliotheksnamen nicht berücksichtigt.
EDIT 15.08.2019:
Mit den Punkten innerhalb einer require Datei habe ich mich in die Irre treiben lassen. Jetzt ist mir die Bedeutung erst klar geworden:
Ein Punkt im require (z.B. require alien.core) bedeutet, Öffne die Datei core(dll od. lua, hier dll) im Verzeichnis alien.
Bei richtiger Definition von package.path & package.cpath kann ich Bibliotheken in Ordnern mit exakt demselben Namen, wie die Datei (ohne Erweiterung), speichern. Das ist bei der Alien-Bibliothek der Fall:
Ordner: alien
Dateien: alien.lua / core.dll / struct.dll
require "alien" bewirkt: Öffne alien.lua
require "alien.core" bewirkt: Öffne Ordner "alien" und dort die Datei "core"(.dll)
..,und weitere Änderung: Da Lua nicht zeilenbasiert ist (mehrere Befehle können hintereinander stehen: require "shell" require "bit"), werden diese Varianten jetzt auch erkannt. Befindet sich der Cursor im Bereich: Vor require bis hinter Bibliothekname wird diese Bibliothek geöffnet. Steht der Cursor irgendwo in der Zeile und eine/mehrere require Aufrufe sind vorhanden wird der erste Aufruf verwendet.
Dieses Verhalten berücksichtigt die v0.5 jetzt.
EDIT 16.08.2019:
Ich habe jetzt noch berücksichtigt, dass bei mehreren require in einer Zeile, diese auch noch mit unterschiedlichen Stringbegrenzern eingefasst sind. Wenn dann der Cursor ausserhalb des require steht soll das erste Vorkommen verwendet werden, was aber nicht zwingend das zuerst gefundene ist, da in zwei Schritten gesucht wird.
Aktuelle Version v0.6
-- TIME_STAMP 2019-08-16 20:44:06 v 0.6
local Require = {}
Require.sortTable = function(self, _t)
local tSort, tRet = {}, {}
for k, v in pairs(_t) do tSort[#tSort+1] = k end
table.sort(tSort)
for k = 1, #tSort do
tRet[k] = {tSort[k], _t[tSort[k]]}
end
return tRet
end
Require.getFile = function(self)
local pos = editor.CurrentPos
local linenum = editor:LineFromPosition(pos)
local line = editor:GetLine(linenum)
local home = editor:PositionFromLine(linenum)
local tReq, file, tIndex, index = {}, '', {}
for s, t, e in line:gmatch("()require%s-%(-(%b'')()") do table.insert(tReq, {home+s-1, home+e-1, t:sub(2,-2)}) tIndex[s] = #tReq end
for s, t, e in line:gmatch('()require%s-%(-(%b"")()') do table.insert(tReq, {home+s-1, home+e-1, t:sub(2,-2)}) tIndex[s] = #tReq end
if #tReq > 0 then
for i=1, #tReq do
if pos >= tReq[i][1] and pos <= tReq[i][2] then file = tReq[i][3] break end
end
if file == '' then -- cursor outside the require command, use 1st match
tIndex = self:sortTable(tIndex)
file = tReq[tIndex[1][2]][3]
end
file = file:gsub('%.', '/')
return file, linenum +1
end
return nil, linenum +1
end
Require.createPathes = function(self, _file)
local pathes = package.path
local tPathes, bBreak = {}, false
while pathes:len() do
local pos = pathes:find(';')
local fullpath
if pos then
local path = pathes:sub(1,pos-1)
pathes = pathes:sub(pos+1)
fullpath = path
else
fullpath = pathes
bBreak = true
end
if not (fullpath:sub(-8) == 'init.lua') then
fullpath = fullpath:gsub('%?',_file) -- print(fullpath)
table.insert(tPathes, fullpath)
end
if bBreak then break end
end
return tPathes
end
Require.openFile = function(self)
local s_require, linenum = self:getFile()
if not s_require then
print('@_NONE_REQUIRE__LINE\t\t'..tostring(linenum))
return
end
local tPathes = self:createPathes(s_require)
local shell = require "shell"
local s_open
for i = 1, #tPathes do
if shell.fileexists(tPathes[i]) then
s_open = tPathes[i]
break
end
end
if s_open then
print('@_REQUIRE_OPENED__LINE\t\t'..tostring(linenum)..' ['..s_open..']')
scite.Open(s_open)
else
print('@_REQUIRE_IS_DLL_FILE__CAN\'T_OPEN__LINE\t\t'..tostring(linenum))
end
end
Require:openFile()
Alles anzeigen