-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathguess-java-import.lua
52 lines (41 loc) · 1.58 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
#! /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")
---- Start execution ----
local selectedText = geany.selection()
debugMessage("Selected text: ["..selectedText.."]")
if selectedText == nil or selectedText == "" then
local oldCursorPos = geany.caret()
debugMessage("No text selected; seeking current word for position "..oldCursorPos)
geany.navigate("word", -1, false)
geany.navigate("word", 1, true)
selectedText = geany.selection():gsub("^%s*(.-)%s*$", "%1")
geany.caret(oldCursorPos)
end
debugMessage("Class name is ["..selectedText.."]")
if geany.text():find("\nimport%s*[a-zA-Z0-9.]+"..selectedText.."%s*;") then
geany.message("Already imported "..selectedText)
return
end
local searchCommand = "cat "..getSupportDir()..geany.dirsep.."*.index |sort |uniq | grep '\\b"..selectedText.."\\b'"
local count,imports = getOutputLines(searchCommand)
if count > 0 then
import = geany.choose("Is one of these the class you want?", imports)
else
geany.message("Couldn't guess import statement for ["..selectedText.."]")
end
if not import then return end
debugMessage("Importing "..import)
local startIndex,stopIndex = geany.text():find("package%s")
if not startIndex then startIndex = 1 end
local oldCursorPos = geany.caret()
geany.caret(startIndex)
geany.navigate("edge", 1)
geany.selection("\nimport "..import..";")
geany.caret(oldCursorPos)