solargraph 0.26.1 → 0.27.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/solargraph.rb +5 -2
- data/lib/solargraph/api_map.rb +236 -234
- data/lib/solargraph/api_map/store.rb +18 -53
- data/lib/solargraph/bundle.rb +22 -0
- data/lib/solargraph/complex_type.rb +9 -5
- data/lib/solargraph/complex_type/type_methods.rb +113 -0
- data/lib/solargraph/complex_type/unique_type.rb +35 -0
- data/lib/solargraph/core_fills.rb +1 -0
- data/lib/solargraph/diagnostics.rb +6 -4
- data/lib/solargraph/diagnostics/base.rb +3 -0
- data/lib/solargraph/diagnostics/require_not_found.rb +2 -1
- data/lib/solargraph/diagnostics/rubocop.rb +21 -6
- data/lib/solargraph/diagnostics/type_not_defined.rb +4 -3
- data/lib/solargraph/diagnostics/update_errors.rb +18 -0
- data/lib/solargraph/language_server/host.rb +90 -222
- data/lib/solargraph/language_server/host/cataloger.rb +68 -0
- data/lib/solargraph/language_server/host/diagnoser.rb +85 -0
- data/lib/solargraph/language_server/message/extended/check_gem_version.rb +35 -24
- data/lib/solargraph/language_server/message/text_document/completion.rb +6 -8
- data/lib/solargraph/language_server/message/text_document/document_symbol.rb +1 -1
- data/lib/solargraph/language_server/message/workspace/did_change_watched_files.rb +0 -1
- data/lib/solargraph/language_server/transport/socket.rb +4 -6
- data/lib/solargraph/language_server/transport/stdio.rb +4 -6
- data/lib/solargraph/library.rb +152 -99
- data/lib/solargraph/live_map.rb +1 -1
- data/lib/solargraph/location.rb +28 -0
- data/lib/solargraph/pin.rb +2 -0
- data/lib/solargraph/pin/attribute.rb +26 -12
- data/lib/solargraph/pin/base.rb +15 -35
- data/lib/solargraph/pin/base_variable.rb +7 -15
- data/lib/solargraph/pin/block.rb +5 -9
- data/lib/solargraph/pin/block_parameter.rb +9 -7
- data/lib/solargraph/pin/conversions.rb +5 -5
- data/lib/solargraph/pin/duck_method.rb +1 -1
- data/lib/solargraph/pin/instance_variable.rb +0 -4
- data/lib/solargraph/pin/keyword.rb +4 -0
- data/lib/solargraph/pin/localized.rb +5 -3
- data/lib/solargraph/pin/method.rb +11 -0
- data/lib/solargraph/pin/namespace.rb +7 -3
- data/lib/solargraph/pin/proxy_type.rb +3 -7
- data/lib/solargraph/pin/reference.rb +2 -2
- data/lib/solargraph/pin/symbol.rb +1 -1
- data/lib/solargraph/pin/yard_pin/method.rb +2 -2
- data/lib/solargraph/pin/yard_pin/namespace.rb +16 -7
- data/lib/solargraph/position.rb +103 -0
- data/lib/solargraph/range.rb +70 -0
- data/lib/solargraph/source.rb +159 -328
- data/lib/solargraph/source/chain.rb +38 -55
- data/lib/solargraph/source/chain/call.rb +47 -29
- data/lib/solargraph/source/chain/class_variable.rb +2 -2
- data/lib/solargraph/source/chain/constant.rb +3 -3
- data/lib/solargraph/source/chain/definition.rb +7 -3
- data/lib/solargraph/source/chain/global_variable.rb +1 -1
- data/lib/solargraph/source/chain/head.rb +22 -9
- data/lib/solargraph/source/chain/instance_variable.rb +2 -2
- data/lib/solargraph/source/chain/link.rb +4 -4
- data/lib/solargraph/source/chain/literal.rb +1 -1
- data/lib/solargraph/source/chain/variable.rb +2 -2
- data/lib/solargraph/source/change.rb +0 -6
- data/lib/solargraph/source/cursor.rb +161 -0
- data/lib/solargraph/source/encoding_fixes.rb +1 -1
- data/lib/solargraph/source/node_chainer.rb +28 -21
- data/lib/solargraph/source/node_methods.rb +1 -1
- data/lib/solargraph/source/source_chainer.rb +217 -0
- data/lib/solargraph/source_map.rb +138 -0
- data/lib/solargraph/source_map/clip.rb +123 -0
- data/lib/solargraph/{source → source_map}/completion.rb +3 -3
- data/lib/solargraph/{source → source_map}/mapper.rb +143 -41
- data/lib/solargraph/version.rb +1 -1
- data/lib/solargraph/workspace.rb +13 -20
- data/lib/solargraph/yard_map.rb +77 -48
- metadata +17 -11
- data/lib/solargraph/basic_type.rb +0 -33
- data/lib/solargraph/basic_type_methods.rb +0 -111
- data/lib/solargraph/source/call_chainer.rb +0 -273
- data/lib/solargraph/source/fragment.rb +0 -342
- data/lib/solargraph/source/location.rb +0 -23
- data/lib/solargraph/source/position.rb +0 -95
- data/lib/solargraph/source/range.rb +0 -64
data/lib/solargraph/version.rb
CHANGED
data/lib/solargraph/workspace.rb
CHANGED
@@ -10,7 +10,7 @@ module Solargraph
|
|
10
10
|
attr_reader :directory
|
11
11
|
|
12
12
|
# @param directory [String]
|
13
|
-
def initialize directory, config = nil
|
13
|
+
def initialize directory = nil, config = nil
|
14
14
|
# @todo Convert to an absolute path?
|
15
15
|
@directory = directory
|
16
16
|
@directory = nil if @directory == ''
|
@@ -48,12 +48,11 @@ module Solargraph
|
|
48
48
|
# Remove a source from the workspace. The source will not be removed if
|
49
49
|
# its file exists and the workspace is configured to include it.
|
50
50
|
#
|
51
|
-
# @param
|
51
|
+
# @param filename [String]
|
52
52
|
# @return [Boolean] True if the source was removed from the workspace
|
53
|
-
def remove
|
54
|
-
return false if config(true).calculated.include?(
|
55
|
-
|
56
|
-
source_hash.delete source.filename
|
53
|
+
def remove filename
|
54
|
+
return false if config(true).calculated.include?(filename)
|
55
|
+
source_hash.delete filename
|
57
56
|
true
|
58
57
|
end
|
59
58
|
|
@@ -67,11 +66,6 @@ module Solargraph
|
|
67
66
|
source_hash.values
|
68
67
|
end
|
69
68
|
|
70
|
-
# @return [Boolean]
|
71
|
-
def has_source? source
|
72
|
-
source_hash.has_value?(source)
|
73
|
-
end
|
74
|
-
|
75
69
|
# @return [Boolean]
|
76
70
|
def has_file? filename
|
77
71
|
source_hash.has_key?(filename)
|
@@ -84,14 +78,6 @@ module Solargraph
|
|
84
78
|
source_hash[filename]
|
85
79
|
end
|
86
80
|
|
87
|
-
# The time of the last workspace synchronization.
|
88
|
-
#
|
89
|
-
# @return [Time]
|
90
|
-
def stime
|
91
|
-
return @stime if source_hash.empty?
|
92
|
-
@stime = source_hash.values.sort{|a, b| a.stime <=> b.stime}.last.stime
|
93
|
-
end
|
94
|
-
|
95
81
|
# The require paths associated with the workspace.
|
96
82
|
#
|
97
83
|
# @return [Array<String>]
|
@@ -125,6 +111,14 @@ module Solargraph
|
|
125
111
|
@gemspecs ||= Dir[File.join(directory, '**/*.gemspec')]
|
126
112
|
end
|
127
113
|
|
114
|
+
# Synchronize the workspace from the provided updater.
|
115
|
+
#
|
116
|
+
# @param [Source::Updater]
|
117
|
+
# @return [void]
|
118
|
+
def synchronize! updater
|
119
|
+
source_hash[updater.filename] = source_hash[updater.filename].synchronize(updater)
|
120
|
+
end
|
121
|
+
|
128
122
|
private
|
129
123
|
|
130
124
|
# @return [Hash<String, Solargraph::Source>]
|
@@ -142,7 +136,6 @@ module Solargraph
|
|
142
136
|
source_hash[filename] = Solargraph::Source.load(filename)
|
143
137
|
end
|
144
138
|
end
|
145
|
-
@stime = Time.now
|
146
139
|
end
|
147
140
|
|
148
141
|
def generate_require_paths
|
data/lib/solargraph/yard_map.rb
CHANGED
@@ -20,24 +20,18 @@ module Solargraph
|
|
20
20
|
@@stdlib_paths[path].push ns
|
21
21
|
end
|
22
22
|
|
23
|
-
# @return [Solargraph::Workspace]
|
24
|
-
attr_reader :workspace
|
25
|
-
|
26
23
|
# @return [Array<String>]
|
27
24
|
attr_reader :required
|
28
25
|
|
29
26
|
# @param required [Array<String>]
|
30
27
|
# @param workspace [Solargraph::Workspace, nil]
|
31
|
-
def initialize(required: []
|
32
|
-
@workspace = workspace
|
28
|
+
def initialize(required: [])
|
33
29
|
# HACK: YardMap needs its own copy of this array
|
34
30
|
@required = required.clone
|
35
31
|
@gem_paths = {}
|
36
32
|
@stdlib_namespaces = []
|
37
33
|
process_requires
|
38
|
-
# yardocs.push CoreDocs.yardoc_file
|
39
34
|
yardocs.uniq!
|
40
|
-
yardocs.delete_if{ |y| y.start_with? workspace.directory } unless workspace.nil? or workspace.directory.nil?
|
41
35
|
end
|
42
36
|
|
43
37
|
# @return [Array<Solargraph::Pin::Base>]
|
@@ -45,6 +39,19 @@ module Solargraph
|
|
45
39
|
@pins ||= []
|
46
40
|
end
|
47
41
|
|
42
|
+
# @param new_requires [Array<String>]
|
43
|
+
# @return [Boolean]
|
44
|
+
def change new_requires
|
45
|
+
if new_requires.uniq.sort == required.uniq.sort
|
46
|
+
false
|
47
|
+
else
|
48
|
+
required.clear
|
49
|
+
required.concat new_requires
|
50
|
+
process_requires
|
51
|
+
true
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
48
55
|
# @return [Array<String>]
|
49
56
|
def yardocs
|
50
57
|
@yardocs ||= []
|
@@ -71,31 +78,24 @@ module Solargraph
|
|
71
78
|
end
|
72
79
|
end
|
73
80
|
|
74
|
-
# @param new_requires [Array<String>]
|
75
|
-
# @return [Boolean]
|
76
|
-
def change new_requires
|
77
|
-
if new_requires.uniq.sort == required.uniq.sort
|
78
|
-
false
|
79
|
-
else
|
80
|
-
required.clear
|
81
|
-
required.concat new_requires
|
82
|
-
process_requires
|
83
|
-
true
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
81
|
# @return [Array<Solargraph::Pin::Base>]
|
88
82
|
def core_pins
|
89
83
|
@@core_pins ||= begin
|
90
84
|
result = []
|
91
85
|
load_yardoc CoreDocs.yardoc_file
|
92
86
|
YARD::Registry.each do |o|
|
93
|
-
result.
|
87
|
+
result.concat generate_pins(o)
|
94
88
|
end
|
95
89
|
result
|
96
90
|
end
|
97
91
|
end
|
98
92
|
|
93
|
+
# @param path [String]
|
94
|
+
# @return [Pin::Base]
|
95
|
+
def path_pin path
|
96
|
+
pins.select{ |p| p.path == path }.first
|
97
|
+
end
|
98
|
+
|
99
99
|
private
|
100
100
|
|
101
101
|
# @return [YardMap::Cache]
|
@@ -108,7 +108,7 @@ module Solargraph
|
|
108
108
|
def recurse_namespace_object ns
|
109
109
|
result = []
|
110
110
|
ns.children.each do |c|
|
111
|
-
result.
|
111
|
+
result.concat generate_pins(c)
|
112
112
|
result.concat recurse_namespace_object(c) if c.respond_to?(:children)
|
113
113
|
end
|
114
114
|
result
|
@@ -116,17 +116,23 @@ module Solargraph
|
|
116
116
|
|
117
117
|
# @param code_object [YARD::CodeObjects::Base]
|
118
118
|
# @return [Solargraph::Pin::Base]
|
119
|
-
def
|
119
|
+
def generate_pins code_object
|
120
|
+
result = []
|
120
121
|
location = object_location(code_object)
|
121
122
|
if code_object.is_a?(YARD::CodeObjects::NamespaceObject)
|
122
|
-
Solargraph::Pin::YardPin::Namespace.new(code_object, location)
|
123
|
+
result.push Solargraph::Pin::YardPin::Namespace.new(code_object, location)
|
123
124
|
elsif code_object.is_a?(YARD::CodeObjects::MethodObject)
|
124
|
-
|
125
|
+
if code_object.name == :initialize && code_object.scope == :instance
|
126
|
+
# @todo Check the visibility of <Class>.new
|
127
|
+
result.push Solargraph::Pin::YardPin::Method.new(code_object, location, 'new', :class, :public)
|
128
|
+
result.push Solargraph::Pin::YardPin::Method.new(code_object, location, 'initialize', :instance, :private)
|
129
|
+
else
|
130
|
+
result.push Solargraph::Pin::YardPin::Method.new(code_object, location)
|
131
|
+
end
|
125
132
|
elsif code_object.is_a?(YARD::CodeObjects::ConstantObject)
|
126
|
-
Solargraph::Pin::YardPin::Constant.new(code_object, location)
|
127
|
-
else
|
128
|
-
nil
|
133
|
+
result.push Solargraph::Pin::YardPin::Constant.new(code_object, location)
|
129
134
|
end
|
135
|
+
result
|
130
136
|
end
|
131
137
|
|
132
138
|
# @return [void]
|
@@ -136,7 +142,6 @@ module Solargraph
|
|
136
142
|
stdnames = {}
|
137
143
|
required.each do |r|
|
138
144
|
next if r.nil? or r.empty?
|
139
|
-
next if !workspace.nil? and workspace.would_require?(r)
|
140
145
|
cached = cache.get_path_pins(r)
|
141
146
|
unless cached.nil?
|
142
147
|
pins.concat cached
|
@@ -147,8 +152,6 @@ module Solargraph
|
|
147
152
|
spec = Gem::Specification.find_by_path(r) || Gem::Specification.find_by_name(r.split('/').first)
|
148
153
|
ver = spec.version.to_s
|
149
154
|
ver = ">= 0" if ver.empty?
|
150
|
-
# @todo Ignoring dependencies for now
|
151
|
-
# add_gem_dependencies spec
|
152
155
|
yd = YARD::Registry.yardoc_file_for_gem(spec.name, ver)
|
153
156
|
@gem_paths[spec.name] = spec.full_gem_path
|
154
157
|
if yd.nil?
|
@@ -156,18 +159,15 @@ module Solargraph
|
|
156
159
|
else
|
157
160
|
unless yardocs.include?(yd)
|
158
161
|
yardocs.unshift yd
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
result.push generate_pin(o)
|
163
|
-
end
|
162
|
+
result.concat process_yardoc yd
|
163
|
+
# @todo Testing performance
|
164
|
+
# result.concat add_gem_dependencies spec
|
164
165
|
end
|
165
166
|
end
|
166
167
|
rescue Gem::LoadError => e
|
167
|
-
next if !workspace.nil? and workspace.would_require?(r)
|
168
168
|
stdtmp = []
|
169
169
|
@@stdlib_paths.each_pair do |path, objects|
|
170
|
-
stdtmp.concat objects if path == r
|
170
|
+
stdtmp.concat objects if path == r || path.start_with?("#{r}/")
|
171
171
|
end
|
172
172
|
if stdtmp.empty?
|
173
173
|
unresolved_requires.push r
|
@@ -197,7 +197,7 @@ module Solargraph
|
|
197
197
|
objects.each do |ns|
|
198
198
|
next if done.include?(ns.path)
|
199
199
|
done.push ns.path
|
200
|
-
result.
|
200
|
+
result.concat generate_pins(ns)
|
201
201
|
result.concat recurse_namespace_object(ns)
|
202
202
|
end
|
203
203
|
result.delete_if(&:nil?)
|
@@ -211,25 +211,54 @@ module Solargraph
|
|
211
211
|
# @param spec [Gem::Specification]
|
212
212
|
# @return [void]
|
213
213
|
def add_gem_dependencies spec
|
214
|
+
result = []
|
214
215
|
(spec.dependencies - spec.development_dependencies).each do |dep|
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
216
|
+
begin
|
217
|
+
depspec = Gem::Specification.find_by_name(dep.name)
|
218
|
+
@gem_paths[depspec.name] = depspec.full_gem_path unless depspec.nil?
|
219
|
+
gy = YARD::Registry.yardoc_file_for_gem(dep.name)
|
220
|
+
if gy.nil?
|
221
|
+
unresolved_requires.push dep.name
|
222
|
+
else
|
223
|
+
unless yardocs.include?(gy)
|
224
|
+
yardocs.unshift gy
|
225
|
+
result.concat process_yardoc gy
|
226
|
+
# @todo Trying only one level of dependencies
|
227
|
+
# result.concat add_gem_dependencies(depspec)
|
228
|
+
end
|
229
|
+
end
|
230
|
+
rescue Gem::LoadError
|
231
|
+
# This error probably indicates a bug in an installed gem
|
232
|
+
STDERR.puts "Warning: failed to resolve #{dep.name} gem dependency for #{spec.name}"
|
222
233
|
end
|
223
234
|
end
|
235
|
+
result
|
236
|
+
end
|
237
|
+
|
238
|
+
def process_yardoc y
|
239
|
+
return [] if y.nil?
|
240
|
+
size = Dir.glob(File.join(y, '**', '*'))
|
241
|
+
.map{ |f| File.size(f) }
|
242
|
+
.inject(:+)
|
243
|
+
if !size.nil? && size > 20_000_000
|
244
|
+
STDERR.puts "Warning: yardoc at #{y} is too large to process (#{size} bytes)"
|
245
|
+
return []
|
246
|
+
end
|
247
|
+
result = []
|
248
|
+
load_yardoc y
|
249
|
+
YARD::Registry.each do |o|
|
250
|
+
result.concat generate_pins(o)
|
251
|
+
end
|
252
|
+
result
|
224
253
|
end
|
225
254
|
|
226
255
|
# @param obj [YARD::CodeObjects::Base]
|
227
|
-
# @return [Solargraph::
|
256
|
+
# @return [Solargraph::Location]
|
228
257
|
def object_location obj
|
229
258
|
return nil if obj.file.nil? or obj.line.nil?
|
230
259
|
@gem_paths.values.each do |path|
|
231
260
|
file = File.join(path, obj.file)
|
232
|
-
return Solargraph::
|
261
|
+
return Solargraph::Location.new(file, Solargraph::Range.from_to(obj.line, 0, obj.line, 0)) if File.exist?(file)
|
233
262
|
end
|
234
263
|
nil
|
235
264
|
end
|
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.
|
4
|
+
version: 0.27.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Fred Snyder
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-09-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: parser
|
@@ -238,9 +238,10 @@ files:
|
|
238
238
|
- lib/solargraph/api_map/cache.rb
|
239
239
|
- lib/solargraph/api_map/source_to_yard.rb
|
240
240
|
- lib/solargraph/api_map/store.rb
|
241
|
-
- lib/solargraph/
|
242
|
-
- lib/solargraph/basic_type_methods.rb
|
241
|
+
- lib/solargraph/bundle.rb
|
243
242
|
- lib/solargraph/complex_type.rb
|
243
|
+
- lib/solargraph/complex_type/type_methods.rb
|
244
|
+
- lib/solargraph/complex_type/unique_type.rb
|
244
245
|
- lib/solargraph/core_fills.rb
|
245
246
|
- lib/solargraph/diagnostics.rb
|
246
247
|
- lib/solargraph/diagnostics/base.rb
|
@@ -248,10 +249,13 @@ files:
|
|
248
249
|
- lib/solargraph/diagnostics/rubocop.rb
|
249
250
|
- lib/solargraph/diagnostics/severities.rb
|
250
251
|
- lib/solargraph/diagnostics/type_not_defined.rb
|
252
|
+
- lib/solargraph/diagnostics/update_errors.rb
|
251
253
|
- lib/solargraph/language_server.rb
|
252
254
|
- lib/solargraph/language_server/completion_item_kinds.rb
|
253
255
|
- lib/solargraph/language_server/error_codes.rb
|
254
256
|
- lib/solargraph/language_server/host.rb
|
257
|
+
- lib/solargraph/language_server/host/cataloger.rb
|
258
|
+
- lib/solargraph/language_server/host/diagnoser.rb
|
255
259
|
- lib/solargraph/language_server/message.rb
|
256
260
|
- lib/solargraph/language_server/message/base.rb
|
257
261
|
- lib/solargraph/language_server/message/cancel_request.rb
|
@@ -301,6 +305,7 @@ files:
|
|
301
305
|
- lib/solargraph/library.rb
|
302
306
|
- lib/solargraph/live_map.rb
|
303
307
|
- lib/solargraph/live_map/cache.rb
|
308
|
+
- lib/solargraph/location.rb
|
304
309
|
- lib/solargraph/page.rb
|
305
310
|
- lib/solargraph/pin.rb
|
306
311
|
- lib/solargraph/pin/attribute.rb
|
@@ -336,10 +341,11 @@ files:
|
|
336
341
|
- lib/solargraph/plugin/canceler.rb
|
337
342
|
- lib/solargraph/plugin/process.rb
|
338
343
|
- lib/solargraph/plugin/runtime.rb
|
344
|
+
- lib/solargraph/position.rb
|
345
|
+
- lib/solargraph/range.rb
|
339
346
|
- lib/solargraph/server_methods.rb
|
340
347
|
- lib/solargraph/shell.rb
|
341
348
|
- lib/solargraph/source.rb
|
342
|
-
- lib/solargraph/source/call_chainer.rb
|
343
349
|
- lib/solargraph/source/chain.rb
|
344
350
|
- lib/solargraph/source/chain/call.rb
|
345
351
|
- lib/solargraph/source/chain/class_variable.rb
|
@@ -352,17 +358,17 @@ files:
|
|
352
358
|
- lib/solargraph/source/chain/literal.rb
|
353
359
|
- lib/solargraph/source/chain/variable.rb
|
354
360
|
- lib/solargraph/source/change.rb
|
355
|
-
- lib/solargraph/source/
|
361
|
+
- lib/solargraph/source/cursor.rb
|
356
362
|
- lib/solargraph/source/encoding_fixes.rb
|
357
363
|
- lib/solargraph/source/flawed_builder.rb
|
358
|
-
- lib/solargraph/source/fragment.rb
|
359
|
-
- lib/solargraph/source/location.rb
|
360
|
-
- lib/solargraph/source/mapper.rb
|
361
364
|
- lib/solargraph/source/node_chainer.rb
|
362
365
|
- lib/solargraph/source/node_methods.rb
|
363
|
-
- lib/solargraph/source/
|
364
|
-
- lib/solargraph/source/range.rb
|
366
|
+
- lib/solargraph/source/source_chainer.rb
|
365
367
|
- lib/solargraph/source/updater.rb
|
368
|
+
- lib/solargraph/source_map.rb
|
369
|
+
- lib/solargraph/source_map/clip.rb
|
370
|
+
- lib/solargraph/source_map/completion.rb
|
371
|
+
- lib/solargraph/source_map/mapper.rb
|
366
372
|
- lib/solargraph/version.rb
|
367
373
|
- lib/solargraph/views/_method.erb
|
368
374
|
- lib/solargraph/views/_name_type_tag.erb
|
@@ -1,33 +0,0 @@
|
|
1
|
-
module Solargraph
|
2
|
-
class BasicType
|
3
|
-
include BasicTypeMethods
|
4
|
-
|
5
|
-
# Create a BasicType with the specified name and an optional substring.
|
6
|
-
# The substring is the parameter section of a parametrized type, e.g., for
|
7
|
-
# the type `Array<String>`, the name is `Array` and the substring is
|
8
|
-
# `<String>`.
|
9
|
-
#
|
10
|
-
# @param name [String] The name of the type
|
11
|
-
# @param substring [String] The substring of the type
|
12
|
-
def initialize name, substring = ''
|
13
|
-
@name = name
|
14
|
-
@substring = substring
|
15
|
-
@tag = name + substring
|
16
|
-
@key_types = []
|
17
|
-
@subtypes = []
|
18
|
-
return unless parameters?
|
19
|
-
subs = ComplexType.parse(substring[1..-2], partial: true)
|
20
|
-
if hash_parameters?
|
21
|
-
raise ComplexTypeError, "Bad hash type" unless !subs.is_a?(ComplexType) and subs.length == 2 and !subs[0].is_a?(ComplexType) and !subs[1].is_a?(ComplexType)
|
22
|
-
@key_types.concat subs[0]
|
23
|
-
@subtypes.concat subs[1]
|
24
|
-
else
|
25
|
-
@subtypes.concat subs
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
def to_s
|
30
|
-
tag
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
@@ -1,111 +0,0 @@
|
|
1
|
-
module Solargraph
|
2
|
-
module BasicTypeMethods
|
3
|
-
# @return [String]
|
4
|
-
attr_reader :name
|
5
|
-
|
6
|
-
# @return [String]
|
7
|
-
attr_reader :substring
|
8
|
-
|
9
|
-
# @return [String]
|
10
|
-
attr_reader :tag
|
11
|
-
|
12
|
-
# @return [Array<ComplexType>]
|
13
|
-
attr_reader :subtypes
|
14
|
-
|
15
|
-
# @return [Boolean]
|
16
|
-
def duck_type?
|
17
|
-
@duck_type ||= name.start_with?('#')
|
18
|
-
end
|
19
|
-
|
20
|
-
# @return [Boolean]
|
21
|
-
def nil_type?
|
22
|
-
@nil_type ||= (name.downcase == 'nil')
|
23
|
-
end
|
24
|
-
|
25
|
-
# @return [Boolean]
|
26
|
-
def parameters?
|
27
|
-
!substring.empty?
|
28
|
-
end
|
29
|
-
|
30
|
-
def void?
|
31
|
-
name == 'void'
|
32
|
-
end
|
33
|
-
|
34
|
-
def defined?
|
35
|
-
!undefined?
|
36
|
-
end
|
37
|
-
|
38
|
-
def undefined?
|
39
|
-
name == 'undefined'
|
40
|
-
end
|
41
|
-
|
42
|
-
# @return [Boolean]
|
43
|
-
def list_parameters?
|
44
|
-
substring.start_with?('<')
|
45
|
-
end
|
46
|
-
|
47
|
-
# @return [Boolean]
|
48
|
-
def fixed_parameters?
|
49
|
-
substring.start_with?('(')
|
50
|
-
end
|
51
|
-
|
52
|
-
# @return [Boolean]
|
53
|
-
def hash_parameters?
|
54
|
-
substring.start_with?('{')
|
55
|
-
end
|
56
|
-
|
57
|
-
# @return [Array<ComplexType>]
|
58
|
-
def value_types
|
59
|
-
@subtypes
|
60
|
-
end
|
61
|
-
|
62
|
-
# @return [Array<ComplexType>]
|
63
|
-
def key_types
|
64
|
-
@key_types
|
65
|
-
end
|
66
|
-
|
67
|
-
# @return [String]
|
68
|
-
def namespace
|
69
|
-
@namespace ||= 'Object' if duck_type?
|
70
|
-
@namespace ||= 'NilClass' if nil_type?
|
71
|
-
@namespace ||= ((name == 'Class' or name == 'Module') and !subtypes.empty?) ? subtypes.first.name : name
|
72
|
-
end
|
73
|
-
|
74
|
-
# @return [Symbol] :class or :instance
|
75
|
-
def scope
|
76
|
-
@scope ||= :instance if duck_type? or nil_type?
|
77
|
-
@scope ||= ((name == 'Class' or name == 'Module') and !subtypes.empty?) ? :class : :instance
|
78
|
-
end
|
79
|
-
|
80
|
-
def == other
|
81
|
-
return false unless self.class == other.class
|
82
|
-
tag == other.tag
|
83
|
-
end
|
84
|
-
|
85
|
-
# Generate a ComplexType that fully qualifies this type's namespaces.
|
86
|
-
#
|
87
|
-
# @param api_map [ApiMap] The ApiMap that performs qualification
|
88
|
-
# @param context [String] The namespace from which to resolve names
|
89
|
-
# @return [ComplexType] The generated ComplexType
|
90
|
-
def qualify api_map, context = ''
|
91
|
-
return ComplexType.parse(tag) if duck_type? or void? or undefined?
|
92
|
-
fqns = api_map.qualify(name, context)
|
93
|
-
return ComplexType::UNDEFINED if fqns.nil?
|
94
|
-
ltypes = key_types.map do |t|
|
95
|
-
t.qualify api_map, context
|
96
|
-
end
|
97
|
-
rtypes = value_types.map do |t|
|
98
|
-
t.qualify api_map, context
|
99
|
-
end
|
100
|
-
if list_parameters?
|
101
|
-
Solargraph::ComplexType.parse("#{fqns}<#{rtypes.map(&:tag).join(', ')}>").first
|
102
|
-
elsif fixed_parameters?
|
103
|
-
Solargraph::ComplexType.parse("#{fqns}(#{rtypes.map(&:tag).join(', ')})").first
|
104
|
-
elsif hash_parameters?
|
105
|
-
Solargraph::ComplexType.parse("#{fqns}{#{ltypes.map(&:tag).join(', ')} => #{rtypes.map(&:tag).join(', ')}}").first
|
106
|
-
else
|
107
|
-
Solargraph::ComplexType.parse(fqns).first
|
108
|
-
end
|
109
|
-
end
|
110
|
-
end
|
111
|
-
end
|