solargraph 0.39.5 → 0.39.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 42d82e12b3dbc74f908e6fd15757e8856c7c3af088ccbfbc6724f390884f878d
4
- data.tar.gz: df04fa34d85b0633d05ab8fd216873264a4cdaa148089ffa3af909ec68a227a7
3
+ metadata.gz: 9d4f1258688f603e08ed6d5ed023579fbdd3e1829ae3d4e96fa093b08bf7377c
4
+ data.tar.gz: 817c20ca0469e682338f40e10edd3c3b21d23e5a4969c519d590ec1c995ae08b
5
5
  SHA512:
6
- metadata.gz: d1972e824e94ad830815eb780629ef985a5be4b21247f426b603e209cecc60d6c5c02219c63cdc2c6c9ab184013d72e1798c29ef90b5a91d77a92f8dd35e1295
7
- data.tar.gz: 7f5564c3de416999a40e381d853746e759898da6fa09eb6d967735478a24635f1af3cee6695894b7213c8b6746e73d8e3d57d3adc141ab505040e0eb4c8134cd
6
+ metadata.gz: 2b553ee5d770a0f3ab3fb4c4c5b386ab5dde03ba74cdffe0eb718e0d64cccd8c9e4bbfcf4b35540b2323a1c0553de4d335ca839a269887f6a6615093e5957971
7
+ data.tar.gz: 4d2b2bae341dc7102f130f04cec657766a02803ecd2f4f74a23246cdac1ef5d2f6c1d53127b4034780b949b2be3ecc3fc1969662c31329c00e4fdd7e5bb43d6a
@@ -1,5 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ Encoding.default_external = 'UTF-8'
4
+
5
+ require 'solargraph/compat'
3
6
  require 'solargraph/version'
4
7
 
5
8
  # The top-level namespace for the Solargraph code mapping, documentation,
@@ -26,7 +26,6 @@ module Solargraph
26
26
  def initialize pins: []
27
27
  @source_map_hash = {}
28
28
  @cache = Cache.new
29
- @mutex = Mutex.new
30
29
  @method_alias_stack = []
31
30
  index pins
32
31
  end
@@ -34,13 +33,11 @@ module Solargraph
34
33
  # @param pins [Array<Pin::Base>]
35
34
  # @return [self]
36
35
  def index pins
37
- @mutex.synchronize {
38
- @source_map_hash.clear
39
- @cache.clear
40
- @store = Store.new(pins + YardMap.new.pins)
41
- @unresolved_requires = []
42
- workspace_filenames.clear
43
- }
36
+ @source_map_hash.clear
37
+ @cache.clear
38
+ @store = Store.new(pins + YardMap.new.pins)
39
+ @unresolved_requires = []
40
+ workspace_filenames.clear
44
41
  self
45
42
  end
46
43
 
@@ -123,14 +120,14 @@ module Solargraph
123
120
  reqs.merge br.keys
124
121
  yard_map.change(reqs.to_a, br, bundle.workspace.gemnames)
125
122
  new_store = Store.new(pins + yard_map.pins)
126
- @mutex.synchronize {
127
- @cache.clear
128
- @source_map_hash = new_map_hash
129
- @store = new_store
130
- @unresolved_requires = yard_map.unresolved_requires
131
- workspace_filenames.clear
132
- workspace_filenames.concat bundle.workspace.filenames
133
- }
123
+ @cache.clear
124
+ @source_map_hash = new_map_hash
125
+ @store = new_store
126
+ @unresolved_requires = yard_map.unresolved_requires
127
+ workspace_filenames.clear
128
+ workspace_filenames.concat bundle.workspace.filenames
129
+ @rebindable_method_names = nil
130
+ store.block_pins.each { |blk| blk.rebind(self) }
134
131
  self
135
132
  end
136
133
 
@@ -179,6 +176,16 @@ module Solargraph
179
176
  store.pins
180
177
  end
181
178
 
179
+ def rebindable_method_names
180
+ @rebindable_method_names ||= begin
181
+ result = yard_map.rebindable_method_names
182
+ source_maps.each do |map|
183
+ result.merge map.rebindable_method_names
184
+ end
185
+ result
186
+ end
187
+ end
188
+
182
189
  # An array of pins based on Ruby keywords (`if`, `end`, etc.).
183
190
  #
184
191
  # @return [Array<Solargraph::Pin::Keyword>]
@@ -280,8 +287,7 @@ module Solargraph
280
287
 
281
288
  # @return [Array<Solargraph::Pin::GlobalVariable>]
282
289
  def get_global_variable_pins
283
- # @todo Slow version
284
- pins.select{ |p| p.is_a?(Pin::GlobalVariable) }
290
+ store.pins_by_class(Pin::GlobalVariable)
285
291
  end
286
292
 
287
293
  # Get an array of methods available in a particular context.
@@ -535,17 +541,17 @@ module Solargraph
535
541
  #
536
542
  # @return [Hash{String => SourceMap}]
537
543
  def source_map_hash
538
- @mutex.synchronize { @source_map_hash }
544
+ @source_map_hash
539
545
  end
540
546
 
