-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathAutocomplete.lua
More file actions
150 lines (129 loc) · 5.48 KB
/
Copy pathAutocomplete.lua
File metadata and controls
150 lines (129 loc) · 5.48 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
--[[`
# Autocomplete.lua
BhWax allows you to write Cocoa code directly in Lua, inside the Gideros IDE. However, the ObjectiveC method selectors
are often quite long and difficult to remember.
This module will walk the Cocoa class object tree and generate an autocompletion file of all the class and instance
methods for the classes that you specify. This autocompletion file, *cocoa_annot.txt*, can be appended to the
*gideros_annot.api* file which is to be found in the */Applications/Gideros Studio/Contents/Resources* folder. Note that you
will have to use **Show Package Contents** to drill into the Gideros Studio application to find this location.
You must restart Gideros Studio after updating this file in order to see the new autocompletion annotations working.
@private
## MIT License: Copyright (C) 2012. Andy Bower, Bowerhaus LLP
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--]]
AutocompleteGenerator=Core.class()
function AutocompleteGenerator:getAllSubclassNamesOf(class, list)
for i,each in ipairs(class:bhSubclassNames()) do
list[#list+1]=each
end
end
function AutocompleteGenerator:getClassNamesMatching(filter, exclusions)
local allClasses={}
self:getAllSubclassNamesOf(NSObject, allClasses)
table.filter(allClasses, function(v) return v:find("^"..filter.."$") end)
for i, excl in ipairs(exclusions) do
table.filter(allClasses, function(v) return not(v:find("^"..excl.."$")) end)
end
table.sort(allClasses)
return allClasses
end
function AutocompleteGenerator:generateAutocompleteListForClassesMatching(inclusions, exclusions)
self.classMethods={}
self.instanceMethods={}
for i, filter in ipairs(inclusions) do
local classes=self:getClassNamesMatching(filter, exclusions)
for _,class in pairs(classes) do
self:collateAutocompleteForClass(class)
end
end
local stream=io.open(getPathForFile(self.outputFile), "w+")
for sel,info in pairsKeySorted(self.classMethods) do
stream:write(self:formatAutocompleteSelector(info.selector, info.className, true))
stream:write("\n")
end
for sel,info in pairsKeySorted(self.instanceMethods) do
stream:write(self:formatAutocompleteSelector(info.selector, info.className, false))
stream:write("\n")
end
stream:close()
end
function AutocompleteGenerator:collateAutocompleteForClass(class)
self:collateAutocompleteForMethods(class, true)
self:collateAutocompleteForMethods(class, false)
self:collateAutocompleteForProperties(class)
end
function AutocompleteGenerator:collateAutocompleteForMethods(class, isClass)
local cls=_G[class]
if isClass then
cls=cls:bhClass()
end
local selectors=cls:bhMethodSelectors()
for i,selector in ipairs(selectors) do
self:collateAutocompleteForMethod(selector, cls, class, isClass)
end
end
function AutocompleteGenerator:collateAutocompleteForProperties(class)
local cls=_G[class]
local properties=cls:bhPropertyNames()
for i,property in ipairs(properties) do
self:collateAutocompleteForMethod(property, cls, class, false, self.instanceMethods)
self:collateAutocompleteForMethod("set"..(property:gsub("^%l", string.upper)), cls, class, false, methods)
end
end
function AutocompleteGenerator:collateAutocompleteForMethod(selector, cls, class, isClassMethod)
if not(selector:find("^_")) then
local key=selector
local methods=self.instanceMethods
if isClassMethod then
key=class..":"..key
methods=self.classMethods
end
methods[key:upper()]={className=class, selector=selector, isClassMethod=isClassMethod}
end
end
function AutocompleteGenerator:formatAutocompleteSelector(selector, className, isClassMethod)
if not(selector:find("^_")) then
-- Ignore hidden selectors beginning with "_"
local selStream=WriteStream.new()
local paramStream=WriteStream.new()
-- Are there any parameters?
if selector:find(":") then
local first=true
for tag in selector:gmatch("[^:]+") do
selStream:put(tag)
selStream:put("_")
if first then
-- Here's a guess for the first parameter
local param1=(tag:match(".*(%u+.*)$") or selector):lower()
paramStream:put(param1)
else
paramStream:put(",")
paramStream:put(tag)
end
first=false
end
selector=selStream:contents():match("(.*)_$")
end
local params=paramStream:contents() or ""
if isClassMethod then
return string.format("%s:%s(%s) %s", className, selector, params, className)
else
return string.format("%s(%s)", selector, params)
end
end
end
function AutocompleteGenerator:init(outputFile, inclusions, exclusions)
self.outputFile=outputFile
self:generateAutocompleteListForClassesMatching(inclusions, exclusions)
end