-
Notifications
You must be signed in to change notification settings - Fork 183
/
Copy pathcode_actions.rb
84 lines (75 loc) · 2.61 KB
/
code_actions.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# typed: strict
# frozen_string_literal: true
module RubyLsp
module Requests
# 
#
# The [code actions](https://microsoft.github.io/language-server-protocol/specification#textDocument_codeAction)
# request informs the editor of RuboCop quick fixes that can be applied. These are accessible by hovering over a
# specific diagnostic.
#
# # Example
#
# ```ruby
# def say_hello
# puts "Hello" # --> code action: quick fix indentation
# end
# ```
class CodeActions < Request
extend T::Sig
EXTRACT_TO_VARIABLE_TITLE = "Refactor: Extract Variable"
EXTRACT_TO_METHOD_TITLE = "Refactor: Extract Method"
TOGGLE_BLOCK_STYLE_TITLE = "Refactor: Toggle block style"
class << self
extend T::Sig
sig { returns(Interface::CodeActionRegistrationOptions) }
def provider
Interface::CodeActionRegistrationOptions.new(
document_selector: [Interface::DocumentFilter.new(language: "ruby")],
resolve_provider: true,
)
end
end
sig do
params(
document: T.any(RubyDocument, ERBDocument),
range: T::Hash[Symbol, T.untyped],
context: T::Hash[Symbol, T.untyped],
).void
end
def initialize(document, range, context)
super()
@document = document
@uri = T.let(document.uri, URI::Generic)
@range = range
@context = context
end
sig { override.returns(T.nilable(T.all(T::Array[Interface::CodeAction], Object))) }
def perform
diagnostics = @context[:diagnostics]
code_actions = diagnostics.flat_map do |diagnostic|
diagnostic.dig(:data, :code_actions) || []
end
# Only add refactor actions if there's a non empty selection in the editor
unless @range.dig(:start) == @range.dig(:end)
code_actions << Interface::CodeAction.new(
title: EXTRACT_TO_VARIABLE_TITLE,
kind: Constant::CodeActionKind::REFACTOR_EXTRACT,
data: { range: @range, uri: @uri.to_s },
)
code_actions << Interface::CodeAction.new(
title: EXTRACT_TO_METHOD_TITLE,
kind: Constant::CodeActionKind::REFACTOR_EXTRACT,
data: { range: @range, uri: @uri.to_s },
)
code_actions << Interface::CodeAction.new(
title: TOGGLE_BLOCK_STYLE_TITLE,
kind: Constant::CodeActionKind::REFACTOR_REWRITE,
data: { range: @range, uri: @uri.to_s },
)
end
code_actions
end
end
end
end