-
Notifications
You must be signed in to change notification settings - Fork 182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extract RubyFileChangeHandler
#3225
Draft
andyw8
wants to merge
1
commit into
main
Choose a base branch
from
andyw8/extract-ruby-file-change-handler
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+180
−150
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
module RubyLsp | ||
module FileChangeHandlers | ||
class Ruby | ||
class << self | ||
extend T::Sig | ||
sig { params(store: Store, index: RubyIndexer::Index, file_path: String, change_type: Integer).void } | ||
def change(store, index, file_path, change_type) | ||
load_path_entry = $LOAD_PATH.find { |load_path| file_path.start_with?(load_path) } | ||
uri = URI::Generic.from_path(load_path_entry: load_path_entry, path: file_path) | ||
|
||
case change_type | ||
when Constant::FileChangeType::CREATED | ||
content = File.read(file_path) | ||
# If we receive a late created notification for a file that has already been claimed by the client, we want to | ||
# handle change for that URI so that the require path tree is updated | ||
store.key?(uri) ? index.handle_change(uri, content) : index.index_single(uri, content) | ||
when Constant::FileChangeType::CHANGED | ||
content = File.read(file_path) | ||
# We only handle changes on file watched notifications if the client is not the one managing this URI. | ||
# Otherwise, these changes are handled when running the combined requests | ||
index.handle_change(uri, content) unless store.key?(uri) | ||
when Constant::FileChangeType::DELETED | ||
index.delete(uri) | ||
end | ||
rescue Errno::ENOENT | ||
# If a file is created and then delete immediately afterwards, we will process the created notification before we | ||
# receive the deleted one, but the file no longer exists. This may happen when running a test suite that creates | ||
# and deletes files automatically. | ||
end | ||
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
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,142 @@ | ||
# typed: true | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
module RubyLsp | ||
module FileChangeHandlers | ||
class RubyTest < Minitest::Test | ||
def setup | ||
@server = RubyLsp::Server.new(test_mode: true) | ||
end | ||
|
||
def teardown | ||
@server.run_shutdown | ||
end | ||
|
||
def test_did_change_watched_files_does_not_fail_for_non_existing_files | ||
@server.process_message({ | ||
method: "workspace/didChangeWatchedFiles", | ||
params: { | ||
changes: [ | ||
{ | ||
uri: URI::Generic.from_path(path: File.join(Dir.pwd, "lib", "non_existing.rb")).to_s, | ||
type: RubyLsp::Constant::FileChangeType::CREATED, | ||
}, | ||
], | ||
}, | ||
}) | ||
|
||
assert_raises(Timeout::Error) do | ||
Timeout.timeout(0.5) do | ||
notification = find_message(RubyLsp::Notification, "window/logMessage") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (this test currently fails because |
||
flunk(notification.params.message) | ||
end | ||
end | ||
end | ||
|
||
def test_did_change_watched_files_handles_deletions | ||
path = File.join(Dir.pwd, "lib", "foo.rb") | ||
|
||
@server.global_state.index.expects(:delete).once.with do |uri| | ||
uri.full_path == path | ||
end | ||
|
||
uri = URI::Generic.from_path(path: path) | ||
|
||
@server.process_message({ | ||
method: "workspace/didChangeWatchedFiles", | ||
params: { | ||
changes: [ | ||
{ | ||
uri: uri, | ||
type: RubyLsp::Constant::FileChangeType::DELETED, | ||
}, | ||
], | ||
}, | ||
}) | ||
end | ||
|
||
def test_did_change_watched_files_reports_addon_errors | ||
Class.new(RubyLsp::Addon) do | ||
def activate(global_state, outgoing_queue); end | ||
|
||
def workspace_did_change_watched_files(changes) | ||
raise StandardError, "boom" | ||
end | ||
|
||
def name | ||
"Foo" | ||
end | ||
|
||
def deactivate; end | ||
|
||
def version | ||
"0.1.0" | ||
end | ||
end | ||
|
||
Class.new(RubyLsp::Addon) do | ||
def activate(global_state, outgoing_queue); end | ||
|
||
def workspace_did_change_watched_files(changes) | ||
end | ||
|
||
def name | ||
"Bar" | ||
end | ||
|
||
def deactivate; end | ||
|
||
def version | ||
"0.1.0" | ||
end | ||
end | ||
|
||
@server.load_addons | ||
|
||
bar = RubyLsp::Addon.get("Bar", "0.1.0") | ||
bar.expects(:workspace_did_change_watched_files).once | ||
|
||
begin | ||
@server.process_message({ | ||
method: "workspace/didChangeWatchedFiles", | ||
params: { | ||
changes: [ | ||
{ | ||
uri: URI::Generic.from_path(path: File.join(Dir.pwd, ".rubocop.yml")).to_s, | ||
type: RubyLsp::Constant::FileChangeType::CREATED, | ||
}, | ||
], | ||
}, | ||
}) | ||
|
||
message = @server.pop_response.params.message | ||
assert_match("Error in Foo add-on while processing watched file notifications", message) | ||
assert_match("boom", message) | ||
ensure | ||
RubyLsp::Addon.unload_addons | ||
end | ||
end | ||
|
||
def test_did_change_watched_files_processes_unique_change_entries | ||
@server.expects(:handle_rubocop_config_change).once | ||
@server.process_message({ | ||
method: "workspace/didChangeWatchedFiles", | ||
params: { | ||
changes: [ | ||
{ | ||
uri: URI::Generic.from_path(path: File.join(Dir.pwd, ".rubocop.yml")).to_s, | ||
type: RubyLsp::Constant::FileChangeType::CHANGED, | ||
}, | ||
{ | ||
uri: URI::Generic.from_path(path: File.join(Dir.pwd, ".rubocop.yml")).to_s, | ||
type: RubyLsp::Constant::FileChangeType::CHANGED, | ||
}, | ||
], | ||
}, | ||
}) | ||
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I put this in a namespace since we may want to extract handlers for other kinds kinds of files, e.g. RuboCop config).