541
547
  # @return [ApiMap::Store]
542
548
  def store
543
- @mutex.synchronize { @store }
549
+ @store
544
550
  end
545
551
 
546
552
  # @return [Solargraph::ApiMap::Cache]
547
553
  def cache
548
- @mutex.synchronize { @cache }
554
+ @cache
549
555
  end
550
556
 
551
557
  # @param fqns [String] A fully qualified namespace
@@ -10,6 +10,7 @@ module Solargraph
10
10
  @receiver_definitions = {}
11
11
  end
12
12
 
13
+ # @return [Array<Pin::BaseMethod>]
13
14
  def get_methods fqns, scope, visibility, deep
14
15
  @methods[[fqns, scope, visibility.sort, deep]]
15
16
  end
@@ -18,6 +19,7 @@ module Solargraph
18
19
  @methods[[fqns, scope, visibility.sort, deep]] = value
19
20
  end
20
21
 
22
+ # @return [Array<Pin::Base>]
21
23
  def get_constants namespace, context
22
24
  @constants[[namespace, context]]
23
25
  end
@@ -26,6 +28,7 @@ module Solargraph
26
28
  @constants[[namespace, context]] = value
27
29
  end
28
30
 
31
+ # @return [String]
29
32
  def get_qualified_namespace name, context
30
33
  @qualified_namespaces[[name, context]]
31
34
  end
@@ -38,6 +41,7 @@ module Solargraph
38
41
  @receiver_definitions.key? path
39
42
  end
40
43
 
44
+ # @return [Pin::BaseMethod]
41
45
  def get_receiver_definition path
42
46
  @receiver_definitions[path]
43
47
  end
@@ -99,12 +99,12 @@ module Solargraph
99
99
 
100
100
  # @return [Array<Solargraph::Pin::Base>]
101
101
  def namespace_pins
102
- @namespace_pins ||= []
102
+ pins_by_class(Solargraph::Pin::Namespace)
103
103
  end
104
104
 
105
- # @return [Array<Solargraph::Pin::Base>]
105
+ # @return [Array<Solargraph::Pin::BaseMethod>]
106
106
  def method_pins
107
- @method_pins ||= []
107
+ pins_by_class(Solargraph::Pin::BaseMethod)
108
108
  end
109
109
 
110
110
  # @param fqns [String]
@@ -133,7 +133,7 @@ module Solargraph
133
133
 
134
134
  # @return [Array<Pin::Block>]
135
135
  def block_pins
136
- @block_pins ||= []
136
+ pins_by_class(Pin::Block)
137
137
  end
138
138
 
139
139
  def inspect
@@ -141,6 +141,12 @@ module Solargraph
141
141
  to_s
142
142
  end
143
143
 
144
+ # @param klass [Class]
145
+ # @return [Array<Solargraph::Pin::Base>]
146
+ def pins_by_class klass
147
+ @pin_select_cache[klass] ||= @pin_class_hash.select { |key, _| key <= klass }.values.flatten
148
+ end
149
+
144
150
  private
145
151
 
146
152
  # @param fqns [String]
@@ -167,7 +173,7 @@ module Solargraph
167
173
 
168
174
  # @return [Array<Solargraph::Pin::Symbol>]
169
175
  def symbols
170
- @symbols ||= []
176
+ pins_by_class(Pin::Symbol)
171
177
  end
172
178
 
173
179
  def superclass_references
@@ -198,7 +204,7 @@ module Solargraph
198
204
  end
199
205
 
200
206
  def all_instance_variables
201
- @all_instance_variables ||= []
207
+ pins_by_class(Pin::InstanceVariable)
202
208
  end
203
209
 
204
210
  def path_pin_hash
@@ -207,48 +213,29 @@ module Solargraph
207
213
 
208
214
  # @return [void]
209
215
  def index
