Skip to content

Commit

Permalink
Move rubocop file watching into the server
Browse files Browse the repository at this point in the history
  • Loading branch information
Earlopain committed Apr 12, 2024
1 parent acee71a commit b77e603
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
49 changes: 33 additions & 16 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 Down Expand Up @@ -610,19 +623,23 @@ 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
end
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

0 comments on commit b77e603

Please sign in to comment.