forked from DGivney/geany-lua-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
guess-java-import.lua
105 lines (90 loc) · 3.03 KB
/
guess-java-import.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
#! /usr/bin/env lua
-- Attempt to guess import statements for the highlighted class name.
--
-- (c) 2014 Carl Antuar.
-- Distribution is permitted under the terms of the GPLv3
-- or any later version.
---- Define functions ----
debugEnabled = false
dofile(geany.appinfo()["scriptdir"]..geany.dirsep.."util.lua")
function getParent(classname)
local reverseIndex = classname:reverse():find(".", 1, true)
if reverseIndex then
local index = classname:len() - reverseIndex
debugMessage("Last dot in "..classname.." is at "..index)
return classname:sub(1, index)
else return nil
end
end
function getSurroundingLine(caret)
return geany.lines(geany.rowcol(caret))
end
function getCurrentWord()
local selectedText = geany.selection()
if selectedText == nil or selectedText == "" then
local oldCursorPos = geany.caret()
debugMessage("No text selected; seeking current word for position "..oldCursorPos)
geany.keycmd("SELECT_WORD")
selectedText = geany.selection():gsub("^%s*(.-)%s*$", "%1")
geany.caret(oldCursorPos)
else
debugMessage("Selected text: ["..selectedText.."]")
end
return selectedText
end
---- Start execution ----
local className = getCurrentWord()
debugMessage("Class name is ["..className.."]")
if geany.text():find("\nimport%s*[a-zA-Z0-9.]+%."..className.."%s*;") then
geany.message("Already imported "..className)
return
end
local searchCommand = "cat "..getSupportDir()..geany.dirsep.."*.index |sort |uniq | grep '\\b"..className.."\\b'"
local count,qualifiedImports = getOutputLines(searchCommand)
for index,import in ipairs(qualifiedImports) do
local starImport = getParent(import)..".*"
debugMessage("Looking for "..starImport)
if geany.text():find("\nimport%s*"..starImport:gsub("[*]", "[*]")..";") then
geany.message("Already imported "..starImport)
return
end
end
if count > 0 then
import = geany.choose("Is one of these the class you want?", qualifiedImports)
else
geany.message("Couldn't guess import statement for ["..className.."]")
end
if not import then return end
debugMessage("Importing "..import)
local startIndex = geany.text():find("package%s")
if startIndex then
startIndex = startIndex + getSurroundingLine(startIndex):len() - 1
else
startIndex = 0
end
local insertedText = "import "..import..";\n"
local package = insertedText
local found = false
repeat
debugMessage("Seeking optimal entry point for "..package)
insertionIndex = geany.text():find(package)
if insertionIndex then
-- seek the best position
startIndex = insertionIndex - 1
repeat
local existingLine = getSurroundingLine(startIndex + 1)
if insertedText > existingLine and existingLine:find(package) then
startIndex = startIndex + existingLine:len()
else
found = true
end
until found
end
-- subtract one character at a time until we find a match
package = package:sub(1, package:len()-1)
until found or package:len() == 8
local oldCursorPos = geany.caret() + insertedText:len()
geany.caret(startIndex)
debugMessage("About to insert "..insertedText.." at "..startIndex)
geany.selection(insertedText)
geany.caret(oldCursorPos)