Class: Solargraph::ApiMap
- Inherits:
-
Object
- Object
- Solargraph::ApiMap
- Includes:
- NodeMethods
- Defined in:
- lib/solargraph/api_map.rb
Constant Summary collapse
- KEYWORDS =
[ '__ENCODING__', '__LINE__', '__FILE__', 'BEGIN', 'END', 'alias', 'and', 'begin', 'break', 'case', 'class', 'def', 'defined?', 'do', 'else', 'elsif', 'end', 'ensure', 'false', 'for', 'if', 'in', 'module', 'next', 'nil', 'not', 'or', 'redo', 'rescue', 'retry', 'return', 'self', 'super', 'then', 'true', 'undef', 'unless', 'until', 'when', 'while', 'yield' ]
- MAPPABLE_METHODS =
[ :include, :extend, :require, :autoload, :attr_reader, :attr_writer, :attr_accessor, :private, :public, :protected, :solargraph_include_public_methods ]
Instance Attribute Summary collapse
-
#required ⇒ Object
readonly
Returns the value of attribute required.
-
#workspace ⇒ Object
readonly
Returns the value of attribute workspace.
Class Method Summary collapse
Instance Method Summary collapse
- #append_file(filename) ⇒ Object
- #append_node(node, comments, filename = nil) ⇒ Object
- #append_source(text, filename = nil) ⇒ Object
- #associate_comments(node, comments) ⇒ Object
- #clear ⇒ Object
- #find_fully_qualified_namespace(name, root = '', skip = []) ⇒ Object
- #find_instance_variable_assignment(var, node, scope) ⇒ Object
- #find_parent(node, *types) ⇒ Object
- #get_comment_for(node) ⇒ Object
- #get_filename_for(node) ⇒ Object
- #get_global_variables ⇒ Object
- #get_include_strings_from(*nodes) ⇒ Object
- #get_instance_methods(namespace, root = '', visibility: [:public]) ⇒ Object
- #get_instance_variables(namespace, scope = :instance) ⇒ Object
- #get_method_args(node) ⇒ Object
- #get_method_return_value(namespace, root, method, scope = :instance) ⇒ Object
- #get_methods(namespace, root = '', visibility: [:public]) ⇒ Object
- #get_namespace_nodes(fqns) ⇒ Object
- #get_namespace_type(namespace, root = '') ⇒ Object
- #get_root_for(node) ⇒ Object
- #get_superclass(namespace, root = '') ⇒ Object
- #infer_instance_variable(var, namespace, scope = :instance) ⇒ Object
-
#infer_signature_type(signature, namespace, scope: :instance) ⇒ String
Get a fully qualified namespace for the given signature.
-
#initialize(workspace = nil) ⇒ ApiMap
constructor
A new instance of ApiMap.
- #inner_get_instance_variables(node, scope) ⇒ Object
- #inner_namespaces_in(name, root, skip) ⇒ Object
- #namespace_exists?(name, root = '') ⇒ Boolean
- #namespaces ⇒ Object
- #namespaces_in(name, root = '') ⇒ Object
- #options ⇒ Object
- #process_maps ⇒ Object
- #yard_map ⇒ Object
Methods included from NodeMethods
#infer, #pack_name, #resolve_node_signature, #stack_node_signature, #unpack_name
Constructor Details
#initialize(workspace = nil) ⇒ ApiMap
Returns a new instance of ApiMap.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/solargraph/api_map.rb', line 26 def initialize workspace = nil @workspace = workspace clear unless @workspace.nil? extra = File.join(workspace, '.solargraph') if File.exist?(extra) append_file(extra) end files = [] opts = (opts[:include] - opts[:exclude]).each { |glob| files += Dir[File.join @workspace, glob] } opts[:exclude].each { |glob| files -= Dir[File.join @workspace, glob] } files.uniq.each { |f| append_file f #unless yardoc_has_file?(f) } end end |
Instance Attribute Details
#required ⇒ Object (readonly)
Returns the value of attribute required.
24 25 26 |
# File 'lib/solargraph/api_map.rb', line 24 def required @required end |
#workspace ⇒ Object (readonly)
Returns the value of attribute workspace.
23 24 25 |
# File 'lib/solargraph/api_map.rb', line 23 def workspace @workspace end |
Class Method Details
.current ⇒ Object
460 461 462 463 464 465 466 |
# File 'lib/solargraph/api_map.rb', line 460 def self.current if @current.nil? @current = ApiMap.new @current.merge(Parser::CurrentRuby.parse(File.read("#{Solargraph::STUB_PATH}/ruby/2.3.0/core.rb"))) end @current end |
.get_keywords(without_snippets: false) ⇒ Object
122 123 124 125 126 127 128 129 130 |
# File 'lib/solargraph/api_map.rb', line 122 def self.get_keywords without_snippets: false result = [] keywords = KEYWORDS keywords -= Snippets.keywords if without_snippets keywords.each { |k| result.push Suggestion.new(k, kind: Suggestion::KEYWORD, detail: 'Keyword') } result end |
Instance Method Details
#append_file(filename) ⇒ Object
82 83 84 |
# File 'lib/solargraph/api_map.rb', line 82 def append_file filename append_source File.read(filename), filename end |
#append_node(node, comments, filename = nil) ⇒ Object
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/solargraph/api_map.rb', line 91 def append_node node, comments, filename = nil @file_comments[filename] = associate_comments(node, comments) mapified = reduce(node, @file_comments[filename]) root = AST::Node.new(:begin, [filename]) root = root.append mapified @file_nodes[filename] = root @required.uniq! process_maps root end |
#append_source(text, filename = nil) ⇒ Object
86 87 88 89 |
# File 'lib/solargraph/api_map.rb', line 86 def append_source text, filename = nil node, comments = Parser::CurrentRuby.parse_with_comments(text) append_node(node, comments, filename) end |
#associate_comments(node, comments) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/solargraph/api_map.rb', line 102 def associate_comments node, comments comment_hash = Parser::Source::Comment.associate_locations(node, comments) yard_hash = {} comment_hash.each_pair { |k, v| ctxt = '' v.each { |l| ctxt += l.text.gsub(/^# /, '') + "\n" } parser = YARD::DocstringParser.new yard_hash[k] = parser.parse(ctxt).to_docstring } yard_hash end |
#clear ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/solargraph/api_map.rb', line 48 def clear @file_nodes = {} @file_comments = {} @parent_stack = {} @namespace_map = {} @namespace_tree = {} @required = [] end |
#find_fully_qualified_namespace(name, root = '', skip = []) ⇒ Object
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 |
# File 'lib/solargraph/api_map.rb', line 203 def find_fully_qualified_namespace name, root = '', skip = [] return nil if skip.include?(root) skip.push root if name == '' if root == '' return '' else return find_fully_qualified_namespace(root, '', skip) end else if (root == '') return name unless @namespace_map[name].nil? get_include_strings_from(*@file_nodes.values).each { |i| reroot = "#{root == '' ? '' : root + '::'}#{i}" recname = find_fully_qualified_namespace name.to_s, reroot, skip return recname unless recname.nil? } else roots = root.to_s.split('::') while roots.length > 0 fqns = roots.join('::') + '::' + name return fqns unless @namespace_map[fqns].nil? roots.pop end return name unless @namespace_map[name].nil? get_include_strings_from(*@file_nodes.values).each { |i| recname = find_fully_qualified_namespace name, i, skip return recname unless recname.nil? } end end yard_map.find_fully_qualified_namespace(name, root) end |
#find_instance_variable_assignment(var, node, scope) ⇒ Object
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/solargraph/api_map.rb', line 312 def find_instance_variable_assignment(var, node, scope) node.children.each { |c| if c.kind_of?(AST::Node) is_inst = !find_parent(c, :def).nil? if c.type == :ivasgn and ( (scope == :instance and is_inst) or (scope != :instance and !is_inst) ) if c.children[0].to_s == var return c end else inner = find_instance_variable_assignment(var, c, scope) return inner unless inner.nil? end end } nil end |
#find_parent(node, *types) ⇒ Object
251 252 253 254 255 256 257 |
# File 'lib/solargraph/api_map.rb', line 251 def find_parent(node, *types) parents = @parent_stack[node] parents.each { |p| return p if types.include?(p.type) } nil end |
#get_comment_for(node) ⇒ Object
116 117 118 119 120 |
# File 'lib/solargraph/api_map.rb', line 116 def get_comment_for node filename = get_filename_for(node) return nil if @file_comments[filename].nil? @file_comments[filename][node.loc] end |
#get_filename_for(node) ⇒ Object
266 267 268 269 |
# File 'lib/solargraph/api_map.rb', line 266 def get_filename_for(node) root = get_root_for(node) root.nil? ? nil : root.children[0] end |
#get_global_variables ⇒ Object
329 330 331 332 |
# File 'lib/solargraph/api_map.rb', line 329 def get_global_variables # TODO: Get them [] end |
#get_include_strings_from(*nodes) ⇒ Object
468 469 470 471 472 473 474 475 476 477 478 |
# File 'lib/solargraph/api_map.rb', line 468 def get_include_strings_from *nodes arr = [] nodes.each { |node| next unless node.kind_of?(AST::Node) arr.push unpack_name(node.children[2]) if (node.type == :send and node.children[1] == :include) node.children.each { |n| arr += get_include_strings_from(n) if n.kind_of?(AST::Node) and n.type != :class and n.type != :module } } arr end |
#get_instance_methods(namespace, root = '', visibility: [:public]) ⇒ Object
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 |
# File 'lib/solargraph/api_map.rb', line 423 def get_instance_methods(namespace, root = '', visibility: [:public]) meths = [] meths += inner_get_instance_methods(namespace, root, []) #unless has_yardoc? fqns = find_fully_qualified_namespace(namespace, root) yard_meths = yard_map.get_instance_methods(fqns, '', visibility: visibility) if yard_meths.any? meths.concat yard_meths else type = get_namespace_type(namespace, root) if type == :class meths += yard_map.get_instance_methods('Object') elsif type == :module meths += yard_map.get_instance_methods('Module') end # TODO: Look out for repeats. Consider not doing this at all. #sc = get_superclass(namespace, root) #until sc.nil? # meths += yard.get_instance_methods(sc, root) # sc = get_superclass(sc) #end end meths end |
#get_instance_variables(namespace, scope = :instance) ⇒ Object
242 243 244 245 246 247 248 249 |
# File 'lib/solargraph/api_map.rb', line 242 def get_instance_variables(namespace, scope = :instance) nodes = get_namespace_nodes(namespace) || @file_nodes.values arr = [] nodes.each { |n| arr += inner_get_instance_variables(n, scope) } arr end |
#get_method_args(node) ⇒ Object
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 |
# File 'lib/solargraph/api_map.rb', line 405 def get_method_args node list = nil args = [] node.children.each { |c| if c.kind_of?(AST::Node) and c.type == :args list = c break end } return args if list.nil? list.children.each { |c| if c.type == :arg args.push c.children[0] end } args end |
#get_method_return_value(namespace, root, method, scope = :instance) ⇒ Object
369 370 371 372 373 374 375 376 |
# File 'lib/solargraph/api_map.rb', line 369 def get_method_return_value namespace, root, method, scope = :instance meths = get_methods(namespace, root).delete_if{ |m| m.insert != method } meths.each { |m| r = get_return_tag(m) return r unless r.nil? } nil end |
#get_methods(namespace, root = '', visibility: [:public]) ⇒ Object
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
# File 'lib/solargraph/api_map.rb', line 388 def get_methods(namespace, root = '', visibility: [:public]) meths = [] meths += inner_get_methods(namespace, root, []) #unless has_yardoc? yard_meths = yard_map.get_methods(namespace, root, visibility: visibility) if yard_meths.any? meths.concat yard_meths else type = get_namespace_type(namespace, root) if type == :class meths += yard_map.get_instance_methods('Class') elsif type == :module meths += yard_map.get_methods('Module') end meths end end |
#get_namespace_nodes(fqns) ⇒ Object
237 238 239 240 |
# File 'lib/solargraph/api_map.rb', line 237 def get_namespace_nodes(fqns) return @file_nodes.values if fqns == '' or fqns.nil? @namespace_map[fqns] || [] end |
#get_namespace_type(namespace, root = '') ⇒ Object
378 379 380 381 382 383 384 385 386 |
# File 'lib/solargraph/api_map.rb', line 378 def get_namespace_type namespace, root = '' type = nil fqns = find_fully_qualified_namespace(namespace, root) nodes = get_namespace_nodes(fqns) unless nodes.nil? or nodes.empty? or !nodes[0].kind_of?(AST::Node) type = nodes[0].type if [:class, :module].include?(nodes[0].type) end type end |
#get_root_for(node) ⇒ Object
259 260 261 262 263 264 |
# File 'lib/solargraph/api_map.rb', line 259 def get_root_for(node) s = @parent_stack[node] return nil if s.nil? return node if s.empty? s.last end |
#get_superclass(namespace, root = '') ⇒ Object
447 448 449 450 451 452 453 454 455 456 457 458 |
# File 'lib/solargraph/api_map.rb', line 447 def get_superclass(namespace, root = '') fqns = find_fully_qualified_namespace(namespace, root) nodes = get_namespace_nodes(fqns) nodes.each { |n| if n.kind_of?(AST::Node) if n.type == :class and !n.children[1].nil? return unpack_name(n.children[1]) end end } return nil end |
#infer_instance_variable(var, namespace, scope = :instance) ⇒ Object
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/solargraph/api_map.rb', line 287 def infer_instance_variable(var, namespace, scope = :instance) result = nil vn = nil fqns = find_fully_qualified_namespace(namespace) unless fqns.nil? get_namespace_nodes(fqns).each { |node| vn = find_instance_variable_assignment(var, node, scope) break unless vn.nil? } end unless vn.nil? cmnt = get_comment_for(vn) unless cmnt.nil? tag = cmnt.tag(:type) result = tag.types[0] unless tag.nil? or tag.types.empty? end result = infer(vn.children[1]) if result.nil? if result.nil? signature = resolve_node_signature(vn.children[1]) result = infer_signature_type(signature, namespace || '', scope: scope) end end result end |
#infer_signature_type(signature, namespace, scope: :instance) ⇒ String
Get a fully qualified namespace for the given signature. The signature should be in the form of a method chain, e.g., method1.method2
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 |
# File 'lib/solargraph/api_map.rb', line 340 def infer_signature_type signature, namespace, scope: :instance parts = signature.split('.') type = find_fully_qualified_namespace(namespace) type ||= '' top = true while parts.length > 0 and !type.nil? p = parts.shift unless p == 'new' and scope != :instance if scope == :instance meths = get_instance_methods(type) meths += get_methods('') if top else meths = get_methods(type) #meths += get_methods('') if top end meths.delete_if{ |m| m.insert != p } return nil if meths.empty? type = nil unless meths[0].documentation.nil? match = meths[0].documentation.all.match(/@return \[([a-z0-9:_]*)/i) type = find_fully_qualified_namespace(match[1]) unless match.nil? end end scope = :instance top = false end type end |
#inner_get_instance_variables(node, scope) ⇒ Object
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/solargraph/api_map.rb', line 271 def inner_get_instance_variables(node, scope) arr = [] if node.kind_of?(AST::Node) node.children.each { |c| if c.kind_of?(AST::Node) is_inst = !find_parent(c, :def).nil? if c.type == :ivasgn and c.children[0] and ( (scope == :instance and is_inst) or (scope != :instance and !is_inst) ) arr.push Suggestion.new(c.children[0], kind: Suggestion::VARIABLE, documentation: get_comment_for(c)) end arr += inner_get_instance_variables(c, scope) unless [:class, :module].include?(c.type) end } end arr end |
#inner_namespaces_in(name, root, skip) ⇒ Object
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 |
# File 'lib/solargraph/api_map.rb', line 164 def inner_namespaces_in name, root, skip result = [] fqns = find_fully_qualified_namespace(name, root) unless fqns.nil? or skip.include?(fqns) skip.push fqns nodes = get_namespace_nodes(fqns) #nodes.delete_if { |n| yardoc_has_file?(get_filename_for(n))} unless nodes.empty? cursor = @namespace_tree parts = fqns.split('::') parts.each { |p| cursor = cursor[p] } unless cursor.nil? cursor.keys.each { |k| type = get_namespace_type(k, fqns) kind = nil detail = nil if type == :class kind = Suggestion::CLASS detail = 'Class' elsif type == :module kind = Suggestion::MODULE detail = 'Module' end result.push Suggestion.new(k, kind: kind, detail: detail) } nodes = get_namespace_nodes(fqns) nodes.each { |n| get_include_strings_from(n).each { |i| result += inner_namespaces_in(i, fqns, skip) } } end end end result end |
#namespace_exists?(name, root = '') ⇒ Boolean
146 147 148 |
# File 'lib/solargraph/api_map.rb', line 146 def namespace_exists? name, root = '' !find_fully_qualified_namespace(name, root).nil? end |
#namespaces ⇒ Object
142 143 144 |
# File 'lib/solargraph/api_map.rb', line 142 def namespaces @namespace_map.keys end |
#namespaces_in(name, root = '') ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/solargraph/api_map.rb', line 150 def namespaces_in name, root = '' result = [] result += inner_namespaces_in(name, root, []) result += yard_map.get_constants name, root fqns = find_fully_qualified_namespace(name, root) unless fqns.nil? nodes = get_namespace_nodes(fqns) get_include_strings_from(*nodes).each { |i| result += yard_map.get_constants(i, root) } end result end |
#options ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/solargraph/api_map.rb', line 57 def o = { include: [], exclude: [] } unless workspace.nil? yardopts_file = File.join(workspace, '.yardopts') if File.exist?(yardopts_file) yardopts = File.read(yardopts_file) yardopts.lines.each { |line| arg = line.strip if !arg.start_with?('-') o[:include].push arg end } end end o[:include].concat ['app/**/*.rb', 'lib/**/*.rb'] if o[:include].empty? o end |
#process_maps ⇒ Object
132 133 134 135 136 137 138 139 140 |
# File 'lib/solargraph/api_map.rb', line 132 def process_maps @parent_stack = {} @namespace_map = {} @namespace_tree = {} @file_nodes.values.each { |f| map_parents f map_namespaces f } end |