Skip to content

Commit

Permalink
Merge pull request #936 from soutaro/watcher
Browse files Browse the repository at this point in the history
Set up file watcher
  • Loading branch information
soutaro authored Oct 20, 2023
2 parents 34b47e3 + 95c1ed6 commit 85bdb82
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 5 deletions.
96 changes: 94 additions & 2 deletions lib/steep/server/master.rb
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,10 @@ def work_done_progress_supported?
initialize_params&.dig(:capabilities, :window, :workDoneProgress) ? true : false
end

def file_system_watcher_supported?
initialize_params&.dig(:capabilities, :workspace, :didChangeWatchedFiles, :dynamicRegistration) || false
end

def process_message_from_client(message)
Steep.logger.info "Processing message from client: method=#{message[:method]}, id=#{message[:id]}"
id = message[:id]
Expand Down Expand Up @@ -578,6 +582,52 @@ def process_message_from_client(message)
)
}
)

if file_system_watcher_supported?
patterns = [] #: Array[String]
project.targets.each do |target|
target.source_pattern.patterns.each do |pat|
path = project.base_dir + pat
patterns << path.to_s unless path.directory?
end
target.source_pattern.prefixes.each do |pat|
path = project.base_dir + pat
patterns << (path + "**/*.rb").to_s unless path.file?
end
target.signature_pattern.patterns.each do |pat|
path = project.base_dir + pat
patterns << path.to_s unless path.directory?
end
target.signature_pattern.prefixes.each do |pat|
path = project.base_dir + pat
patterns << (path + "**/*.rbs").to_s unless path.file?
end
end
patterns.sort!
patterns.uniq!

Steep.logger.info { "Setting up didChangeWatchedFiles with pattern: #{patterns.inspect}" }

job_queue << SendMessageJob.to_client(
message: {
id: SecureRandom.uuid,
method: "client/registerCapability",
params: {
registrations: [
{
id: SecureRandom.uuid,
method: "workspace/didChangeWatchedFiles",
registerOptions: {
watchers: patterns.map do |pattern|
{ globPattern: pattern }
end
}
}
]
}
}
)
end
end
end

Expand All @@ -588,6 +638,42 @@ def process_message_from_client(message)
end
end

when "workspace/didChangeWatchedFiles"
message[:params][:changes].each do |change|
uri = change[:uri]
type = change[:type]

path = PathHelper.to_pathname(uri) or next

controller.push_changes(path)

case type
when 1, 2
content = path.read
when 4
# Deleted
content = ""
end

broadcast_notification({
method: "$/file/reset",
params: { uri: uri, content: content }
})
end

if typecheck_automatically
start_type_checking_queue.execute do
job_queue.push(
-> do
last_request = current_type_check_request
if request = controller.make_request(last_request: last_request)
start_type_check(request, last_request: last_request, start_progress: request.total > 10)
end
end
)
end
end

when "textDocument/didChange"
if path = pathname(message[:params][:textDocument][:uri])
broadcast_notification(message)
Expand All @@ -608,9 +694,15 @@ def process_message_from_client(message)
end

when "textDocument/didOpen"
if path = pathname(message[:params][:textDocument][:uri])
uri = message[:params][:textDocument][:uri]
text = message[:params][:textDocument][:text]

if path = pathname(uri)
controller.update_priority(open: path)
broadcast_notification(message)
broadcast_notification({
method: "$/file/reset",
params: { uri: uri, content: text }
})
end

when "textDocument/didClose"
Expand Down
6 changes: 3 additions & 3 deletions lib/steep/server/type_check_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ def handle_request(request)
when "textDocument/didChange"
collect_changes(request)

when "textDocument/didOpen"
uri = request[:params][:textDocument][:uri]
text = request[:params][:textDocument][:text]
when "$/file/reset"
uri = request[:params][:uri]
text = request[:params][:content]
reset_change(uri: uri, text: text)

when "workspace/symbol"
Expand Down
2 changes: 2 additions & 0 deletions sig/steep/server/master.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ module Steep

def work_done_progress_supported?: () -> bool

def file_system_watcher_supported?: () -> bool

def process_message_from_client: (untyped message) -> void

def process_message_from_worker: (untyped message, worker: WorkerProcess) -> void
Expand Down

0 comments on commit 85bdb82

Please sign in to comment.