210
- namespace_map.clear
211
- namespaces.clear
212
- namespace_pins.clear
213
- method_pins.clear
214
- symbols.clear
215
- block_pins.clear
216
- all_instance_variables.clear
217
- path_pin_hash.clear
218
- namespace_map[''] = []
219
- override_pins = []
220
- pins.each do |pin|
221
- namespace_map[pin.namespace] ||= []
222
- namespace_map[pin.namespace].push pin
223
- namespaces.add pin.path if pin.is_a?(Pin::Namespace) && !pin.path.empty?
224
- namespace_pins.push pin if pin.is_a?(Pin::Namespace)
225
- method_pins.push pin if pin.is_a?(Pin::BaseMethod)
226
- symbols.push pin if pin.is_a?(Pin::Symbol)
227
- if pin.is_a?(Pin::Reference::Include)
228
- include_references[pin.namespace] ||= []
229
- include_references[pin.namespace].push pin.name
230
- elsif pin.is_a?(Pin::Reference::Prepend)
231
- prepend_references[pin.namespace] ||= []
232
- prepend_references[pin.namespace].push pin.name
233
- elsif pin.is_a?(Pin::Reference::Extend)
234
- extend_references[pin.namespace] ||= []
235
- extend_references[pin.namespace].push pin.name
236
- elsif pin.is_a?(Pin::Reference::Superclass)
237
- superclass_references[pin.namespace] ||= []
238
- superclass_references[pin.namespace].push pin.name
239
- elsif pin.is_a?(Pin::Block)
240
- block_pins.push pin
241
- elsif pin.is_a?(Pin::InstanceVariable)
242
- all_instance_variables.push pin
243
- elsif pin.is_a?(Pin::Reference::Override)
244
- override_pins.push pin
245
- end
246
- if pin.path
247
- path_pin_hash[pin.path] ||= []
248
- path_pin_hash[pin.path].push pin
249
- end
216
+ set = pins.to_set
217
+ @pin_class_hash = set.classify(&:class).transform_values(&:to_a)
218
+ @pin_select_cache = {}
219
+ @namespace_map = set.classify(&:namespace).transform_values(&:to_a)
220
+ @path_pin_hash = set.classify(&:path).transform_values(&:to_a)
221
+ @namespaces = @path_pin_hash.keys.compact
222
+ pins_by_class(Pin::Reference::Include).each do |pin|
223
+ include_references[pin.namespace] ||= []
224
+ include_references[pin.namespace].push pin.name
225
+ end
226
+ pins_by_class(Pin::Reference::Prepend).each do |pin|
227
+ prepend_references[pin.namespace] ||= []
228
+ prepend_references[pin.namespace].push pin.name
229
+ end
230
+ pins_by_class(Pin::Reference::Extend).each do |pin|
231
+ extend_references[pin.namespace] ||= []
232
+ extend_references[pin.namespace].push pin.name
233
+ end
234
+ pins_by_class(Pin::Reference::Superclass).each do |pin|
235
+ superclass_references[pin.namespace] ||= []
236
+ superclass_references[pin.namespace].push pin.name
250
237
  end
251
- override_pins.each do |ovr|
238
+ pins_by_class(Pin::Reference::Override).each do |ovr|
252
239
  pin = get_path_pins(ovr.name).first
253
240
  next if pin.nil?
254
241
  (ovr.tags.map(&:tag_name) + ovr.delete).uniq.each do |tag|
@@ -261,6 +248,7 @@ module Solargraph
261
248
  # @todo This is probably not the best place for these overrides
262
249
  superclass_references['Integer'] = ['Numeric']
263
250
  superclass_references['Float'] = ['Numeric']
251
+ superclass_references['File'] = ['IO']
264
252
  end
265
253
  end
266
254
  end
@@ -0,0 +1,9 @@
1
+ unless Hash.method_defined?(:transform_values)
2
+ class Hash
3
+ def transform_values &block
4
+ each_pair do |k, v|
5
+ self[k] = block.call(v)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -99,7 +99,7 @@ module Solargraph
99
99
  message = Message.select(request['method']).new(self, request)
100
100
  begin
101
101
  message.process
102
- rescue Exception => e
102
+ rescue StandardError => e
103
103
  logger.warn "Error processing request: [#{e.class}] #{e.message}"
104
104
  logger.warn e.backtrace.join("\n")
105
105
  message.set_error Solargraph::LanguageServer::ErrorCodes::INTERNAL_ERROR, "[#{e.class}] #{e.message}"
@@ -34,6 +34,7 @@ module Solargraph
34
34
  .gsub('%3A', ':')
35
35
  .gsub('%5C', '\\')
36
36
  .gsub('%2F', '/')
37
+ .gsub('+', '%20')
37
38
  end
38
39
 
39
40
  # Decode text from a URI path component in LSP.
@@ -339,7 +339,7 @@ module Solargraph
339
339
  logger.info "Cataloging #{workspace.directory.empty? ? 'generic workspace' : workspace.directory}"
340
340
  api_map.catalog bundle
341
341
  @synchronized = true
342
- logger.info "Catalog complete (#{api_map.pins.length} pins)"
342
+ logger.info "Catalog complete (#{api_map.source_maps.length} files, #{api_map.pins.length} pins)" if logger.info?
343
343
  end
344
344
  end
345
345
 
@@ -36,6 +36,8 @@ module Solargraph
36
36
  process_prepend
37
37
  elsif node.children[1] == :require
38
38
  process_require
39
+ elsif node.children[1] == :autoload
40
+ process_autoload
39
41
  elsif node.children[1] == :private_constant
40
42
  process_private_constant
41
43
  elsif node.children[1] == :alias_method && node.children[2] && node.children[2] && node.children[2].type == :sym && node.children[3] && node.children[3].type == :sym
@@ -138,6 +140,14 @@ module Solargraph
138
140
  end
139
141
  end
140
142
 
143
+ # @return [void]
144
+ def process_autoload
145
+ if node.children[3].is_a?(AST::Node) && node.children[3].type == :str
146
+ path = node.children[3].children[0].to_s
147
+ pins.push Pin::Reference::Require.new(get_node_location(node), path)
148
+ end
149
+ end
150
+
141
151
  # @return [void]
142
152
  def process_module_function
143
153
  if node.children[2].nil?
@@ -95,6 +95,7 @@ module Solargraph
95
95
  index = 0
96
96
  until index > node.children[0].children.length - 2
97
97
  k = node.children[0].children[index]
98
+ return {} unless node?(k)
98
99
  v = node.children[0].children[index + 1]
99
100
  result[k.children[0]] = Solargraph::Parser.chain(v)
100
101
  index += 2
@@ -16,7 +16,12 @@ module Solargraph
16
16
  presence: region.closure.location.range,
17
17
  decl: require_keyword?(node) ? :kwarg : :kwoptarg
18
18
  )
19
- region.closure.parameters.push locals.last
19
+ idx = region.closure.parameters.find_index { |par| [:kwrestarg, :blockarg].include?(par.decl) }
20
+ if idx
21
+ region.closure.parameters.insert idx, locals.last
22
+ else
23
+ region.closure.parameters.push locals.last
24
+ end
20
25
  node.children[1] && NodeProcessor.process(node.children[1], region, pins, locals)
21
26
  end
22
27
 
@@ -16,7 +16,12 @@ module Solargraph
16
16
  presence: region.closure.location.range,
17
17
  decl: :optarg
18
18
  )
19
- region.closure.parameters.push locals.last
19
+ idx = region.closure.parameters.find_index { |par| par.decl != :arg }
20
+ if idx
21
+ region.closure.parameters.insert idx, locals.last
22
+ else
23
+ region.closure.parameters.push locals.last
24
+ end
20
25
  node.children[1] && NodeProcessor.process(node.children[1], region, pins, locals)
21
26
  end
22
27
  end
@@ -28,6 +28,8 @@ module Solargraph
28
28
  process_module_function
29
29
  elsif node.children[0] == :require
30
30
  process_require
31
+ elsif node.children[0] == :autoload
32
+ process_autoload
31
33
  elsif node.children[0] == :alias_method
32
34
  process_alias_method
33
35
  elsif node.children[0] == :private_class_method
@@ -143,6 +145,15 @@ module Solargraph
143
145
  end
144
146
  end
145
147
 
148
+ # @return [void]
149
+ def process_autoload
150
+ return unless Parser.is_ast_node?(node.children[1]) && Parser.is_ast_node?(node.children[1].children[1])
151
+ arg = node.children[1].children[1]
152
+ if arg.type == :STR
153
+ pins.push Pin::Reference::Require.new(get_node_location(arg), arg.children[0])
154
+ end
155
+ end
156
+
146
157
  # @return [void]
147
158
  def process_module_function
148
159
  if node.type == :VCALL
@@ -23,7 +23,7 @@ module Solargraph
23
23
 
24
24
  # @param location [Solargraph::Location]
25
25
  # @param kind [Integer]
26
- # @param closure [String]
26
+ # @param closure [Solargraph::Pin::Closure]
27
27
  # @param name [String]
28
28
  # @param comments [String]
29
29
  def initialize location: nil, closure: nil, name: '', comments: ''
@@ -38,7 +38,7 @@ module Solargraph
38
38
  @comments ||= ''
39
39
  end
40
40
 
41
- # @return [String]
41
+ # @return [String, nil]
42
42
  def filename
43
43
  return nil if location.nil?
44
44
  location.filename
@@ -14,17 +14,14 @@ module Solargraph
14
14
  @parameters = args
15
15
  end
16
16
 
17
- def rebind context
18
- @rebound = true
19
- @binder = context unless context.undefined?
20
- end
21
-
22
- def rebound?
23
- @rebound ||= false
17
+ # @param api_map [ApiMap]
18
+ # @return [void]
19
+ def rebind api_map
20
+ @binder ||= binder_or_nil(api_map)
24
21
  end
25
22
 
26
23
  def binder
27
- @binder || context
24
+ @binder || closure.binder
28
25
  end
29
26
 
30
27
  # @return [Array<String>]
@@ -37,11 +34,28 @@ module Solargraph
37
34
  @parameter_names ||= parameters.map(&:name)
38
35
  end
39
36
 
40
- def nearly? other
41
- return false unless super
42
- # @todo Trying to not to block merges too much
43
- # receiver == other.receiver and parameters == other.parameters
44
- true
37
+ private
38
+
39
+ # @param api_map [ApiMap]
40
+ # @return [ComplexType, nil]
41
+ def binder_or_nil api_map
42
+ return nil unless receiver
43
+ word = receiver.children.find { |c| c.is_a?(::Symbol) }.to_s
44
+ return nil unless api_map.rebindable_method_names.include?(word)
45
+ chain = Parser.chain(receiver, location.filename)
46
+ locals = api_map.source_map(location.filename).locals_at(location)
47
+ if ['instance_eval', 'instance_exec', 'class_eval', 'class_exec', 'module_eval', 'module_exec'].include?(chain.links.last.word)
48
+ return chain.base.infer(api_map, self, locals)
49
+ else
50
+ receiver_pin = chain.define(api_map, self, locals).first
51
+ if receiver_pin && receiver_pin.docstring
52
+ ys = receiver_pin.docstring.tag(:yieldself)
53
+ if ys && ys.types && !ys.types.empty?
54
+ return ComplexType.try_parse(*ys.types).qualify(api_map, receiver_pin.context.namespace)
55
+ end
56
+ end
57
+ end
58
+ nil
45
59
  end
