Skip to content

Commit

Permalink
Allow snippets for Cursor editor (#3109)
Browse files Browse the repository at this point in the history
### Motivation

Fixes the behaviour described in #3094 (reply in thread).

While the LSP spec doesn't standardize snippet support, we use a custom middleware to allow moving the user's cursor around when applying on type formatting. We were checking to apply this only in VS Code, but not Cursor.

### Implementation

Added Cursor to the mix.

### Automated Tests

Added a test.
  • Loading branch information
vinistock committed Jan 29, 2025
1 parent e4f6a84 commit 1d79955
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/ruby_lsp/requests/on_type_formatting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def add_edit_with_text(text, position = @position)

sig { params(line: Integer, character: Integer).void }
def move_cursor_to(line, character)
return unless @client_name.start_with?("Visual Studio Code")
return unless /Visual Studio Code|Cursor/.match?(@client_name)

position = Interface::Position.new(
line: line,
Expand Down
40 changes: 40 additions & 0 deletions test/requests/on_type_formatting_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,46 @@ def test_includes_snippets_on_vscode_insiders
assert_equal(expected_edits.to_json, T.must(edits).to_json)
end

def test_includes_snippets_on_cursor
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: "class Foo",
}],
version: 2,
)
document.parse!

edits = RubyLsp::Requests::OnTypeFormatting.new(
document,
{ line: 1, character: 2 },
"\n",
"Cursor",
).perform
expected_edits = [
{
range: { start: { line: 1, character: 2 }, end: { line: 1, character: 2 } },
newText: "\n",
},
{
range: { start: { line: 1, character: 2 }, end: { line: 1, character: 2 } },
newText: "end",
},
{
range: { start: { line: 1, character: 2 }, end: { line: 1, character: 2 } },
newText: "$0",
},
]
assert_equal(expected_edits.to_json, T.must(edits).to_json)
end

def test_does_not_confuse_class_parameter_with_keyword
document = RubyLsp::RubyDocument.new(
source: +"",
Expand Down

0 comments on commit 1d79955

Please sign in to comment.