ruby_language_server 0.9.9 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9a7d97c691bd4521a2e8c1530a9f393f1cd9a8f95c82e43f4c2d19f250b96107
4
- data.tar.gz: ceb2e4e69bfb70eedc92228dbeb70e6ce1525ed2cdf1eef5dceb11546fafb795
3
+ metadata.gz: 07d1400ade2c96eadb3a296739d277651d8de772f05e99e603c96cd61648994c
4
+ data.tar.gz: 507cb4a0251f28c61a82ac0c75c8adbd30fabf26e3b61fb23b10fbf1e27f31bc
5
5
  SHA512:
6
- metadata.gz: 32ea5d3d4518515baf5d2c0801021670b4cfec18031995f13c5ec2a129b640906a3942469dcf26577cd397bdaec5002f3079f0faef2add125f0e9e50871eb496
7
- data.tar.gz: 6c7abcae6d89c7268c7793bd29a216ed8667ab3c1b0280ffe6f530356c89082b05b6a7335b93081fea792cf2f9134572665c4ecdae14736c3ee381dac2bbf72b
6
+ metadata.gz: dc085581df4ad5c5820b3aef69e3e8f44e3f2f3852e6ea6e0a5e9296e235f61618fb46b2a63aef781dff0df2490660f3bba1805d26528baff234391347df07cb
7
+ data.tar.gz: 4b76e646f09ac0858b4b33712f3ce3aaabd662d09c1b5d1d9c5b5522f1af8fd2e057c8b5dc4ea20f8792d76e6bc456f3ef9ba43c27e885edd6d28e28ba4a5715
data/CHANGELOG.txt CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ #### 0.9.10 Tue Nov 24 23:52:32 PST 2026
4
+
5
+ * Completion and hover know Ruby's core classes and modules (String, Array, Hash, Kernel, ...), with signatures and documentation from the RBS core signatures. The project's own definitions come first. Go-to-definition doesn't offer core methods yet, because their files are inside the container. Set RUBY_LANGUAGE_SERVER_CORE_INDEX=false to skip them
6
+
3
7
  #### 0.9.9 Tue Nov 17 08:09:13 PST 2026
4
8
 
5
9
  * Hover: show what a name refers to, from the existing index
data/FAQ_ROADMAP.md CHANGED
@@ -9,4 +9,7 @@
9
9
 
10
10
  * Pay special attention to the project's Gemfile. Install all the gems (we can). Integrate with gem server?
11
11
  * Full scan of installed gems?
12
+ * ActiveSupport's core extensions (blank?, present?, 3.days) alongside the Ruby core classes. They're plain Ruby, so the existing parser can read them.
13
+ * Go-to-definition for Ruby core methods, through a virtual document for the .rbs file.
14
+ * Know what type a local variable holds, so `name.up` completes String methods.
12
15
  * Integrate class based scope logic. If I'm in Foo < Bar then I should see Bar's methods at just slightly lower priority than Foo's.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ruby_language_server (0.9.9)
4
+ ruby_language_server (0.9.10)
5
5
  activerecord (~> 8.1)
6
6
  amatch
7
7
  bundler
@@ -10,6 +10,7 @@ PATH
10
10
  json
11
11
  ostruct
12
12
  prism (>= 1.8.0)
13
+ rbs (>= 4.0)
13
14
  sqlite3
14
15
 
15
16
  GEM
@@ -42,6 +43,9 @@ GEM
42
43
  base64 (0.3.0)
43
44
  bigdecimal (4.1.3)
44
45
  builder (3.3.0)
46
+ bundler-audit (0.9.3)
47
+ bundler (>= 1.2.0)
48
+ thor (~> 1.0)
45
49
  coderay (1.1.3)
46
50
  concurrent-ruby (1.3.8)
47
51
  connection_pool (3.0.2)
@@ -225,6 +229,7 @@ PLATFORMS
225
229
  x86_64-linux-musl
226
230
 
227
231
  DEPENDENCIES
