Skip to content

Commit

Permalink
Raise explicit NonExistingDocumentError for unseen untitled files
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock committed Jul 18, 2024
1 parent 91346f3 commit bf64744
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/ruby_lsp/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,18 @@ def get(uri)
document = @state[uri.to_s]
return document unless document.nil?

path = T.must(uri.to_standardized_path)
# For unsaved files (`untitled:Untitled-1` uris), there's no path to read from. If we don't have the untitled file
# already present in the store, then we have to raise non existing document error
path = uri.to_standardized_path
raise NonExistingDocumentError, uri.to_s unless path

ext = File.extname(path)
language_id = if ext == ".erb" || ext == ".rhtml"
Document::LanguageId::ERB
else
Document::LanguageId::Ruby
end

set(uri: uri, source: File.binread(path), version: 0, language_id: language_id)
T.must(@state[uri.to_s])
rescue Errno::ENOENT
Expand Down
6 changes: 6 additions & 0 deletions test/store_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,10 @@ def test_push_edits
@store.get(uri),
)
end

def test_raises_non_existing_document_error_on_unknown_unsaved_files
assert_raises(RubyLsp::Store::NonExistingDocumentError) do
@store.get(URI("untitled:Untitled-1"))
end
end
end

0 comments on commit bf64744

Please sign in to comment.