-
Notifications
You must be signed in to change notification settings - Fork 0
/
ri-emacs.rb
312 lines (268 loc) · 8.57 KB
/
ri-emacs.rb
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
## ri-emacs.rb helper script for use with ri-ruby.el
#
# Author: Kristof Bastiaensen <[email protected]>
#
#
# Copyright (C) 2004,2006 Kristof Bastiaensen
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#----------------------------------------------------------------------
#
# For information on how to use and install see ri-ruby.el
#
require 'rdoc/ri/ri_paths'
require 'rdoc/ri/ri_cache'
require 'rdoc/ri/ri_util'
require 'rdoc/ri/ri_reader'
require 'rdoc/ri/ri_formatter'
require 'rdoc/ri/ri_display'
class DefaultDisplay
def full_params(method)
method.params.split(/\n/).each do |p|
p.sub!(/^#{method.name}\(/o,'(')
unless p =~ /\b\.\b/
p = method.full_name + p
end
@formatter.wrap(p)
@formatter.break_to_newline
end
end
end
def ruby_minimum_version?(*version)
VERSION.split(".").
map{ |i| i.to_i }.
<=>(version) > -1
end
class RiEmacs
Options = Struct.new(:formatter, :use_stdout, :width)
def initialize(paths)
options = Options.new
options.use_stdout = true
options.formatter = RI::TextFormatter.for("ansi")
options.width = 72
if ruby_minimum_version?(1, 8, 5)
begin
require 'rubygems'
Dir["#{Gem.path}/doc/*/ri"].each do |path|
RI::Paths::PATH << path
end
rescue LoadError
end
end
paths = paths || RI::Paths::PATH
@ri_reader = RI::RiReader.new(RI::RiCache.new(paths))
@display = RiDisplay.new(options)
end
def lookup_keyw(keyw)
begin
desc = NameDescriptor.new(keyw)
rescue RiError => e
return nil
end
last_space = @ri_reader.top_level_namespace
class_name = nil
container = desc.class_names.inject(last_space) do
|container, class_name|
last_space = @ri_reader.lookup_namespace_in(class_name, container)
last_space.find_all {|m| m.name == class_name}
end
if desc.method_name.nil?
if [?., ?:, ?#].include? keyw[-1]
namespaces = @ri_reader.lookup_namespace_in("", container)
is_class_method = case keyw[-1]
when ?.: nil
when ?:: true
when ?#: false
end
methods = @ri_reader.find_methods("", is_class_method,
container)
return nil if methods.empty? && namespaces.empty?
else
#class_name = desc.class_names.last
namespaces = last_space.find_all{ |n| n.name.index(class_name).zero? }
return nil if namespaces.empty?
methods = []
end
else
return nil if container.empty?
namespaces = []
methods = @ri_reader.
find_methods(desc.method_name,
desc.is_class_method,
container).
find_all { |m| m.name.index(desc.method_name).zero? }
return nil if methods.empty?
end
return desc, methods, namespaces
end
def completion_list(keyw)
return @ri_reader.full_class_names if keyw == ""
desc, methods, namespaces = lookup_keyw(keyw)
return nil unless desc
if desc.class_names.empty?
return methods.map { |m| m.name }.uniq
else
return methods.map { |m| m.full_name } +
namespaces.map { |n| n.full_name }
end
end
def complete(keyw, type)
list = completion_list(keyw)
if list.nil?
return "nil"
elsif type == :all
return "(" + list.map { |w| w.inspect }.join(" ") + ")"
elsif type == :lambda
if list.find { |n|
n.split(/(::)|#|\./) == keyw.split(/(::)|#|\./) }
return "t"
else
return "nil"
end
# type == try
elsif list.size == 1 and
list[0].split(/(::)|#|\./) == keyw.split(/(::)|#|\./)
return "t"
end
first = list.shift;
if first =~ /(.*)((?:::)|(?:#))(.*)/
other = $1 + ($2 == "::" ? "#" : "::") + $3
end
len = first.size
match_both = false
list.each do |w|
while w[0, len] != first[0, len]
if other and w[0, len] == other[0, len]
match_both = true
break
end
len -= 1
end
end
if match_both
return other.sub(/(.*)((?:::)|(?:#))/) {
$1 + "." }[0, len].inspect
else
return first[0, len].inspect
end
end
def display_info(keyw)
desc, methods, namespaces = lookup_keyw(keyw)
return false if desc.nil?
if desc.method_name
methods = methods.find_all { |m| m.name == desc.method_name }
return false if methods.empty?
meth = @ri_reader.get_method(methods[0])
@display.display_method_info(meth)
else
namespaces = namespaces.find_all { |n| n.full_name == desc.full_class_name }
return false if namespaces.empty?
klass = @ri_reader.get_class(namespaces[0])
@display.display_class_info(klass, @ri_reader)
end
return true
end
def display_args(keyw)
desc, methods, namespaces = lookup_keyw(keyw)
return nil unless desc && desc.class_names.empty?
methods = methods.find_all { |m| m.name == desc.method_name }
return false if methods.empty?
methods.each do |m|
meth = @ri_reader.get_method(m)
@display.full_params(meth)
end
return true
end
# return a list of classes for the method keyw
# return nil if keyw has already a class
def class_list(keyw, rep='\1')
desc, methods, namespaces = lookup_keyw(keyw)
return nil unless desc && desc.class_names.empty?
methods = methods.find_all { |m| m.name == desc.method_name }
return "(" + methods.map do |m|
"(" + m.full_name.sub(/(.*)(#|(::)).*/,
rep).inspect + ")"
end.uniq.join(" ") + ")"
end
# flag means (#|::)
# return a list of classes and flag for the method keyw
# return nil if keyw has already a class
def class_list_with_flag(keyw)
class_list(keyw, '\1\2')
end
end
class Command
def initialize(ri)
@ri = ri
end
Command2Method = {
"TRY_COMPLETION" => :try_completion,
"COMPLETE_ALL" => :complete_all,
"LAMBDA" => :lambda,
"CLASS_LIST" => :class_list,
"CLASS_LIST_WITH_FLAG" => :class_list_with_flag,
"DISPLAY_ARGS" => :display_args,
"DISPLAY_INFO" => :display_info}
def read_next
line = STDIN.gets
cmd, param = /(\w+)(.*)$/.match(line)[1..2]
method = Command2Method[cmd]
fail "unrecognised command: #{cmd}" if method.nil?
send(method, param.strip)
STDOUT.flush
end
def try_completion(keyw)
STDOUT.puts @ri.complete(keyw, :try)
end
def complete_all(keyw)
STDOUT.puts @ri.complete(keyw, :all)
end
def lambda(keyw)
STDOUT.puts @ri.complete(keyw, :lambda)
end
def class_list(keyw)
STDOUT.puts @ri.class_list(keyw)
end
def class_list_with_flag(keyw)
STDOUT.puts @ri.class_list_with_flag(keyw)
end
def display_args(keyw)
@ri.display_args(keyw)
STDOUT.puts "RI_EMACS_END_OF_INFO"
end
def display_info(keyw)
@ri.display_info(keyw)
STDOUT.puts "RI_EMACS_END_OF_INFO"
end
def test
[:try, :all, :lambda].each do |t|
@ri.complete("each", t) or
fail "@ri.complete(\"each\", #{t.inspect}) returned nil"
end
@ri.display_info("Array#each") or
raise 'display_info("Array#each") returned false'
end
end
arg = ARGV[0]
if arg == "--test"
cmd = Command.new(RiEmacs.new(nil))
cmd.test
puts "Test succeeded"
else
cmd = Command.new(RiEmacs.new(arg))
STDOUT.puts 'READY'
STDOUT.flush
loop { cmd.read_next }
end