diff --git a/lib/ruby/lsp/handler.rb b/lib/ruby/lsp/handler.rb index 4a906c137f..d919a02985 100644 --- a/lib/ruby/lsp/handler.rb +++ b/lib/ruby/lsp/handler.rb @@ -1,8 +1,7 @@ # frozen_string_literal: true -require "cgi" -require "uri" require_relative "requests" +require_relative "store" module Ruby module Lsp @@ -18,9 +17,7 @@ def initialize @reader = Transport::Stdio::Reader.new @handlers = {} @running = true - @store = Hash.new do |hash, uri| - hash[uri] = File.binread(CGI.unescape(URI.parse(uri).path)) - end + @store = Store.new end def start diff --git a/lib/ruby/lsp/store.rb b/lib/ruby/lsp/store.rb new file mode 100644 index 0000000000..bca863e4c5 --- /dev/null +++ b/lib/ruby/lsp/store.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +require "cgi" +require "uri" + +module Ruby + module Lsp + class Store + def initialize + @state = {} + end + + def [](uri) + @state[uri] || File.binread(CGI.unescape(URI.parse(uri).path)) + end + + def []=(uri, content) + @state[uri] = content + end + + def clear + @state.clear + end + + def delete(uri) + @state.delete(uri) + end + end + end +end diff --git a/test/store_test.rb b/test/store_test.rb new file mode 100644 index 0000000000..6ca0931ab6 --- /dev/null +++ b/test/store_test.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +require "test_helper" + +class StoreTest < Minitest::Test + def test_hash_accessors + store = Ruby::Lsp::Store.new + store["/foo/bar.rb"] = "foo" + + assert_equal("foo", store["/foo/bar.rb"]) + end + + def test_reads_from_file_if_missing_in_store + store = Ruby::Lsp::Store.new + + file = Tempfile.new("foo.rb") + file.write("some great code") + file.rewind + + assert_equal("some great code", store[file.path]) + ensure + file.close + file.unlink + end + + def test_clear + store = Ruby::Lsp::Store.new + store["/foo/bar.rb"] = "foo" + store.clear + + assert_empty(store.instance_variable_get(:@state)) + end + + def test_delete + store = Ruby::Lsp::Store.new + store["/foo/bar.rb"] = "foo" + store.delete("/foo/bar.rb") + + assert_empty(store.instance_variable_get(:@state)) + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 1470d03847..669b2b5498 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -5,6 +5,7 @@ require "ruby/lsp" require "minitest/autorun" require "minitest/reporters" +require "tempfile" Minitest::Reporters.use!(Minitest::Reporters::SpecReporter.new(color: true))