Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions jekyll/editors.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ configuration languages (JSON, Lua, ELisp, etc.).
"inlayHint": {
"implicitHashValue": true,
"implicitRescue": true
},
"onTypeFormatting": {
"commentContinuation": true
}
},
"indexing": {
Expand Down
12 changes: 12 additions & 0 deletions jekyll/index.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,18 @@ On type formatting applies changes to the code as the user is typing. For exampl
{: .note }
In VS Code, format on type is disabled by default. You can enable it with `"editor.formatOnType": true`

By default, breaking a line inside a comment continues the comment on the next line. VS Code users can turn this
behavior off with the following setting:

```jsonc
{
// Disable comment continuation when breaking lines inside a comment (defaults to true)
"rubyLsp.featuresConfiguration.onTypeFormatting.commentContinuation": false
}
```

To configure other editors, see the [initialization options](editors#all-initialization-options).

<video src="images/on_type_formatting.mp4" width="100%" controls>
Sorry, your browser doesn't support embedded videos. This video demonstrates on-type formatting that auto-completes `end` tokens as you type.
</video>
Expand Down
4 changes: 4 additions & 0 deletions lib/ruby_lsp/global_state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ def initialize
enableAll: false,
enableTestCodeLens: true,
}),
onTypeFormatting: RequestConfig.new({
enableAll: false,
commentContinuation: true,
}),
} #: Hash[Symbol, RequestConfig]
end

Expand Down
21 changes: 16 additions & 5 deletions lib/ruby_lsp/requests/on_type_formatting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def provider
/.*\s\bdo\b($|\s)/,
] #: Array[Regexp]

#: (RubyDocument document, Hash[Symbol, untyped] position, String trigger_character, String client_name) -> void
def initialize(document, position, trigger_character, client_name)
#: (RubyDocument document, Hash[Symbol, untyped] position, String trigger_character, String client_name, ?RequestConfig? config) -> void
def initialize(document, position, trigger_character, client_name, config = nil)
super()
@document = document
@lines = @document.source.lines #: Array[String]
Expand All @@ -36,6 +36,7 @@ def initialize(document, position, trigger_character, client_name)
@edits = [] #: Array[Interface::TextEdit]
@trigger_character = trigger_character
@client_name = client_name
@config = config
end

# @override
Expand All @@ -51,9 +52,11 @@ def perform
# But if it's a RBS signature starting with `#:`, we'll ignore it
# so users can immediately continue typing the method definition
if (comment_match = @previous_line.match(/^#(?!:)(\s*)/))
handle_comment_line(
comment_match[1], #: as !nil
)
if comment_continuation_enabled?
handle_comment_line(
comment_match[1], #: as !nil
)
end
elsif @document.syntax_error?
match = /(<<((-|~)?))(?<quote>['"`]?)(?<delimiter>\w+)\k<quote>/.match(@previous_line)
heredoc_delimiter = match && match.named_captures["delimiter"]
Expand All @@ -73,6 +76,14 @@ def perform

private

#: -> bool
def comment_continuation_enabled?
config = @config
return true unless config

config.enabled?(:commentContinuation) || false
end

#: -> void
def handle_pipe
current_line = @lines[@position[:line]]
Expand Down
1 change: 1 addition & 0 deletions lib/ruby_lsp/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,7 @@ def text_document_on_type_formatting(message)
params[:position],
params[:ch],
@store.client_name,
@global_state.feature_configuration(:onTypeFormatting),
).perform,
),
)
Expand Down
18 changes: 18 additions & 0 deletions test/global_state_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,24 @@ def test_feature_configuration_with_partially_provided_configuration
assert(inlay_hint_config.enabled?(:implicitHashValue))
end

def test_on_type_formatting_comment_continuation_configuration
state = GlobalState.new
on_type_formatting_config = state.feature_configuration(:onTypeFormatting) #: as !nil
assert(on_type_formatting_config.enabled?(:commentContinuation))

state.apply_options({
initializationOptions: {
featuresConfiguration: {
onTypeFormatting: {
commentContinuation: false,
},
},
},
})

refute(on_type_formatting_config.enabled?(:commentContinuation))
end

def test_initialize_features_with_enable_all_configuration
state = GlobalState.new
state.apply_options({
Expand Down
70 changes: 70 additions & 0 deletions test/requests/on_type_formatting_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,76 @@ def test_comment_continuation
assert_equal(expected_edits.to_json, edits.to_json)
end

def test_comment_continuation_applies_with_default_configuration
document = RubyLsp::RubyDocument.new(
source: +"",
version: 1,
uri: URI("file:///fake.rb"),
global_state: @global_state,
)

document.push_edits(
[{
range: { start: { line: 0, character: 0 }, end: { line: 0, character: 0 } },
text: " # something",
}],
version: 2,
)
document.parse!

edits = RubyLsp::Requests::OnTypeFormatting.new(
document,
{ line: 0, character: 14 },
"\n",
"Visual Studio Code",
@global_state.feature_configuration(:onTypeFormatting),
).perform
expected_edits = [
{
range: { start: { line: 0, character: 14 }, end: { line: 0, character: 14 } },
newText: "# ",
},
]
assert_equal(expected_edits.to_json, edits.to_json)
end

def test_comment_continuation_can_be_disabled_through_configuration
@global_state.apply_options({
initializationOptions: {
featuresConfiguration: {
onTypeFormatting: {
commentContinuation: false,
},
},
},
})

document = RubyLsp::RubyDocument.new(
source: +"",
version: 1,
uri: URI("file:///fake.rb"),
global_state: @global_state,
)

document.push_edits(
[{
range: { start: { line: 0, character: 0 }, end: { line: 0, character: 0 } },
text: " # something",
}],
version: 2,
)
document.parse!

edits = RubyLsp::Requests::OnTypeFormatting.new(
document,
{ line: 0, character: 14 },
"\n",
"Visual Studio Code",
@global_state.feature_configuration(:onTypeFormatting),
).perform
assert_empty(edits)
end

def test_comment_continuation_does_not_apply_to_rbs_signatures
document = RubyLsp::RubyDocument.new(
source: +"",
Expand Down
41 changes: 41 additions & 0 deletions test/server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,47 @@ def foo
end
end

def test_on_type_formatting_comment_continuation_can_be_disabled
uri = URI::Generic.from_path(path: "/fake.rb")

capture_io do
@server.process_message(id: 1, method: "initialize", params: {
initializationOptions: {
featuresConfiguration: {
onTypeFormatting: {
commentContinuation: false,
},
},
},
})

@server.process_message({
method: "textDocument/didOpen",
params: {
textDocument: {
uri: uri,
text: "# some comment",
version: 1,
languageId: "ruby",
},
},
})

@server.process_message({
id: 2,
method: "textDocument/onTypeFormatting",
params: {
textDocument: { uri: uri },
position: { line: 1, character: 0 },
ch: "\n",
},
})

result = find_message(RubyLsp::Result, id: 2)
assert_empty(result.response)
end
end

def test_show_window_responses_are_redirected_to_addons
klass = Class.new(RubyLsp::Addon) do
def activate(global_state, outgoing_queue)
Expand Down
14 changes: 14 additions & 0 deletions vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,20 @@
"default": true
}
}
},
"onTypeFormatting": {
"description": "Customize on type formatting features",
"type": "object",
"properties": {
"enableAll": {
"type": "boolean"
},
"commentContinuation": {
"description": "Automatically continue comments when breaking lines inside a comment",
"type": "boolean",
"default": true
}
}
}
}
},
Expand Down
Loading