232
+ bundler-audit
228
233
  debug
229
234
  guard
230
235
  guard-minitest
@@ -253,6 +258,7 @@ CHECKSUMS
253
258
  base64 (0.3.0) sha256=27337aeabad6ffae05c265c450490628ef3ebd4b67be58257393227588f5a97b
254
259
  bigdecimal (4.1.3) sha256=61ebe1e5e559bdc3cc6f2c0ee7f427321fc838f59611c294356eb04d6e21cf66
255
260
  builder (3.3.0) sha256=497918d2f9dca528fdca4b88d84e4ef4387256d984b8154e9d5d3fe5a9c8835f
261
+ bundler-audit (0.9.3) sha256=81c8766c71e47d0d28a0f98c7eed028539f21a6ea3cd8f685eb6f42333c9b4e9
256
262
  coderay (1.1.3) sha256=dc530018a4684512f8f38143cd2a096c9f02a1fc2459edcfe534787a7fc77d4b
257
263
  concurrent-ruby (1.3.8) sha256=b2f1be836e968ccc78ccfce277ea79c72a88633f22306782c16ff23fb415d1e1
258
264
  connection_pool (3.0.2) sha256=33fff5ba71a12d2aa26cb72b1db8bba2a1a01823559fb01d29eb74c286e62e0a
@@ -317,7 +323,7 @@ CHECKSUMS
317
323
  rubocop-rake (0.7.1) sha256=3797f2b6810c3e9df7376c26d5f44f3475eda59eb1adc38e6f62ecf027cbae4d
318
324
  rubocop-rspec (3.10.2) sha256=0b3e2ecc592cd10ecbf0095bb58d1e357905276e069643523cc19eb7495f65e2
319
325
  ruby-progressbar (1.13.0) sha256=80fc9c47a9b640d6834e0dc7b3c94c9df37f08cb072b7761e4a71e22cff29b33
320
- ruby_language_server (0.9.9)
326
+ ruby_language_server (0.9.10)
321
327
  securerandom (0.4.1) sha256=cc5193d414a4341b6e225f0cb4446aceca8e50d5e1888743fac16987638ea0b1
322
328
  shellany (0.0.1) sha256=0e127a9132698766d7e752e82cdac8250b6adbd09e6c0a7fbbb6f61964fedee7
323
329
  simplecov (0.22.0) sha256=fe2622c7834ff23b98066bb0a854284b2729a569ac659f82621fc22ef36213a5
data/Makefile CHANGED
@@ -1,4 +1,4 @@
1
- .PHONY: image guard continuous_development console test coverage rubocop shell run_in_shell server debug_server gem gem_release
1
+ .PHONY: image guard continuous_development console test coverage rubocop audit shell run_in_shell server debug_server gem gem_release
2
2
  PROJECT_NAME=ruby_language_server
3
3
  # The image runs as UID 1000. Dev and test containers run as root, because they write
4
4
  # coverage/ and logs into the mounted checkout, which CI runners own under another UID.
@@ -38,6 +38,10 @@ coverage: image
38
38
  rubocop: image
39
39
  docker run --rm $(LOCAL_LINK) $(PROJECT_NAME) sh -c "bundle exec rubocop"
40
40
 
41
+ # Checks Gemfile.lock for gems with known vulnerabilities, using a freshly fetched ruby-advisory-db.
42
+ audit: image
43
+ docker run --rm $(LOCAL_LINK) $(PROJECT_NAME) sh -c "bundle exec bundle-audit check --update"
44
+
41
45
  shell: image
42
46
  ./bin/run_in_shell sh
43
47
 
data/README.md CHANGED
@@ -17,6 +17,8 @@ Help welcome.
17
17
  * Definitions
18
18
  * Completions
19
19
  * Document symbols
20
+ * Hover
21
+ * Ruby core classes (String, Array, Hash, ...) in completion and hover, from the RBS core signatures
20
22
  * Please see the [FAQ_ROADMAP.md](./FAQ_ROADMAP.md)
21
23
 
22
24
  # Editor Integrations
@@ -104,8 +106,8 @@ Notes:
104
106
 
105
107
  # Release instructions to self
106
108
 
107
- Images and the gem are published only by the [release workflow](.github/workflows/release.yml),
108
- when a `v*` tag is pushed for a commit on master. Never push `kwerle/ruby_language_server` from a
109
+ Images are published only by the [release workflow](.github/workflows/release.yml), when a
110
+ `v*` tag is pushed for a commit on master. Never push `kwerle/ruby_language_server` from a
109
111
  laptop: the VS Code client runs `:latest` on every start.
110
112
 
111
113
  * bump version in [version.rb](lib/ruby_language_server/version.rb) file and [Gemfile.lock](Gemfile.lock)
@@ -114,12 +116,8 @@ laptop: the VS Code client runs `:latest` on every start.
114
116
  (with `git config gitflow.prefix.versiontag v`, this tags master as `v0.9.8`)
115
117
  * `git push origin master develop v0.9.8`
116
118
  * CI checks the tag matches `version.rb`, builds and pushes `:latest` and `:0.9.8`
117
- for amd64 and arm64, signs the image with cosign, pushes the gem to RubyGems, and creates
118
- the GitHub release.
119
- * The gem is pushed with RubyGems [trusted publishing](https://guides.rubygems.org/trusted-publishing/),
120
- so the repo holds no RubyGems key. On rubygems.org, ruby_language_server's trusted publisher is
121
- repository `kwerle/ruby_language_server`, workflow `release.yml`, environment `release`.
122
- `make gem_release` still works by hand if CI can't push.
119
+ for amd64 and arm64, signs the image with cosign, and creates the GitHub release.
120
+ * The gem is still released by hand: `make gem_release`
123
121
 
124
122
  To verify a published image:
125
123
 
data/lib/db/schema.rb CHANGED
@@ -20,6 +20,8 @@ module Schema
20
20
  t.string :class_type, null: false
21
21
  t.text :parameters # JSON string of method parameters
22
22
  t.boolean :class_method, default: false # true for class methods (def self.method)
23
+ t.text :signature # RBS overloads, one per line (core scopes only)
24
+ t.text :documentation # rdoc text (core scopes only)
23
25
  end
24
26
 
25
27
  add_index :scopes, :name
@@ -12,6 +12,7 @@ require_relative 'io'
12
12
  require_relative 'location'
13
13
  require_relative 'code_file'
14
14
  require_relative 'scope_parser'
15
+ require_relative 'core_index'
15
16
  require_relative 'project_manager'
16
17
  require_relative 'server'
17
18
  require_relative 'line_context'
@@ -0,0 +1,173 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rbs'
4
+
5
+ module RubyLanguageServer
6
+ # Ruby's core classes and modules (String, Array, Kernel, ...), read from the RBS core
7
+ # signatures and stored as ordinary scopes, so completion and hover know about them.
8
+ #
9
+ # The .rbs files are inside the container, where the editor can't open them, so these
10
+ # scopes store no location: no code file and no lines. Go-to-definition only returns
11
+ # records with a code file, so it leaves them out, and hover still finds them.
12
+ module CoreIndex
13
+ # Signatures say ::String for a top-level name. Drop the leading :: so hover reads
14
+ # like Ruby. A :: after a name (File::Stat) stays.
15
+ TOP_LEVEL_PREFIX = /(?<![\w\]])::(?=[A-Za-z_])/
16
+
17
+ # The call-seq block rdoc puts at the top of each comment.
18
+ RDOC_HEADER = /\A\s*<!--.*?-->\s*/m
19
+
20
+ class << self
21
+ # Stores the core scopes, unless they are already stored. only limits it to some
22
+ # classes and modules, like %w[String Integer], so specs don't store all of core.
23
+ def load!(only: nil)
24
+ return if loaded?
25
+
26
+ started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
27
+ classes = only ? class_entries.slice(*only) : class_entries
28
+ ActiveRecord::Base.transaction do
29
+ class_ids = insert_class_scopes(classes)
30
+ rows = classes.flat_map { |path, entry| method_rows(path, class_ids.fetch(path), entry) }
31
+ rows.each_slice(1000) { |slice| ScopeData::Scope.insert_all!(slice) }
32
+ end
33
+ elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started
34
+ RubyLanguageServer.logger.info("Indexed Ruby core classes in #{elapsed.round(2)}s")
35
+ end
36
+
37
+ # Core scopes are the only ones with a signature.
38
+ def loaded?
39
+ ScopeData::Scope.where(code_file_id: nil).where.not(signature: nil).exists?
40
+ end
41
+
42
+ private
43
+
44
+ # { 'File::Stat' => entry }. An entry can be declared in several files, and lists each
45
+ # declaration. Parsing the signatures takes
46
+ # a couple of seconds, so it's done once per process.
47
+ def class_entries
48
+ @class_entries ||= begin
49
+ loader = RBS::EnvironmentLoader.new(core_root: RBS::EnvironmentLoader::DEFAULT_CORE_ROOT)
50
+ RBS::Environment.from_loader(loader).class_decls.transform_keys { |type_name| type_name.to_s.delete_prefix('::') }
51
+ end
52
+ end
53
+
54
+ # Inserts one scope per class or module, a level of nesting at a time so each parent's
55
+ # id is known. Returns { path => id }.
56
+ def insert_class_scopes(classes)
57
+ ids = {}
58
+ classes.group_by { |path, _entry| path.count(':') }.sort.each do |_depth, level|
59
+ rows = level.map { |path, entry| class_row(path, entry, ids) }
60
+ ScopeData::Scope.insert_all!(rows)
61
+ ids.merge!(ScopeData::Scope.where(code_file_id: nil, path: level.map(&:first)).pluck(:path, :id).to_h)
62
+ end
63
+ ids
64
+ end
65
+
66
+ def class_row(path, entry, ids)
67
+ declarations = declarations_of(entry)
68
+ namespace, _, name = path.rpartition('::')
69
+ {
70
+ parent_id: ids[namespace],
71
+ name:,
72
+ path:,
73
+ class_type: declarations.first.is_a?(RBS::AST::Declarations::Module) ? ScopeData::Base::TYPE_MODULE : ScopeData::Base::TYPE_CLASS,
74
+ superclass_name: superclass_name(declarations, namespace),
75
+ documentation: declarations.filter_map { |declaration| documentation(declaration.comment) }.first
76
+ }
77
+ end
78
+
79
+ def declarations_of(entry)
80
+ declarations = []
81
+ entry.each_decl { |declaration| declarations << declaration }
82
+ declarations
83
+ end
84
+
85
+ # Qualified with the namespace the way the parser does it (see Scope#set_superclass_name),
86
+ # so Scope#superclass_scopes falls back to outer namespaces.
87
+ def superclass_name(declarations, namespace)
88
+ super_class = declarations.filter_map { |declaration| declaration.try(:super_class) }.first
89
+ return nil if super_class.nil?
90
+
91
+ written = super_class.name.to_s
92
+ return written.delete_prefix('::') if written.start_with?('::') || namespace.empty?
93
+
94
+ "#{namespace}::#{written}"
95
+ end
96
+
97
+ # Every method and alias in the class's declarations. A method declared in more than one
98
+ # file is stored once.
99
+ def method_rows(class_path, class_id, entry)
100
+ members = declarations_of(entry).flat_map(&:members)
101
+ definitions = members.grep(RBS::AST::Members::MethodDefinition).uniq { |definition| [definition.name, definition.kind] }
102
+ by_name = definitions.to_h { |definition| [[definition.name, definition.singleton?], definition] }
103
+ aliases = members.grep(RBS::AST::Members::Alias).filter_map do |alias_member|
104
+ original = by_name[[alias_member.old_name, alias_member.singleton?]]
105
+ [alias_member.new_name, original] if original
106
+ end
107
+ named = definitions.map { |definition| [definition.name, definition] } + aliases
108
+ named.uniq { |name, definition| [name, definition.kind] }.flat_map do |name, definition|
109
+ method_rows_for(class_path, class_id, definition, name)
110
+ end
111
+ end
112
+
113
+ # A module_function (self?.) method is both an instance method and a class method.
114
+ def method_rows_for(class_path, class_id, definition, name)
115
+ class_methods = case definition.kind
116
+ when :instance then [false]
117
+ when :singleton then [true]
118
+ else [false, true]
119
+ end
120
+ class_methods.map do |class_method|
121
+ {
122
+ parent_id: class_id,
123
+ name: name.to_s,
124
+ path: "#{class_path}#{ScopeData::Base::JoinHash[ScopeData::Base::TYPE_METHOD]}#{name}",
125
+ class_type: ScopeData::Base::TYPE_METHOD,
126
+ class_method:,
127
+ parameters: parameters(definition.overloads.first&.method_type)&.to_json,
128
+ signature: definition.overloads.map { |overload| readable(overload.method_type.to_s) }.join("\n"),
129
+ documentation: documentation(definition.comment)
130
+ }
131
+ end
132
+ end
133
+
134
+ # The first overload's parameters, in the { name:, type: } form the parser stores.
135
+ def parameters(method_type)
136
+ function = method_type&.type
137
+ return nil unless function.is_a?(RBS::Types::Function)
138
+
139
+ params = function.required_positionals.map { |param| { name: param_name(param), type: 'required' } }
140
+ params += function.optional_positionals.map { |param| { name: param_name(param), type: 'optional' } }
141
+ params << { name: "*#{param_name(function.rest_positionals)}", type: 'rest' } if function.rest_positionals
142
+ params += function.trailing_positionals.map { |param| { name: param_name(param), type: 'required' } }
143
+ params += function.required_keywords.keys.map { |key| { name: "#{key}:", type: 'keyword' } }
144
+ params += function.optional_keywords.keys.map { |key| { name: "#{key}:", type: 'keyword' } }
145
+ params << { name: "**#{param_name(function.rest_keywords)}", type: 'keyword_rest' } if function.rest_keywords
146
+ params << { name: '&block', type: 'block' } if method_type.block
147
+ params.presence
148
+ end
149
+
150
+ # RBS parameters often have only a type, like (::string, ?int). Name those after the
151
+ # type: string, int.
152
+ def param_name(param)
153
+ return param.name.to_s if param.name
154
+
155
+ type = param.type
156
+ type = type.type while type.is_a?(RBS::Types::Optional)
157
+ return 'arg' unless type.respond_to?(:name)
158
+
159
+ type.name.name.to_s.delete_prefix('_').gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase
160
+ end
161
+
162
+ def documentation(comment)
163
+ return nil if comment.nil?
164
+
165
+ comment.string.sub(RDOC_HEADER, '').strip.presence
166
+ end
167
+
168
+ def readable(signature)
169
+ signature.gsub(TOP_LEVEL_PREFIX, '')
170
+ end
171
+ end
172
+ end
173
+ end
@@ -15,11 +15,13 @@ module RubyLanguageServer
15
15
  private
