ruby-lsp 0.4.2 → 0.4.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +29 -35
  3. data/VERSION +1 -1
  4. data/lib/rubocop/cop/ruby_lsp/use_language_server_aliases.rb +62 -0
  5. data/lib/ruby_lsp/document.rb +74 -6
  6. data/lib/ruby_lsp/event_emitter.rb +52 -0
  7. data/lib/ruby_lsp/executor.rb +60 -14
  8. data/lib/ruby_lsp/internal.rb +2 -0
  9. data/lib/ruby_lsp/listener.rb +39 -0
  10. data/lib/ruby_lsp/requests/base_request.rb +2 -85
  11. data/lib/ruby_lsp/requests/code_action_resolve.rb +46 -19
  12. data/lib/ruby_lsp/requests/code_actions.rb +5 -4
  13. data/lib/ruby_lsp/requests/code_lens.rb +135 -0
  14. data/lib/ruby_lsp/requests/diagnostics.rb +3 -3
  15. data/lib/ruby_lsp/requests/document_highlight.rb +7 -9
  16. data/lib/ruby_lsp/requests/document_link.rb +7 -7
  17. data/lib/ruby_lsp/requests/document_symbol.rb +13 -10
  18. data/lib/ruby_lsp/requests/folding_ranges.rb +13 -9
  19. data/lib/ruby_lsp/requests/formatting.rb +5 -4
  20. data/lib/ruby_lsp/requests/hover.rb +28 -32
  21. data/lib/ruby_lsp/requests/inlay_hints.rb +5 -4
  22. data/lib/ruby_lsp/requests/path_completion.rb +16 -10
  23. data/lib/ruby_lsp/requests/selection_ranges.rb +3 -3
  24. data/lib/ruby_lsp/requests/semantic_highlighting.rb +29 -15
  25. data/lib/ruby_lsp/requests/support/common.rb +55 -0
  26. data/lib/ruby_lsp/requests/support/highlight_target.rb +5 -4
  27. data/lib/ruby_lsp/requests/support/rails_document_client.rb +7 -6
  28. data/lib/ruby_lsp/requests/support/selection_range.rb +1 -1
  29. data/lib/ruby_lsp/requests/support/semantic_token_encoder.rb +2 -2
  30. data/lib/ruby_lsp/requests/support/sorbet.rb +5 -15
  31. data/lib/ruby_lsp/requests/support/syntax_tree_formatting_runner.rb +39 -0
  32. data/lib/ruby_lsp/requests.rb +3 -0
  33. data/lib/ruby_lsp/server.rb +4 -1
  34. data/lib/ruby_lsp/store.rb +10 -10
  35. metadata +11 -5
@@ -17,8 +17,11 @@ module RubyLsp
17
17
  # before_save :do_something # when hovering on before_save, the link will be rendered
18
18
  # end
19
19
  # ```
20
- class Hover < BaseRequest
20
+ class Hover < Listener
21
21
  extend T::Sig
22
+ extend T::Generic
23
+
24
+ ResponseType = type_member { { fixed: T.nilable(Interface::Hover) } }
22
25
 