46
60
  end
47
61
  end
@@ -5,7 +5,7 @@ module Solargraph
5
5
  class Method < BaseMethod
6
6
  include Solargraph::Parser::NodeMethods
7
7
 
8
- # @return [Array<String>]
8
+ # @return [Array<Pin::Parameter>]
9
9
  attr_reader :parameters
10
10
 
11
11
  # @param args [Array<String>]
@@ -41,7 +41,7 @@ module Solargraph
41
41
  "*#{name}"
42
42
  when :kwrestarg
43
43
  "**#{name}"
44
- when :block
44
+ when :block, :blockarg
45
45
  "&#{name}"
46
46
  else
47
47
  name
@@ -175,7 +175,7 @@ module Solargraph
175
175
  puts pin_description(pin) if options[:verbose]
176
176
  pin.typify api_map
177
177
  pin.probe api_map
178
- rescue Exception => e
178
+ rescue StandardError => e
179
179
  STDERR.puts "Error testing #{pin_description(pin)} #{pin.location ? "at #{pin.location.filename}:#{pin.location.range.start.line + 1}" : ''}"
180
180
  STDERR.puts "[#{e.class}]: #{e.message}"
181
181
  STDERR.puts e.backtrace.join("\n")
@@ -57,7 +57,6 @@ module Solargraph
57
57
  # @param locals [Array<Pin::Base>]
58
58
  # @return [Array<Pin::Base>]
59
59
  def define api_map, name_pin, locals
60
- rebind_block name_pin, api_map, locals
61
60
  return [] if undefined?
62
61
  working_pin = name_pin
63
62
  links[0..-2].each do |link|
@@ -79,7 +78,6 @@ module Solargraph
79
78
  # @param locals [Array<Pin::Base>]
80
79
  # @return [ComplexType]
81
80
  def infer api_map, name_pin, locals
82
- rebind_block name_pin, api_map, locals
83
81
  pins = define(api_map, name_pin, locals)
84
82
  infer_first_defined(pins, links.last.last_context, api_map)
85
83
  end
@@ -138,35 +136,6 @@ module Solargraph
138
136
  return type if context.nil? || context.return_type.undefined?
139
137
  type.self_to(context.return_type.namespace)
140
138
  end
141
-
142
- def skippable_block_receivers api_map
143
- @@skippable_block_receivers ||= (
144
- api_map.get_methods('Array', deep: false).map(&:name) +
145
- api_map.get_methods('Enumerable', deep: false).map(&:name) +
146
- api_map.get_methods('Hash', deep: false).map(&:name) +
147
- ['new']
148
- ).to_set
149
- end
150
-
151
- def rebind_block pin, api_map, locals
152
- return unless pin.is_a?(Pin::Block) && pin.receiver && !pin.rebound?
153
- # This first rebind just sets the block pin's rebound state
154
- pin.rebind ComplexType::UNDEFINED
155
- chain = Parser.chain(pin.receiver, pin.location.filename)
156
- return if skippable_block_receivers(api_map).include?(chain.links.last.word)
157
- if ['instance_eval', 'instance_exec', 'class_eval', 'class_exec', 'module_eval', 'module_exec'].include?(chain.links.last.word)
158
- type = chain.base.infer(api_map, pin, locals)
159
- pin.rebind type
160
- else
161
- receiver_pin = chain.define(api_map, pin, locals).first
162
- return if receiver_pin.nil? || receiver_pin.docstring.nil?
163
- ys = receiver_pin.docstring.tag(:yieldself)
164
- unless ys.nil? || ys.types.nil? || ys.types.empty?
165
- ysct = ComplexType.try_parse(*ys.types).qualify(api_map, receiver_pin.context.namespace)
166
- pin.rebind ysct
167
- end
168
- end
169
- end
170
139
  end
171
140
  end
172
141
  end
@@ -17,7 +17,6 @@ module Solargraph
17
17
  # @param position [Position]
18
18
  # @return [Source::Chain]
19
19
  def chain source, position
20
- # raise "Not a source" unless source.is_a?(Source)
21
20
  new(source, position).chain
22
21
  end
23
22
  end
@@ -32,6 +31,8 @@ module Solargraph
32
31
 
33
32
  # @return [Source::Chain]
34
33
  def chain
34
+ # Special handling for files that end with an integer and a period
35
+ return Chain.new([Chain::Literal.new('Integer'), Chain::UNDEFINED_CALL]) if phrase =~ /^[0-9]+\.$/
35
36
  return Chain.new([Chain::Literal.new('Symbol')]) if phrase.start_with?(':') && !phrase.start_with?('::')
36
37
  begin
37
38
  return Chain.new([]) if phrase.end_with?('..')
@@ -3,6 +3,7 @@
3
3
  require 'jaro_winkler'
4
4
  require 'yard'
5
5
  require 'yard-solargraph'
6
+ require 'set'
6
7
 
7
8
  module Solargraph
8
9
  # An index of pins and other ApiMap-related data for a Source.