16
16
 
17
17
  def scope_markdown(scope)
18
- case scope.class_type
19
- when ScopeData::Base::TYPE_METHOD then method_markdown(scope)
20
- when ScopeData::Base::TYPE_CLASS then code_block(['class', scope.path, superclass(scope)].compact.join(' '))
21
- when ScopeData::Base::TYPE_MODULE then code_block("module #{scope.path}")
22
- end
18
+ markdown = case scope.class_type
19
+ when ScopeData::Base::TYPE_METHOD then method_markdown(scope)
20
+ when ScopeData::Base::TYPE_CLASS then code_block(['class', scope.path, superclass(scope)].compact.join(' '))
21
+ when ScopeData::Base::TYPE_MODULE then code_block("module #{scope.path}")
22
+ end
23
+ # Only Ruby core scopes (CoreIndex) have documentation.
24
+ [markdown, scope.documentation.presence].compact.join("\n\n").presence
23
25
  end
24
26
 
25
27
  def superclass(scope)
@@ -28,12 +30,19 @@ module RubyLanguageServer
28
30
 
29
31
  def method_markdown(scope)
30
32
  receiver = scope.class_method ? 'self.' : ''
33
+ return [code_block(rbs_signature(scope, receiver)), container_line(scope.parent)].compact.join("\n\n") if scope.signature.present?
34
+
31
35
  parameters = scope.parsed_parameters.map { |parameter| parameter_text(parameter) }
