Skip to content
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

Move rubocop file watching into the server #1921

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
70 changes: 46 additions & 24 deletions lib/ruby_lsp/server.rb
Expand Up @@ -205,6 +205,10 @@ def run_initialize(message)
glob_pattern: "**/*.rb",
kind: Constant::WatchKind::CREATE | Constant::WatchKind::CHANGE | Constant::WatchKind::DELETE,
),
Interface::FileSystemWatcher.new(
glob_pattern: "**/.rubocop.yml",
kind: Constant::WatchKind::CREATE | Constant::WatchKind::CHANGE | Constant::WatchKind::DELETE,
),
],
),
),
Expand Down Expand Up @@ -244,15 +248,24 @@ def run_initialized
end
end

register_rubocop_formatter
register_syntax_tree_formatter
perform_initial_indexing(indexing_config)
check_formatter_is_available
end

sig { void }
def register_rubocop_formatter
if defined?(Requests::Support::RuboCopFormatter)
@global_state.register_formatter("rubocop", Requests::Support::RuboCopFormatter.new)
end
end

sig { void }
def register_syntax_tree_formatter
if defined?(Requests::Support::SyntaxTreeFormatter)
@global_state.register_formatter("syntax_tree", Requests::Support::SyntaxTreeFormatter.new)
end

perform_initial_indexing(indexing_config)
check_formatter_is_available
end

sig { params(message: T::Hash[Symbol, T.untyped]).void }
Expand All @@ -270,20 +283,24 @@ def text_document_did_open(message)

sig { params(message: T::Hash[Symbol, T.untyped]).void }
def text_document_did_close(message)
uri = message.dig(:params, :textDocument, :uri)
@mutex.synchronize do
uri = message.dig(:params, :textDocument, :uri)
@store.delete(uri)

# Clear diagnostics for the closed file, so that they no longer appear in the problems tab
send_message(
Notification.new(
method: "textDocument/publishDiagnostics",
params: Interface::PublishDiagnosticsParams.new(uri: uri.to_s, diagnostics: []),
),
)
clear_diagnostics(uri)
end
end

sig { params(uri: T.any(URI::Generic, String)).void }
def clear_diagnostics(uri)
send_message(
Notification.new(
method: "textDocument/publishDiagnostics",
params: Interface::PublishDiagnosticsParams.new(uri: uri.to_s, diagnostics: []),
),
)
end

sig { params(message: T::Hash[Symbol, T.untyped]).void }
def text_document_did_change(message)
params = message[:params]
Expand Down Expand Up @@ -610,19 +627,24 @@ def workspace_did_change_watched_files(message)
uri = URI(change[:uri])
file_path = uri.to_standardized_path
next if file_path.nil? || File.directory?(file_path)
next unless file_path.end_with?(".rb")

load_path_entry = $LOAD_PATH.find { |load_path| file_path.start_with?(load_path) }
indexable = RubyIndexer::IndexablePath.new(load_path_entry, file_path)

case change[:type]
when Constant::FileChangeType::CREATED
index.index_single(indexable)
when Constant::FileChangeType::CHANGED
index.delete(indexable)
index.index_single(indexable)
when Constant::FileChangeType::DELETED
index.delete(indexable)

if file_path.end_with?(".rb")
load_path_entry = $LOAD_PATH.find { |load_path| file_path.start_with?(load_path) }
indexable = RubyIndexer::IndexablePath.new(load_path_entry, file_path)

case change[:type]
when Constant::FileChangeType::CREATED
index.index_single(indexable)
when Constant::FileChangeType::CHANGED
index.delete(indexable)
index.index_single(indexable)
when Constant::FileChangeType::DELETED
index.delete(indexable)
end
elsif file_path.end_with?(".rubocop.yml")
$stderr.puts("Reloading RuboCop config")
register_rubocop_formatter
@store.each_uri { |uri| clear_diagnostics(uri) }
end
end

Expand Down
7 changes: 6 additions & 1 deletion lib/ruby_lsp/store.rb
Expand Up @@ -56,6 +56,11 @@ def push_edits(uri:, edits:, version:)
T.must(@state[uri.to_s]).push_edits(edits, version: version)
end

sig { params(block: T.proc.params(uri: String).void).void }
def each_uri(&block)
@state.each_key(&block)
end

sig { void }
def clear
@state.clear
Expand All @@ -66,7 +71,7 @@ def empty?
@state.empty?
end

sig { params(uri: URI::Generic).void }
sig { params(uri: T.any(URI::Generic, String)).void }
def delete(uri)
@state.delete(uri.to_s)
end
Expand Down
1 change: 0 additions & 1 deletion vscode/src/workspace.ts
Expand Up @@ -245,7 +245,6 @@ export class Workspace implements WorkspaceInterface {
private registerRestarts(context: vscode.ExtensionContext) {
this.createRestartWatcher(context, "Gemfile.lock");
this.createRestartWatcher(context, "gems.locked");
this.createRestartWatcher(context, "**/.rubocop.yml");

// If a configuration that affects the Ruby LSP has changed, update the client options using the latest
// configuration and restart the server
Expand Down