solargraph 0.38.6 → 0.39.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/SPONSORS.md +9 -0
- data/lib/solargraph.rb +1 -0
- data/lib/solargraph/api_map.rb +24 -13
- data/lib/solargraph/api_map/bundler_methods.rb +3 -0
- data/lib/solargraph/api_map/source_to_yard.rb +1 -6
- data/lib/solargraph/api_map/store.rb +19 -3
- data/lib/solargraph/complex_type/type_methods.rb +4 -4
- data/lib/solargraph/convention/base.rb +0 -5
- data/lib/solargraph/core_fills.rb +28 -2
- data/lib/solargraph/diagnostics/base.rb +1 -0
- data/lib/solargraph/diagnostics/type_check.rb +13 -13
- data/lib/solargraph/language_server/host.rb +11 -2
- data/lib/solargraph/language_server/host/sources.rb +20 -11
- data/lib/solargraph/language_server/message/text_document/formatting.rb +16 -11
- data/lib/solargraph/language_server/transport/data_reader.rb +2 -0
- data/lib/solargraph/library.rb +3 -3
- data/lib/solargraph/parser.rb +26 -0
- data/lib/solargraph/parser/comment_ripper.rb +52 -0
- data/lib/solargraph/parser/legacy.rb +12 -0
- data/lib/solargraph/parser/legacy/class_methods.rb +105 -0
- data/lib/solargraph/parser/legacy/flawed_builder.rb +16 -0
- data/lib/solargraph/parser/legacy/node_chainer.rb +115 -0
- data/lib/solargraph/parser/legacy/node_methods.rb +289 -0
- data/lib/solargraph/parser/legacy/node_processors.rb +54 -0
- data/lib/solargraph/parser/legacy/node_processors/alias_node.rb +23 -0
- data/lib/solargraph/parser/legacy/node_processors/args_node.rb +35 -0
- data/lib/solargraph/parser/legacy/node_processors/begin_node.rb +15 -0
- data/lib/solargraph/parser/legacy/node_processors/block_node.rb +22 -0
- data/lib/solargraph/parser/legacy/node_processors/casgn_node.rb +25 -0
- data/lib/solargraph/parser/legacy/node_processors/cvasgn_node.rb +23 -0
- data/lib/solargraph/parser/legacy/node_processors/def_node.rb +63 -0
- data/lib/solargraph/parser/legacy/node_processors/defs_node.rb +36 -0
- data/lib/solargraph/parser/legacy/node_processors/gvasgn_node.rb +23 -0
- data/lib/solargraph/parser/legacy/node_processors/ivasgn_node.rb +38 -0
- data/lib/solargraph/parser/legacy/node_processors/lvasgn_node.rb +28 -0
- data/lib/solargraph/parser/legacy/node_processors/namespace_node.rb +39 -0
- data/lib/solargraph/parser/legacy/node_processors/orasgn_node.rb +16 -0
- data/lib/solargraph/parser/legacy/node_processors/resbody_node.rb +36 -0
- data/lib/solargraph/parser/legacy/node_processors/sclass_node.rb +21 -0
- data/lib/solargraph/parser/legacy/node_processors/send_node.rb +232 -0
- data/lib/solargraph/parser/legacy/node_processors/sym_node.rb +18 -0
- data/lib/solargraph/parser/node_methods.rb +43 -0
- data/lib/solargraph/parser/node_processor.rb +43 -0
- data/lib/solargraph/parser/node_processor/base.rb +77 -0
- data/lib/solargraph/{source_map → parser}/region.rb +11 -6
- data/lib/solargraph/parser/rubyvm.rb +40 -0
- data/lib/solargraph/parser/rubyvm/class_methods.rb +146 -0
- data/lib/solargraph/parser/rubyvm/node_chainer.rb +133 -0
- data/lib/solargraph/parser/rubyvm/node_methods.rb +279 -0
- data/lib/solargraph/parser/rubyvm/node_processors.rb +60 -0
- data/lib/solargraph/parser/rubyvm/node_processors/alias_node.rb +23 -0
- data/lib/solargraph/parser/rubyvm/node_processors/args_node.rb +62 -0
- data/lib/solargraph/parser/rubyvm/node_processors/begin_node.rb +15 -0
- data/lib/solargraph/parser/rubyvm/node_processors/block_node.rb +22 -0
- data/lib/solargraph/parser/rubyvm/node_processors/casgn_node.rb +22 -0
- data/lib/solargraph/parser/rubyvm/node_processors/cvasgn_node.rb +23 -0
- data/lib/solargraph/parser/rubyvm/node_processors/def_node.rb +64 -0
- data/lib/solargraph/parser/rubyvm/node_processors/defs_node.rb +57 -0
- data/lib/solargraph/parser/rubyvm/node_processors/gvasgn_node.rb +23 -0
- data/lib/solargraph/parser/rubyvm/node_processors/ivasgn_node.rb +38 -0
- data/lib/solargraph/parser/rubyvm/node_processors/kw_arg_node.rb +34 -0
- data/lib/solargraph/parser/rubyvm/node_processors/lit_node.rb +20 -0
- data/lib/solargraph/parser/rubyvm/node_processors/lvasgn_node.rb +27 -0
- data/lib/solargraph/parser/rubyvm/node_processors/namespace_node.rb +39 -0
- data/lib/solargraph/parser/rubyvm/node_processors/opt_arg_node.rb +26 -0
- data/lib/solargraph/parser/rubyvm/node_processors/orasgn_node.rb +15 -0
- data/lib/solargraph/parser/rubyvm/node_processors/resbody_node.rb +45 -0
- data/lib/solargraph/parser/rubyvm/node_processors/sclass_node.rb +21 -0
- data/lib/solargraph/parser/rubyvm/node_processors/scope_node.rb +15 -0
- data/lib/solargraph/parser/rubyvm/node_processors/send_node.rb +292 -0
- data/lib/solargraph/parser/rubyvm/node_processors/sym_node.rb +18 -0
- data/lib/solargraph/parser/snippet.rb +13 -0
- data/lib/solargraph/pin/attribute.rb +8 -0
- data/lib/solargraph/pin/base.rb +1 -1
- data/lib/solargraph/pin/base_method.rb +26 -4
- data/lib/solargraph/pin/base_variable.rb +7 -8
- data/lib/solargraph/pin/block.rb +1 -1
- data/lib/solargraph/pin/constant.rb +16 -0
- data/lib/solargraph/pin/conversions.rb +2 -2
- data/lib/solargraph/pin/method.rb +26 -14
- data/lib/solargraph/pin/parameter.rb +56 -2
- data/lib/solargraph/pin/reference.rb +1 -0
- data/lib/solargraph/pin/reference/override.rb +2 -0
- data/lib/solargraph/pin/reference/prepend.rb +10 -0
- data/lib/solargraph/pin/yard_pin/constant.rb +2 -4
- data/lib/solargraph/pin/yard_pin/method.rb +34 -18
- data/lib/solargraph/pin/yard_pin/namespace.rb +3 -11
- data/lib/solargraph/pin/yard_pin/yard_mixin.rb +3 -12
- data/lib/solargraph/range.rb +8 -2
- data/lib/solargraph/shell.rb +17 -11
- data/lib/solargraph/source.rb +114 -135
- data/lib/solargraph/source/chain.rb +11 -3
- data/lib/solargraph/source/chain/call.rb +1 -2
- data/lib/solargraph/source/chain/constant.rb +44 -8
- data/lib/solargraph/source/chain/link.rb +1 -0
- data/lib/solargraph/source/cursor.rb +2 -28
- data/lib/solargraph/source/source_chainer.rb +12 -6
- data/lib/solargraph/source/updater.rb +2 -0
- data/lib/solargraph/source_map.rb +0 -2
- data/lib/solargraph/source_map/clip.rb +24 -17
- data/lib/solargraph/source_map/mapper.rb +34 -29
- data/lib/solargraph/type_checker.rb +367 -275
- data/lib/solargraph/type_checker/checks.rb +95 -0
- data/lib/solargraph/type_checker/param_def.rb +2 -17
- data/lib/solargraph/type_checker/rules.rb +53 -0
- data/lib/solargraph/version.rb +1 -1
- data/lib/solargraph/views/environment.erb +5 -3
- data/lib/solargraph/workspace.rb +17 -2
- data/lib/solargraph/workspace/config.rb +15 -7
- data/lib/solargraph/yard_map.rb +89 -49
- data/lib/solargraph/yard_map/core_docs.rb +1 -1
- data/solargraph.gemspec +2 -1
- metadata +88 -32
- data/OVERVIEW.md +0 -37
- data/lib/solargraph/source/flawed_builder.rb +0 -15
- data/lib/solargraph/source/node_chainer.rb +0 -111
- data/lib/solargraph/source/node_methods.rb +0 -240
- data/lib/solargraph/source_map/node_processor.rb +0 -85
- data/lib/solargraph/source_map/node_processor/alias_node.rb +0 -21
- data/lib/solargraph/source_map/node_processor/args_node.rb +0 -24
- data/lib/solargraph/source_map/node_processor/base.rb +0 -103
- data/lib/solargraph/source_map/node_processor/begin_node.rb +0 -13
- data/lib/solargraph/source_map/node_processor/block_node.rb +0 -21
- data/lib/solargraph/source_map/node_processor/casgn_node.rb +0 -21
- data/lib/solargraph/source_map/node_processor/cvasgn_node.rb +0 -21
- data/lib/solargraph/source_map/node_processor/def_node.rb +0 -62
- data/lib/solargraph/source_map/node_processor/defs_node.rb +0 -33
- data/lib/solargraph/source_map/node_processor/gvasgn_node.rb +0 -21
- data/lib/solargraph/source_map/node_processor/ivasgn_node.rb +0 -34
- data/lib/solargraph/source_map/node_processor/lvasgn_node.rb +0 -24
- data/lib/solargraph/source_map/node_processor/namespace_node.rb +0 -35
- data/lib/solargraph/source_map/node_processor/orasgn_node.rb +0 -14
- data/lib/solargraph/source_map/node_processor/resbody_node.rb +0 -32
- data/lib/solargraph/source_map/node_processor/sclass_node.rb +0 -19
- data/lib/solargraph/source_map/node_processor/send_node.rb +0 -217
- data/lib/solargraph/source_map/node_processor/sym_node.rb +0 -16
@@ -36,17 +36,22 @@ module Solargraph
|
|
36
36
|
# @param result [String]
|
37
37
|
# @return [void]
|
38
38
|
def format original, result
|
39
|
-
if original.end_with?("\n")
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
39
|
+
ending = if original.end_with?("\n")
|
40
|
+
{
|
41
|
+
line: original.lines.length,
|
42
|
+
character: 0
|
43
|
+
}
|
44
|
+
elsif original.lines.empty?
|
45
|
+
{
|
46
|
+
line: 0,
|
47
|
+
character: 0
|
48
|
+
}
|
49
|
+
else
|
50
|
+
{
|
51
|
+
line: original.lines.length - 1,
|
52
|
+
character: original.lines.last.length
|
53
|
+
}
|
54
|
+
end
|
50
55
|
set_result(
|
51
56
|
[
|
52
57
|
{
|
@@ -39,6 +39,7 @@ module Solargraph
|
|
39
39
|
|
40
40
|
private
|
41
41
|
|
42
|
+
# @return [void]
|
42
43
|
def prepare_to_parse_message
|
43
44
|
@in_header = false
|
44
45
|
@buffer.each_line do |line|
|
@@ -51,6 +52,7 @@ module Solargraph
|
|
51
52
|
@buffer.clear
|
52
53
|
end
|
53
54
|
|
55
|
+
# @return [void]
|
54
56
|
def parse_message_from_buffer
|
55
57
|
begin
|
56
58
|
msg = JSON.parse(@buffer)
|
data/lib/solargraph/library.rb
CHANGED
@@ -224,10 +224,10 @@ module Solargraph
|
|
224
224
|
result.uniq
|
225
225
|
end
|
226
226
|
|
227
|
-
# Get the
|
227
|
+
# Get the pins at the specified location or nil if the pin does not exist.
|
228
228
|
#
|
229
229
|
# @param location [Location]
|
230
|
-
# @return [Solargraph::Pin::Base]
|
230
|
+
# @return [Array<Solargraph::Pin::Base>]
|
231
231
|
def locate_pins location
|
232
232
|
api_map.locate_pins(location).map { |pin| pin.realize(api_map) }
|
233
233
|
end
|
@@ -369,7 +369,7 @@ module Solargraph
|
|
369
369
|
# @param source [Source]
|
370
370
|
# @return [Boolean] True if the source was merged into the workspace.
|
371
371
|
def merge source
|
372
|
-
result =
|
372
|
+
result = false
|
373
373
|
mutex.synchronize do
|
374
374
|
result = workspace.merge(source)
|
375
375
|
@synchronized = !result if synchronized?
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Solargraph
|
2
|
+
module Parser
|
3
|
+
autoload :CommentRipper, 'solargraph/parser/comment_ripper'
|
4
|
+
autoload :Legacy, 'solargraph/parser/legacy'
|
5
|
+
autoload :Rubyvm, 'solargraph/parser/rubyvm'
|
6
|
+
autoload :Region, 'solargraph/parser/region'
|
7
|
+
autoload :NodeProcessor, 'solargraph/parser/node_processor'
|
8
|
+
autoload :Snippet, 'solargraph/parser/snippet'
|
9
|
+
|
10
|
+
class SyntaxError < StandardError
|
11
|
+
end
|
12
|
+
|
13
|
+
# True if the parser can use RubyVM.
|
14
|
+
#
|
15
|
+
def self.rubyvm?
|
16
|
+
!!defined?(RubyVM::AbstractSyntaxTree)
|
17
|
+
# false
|
18
|
+
end
|
19
|
+
|
20
|
+
selected = rubyvm? ? Rubyvm : Legacy
|
21
|
+
# include selected
|
22
|
+
extend selected::ClassMethods
|
23
|
+
|
24
|
+
NodeMethods = (rubyvm? ? Rubyvm::NodeMethods : Legacy::NodeMethods)
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'ripper'
|
2
|
+
|
3
|
+
module Solargraph
|
4
|
+
module Parser
|
5
|
+
class CommentRipper < Ripper::SexpBuilder
|
6
|
+
def initialize src, filename = '(ripper)', lineno = 0
|
7
|
+
super
|
8
|
+
@buffer = src
|
9
|
+
@buffer_lines = @buffer.lines
|
10
|
+
end
|
11
|
+
|
12
|
+
def on_comment *args
|
13
|
+
result = super
|
14
|
+
if @buffer_lines[result[2][0]][0..result[2][1]].strip =~ /^#/
|
15
|
+
chomped = result[1].chomp
|
16
|
+
if result[2][0] == 0 && chomped.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '').match(/^#\s*frozen_string_literal:/)
|
17
|
+
chomped = '#'
|
18
|
+
end
|
19
|
+
@comments[result[2][0]] = Snippet.new(Range.from_to(result[2][0], result[2][1], result[2][0], result[2][1] + chomped.length), chomped)
|
20
|
+
end
|
21
|
+
result
|
22
|
+
end
|
23
|
+
|
24
|
+
def on_embdoc_beg *args
|
25
|
+
result = super
|
26
|
+
chomped = result[1].chomp
|
27
|
+
@comments[result[2][0]] = Snippet.new(Range.from_to(result[2][0], result[2][1], result[2][0], result[2][1] + chomped.length), chomped)
|
28
|
+
result
|
29
|
+
end
|
30
|
+
|
31
|
+
def on_embdoc *args
|
32
|
+
result = super
|
33
|
+
chomped = result[1].chomp
|
34
|
+
@comments[result[2][0]] = Snippet.new(Range.from_to(result[2][0], result[2][1], result[2][0], result[2][1] + chomped.length), chomped)
|
35
|
+
result
|
36
|
+
end
|
37
|
+
|
38
|
+
def on_embdoc_end *args
|
39
|
+
result = super
|
40
|
+
chomped = result[1].chomp
|
41
|
+
@comments[result[2][0]] = Snippet.new(Range.from_to(result[2][0], result[2][1], result[2][0], result[2][1] + chomped.length), chomped)
|
42
|
+
result
|
43
|
+
end
|
44
|
+
|
45
|
+
def parse
|
46
|
+
@comments = {}
|
47
|
+
super
|
48
|
+
@comments
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module Solargraph
|
2
|
+
module Parser
|
3
|
+
module Legacy
|
4
|
+
autoload :FlawedBuilder, 'solargraph/parser/legacy/flawed_builder'
|
5
|
+
autoload :ClassMethods, 'solargraph/parser/legacy/class_methods'
|
6
|
+
autoload :NodeMethods, 'solargraph/parser/legacy/node_methods'
|
7
|
+
autoload :NodeChainer, 'solargraph/parser/legacy/node_chainer'
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
require 'solargraph/parser/legacy/node_processors'
|
@@ -0,0 +1,105 @@
|
|
1
|
+
require 'parser/current'
|
2
|
+
|
3
|
+
module Solargraph
|
4
|
+
module Parser
|
5
|
+
module Legacy
|
6
|
+
module ClassMethods
|
7
|
+
# @param code [String]
|
8
|
+
# @param filename [String]
|
9
|
+
# @return [Array(Parser::AST::Node, Array<Parser::Source::Comment>)]
|
10
|
+
def parse_with_comments code, filename = nil
|
11
|
+
buffer = ::Parser::Source::Buffer.new(filename, 0)
|
12
|
+
buffer.source = code
|
13
|
+
node = parser.parse(buffer)
|
14
|
+
comments = CommentRipper.new(code, filename, 0).parse
|
15
|
+
[node, comments]
|
16
|
+
rescue ::Parser::SyntaxError => e
|
17
|
+
raise Parser::SyntaxError, e.message
|
18
|
+
end
|
19
|
+
|
20
|
+
# @param code [String]
|
21
|
+
# @param filename [String, nil]
|
22
|
+
# @param line [Integer]
|
23
|
+
# @return [Parser::AST::Node]
|
24
|
+
def parse code, filename = nil, line = 0
|
25
|
+
buffer = ::Parser::Source::Buffer.new(filename, line)
|
26
|
+
buffer.source = code
|
27
|
+
parser.parse(buffer)
|
28
|
+
rescue ::Parser::SyntaxError => e
|
29
|
+
raise Parser::SyntaxError, e.message
|
30
|
+
end
|
31
|
+
|
32
|
+
# @return [Parser::Base]
|
33
|
+
def parser
|
34
|
+
# @todo Consider setting an instance variable. We might not need to
|
35
|
+
# recreate the parser every time we use it.
|
36
|
+
parser = ::Parser::CurrentRuby.new(FlawedBuilder.new)
|
37
|
+
parser.diagnostics.all_errors_are_fatal = true
|
38
|
+
parser.diagnostics.ignore_warnings = true
|
39
|
+
parser
|
40
|
+
end
|
41
|
+
|
42
|
+
def map source
|
43
|
+
NodeProcessor.process(source.node, Region.new(source: source))
|
44
|
+
end
|
45
|
+
|
46
|
+
def returns_from node
|
47
|
+
NodeMethods.returns_from(node)
|
48
|
+
end
|
49
|
+
|
50
|
+
def references source, name
|
51
|
+
inner_node_references(name, source.node).map do |n|
|
52
|
+
offset = Position.to_offset(source.code, NodeMethods.get_node_start_position(n))
|
53
|
+
soff = source.code.index(name, offset)
|
54
|
+
eoff = soff + name.length
|
55
|
+
Location.new(
|
56
|
+
source.filename,
|
57
|
+
Range.new(
|
58
|
+
Position.from_offset(source.code, soff),
|
59
|
+
Position.from_offset(source.code, eoff)
|
60
|
+
)
|
61
|
+
)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
# @param name [String]
|
66
|
+
# @param top [AST::Node]
|
67
|
+
# @return [Array<AST::Node>]
|
68
|
+
def inner_node_references name, top
|
69
|
+
result = []
|
70
|
+
if top.is_a?(AST::Node) && top.to_s.include?(":#{name}")
|
71
|
+
result.push top if top.children.any? { |c| c.to_s == name }
|
72
|
+
top.children.each { |c| result.concat inner_node_references(name, c) }
|
73
|
+
end
|
74
|
+
result
|
75
|
+
end
|
76
|
+
|
77
|
+
def chain *args
|
78
|
+
NodeChainer.chain *args
|
79
|
+
end
|
80
|
+
|
81
|
+
def process_node *args
|
82
|
+
Solargraph::Parser::NodeProcessor.process *args
|
83
|
+
end
|
84
|
+
|
85
|
+
def infer_literal_node_type node
|
86
|
+
NodeMethods.infer_literal_node_type node
|
87
|
+
end
|
88
|
+
|
89
|
+
def version
|
90
|
+
parser.version
|
91
|
+
end
|
92
|
+
|
93
|
+
def is_ast_node? node
|
94
|
+
node.is_a?(::Parser::AST::Node)
|
95
|
+
end
|
96
|
+
|
97
|
+
def node_range node
|
98
|
+
st = Position.new(node.loc.line, node.loc.column)
|
99
|
+
en = Position.new(node.loc.last_line, node.loc.last_column)
|
100
|
+
Range.new(st, en)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Solargraph
|
4
|
+
module Parser
|
5
|
+
module Legacy
|
6
|
+
# A custom builder for source parsers that ignores character encoding
|
7
|
+
# issues in literal strings.
|
8
|
+
#
|
9
|
+
class FlawedBuilder < ::Parser::Builders::Default
|
10
|
+
def string_value(token)
|
11
|
+
value(token)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Solargraph
|
4
|
+
module Parser
|
5
|
+
module Legacy
|
6
|
+
# A factory for generating chains from nodes.
|
7
|
+
#
|
8
|
+
class NodeChainer
|
9
|
+
include NodeMethods
|
10
|
+
Chain = Source::Chain
|
11
|
+
|
12
|
+
# @param node [Parser::AST::Node]
|
13
|
+
# @param filename [String]
|
14
|
+
def initialize node, filename = nil, in_block = false
|
15
|
+
@node = node
|
16
|
+
@filename = filename
|
17
|
+
@in_block = in_block
|
18
|
+
end
|
19
|
+
|
20
|
+
# @return [Source::Chain]
|
21
|
+
def chain
|
22
|
+
links = generate_links(@node)
|
23
|
+
Chain.new(links, @node, (Parser.is_ast_node?(@node) && @node.type == :splat))
|
24
|
+
end
|
25
|
+
|
26
|
+
class << self
|
27
|
+
# @param node [Parser::AST::Node]
|
28
|
+
# @param filename [String]
|
29
|
+
# @return [Source::Chain]
|
30
|
+
def chain node, filename = nil, in_block = false
|
31
|
+
NodeChainer.new(node, filename, in_block).chain
|
32
|
+
end
|
33
|
+
|
34
|
+
# @param code [String]
|
35
|
+
# @return [Source::Chain]
|
36
|
+
def load_string(code)
|
37
|
+
node = Parser.parse(code.sub(/\.$/, ''))
|
38
|
+
chain = NodeChainer.new(node).chain
|
39
|
+
chain.links.push(Chain::Link.new) if code.end_with?('.')
|
40
|
+
chain
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
# @param n [Parser::AST::Node]
|
47
|
+
# @return [Array<Chain::Link>]
|
48
|
+
def generate_links n
|
49
|
+
return [] unless n.is_a?(::Parser::AST::Node)
|
50
|
+
return generate_links(n.children[0]) if n.type == :begin
|
51
|
+
return generate_links(n.children[0]) if n.type == :splat
|
52
|
+
result = []
|
53
|
+
if n.type == :block
|
54
|
+
@in_block = true
|
55
|
+
result.concat generate_links(n.children[0])
|
56
|
+
@in_block = false
|
57
|
+
elsif n.type == :send
|
58
|
+
if n.children[0].is_a?(::Parser::AST::Node)
|
59
|
+
result.concat generate_links(n.children[0])
|
60
|
+
args = []
|
61
|
+
n.children[2..-1].each do |c|
|
62
|
+
args.push NodeChainer.chain(c)
|
63
|
+
end
|
64
|
+
result.push Chain::Call.new(n.children[1].to_s, args, @in_block || block_passed?(n))
|
65
|
+
elsif n.children[0].nil?
|
66
|
+
args = []
|
67
|
+
n.children[2..-1].each do |c|
|
68
|
+
args.push NodeChainer.chain(c)
|
69
|
+
end
|
70
|
+
result.push Chain::Call.new(n.children[1].to_s, args, @in_block || block_passed?(n))
|
71
|
+
else
|
72
|
+
raise "No idea what to do with #{n}"
|
73
|
+
end
|
74
|
+
elsif n.type == :self
|
75
|
+
result.push Chain::Head.new('self')
|
76
|
+
elsif [:super, :zsuper].include?(n.type)
|
77
|
+
result.push Chain::Head.new('super')
|
78
|
+
elsif n.type == :const
|
79
|
+
const = unpack_name(n)
|
80
|
+
result.push Chain::Constant.new(const)
|
81
|
+
elsif [:lvar, :lvasgn].include?(n.type)
|
82
|
+
result.push Chain::Call.new(n.children[0].to_s)
|
83
|
+
elsif [:ivar, :ivasgn].include?(n.type)
|
84
|
+
result.push Chain::InstanceVariable.new(n.children[0].to_s)
|
85
|
+
elsif [:cvar, :cvasgn].include?(n.type)
|
86
|
+
result.push Chain::ClassVariable.new(n.children[0].to_s)
|
87
|
+
elsif [:gvar, :gvasgn].include?(n.type)
|
88
|
+
result.push Chain::GlobalVariable.new(n.children[0].to_s)
|
89
|
+
elsif n.type == :or_asgn
|
90
|
+
result.concat generate_links n.children[1]
|
91
|
+
elsif [:class, :module, :def, :defs].include?(n.type)
|
92
|
+
# @todo Undefined or what?
|
93
|
+
result.push Chain::UNDEFINED_CALL
|
94
|
+
elsif n.type == :and
|
95
|
+
result.concat generate_links(n.children.last)
|
96
|
+
elsif n.type == :or
|
97
|
+
result.push Chain::Or.new([NodeChainer.chain(n.children[0], @filename), NodeChainer.chain(n.children[1], @filename)])
|
98
|
+
elsif [:begin, :kwbegin].include?(n.type)
|
99
|
+
result.concat generate_links(n.children[0])
|
100
|
+
elsif n.type == :block_pass
|
101
|
+
result.push Chain::BlockVariable.new("&#{n.children[0].children[0].to_s}")
|
102
|
+
else
|
103
|
+
lit = infer_literal_node_type(n)
|
104
|
+
result.push (lit ? Chain::Literal.new(lit) : Chain::Link.new)
|
105
|
+
end
|
106
|
+
result
|
107
|
+
end
|
108
|
+
|
109
|
+
def block_passed? node
|
110
|
+
node.children.last.is_a?(::Parser::AST::Node) && node.children.last.type == :block_pass
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
@@ -0,0 +1,289 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'parser'
|
4
|
+
|
5
|
+
module Solargraph
|
6
|
+
module Parser
|
7
|
+
module Legacy
|
8
|
+
module NodeMethods
|
9
|
+
module_function
|
10
|
+
|
11
|
+
# @param node [Parser::AST::Node]
|
12
|
+
# @return [String]
|
13
|
+
def unpack_name(node)
|
14
|
+
pack_name(node).join("::")
|
15
|
+
end
|
16
|
+
|
17
|
+
# @param node [Parser::AST::Node]
|
18
|
+
# @return [Array<String>]
|
19
|
+
def pack_name(node)
|
20
|
+
parts = []
|
21
|
+
if node.is_a?(AST::Node)
|
22
|
+
node.children.each { |n|
|
23
|
+
if n.is_a?(AST::Node)
|
24
|
+
if n.type == :cbase
|
25
|
+
parts = [''] + pack_name(n)
|
26
|
+
else
|
27
|
+
parts += pack_name(n)
|
28
|
+
end
|
29
|
+
else
|
30
|
+
parts.push n unless n.nil?
|
31
|
+
end
|
32
|
+
}
|
33
|
+
end
|
34
|
+
parts
|
35
|
+
end
|
36
|
+
|
37
|
+
# @param node [Parser::AST::Node]
|
38
|
+
# @return [String, nil]
|
39
|
+
def infer_literal_node_type node
|
40
|
+
return nil unless node.is_a?(AST::Node)
|
41
|
+
if node.type == :str || node.type == :dstr
|
42
|
+
return '::String'
|
43
|
+
elsif node.type == :array
|
44
|
+
return '::Array'
|
45
|
+
elsif node.type == :hash
|
46
|
+
return '::Hash'
|
47
|
+
elsif node.type == :int
|
48
|
+
return '::Integer'
|
49
|
+
elsif node.type == :float
|
50
|
+
return '::Float'
|
51
|
+
elsif node.type == :sym
|
52
|
+
return '::Symbol'
|
53
|
+
elsif node.type == :regexp
|
54
|
+
return '::Regexp'
|
55
|
+
elsif node.type == :irange
|
56
|
+
return '::Range'
|
57
|
+
elsif node.type == :true || node.type == :false
|
58
|
+
return '::Boolean'
|
59
|
+
# @todo Support `nil` keyword in types
|
60
|
+
# elsif node.type == :nil
|
61
|
+
# return 'NilClass'
|
62
|
+
end
|
63
|
+
nil
|
64
|
+
end
|
65
|
+
|
66
|
+
# @param node [Parser::AST::Node]
|
67
|
+
# @return [Position]
|
68
|
+
def get_node_start_position(node)
|
69
|
+
Position.new(node.loc.line, node.loc.column)
|
70
|
+
end
|
71
|
+
|
72
|
+
# @param node [Parser::AST::Node]
|
73
|
+
# @return [Position]
|
74
|
+
def get_node_end_position(node)
|
75
|
+
Position.new(node.loc.last_line, node.loc.last_column)
|
76
|
+
end
|
77
|
+
|
78
|
+
def drill_signature node, signature
|
79
|
+
return signature unless node.is_a?(AST::Node)
|
80
|
+
if node.type == :const or node.type == :cbase
|
81
|
+
unless node.children[0].nil?
|
82
|
+
signature += drill_signature(node.children[0], signature)
|
83
|
+
end
|
84
|
+
signature += '::' unless signature.empty?
|
85
|
+
signature += node.children[1].to_s
|
86
|
+
elsif node.type == :lvar or node.type == :ivar or node.type == :cvar
|
87
|
+
signature += '.' unless signature.empty?
|
88
|
+
signature += node.children[0].to_s
|
89
|
+
elsif node.type == :send
|
90
|
+
unless node.children[0].nil?
|
91
|
+
signature += drill_signature(node.children[0], signature)
|
92
|
+
end
|
93
|
+
signature += '.' unless signature.empty?
|
94
|
+
signature += node.children[1].to_s
|
95
|
+
end
|
96
|
+
signature
|
97
|
+
end
|
98
|
+
|
99
|
+
def convert_hash node
|
100
|
+
return {} unless Parser.is_ast_node?(node) && node.type == :hash
|
101
|
+
result = {}
|
102
|
+
node.children.each do |pair|
|
103
|
+
result[pair.children[0].children[0]] = Solargraph::Parser.chain(pair.children[1])
|
104
|
+
end
|
105
|
+
result
|
106
|
+
end
|
107
|
+
|
108
|
+
NIL_NODE = ::Parser::AST::Node.new(:nil)
|
109
|
+
|
110
|
+
def const_nodes_from node
|
111
|
+
return [] unless Parser.is_ast_node?(node)
|
112
|
+
result = []
|
113
|
+
if node.type == :const
|
114
|
+
result.push node
|
115
|
+
else
|
116
|
+
node.children.each { |child| result.concat const_nodes_from(child) }
|
117
|
+
end
|
118
|
+
result
|
119
|
+
end
|
120
|
+
|
121
|
+
# @todo Temporarily here for testing. Move to Solargraph::Parser.
|
122
|
+
def call_nodes_from node
|
123
|
+
return [] unless node.is_a?(::Parser::AST::Node)
|
124
|
+
result = []
|
125
|
+
if node.type == :block
|
126
|
+
result.push node
|
127
|
+
node.children[0].children[2..-1].each { |child| result.concat call_nodes_from(child) }
|
128
|
+
node.children[1..-1].each { |child| result.concat call_nodes_from(child) }
|
129
|
+
elsif node.type == :send
|
130
|
+
result.push node
|
131
|
+
node.children[2..-1].each { |child| result.concat call_nodes_from(child) }
|
132
|
+
else
|
133
|
+
node.children.each { |child| result.concat call_nodes_from(child) }
|
134
|
+
end
|
135
|
+
result
|
136
|
+
end
|
137
|
+
|
138
|
+
# Find all the nodes within the provided node that potentially return a
|
139
|
+
# value.
|
140
|
+
#
|
141
|
+
# The node parameter typically represents a method's logic, e.g., the
|
142
|
+
# second child (after the :args node) of a :def node. A simple one-line
|
143
|
+
# method would typically return itself, while a node with conditions
|
144
|
+
# would return the resulting node from each conditional branch. Nodes
|
145
|
+
# that follow a :return node are assumed to be unreachable. Nil values
|
146
|
+
# are converted to nil node types.
|
147
|
+
#
|
148
|
+
# @param node [Parser::AST::Node]
|
149
|
+
# @return [Array<Parser::AST::Node>]
|
150
|
+
def returns_from node
|
151
|
+
DeepInference.get_return_nodes(node).map { |n| n || NIL_NODE }
|
152
|
+
end
|
153
|
+
|
154
|
+
# @param cursor [Solargraph::Source::Cursor]
|
155
|
+
def find_recipient_node cursor
|
156
|
+
source = cursor.source
|
157
|
+
position = cursor.position
|
158
|
+
offset = cursor.offset
|
159
|
+
tree = if source.synchronized?
|
160
|
+
match = source.code[0..offset-1].match(/,\s*\z/)
|
161
|
+
if match
|
162
|
+
source.tree_at(position.line, position.column - match[0].length)
|
163
|
+
else
|
164
|
+
source.tree_at(position.line, position.column)
|
165
|
+
end
|
166
|
+
else
|
167
|
+
source.tree_at(position.line, position.column - 1)
|
168
|
+
end
|
169
|
+
prev = nil
|
170
|
+
tree.each do |node|
|
171
|
+
if node.type == :send
|
172
|
+
args = node.children[2..-1]
|
173
|
+
if !args.empty?
|
174
|
+
return node if prev && args.include?(prev)
|
175
|
+
else
|
176
|
+
if source.synchronized?
|
177
|
+
return node if source.code[0..offset-1] =~ /\(\s*\z/ && source.code[offset..-1] =~ /^\s*\)/
|
178
|
+
else
|
179
|
+
return node if source.code[0..offset-1] =~ /\([^\(]*\z/
|
180
|
+
end
|
181
|
+
end
|
182
|
+
end
|
183
|
+
prev = node
|
184
|
+
end
|
185
|
+
nil
|
186
|
+
end
|
187
|
+
|
188
|
+
module DeepInference
|
189
|
+
class << self
|
190
|
+
CONDITIONAL = [:if, :unless]
|
191
|
+
REDUCEABLE = [:begin, :kwbegin]
|
192
|
+
SKIPPABLE = [:def, :defs, :class, :sclass, :module]
|
193
|
+
|
194
|
+
# @param node [Parser::AST::Node]
|
195
|
+
# @return [Array<Parser::AST::Node>]
|
196
|
+
def get_return_nodes node
|
197
|
+
return [] unless node.is_a?(::Parser::AST::Node)
|
198
|
+
result = []
|
199
|
+
if REDUCEABLE.include?(node.type)
|
200
|
+
result.concat get_return_nodes_from_children(node)
|
201
|
+
elsif CONDITIONAL.include?(node.type)
|
202
|
+
result.concat reduce_to_value_nodes(node.children[1..-1])
|
203
|
+
# result.push NIL_NODE unless node.children[2]
|
204
|
+
elsif node.type == :or
|
205
|
+
result.concat reduce_to_value_nodes(node.children)
|
206
|
+
elsif node.type == :return
|
207
|
+
result.concat reduce_to_value_nodes([node.children[0]])
|
208
|
+
elsif node.type == :block
|
209
|
+
result.push node
|
210
|
+
result.concat get_return_nodes_only(node.children[2])
|
211
|
+
elsif node.type == :case
|
212
|
+
node.children[1..-1].each do |cc|
|
213
|
+
result.concat reduce_to_value_nodes(cc.children[1..-2])
|
214
|
+
result.concat reduce_to_value_nodes([cc.children.last])
|
215
|
+
end
|
216
|
+
else
|
217
|
+
result.push node
|
218
|
+
end
|
219
|
+
result
|
220
|
+
end
|
221
|
+
|
222
|
+
private
|
223
|
+
|
224
|
+
def get_return_nodes_from_children parent
|
225
|
+
result = []
|
226
|
+
nodes = parent.children.select{|n| n.is_a?(AST::Node)}
|
227
|
+
nodes.each_with_index do |node, idx|
|
228
|
+
if node.type == :block
|
229
|
+
result.concat get_return_nodes_only(node.children[2])
|
230
|
+
elsif SKIPPABLE.include?(node.type)
|
231
|
+
next
|
232
|
+
elsif node.type == :return
|
233
|
+
result.concat reduce_to_value_nodes([node.children[0]])
|
234
|
+
# Return the result here because the rest of the code is
|
235
|
+
# unreachable
|
236
|
+
return result
|
237
|
+
else
|
238
|
+
result.concat get_return_nodes_only(node)
|
239
|
+
end
|
240
|
+
result.concat reduce_to_value_nodes([nodes.last]) if idx == nodes.length - 1
|
241
|
+
end
|
242
|
+
result
|
243
|
+
end
|
244
|
+
|
245
|
+
def get_return_nodes_only parent
|
246
|
+
return [] unless parent.is_a?(::Parser::AST::Node)
|
247
|
+
result = []
|
248
|
+
nodes = parent.children.select{|n| n.is_a?(::Parser::AST::Node)}
|
249
|
+
nodes.each do |node|
|
250
|
+
next if SKIPPABLE.include?(node.type)
|
251
|
+
if node.type == :return
|
252
|
+
result.concat reduce_to_value_nodes([node.children[0]])
|
253
|
+
# Return the result here because the rest of the code is
|
254
|
+
# unreachable
|
255
|
+
return result
|
256
|
+
else
|
257
|
+
result.concat get_return_nodes_only(node)
|
258
|
+
end
|
259
|
+
end
|
260
|
+
result
|
261
|
+
end
|
262
|
+
|
263
|
+
def reduce_to_value_nodes nodes
|
264
|
+
result = []
|
265
|
+
nodes.each do |node|
|
266
|
+
if !node.is_a?(::Parser::AST::Node)
|
267
|
+
result.push nil
|
268
|
+
elsif REDUCEABLE.include?(node.type)
|
269
|
+
result.concat get_return_nodes_from_children(node)
|
270
|
+
elsif CONDITIONAL.include?(node.type)
|
271
|
+
result.concat reduce_to_value_nodes(node.children[1..-1])
|
272
|
+
elsif node.type == :return
|
273
|
+
result.concat reduce_to_value_nodes([node.children[0]])
|
274
|
+
elsif node.type == :or
|
275
|
+
result.concat reduce_to_value_nodes(node.children)
|
276
|
+
elsif node.type == :block
|
277
|
+
result.concat get_return_nodes_only(node.children[2])
|
278
|
+
else
|
279
|
+
result.push node
|
280
|
+
end
|
281
|
+
end
|
282
|
+
result
|
283
|
+
end
|
284
|
+
end
|
285
|
+
end
|
286
|
+
end
|
287
|
+
end
|
288
|
+
end
|
289
|
+
end
|