32
36
  signature = "def #{receiver}#{scope.name}"
33
37
  signature += "(#{parameters.join(', ')})" if parameters.any?
34
38
  [code_block(signature), container_line(scope.parent)].compact.join("\n\n")
35
39
  end
36
40
 
41
+ # A Ruby core method's RBS overloads, one `def name: overload` line each.
42
+ def rbs_signature(scope, receiver)
43
+ scope.signature.lines(chomp: true).map { |overload| "def #{receiver}#{scope.name}: #{overload}" }.join("\n")
44
+ end
45
+
37
46
  # Parameters are stored as { 'name' => ..., 'type' => ... } (see PrismProcessor).
38
47
  # Rest, keyword rest and block names already carry their *, ** or &, and keyword
39
48
  # names their trailing colon.
@@ -155,11 +155,23 @@ module RubyLanguageServer
155
155
  # One bad file mustn't stall the scan: log it and go on to the next.
156
156
  RubyLanguageServer.logger.error("Skipping #{container_path}: #{e.class}: #{e}\n#{e.backtrace * "\n"}")
157
157
  end
158
+ index_core
158
159
  end
159
160
  RubyLanguageServer.logger.debug("Forked process id to look at other files: #{fork_id}")
160
161
  Process.detach(fork_id)
161
162
  end
