-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for range formatting with Syntax Tree
- Loading branch information
Showing
11 changed files
with
178 additions
and
14 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
module RubyLsp | ||
module Requests | ||
# The [range formatting](https://microsoft.github.io/language-server-protocol/specification#textDocument_rangeFormatting) | ||
# is used to format a selection or to format on paste. | ||
class RangeFormatting < Request | ||
extend T::Sig | ||
|
||
sig { params(global_state: GlobalState, document: RubyDocument, params: T::Hash[Symbol, T.untyped]).void } | ||
def initialize(global_state, document, params) | ||
super() | ||
@document = document | ||
@uri = T.let(document.uri, URI::Generic) | ||
@params = params | ||
@active_formatter = T.let(global_state.active_formatter, T.nilable(Support::Formatter)) | ||
end | ||
|
||
sig { override.returns(T.nilable(T::Array[Interface::TextEdit])) } | ||
def perform | ||
return unless @active_formatter | ||
return if @document.syntax_error? | ||
|
||
target = @document.locate_first_within_range(@params[:range]) | ||
return unless target | ||
|
||
location = target.location | ||
|
||
formatted_text = @active_formatter.run_range_formatting( | ||
@uri, | ||
target.slice, | ||
location.start_column / 2, | ||
) | ||
return unless formatted_text | ||
|
||
[ | ||
Interface::TextEdit.new( | ||
range: Interface::Range.new( | ||
start: Interface::Position.new( | ||
line: location.start_line - 1, | ||
character: location.start_code_units_column(@document.encoding), | ||
), | ||
end: Interface::Position.new( | ||
line: location.end_line - 1, | ||
character: location.end_code_units_column(@document.encoding), | ||
), | ||
), | ||
new_text: formatted_text.strip, | ||
), | ||
] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# typed: true | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class RangeFormattingTest < Minitest::Test | ||
def setup | ||
@global_state = RubyLsp::GlobalState.new | ||
@global_state.formatter = "syntax_tree" | ||
regular_formatter = RubyLsp::Requests::Support::SyntaxTreeFormatter.new | ||
@global_state.register_formatter("syntax_tree", regular_formatter) | ||
@global_state.stubs(:active_formatter).returns(regular_formatter) | ||
@document = RubyLsp::RubyDocument.new(source: +<<~RUBY, version: 1, uri: URI::Generic.from_path(path: __FILE__)) | ||
class Foo | ||
def foo | ||
[ | ||
1, | ||
2, | ||
3, | ||
4, | ||
] | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_syntax_tree_supports_range_formatting | ||
# Note how only the selected array is formatted, otherwise the blank lines would be removed | ||
expect_formatted_range({ start: { line: 3, character: 2 }, end: { line: 8, character: 5 } }, <<~RUBY) | ||
class Foo | ||
def foo | ||
[1, 2, 3, 4] | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
private | ||
|
||
def expect_formatted_range(range, expected) | ||
edits = T.must(RubyLsp::Requests::RangeFormatting.new(@global_state, @document, { range: range }).perform) | ||
|
||
@document.push_edits( | ||
edits.map do |edit| | ||
{ range: edit.range.to_hash.transform_values(&:to_hash), text: edit.new_text } | ||
end, | ||
version: 2, | ||
) | ||
|
||
assert_equal(expected, @document.source) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters