Module: Canopus::Workspace::FileChangeAware

Defined in:
lib/canopus/workspace/file_change_aware.rb

Instance Method Summary collapse

Instance Method Details

#poll_changes ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/canopus/workspace/file_change_aware.rb', line 19

def poll_changes
  return unless @watcher
  now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  return if now - @last_watch < (@watcher.backend == :native ? 0.05 : 1.0)
  @last_watch = now
  events = @watcher.poll
  invalidate_git unless events.empty?
  refresh_files unless events.empty?
  notify_plugin_event("workspace/didChangeFiles", events.map { |event| {"type" => event.type.to_s, "path" => event.path} }) unless events.empty?
  if events.any? { |event| File.basename(event.path) == ".editorconfig" }
    invalidate_editorconfig
    apply_settings
  end
  events.each do |event|
    path = File.expand_path(event.path, @root)
    buffer = @buffers[path]
    next unless buffer
    if event.type == :deleted
      @message = "File was deleted on disk: #{event.path}"
    elsif buffer.dirty?
      @message = "File changed on disk; unsaved edits preserved: #{event.path}"
    else
      buffer.reload
    end
  rescue SaveConflict, SystemCallError, Error => error
    @message = error.message
  end
  @window&.request_frame unless events.empty?
end

#start_watching ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/canopus/workspace/file_change_aware.rb', line 5

def start_watching
  return if @watcher || !@project
  native = if @window && @window.class == Zaniah::Platform::Headless::Window
    nil
  else
    begin
      Zaniah::Platform.watch(@root)
    rescue LoadError, Zaniah::Error, SystemCallError
      nil
    end
  end
  @watcher = @project.watcher(native: native)
  @last_watch = 0
end