162
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
+
163
175
  # returns diagnostic info (if possible)
164
176
  def update_document_content(uri, text)
165
177
  RubyLanguageServer.logger.debug("update_document_content: #{uri}")
@@ -221,7 +233,14 @@ module RubyLanguageServer
221
233
  context_at_location(uri, position)&.last
222
234
  end
223
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.
224
238
  def possible_definitions(uri, position)
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)
225
244
  context = context_at_location(uri, position)
226
245
  name = context&.last
227
246
  return [] if name.blank?
@@ -267,69 +286,61 @@ module RubyLanguageServer
267
286
  # Determine if it's a class/module lookup or a method call
268
287
  # If the name also looks like a class/module name, it's a namespace lookup (Foo::Bar)
269
288
  # Otherwise it's a method call (Foo.method or Foo::Bar.method)
270
- return receiver_definitions_for(name, receiver_scopes) if likely_class_name?(name) || constant_name?(name)
289
+ return receiver_definition_records(name, receiver_scopes) if likely_class_name?(name) || constant_name?(name)
271
290
 
272
291
  # Method call - determine if it's a class method or instance method
273
292
  class_method_filter = name != 'initialize'
274
- return receiver_definitions_for(name, receiver_scopes, class_method_filter)
293
+ return receiver_definition_records(name, receiver_scopes, class_method_filter)
275
294
 
276
295
  end
277
296
 
278
297
  # No class/module found in chain, treat as instance method call on unknown type
279
298
  # Search project-wide for all instance methods with this name
280
- return project_definitions_for(name, [], false)
299
+ return project_definition_records(name, [], false)
281
300
  end
282
301
 
283
302
  # No receiver - search in scope chain first, then project-wide
284
- scope = current_scopes.first
285
- results = scope_definitions_for(name, scope, uri)
303
+ results = scope_variables_named(name, current_scopes.first)
286
304
  return results unless results.empty?
287
305
 
288
- # Always return an array
289
- project_definitions_for(name, current_scopes) || []
306
+ project_definition_records(name, current_scopes)
290
307
  end
