Class: Steep::Drivers::Watch

Inherits:
Object
  • Object
show all
Includes:
Utils::DriverHelper
Defined in:
lib/steep/drivers/watch.rb

Instance Attribute Summary collapse

Attributes included from Utils::DriverHelper

#steepfile

Instance Method Summary collapse

Methods included from Utils::DriverHelper

#load_config, #type_check

Constructor Details

#initialize(stdout:, stderr:) ⇒ Watch

Returns a new instance of Watch.



11
12
13
14
15
16
# File 'lib/steep/drivers/watch.rb', line 11

def initialize(stdout:, stderr:)
  @dirs = []
  @stdout = stdout
  @stderr = stderr
  @queue = Thread::Queue.new
end

Instance Attribute Details

#dirsObject (readonly)

Returns the value of attribute dirs.



4
5
6
# File 'lib/steep/drivers/watch.rb', line 4

def dirs
  @dirs
end

#queueObject (readonly)

Returns the value of attribute queue.



7
8
9
# File 'lib/steep/drivers/watch.rb', line 7

def queue
  @queue
end

#stderrObject (readonly)

Returns the value of attribute stderr.



6
7
8
# File 'lib/steep/drivers/watch.rb', line 6

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



5
6
7
# File 'lib/steep/drivers/watch.rb', line 5

def stdout
  @stdout
end

Instance Method Details

#runObject



18
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/steep/drivers/watch.rb', line 18

def run()
  if dirs.empty?
    stdout.puts "Specify directories to watch"
    return 1
  end

  project = load_config()

  loader = Project::FileLoader.new(project: project)
  loader.load_sources([])
  loader.load_signatures()

  client_read, server_write = IO.pipe
  server_read, client_write = IO.pipe

  client_reader = LanguageServer::Protocol::Transport::Io::Reader.new(client_read)
  client_writer = LanguageServer::Protocol::Transport::Io::Writer.new(client_write)

  server_reader = LanguageServer::Protocol::Transport::Io::Reader.new(server_read)
  server_writer = LanguageServer::Protocol::Transport::Io::Writer.new(server_write)

  interaction_worker = Server::InteractionWorker.spawn_worker(:interaction, name: "interaction", steepfile: project.steepfile_path)
  signature_worker = Server::WorkerProcess.spawn_worker(:signature, name: "signature", steepfile: project.steepfile_path)
  code_workers = Server::WorkerProcess.spawn_code_workers(steepfile: project.steepfile_path)

  master = Server::Master.new(
    project: project,
    reader: server_reader,
    writer: server_writer,
    interaction_worker: interaction_worker,
    signature_worker: signature_worker,
    code_workers: code_workers
  )

  main_thread = Thread.start do
    master.start()
  end
  main_thread.abort_on_exception = true

  client_writer.write(method: "initialize", id: 0)

  Steep.logger.info "Watching #{dirs.join(", ")}..."
  listener = Listen.to(*dirs.map(&:to_s)) do |modified, added, removed|
    stdout.puts "🔬 Type checking updated files..."

    version = Time.now.to_i
    Steep.logger.tagged "watch" do
      Steep.logger.info "Received file system updates: modified=[#{modified.join(",")}], added=[#{added.join(",")}], removed=[#{removed.join(",")}]"

      (modified + added).each do |path|
        client_writer.write(
          method: "textDocument/didChange",
          params: {
            textDocument: {
              uri: "file://#{path}",
              version: version
            },
            contentChanges: [
              {
                text: Pathname(path).read
              }
            ]
          }
        )
      end

      removed.each do |path|
        client_writer.write(
          method: "textDocument/didChange",
          params: {
            textDocument: {
              uri: "file://#{path}",
              version: version
            },
            contentChanges: [
              {
                text: ""
              }
            ]
          }
        )
      end
    end
  end.tap(&:start)

  begin
    stdout.puts "👀 Watching directories, Ctrl-C to stop."
    client_reader.read do |response|
      case response[:method]
      when "textDocument/publishDiagnostics"
        uri = URI.parse(response[:params][:uri])
        path = project.relative_path(Pathname(uri.path))

        diagnostics = response[:params][:diagnostics]

        unless diagnostics.empty?
          diagnostics.each do |diagnostic|
            start = diagnostic[:range][:start]
            loc = "#{start[:line]+1}:#{start[:character]}"
            message = diagnostic[:message].chomp.lines.join("  ")

            stdout.puts "#{path}:#{loc}: #{message}"
          end
        end
      end
    end
  rescue Interrupt
    stdout.puts "Shutting down workers..."
    client_writer.write({ method: :shutdown, id: 10000 })
    client_writer.write({ method: :exit })
    client_writer.io.close()
  end

  listener.stop
  main_thread.join

  0
end