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
|
@@ -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
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RubyLanguageServer
|
|
4
|
+
# Markdown for textDocument/hover, built from what the index already stores.
|
|
5
|
+
module Hover
|
|
6
|
+
class << self
|
|
7
|
+
# A Scope (class, module or method) or a Variable.
|
|
8
|
+
def markdown(record)
|
|
9
|
+
case record
|
|
10
|
+
when ScopeData::Variable then variable_markdown(record)
|
|
11
|
+
when ScopeData::Scope then scope_markdown(record)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
private
|
|
16
|
+
|
|
17
|
+
def scope_markdown(scope)
|
|
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
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def superclass(scope)
|
|
28
|
+
"< #{scope.superclass_name}" if scope.superclass_name.present?
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def method_markdown(scope)
|
|
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
|
+
|
|
35
|
+
parameters = scope.parsed_parameters.map { |parameter| parameter_text(parameter) }
|
|
36
|
+
signature = "def #{receiver}#{scope.name}"
|
|
37
|
+
signature += "(#{parameters.join(', ')})" if parameters.any?
|
|
38
|
+
[code_block(signature), container_line(scope.parent)].compact.join("\n\n")
|
|
39
|
+
end
|
|
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
|
+
|
|
46
|
+
# Parameters are stored as { 'name' => ..., 'type' => ... } (see PrismProcessor).
|
|
47
|
+
# Rest, keyword rest and block names already carry their *, ** or &, and keyword
|
|
48
|
+
# names their trailing colon.
|
|
49
|
+
def parameter_text(parameter)
|
|
50
|
+
parameter['type'] == 'optional' ? "#{parameter['name']} = …" : parameter['name']
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def variable_markdown(variable)
|
|
54
|
+
[code_block(variable.name), "Defined in `#{variable.scope&.path.presence || 'top level'}`, line #{variable.line}"].join("\n\n")
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def container_line(scope)
|
|
58
|
+
return nil if scope.nil? || scope.path.blank?
|
|
59
|
+
|
|
60
|
+
"In `#{scope.path}`"
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def code_block(code)
|
|
64
|
+
"```ruby\n#{code}\n```"
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
@@ -1,16 +1,63 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'json'
|
|
4
|
+
require 'socket'
|
|
4
5
|
|
|
5
6
|
module RubyLanguageServer
|
|
6
7
|
class IO
|
|
7
|
-
|
|
8
|
+
attr_reader :using_socket
|
|
9
|
+
|
|
10
|
+
def initialize(server)
|
|
8
11
|
@server = server
|
|
9
|
-
@mutex = mutex
|
|
10
12
|
server.io = self
|
|
13
|
+
|
|
14
|
+
configure_io
|
|
15
|
+
run_loop
|
|
16
|
+
return unless @using_socket
|
|
17
|
+
|
|
18
|
+
begin
|
|
19
|
+
@in&.close
|
|
20
|
+
rescue StandardError => e
|
|
21
|
+
RubyLanguageServer.logger.error "Error closing socket: #{e}"
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# JSON-RPC error codes.
|
|
26
|
+
INVALID_REQUEST = -32_600 # used for requests that arrive after shutdown
|
|
27
|
+
METHOD_NOT_FOUND = -32_601
|
|
28
|
+
INTERNAL_ERROR = -32_603 # used when a request handler raises
|
|
29
|
+
|
|
30
|
+
# The background scanner (ProjectManager#scan_all_project_files) writes to the same SQLite
|
|
31
|
+
# database. SQLite waits up to the connection's busy timeout for a lock; these cover the rare
|
|
32
|
+
# BUSY it returns without waiting.
|
|
33
|
+
BUSY_RETRIES = 2
|
|
34
|
+
BUSY_RETRY_DELAY = 0.1 # seconds
|
|
35
|
+
|
|
36
|
+
def send_notification(message, params)
|
|
37
|
+
body = write_message(jsonrpc: '2.0', method: message, params:)
|
|
38
|
+
RubyLanguageServer.logger.info "send_notification body: #{body}"
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Sends a request to the client and returns its id. The client's response is logged
|
|
42
|
+
# and otherwise ignored (see #process_request).
|
|
43
|
+
def send_request(message, params)
|
|
44
|
+
@next_request_id = (@next_request_id || 0) + 1
|
|
45
|
+
id = "ruby_language_server-#{@next_request_id}"
|
|
46
|
+
body = write_message(jsonrpc: '2.0', id:, method: message, params:)
|
|
47
|
+
RubyLanguageServer.logger.info "send_request body: #{body}"
|
|
48
|
+
id
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
private
|
|
52
|
+
|
|
53
|
+
attr_accessor :in, :out
|
|
54
|
+
|
|
55
|
+
def run_loop
|
|
11
56
|
loop do
|
|
12
|
-
(id, response) = process_request
|
|
13
|
-
return_response(id, response
|
|
57
|
+
(id, response) = process_request
|
|
58
|
+
return_response(id, response) unless id.nil?
|
|
59
|
+
rescue SystemExit
|
|
60
|
+
raise # on_exit, or a signal below. Don't treat it as an error and keep looping.
|
|
14
61
|
rescue SignalException => e
|
|
15
62
|
RubyLanguageServer.logger.error "We received a signal. Let's bail: #{e}"
|
|
16
63
|
exit
|
|
@@ -21,121 +68,158 @@ module RubyLanguageServer
|
|
|
21
68
|
end
|
|
22
69
|
end
|
|
23
70
|
|
|
24
|
-
def
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
body =
|
|
45
|
-
RubyLanguageServer.logger.info "
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
71
|
+
def configure_io
|
|
72
|
+
if ENV['LSP_PORT']
|
|
73
|
+
@tcp_server = TCPServer.new(ENV['LSP_PORT'].to_i)
|
|
74
|
+
RubyLanguageServer.logger.info "Listening on TCP port #{ENV['LSP_PORT']} for LSP connections"
|
|
75
|
+
self.in = @tcp_server.accept
|
|
76
|
+
self.out = self.in
|
|
77
|
+
@using_socket = true
|
|
78
|
+
RubyLanguageServer.logger.info 'Accepted LSP socket connection'
|
|
79
|
+
else
|
|
80
|
+
self.in = $stdin
|
|
81
|
+
# Keep the real stdout for protocol messages and send anything else written to
|
|
82
|
+
# $stdout (a stray puts, a gem warning) to stderr, so it can't corrupt the stream.
|
|
83
|
+
self.out = $stdout.dup
|
|
84
|
+
out.binmode
|
|
85
|
+
$stdout.reopen($stderr)
|
|
86
|
+
@using_socket = false
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def return_response(id, response)
|
|
91
|
+
body = write_message(jsonrpc: '2.0', id:, result: response)
|
|
92
|
+
RubyLanguageServer.logger.info "return_response body: #{body}"
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def return_error(id, code, message)
|
|
96
|
+
body = write_message(jsonrpc: '2.0', id:, error: { code:, message: })
|
|
97
|
+
RubyLanguageServer.logger.info "return_error body: #{body}"
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def log_client_response(response)
|
|
101
|
+
if response['error']
|
|
102
|
+
RubyLanguageServer.logger.warn "Client returned an error for request #{response['id']}: #{response['error']}"
|
|
103
|
+
else
|
|
104
|
+
RubyLanguageServer.logger.info "Client replied to request #{response['id']}"
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# Writes one framed LSP message and returns its JSON body.
|
|
109
|
+
# Content-Length is a byte count, not a character count.
|
|
110
|
+
def write_message(message)
|
|
111
|
+
body = JSON.generate(message)
|
|
112
|
+
out.write "Content-Length: #{body.bytesize}\r\n"
|
|
113
|
+
out.write "\r\n"
|
|
114
|
+
out.write body
|
|
115
|
+
out.flush if out.respond_to?(:flush)
|
|
116
|
+
body
|
|
50
117
|
end
|
|
51
118
|
|
|
52
|
-
def process_request
|
|
53
|
-
request_body =
|
|
119
|
+
def process_request
|
|
120
|
+
request_body = read_request
|
|
54
121
|
# RubyLanguageServer.logger.debug "request_body: #{request_body}"
|
|
55
122
|
request_json = JSON.parse request_body
|
|
56
123
|
id = request_json['id']
|
|
57
|
-
|
|
124
|
+
lsp_method = request_json['method']
|
|
58
125
|
params = request_json['params']
|
|
59
|
-
|
|
126
|
+
if lsp_method.nil?
|
|
127
|
+
# A response to a request we sent the client, such as client/registerCapability.
|
|
128
|
+
log_client_response(request_json)
|
|
129
|
+
return nil
|
|
130
|
+
end
|
|
131
|
+
method_name = "on_#{lsp_method.gsub(/[^\w]/, '_')}"
|
|
132
|
+
if !id.nil? && @server.respond_to?(:shutdown_requested?) && @server.shutdown_requested?
|
|
133
|
+
RubyLanguageServer.logger.warn "Request #{lsp_method} after shutdown"
|
|
134
|
+
return_error(id, INVALID_REQUEST, "Server is shutting down; received #{lsp_method}")
|
|
135
|
+
return nil
|
|
136
|
+
end
|
|
60
137
|
if @server.respond_to? method_name
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
retry unless retries <= 0
|
|
70
|
-
end
|
|
138
|
+
begin
|
|
139
|
+
[id, call_handler(method_name, params)]
|
|
140
|
+
rescue StandardError => e
|
|
141
|
+
RubyLanguageServer.logger.error("Error in #{lsp_method}: #{e.class}: #{e.message}\n#{e.backtrace * "\n"}")
|
|
142
|
+
# A request gets an error reply, so the client doesn't mistake it for an empty result.
|
|
143
|
+
# Notifications get none.
|
|
144
|
+
return_error(id, INTERNAL_ERROR, "#{e.class}: #{e.message}") unless id.nil?
|
|
145
|
+
nil
|
|
71
146
|
end
|
|
72
|
-
exit if response == 'EXIT'
|
|
73
|
-
[id, response]
|
|
74
147
|
else
|
|
75
148
|
RubyLanguageServer.logger.warn "SERVER DOES NOT RESPOND TO #{method_name}"
|
|
149
|
+
# Every request needs a reply, or the client waits forever. Notifications get none.
|
|
150
|
+
return_error(id, METHOD_NOT_FOUND, "Method not found: #{lsp_method}") unless id.nil?
|
|
76
151
|
nil
|
|
77
152
|
end
|
|
78
153
|
end
|
|
79
154
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
content = ''
|
|
85
|
-
while content.length < length + 2
|
|
155
|
+
# Calls the handler, retrying only when the database is locked.
|
|
156
|
+
def call_handler(method_name, params)
|
|
157
|
+
ActiveRecord::Base.connection_pool.with_connection do
|
|
158
|
+
attempts = 0
|
|
86
159
|
begin
|
|
87
|
-
|
|
88
|
-
rescue
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
160
|
+
@server.send(method_name, params)
|
|
161
|
+
rescue ActiveRecord::StatementTimeout => e
|
|
162
|
+
attempts += 1
|
|
163
|
+
raise if attempts > BUSY_RETRIES
|
|
164
|
+
|
|
165
|
+
RubyLanguageServer.logger.warn("Database busy in #{method_name}, retrying: #{e.message}")
|
|
166
|
+
sleep busy_retry_delay
|
|
167
|
+
retry
|
|
92
168
|
end
|
|
93
169
|
end
|
|
94
|
-
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
def busy_retry_delay
|
|
173
|
+
BUSY_RETRY_DELAY
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
# Reads one framed message: header lines up to the blank line that ends them, then
|
|
177
|
+
# exactly Content-Length bytes of body. Exits if the client has disconnected.
|
|
178
|
+
def read_request
|
|
179
|
+
length = read_content_length
|
|
180
|
+
content = read_content(length)
|
|
181
|
+
if content.nil? || content.bytesize < length
|
|
182
|
+
RubyLanguageServer.logger.error("Expected #{length} bytes, got #{content&.bytesize.to_i}; client disconnected")
|
|
183
|
+
exit!(1)
|
|
184
|
+
end
|
|
185
|
+
RubyLanguageServer.logger.debug "content.bytesize: #{content.bytesize}"
|
|
95
186
|
content
|
|
96
187
|
end
|
|
97
188
|
|
|
98
|
-
|
|
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
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
# puts ex.backtrace
|
|
134
|
-
# ensure
|
|
135
|
-
# # restore previous state of stty
|
|
136
|
-
# system "stty #{old_state}"
|
|
137
|
-
# end
|
|
138
|
-
# return c
|
|
139
|
-
# end
|
|
189
|
+
# Reads the header block and returns its Content-Length. Other headers, such as
|
|
190
|
+
# Content-Type, are skipped. Header names are case-insensitive.
|
|
191
|
+
def read_content_length
|
|
192
|
+
length = nil
|
|
193
|
+
headers_seen = false
|
|
194
|
+
loop do
|
|
195
|
+
line = read_header_line
|
|
196
|
+
if line.nil?
|
|
197
|
+
RubyLanguageServer.logger.error('End of input while reading headers; client disconnected')
|
|
198
|
+
exit!(1)
|
|
199
|
+
end
|
|
200
|
+
line = line.chomp
|
|
201
|
+
RubyLanguageServer.logger.debug "header: #{line}"
|
|
202
|
+
# A blank line ends the headers. Tolerate stray blank lines before the first header.
|
|
203
|
+
if line.empty?
|
|
204
|
+
break if headers_seen
|
|
205
|
+
|
|
206
|
+
next
|
|
207
|
+
end
|
|
208
|
+
headers_seen = true
|
|
209
|
+
name, value = line.split(':', 2)
|
|
210
|
+
length = Integer(value.strip, 10) if value && name.strip.casecmp?('Content-Length')
|
|
211
|
+
end
|
|
212
|
+
raise ArgumentError, 'Message has no Content-Length header' if length.nil?
|
|
213
|
+
|
|
214
|
+
length
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
def read_header_line
|
|
218
|
+
self.in.gets
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
def read_content(size)
|
|
222
|
+
self.in.read(size)
|
|
223
|
+
end
|
|
140
224
|
end # class
|
|
141
225
|
end # module
|
|
@@ -17,6 +17,20 @@
|
|
|
17
17
|
|
|
18
18
|
module RubyLanguageServer
|
|
19
19
|
module LineContext
|
|
20
|
+
# LSP Position.character counts UTF-16 code units - NOT UTF-8.
|
|
21
|
+
# Returns the matching index into the
|
|
22
|
+
# Ruby string, which counts characters. They differ for characters outside the BMP,
|
|
23
|
+
# such as emoji, which take two code units.
|
|
24
|
+
def self.character_index(line, utf16_offset)
|
|
25
|
+
units = 0
|
|
26
|
+
line.each_char.with_index do |char, index|
|
|
27
|
+
return index if units >= utf16_offset
|
|
28
|
+
|
|
29
|
+
units += char.ord > 0xFFFF ? 2 : 1
|
|
30
|
+
end
|
|
31
|
+
line.length
|
|
32
|
+
end
|
|
33
|
+
|
|
20
34
|
# This will not work if the search starts on the ending ? of "method?". Bummer.
|
|
21
35
|
def self.for(line, position)
|
|
22
36
|
# Grab just the last part of the line - from the index onward
|