291
308
 
292
309
  # Hover for the word at position: the signature of the first definition that
293
- # possible_definitions finds, or nil.
310
+ # definition_records finds, or nil.
294
311
  def hover_at(uri, position)
295
- name = context_at_location(uri, position)&.last
296
- return nil if name.blank?
297
-
298
- name = 'initialize' if name == 'new'
299
- definition = possible_definitions(uri, position).first
300
- record = definition && record_at(definition, name)
312
+ record = definition_records(uri, position).first
301
313
  markdown = record && Hover.markdown(record)
302
314
  return nil if markdown.blank?
303
315
 
304
316
  { contents: { kind: 'markdown', value: markdown } }
305
317
  end
306
318
 
307
- # The scope or variable named name that a definition location points at.
308
- def record_at(location, name)
309
- code_file = CodeFile.find_by(uri: location[:uri])
310
- return nil if code_file.nil?
311
-
312
- line = location[:range][:start][:line] + 1 # stored lines are 1-based
313
- code_file.scopes.find_by(top_line: line, name:) || code_file.variables.find_by(line:, name:)
314
- end
315
-
316
319
  # Return variables found in the current scope. After all, those are the important ones.
317
320
  # Should probably be private...
318
321
  def scope_definitions_for(name, scope, uri)
319
- check_scope = scope
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)
320
334
  matches = []
335
+ check_scope = scope
321
336
  while check_scope
322
- check_scope.variables.each do |variable|
323
- matches << Location.hash(uri, variable.line, variable.column.to_i) if variable.name == name
324
- end
337
+ matches.concat(check_scope.variables.select { |variable| variable.name == name })
325
338
  check_scope = check_scope.parent
326
339
  end
327
- RubyLanguageServer.logger.debug("==============>> scope_definitions_for(#{name}, #{scope.to_json}, #{uri}: #{matches.inspect} )")
328
340
  matches
329
341
  end
330
342
 
331
- # class_method_filter is for new -> initialize
332
- def project_definitions_for(name, parent_scopes = [], class_method_filter = nil)
343
+ def project_definition_records(name, parent_scopes = [], class_method_filter = nil)
333
344
  # If no parent scope, search project-wide
334
345
  return search_project_wide(name, class_method_filter) if parent_scopes.empty?
335
346
 
@@ -347,16 +358,20 @@ module RubyLanguageServer
347
358
  search_project_wide(name, class_method_filter)
348
359
  end
349
360
 
350
- private
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
351
369
 
352
- # Convert a collection of scopes or variables to location hashes
353
- def scope_or_variable_to_locations(items)
354
- items = items.to_a
355
- # One query for all the items' files, not one per item.
356
- ActiveRecord::Associations::Preloader.new(records: items, associations: :code_file).call
357
- items.reject { |item| item.code_file.nil? }.map do |item|
358
- line = item.respond_to?(:top_line) ? item.top_line : item.line
359
- Location.hash(item.code_file.uri, line, item.column.to_i)
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)
360
375
  end
361
376
  end
362
377
 
@@ -373,10 +388,7 @@ module RubyLanguageServer
373
388
 
374
389
  # Check variables (constants) in the current scope
375
390
  matching_variable = current.variables.where(name: name).includes(:code_file).find(&:code_file)
376
- if matching_variable
377
- line = matching_variable.respond_to?(:top_line) ? matching_variable.top_line : matching_variable.line
378
- return [Location.hash(matching_variable.code_file.uri, line, matching_variable.column.to_i)]
379
- end
391
+ return [matching_variable] if matching_variable
380
392
 
381
393
  current = current.parent
382
394
  end
@@ -398,7 +410,7 @@ module RubyLanguageServer
398
410
  [current, *(current.class_scope? ? current.ancestor_class_scopes : [])].each do |scope|
399
411
  child_scopes = scope.children.where(name: name)
400
412
  child_scopes = child_scopes.where(class_method: class_method_filter) unless class_method_filter.nil?
