Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add store class and prefer always reading from memory
- Loading branch information
Showing
4 changed files
with
74 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters