Class: Steep::Drivers::Checkfile

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

Constant Summary collapse

LSP =
LanguageServer::Protocol

Instance Attribute Summary collapse

Attributes included from Utils::DriverHelper

#disable_install_collection, #steepfile

Instance Method Summary collapse

Methods included from Utils::DriverHelper

#install_collection, #keep_diagnostic?, #load_config, #request_id, #shutdown_exit, #wait_for_message, #wait_for_response_id

Constructor Details

#initialize(stdout:, stderr:) ⇒ Checkfile

Returns a new instance of Checkfile.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/steep/drivers/checkfile.rb', line 15

def initialize(stdout:, stderr:)
  @stdout = stdout
  @stderr = stderr
  @command_line_args = []

  @all_rbs = false
  @all_ruby = false
  @stdin_input = {}

  @jobs_option = Utils::JobsOption.new()
end

Instance Attribute Details

#all_rbsObject

Returns the value of attribute all_rbs.



9
10
11
# File 'lib/steep/drivers/checkfile.rb', line 9

def all_rbs
  @all_rbs
end

#all_rubyObject

Returns the value of attribute all_ruby.



9
10
11
# File 'lib/steep/drivers/checkfile.rb', line 9

def all_ruby
  @all_ruby
end

#command_line_argsObject (readonly)

Returns the value of attribute command_line_args.



8
9
10
# File 'lib/steep/drivers/checkfile.rb', line 8

def command_line_args
  @command_line_args
end

#jobs_optionObject (readonly)

Returns the value of attribute jobs_option.



11
12
13
# File 'lib/steep/drivers/checkfile.rb', line 11

def jobs_option
  @jobs_option
end

#stderrObject (readonly)

Returns the value of attribute stderr.



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

def stderr
  @stderr
end

#stdin_inputObject (readonly)

Returns the value of attribute stdin_input.



10
11
12
# File 'lib/steep/drivers/checkfile.rb', line 10

def stdin_input
  @stdin_input
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



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

def stdout
  @stdout
end

Instance Method Details

#runObject



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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/steep/drivers/checkfile.rb', line 27

def run
  return 0 if command_line_args.empty? && !all_rbs && !all_ruby && stdin_input.empty?

  project = load_config()

  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)

  # @type var target_paths: Set[Pathname]
  target_paths = Set[]
  # @type var signature_paths: Set[Pathname]
  signature_paths = Set[]

  loader = Services::FileLoader.new(base_dir: project.base_dir)
  project.targets.each do |target|
    ruby_patterns =
      case
      when all_ruby
        []
      when command_line_args.empty?
        nil
      else
        command_line_args
      end #: Array[String]?

    if ruby_patterns
      loader.each_path_in_patterns(target.source_pattern, ruby_patterns) do |path|
        target_paths << path
      end
    end

    rbs_patterns =
      case
      when all_rbs
        []
      when command_line_args.empty?
        nil
      else
        command_line_args
      end #: Array[String]

    if rbs_patterns
      loader.each_path_in_patterns(target.signature_pattern, rbs_patterns) do |path|
        signature_paths << path
      end
    end
  end

  stdin_input.each_key do |path|
    case
    when project.target_for_signature_path(path)
      signature_paths << path
    when project.target_for_source_path(path)
      target_paths << path
    end
  end

  files = target_paths + signature_paths

  count =
    if jobs_option.jobs_count
      jobs_option.jobs_count
    else
      [
        files.size + 2,
        jobs_option.default_jobs_count
      ].min || raise
    end

  Steep.logger.info { "Starting #{count} workers for #{files.size} files..." }

  typecheck_workers = Server::WorkerProcess.start_typecheck_workers(
    steepfile: project.steepfile_path,
    args: [],
    delay_shutdown: true,
    steep_command: jobs_option.steep_command,
    count: count
  )

  master = Server::Master.new(
    project: project,
    reader: server_reader,
    writer: server_writer,
    interaction_worker: nil,
    typecheck_workers: typecheck_workers
  )
  master.typecheck_automatically = false

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

  Steep.logger.info { "Initializing server" }
  initialize_id = request_id()
  client_writer.write({ method: :initialize, id: initialize_id, params: DEFAULT_CLI_LSP_INITIALIZE_PARAMS })
  wait_for_response_id(reader: client_reader, id: initialize_id)

  stdin_input.each do |path, content|
    uri = PathHelper.to_uri(project.absolute_path(path))

    master.broadcast_notification(
      {
        method: "textDocument/didChange",
        params: {
        textDocument: { uri: uri, version: 0 },
        contentChanges: [{ text: content }]
        }
      }
    )
    master.broadcast_notification(
      {
        method: "textDocument/didSave",
        params: {
          textDocument: { uri: uri }
        }
      }
    )
  end

  ping_guid = master.fresh_request_id()
  client_writer.write({ method: "$/ping", id: ping_guid, params: {}})
  wait_for_response_id(reader: client_reader, id: ping_guid)

  request_guid = master.fresh_request_id()
  progress = master.work_done_progress(request_guid)
  request = Server::TypeCheckController::Request.new(guid: request_guid, progress: progress)

  target_paths.each do |path|
    target = project.target_for_source_path(path) or next
    request.code_paths << [target.name, project.absolute_path(path)]
  end
  signature_paths.each do |path|
    target = project.target_for_signature_path(path) or next
    request.signature_paths << [target.name, project.absolute_path(path)]
  end

  request.needs_response = true
  master.start_type_check(request: request, last_request: nil, report_progress_threshold: 0)

  Steep.logger.info { "Starting type checking: #{request_guid}" }

  error_messages = [] #: Array[String]
  client_reader.read do |response|
    Steep.logger.info { response.inspect }
    case
    when response[:method] == "textDocument/publishDiagnostics"
      params = response[:params]

      if path = PathHelper.to_pathname(params[:uri])
        stdout.puts(
          {
            path: project.relative_path(path).to_s,
            diagnostics: params[:diagnostics]
          }.to_json
        )
      end
    when response[:method] == "window/showMessage"
      # Assuming ERROR message means unrecoverable error.
      message = response[:params]
      if message[:type] == LSP::Constant::MessageType::ERROR
        error_messages << message[:message]
      end
    when response[:id] == request_guid
      break
    end
  end

  Steep.logger.info { "Shutting down..." }

  shutdown_exit(reader: client_reader, writer: client_writer)
  main_thread.join()

  0
end