401
- return scope_or_variable_to_locations(child_scopes) if child_scopes.any?
413
+ return with_files_first(child_scopes) if child_scopes.any?
402
414
  end
403
415
 
404
416
  current = current.parent
@@ -411,8 +423,7 @@ module RubyLanguageServer
411
423
  all_scopes = RubyLanguageServer::ScopeData::Scope.where(name: name)
412
424
  all_scopes = all_scopes.where(class_method: class_method_filter) unless class_method_filter.nil?
413
425
  all_variables = RubyLanguageServer::ScopeData::Variable.where(name: name)
414
- all_matches = all_scopes.to_a + all_variables.to_a
415
- scope_or_variable_to_locations(all_matches)
426
+ with_files_first(all_scopes.to_a + all_variables.to_a)
416
427
  end
417
428
 
418
429
  # The scopes a constant path like "Bar" or "Bar::Baz" refers to from the cursor.
@@ -441,7 +452,7 @@ module RubyLanguageServer
441
452
 
442
453
  # Definitions of name inside the receiver's scopes (and their parents), or anywhere
443
454
  # in the project if there are none.
444
- def receiver_definitions_for(name, receiver_scopes, class_method_filter = nil)
455
+ def receiver_definition_records(name, receiver_scopes, class_method_filter = nil)
445
456
  return search_project_wide(name, class_method_filter) if receiver_scopes.empty?
446
457
 
447
458
  results = receiver_scopes.flat_map { |scope| search_variables_in_scope_chain(name, scope) }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubyLanguageServer
4
- VERSION = '0.9.9'
4
+ VERSION = '0.9.10'
5
5
  end
@@ -43,8 +43,10 @@ Gem::Specification.new do |spec|
43
43
  spec.add_dependency 'json'
44
44
  spec.add_dependency 'ostruct'
45
45
  spec.add_dependency 'prism', '>=1.8.0'
46
+ spec.add_dependency 'rbs', '>=4.0' # Ruby core class signatures and docs
46
47
  spec.add_dependency 'sqlite3'
47
48
 
49
+ spec.add_development_dependency 'bundler-audit' # make audit: checks Gemfile.lock against ruby-advisory-db
48
50
  spec.add_development_dependency 'debug'
49
51
  spec.add_development_dependency 'guard' # as of ruby 4: Sorry, you can't use Pry without Readline or a compatible library.
50
52
  spec.add_development_dependency 'guard-minitest'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_language_server
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.9
4
+ version: 0.9.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kurt Werle
@@ -121,6 +121,20 @@ dependencies:
121
121
  - - ">="
122
122
  - !ruby/object:Gem::Version
123
123
  version: 1.8.0
124
+ - !ruby/object:Gem::Dependency
125
+ name: rbs
126
+ requirement: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '4.0'
131
+ type: :runtime
132
+ prerelease: false
133
+ version_requirements: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '4.0'
124
138
  - !ruby/object:Gem::Dependency
125
139
  name: sqlite3
126
140
  requirement: !ruby/object:Gem::Requirement
@@ -135,6 +149,20 @@ dependencies:
135
149
  - - ">="
136
150
  - !ruby/object:Gem::Version
137
151
  version: '0'
152
+ - !ruby/object:Gem::Dependency
153
+ name: bundler-audit
154
+ requirement: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ type: :development
160
+ prerelease: false
161
+ version_requirements: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - ">="
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
138
166
  - !ruby/object:Gem::Dependency
139
167
  name: debug
140
168
  requirement: !ruby/object:Gem::Requirement
@@ -390,6 +418,7 @@ files:
390
418
  - lib/ruby_language_server/application.rb
391
419
  - lib/ruby_language_server/code_file.rb
392
420
  - lib/ruby_language_server/completion.rb
421
+ - lib/ruby_language_server/core_index.rb
393
422
  - lib/ruby_language_server/hover.rb
394
423
  - lib/ruby_language_server/io.rb
395
424
  - lib/ruby_language_server/line_context.rb