Class: Solargraph::Source
- Inherits:
-
Object
- Object
- Solargraph::Source
show all
- Includes:
- EncodingFixes
- Defined in:
- lib/solargraph/source.rb,
lib/solargraph/source/chain.rb,
lib/solargraph/source/change.rb,
lib/solargraph/source/cursor.rb,
lib/solargraph/source/updater.rb,
lib/solargraph/source/chain/or.rb,
lib/solargraph/source/chain/call.rb,
lib/solargraph/source/chain/head.rb,
lib/solargraph/source/chain/link.rb,
lib/solargraph/source/chain/literal.rb,
lib/solargraph/source/chain/z_super.rb,
lib/solargraph/source/chain/constant.rb,
lib/solargraph/source/chain/variable.rb,
lib/solargraph/source/encoding_fixes.rb,
lib/solargraph/source/source_chainer.rb,
lib/solargraph/source/chain/block_variable.rb,
lib/solargraph/source/chain/class_variable.rb,
lib/solargraph/source/chain/global_variable.rb,
lib/solargraph/source/chain/instance_variable.rb
Overview
A Ruby file that has been parsed into an AST.
Defined Under Namespace
Modules: EncodingFixes
Classes: Chain, Change, Cursor, SourceChainer, Updater
Constant Summary
collapse
- FOLDING_NODE_TYPES =
if Parser.rubyvm?
%i[
CLASS SCLASS MODULE DEFN DEFS IF WHILE UNLESS ITER STR HASH ARRAY LIST
].freeze
else
%i[
class sclass module def defs if str dstr array while unless kwbegin hash block
].freeze
end
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#associated_comments ⇒ Hash{Integer => Array<Parser::Source::Comment>}
Get a hash of comments grouped by the line numbers of the associated code.
-
#at(range) ⇒ String
-
#code_for(node) ⇒ String
-
#comment_at?(position) ⇒ Boolean
-
#comments_for(node) ⇒ String
-
#cursor_at(position) ⇒ Source::Cursor
-
#finish_synchronize ⇒ Source
Finish synchronizing a source that was updated via #start_synchronize.
-
#folding_ranges ⇒ Array<Range>
Get an array of ranges that can be folded, e.g., the range of a class definition or an if condition.
-
#from_to(l1, c1, l2, c2) ⇒ String
-
#initialize(code, filename = nil, version = 0) ⇒ Source
constructor
A new instance of Source.
-
#location ⇒ Location
A location representing the file in its entirety.
-
#node_at(line, column) ⇒ AST::Node
Get the nearest node that contains the specified index.
-
#parsed? ⇒ Boolean
-
#references(name) ⇒ Array<Location>
-
#repaired? ⇒ Boolean
-
#start_synchronize(updater) ⇒ Source
Start synchronizing the source.
-
#string_at?(position) ⇒ Boolean
-
#string_ranges ⇒ Object
-
#synchronize(updater) ⇒ Source
Synchronize the Source with an update.
-
#synchronized? ⇒ Boolean
-
#tree_at(line, column) ⇒ Array<AST::Node>
Get an array of nodes containing the specified index, starting with the nearest node and ending with the root.
normalize
Constructor Details
#initialize(code, filename = nil, version = 0) ⇒ Source
Returns a new instance of Source.
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/solargraph/source.rb', line 42
def initialize code, filename = nil, version = 0
@code = normalize(code)
@repaired = code
@filename = filename
@version = version
@domains = []
begin
@node, @comments = Solargraph::Parser.(@code, filename)
@parsed = true
rescue Parser::SyntaxError, EncodingError => e
@node = nil
@comments = {}
@parsed = false
ensure
@code.freeze
end
end
|
Instance Attribute Details
#code ⇒ String
27
28
29
|
# File 'lib/solargraph/source.rb', line 27
def code
@code
end
|
33
34
35
|
# File 'lib/solargraph/source.rb', line 33
def
@comments
end
|
251
252
253
|
# File 'lib/solargraph/source.rb', line 251
def error_ranges
@error_ranges ||= []
end
|
#filename ⇒ String
24
25
26
|
# File 'lib/solargraph/source.rb', line 24
def filename
@filename
end
|
#node ⇒ Parser::AST::Node
30
31
32
|
# File 'lib/solargraph/source.rb', line 30
def node
@node
end
|
#version ⇒ Integer
37
38
39
|
# File 'lib/solargraph/source.rb', line 37
def version
@version
end
|
Class Method Details
513
514
515
516
517
518
|
# File 'lib/solargraph/source.rb', line 513
def load filename
file = File.open(filename)
code = file.read
file.close
Source.load_string(code, filename)
end
|
.load_string(code, filename = nil, version = 0) ⇒ Solargraph::Source
524
525
526
|
# File 'lib/solargraph/source.rb', line 524
def load_string code, filename = nil, version = 0
Source.new code, filename, version
end
|
.parse_docstring(comments) ⇒ YARD::DocstringParser
530
531
532
533
534
|
# File 'lib/solargraph/source.rb', line 530
def parse_docstring
YARD::Docstring.parser.parse(, YARD::CodeObjects::Base.new(:root, 'stub'))
end
|
Instance Method Details
Get a hash of comments grouped by the line numbers of the associated code.
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
# File 'lib/solargraph/source.rb', line 318
def
@associated_comments ||= begin
result = {}
buffer = String.new('')
last = nil
@comments.each_pair do |num, snip|
if !last || num == last + 1
buffer.concat "#{snip.text}\n"
else
result[first_not_empty_from(last + 1)] = buffer.clone
buffer.replace "#{snip.text}\n"
end
last = num
end
result[first_not_empty_from(last + 1)] = buffer unless buffer.empty? || last.nil?
result
end
end
|
#at(range) ⇒ String
71
72
73
|
# File 'lib/solargraph/source.rb', line 71
def at range
from_to range.start.line, range.start.character, range.ending.line, range.ending.character
end
|
235
236
237
238
239
240
241
242
|
# File 'lib/solargraph/source.rb', line 235
def position
.each do |range|
return true if range.include?(position) ||
(range.ending.line == position.line && range.ending.column < position.column)
break if range.ending.line > position.line
end
false
end
|
267
268
269
270
271
272
273
|
# File 'lib/solargraph/source.rb', line 267
def node
rng = Range.from_node(node)
[rng.start.line] ||= begin
buff = [rng.start.line]
buff ? (buff) : nil
end
end
|
180
181
182
|
# File 'lib/solargraph/source.rb', line 180
def cursor_at position
Cursor.new(self, position)
end
|
#finish_synchronize ⇒ Source
Finish synchronizing a source that was updated via #start_synchronize. This method returns self if the source is already synchronized. Otherwise it parses the AST and returns a new synchronized Source.
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
# File 'lib/solargraph/source.rb', line 138
def finish_synchronize
return self if synchronized?
synced = Source.new(@code, filename)
if synced.parsed?
synced.version = version
return synced
end
synced = Source.new(@repaired, filename)
synced.error_ranges.concat (error_ranges + last_updater.changes.map(&:range))
synced.code = @code
synced.synchronized = true
synced.version = version
synced
end
|
Get an array of ranges that can be folded, e.g., the range of a class definition or an if condition.
See FOLDING_NODE_TYPES for the list of node types that can be folded.
301
302
303
304
305
306
307
308
|
# File 'lib/solargraph/source.rb', line 301
def folding_ranges
@folding_ranges ||= begin
result = []
inner_folding_ranges node, result
result.concat
result
end
end
|
#from_to(l1, c1, l2, c2) ⇒ String
A location representing the file in its entirety.
#node_at(line, column) ⇒ AST::Node
Get the nearest node that contains the specified index.
91
92
93
|
# File 'lib/solargraph/source.rb', line 91
def node_at(line, column)
tree_at(line, column).first
end
|
#parsed? ⇒ Boolean
185
186
187
|
# File 'lib/solargraph/source.rb', line 185
def parsed?
@parsed
end
|
246
247
248
|
# File 'lib/solargraph/source.rb', line 246
def references name
Parser.references self, name
end
|
#repaired? ⇒ Boolean
189
190
191
|
# File 'lib/solargraph/source.rb', line 189
def repaired?
@is_repaired ||= (@code != @repaired)
end
|
#start_synchronize(updater) ⇒ Source
Start synchronizing the source. This method updates the code without parsing a new AST. The resulting Source object will be marked not synchronized (#synchronized? == false).
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/solargraph/source.rb', line 115
def start_synchronize updater
raise 'Invalid synchronization' unless updater.filename == filename
real_code = updater.write(@code)
src = Source.allocate
src.filename = filename
src.code = real_code
src.version = updater.version
src.parsed = parsed?
src.repaired = updater.repair(@repaired)
src.synchronized = false
src.node = @node
src. = @comments
src.error_ranges = error_ranges
src.last_updater = updater
return src.finish_synchronize unless real_code.lines.length == @code.lines.length
src
end
|
#string_at?(position) ⇒ Boolean
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
# File 'lib/solargraph/source.rb', line 195
def string_at? position
if Parser.rubyvm?
string_ranges.each do |range|
if synchronized?
return true if range.include?(position) || range.ending == position
else
return true if last_updater && last_updater.changes.one? && range.contain?(last_updater.changes.first.range.start)
end
end
false
else
return false if Position.to_offset(code, position) >= code.length
string_nodes.each do |node|
range = Range.from_node(node)
next if range.ending.line < position.line
break if range.ending.line > position.line
return true if node.type == :str && range.include?(position) && range.start != position
return true if [:STR, :str].include?(node.type) && range.include?(position) && range.start != position
if node.type == :dstr
inner = node_at(position.line, position.column)
next if inner.nil?
inner_range = Range.from_node(inner)
next unless range.include?(inner_range.ending)
return true if inner.type == :str
inner_code = at(Solargraph::Range.new(inner_range.start, position))
return true if (inner.type == :dstr && inner_range.ending.character <= position.character) && !inner_code.end_with?('}') ||
(inner.type != :dstr && inner_range.ending.line == position.line && position.character <= inner_range.ending.character && inner_code.end_with?('}'))
end
break if range.ending.line > position.line
end
false
end
end
|
#string_ranges ⇒ Object
229
230
231
|
# File 'lib/solargraph/source.rb', line 229
def string_ranges
@string_ranges ||= Parser.string_ranges(node)
end
|
#synchronize(updater) ⇒ Source
Synchronize the Source with an update. This method applies changes to the code, parses the new code’s AST, and returns the resulting Source object.
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
# File 'lib/solargraph/source.rb', line 158
def synchronize updater
raise 'Invalid synchronization' unless updater.filename == filename
real_code = updater.write(@code)
if real_code == @code
@version = updater.version
return self
end
synced = Source.new(real_code, filename)
if synced.parsed?
synced.version = updater.version
return synced
end
incr_code = updater.repair(@repaired)
synced = Source.new(incr_code, filename)
synced.error_ranges.concat (error_ranges + updater.changes.map(&:range))
synced.code = real_code
synced.version = updater.version
synced
end
|
#synchronized? ⇒ Boolean
310
311
312
313
|
# File 'lib/solargraph/source.rb', line 310
def synchronized?
@synchronized = true if @synchronized.nil?
@synchronized
end
|
#tree_at(line, column) ⇒ Array<AST::Node>
Get an array of nodes containing the specified index, starting with the nearest node and ending with the root.
101
102
103
104
105
106
107
|
# File 'lib/solargraph/source.rb', line 101
def tree_at(line, column)
position = Position.new(line, column)
stack = []
inner_tree_at @node, position, stack
stack
end
|