@@ -30,6 +31,19 @@ module Solargraph
30
31
  @pins = pins
31
32
  @locals = locals
32
33
  environ.merge Convention.for(source)
34
+ @pin_class_hash = pins.to_set.classify(&:class).transform_values(&:to_a)
35
+ @pin_select_cache = {}
36
+ end
37
+
38
+ def pins_by_class klass
39
+ @pin_select_cache[klass] ||= @pin_class_hash.select { |key, _| key <= klass }.values.flatten
40
+ end
41
+
42
+ def rebindable_method_names
43
+ @rebindable_method_names ||= pins_by_class(Pin::Method)
44
+ .select { |pin| pin.comments && pin.comments.include?('@yieldself') }
45
+ .map(&:name)
46
+ .to_set
33
47
  end
34
48
 
35
49
  # @return [String]
@@ -44,7 +58,7 @@ module Solargraph
44
58
 
45
59
  # @return [Array<Pin::Reference::Require>]
46
60
  def requires
47
- @requires ||= pins.select{ |p| p.is_a?(Pin::Reference::Require) }
61
+ pins_by_class(Pin::Reference::Require)
48
62
  end
49
63
 
50
64
  # @return [Environ]
@@ -62,6 +76,7 @@ module Solargraph
62
76
  # @param query [String]
63
77
  # @return [Array<Pin::Base>]
64
78
  def query_symbols query
79
+ return document_symbols if query && query.empty?
65
80
  document_symbols.select{ |pin| fuzzy_string_match(pin.path, query) || fuzzy_string_match(pin.name, query) }
66
81
  end
67
82
 
@@ -82,6 +82,8 @@ module Solargraph
82
82
  # @param comment [String]
83
83
  # @return [Integer]
84
84
  def find_directive_line_number comment, tag, start
85
+ # Avoid overruning the index
86
+ return start unless start < comment.lines.length
85
87
  num = comment.lines[start..-1].find_index do |line|
86
88
  # Legacy method directives might be `@method` instead of `@!method`
87
89
  # @todo Legacy syntax should probably emit a warning
@@ -93,6 +95,7 @@ module Solargraph
93
95
  # @param source_position [Position]
94
96
  # @param comment_position [Position]
95
97
  # @param directive [YARD::Tags::Directive]
98
+ # @return [void]
96
99
  def process_directive source_position, comment_position, directive
97
100
  docstring = Solargraph::Source.parse_docstring(directive.tag.text).to_docstring
98
101
  location = Location.new(@filename, Range.new(comment_position, comment_position))
@@ -102,10 +105,10 @@ module Solargraph
102
105
  if namespace.location.range.start.line < comment_position.line
103
106
  namespace = closure_at(comment_position)
104
107
  end
105
- region = Parser::Region.new(source: @source, closure: namespace)
106
108
  begin
107
- src_node = Parser.parse("def #{directive.tag.name};end", @filename, location.range.start.line)
108
- gen_pin = Parser.process_node(src_node, region).first.last
109
+ src = Solargraph::Source.load_string("def #{directive.tag.name};end", @source.filename)
110
+ region = Parser::Region.new(source: src, closure: namespace)
111
+ gen_pin = Parser.process_node(src.node, region).first.last
109
112
  return if gen_pin.nil?
110
113
  # Move the location to the end of the line so it gets recognized
111
114
  # as originating from a comment
@@ -146,12 +149,23 @@ module Solargraph
146
149
  )
147
150
  end
148
151
  when 'parse'
149
- ns = closure_at(source_position)
150
- region = Parser::Region.new(source: @source, closure: ns)
151
152
  begin
152
- node = Parser.parse(directive.tag.text, @filename, comment_position.line)
153
+ ns = closure_at(source_position)
154
+ src = Solargraph::Source.load_string(directive.tag.text, @source.filename)
155
+ region = Parser::Region.new(source: src, closure: ns)
153
156
  # @todo These pins may need to be marked not explicit
154
- Parser.process_node(node, region, @pins)
157
+ index = @pins.length
158
+ loff = if @code.lines[comment_position.line].strip.end_with?('@!parse')
159
+ comment_position.line + 1
160
+ else
161
+ comment_position.line
162
+ end
163
+ Parser.process_node(src.node, region, @pins)
164
+ @pins[index..-1].each do |p|
165
+ # @todo Smelly instance variable access
166
+ p.location.range.start.instance_variable_set(:@line, p.location.range.start.line + loff)
167
+ p.location.range.ending.instance_variable_set(:@line, p.location.range.ending.line + loff)
168
+ end
155
169
  rescue Parser::SyntaxError => e
156
170
  # @todo Handle parser errors in !parse directives
157
171
  end
@@ -20,6 +20,14 @@ module Solargraph
20
20
  Override.method_return('Pathname#cleanpath', 'Pathname'),
21
21
  Override.method_return('Pathname#children', 'Array<Pathname>'),
22
22
  Override.method_return('Pathname#entries', 'Array<Pathname>')
