Skip to content

Commit

Permalink
Add watched_files_dynamic_registration to global state
Browse files Browse the repository at this point in the history
This allows addons to check if they can register custom file change listerens. Work towards Shopify#326

It makes sense to add docs on how to make use of this option,
but I'll defer that until after I've actually written some functioning code with it.
  • Loading branch information
Earlopain committed Apr 10, 2024
1 parent 729df3e commit 430bb6c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
9 changes: 9 additions & 0 deletions lib/ruby_lsp/global_state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class GlobalState
sig { returns(Encoding) }
attr_reader :encoding

sig { returns(T::Boolean) }
attr_reader :supports_watching_files

sig { void }
def initialize
@workspace_uri = T.let(URI::Generic.from_path(path: Dir.pwd), URI::Generic)
Expand All @@ -30,6 +33,7 @@ def initialize
@typechecker = T.let(detect_typechecker, T::Boolean)
@index = T.let(RubyIndexer::Index.new, RubyIndexer::Index)
@supported_formatters = T.let({}, T::Hash[String, Requests::Support::Formatter])
@supports_watching_files = T.let(false, T::Boolean)
end

sig { params(identifier: String, instance: Requests::Support::Formatter).void }
Expand Down Expand Up @@ -61,6 +65,11 @@ def apply_options(options)
else
Encoding::UTF_32
end

file_watching_caps = options.dig(:capabilities, :workspace, :didChangeWatchedFiles)
if file_watching_caps&.dig(:dynamicRegistration) && file_watching_caps&.dig(:relativePatternSupport)
@supports_watching_files = true
end
end

sig { returns(String) }
Expand Down
5 changes: 1 addition & 4 deletions lib/ruby_lsp/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,8 @@ def run_initialize(message)

send_message(Result.new(id: message[:id], response: response))

# Dynamically registered capabilities
file_watching_caps = options.dig(:capabilities, :workspace, :didChangeWatchedFiles)

# Not every client supports dynamic registration or file watching
if file_watching_caps&.dig(:dynamicRegistration) && file_watching_caps&.dig(:relativePatternSupport)
if global_state.supports_watching_files
send_message(
Request.new(
id: @current_request_id,
Expand Down
40 changes: 40 additions & 0 deletions test/global_state_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,46 @@ def test_applying_auto_formatter_invokes_detection
assert_equal("rubocop", state.formatter)
end

def test_watching_files_if_supported
state = GlobalState.new
state.apply_options({
capabilities: {
workspace: {
didChangeWatchedFiles: {
dynamicRegistration: true,
relativePatternSupport: true,
},
},
},
})
assert(state.supports_watching_files)
end

def test_watching_files_if_not_supported
state = GlobalState.new
state.apply_options({
capabilities: {
workspace: {
didChangeWatchedFiles: {
dynamicRegistration: true,
relativePatternSupport: false,
},
},
},
})
refute(state.supports_watching_files)
end

def test_watching_files_if_not_reported
state = GlobalState.new
state.apply_options({
capabilities: {
workspace: {},
},
})
refute(state.supports_watching_files)
end

private

def stub_dependencies(dependencies)
Expand Down

0 comments on commit 430bb6c

Please sign in to comment.