Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always use complete URI for store key #891

Merged
merged 1 commit into from
Aug 17, 2023
Merged
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
5 changes: 0 additions & 5 deletions lib/core_ext/uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,5 @@ def to_standardized_path
unescaped_path
end
end

sig { returns(String) }
def storage_key
T.must(to_standardized_path || opaque)
end
end
end
16 changes: 7 additions & 9 deletions lib/ruby_lsp/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,23 @@ def initialize

sig { params(uri: URI::Generic).returns(Document) }
def get(uri)
path = uri.to_standardized_path
return T.must(@state[T.must(uri.opaque)]) unless path

document = @state[path]
document = @state[uri.to_s]
vinistock marked this conversation as resolved.
Show resolved Hide resolved
return document unless document.nil?

set(uri: uri, source: File.binread(CGI.unescape(path)), version: 0)
T.must(@state[path])
path = T.must(uri.to_standardized_path)
vinistock marked this conversation as resolved.
Show resolved Hide resolved
set(uri: uri, source: File.binread(path), version: 0)
T.must(@state[uri.to_s])
end

sig { params(uri: URI::Generic, source: String, version: Integer).void }
def set(uri:, source:, version:)
document = Document.new(source: source, version: version, uri: uri, encoding: @encoding)
@state[uri.storage_key] = document
@state[uri.to_s] = document
end

sig { params(uri: URI::Generic, edits: T::Array[Document::EditShape], version: Integer).void }
def push_edits(uri:, edits:, version:)
T.must(@state[uri.storage_key]).push_edits(edits, version: version)
T.must(@state[uri.to_s]).push_edits(edits, version: version)
end

sig { void }
Expand All @@ -55,7 +53,7 @@ def empty?

sig { params(uri: URI::Generic).void }
def delete(uri)
@state.delete(uri.storage_key)
@state.delete(uri.to_s)
end

sig do
Expand Down
33 changes: 32 additions & 1 deletion test/store_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class StoreTest < Minitest::Test
def setup
@store = RubyLsp::Store.new
@store.set(uri: URI("/foo/bar.rb"), source: "def foo; end", version: 1)
@store.set(uri: URI::Generic.from_path(path: "/foo/bar.rb"), source: "def foo; end", version: 1)
end

def test_get
Expand All @@ -27,6 +27,37 @@ def test_handling_uris_with_spaces
)
end

def test_handling_uris_for_unsaved_files
uri = URI("untitled:Untitled-1")
@store.set(uri: uri, source: "def foo; end", version: 1)

assert_equal(
RubyLsp::Document.new(source: "def foo; end", version: 1, uri: uri),
@store.get(uri),
)

@store.delete(uri)
refute_includes(@store.instance_variable_get(:@state), uri.to_s)
end

def test_uris_with_different_schemes_do_not_collide
path = "/foo/bar.rb"
file_uri = URI("file://#{path}")
git_uri = URI("git://#{path}")
@store.set(uri: file_uri, source: "def foo; end", version: 1)
@store.set(uri: git_uri, source: "def foo; end", version: 1)

refute_same(@store.get(file_uri), @store.get(git_uri))

state = @store.instance_variable_get(:@state)
assert_includes(state, file_uri.to_s)
assert_includes(state, git_uri.to_s)

@store.delete(git_uri)
assert_includes(state, file_uri.to_s)
refute_includes(state, git_uri.to_s)
end

def test_reading_from_tempfile_can_handle_spaces
file = Tempfile.new("foo bar.rb")
file.write("def great_code; end")
Expand Down