23
+ ],
24
+
25
+ 'set' => [
26
+ Override.method_return('Enumerable#to_set', 'Set'),
27
+ Override.method_return('Set#add', 'self'),
28
+ Override.method_return('Set#add?', 'self, nil'),
29
+ Override.method_return('Set#classify', 'Hash'),
30
+ Override.from_comment('Set#each', '@yieldparam_single_parameter')
23
31
  ]
24
32
  }
25
33
 
@@ -74,7 +74,7 @@ module Solargraph
74
74
  def method_tag_problems
75
75
  result = []
76
76
  # @param pin [Pin::BaseMethod]
77
- source_map.pins.select { |pin| pin.is_a?(Pin::BaseMethod) }.each do |pin|
77
+ source_map.pins_by_class(Pin::BaseMethod).each do |pin|
78
78
  result.concat method_return_type_problems_for(pin)
79
79
  result.concat method_param_type_problems_for(pin)
80
80
  end
@@ -184,8 +184,7 @@ module Solargraph
184
184
 
185
185
  # @return [Array<Pin::BaseVariable>]
186
186
  def all_variables
187
- source_map.pins.select { |pin| pin.is_a?(Pin::BaseVariable) } +
188
- source_map.locals.select { |pin| pin.is_a?(Pin::LocalVariable) }
187
+ source_map.pins_by_class(Pin::BaseVariable) + source_map.locals.select { |pin| pin.is_a?(Pin::LocalVariable) }
189
188
  end
190
189
 
191
190
  def const_problems
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Solargraph
4
- VERSION = '0.39.5'
4
+ VERSION = '0.39.10'
5
5
  end
@@ -170,7 +170,7 @@ module Solargraph
170
170
  Dir.chdir base do
171
171
  begin
172
172
  # @type [Gem::Specification]
173
- spec = eval(File.read(file), nil, file)
173
+ spec = eval(File.read(file), TOPLEVEL_BINDING, file)
174
174
  next unless Gem::Specification === spec
175
175
  @gemnames.push spec.name
176
176
  result.concat(spec.require_paths.map { |path| File.join(base, path) })
@@ -3,6 +3,7 @@
3
3
  require 'yard'
4
4
  require 'yard-solargraph'
5
5
  require 'rubygems/package'
6
+ require 'set'
6
7
 
7
8
  module Solargraph
8
9
  # The YardMap provides access to YARD documentation for the Ruby core, the
@@ -56,6 +57,7 @@ module Solargraph
56
57
  @source_gems = []
57
58
  process_requires
58
59
  yardocs.uniq!
60
+ @pin_select_cache = {}
59
61
  end
60
62
 
61
63
  # @return [Array<Solargraph::Pin::Base>]
@@ -80,10 +82,22 @@ module Solargraph
80
82
  @gemset = new_gemset
81
83
  @source_gems = source_gems
82
84
  process_requires
85
+ @rebindable_method_names = nil
86
+ @pin_class_hash = nil
87
+ @pin_select_cache = {}
83
88
  true
84
89
  end
85
90
  end
86
91
 
92
+ # @return [Set<String>]
93
+ def rebindable_method_names
94
+ @rebindable_method_names ||= pins_by_class(Pin::Method)
95
+ .select { |pin| pin.comments && pin.comments.include?('@yieldself') }
96
+ .map(&:name)
97
+ .concat(['instance_eval', 'instance_exec', 'class_eval', 'class_exec', 'module_eval', 'module_exec'])
98
+ .to_set
99
+ end
100
+
87
101
  # @return [Array<String>]
88
102
  def yardocs
89
103
  @yardocs ||= []
@@ -102,7 +116,7 @@ module Solargraph
102
116
  else
103
117
  YARD::Registry.load! y
104
118
  end
105
- rescue Exception => e
119
+ rescue StandardError => e
106
120
  Solargraph::Logging.logger.warn "Error loading yardoc '#{y}' #{e.class} #{e.message}"
107
121
  yardocs.delete y
108
122
  nil
@@ -144,6 +158,14 @@ module Solargraph
144
158
  @cache ||= YardMap::Cache.new
145
159
  end
146
160
 
161
+ def pin_class_hash
162
+ @pin_class_hash ||= pins.to_set.classify(&:class).transform_values(&:to_a)
163
+ end
164
+
165
+ def pins_by_class klass
166
+ @pin_select_cache[klass] ||= pin_class_hash.select { |key, _| key <= klass }.values.flatten
167
+ end
168
+
147
169
  # @param ns [YARD::CodeObjects::NamespaceObject]
148
170
  # @return [Array<YARD::CodeObjects::Base>]
149
171
  def recurse_namespace_object ns
@@ -15,23 +15,23 @@ module Solargraph
15
15
  # @return [void]
16
16
  def self.run spec, cache_dir: nil
17
17
  Dir.mktmpdir do |tmpdir|
18
- rdir = File.join(tmpdir, 'rdoc')
19
- Dir.chdir spec.full_gem_path do
18
+ rdir = File.join(tmpdir, 'sg_tmp_rdoc')
19
+ FileUtils.cp_r Dir.glob(File.join(spec.full_gem_path, '*')), tmpdir
20
+ Dir.chdir tmpdir do
20
21
  pins = []
