ruby_language_server 0.4.2 → 0.9.10
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/CHANGELOG.txt +81 -0
- data/FAQ_ROADMAP.md +4 -2
- data/Gemfile.lock +232 -70
- data/Guardfile +1 -1
- data/Makefile +42 -13
- data/README.md +75 -7
- data/bin/rdbg +8 -0
- data/bin/run_in_shell +5 -0
- data/exe/ruby_language_server +1 -1
- data/lib/config/initializers/active_record.rb +16 -16
- data/lib/db/schema.rb +7 -1
- data/lib/ruby_language_server/application.rb +4 -4
- data/lib/ruby_language_server/code_file.rb +41 -16
- data/lib/ruby_language_server/completion.rb +92 -13
- data/lib/ruby_language_server/core_index.rb +173 -0
- data/lib/ruby_language_server/hover.rb +68 -0
- data/lib/ruby_language_server/io.rb +184 -100
- data/lib/ruby_language_server/line_context.rb +14 -0
- data/lib/ruby_language_server/project_manager.rb +343 -133
- data/lib/ruby_language_server/scope_data/base.rb +12 -4
- data/lib/ruby_language_server/scope_data/scope.rb +65 -14
- data/lib/ruby_language_server/scope_parser.rb +399 -203
- data/lib/ruby_language_server/scope_parser_commands/rails_commands.rb +7 -8
- data/lib/ruby_language_server/scope_parser_commands/rake_commands.rb +16 -20
- data/lib/ruby_language_server/scope_parser_commands/rspec_commands.rb +24 -13
- data/lib/ruby_language_server/scope_parser_commands/ruby_commands.rb +16 -42
- data/lib/ruby_language_server/server.rb +52 -29
- data/lib/ruby_language_server/version.rb +1 -1
- data/ruby_language_server.gemspec +9 -3
- metadata +93 -6
- data/lib/ruby_language_server/gem_installer.rb +0 -38
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require 'uri'
|
|
3
4
|
require 'fuzzy_match'
|
|
4
5
|
require 'amatch' # NOTE: that you have to require this... fuzzy_match won't require it for you
|
|
5
6
|
FuzzyMatch.engine = :amatch # This should be in a config somewhere
|
|
6
7
|
|
|
7
8
|
module RubyLanguageServer
|
|
8
9
|
class ProjectManager
|
|
9
|
-
attr_reader :uri_code_file_hash
|
|
10
|
-
|
|
11
10
|
# GoodCop wants to know where to find its config. So here we are.
|
|
12
11
|
ROOT_PATH_MUTEX = Mutex.new
|
|
13
12
|
@_root_path = nil
|
|
@@ -40,6 +39,17 @@ module RubyLanguageServer
|
|
|
40
39
|
def root_uri
|
|
41
40
|
@_root_uri || "file://#{root_path}"
|
|
42
41
|
end
|
|
42
|
+
|
|
43
|
+
# Let's not preload spec/test files or vendor - yet.
|
|
44
|
+
# Matches foo_spec.rb, foo_test.rb, spec.rb and test.rb, and anything in a vendor
|
|
45
|
+
# directory, but not files that merely contain those words, like latest.rb.
|
|
46
|
+
SKIP_SCAN_PATTERN = %r{(?:\A|/|_)(?:spec|test)\.rb\z|(?:\A|/)vendor/}
|
|
47
|
+
|
|
48
|
+
# relative_path is relative to the project root, so a root inside a directory named
|
|
49
|
+
# vendor doesn't skip everything.
|
|
50
|
+
def skip_scan?(relative_path)
|
|
51
|
+
relative_path.match?(SKIP_SCAN_PATTERN)
|
|
52
|
+
end
|
|
43
53
|
end
|
|
44
54
|
|
|
45
55
|
def initialize(path, uri = nil)
|
|
@@ -47,7 +57,8 @@ module RubyLanguageServer
|
|
|
47
57
|
self.class.root_path = path if self.class.root_path.nil?
|
|
48
58
|
self.class.root_uri = uri if uri
|
|
49
59
|
|
|
50
|
-
|
|
60
|
+
# The URI the editor uses for the project root. The scan maps container paths onto it.
|
|
61
|
+
@root_uri = uri || "file://#{path}"
|
|
51
62
|
# This is {uri: code_file} where content stuff is like
|
|
52
63
|
# @additional_gems_installed = false
|
|
53
64
|
# @additional_gem_mutex = Mutex.new
|
|
@@ -57,6 +68,12 @@ module RubyLanguageServer
|
|
|
57
68
|
# @additional_gem_mutex.synchronize { @additional_gems_installed }
|
|
58
69
|
# end
|
|
59
70
|
|
|
71
|
+
# The prefix the background scan puts in front of each file's path relative to the
|
|
72
|
+
# project root: the client's rootUri, or file:// + rootPath, with a trailing slash.
|
|
73
|
+
def scan_root_uri
|
|
74
|
+
@root_uri.end_with?('/') ? @root_uri : "#{@root_uri}/"
|
|
75
|
+
end
|
|
76
|
+
|
|
60
77
|
def text_for_uri(uri)
|
|
61
78
|
code_file = code_file_for_uri(uri)
|
|
62
79
|
code_file&.text || ''
|
|
@@ -64,144 +81,97 @@ module RubyLanguageServer
|
|
|
64
81
|
|
|
65
82
|
def tags_for_uri(uri)
|
|
66
83
|
code_file = code_file_for_uri(uri)
|
|
67
|
-
return
|
|
84
|
+
return [] if code_file.nil?
|
|
68
85
|
|
|
69
86
|
code_file.tags
|
|
70
87
|
end
|
|
71
88
|
|
|
72
89
|
def root_scope_for(uri)
|
|
73
|
-
|
|
74
|
-
RubyLanguageServer.logger.error('code_file.nil?!!!!!!!!!!!!!!') if code_file.nil?
|
|
75
|
-
code_file&.root_scope
|
|
76
|
-
end
|
|
77
|
-
|
|
78
|
-
def all_scopes
|
|
79
|
-
RubyLanguageServer::ScopeData::Scope.all
|
|
90
|
+
code_file_for_uri(uri).scopes.root_scope
|
|
80
91
|
end
|
|
81
92
|
|
|
82
93
|
# Return the list of scopes [deepest, parent, ..., Object]
|
|
83
94
|
def scopes_at(uri, position)
|
|
84
95
|
code_file = code_file_for_uri(uri)
|
|
85
96
|
code_file.refresh_scopes_if_needed
|
|
86
|
-
code_file.scopes.for_line(position.line).where.not(path: nil).by_path_length
|
|
97
|
+
code_file.scopes.for_line(position.line + 1).where.not(path: nil).by_path_length
|
|
87
98
|
end
|
|
88
99
|
|
|
89
100
|
def completion_at(uri, position)
|
|
90
101
|
context = context_at_location(uri, position)
|
|
91
|
-
return
|
|
102
|
+
return nil if context.blank? # null: no completions
|
|
92
103
|
|
|
93
|
-
|
|
94
|
-
|
|
104
|
+
position_scopes = scopes_at(uri, position)
|
|
105
|
+
RubyLanguageServer.logger.debug("scopes_at(uri, position) #{position_scopes.map(&:name)}")
|
|
106
|
+
# Outside every class, module, method and block, complete from the file's top level.
|
|
107
|
+
position_scopes = RubyLanguageServer::ScopeData::Scope.where(id: root_scope_for(uri)&.id) if position_scopes.empty?
|
|
95
108
|
context_scope = position_scopes.first
|
|
96
109
|
RubyLanguageServer::Completion.completion(context, context_scope, position_scopes)
|
|
97
110
|
end
|
|
98
111
|
|
|
99
|
-
#
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
#
|
|
104
|
-
#
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
# /**
|
|
126
|
-
# * A string that should be used when filtering a set of
|
|
127
|
-
# * completion items. When `falsy` the label is used.
|
|
128
|
-
# */
|
|
129
|
-
# filterText?: string;
|
|
130
|
-
# /**
|
|
131
|
-
# * A string that should be inserted a document when selecting
|
|
132
|
-
# * this completion. When `falsy` the label is used.
|
|
133
|
-
# */
|
|
134
|
-
# insertText?: string;
|
|
135
|
-
# /**
|
|
136
|
-
# * The format of the insert text. The format applies to both the `insertText` property
|
|
137
|
-
# * and the `newText` property of a provided `textEdit`.
|
|
138
|
-
# */
|
|
139
|
-
# insertTextFormat?: InsertTextFormat;
|
|
140
|
-
# /**
|
|
141
|
-
# * An edit which is applied to a document when selecting this completion. When an edit is provided the value of
|
|
142
|
-
# * `insertText` is ignored.
|
|
143
|
-
# *
|
|
144
|
-
# * *Note:* The range of the edit must be a single line range and it must contain the position at which completion
|
|
145
|
-
# * has been requested.
|
|
146
|
-
# */
|
|
147
|
-
# textEdit?: TextEdit;
|
|
148
|
-
# /**
|
|
149
|
-
# * An optional array of additional text edits that are applied when
|
|
150
|
-
# * selecting this completion. Edits must not overlap with the main edit
|
|
151
|
-
# * nor with themselves.
|
|
152
|
-
# */
|
|
153
|
-
# additionalTextEdits?: TextEdit[];
|
|
154
|
-
# /**
|
|
155
|
-
# * An optional set of characters that when pressed while this completion is active will accept it first and
|
|
156
|
-
# * then type that character. *Note* that all commit characters should have `length=1` and that superfluous
|
|
157
|
-
# * characters will be ignored.
|
|
158
|
-
# */
|
|
159
|
-
# commitCharacters?: string[];
|
|
160
|
-
# /**
|
|
161
|
-
# * An optional command that is executed *after* inserting this completion. *Note* that
|
|
162
|
-
# * additional modifications to the current document should be described with the
|
|
163
|
-
# * additionalTextEdits-property.
|
|
164
|
-
# */
|
|
165
|
-
# command?: Command;
|
|
166
|
-
# /**
|
|
167
|
-
# * An data entry field that is preserved on a completion item between
|
|
168
|
-
# * a completion and a completion resolve request.
|
|
169
|
-
# */
|
|
170
|
-
# data?: any
|
|
171
|
-
# }
|
|
112
|
+
# How many times the background scan retries a file when the database is locked.
|
|
113
|
+
SCAN_BUSY_RETRIES = 3
|
|
114
|
+
SCAN_BUSY_RETRY_DELAY = 1 # seconds
|
|
115
|
+
|
|
116
|
+
# Stores and parses one file for the background scan. Only a locked database is
|
|
117
|
+
# retried, briefly; any other error is raised for the caller to log.
|
|
118
|
+
def index_file(uri, text)
|
|
119
|
+
attempts = 0
|
|
120
|
+
begin
|
|
121
|
+
ActiveRecord::Base.connection_pool.with_connection do
|
|
122
|
+
update_document_content(uri, text)
|
|
123
|
+
code_file_for_uri(uri).refresh_scopes_if_needed
|
|
124
|
+
end
|
|
125
|
+
rescue ActiveRecord::StatementTimeout => e
|
|
126
|
+
attempts += 1
|
|
127
|
+
raise if attempts > SCAN_BUSY_RETRIES
|
|
128
|
+
|
|
129
|
+
RubyLanguageServer.logger.warn("Database busy indexing #{uri}, retrying: #{e.message}")
|
|
130
|
+
sleep scan_busy_retry_delay
|
|
131
|
+
retry
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def scan_busy_retry_delay
|
|
136
|
+
SCAN_BUSY_RETRY_DELAY
|
|
137
|
+
end
|
|
172
138
|
|
|
173
139
|
def scan_all_project_files
|
|
174
140
|
project_ruby_files = Dir.glob("#{self.class.root_path}**/*.rb")
|
|
175
141
|
RubyLanguageServer.logger.debug('Threading up!')
|
|
176
|
-
root_uri =
|
|
177
|
-
root_uri += '/' unless root_uri.end_with? '/'
|
|
142
|
+
root_uri = scan_root_uri
|
|
178
143
|
# Using fork because this is run in a docker container that has fork.
|
|
179
144
|
# If you want to run this on some platform without fork, fork the code and PR it :-)
|
|
180
145
|
fork_id = fork do
|
|
181
146
|
project_ruby_files.each do |container_path|
|
|
182
|
-
|
|
183
|
-
next if
|
|
147
|
+
relative_path = container_path.delete_prefix(self.class.root_path)
|
|
148
|
+
next if self.class.skip_scan?(relative_path)
|
|
184
149
|
|
|
185
150
|
text = File.read(container_path)
|
|
186
|
-
relative_path = container_path.delete_prefix(self.class.root_path)
|
|
187
151
|
host_uri = root_uri + relative_path
|
|
188
152
|
RubyLanguageServer.logger.debug("Threading #{host_uri}")
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
end
|
|
194
|
-
rescue StandardError => e
|
|
195
|
-
RubyLanguageServer.logger.warn("Error updating: #{e}\n#{e.backtrace * "\n"}")
|
|
196
|
-
sleep 5
|
|
197
|
-
retry
|
|
198
|
-
end
|
|
153
|
+
index_file(host_uri, text)
|
|
154
|
+
rescue StandardError => e
|
|
155
|
+
# One bad file mustn't stall the scan: log it and go on to the next.
|
|
156
|
+
RubyLanguageServer.logger.error("Skipping #{container_path}: #{e.class}: #{e}\n#{e.backtrace * "\n"}")
|
|
199
157
|
end
|
|
158
|
+
index_core
|
|
200
159
|
end
|
|
201
160
|
RubyLanguageServer.logger.debug("Forked process id to look at other files: #{fork_id}")
|
|
202
161
|
Process.detach(fork_id)
|
|
203
162
|
end
|
|
204
163
|
|
|
164
|
+
# Ruby's core classes, after the project's files, which matter more. A failure is
|
|
165
|
+
# logged: the project index still works without them. Set
|
|
166
|
+
# RUBY_LANGUAGE_SERVER_CORE_INDEX=false to skip them (the specs do).
|
|
167
|
+
def index_core
|
|
168
|
+
return if ENV['RUBY_LANGUAGE_SERVER_CORE_INDEX'] == 'false'
|
|
169
|
+
|
|
170
|
+
ActiveRecord::Base.connection_pool.with_connection { CoreIndex.load! }
|
|
171
|
+
rescue StandardError => e
|
|
172
|
+
RubyLanguageServer.logger.error("Couldn't index Ruby core classes: #{e.class}: #{e}\n#{e.backtrace * "\n"}")
|
|
173
|
+
end
|
|
174
|
+
|
|
205
175
|
# returns diagnostic info (if possible)
|
|
206
176
|
def update_document_content(uri, text)
|
|
207
177
|
RubyLanguageServer.logger.debug("update_document_content: #{uri}")
|
|
@@ -213,68 +183,308 @@ module RubyLanguageServer
|
|
|
213
183
|
# diagnostics_ready? ? updated_diagnostics_for_codefile(code_file) : []
|
|
214
184
|
end
|
|
215
185
|
|
|
216
|
-
# def updated_diagnostics_for_codefile(code_file)
|
|
217
|
-
# # Maybe we should be sharing this GoodCop across instances
|
|
218
|
-
# RubyLanguageServer.logger.debug("updated_diagnostics_for_codefile: #{code_file.uri}")
|
|
219
|
-
# project_relative_filename = filename_relative_to_project(code_file.uri)
|
|
220
|
-
# # code_file.diagnostics = GoodCop.instance&.diagnostics(code_file.text, project_relative_filename)
|
|
221
|
-
# RubyLanguageServer.logger.debug("code_file.diagnostics: #{code_file.diagnostics}")
|
|
222
|
-
# code_file.diagnostics
|
|
223
|
-
# end
|
|
224
|
-
|
|
225
186
|
# Returns the context of what is being typed in the given line
|
|
226
187
|
def context_at_location(uri, position)
|
|
227
188
|
code_file = code_file_for_uri(uri)
|
|
228
189
|
code_file&.context_at_location(position)
|
|
229
190
|
end
|
|
230
191
|
|
|
192
|
+
# FileChangeType values in workspace/didChangeWatchedFiles.
|
|
193
|
+
FILE_CREATED = 1
|
|
194
|
+
FILE_CHANGED = 2
|
|
195
|
+
FILE_DELETED = 3
|
|
196
|
+
|
|
197
|
+
# Keeps the index in step with files created, changed or deleted outside the editor.
|
|
198
|
+
def watched_files_changed(changes)
|
|
199
|
+
changes.each do |change|
|
|
200
|
+
uri = change['uri']
|
|
201
|
+
case change['type']
|
|
202
|
+
when FILE_DELETED then forget_uri(uri)
|
|
203
|
+
when FILE_CREATED, FILE_CHANGED then reload_from_disk(uri)
|
|
204
|
+
end
|
|
205
|
+
rescue StandardError => e
|
|
206
|
+
RubyLanguageServer.logger.error("Error handling watched file change #{change}: #{e.class}: #{e}")
|
|
207
|
+
end
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
# Drops a file from the index. If uri is a directory, drops everything under it.
|
|
211
|
+
def forget_uri(uri)
|
|
212
|
+
under_directory = "#{ActiveRecord::Base.sanitize_sql_like(uri.delete_suffix('/'))}/%"
|
|
213
|
+
CodeFile.where(uri:).or(CodeFile.where('uri LIKE ?', under_directory)).destroy_all
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
def reload_from_disk(uri)
|
|
217
|
+
path = container_path_for(uri)
|
|
218
|
+
return if path.nil? || !path.end_with?('.rb') || !File.file?(path)
|
|
219
|
+
|
|
220
|
+
update_document_content(uri, File.read(path))
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
# Maps an editor URI under the project root to the path this server reads it from.
|
|
224
|
+
# Returns nil for URIs outside the project.
|
|
225
|
+
def container_path_for(uri)
|
|
226
|
+
prefix = self.class.root_uri
|
|
227
|
+
return nil if self.class.root_path.nil? || !uri.start_with?(prefix)
|
|
228
|
+
|
|
229
|
+
File.join(self.class.root_path, URI.decode_uri_component(uri.delete_prefix(prefix)))
|
|
230
|
+
end
|
|
231
|
+
|
|
231
232
|
def word_at_location(uri, position)
|
|
232
233
|
context_at_location(uri, position)&.last
|
|
233
234
|
end
|
|
234
235
|
|
|
236
|
+
# Locations of what the word at position refers to. Records with nowhere the editor can
|
|
237
|
+
# open, like Ruby core (CoreIndex), have no code file and are left out.
|
|
235
238
|
def possible_definitions(uri, position)
|
|
236
|
-
|
|
237
|
-
|
|
239
|
+
records_to_locations(definition_records(uri, position))
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
# The scopes and variables the word at position refers to, the project's own first.
|
|
243
|
+
def definition_records(uri, position)
|
|
244
|
+
context = context_at_location(uri, position)
|
|
245
|
+
name = context&.last
|
|
246
|
+
return [] if name.blank?
|
|
238
247
|
|
|
248
|
+
# Special case: "new" should find "initialize" (instance method, not class method)
|
|
239
249
|
name = 'initialize' if name == 'new'
|
|
240
|
-
|
|
241
|
-
|
|
250
|
+
|
|
251
|
+
# Get current scopes for context
|
|
252
|
+
current_scopes = scopes_at(uri, position)
|
|
253
|
+
|
|
254
|
+
# If context has more than one element it could be a method call or namespace reference
|
|
255
|
+
if context.length > 1
|
|
256
|
+
# Find the rightmost class/module reference in the context (excluding the last element which is the name)
|
|
257
|
+
# Examples:
|
|
258
|
+
# - foo.Bar::Baz.something -> find Baz (index 2), build scope from Bar::Baz
|
|
259
|
+
# - Bar.foo.Baz.something -> find Baz (index 2), use Baz as scope
|
|
260
|
+
class_module_indices = []
|
|
261
|
+
(0...(context.length - 1)).each do |i|
|
|
262
|
+
class_module_indices << i if likely_class_name?(context[i])
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
if class_module_indices.any?
|
|
266
|
+
# Find the continuous chain of class/module names ending at the rightmost one
|
|
267
|
+
# For "Foo::Bar.method", we want both Foo and Bar, not just Bar
|
|
268
|
+
# For "foo.Bar::Baz.method", we want Bar and Baz, not just Baz
|
|
269
|
+
rightmost_class_index = class_module_indices.last
|
|
270
|
+
|
|
271
|
+
# Find the leftmost index of the continuous chain ending at rightmost_class_index
|
|
272
|
+
leftmost_continuous_index = rightmost_class_index
|
|
273
|
+
class_module_indices.reverse.each_cons(2) do |right, left|
|
|
274
|
+
break unless right == left + 1 # Break if not continuous
|
|
275
|
+
|
|
276
|
+
leftmost_continuous_index = left
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
scope_path_parts = context[leftmost_continuous_index..-2]
|
|
280
|
+
# A leading :: (::Foo::Bar) splits into an empty first element: top level only.
|
|
281
|
+
top_level = leftmost_continuous_index.positive? && context[leftmost_continuous_index - 1] == ''
|
|
282
|
+
|
|
283
|
+
# Resolve the receiver the way Ruby does: inside the enclosing namespaces first.
|
|
284
|
+
receiver_scopes = resolve_constant_scopes(scope_path_parts.join('::'), current_scopes, top_level:)
|
|
285
|
+
|
|
286
|
+
# Determine if it's a class/module lookup or a method call
|
|
287
|
+
# If the name also looks like a class/module name, it's a namespace lookup (Foo::Bar)
|
|
288
|
+
# Otherwise it's a method call (Foo.method or Foo::Bar.method)
|
|
289
|
+
return receiver_definition_records(name, receiver_scopes) if likely_class_name?(name) || constant_name?(name)
|
|
290
|
+
|
|
291
|
+
# Method call - determine if it's a class method or instance method
|
|
292
|
+
class_method_filter = name != 'initialize'
|
|
293
|
+
return receiver_definition_records(name, receiver_scopes, class_method_filter)
|
|
294
|
+
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
# No class/module found in chain, treat as instance method call on unknown type
|
|
298
|
+
# Search project-wide for all instance methods with this name
|
|
299
|
+
return project_definition_records(name, [], false)
|
|
300
|
+
end
|
|
301
|
+
|
|
302
|
+
# No receiver - search in scope chain first, then project-wide
|
|
303
|
+
results = scope_variables_named(name, current_scopes.first)
|
|
242
304
|
return results unless results.empty?
|
|
243
305
|
|
|
244
|
-
|
|
306
|
+
project_definition_records(name, current_scopes)
|
|
307
|
+
end
|
|
308
|
+
|
|
309
|
+
# Hover for the word at position: the signature of the first definition that
|
|
310
|
+
# definition_records finds, or nil.
|
|
311
|
+
def hover_at(uri, position)
|
|
312
|
+
record = definition_records(uri, position).first
|
|
313
|
+
markdown = record && Hover.markdown(record)
|
|
314
|
+
return nil if markdown.blank?
|
|
315
|
+
|
|
316
|
+
{ contents: { kind: 'markdown', value: markdown } }
|
|
245
317
|
end
|
|
246
318
|
|
|
247
319
|
# Return variables found in the current scope. After all, those are the important ones.
|
|
248
320
|
# Should probably be private...
|
|
249
321
|
def scope_definitions_for(name, scope, uri)
|
|
322
|
+
scope_variables_named(name, scope).map { |variable| Location.hash(uri, variable.line, variable.column.to_i) }
|
|
323
|
+
end
|
|
324
|
+
|
|
325
|
+
# class_method_filter is for new -> initialize
|
|
326
|
+
def project_definitions_for(name, parent_scopes = [], class_method_filter = nil)
|
|
327
|
+
records_to_locations(project_definition_records(name, parent_scopes, class_method_filter))
|
|
328
|
+
end
|
|
329
|
+
|
|
330
|
+
private
|
|
331
|
+
|
|
332
|
+
# The variables named name in scope and the scopes around it, innermost first.
|
|
333
|
+
def scope_variables_named(name, scope)
|
|
334
|
+
matches = []
|
|
250
335
|
check_scope = scope
|
|
251
|
-
return_array = []
|
|
252
336
|
while check_scope
|
|
253
|
-
|
|
254
|
-
return_array << Location.hash(uri, variable.line, 1) if variable.name == name
|
|
255
|
-
end
|
|
337
|
+
matches.concat(check_scope.variables.select { |variable| variable.name == name })
|
|
256
338
|
check_scope = check_scope.parent
|
|
257
339
|
end
|
|
258
|
-
|
|
259
|
-
return_array.uniq
|
|
340
|
+
matches
|
|
260
341
|
end
|
|
261
342
|
|
|
262
|
-
def
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
343
|
+
def project_definition_records(name, parent_scopes = [], class_method_filter = nil)
|
|
344
|
+
# If no parent scope, search project-wide
|
|
345
|
+
return search_project_wide(name, class_method_filter) if parent_scopes.empty?
|
|
346
|
+
|
|
347
|
+
supplied_scope = parent_scopes.first
|
|
348
|
+
|
|
349
|
+
# First pass: Look for variables (constants) in the scope chain
|
|
350
|
+
result = search_variables_in_scope_chain(name, supplied_scope)
|
|
351
|
+
return result if result.any?
|
|
352
|
+
|
|
353
|
+
# Second pass: Look for child scopes in the scope chain
|
|
354
|
+
result = search_child_scopes_in_chain(name, supplied_scope, class_method_filter)
|
|
355
|
+
return result if result.any?
|
|
356
|
+
|
|
357
|
+
# Fall back to project-wide search
|
|
358
|
+
search_project_wide(name, class_method_filter)
|
|
359
|
+
end
|
|
360
|
+
|
|
361
|
+
# Scopes and variables with a file first, so the project's own definitions win over
|
|
362
|
+
# Ruby core (CoreIndex), which has none.
|
|
363
|
+
def with_files_first(records)
|
|
364
|
+
records = records.to_a
|
|
365
|
+
# One query for all the records' files, not one per record.
|
|
366
|
+
ActiveRecord::Associations::Preloader.new(records:, associations: :code_file).call
|
|
367
|
+
records.partition(&:code_file).flatten
|
|
368
|
+
end
|
|
369
|
+
|
|
370
|
+
# Location hashes for the records that have a file the editor can open.
|
|
371
|
+
def records_to_locations(records)
|
|
372
|
+
with_files_first(records).select(&:code_file).map do |record|
|
|
373
|
+
line = record.respond_to?(:top_line) ? record.top_line : record.line
|
|
374
|
+
Location.hash(record.code_file.uri, line, record.column.to_i)
|
|
267
375
|
end
|
|
268
376
|
end
|
|
269
377
|
|
|
270
|
-
|
|
378
|
+
# Search for a variable/constant up the scope chain (first pass)
|
|
379
|
+
# Variables in scope chain take priority over child scopes
|
|
380
|
+
def search_variables_in_scope_chain(name, supplied_scope)
|
|
381
|
+
current = supplied_scope
|
|
382
|
+
while current
|
|
383
|
+
# For constants, skip method scopes and keep searching up to class/module scopes
|
|
384
|
+
if current.class_type == RubyLanguageServer::ScopeData::Scope::TYPE_METHOD
|
|
385
|
+
current = current.parent
|
|
386
|
+
next
|
|
387
|
+
end
|
|
271
388
|
|
|
272
|
-
|
|
273
|
-
|
|
389
|
+
# Check variables (constants) in the current scope
|
|
390
|
+
matching_variable = current.variables.where(name: name).includes(:code_file).find(&:code_file)
|
|
391
|
+
return [matching_variable] if matching_variable
|
|
392
|
+
|
|
393
|
+
current = current.parent
|
|
394
|
+
end
|
|
395
|
+
[]
|
|
396
|
+
end
|
|
397
|
+
|
|
398
|
+
# Search for child scopes up the scope chain (second pass)
|
|
399
|
+
def search_child_scopes_in_chain(name, supplied_scope, class_method_filter)
|
|
400
|
+
current = supplied_scope
|
|
401
|
+
while current
|
|
402
|
+
# For constants, skip method scopes and keep searching up to class/module scopes
|
|
403
|
+
if current.class_type == RubyLanguageServer::ScopeData::Scope::TYPE_METHOD
|
|
404
|
+
current = current.parent
|
|
405
|
+
next
|
|
406
|
+
end
|
|
407
|
+
|
|
408
|
+
# Check child scopes (classes/modules/methods) with this name, then, for a class,
|
|
409
|
+
# its superclasses' before moving out to the enclosing scope, as Ruby does.
|
|
410
|
+
[current, *(current.class_scope? ? current.ancestor_class_scopes : [])].each do |scope|
|
|
411
|
+
child_scopes = scope.children.where(name: name)
|
|
412
|
+
child_scopes = child_scopes.where(class_method: class_method_filter) unless class_method_filter.nil?
|
|
413
|
+
return with_files_first(child_scopes) if child_scopes.any?
|
|
414
|
+
end
|
|
415
|
+
|
|
416
|
+
current = current.parent
|
|
417
|
+
end
|
|
418
|
+
[]
|
|
419
|
+
end
|
|
420
|
+
|
|
421
|
+
# Search for a name project-wide (fallback)
|
|
422
|
+
def search_project_wide(name, class_method_filter)
|
|
423
|
+
all_scopes = RubyLanguageServer::ScopeData::Scope.where(name: name)
|
|
424
|
+
all_scopes = all_scopes.where(class_method: class_method_filter) unless class_method_filter.nil?
|
|
425
|
+
all_variables = RubyLanguageServer::ScopeData::Variable.where(name: name)
|
|
426
|
+
with_files_first(all_scopes.to_a + all_variables.to_a)
|
|
427
|
+
end
|
|
428
|
+
|
|
429
|
+
# The scopes a constant path like "Bar" or "Bar::Baz" refers to from the cursor.
|
|
430
|
+
# Like Ruby, try each enclosing class or module, innermost first, then the top
|
|
431
|
+
# level. Inside Outer::Other, "Inner" tries Outer::Other::Inner, Outer::Inner, Inner.
|
|
432
|
+
# Returns every scope with the first path that exists: a class reopened in several
|
|
433
|
+
# files has one scope per file. Returns [] if nothing matches.
|
|
434
|
+
#
|
|
435
|
+
# This uses the nesting of scope paths, so `class Outer::Other` counts as being
|
|
436
|
+
# inside Outer, which Ruby's lexical scope doesn't do.
|
|
437
|
+
def resolve_constant_scopes(path, current_scopes, top_level: false)
|
|
438
|
+
return [] if path.blank?
|
|
439
|
+
|
|
440
|
+
namespaces = top_level ? [] : current_scopes.select { |scope| type_is_class_or_module?(scope) }.map(&:path)
|
|
441
|
+
candidates = namespaces.map { |namespace| "#{namespace}::#{path}" } + [path]
|
|
442
|
+
candidates.each do |candidate|
|
|
443
|
+
scopes = RubyLanguageServer::ScopeData::Scope.where(path: candidate).to_a
|
|
444
|
+
return scopes if scopes.any?
|
|
445
|
+
end
|
|
446
|
+
[]
|
|
274
447
|
end
|
|
275
448
|
|
|
276
|
-
def
|
|
277
|
-
|
|
449
|
+
def type_is_class_or_module?(scope)
|
|
450
|
+
[RubyLanguageServer::ScopeData::Base::TYPE_CLASS, RubyLanguageServer::ScopeData::Base::TYPE_MODULE].include?(scope.class_type)
|
|
451
|
+
end
|
|
452
|
+
|
|
453
|
+
# Definitions of name inside the receiver's scopes (and their parents), or anywhere
|
|
454
|
+
# in the project if there are none.
|
|
455
|
+
def receiver_definition_records(name, receiver_scopes, class_method_filter = nil)
|
|
456
|
+
return search_project_wide(name, class_method_filter) if receiver_scopes.empty?
|
|
457
|
+
|
|
458
|
+
results = receiver_scopes.flat_map { |scope| search_variables_in_scope_chain(name, scope) }
|
|
459
|
+
results = receiver_scopes.flat_map { |scope| search_child_scopes_in_chain(name, scope, class_method_filter) } if results.empty?
|
|
460
|
+
results = search_project_wide(name, class_method_filter) if results.empty?
|
|
461
|
+
results.uniq
|
|
462
|
+
end
|
|
463
|
+
|
|
464
|
+
# Check if a name looks like a constant (all uppercase)
|
|
465
|
+
def constant_name?(name)
|
|
466
|
+
# Must start with uppercase letter and contain no lowercase letters
|
|
467
|
+
return false unless /\A[A-Z]/.match?(name)
|
|
468
|
+
|
|
469
|
+
!/[a-z]/.match?(name)
|
|
470
|
+
end
|
|
471
|
+
|
|
472
|
+
# Guess if a receiver name is likely a class name based on idiomatic Ruby conventions.
|
|
473
|
+
# This is a heuristic and not 100% accurate (e.g., FOO could be a constant holding an instance).
|
|
474
|
+
# Returns true for names like "Foo" or "FooBar" (starts with uppercase, has lowercase letters).
|
|
475
|
+
# Returns false for "FOO" (all uppercase constant) or "foo" (variable/method).
|
|
476
|
+
def likely_class_name?(name)
|
|
477
|
+
# Must start with uppercase letter
|
|
478
|
+
return false unless /[A-Z]/.match?(name[0])
|
|
479
|
+
|
|
480
|
+
# Must contain at least one lowercase letter to distinguish from constants like FOO
|
|
481
|
+
name =~ /[a-z]/
|
|
482
|
+
end
|
|
483
|
+
|
|
484
|
+
# The stored file for uri. For a URI the server hasn't seen, an unsaved, empty
|
|
485
|
+
# CodeFile, so lookups don't write to the database; CodeFile#update_text saves it.
|
|
486
|
+
def code_file_for_uri(uri)
|
|
487
|
+
CodeFile.find_by(uri:) || CodeFile.new(uri:)
|
|
278
488
|
end
|
|
279
489
|
end
|
|
280
490
|
end
|
|
@@ -25,19 +25,27 @@ module RubyLanguageServer
|
|
|
25
25
|
TYPE_VARIABLE => '^'
|
|
26
26
|
}.freeze
|
|
27
27
|
|
|
28
|
-
|
|
28
|
+
# Return the class_type as a symbol for easier testing
|
|
29
|
+
def type
|
|
30
|
+
class_type&.to_sym
|
|
31
|
+
end
|
|
29
32
|
|
|
30
33
|
# bar should be closer to Bar than Far. Adding the UPPER version accomplishes this.
|
|
34
|
+
# The word is always passed as a bound value, never interpolated into the SQL.
|
|
31
35
|
scope :with_distance_from, lambda { |word|
|
|
32
|
-
|
|
33
|
-
|
|
36
|
+
length = word.length
|
|
37
|
+
distance = sanitize_sql_array([
|
|
38
|
+
'*, LEVENSHTEIN(SUBSTR(name, 1, ?), ?) + LEVENSHTEIN(SUBSTR(UPPER(name), 1, ?), UPPER(?)) AS levenshtein_distance',
|
|
39
|
+
length, word, length, word
|
|
40
|
+
])
|
|
41
|
+
where('LENGTH(name) >= ?', length).select(distance)
|
|
34
42
|
}
|
|
35
43
|
scope :closest_to, ->(word) { with_distance_from(word).order(:levenshtein_distance) }
|
|
36
44
|
|
|
37
45
|
# RubyLanguageServer::ScopeData::Scope.connection.exec_query("SELECT LEVENSHTEIN( 'This is not correct', 'This is correct' )")
|
|
38
46
|
|
|
39
47
|
def method?
|
|
40
|
-
|
|
48
|
+
class_type == TYPE_METHOD
|
|
41
49
|
end
|
|
42
50
|
end
|
|
43
51
|
end
|