23
26
  ALLOWED_TARGETS = T.let(
24
27
  [
@@ -29,52 +32,45 @@ module RubyLsp
29
32
  T::Array[T.class_of(SyntaxTree::Node)],
30
33
  )
31
34
 
32
- sig { params(document: Document, position: Document::PositionShape).void }
33
- def initialize(document, position)
34
- super(document)
35
+ sig { override.returns(ResponseType) }
36
+ attr_reader :response
35
37
 
36
- @position = T.let(document.create_scanner.find_char_position(position), Integer)
38
+ sig { void }
39
+ def initialize
40
+ @response = T.let(nil, ResponseType)
41
+ super()
37
42
  end
38
43
 
39
- sig { override.returns(T.nilable(LanguageServer::Protocol::Interface::Hover)) }
40
- def run
41
- return unless @document.parsed?
44
+ listener_events do
45
+ sig { params(node: SyntaxTree::Command).void }
46
+ def on_command(node)
47
+ message = node.message
48
+ @response = generate_rails_document_link_hover(message.value, message)
49
+ end
42
50
 
43
- target, parent = locate(T.must(@document.tree), @position)
44
- target = parent if !ALLOWED_TARGETS.include?(target.class) && ALLOWED_TARGETS.include?(parent.class)
51
+ sig { params(node: SyntaxTree::ConstPathRef).void }
52
+ def on_const_path_ref(node)
53
+ @response = generate_rails_document_link_hover(full_constant_name(node), node)
54
+ end
45
55
 
46
- case target
47
- when SyntaxTree::Command
48
- message = target.message
49
- generate_rails_document_link_hover(message.value, message)
50
- when SyntaxTree::CallNode
51
- return if target.message == :call
56
+ sig { params(node: SyntaxTree::CallNode).void }
57
+ def on_call(node)
58
+ message = node.message
59
+ return if message.is_a?(Symbol)
52
60
 
53
- generate_rails_document_link_hover(target.message.value, target.message)
54
- when SyntaxTree::ConstPathRef
55
- constant_name = full_constant_name(target)
56
- generate_rails_document_link_hover(constant_name, target)
61
+ @response = generate_rails_document_link_hover(message.value, message)
57
62
  end
58
63
  end
59
64
 
60
65
  private
61
66
 
62
- sig do
63
- params(name: String, node: SyntaxTree::Node).returns(T.nilable(LanguageServer::Protocol::Interface::Hover))
64
- end
67
+ sig { params(name: String, node: SyntaxTree::Node).returns(T.nilable(Interface::Hover)) }
65
68
  def generate_rails_document_link_hover(name, node)
66
69
  urls = Support::RailsDocumentClient.generate_rails_document_urls(name)
67
-
68
70
  return if urls.empty?
69
71
 
70
- contents = LanguageServer::Protocol::Interface::MarkupContent.new(
71
- kind: "markdown",
72
- value: urls.join("\n\n"),
73
- )
74
- LanguageServer::Protocol::Interface::Hover.new(
75
- range: range_from_syntax_tree_node(node),
76
- contents: contents,
77
- )
72
+ contents = Interface::MarkupContent.new(kind: "markdown", value: urls.join("\n\n"))
73
+ Interface::Hover.new(range: range_from_syntax_tree_node(node), contents: contents)
78
74
  end
79
75
  end
80
76
  end
@@ -25,11 +25,11 @@ module RubyLsp
25
25
  def initialize(document, range)
26
26
  super(document)
27
27
 
28
- @hints = T.let([], T::Array[LanguageServer::Protocol::Interface::InlayHint])
28
+ @hints = T.let([], T::Array[Interface::InlayHint])
29
29
  @range = range
30
30
  end
31
31
 
32
- sig { override.returns(T.all(T::Array[LanguageServer::Protocol::Interface::InlayHint], Object)) }
32
+ sig { override.returns(T.all(T::Array[Interface::InlayHint], Object)) }
33
33
  def run
34
34
  visit(@document.tree) if @document.parsed?
35
35
  @hints
@@ -37,12 +37,13 @@ module RubyLsp
37
37
 
38
38
  sig { override.params(node: SyntaxTree::Rescue).void }
39
39
  def visit_rescue(node)
40
- return unless node.exception.nil?
40
+ exception = node.exception
41
+ return unless exception.nil? || exception.exceptions.nil?
41
42
 
42
43
  loc = node.location
43
44
  return unless visible?(node, @range)
44
45
 
45
- @hints << LanguageServer::Protocol::Interface::InlayHint.new(
46
+ @hints << Interface::InlayHint.new(
46
47
  position: { line: loc.start_line - 1, character: loc.start_column + RESCUE_STRING_LENGTH },
47
48
  label: "StandardError",
48
49
  padding_left: true,
@@ -29,8 +29,7 @@ module RubyLsp
29
29
  # We can't verify if we're inside a require when there are syntax errors
30
30
  return [] if @document.syntax_error?
31
31
 
32
- char_position = @document.create_scanner.find_char_position(@position)
33
- target = T.let(find(char_position), T.nilable(SyntaxTree::TStringContent))
32
+ target = T.let(find, T.nilable(SyntaxTree::TStringContent))
34
33
  # no target means the we are not inside a `require` call
35
34
  return [] unless target
36
35
 
@@ -51,11 +50,12 @@ module RubyLsp
51
50
  end
52
51
  end
53
52
 
54
- sig { params(position: Integer).returns(T.nilable(SyntaxTree::TStringContent)) }
55
- def find(position)
56
- matched, parent = locate(
53
+ sig { returns(T.nilable(SyntaxTree::TStringContent)) }
54
+ def find
55
+ char_position = @document.create_scanner.find_char_position(@position)
56
+ matched, parent = @document.locate(
57
57
  T.must(@document.tree),
58
- position,
58
+ char_position,
59
59
  node_types: [SyntaxTree::Command, SyntaxTree::CommandCall, SyntaxTree::CallNode],
60
60
  )
61
61
 
@@ -63,14 +63,20 @@ module RubyLsp
63
63
 
64
64
  case matched
65
65
  when SyntaxTree::Command, SyntaxTree::CallNode, SyntaxTree::CommandCall
66
- return unless matched.message.value == "require"
66
+ message = matched.message
67
+ return if message.is_a?(Symbol)
68
+ return unless message.value == "require"
67
69
 
68
70
  args = matched.arguments
69
71
  args = args.arguments if args.is_a?(SyntaxTree::ArgParen)
72
+ return if args.nil? || args.is_a?(SyntaxTree::ArgsForward)
73
+
74
+ argument = args.parts.first
75
+ return unless argument.is_a?(SyntaxTree::StringLiteral)
70
76
 
71
- path_node = args.parts.first.parts.first
72
- return unless path_node
73
- return unless (path_node.location.start_char..path_node.location.end_char).cover?(position)
77
+ path_node = argument.parts.first
78
+ return unless path_node.is_a?(SyntaxTree::TStringContent)
79
+ return unless (path_node.location.start_char..path_node.location.end_char).cover?(char_position)
74
80
 
75
81
  path_node
76
82
  end
@@ -100,12 +100,12 @@ module RubyLsp
100
100
  end
101
101
  def create_selection_range(location, parent = nil)
102
102
  RubyLsp::Requests::Support::SelectionRange.new(
103
- range: LanguageServer::Protocol::Interface::Range.new(
104
- start: LanguageServer::Protocol::Interface::Position.new(
103
+ range: Interface::Range.new(
104
+ start: Interface::Position.new(
105
105
  line: location.start_line - 1,
106
106
  character: location.start_column,
107
107
  ),
108
- end: LanguageServer::Protocol::Interface::Position.new(
108
+ end: Interface::Position.new(
109
109
  line: location.end_line - 1,
110
110
  character: location.end_column,
111
111
  ),
@@ -122,7 +122,7 @@ module RubyLsp
122
122
  sig do
123
123
  override.returns(
124
124
  T.any(
125
- LanguageServer::Protocol::Interface::SemanticTokens,
125
+ Interface::SemanticTokens,
126
126
  T.all(T::Array[SemanticToken], Object),
127
127
  ),
128
128
  )
@@ -143,7 +143,7 @@ module RubyLsp
143
143
  visit(node.receiver)
144
144
 
145
145
  message = node.message
146
- if message != :call && !special_method?(message.value)
146
+ if !message.is_a?(Symbol) && !special_method?(message.value)
147
147
  type = Support::Sorbet.annotation?(node) ? :type : :method
148
148
 
149
149
  add_token(message.location, type)
@@ -168,7 +168,8 @@ module RubyLsp
168
168
  return super unless visible?(node, @range)
169
169
 
170
170
  visit(node.receiver)
171
- add_token(node.message.location, :method)
171
+ message = node.message
172
+ add_token(message.location, :method) unless message.is_a?(Symbol)
172
173
  visit(node.arguments)
173
174
  visit(node.block)
174
175
  end
@@ -205,7 +206,7 @@ module RubyLsp
205
206
  def visit_params(node)
206
207
  return super unless visible?(node, @range)
207
208
 
208
- node.keywords.each do |keyword,|
209
+ node.keywords.each do |keyword, *|
209
210
  location = keyword.location
210
211
  add_token(location_without_colon(location), :parameter)
211
212
  end
@@ -215,7 +216,7 @@ module RubyLsp
215
216
  end
216
217
 
217
218
  rest = node.keyword_rest
218
- if rest && !rest.is_a?(SyntaxTree::ArgsForward)
219
+ if rest && !rest.is_a?(SyntaxTree::ArgsForward) && !rest.is_a?(Symbol)
219
220
  name = rest.name
220
221
  add_token(name.location, :parameter) if name
221
222
  end
@@ -238,9 +239,14 @@ module RubyLsp
238
239
 
239
240
  value = node.value
240
241
 
241
- if value.is_a?(SyntaxTree::Ident)
242
+ case value
243
+ when SyntaxTree::Ident
242
244
  type = type_for_local(value)
243
245
  add_token(value.location, type)
246
+ when Symbol
247
+ # do nothing
248
+ else
249
+ visit(value)
244
250
  end
245
251
 
246
252
  super
@@ -256,6 +262,8 @@ module RubyLsp
256
262
  when SyntaxTree::Ident
257
263
  type = type_for_local(value)
258
264
  add_token(value.location, type)
265
+ when Symbol
266
+ # do nothing
259
267
  else
260
268
  visit(value)
261
269
  end
@@ -305,18 +313,21 @@ module RubyLsp
305
313
  return unless node.operator == :=~
306
314
 
307
315
  left = node.left
316
+ # The regexp needs to be on the left hand side of the =~ for local variable capture
317
+ return unless left.is_a?(SyntaxTree::RegexpLiteral)
318
+
308
319
  parts = left.parts
320
+ return unless parts.one?
309
321
 
310
- if left.is_a?(SyntaxTree::RegexpLiteral) && parts.one? && parts.first.is_a?(SyntaxTree::TStringContent)
311
- content = parts.first
322
+ content = parts.first
323
+ return unless content.is_a?(SyntaxTree::TStringContent)
312
324
 
313
- # For each capture name we find in the regexp, look for a local in the current_scope
314
- Regexp.new(content.value, Regexp::FIXEDENCODING).names.each do |name|
315
- local = current_scope.find_local(name)
316
- next unless local
325
+ # For each capture name we find in the regexp, look for a local in the current_scope
326
+ Regexp.new(content.value, Regexp::FIXEDENCODING).names.each do |name|
327
+ local = current_scope.find_local(name)
328
+ next unless local
317
329
 
318
- local.definitions.each { |definition| add_token(definition, :variable) }
319
- end
330
+ local.definitions.each { |definition| add_token(definition, :variable) }
320
331
  end
321
332
  end
322
333
 
@@ -325,7 +336,10 @@ module RubyLsp
325
336
  return super unless visible?(node, @range)
326
337
 
327
338
  add_token(node.constant.location, :class, [:declaration])
328
- add_token(node.superclass.location, :class) if node.superclass
339
+
340
+ superclass = node.superclass
341
+ add_token(superclass.location, :class) if superclass
342
+
329
343
  visit(node.bodystmt)
330
344
  end
331
345
 
@@ -0,0 +1,55 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module RubyLsp
5
+ module Requests
6
+ module Support
7
+ module Common
8
+ extend T::Sig
9
+
10
+ sig { params(node: SyntaxTree::Node).returns(Interface::Range) }
11
+ def range_from_syntax_tree_node(node)
12
+ loc = node.location
13
+
14
+ Interface::Range.new(
15
+ start: Interface::Position.new(
16
+ line: loc.start_line - 1,
17
+ character: loc.start_column,
18
+ ),
19
+ end: Interface::Position.new(line: loc.end_line - 1, character: loc.end_column),
20
+ )
21
+ end
22
+
23
+ sig do
24
+ params(node: T.any(SyntaxTree::ConstPathRef, SyntaxTree::ConstRef, SyntaxTree::TopConstRef)).returns(String)
25
+ end
26
+ def full_constant_name(node)
27
+ name = +node.constant.value
28
+ constant = T.let(node, SyntaxTree::Node)
29
+
30
+ while constant.is_a?(SyntaxTree::ConstPathRef)
31
+ constant = constant.parent
32
+
33
+ case constant
34
+ when SyntaxTree::ConstPathRef
35
+ name.prepend("#{constant.constant.value}::")
36
+ when SyntaxTree::VarRef
37
+ name.prepend("#{constant.value.value}::")
38
+ end
39
+ end
40
+
41
+ name
42
+ end
43
+
44
+ sig { params(node: T.nilable(SyntaxTree::Node), range: T.nilable(T::Range[Integer])).returns(T::Boolean) }
45
+ def visible?(node, range)
46
+ return true if range.nil?
47
+ return false if node.nil?
48
+
49
+ loc = node.location
50
+ range.cover?(loc.start_line - 1) && range.cover?(loc.end_line - 1)
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -7,8 +7,8 @@ module RubyLsp
7
7
  class HighlightTarget
8
8
  extend T::Sig
9
9
 
10
- READ = LanguageServer::Protocol::Constant::DocumentHighlightKind::READ
11
- WRITE = LanguageServer::Protocol::Constant::DocumentHighlightKind::WRITE
10
+ READ = Constant::DocumentHighlightKind::READ
11
+ WRITE = Constant::DocumentHighlightKind::WRITE
12
12
 
13
13
  class HighlightMatch
14
14
  extend T::Sig
@@ -84,10 +84,11 @@ module RubyLsp
84
84
  SyntaxTree::KwRestParam, SyntaxTree::BlockArg
85
85
  node.name&.value
86
86
  when SyntaxTree::VarField, SyntaxTree::VarRef, SyntaxTree::VCall
87
- node.value&.value
87
+ value = node.value
88
+ value.value unless value.nil? || value.is_a?(Symbol)
88
89
  when SyntaxTree::CallNode, SyntaxTree::Command, SyntaxTree::CommandCall
89
90
  message = node.message
90
- message != :call ? message.value : nil
91
+ message.value unless message.is_a?(Symbol)
91
92
  when SyntaxTree::ClassDeclaration, SyntaxTree::ModuleDeclaration
92
93
  node.constant.constant.value
93
94
  end
@@ -67,12 +67,13 @@ module RubyLsp
67
67
  return unless RAILTIES_VERSION
68
68
 
69
69
  warn("Fetching Rails Documents...")
70
- # If the version's doc is not found, e.g. Rails main, it'll be redirected
71
- # In this case, we just fetch the latest doc
72
- response = if Gem::Version.new(RAILTIES_VERSION).prerelease?
73
- Net::HTTP.get_response(URI("#{RAILS_DOC_HOST}/js/search_index.js"))
74
- else
75
- Net::HTTP.get_response(URI("#{RAILS_DOC_HOST}/v#{RAILTIES_VERSION}/js/search_index.js"))
70
+
71
+ response = Net::HTTP.get_response(URI("#{RAILS_DOC_HOST}/v#{RAILTIES_VERSION}/js/search_index.js"))
72
+
73
+ if response.code == "302"
74
+ # If the version's doc is not found, e.g. Rails main, it'll be redirected
75
+ # In this case, we just fetch the latest doc
76
+ response = Net::HTTP.get_response(URI("#{RAILS_DOC_HOST}/js/search_index.js"))
76
77
  end
77
78
 
78
79
  if response.code == "200"
@@ -4,7 +4,7 @@
4
4
  module RubyLsp
5
5
  module Requests
6
6
  module Support
7
- class SelectionRange < LanguageServer::Protocol::Interface::SelectionRange
7
+ class SelectionRange < Interface::SelectionRange
8
8
  extend T::Sig
9
9
 
10
10
  sig { params(position: Document::PositionShape).returns(T::Boolean) }
@@ -16,7 +16,7 @@ module RubyLsp
16
16
  sig do
17
17
  params(
18
18
  tokens: T::Array[SemanticHighlighting::SemanticToken],
19
- ).returns(LanguageServer::Protocol::Interface::SemanticTokens)
19
+ ).returns(Interface::SemanticTokens)
20
20
  end
21
21
  def encode(tokens)
22
22
  delta = tokens
@@ -27,7 +27,7 @@ module RubyLsp
27
27
  compute_delta(token)
28
28
  end
29
29
 
30
- LanguageServer::Protocol::Interface::SemanticTokens.new(data: delta)
30
+ Interface::SemanticTokens.new(data: delta)
31
31
  end
32
32
 
33
33
  # The delta array is computed according to the LSP specification:
@@ -63,7 +63,8 @@ module RubyLsp
63
63
  when SyntaxTree::VCall
64
64
  ANNOTATIONS[node.value.value]
65
65
  when SyntaxTree::CallNode
66
- ANNOTATIONS[node.message.value]
66
+ message = node.message
67
+ ANNOTATIONS[message.value] unless message.is_a?(Symbol)
67
68
  else
68
69
  T.absurd(node)
69
70
  end
@@ -84,21 +85,10 @@ module RubyLsp
84
85
  end
85
86
 
86
87
  sig do
87
- params(node: T.nilable(T.any(
88
- SyntaxTree::VarRef,
89
- SyntaxTree::CallNode,
90
- SyntaxTree::VCall,
91
- SyntaxTree::Ident,
92
- SyntaxTree::Backtick,
93
- SyntaxTree::Const,
94
- SyntaxTree::Op,
95
- Symbol,
96
- ))).returns(T.nilable(String))
88
+ params(node: T.nilable(SyntaxTree::Node)).returns(T.nilable(String))
97
89
  end
98
90
  def node_name(node)
99
91
  case node
100
- when NilClass
101
- nil
102
92
  when SyntaxTree::VarRef
103
93
  node.value.value
104
94
  when SyntaxTree::CallNode
@@ -107,8 +97,8 @@ module RubyLsp
107
97
  node_name(node.value)
108
98
  when SyntaxTree::Ident, SyntaxTree::Backtick, SyntaxTree::Const, SyntaxTree::Op
109
99
  node.value
110
- when Symbol
111
- node.to_s
100
+ when NilClass, SyntaxTree::Node
101
+ nil
112
102
  else
113
103
  T.absurd(node)
114
104
  end
@@ -0,0 +1,39 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require "syntax_tree/cli"
5
+ require "singleton"
6
+
7
+ module RubyLsp
8
+ module Requests
9
+ module Support
10
+ # :nodoc:
11
+ class SyntaxTreeFormattingRunner
12
+ extend T::Sig
13
+ include Singleton
14
+
15
+ sig { void }
16
+ def initialize
17
+ @options =
18
+ T.let(
19
+ begin
20
+ opts = SyntaxTree::CLI::Options.new
21
+ opts.parse(SyntaxTree::CLI::ConfigFile.new.arguments)
22
+ opts
23
+ end,
24
+ SyntaxTree::CLI::Options,
25
+ )
26
+ end
27
+
28
+ sig { params(_uri: String, document: Document).returns(T.nilable(String)) }
29
+ def run(_uri, document)
30
+ SyntaxTree.format(
31
+ document.source,
32
+ @options.print_width,
33
+ options: @options.formatter_options,
34
+ )
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -18,6 +18,7 @@ module RubyLsp
18
18
  # - {RubyLsp::Requests::DocumentHighlight}
19
19
  # - {RubyLsp::Requests::InlayHints}
20
20
  # - {RubyLsp::Requests::PathCompletion}
21
+ # - {RubyLsp::Requests::CodeLens}
21
22
 
22
23
  module Requests
23
24
  autoload :BaseRequest, "ruby_lsp/requests/base_request"
@@ -35,6 +36,7 @@ module RubyLsp
35
36
  autoload :DocumentHighlight, "ruby_lsp/requests/document_highlight"
36
37
  autoload :InlayHints, "ruby_lsp/requests/inlay_hints"
37
38
  autoload :PathCompletion, "ruby_lsp/requests/path_completion"
39
+ autoload :CodeLens, "ruby_lsp/requests/code_lens"
38
40
 
39
41
  # :nodoc:
40
42
  module Support
@@ -46,6 +48,7 @@ module RubyLsp
46
48
  autoload :HighlightTarget, "ruby_lsp/requests/support/highlight_target"
47
49
  autoload :RailsDocumentClient, "ruby_lsp/requests/support/rails_document_client"
48
50
  autoload :PrefixTree, "ruby_lsp/requests/support/prefix_tree"
51
+ autoload :Common, "ruby_lsp/requests/support/common"
49
52
  end
50
53
  end
51
54
  end
@@ -2,9 +2,11 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module RubyLsp
5
+ # rubocop:disable RubyLsp/UseLanguageServerAliases
5
6
  Interface = LanguageServer::Protocol::Interface
6
7
  Constant = LanguageServer::Protocol::Constant
7
8
  Transport = LanguageServer::Protocol::Transport
9
+ # rubocop:enable RubyLsp/UseLanguageServerAliases
8
10
 
9
11
  class Server
10
12
  extend T::Sig
@@ -33,7 +35,8 @@ module RubyLsp
33
35
  # fall under the else branch which just pushes requests to the queue
34
36
  @reader.read do |request|
35
37
  case request[:method]
36
- when "initialize", "initialized", "textDocument/didOpen", "textDocument/didClose", "textDocument/didChange"
38
+ when "initialize", "initialized", "textDocument/didOpen", "textDocument/didClose", "textDocument/didChange",
39
+ "textDocument/formatting", "textDocument/onTypeFormatting", "codeAction/resolve"
37
40
  result = Executor.new(@store).execute(request)
38
41
  finalize_request(result, request)
39
42
  when "$/cancelRequest"
@@ -9,8 +9,8 @@ module RubyLsp
9
9
  class Store
10
10
  extend T::Sig
11
11
 
12
- sig { params(encoding: String).void }
13
- attr_writer :encoding
12
+ sig { returns(String) }
13
+ attr_accessor :encoding
14
14
 
15
15
  sig { returns(String) }
16
16
  attr_accessor :formatter
@@ -18,7 +18,7 @@ module RubyLsp
18
18
  sig { void }
19
19
  def initialize
20
20
  @state = T.let({}, T::Hash[String, Document])
21
- @encoding = T.let("utf-8", String)
21
+ @encoding = T.let(Constant::PositionEncodingKind::UTF8, String)
22
22
  @formatter = T.let("auto", String)
23
23
  end
24
24
 
@@ -27,19 +27,19 @@ module RubyLsp
27
27
  document = @state[uri]
28
28
  return document unless document.nil?
29
29
 
30
- set(uri, File.binread(CGI.unescape(URI.parse(uri).path)))
30
+ set(uri: uri, source: File.binread(CGI.unescape(URI.parse(uri).path)), version: 0)
31
31
  T.must(@state[uri])
32
32
  end
33
33
 
34
- sig { params(uri: String, content: String).void }
35
- def set(uri, content)
36
- document = Document.new(content, @encoding)
34
+ sig { params(uri: String, source: String, version: Integer).void }
35
+ def set(uri:, source:, version:)
36
+ document = Document.new(source: source, version: version, uri: uri, encoding: @encoding)
37
37
  @state[uri] = document
38
38
  end
39
39
 
40
- sig { params(uri: String, edits: T::Array[Document::EditShape]).void }
41
- def push_edits(uri, edits)
42
- T.must(@state[uri]).push_edits(edits)
40
+ sig { params(uri: String, edits: T::Array[Document::EditShape], version: Integer).void }
41
+ def push_edits(uri:, edits:, version:)
42
+ T.must(@state[uri]).push_edits(edits, version: version)
43
43
  end
44
44
 
45
45
  sig { void }