Skip to content

Commit 1bf88c8

Browse files
committed
Debug
1 parent 0e8c861 commit 1bf88c8

File tree

5 files changed

+43
-13
lines changed

5 files changed

+43
-13
lines changed

Diff for: lib/ruby_lsp/requests/formatting.rb

+21-4
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ def provider
1616
end
1717
end
1818

19-
#: (GlobalState global_state, RubyDocument document) -> void
20-
def initialize(global_state, document)
19+
#: (GlobalState global_state, RubyDocument document, Thread::Queue outgoing_queue) -> void
20+
def initialize(global_state, document, outgoing_queue)
2121
super()
2222
@document = document
2323
@active_formatter = global_state.active_formatter #: Support::Formatter?
2424
@uri = document.uri #: URI::Generic
25+
@queue = outgoing_queue
2526
end
2627

2728
# @override
@@ -33,12 +34,28 @@ def perform
3334
# We don't format erb documents yet
3435

3536
formatted_text = @active_formatter.run_formatting(@uri, @document)
36-
return unless formatted_text
37+
unless formatted_text
38+
@queue << Notification.window_log_message("No text returned by formatter")
39+
return
40+
end
3741

3842
lines = @document.source.lines
3943
size = @document.source.size
4044

41-
return if formatted_text.size == size && formatted_text == @document.source
45+
if formatted_text.size == size && formatted_text == @document.source
46+
@queue << Notification.window_log_message(<<~MESSAGE)
47+
No changes made to the document.
48+
49+
Original:
50+
51+
#{@document.source}
52+
53+
Formatted:
54+
55+
#{formatted_text}
56+
MESSAGE
57+
return
58+
end
4259

4360
[
4461
Interface::TextEdit.new(

Diff for: lib/ruby_lsp/server.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ def text_document_formatting(message)
653653
return
654654
end
655655

656-
response = Requests::Formatting.new(@global_state, document).perform
656+
response = Requests::Formatting.new(@global_state, document, @outgoing_queue).perform
657657
send_message(Result.new(id: message[:id], response: response))
658658
rescue Requests::Request::InvalidFormatter => error
659659
send_message(Notification.window_show_message(

Diff for: test/requests/formatting_expectations_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def run_expectations(source)
1919
uri: URI::Generic.from_path(path: __FILE__),
2020
global_state: @global_state,
2121
)
22-
RubyLsp::Requests::Formatting.new(@global_state, document).perform&.first&.new_text
22+
RubyLsp::Requests::Formatting.new(@global_state, document, Thread::Queue.new).perform&.first&.new_text
2323
rescue RubyLsp::Requests::Support::InternalRuboCopError
2424
skip("Fixture requires a fix from Rubocop")
2525
end

Diff for: test/requests/formatting_test.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def test_does_not_format_with_formatter_is_none
5050
original_formatter = @global_state.formatter
5151
@global_state.formatter = "none"
5252
document = RubyLsp::RubyDocument.new(source: "def foo", version: 1, uri: @uri, global_state: @global_state)
53-
assert_nil(RubyLsp::Requests::Formatting.new(@global_state, document).perform)
53+
assert_nil(RubyLsp::Requests::Formatting.new(@global_state, document, Thread::Queue.new).perform)
5454
ensure
5555
@global_state.formatter = original_formatter
5656
end
@@ -89,7 +89,7 @@ def test_syntax_tree_formatting_ignores_syntax_invalid_documents
8989
require "ruby_lsp/requests/formatting"
9090
@global_state.formatter = "syntax_tree"
9191
document = RubyLsp::RubyDocument.new(source: "def foo", version: 1, uri: @uri, global_state: @global_state)
92-
assert_nil(RubyLsp::Requests::Formatting.new(@global_state, document).perform)
92+
assert_nil(RubyLsp::Requests::Formatting.new(@global_state, document, Thread::Queue.new).perform)
9393
end
9494

9595
def test_syntax_tree_formatting_returns_nil_if_file_matches_ignore_files_options_from_streerc
@@ -110,7 +110,7 @@ def foo
110110

111111
def test_rubocop_formatting_ignores_syntax_invalid_documents
112112
document = RubyLsp::RubyDocument.new(source: "def foo", version: 1, uri: @uri, global_state: @global_state)
113-
assert_nil(RubyLsp::Requests::Formatting.new(@global_state, document).perform)
113+
assert_nil(RubyLsp::Requests::Formatting.new(@global_state, document, Thread::Queue.new).perform)
114114
end
115115

116116
def test_returns_nil_if_document_is_already_formatted
@@ -123,7 +123,7 @@ def foo
123123
end
124124
end
125125
RUBY
126-
assert_nil(RubyLsp::Requests::Formatting.new(@global_state, document).perform)
126+
assert_nil(RubyLsp::Requests::Formatting.new(@global_state, document, Thread::Queue.new).perform)
127127
end
128128

129129
def test_allows_specifying_formatter
@@ -163,7 +163,7 @@ def test_returns_nil_when_formatter_is_none
163163

164164
def formatted_document(formatter)
165165
@global_state.formatter = formatter
166-
RubyLsp::Requests::Formatting.new(@global_state, @document).perform&.first&.new_text
166+
RubyLsp::Requests::Formatting.new(@global_state, @document, Thread::Queue.new).perform&.first&.new_text
167167
end
168168

169169
def with_syntax_tree_config_file(contents)

Diff for: vscode/src/test/suite/client.test.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
TextDocumentFilter,
2525
ShowMessageParams,
2626
MessageType,
27+
LogMessageNotification,
2728
} from "vscode-languageclient/node";
2829
import { after, afterEach, before, setup } from "mocha";
2930

@@ -431,7 +432,8 @@ suite("Client", () => {
431432
}).timeout(20000);
432433

433434
test("formatting", async () => {
434-
const text = ["", "foo()"].join("\n").trim();
435+
const lineBreak = os.platform() === "win32" ? "\r\n" : "\n";
436+
const text = ["", "foo()"].join(lineBreak).trim();
435437

436438
await client.sendNotification("textDocument/didOpen", {
437439
textDocument: {
@@ -440,6 +442,15 @@ suite("Client", () => {
440442
text,
441443
},
442444
});
445+
446+
const disposable = client.onNotification(
447+
LogMessageNotification.type,
448+
(params) => {
449+
// eslint-disable-next-line no-console
450+
console.log(params.message);
451+
},
452+
);
453+
443454
const response: TextEdit[] = await client.sendRequest(
444455
"textDocument/formatting",
445456
{
@@ -449,8 +460,10 @@ suite("Client", () => {
449460
},
450461
);
451462

463+
disposable.dispose();
464+
452465
const expected = ["# frozen_string_literal: true", "", "foo"]
453-
.join("\n")
466+
.join(lineBreak)
454467
.trim();
455468

456469
assert.strictEqual(response[0].newText.trim(), expected);

0 commit comments

Comments
 (0)