Skip to content

Commit

Permalink
Add store class and prefer always reading from memory
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock committed Mar 22, 2022
1 parent 2c281e3 commit a0c892a
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 5 deletions.
7 changes: 2 additions & 5 deletions 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
Expand All @@ -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
Expand Down
30 changes: 30 additions & 0 deletions 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
41 changes: 41 additions & 0 deletions 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
1 change: 1 addition & 0 deletions test/test_helper.rb
Expand Up @@ -5,6 +5,7 @@
require "ruby/lsp"
require "minitest/autorun"
require "minitest/reporters"
require "tempfile"

Minitest::Reporters.use!(Minitest::Reporters::SpecReporter.new(color: true))

Expand Down

0 comments on commit a0c892a

Please sign in to comment.