forked from Shopify/ruby-lsp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatting.rb
78 lines (67 loc) · 2.45 KB
/
formatting.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
# typed: strict
# frozen_string_literal: true
module RubyLsp
module Requests
# 
#
# The [formatting](https://microsoft.github.io/language-server-protocol/specification#textDocument_formatting)
# request uses RuboCop to fix auto-correctable offenses in the document. This requires enabling format on save and
# registering the ruby-lsp as the Ruby formatter.
#
# The `rubyLsp.formatter` setting specifies which formatter to use.
# If set to `auto` then it behaves as follows:
# * It will use RuboCop if it is part of the bundle.
# * If RuboCop is not available, and `syntax_tree` is a direct dependency, it will use that.
# * Otherwise, no formatting will be applied.
#
# # Example
#
# ```ruby
# def say_hello
# puts "Hello" # --> formatting: fixes the indentation on save
# end
# ```
class Formatting < Request
extend T::Sig
class Error < StandardError; end
class << self
extend T::Sig
sig { returns(Interface::DocumentFormattingRegistrationOptions) }
def provider
Interface::DocumentFormattingRegistrationOptions.new(
document_selector: [
Interface::DocumentFilter.new(language: "ruby"),
],
)
end
end
sig { params(global_state: GlobalState, document: Document).void }
def initialize(global_state, document)
super()
@document = document
@active_formatter = T.let(global_state.active_formatter, T.nilable(Support::Formatter))
@uri = T.let(document.uri, URI::Generic)
end
sig { override.returns(T.nilable(T.all(T::Array[Interface::TextEdit], Object))) }
def perform
return unless @active_formatter
return if @document.syntax_error?
# We don't format erb documents yet
formatted_text = @active_formatter.run_formatting(@uri, @document)
return unless formatted_text
lines = @document.source.lines
size = @document.source.size
return if formatted_text.size == size && formatted_text == @document.source
[
Interface::TextEdit.new(
range: Interface::Range.new(
start: Interface::Position.new(line: 0, character: 0),
end: Interface::Position.new(line: lines.size, character: 0),
),
new_text: formatted_text,
),
]
end
end
end
end