solargraph 0.61.0 → 0.62.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/linting.yml +24 -2
- data/.github/workflows/plugins.yml +18 -0
- data/.github/workflows/rspec.yml +14 -1
- data/.github/workflows/typecheck.yml +6 -0
- data/CHANGELOG.md +42 -0
- data/lib/solargraph/api_map/index.rb +14 -3
- data/lib/solargraph/api_map/store.rb +6 -0
- data/lib/solargraph/api_map.rb +34 -2
- data/lib/solargraph/collection/gem.rb +12 -10
- data/lib/solargraph/collection.rb +0 -1
- data/lib/solargraph/complex_type/unique_type.rb +13 -0
- data/lib/solargraph/complex_type.rb +23 -3
- data/lib/solargraph/convention.rb +0 -4
- data/lib/solargraph/diagnostics/type_check.rb +0 -1
- data/lib/solargraph/external.rb +56 -28
- data/lib/solargraph/language_server/host/diagnoser.rb +1 -1
- data/lib/solargraph/language_server/host/dispatch.rb +4 -2
- data/lib/solargraph/language_server/host.rb +25 -9
- data/lib/solargraph/language_server/message/completion_item/resolve.rb +10 -3
- data/lib/solargraph/language_server/message/text_document/definition.rb +1 -1
- data/lib/solargraph/language_server/progress.rb +55 -17
- data/lib/solargraph/library.rb +33 -42
- data/lib/solargraph/parser/node_processor/base.rb +6 -0
- data/lib/solargraph/parser/parser_gem/node_methods.rb +29 -6
- data/lib/solargraph/parser/parser_gem/node_processors/class_definition/class_assignment_node.rb +98 -0
- data/lib/solargraph/parser/parser_gem/node_processors/class_definition.rb +54 -0
- data/lib/solargraph/parser/parser_gem/node_processors/cvasgn_node.rb +1 -0
- data/lib/solargraph/parser/parser_gem/node_processors/data_definition/data_assignment_node.rb +65 -0
- data/lib/solargraph/parser/parser_gem/node_processors/data_definition/data_definition_node.rb +97 -0
- data/lib/solargraph/parser/parser_gem/node_processors/data_definition.rb +110 -0
- data/lib/solargraph/parser/parser_gem/node_processors/gvasgn_node.rb +1 -0
- data/lib/solargraph/parser/parser_gem/node_processors/ivasgn_node.rb +2 -0
- data/lib/solargraph/parser/parser_gem/node_processors/lvasgn_node.rb +1 -0
- data/lib/solargraph/parser/parser_gem/node_processors/send_node.rb +185 -0
- data/lib/solargraph/parser/parser_gem/node_processors/struct_definition/struct_assignment_node.rb +66 -0
- data/lib/solargraph/parser/parser_gem/node_processors/struct_definition/struct_definition_node.rb +108 -0
- data/lib/solargraph/parser/parser_gem/node_processors/struct_definition.rb +170 -0
- data/lib/solargraph/parser/parser_gem/node_processors.rb +33 -29
- data/lib/solargraph/pin/base.rb +25 -1
- data/lib/solargraph/pin/base_variable.rb +31 -1
- data/lib/solargraph/pin/callable.rb +11 -2
- data/lib/solargraph/pin/delegated_method.rb +5 -0
- data/lib/solargraph/pin/duck_method.rb +36 -16
- data/lib/solargraph/pin/instance_variable.rb +45 -0
- data/lib/solargraph/pin/method.rb +10 -0
- data/lib/solargraph/pin/method_alias.rb +4 -0
- data/lib/solargraph/pin/parameter.rb +1 -1
- data/lib/solargraph/pin/reference/included_mixin.rb +22 -0
- data/lib/solargraph/pin/reference.rb +1 -0
- data/lib/solargraph/plugin/active_support.rb +100 -0
- data/lib/solargraph/plugin/addon.rb +16 -0
- data/lib/solargraph/plugin/base.rb +25 -0
- data/lib/solargraph/plugin/panel.rb +37 -0
- data/lib/solargraph/plugin.rb +26 -0
- data/lib/solargraph/rbs_collection.rb +119 -0
- data/lib/solargraph/rbs_map/conversions.rb +106 -47
- data/lib/solargraph/rbs_map/gem.rb +4 -1
- data/lib/solargraph/rbs_map/helpers.rb +7 -6
- data/lib/solargraph/rbs_translator.rb +105 -70
- data/lib/solargraph/repo.rb +1 -1
- data/lib/solargraph/source/chain/call.rb +32 -13
- data/lib/solargraph/source/node_map.rb +27 -0
- data/lib/solargraph/source.rb +17 -0
- data/lib/solargraph/source_map/call.rb +18 -0
- data/lib/solargraph/source_map/mapper.rb +1 -1
- data/lib/solargraph/source_map.rb +18 -4
- data/lib/solargraph/type_checker/rules.rb +2 -2
- data/lib/solargraph/type_checker.rb +4 -1
- data/lib/solargraph/version.rb +1 -1
- data/lib/solargraph/yard_map/directives/attribute_directive.rb +4 -1
- data/lib/solargraph/yard_map/macro.rb +51 -1
- data/lib/solargraph.rb +2 -0
- data/rbs/fills/boolean/0/boolean.rbs +12 -0
- data/solargraph.gemspec +1 -1
- metadata +21 -11
- data/lib/solargraph/collection/rbs.rb +0 -49
- data/lib/solargraph/convention/active_support_concern.rb +0 -111
- data/lib/solargraph/convention/data_definition/data_assignment_node.rb +0 -61
- data/lib/solargraph/convention/data_definition/data_definition_node.rb +0 -93
- data/lib/solargraph/convention/data_definition.rb +0 -108
- data/lib/solargraph/convention/struct_definition/struct_assignment_node.rb +0 -62
- data/lib/solargraph/convention/struct_definition/struct_definition_node.rb +0 -103
- data/lib/solargraph/convention/struct_definition.rb +0 -168
|
@@ -118,11 +118,13 @@ module Solargraph
|
|
|
118
118
|
.tap { |lib| lib.add_observer self }
|
|
119
119
|
end
|
|
120
120
|
|
|
121
|
-
#
|
|
121
|
+
# The Library observer callback that #generic_library registers.
|
|
122
122
|
#
|
|
123
|
+
# @abstract
|
|
124
|
+
# @param progress [Solargraph::LanguageServer::Progress, nil]
|
|
123
125
|
# @return [void]
|
|
124
126
|
def update progress
|
|
125
|
-
|
|
127
|
+
raise NotImplementedError, 'Host::Dispatch requires an update method'
|
|
126
128
|
end
|
|
127
129
|
end
|
|
128
130
|
end
|
|
@@ -495,7 +495,7 @@ module Solargraph
|
|
|
495
495
|
)
|
|
496
496
|
result.concat(library.locate_pins(location).select { |pin| pin.name == params['label'] })
|
|
497
497
|
end
|
|
498
|
-
if params['data']['path']
|
|
498
|
+
if params['data']['path'] && !params['data']['path'].empty?
|
|
499
499
|
result.concat library.path_pins(params['data']['path'])
|
|
500
500
|
# @todo This exception is necessary because `Library#path_pins` does
|
|
501
501
|
# not perform a namespace method query, so the implicit `.new` pin
|
|
@@ -706,6 +706,15 @@ module Solargraph
|
|
|
706
706
|
client_capabilities['window'] && client_capabilities['window']['workDoneProgress']
|
|
707
707
|
end
|
|
708
708
|
|
|
709
|
+
# The Library observer callback that sends a library's progress to
|
|
710
|
+
# the client.
|
|
711
|
+
#
|
|
712
|
+
# @param progress [Solargraph::LanguageServer::Progress, nil]
|
|
713
|
+
# @return [void]
|
|
714
|
+
def update progress
|
|
715
|
+
progress&.send(self)
|
|
716
|
+
end
|
|
717
|
+
|
|
709
718
|
private
|
|
710
719
|
|
|
711
720
|
# @return [Array<Integer>]
|
|
@@ -736,7 +745,6 @@ module Solargraph
|
|
|
736
745
|
# @return [String]
|
|
737
746
|
def normalize_separators path
|
|
738
747
|
return path if File::ALT_SEPARATOR.nil?
|
|
739
|
-
# @sg-ignore flow sensitive typing needs to handle constants
|
|
740
748
|
path.gsub(File::ALT_SEPARATOR, File::SEPARATOR)
|
|
741
749
|
end
|
|
742
750
|
|
|
@@ -870,15 +878,23 @@ module Solargraph
|
|
|
870
878
|
def sync_library_map library
|
|
871
879
|
total = library.workspace.sources.length
|
|
872
880
|
progress = Progress.new('Mapping workspace')
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
881
|
+
begin
|
|
882
|
+
progress.begin "0/#{total} files", 0
|
|
883
|
+
progress.send self
|
|
884
|
+
while library.next_map
|
|
885
|
+
mapped = library.source_map_hash.length
|
|
886
|
+
# The workspace can change while it's being mapped, e.g., when the
|
|
887
|
+
# user opens a file that wasn't included in the original scan. The
|
|
888
|
+
# progress should never report more than 100%.
|
|
889
|
+
total = mapped if mapped > total
|
|
890
|
+
# @sg-ignore Wrong argument type for Integer#/: arg_0 expected BigDecimal, received Integer
|
|
891
|
+
pct = total.zero? ? 100 : ((mapped * 100) / total).clamp(0, 100)
|
|
892
|
+
progress.report "#{mapped}/#{total} files", pct
|
|
893
|
+
progress.send self
|
|
894
|
+
end
|
|
895
|
+
progress.finish 'done'
|
|
878
896
|
progress.send self
|
|
879
897
|
end
|
|
880
|
-
progress.finish 'done'
|
|
881
|
-
progress.send self
|
|
882
898
|
end
|
|
883
899
|
end
|
|
884
900
|
end
|
|
@@ -7,6 +7,12 @@ module Solargraph
|
|
|
7
7
|
# completionItem/resolve message handler
|
|
8
8
|
#
|
|
9
9
|
class Resolve < Base
|
|
10
|
+
# Properties that must not be changed during completionItem/resolve
|
|
11
|
+
# according to the LSP specification.
|
|
12
|
+
#
|
|
13
|
+
# @see https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_completion
|
|
14
|
+
IMMUTABLE_FIELDS = %i[label kind sortText filterText insertText textEdit data].freeze
|
|
15
|
+
|
|
10
16
|
def process
|
|
11
17
|
pins = host.locate_pins(params)
|
|
12
18
|
set_result merge(pins)
|
|
@@ -17,14 +23,15 @@ module Solargraph
|
|
|
17
23
|
# @param pins [Array<Pin::Base>]
|
|
18
24
|
# @return [Hash]
|
|
19
25
|
def merge pins
|
|
20
|
-
|
|
26
|
+
original = params.transform_keys(&:to_sym)
|
|
27
|
+
return original if pins.empty?
|
|
21
28
|
docs = pins
|
|
22
29
|
.reject { |pin| pin.documentation.empty? && pin.return_type.undefined? }
|
|
23
|
-
result =
|
|
24
|
-
.transform_keys(&:to_sym)
|
|
30
|
+
result = original
|
|
25
31
|
.merge(pins.first.resolve_completion_item)
|
|
26
32
|
.merge(documentation: markup_content(join_docs(docs)))
|
|
27
33
|
result[:detail] = pins.first.detail
|
|
34
|
+
IMMUTABLE_FIELDS.each { |field| result[field] = original[field] }
|
|
28
35
|
result
|
|
29
36
|
end
|
|
30
37
|
|
|
@@ -15,7 +15,7 @@ module Solargraph
|
|
|
15
15
|
|
|
16
16
|
# @return [Array<Hash>, nil]
|
|
17
17
|
def code_location
|
|
18
|
-
suggestions = host.definitions_at(params['textDocument']['uri'], @line, @column)
|
|
18
|
+
suggestions = host.definitions_at(params['textDocument']['uri'], @line, @column) || []
|
|
19
19
|
# @sg-ignore Need to add nil check here
|
|
20
20
|
return nil if suggestions.empty?
|
|
21
21
|
# @sg-ignore Need to add nil check here
|
|
@@ -11,6 +11,12 @@ module Solargraph
|
|
|
11
11
|
CREATED = :created
|
|
12
12
|
FINISHED = :finished
|
|
13
13
|
|
|
14
|
+
# The number of seconds an idle notification can go without an update
|
|
15
|
+
# before it's refreshed. Some hosts dismiss progress notifications that
|
|
16
|
+
# haven't been updated recently.
|
|
17
|
+
#
|
|
18
|
+
KEEP_ALIVE_INTERVAL = 10
|
|
19
|
+
|
|
14
20
|
# @return [String]
|
|
15
21
|
attr_reader :uuid
|
|
16
22
|
|
|
@@ -43,21 +49,23 @@ module Solargraph
|
|
|
43
49
|
def begin message, percentage
|
|
44
50
|
@kind = 'begin'
|
|
45
51
|
@message = message
|
|
46
|
-
@percentage = percentage
|
|
52
|
+
@percentage = clamp_percentage percentage
|
|
47
53
|
end
|
|
48
54
|
|
|
49
55
|
# @param message [String]
|
|
50
56
|
# @param percentage [Integer]
|
|
51
57
|
# @return [void]
|
|
52
58
|
def report message, percentage
|
|
59
|
+
return if finished?
|
|
53
60
|
@kind = 'report'
|
|
54
61
|
@message = message
|
|
55
|
-
@percentage = percentage
|
|
62
|
+
@percentage = clamp_percentage percentage
|
|
56
63
|
end
|
|
57
64
|
|
|
58
65
|
# @param message [String]
|
|
59
66
|
# @return [void]
|
|
60
67
|
def finish message
|
|
68
|
+
return false if finished?
|
|
61
69
|
@kind = 'end'
|
|
62
70
|
@message = message
|
|
63
71
|
@percentage = 100
|
|
@@ -67,13 +75,16 @@ module Solargraph
|
|
|
67
75
|
# @param host [Solargraph::LanguageServer::Host]
|
|
68
76
|
# @return [void]
|
|
69
77
|
def send host
|
|
70
|
-
return unless host.client_supports_progress?
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
78
|
+
return unless host.client_supports_progress?
|
|
79
|
+
mutex.synchronize do
|
|
80
|
+
return if finished?
|
|
81
|
+
message = build
|
|
82
|
+
create host
|
|
83
|
+
host.send_notification '$/progress', message
|
|
84
|
+
@last = Time.now
|
|
85
|
+
@status = FINISHED if kind == 'end'
|
|
86
|
+
start_keep_alive host unless finished?
|
|
87
|
+
end
|
|
77
88
|
end
|
|
78
89
|
|
|
79
90
|
def created?
|
|
@@ -120,20 +131,47 @@ module Solargraph
|
|
|
120
131
|
end
|
|
121
132
|
end
|
|
122
133
|
|
|
134
|
+
# Keep idle notifications alive. Some hosts, including VS Code, stop
|
|
135
|
+
# responding to progress notifications that haven't been updated for a
|
|
136
|
+
# while, so a long-running job has to refresh its notification until it
|
|
137
|
+
# either reports again or finishes.
|
|
138
|
+
#
|
|
139
|
+
# The thread is only created once and exits as soon as the progress is
|
|
140
|
+
# finished or the host shuts down, so a completed notification can never
|
|
141
|
+
# be resurrected.
|
|
142
|
+
#
|
|
123
143
|
# @param host [Host]
|
|
124
144
|
# @return [void]
|
|
125
|
-
def
|
|
126
|
-
|
|
127
|
-
@keep_alive
|
|
128
|
-
|
|
129
|
-
sleep
|
|
130
|
-
break if finished?
|
|
131
|
-
next if mutex.synchronize {
|
|
132
|
-
|
|
145
|
+
def start_keep_alive host
|
|
146
|
+
return if @keep_alive
|
|
147
|
+
@keep_alive = Thread.new do
|
|
148
|
+
loop do
|
|
149
|
+
sleep KEEP_ALIVE_INTERVAL
|
|
150
|
+
break if finished? || host.stopped?
|
|
151
|
+
next if mutex.synchronize { elapsed < KEEP_ALIVE_INTERVAL }
|
|
152
|
+
begin
|
|
153
|
+
send host
|
|
154
|
+
rescue StandardError
|
|
155
|
+
break
|
|
156
|
+
end
|
|
133
157
|
end
|
|
134
158
|
end
|
|
135
159
|
end
|
|
136
160
|
|
|
161
|
+
# The number of seconds since the last notification was sent.
|
|
162
|
+
#
|
|
163
|
+
# @return [Float]
|
|
164
|
+
def elapsed
|
|
165
|
+
(Time.now - (@last ||= Time.now)).to_f
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
# @param percentage [Integer, nil]
|
|
169
|
+
# @return [Integer]
|
|
170
|
+
def clamp_percentage percentage
|
|
171
|
+
return 0 if percentage.nil?
|
|
172
|
+
percentage.to_i.clamp 0, 100
|
|
173
|
+
end
|
|
174
|
+
|
|
137
175
|
# @return [Thread::Mutex]
|
|
138
176
|
def mutex
|
|
139
177
|
@mutex ||= Mutex.new
|
data/lib/solargraph/library.rb
CHANGED
|
@@ -481,15 +481,12 @@ module Solargraph
|
|
|
481
481
|
def next_map
|
|
482
482
|
return false if mapped?
|
|
483
483
|
src = workspace.sources.find { |s| !source_map_hash.key?(s.filename) }
|
|
484
|
-
if src
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
else
|
|
491
|
-
false
|
|
492
|
-
end
|
|
484
|
+
return false if src.nil?
|
|
485
|
+
Logging.logger.debug "Mapping #{src.filename}"
|
|
486
|
+
# @sg-ignore OK if src.filename is nil
|
|
487
|
+
mutex.synchronize { source_map_hash[src.filename] = Solargraph::SourceMap.map(src) }
|
|
488
|
+
# @sg-ignore OK if src.filename is nil
|
|
489
|
+
source_map_hash[src.filename]
|
|
493
490
|
end
|
|
494
491
|
|
|
495
492
|
# @return [self]
|
|
@@ -589,49 +586,43 @@ module Solargraph
|
|
|
589
586
|
def cache_next_gemspec
|
|
590
587
|
return if @cache_progress
|
|
591
588
|
|
|
592
|
-
spec = api_map.unloaded_gems.first
|
|
589
|
+
spec = (api_map.unloaded_gems - cache_errors).first
|
|
593
590
|
return end_cache_progress unless spec
|
|
594
591
|
|
|
595
592
|
pending = api_map.unloaded_gems.length - cache_errors.length - 1
|
|
596
593
|
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
return if pending - queued_gemspec_cache.length < 1
|
|
601
|
-
|
|
602
|
-
# Try the next cacheable gemspec. This method is always called
|
|
603
|
-
# from inside sync_catalog's mutex (either directly or via this
|
|
604
|
-
# same recursive call), so recursing through sync_catalog here
|
|
605
|
-
# would try to re-lock a mutex this thread already holds and
|
|
606
|
-
# deadlock (https://github.com/castwide/solargraph/issues/1111).
|
|
607
|
-
cache_next_gemspec
|
|
608
|
-
else
|
|
609
|
-
logger.info "Caching #{spec.name} #{spec.version}"
|
|
610
|
-
Thread.new do
|
|
611
|
-
report_cache_progress spec.name, pending
|
|
594
|
+
logger.info "Caching #{spec.name} #{spec.version}"
|
|
595
|
+
Thread.new do
|
|
596
|
+
report_cache_progress spec.name, pending
|
|
612
597
|
|
|
613
|
-
|
|
614
|
-
|
|
598
|
+
cmd = [workspace.command_path, 'cache', spec.name]
|
|
599
|
+
cmd.push('--directory', workspace.directory) if workspace.directory
|
|
600
|
+
begin
|
|
615
601
|
_o, e, s = Open3.capture3(*cmd)
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
else
|
|
619
|
-
cache_errors.add spec
|
|
620
|
-
logger.warn "Error caching gemspec #{spec.name} #{spec.version}"
|
|
621
|
-
logger.warn e
|
|
622
|
-
end
|
|
623
|
-
end_cache_progress
|
|
624
|
-
catalog
|
|
625
|
-
sync_catalog
|
|
602
|
+
rescue SystemCallError => e
|
|
603
|
+
# The command could not start, e.g. Errno::ENOENT - handled below
|
|
626
604
|
end
|
|
605
|
+
if s&.success?
|
|
606
|
+
logger.info "Cached #{spec.name} #{spec.version}"
|
|
607
|
+
else
|
|
608
|
+
cache_errors.add spec
|
|
609
|
+
logger.warn "Error caching gemspec #{spec.name} #{spec.version}"
|
|
610
|
+
logger.warn e
|
|
611
|
+
end
|
|
612
|
+
end_cache_progress
|
|
613
|
+
catalog
|
|
614
|
+
sync_catalog
|
|
615
|
+
rescue StandardError => e
|
|
616
|
+
logger.warn "Error caching gemspec #{spec.name} #{spec.version}"
|
|
617
|
+
logger.warn e
|
|
618
|
+
ensure
|
|
619
|
+
# The progress notification has to be closed even if caching failed.
|
|
620
|
+
# Clients have no way to dismiss a notification that never receives an
|
|
621
|
+
# end.
|
|
622
|
+
end_cache_progress
|
|
627
623
|
end
|
|
628
624
|
end
|
|
629
625
|
|
|
630
|
-
# @return [Array<Gem::Specification>]
|
|
631
|
-
def queued_gemspec_cache
|
|
632
|
-
@queued_gemspec_cache ||= []
|
|
633
|
-
end
|
|
634
|
-
|
|
635
626
|
# @param gem_name [String]
|
|
636
627
|
# @param pending [Integer]
|
|
637
628
|
# @return [void]
|
|
@@ -91,6 +91,12 @@ module Solargraph
|
|
|
91
91
|
region.source.comments_for(node)
|
|
92
92
|
end
|
|
93
93
|
|
|
94
|
+
# @param node [Parser::AST::Node]
|
|
95
|
+
# @return [String, nil]
|
|
96
|
+
def type_assertion_for node
|
|
97
|
+
region.source.type_assertion_for(node)
|
|
98
|
+
end
|
|
99
|
+
|
|
94
100
|
# @param position [Solargraph::Position]
|
|
95
101
|
# @return [Pin::Closure, nil]
|
|
96
102
|
def named_path_pin position
|
|
@@ -197,6 +197,18 @@ module Solargraph
|
|
|
197
197
|
nodes.any? { |n| splatted_call?(n) }
|
|
198
198
|
end
|
|
199
199
|
|
|
200
|
+
# Anonymous `*`, `**` and `...` arguments pass on whatever the
|
|
201
|
+
# enclosing method received, so neither count nor types are known.
|
|
202
|
+
#
|
|
203
|
+
# @param nodes [Enumerable<Parser::AST::Node>]
|
|
204
|
+
def any_forwarded_args? nodes
|
|
205
|
+
nodes.any? do |n|
|
|
206
|
+
next false unless Parser.is_ast_node?(n)
|
|
207
|
+
next true if %i[forwarded_args forwarded_restarg].include?(n.type)
|
|
208
|
+
n.type == :hash && n.children.any? { |c| Parser.is_ast_node?(c) && c.type == :forwarded_kwrestarg }
|
|
209
|
+
end
|
|
210
|
+
end
|
|
211
|
+
|
|
200
212
|
# @todo Temporarily here for testing. Move to Solargraph::Parser.
|
|
201
213
|
# @param node [Parser::AST::Node]
|
|
202
214
|
# @return [Array<Parser::AST::Node>]
|
|
@@ -442,6 +454,11 @@ module Solargraph
|
|
|
442
454
|
CONDITIONAL_ALL_BUT_FIRST = %i[if unless].freeze
|
|
443
455
|
ONLY_ONE_CHILD = [:return].freeze
|
|
444
456
|
FIRST_TWO_CHILDREN = [:rescue].freeze
|
|
457
|
+
# :ensure's value is always its body's value (first
|
|
458
|
+
# child); the ensure clause itself (second child) never
|
|
459
|
+
# contributes to the method's return value unless it
|
|
460
|
+
# explicitly returns.
|
|
461
|
+
ENSURE = [:ensure].freeze
|
|
445
462
|
COMPOUND_STATEMENTS = %i[begin kwbegin].freeze
|
|
446
463
|
SKIPPABLE = %i[def defs class sclass module].freeze
|
|
447
464
|
FUNCTION_VALUE = [:block].freeze
|
|
@@ -488,6 +505,13 @@ module Solargraph
|
|
|
488
505
|
result.concat reduce_to_value_nodes([node.children[0]])
|
|
489
506
|
elsif FIRST_TWO_CHILDREN.include?(node.type)
|
|
490
507
|
result.concat reduce_to_value_nodes([node.children[0], node.children[1]])
|
|
508
|
+
elsif ENSURE.include?(node.type)
|
|
509
|
+
result.concat reduce_to_value_nodes([node.children[0]])
|
|
510
|
+
ensure_body = node.children[1]
|
|
511
|
+
if include_explicit_returns && ensure_body.is_a?(::Parser::AST::Node)
|
|
512
|
+
# @sg-ignore flow sensitive typing needs to narrow down type with an if is_a? check
|
|
513
|
+
result.concat explicit_return_values_from_compound_statement(ensure_body)
|
|
514
|
+
end
|
|
491
515
|
elsif FUNCTION_VALUE.include?(node.type)
|
|
492
516
|
# the block itself is a first class value that could be returned
|
|
493
517
|
result.push node
|
|
@@ -605,14 +629,13 @@ module Solargraph
|
|
|
605
629
|
elsif CONDITIONAL_ALL_BUT_FIRST.include?(node.type)
|
|
606
630
|
# @sg-ignore flow sensitive typing needs to narrow down type with an if is_a? check
|
|
607
631
|
result.concat reduce_to_value_nodes(node.children[1..])
|
|
608
|
-
|
|
609
|
-
elsif node.type == :return
|
|
632
|
+
elsif ONLY_ONE_CHILD.include?(node.type) || ENSURE.include?(node.type)
|
|
610
633
|
# @sg-ignore flow sensitive typing needs to narrow down type with an if is_a? check
|
|
611
634
|
result.concat reduce_to_value_nodes([node.children[0]])
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
#
|
|
615
|
-
result.concat reduce_to_value_nodes(node.children)
|
|
635
|
+
elsif FIRST_TWO_CHILDREN.include?(node.type)
|
|
636
|
+
# a :rescue node's value is its body's value or the
|
|
637
|
+
# value of whichever resbody handled the exception
|
|
638
|
+
result.concat reduce_to_value_nodes([node.children[0], node.children[1]])
|
|
616
639
|
# @sg-ignore flow sensitive typing needs to narrow down type with an if is_a? check
|
|
617
640
|
elsif node.type == :block
|
|
618
641
|
# @sg-ignore flow sensitive typing needs to narrow down type with an if is_a? check
|
data/lib/solargraph/parser/parser_gem/node_processors/class_definition/class_assignment_node.rb
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Solargraph
|
|
4
|
+
module Parser
|
|
5
|
+
module ParserGem
|
|
6
|
+
module NodeProcessors
|
|
7
|
+
module ClassDefinition
|
|
8
|
+
# A node wrapper for a class definition via const assignment.
|
|
9
|
+
# @example
|
|
10
|
+
# MyError = Class.new(StandardError) do
|
|
11
|
+
# def retry_after; 5; end
|
|
12
|
+
# end
|
|
13
|
+
class ClassAssignmentNode
|
|
14
|
+
class << self
|
|
15
|
+
# @example
|
|
16
|
+
# s(:casgn, nil, :Foo,
|
|
17
|
+
# s(:block,
|
|
18
|
+
# s(:send,
|
|
19
|
+
# s(:const, nil, :Class), :new,
|
|
20
|
+
# s(:const, nil, :StandardError)),
|
|
21
|
+
# s(:args),
|
|
22
|
+
# s(:def, :retry_after, s(:args), s(:int, 5))))
|
|
23
|
+
#
|
|
24
|
+
# @param node [Parser::AST::Node]
|
|
25
|
+
# @return [Boolean]
|
|
26
|
+
def match? node
|
|
27
|
+
return false unless node&.type == :casgn
|
|
28
|
+
|
|
29
|
+
class_new_node?(new_node(node))
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# The `Class.new(...)` send node, whether or not a block is attached.
|
|
33
|
+
#
|
|
34
|
+
# @param node [Parser::AST::Node]
|
|
35
|
+
# @return [Parser::AST::Node, nil]
|
|
36
|
+
def new_node node
|
|
37
|
+
value = node.children[2]
|
|
38
|
+
return nil if value.nil?
|
|
39
|
+
return value unless value.type == :block
|
|
40
|
+
|
|
41
|
+
value.children[0]
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
private
|
|
45
|
+
|
|
46
|
+
# @param send_node [Parser::AST::Node, nil]
|
|
47
|
+
# @return [Boolean]
|
|
48
|
+
def class_new_node? send_node
|
|
49
|
+
return false if send_node.nil?
|
|
50
|
+
return false unless send_node.type == :send
|
|
51
|
+
return false unless send_node.children[1] == :new
|
|
52
|
+
|
|
53
|
+
receiver = send_node.children[0]
|
|
54
|
+
return false if receiver.nil?
|
|
55
|
+
return false unless receiver.type == :const
|
|
56
|
+
|
|
57
|
+
receiver.children[1] == :Class
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# @param node [Parser::AST::Node]
|
|
62
|
+
def initialize node
|
|
63
|
+
@node = node
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# @return [String]
|
|
67
|
+
def class_name
|
|
68
|
+
namespace = node.children[0]
|
|
69
|
+
return node.children[1].to_s if namespace.nil?
|
|
70
|
+
|
|
71
|
+
"#{Parser::NodeMethods.unpack_name(namespace)}::#{node.children[1]}"
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# The superclass, when it is written as a constant. `Class.new(expr)`
|
|
75
|
+
# with a non-constant argument yields nil (superclass stays Object).
|
|
76
|
+
#
|
|
77
|
+
# @return [String, nil]
|
|
78
|
+
def superclass_name
|
|
79
|
+
send_node = self.class.new_node(node)
|
|
80
|
+
return nil if send_node.nil?
|
|
81
|
+
|
|
82
|
+
arg = send_node.children[2]
|
|
83
|
+
return nil if arg.nil?
|
|
84
|
+
return nil unless arg.type == :const
|
|
85
|
+
|
|
86
|
+
Parser::NodeMethods.unpack_name(arg)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
private
|
|
90
|
+
|
|
91
|
+
# @return [Parser::AST::Node]
|
|
92
|
+
attr_reader :node
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Solargraph
|
|
4
|
+
module Parser
|
|
5
|
+
module ParserGem
|
|
6
|
+
module NodeProcessors
|
|
7
|
+
module ClassDefinition
|
|
8
|
+
autoload :ClassAssignmentNode, 'solargraph/parser/parser_gem/node_processors/class_definition/class_assignment_node'
|
|
9
|
+
|
|
10
|
+
class ClassNode < Parser::NodeProcessor::Base
|
|
11
|
+
# @return [Boolean] continue processing the next processor of the same node.
|
|
12
|
+
def process
|
|
13
|
+
definition_node = class_definition_node
|
|
14
|
+
return true if definition_node.nil?
|
|
15
|
+
|
|
16
|
+
loc = get_node_location(node)
|
|
17
|
+
nspin = Solargraph::Pin::Namespace.new(
|
|
18
|
+
type: :class,
|
|
19
|
+
location: loc,
|
|
20
|
+
closure: region.closure,
|
|
21
|
+
name: definition_node.class_name,
|
|
22
|
+
comments: comments_for(node),
|
|
23
|
+
visibility: :public,
|
|
24
|
+
gates: region.closure.gates.freeze,
|
|
25
|
+
source: :class_definition
|
|
26
|
+
)
|
|
27
|
+
pins.push nspin
|
|
28
|
+
|
|
29
|
+
superclass_name = definition_node.superclass_name
|
|
30
|
+
if superclass_name
|
|
31
|
+
pins.push Pin::Reference::Superclass.new(
|
|
32
|
+
location: loc,
|
|
33
|
+
closure: nspin,
|
|
34
|
+
name: superclass_name,
|
|
35
|
+
source: :class_definition
|
|
36
|
+
)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
process_children region.update(closure: nspin, visibility: :public)
|
|
40
|
+
false
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
private
|
|
44
|
+
|
|
45
|
+
# @return [ClassDefinition::ClassAssignmentNode, nil]
|
|
46
|
+
def class_definition_node
|
|
47
|
+
@class_definition_node ||= ClassAssignmentNode.new(node) if ClassAssignmentNode.match?(node)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Solargraph
|
|
4
|
+
module Parser
|
|
5
|
+
module ParserGem
|
|
6
|
+
module NodeProcessors
|
|
7
|
+
module DataDefinition
|
|
8
|
+
# A node wrapper for a Data definition via const assignment.
|
|
9
|
+
# @example
|
|
10
|
+
# MyData = Data.new(:bar, :baz) do
|
|
11
|
+
# def foo
|
|
12
|
+
# end
|
|
13
|
+
# end
|
|
14
|
+
class DataAssignmentNode < DataDefintionNode
|
|
15
|
+
class << self
|
|
16
|
+
# @example
|
|
17
|
+
# s(:casgn, nil, :Foo,
|
|
18
|
+
# s(:block,
|
|
19
|
+
# s(:send,
|
|
20
|
+
# s(:const, nil, :Data), :define,
|
|
21
|
+
# s(:sym, :bar),
|
|
22
|
+
# s(:sym, :baz)),
|
|
23
|
+
# s(:args),
|
|
24
|
+
# s(:def, :foo,
|
|
25
|
+
# s(:args),
|
|
26
|
+
# s(:send, nil, :bar))))
|
|
27
|
+
# @param node [::Parser::AST::Node]
|
|
28
|
+
def match? node
|
|
29
|
+
return false unless node&.type == :casgn
|
|
30
|
+
return false if node.children[2].nil?
|
|
31
|
+
|
|
32
|
+
data_node = if node.children[2].type == :block
|
|
33
|
+
node.children[2].children[0]
|
|
34
|
+
else
|
|
35
|
+
node.children[2]
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
data_definition_node?(data_node)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def class_name
|
|
43
|
+
if node.children[0]
|
|
44
|
+
Parser::NodeMethods.unpack_name(node.children[0]) + "::#{node.children[1]}"
|
|
45
|
+
else
|
|
46
|
+
node.children[1].to_s
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
# @return [Parser::AST::Node]
|
|
53
|
+
def data_node
|
|
54
|
+
if node.children[2].type == :block
|
|
55
|
+
node.children[2].children[0]
|
|
56
|
+
else
|
|
57
|
+
node.children[2]
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|