21
22
  pins.push Solargraph::Pin::ROOT_PIN
22
23
  name_hash = {}
23
24
 
24
- argv = ['-q', '-N', '-r', '-o', rdir]
25
+ argv = ['-q', '-r', '-N', '-o', rdir]
25
26
  spec.load_paths.each do |path|
26
27
  argv.concat ['-i', path]
27
28
  end
28
29
  rdoc = RDoc::RDoc.new
29
30
  rdoc.document argv
30
-
31
- store = RDoc::Store.new(rdir)
32
- store.load_all
31
+ # @type [RDoc::Store]
32
+ store = rdoc.store
33
+ store.path = rdir
33
34
  store.cache[:modules].each do |mod|
34
- # store.load_class(mod)
35
35
  # @type [RDoc::NormalClass]
36
36
  mod = store.find_class_or_module(mod)
37
37
  closure = pins.select { |pin| pin.path == mod.full_name.split('::')[0..-2].join('::') }.first || pins.first
@@ -51,7 +51,7 @@ module Solargraph
51
51
  closure: namepin
52
52
  )
53
53
  end
54
- # @param inc [RDoc::Extend]
54
+ # @param ext [RDoc::Extend]
55
55
  mod.extends.each do |ext|
56
56
  pins.push Solargraph::Pin::Reference::Extend.new(
57
57
  location: locate(ext),
@@ -105,7 +105,11 @@ module Solargraph
105
105
  mod.full_name.split('::')[0..-2].join('::')
106
106
  end
107
107
 
108
+ # @param cmnt [RDoc::Comment]
109
+ # @return [String]
108
110
  def self.commentary cmnt
111
+ return cmnt if cmnt.is_a?(String)
112
+ return cmnt.text if cmnt.is_a?(RDoc::Comment)
109
113
  result = []
110
114
  cmnt.parts.each do |part|
111
115
  result.push RDoc::Markup::ToHtml.new({}).to_html(part.text) if part.respond_to?(:text)
@@ -126,9 +130,9 @@ module Solargraph
126
130
 
127
131
  def self.find_file obj
128
132
  if obj.respond_to?(:in_files) && !obj.in_files.empty?
129
- [obj.in_files.first.to_s.sub(/^file /, ''), obj.in_files.first.line]
133
+ [obj.in_files.first.to_s.sub(/^file /, ''), obj.line]
130
134
  else
131
- [obj.file_name, obj.line]
135
+ [obj.file, obj.line]
132
136
  end
133
137
  end
134
138
  end
@@ -27,7 +27,7 @@ Gem::Specification.new do |s|
27
27
  s.add_runtime_dependency 'maruku', '~> 0.7', '>= 0.7.3'
28
28
  s.add_runtime_dependency 'nokogiri', '~> 1.9', '>= 1.9.1'
29
29
  s.add_runtime_dependency 'parser', '~> 2.3'
30
- s.add_runtime_dependency 'reverse_markdown', '~> 1.0', '>= 1.0.5'
30
+ s.add_runtime_dependency 'reverse_markdown', '>= 1.0.5', '< 3'
31
31
  s.add_runtime_dependency 'rubocop', '~> 0.52'
32
32
  s.add_runtime_dependency 'thor', '~> 1.0'
33
33
  s.add_runtime_dependency 'tilt', '~> 2.0'
Binary file
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: solargraph
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.39.5
4
+ version: 0.39.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fred Snyder
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-02 00:00:00.000000000 Z
11
+ date: 2020-07-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: backport
@@ -138,22 +138,22 @@ dependencies:
138
138
  name: reverse_markdown
139
139
  requirement: !ruby/object:Gem::Requirement
140
140
  requirements:
141
- - - "~>"
142
- - !ruby/object:Gem::Version
143
- version: '1.0'
144
141
  - - ">="
145
142
  - !ruby/object:Gem::Version
146
143
  version: 1.0.5
144
+ - - "<"
145
+ - !ruby/object:Gem::Version
146
+ version: '3'
147
147
  type: :runtime
148
148
  prerelease: false
149
149
  version_requirements: !ruby/object:Gem::Requirement
150
150
  requirements:
151
- - - "~>"
152
- - !ruby/object:Gem::Version
153
- version: '1.0'
154
151
  - - ">="
155
152
  - !ruby/object:Gem::Version
156
153
  version: 1.0.5
154
+ - - "<"
155
+ - !ruby/object:Gem::Version
156
+ version: '3'
157
157
  - !ruby/object:Gem::Dependency
158
158
  name: rubocop
159
159
  requirement: !ruby/object:Gem::Requirement
@@ -317,6 +317,7 @@ files:
317
317
  - lib/solargraph/api_map/source_to_yard.rb
318
318
  - lib/solargraph/api_map/store.rb
319
319
  - lib/solargraph/bundle.rb
320
+ - lib/solargraph/compat.rb
320
321
  - lib/solargraph/complex_type.rb
321
322
  - lib/solargraph/complex_type/type_methods.rb
322
323
  - lib/solargraph/complex_type/unique_type.rb