diff --git a/lib/core_ext/uri.rb b/lib/core_ext/uri.rb index 77fe38de6..319199b1e 100644 --- a/lib/core_ext/uri.rb +++ b/lib/core_ext/uri.rb @@ -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 diff --git a/lib/ruby_lsp/store.rb b/lib/ruby_lsp/store.rb index fc95e1d65..3dd290b16 100644 --- a/lib/ruby_lsp/store.rb +++ b/lib/ruby_lsp/store.rb @@ -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] 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) + 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 } @@ -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 diff --git a/test/store_test.rb b/test/store_test.rb index 87dd31149..51f984afe 100644 --- a/test/store_test.rb +++ b/test/store_test.rb @@ -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 @@ -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")