Skip to content

Commit

Permalink
Limit Snippet for Cursor Positioning to VS Code (#1308)
Browse files Browse the repository at this point in the history
The $0 snippet, used for cursor positioning, is not universally supported across all editors. This can lead to unexpected behaviour in editors like Emacs. To address this, we can limit the use of the $0 snippet to Visual Studio Code only.
  • Loading branch information
aryan-soni authored Jan 15, 2024
1 parent bdd4ff8 commit 291bc84
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 23 deletions.
5 changes: 4 additions & 1 deletion lib/ruby_lsp/executor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ def formatting(uri)
).returns(T::Array[Interface::TextEdit])
end
def on_type_formatting(uri, position, character)
Requests::OnTypeFormatting.new(@store.get(uri), position, character).perform
Requests::OnTypeFormatting.new(@store.get(uri), position, character, @store.client_name).perform
end

sig do
Expand Down Expand Up @@ -497,6 +497,9 @@ def initialize_request(options)
workspace_uri = options.dig(:workspaceFolders, 0, :uri)
@store.workspace_uri = URI(workspace_uri) if workspace_uri

client_name = options.dig(:clientInfo, :name)
@store.client_name = client_name if client_name

encodings = options.dig(:capabilities, :general, :positionEncodings)
@store.encoding = if encodings.nil? || encodings.empty?
Constant::PositionEncodingKind::UTF16
Expand Down
14 changes: 12 additions & 2 deletions lib/ruby_lsp/requests/on_type_formatting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,15 @@ def provider
T::Array[Regexp],
)

sig { params(document: Document, position: T::Hash[Symbol, T.untyped], trigger_character: String).void }
def initialize(document, position, trigger_character)
sig do
params(
document: Document,
position: T::Hash[Symbol, T.untyped],
trigger_character: String,
client_name: String,
).void
end
def initialize(document, position, trigger_character, client_name)
super()
@document = document
@lines = T.let(@document.source.lines, T::Array[String])
Expand All @@ -50,6 +57,7 @@ def initialize(document, position, trigger_character)
@position = position
@edits = T.let([], T::Array[Interface::TextEdit])
@trigger_character = trigger_character
@client_name = client_name
end

sig { override.returns(T.all(T::Array[Interface::TextEdit], Object)) }
Expand Down Expand Up @@ -170,6 +178,8 @@ def add_edit_with_text(text, position = @position)

sig { params(line: Integer, character: Integer).void }
def move_cursor_to(line, character)
return if @client_name != "Visual Studio Code"

position = Interface::Position.new(
line: line,
character: character,
Expand Down
4 changes: 4 additions & 0 deletions lib/ruby_lsp/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class Store
sig { returns(T::Hash[Symbol, RequestConfig]) }
attr_accessor :features_configuration

sig { returns(String) }
attr_accessor :client_name

sig { void }
def initialize
@state = T.let({}, T::Hash[String, Document])
Expand All @@ -45,6 +48,7 @@ def initialize
},
T::Hash[Symbol, RequestConfig],
)
@client_name = T.let("Unknown", String)
end

sig { params(uri: URI::Generic).returns(Document) }
Expand Down
11 changes: 11 additions & 0 deletions test/executor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,17 @@ def test_returns_void_for_unhandled_request
assert_same(RubyLsp::VOID, result.response)
end

def test_initialize_sets_client_name
@executor.execute({
method: "initialize",
params: {
clientInfo: { name: "Foo" },
},
})

assert_equal("Foo", @store.client_name)
end

private

def with_uninstalled_rubocop(&block)
Expand Down
Loading

0 comments on commit 291bc84

Please sign in to comment.