Class: Steep::Server::Master

Inherits:
Object
  • Object
show all
Defined in:
lib/steep/server/master.rb

Defined Under Namespace

Modules: MessageUtils Classes: GroupHandler, ResultController, ResultHandler, SendMessageJob

Constant Summary collapse

LSP =
LanguageServer::Protocol
ReceiveMessageJob =
_ = Struct.new(:source, :message, keyword_init: true) do
  # @implements ReceiveMessageJob

  def response?
    message.key?(:id) && !message.key?(:method)
  end

  include MessageUtils
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project:, reader:, writer:, interaction_worker:, typecheck_workers:, queue: Queue.new) ⇒ Master

Returns a new instance of Master.



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/steep/server/master.rb', line 188

def initialize(project:, reader:, writer:, interaction_worker:, typecheck_workers:, queue: Queue.new)
  @project = project
  @reader = reader
  @writer = writer
  @interaction_worker = interaction_worker
  @typecheck_workers = typecheck_workers
  @current_type_check_request = nil
  @typecheck_automatically = true
  @commandline_args = []
  @job_queue = queue
  @write_queue = SizedQueue.new(100)

  @controller = TypeCheckController.new(project: project)
  @result_controller = ResultController.new()
  @start_type_checking_queue = DelayQueue.new(delay: 0.3)
end

Instance Attribute Details

#commandline_argsObject (readonly)

Returns the value of attribute commandline_args.



173
174
175
# File 'lib/steep/server/master.rb', line 173

def commandline_args
  @commandline_args
end

#controllerObject (readonly)

Returns the value of attribute controller.



181
182
183
# File 'lib/steep/server/master.rb', line 181

def controller
  @controller
end

#current_type_check_requestObject (readonly)

Returns the value of attribute current_type_check_request.



180
181
182
# File 'lib/steep/server/master.rb', line 180

def current_type_check_request
  @current_type_check_request
end

#initialize_paramsObject (readonly)

Returns the value of attribute initialize_params.



184
185
186
# File 'lib/steep/server/master.rb', line 184

def initialize_params
  @initialize_params
end

#interaction_workerObject (readonly)

Returns the value of attribute interaction_worker.



175
176
177
# File 'lib/steep/server/master.rb', line 175

def interaction_worker
  @interaction_worker
end

#job_queueObject (readonly)

Returns the value of attribute job_queue.



178
179
180
# File 'lib/steep/server/master.rb', line 178

def job_queue
  @job_queue
end

#projectObject (readonly)

Returns the value of attribute project.



171
172
173
# File 'lib/steep/server/master.rb', line 171

def project
  @project
end

#readerObject (readonly)

Returns the value of attribute reader.



172
173
174
# File 'lib/steep/server/master.rb', line 172

def reader
  @reader
end

#result_controllerObject (readonly)

Returns the value of attribute result_controller.



182
183
184
# File 'lib/steep/server/master.rb', line 182

def result_controller
  @result_controller
end

#start_type_checking_queueObject (readonly)

Returns the value of attribute start_type_checking_queue.



186
187
188
# File 'lib/steep/server/master.rb', line 186

def start_type_checking_queue
  @start_type_checking_queue
end

#typecheck_automaticallyObject

Returns the value of attribute typecheck_automatically.



185
186
187
# File 'lib/steep/server/master.rb', line 185

def typecheck_automatically
  @typecheck_automatically
end

#typecheck_workersObject (readonly)

Returns the value of attribute typecheck_workers.



176
177
178
# File 'lib/steep/server/master.rb', line 176

def typecheck_workers
  @typecheck_workers
end

#write_queueObject (readonly)

Returns the value of attribute write_queue.



178
179
180
# File 'lib/steep/server/master.rb', line 178

def write_queue
  @write_queue
end

#writerObject (readonly)

Returns the value of attribute writer.



172
173
174
# File 'lib/steep/server/master.rb', line 172

def writer
  @writer
end

Instance Method Details

#assign_initialize_params(params) ⇒ Object



329
330
331
# File 'lib/steep/server/master.rb', line 329

def assign_initialize_params(params)
  @initialize_params = params
end

#broadcast_notification(message) ⇒ Object



802
803
804
805
806
807
# File 'lib/steep/server/master.rb', line 802

def broadcast_notification(message)
  Steep.logger.info "Broadcasting notification #{message[:method]}"
  each_worker do |worker|
    enqueue_write_job SendMessageJob.new(dest: worker, message: message)
  end
end

#each_worker(&block) ⇒ Object



316
317
318
319
320
321
322
323
# File 'lib/steep/server/master.rb', line 316

def each_worker(&block)
  if block
    yield interaction_worker if interaction_worker
    typecheck_workers.each(&block)
  else
    enum_for :each_worker
  end
end

#enqueue_write_job(job) ⇒ Object



841
842
843
844
# File 'lib/steep/server/master.rb', line 841

def enqueue_write_job(job)
  Steep.logger.info { "Write_queue has #{write_queue.size} items"}
  write_queue.push(job) # steep:ignore InsufficientKeywordArguments
end

#file_system_watcher_supported?Boolean

Returns:

  • (Boolean)


338
339
340
341
# File 'lib/steep/server/master.rb', line 338

def file_system_watcher_supported?
  initialize_params or raise "`initialize` request is not receiged yet"
  initialize_params.dig(:capabilities, :workspace, :didChangeWatchedFiles, :dynamicRegistration) || false
end

#finish_type_check(request) ⇒ Object



706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
# File 'lib/steep/server/master.rb', line 706

def finish_type_check(request)
  request.work_done_progress.end()

  finished_at = Time.now
  duration = finished_at - request.started_at

  if request.needs_response
    enqueue_write_job(
      SendMessageJob.to_client(
        message: CustomMethods::TypeCheck.response(
          request.guid,
          {
            guid: request.guid,
            completed: request.finished?,
            started_at: request.started_at.iso8601,
            finished_at: finished_at.iso8601,
            duration: duration.to_i
          }
        )
      )
    )
  else
    Steep.logger.debug { "Skip sending response to #{CustomMethods::TypeCheck::METHOD} request" }
  end
end

#fresh_request_idObject



814
815
816
# File 'lib/steep/server/master.rb', line 814

def fresh_request_id
  SecureRandom.alphanumeric(10)
end

#group_requestObject



829
830
831
832
833
# File 'lib/steep/server/master.rb', line 829

def group_request()
  GroupHandler.new().tap do |group|
    yield group
  end
end

#killObject



835
836
837
838
839
# File 'lib/steep/server/master.rb', line 835

def kill
  each_worker do |worker|
    worker.kill
  end
end

#on_type_check_update(guid:, path:, target:, diagnostics:) ⇒ Object



782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
# File 'lib/steep/server/master.rb', line 782

def on_type_check_update(guid:, path:, target:, diagnostics:)
  if current = current_type_check_request()
    if current.guid == guid
      current.checked(path, target)

      Steep.logger.info { "Request updated: checked=#{path}, unchecked=#{current.each_unchecked_code_target_path.size}, diagnostics=#{diagnostics&.size}" }

      percentage = current.percentage
      current.work_done_progress.report(percentage, "#{current.checked_paths.size}/#{current.total}") if current.report_progress

      push_diagnostics(path, diagnostics)

      if current.finished?
        finish_type_check(current)
        @current_type_check_request = nil
      end
    end
  end
end

#pathname(uri) ⇒ Object



325
326
327
# File 'lib/steep/server/master.rb', line 325

def pathname(uri)
  Steep::PathHelper.to_pathname(uri)
end

#paths_to_watch(pattern, extname:) ⇒ Object



906
907
908
909
910
911
912
913
914
915
916
917
918
919
# File 'lib/steep/server/master.rb', line 906

def paths_to_watch(pattern, extname:)
  result = [] #: Array[String]

  pattern.patterns.each do |pat|
    path = project.base_dir + pat
    result << path.to_s unless path.directory?
  end
  pattern.prefixes.each do |pat|
    path = project.base_dir + pat
    result << (path + "**/*#{extname}").to_s unless path.file?
  end

  result
end

#process_message_from_client(message) ⇒ Object



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
# File 'lib/steep/server/master.rb', line 343

def process_message_from_client(message)
  Steep.logger.info "Processing message from client: method=#{message[:method]}, id=#{message[:id]}"
  id = message[:id]

  case message[:method]
  when "initialize"
    assign_initialize_params(message[:params])

    result_controller << group_request do |group|
      each_worker do |worker|
        group << send_request(method: "initialize", params: message[:params], worker: worker)
      end

      group.on_completion do
        enqueue_write_job SendMessageJob.to_client(
          message: {
            id: id,
            result: LSP::Interface::InitializeResult.new(
              capabilities: LSP::Interface::ServerCapabilities.new(
                text_document_sync: LSP::Interface::TextDocumentSyncOptions.new(
                  change: LSP::Constant::TextDocumentSyncKind::INCREMENTAL,
                  open_close: true
                ),
                hover_provider: {
                  workDoneProgress: true,
                  partialResults: true,
                  partialResult: true
                },
                completion_provider: LSP::Interface::CompletionOptions.new(
                  trigger_characters: [".", "@", ":"],
                  work_done_progress: true
                ),
                signature_help_provider: {
                  triggerCharacters: ["("]
                },
                workspace_symbol_provider: true,
                definition_provider: true,
                declaration_provider: false,
                implementation_provider: true,
                type_definition_provider: true
              ),
              server_info: {
                name: "steep",
                version: VERSION
              }
            )
          }
        )

        progress = work_done_progress(SecureRandom.uuid)
        if typecheck_automatically
          progress.begin("Type checking", "loading projects...", request_id: fresh_request_id)
        end

        Steep.measure("Load files from disk...") do
          controller.load(command_line_args: commandline_args) do |input|
            input.transform_values! do |content|
              content.is_a?(String) or raise
              if content.valid_encoding?
                content
              else
                { text: Base64.encode64(content), binary: true }
              end
            end
            broadcast_notification(CustomMethods::FileLoad.notification({ content: input }))
          end
        end

        if typecheck_automatically
          progress.end()
        end

        if file_system_watcher_supported?
          setup_file_system_watcher()
        end

        controller.changed_paths.clear()

        # if typecheck_automatically
        #   if request = controller.make_request(guid: progress.guid, include_unchanged: true, progress: progress)
        #     start_type_check(request: request, last_request: nil)
        #   end
        # end
      end
    end

  when "workspace/didChangeWatchedFiles"
    updated_watched_files = [] #: Array[Pathname]

    message[:params][:changes].each do |change|
      uri = change[:uri]
      type = change[:type]

      path = PathHelper.to_pathname!(uri)

      unless controller.priority_paths.include?(path)
        updated_watched_files << path

        controller.push_changes(path)

        case type
        when LSP::Constant::FileChangeType::CREATED, LSP::Constant::FileChangeType::CHANGED
          content = path.read
        when LSP::Constant::FileChangeType::DELETED
          content = ""
        end

        content or raise

        broadcast_notification(CustomMethods::FileReset.notification({ uri: uri, content: content }))
      end
    end

    if updated_watched_files.empty?
      Steep.logger.info { "Exit from workspace/didChangeWatchedFiles notification because all of the changed files are already open" }
      return
    end

    if typecheck_automatically
      start_type_checking_queue.execute do
        job_queue.push(
          -> do
            last_request = current_type_check_request
            guid = SecureRandom.uuid

            start_type_check(
              last_request: last_request,
              progress: work_done_progress(guid),
              needs_response: false
            )
          end
        )
      end
    end

  when "textDocument/didChange"
    if path = pathname(message[:params][:textDocument][:uri])
      broadcast_notification(message)
      controller.push_changes(path)

      if typecheck_automatically
        start_type_checking_queue.execute do
          job_queue.push(
            -> do
              Steep.logger.info { "Starting type check from textDocument/didChange notification..." }

              last_request = current_type_check_request
              guid = SecureRandom.uuid

              start_type_check(
                last_request: last_request,
                progress: work_done_progress(guid),
                needs_response: false
              )
            end
          )
        end
      end
    end

  when "textDocument/didOpen"
    uri = message[:params][:textDocument][:uri]
    text = message[:params][:textDocument][:text]

    if path = pathname(uri)
      if target = project.group_for_path(path)
        controller.update_priority(open: path)
        # broadcast_notification(CustomMethods::FileReset.notification({ uri: uri, content: text }))

        start_type_checking_queue.execute do
          guid = SecureRandom.uuid
          start_type_check(last_request: current_type_check_request, progress: work_done_progress(guid), needs_response: true)
        end
      end
    end

  when "textDocument/didClose"
    if path = pathname(message[:params][:textDocument][:uri])
      controller.update_priority(close: path)
    end

  when "textDocument/hover", "textDocument/completion", "textDocument/signatureHelp"
    if interaction_worker
      if path = pathname(message[:params][:textDocument][:uri])
        result_controller << send_request(method: message[:method], params: message[:params], worker: interaction_worker) do |handler|
          handler.on_completion do |response|
            enqueue_write_job SendMessageJob.to_client(
              message: {
                id: message[:id],
                result: response[:result]
              }
            )
          end
        end
      else
        enqueue_write_job SendMessageJob.to_client(
          message: {
            id: message[:id],
            result: nil
          }
        )
      end
    end

  when "workspace/symbol"
    result_controller << group_request do |group|
      typecheck_workers.each do |worker|
        group << send_request(method: "workspace/symbol", params: message[:params], worker: worker)
      end

      group.on_completion do |handlers|
        result = handlers.flat_map(&:result)
        result.uniq!
        enqueue_write_job SendMessageJob.to_client(message: { id: message[:id], result: result })
      end
    end

  when CustomMethods::Stats::METHOD
    result_controller << group_request do |group|
      typecheck_workers.each do |worker|
        group << send_request(method: CustomMethods::Stats::METHOD, params: nil, worker: worker)
      end

      group.on_completion do |handlers|
        stats = handlers.flat_map(&:result) #: Server::CustomMethods::Stats::result
        enqueue_write_job SendMessageJob.to_client(
          message: CustomMethods::Stats.response(message[:id], stats)
        )
      end
    end

  when "textDocument/definition", "textDocument/implementation", "textDocument/typeDefinition"
    if path = pathname(message[:params][:textDocument][:uri])
      result_controller << group_request do |group|
        typecheck_workers.each do |worker|
          group << send_request(method: message[:method], params: message[:params], worker: worker)
        end

        group.on_completion do |handlers|
          links = handlers.flat_map(&:result)
          links.uniq!
          enqueue_write_job SendMessageJob.to_client(
            message: {
              id: message[:id],
              result: links
            }
          )
        end
      end
    else
      enqueue_write_job SendMessageJob.to_client(
        message: {
          id: message[:id],
          result: [] #: Array[untyped]
        }
      )
    end

  when CustomMethods::TypeCheck::METHOD
    id = message[:id]
    params = message[:params] #: CustomMethods::TypeCheck::params

    request = TypeCheckController::Request.new(guid: id, progress: work_done_progress(id))
    request.needs_response = true

    params[:code_paths].each do |target_name, path|
      request.code_paths << [target_name.to_sym, Pathname(path)]
    end
    params[:signature_paths].each do |target_name, path|
      request.signature_paths << [target_name.to_sym, Pathname(path)]
    end
    params[:library_paths].each do |target_name, path|
      request.library_paths << [target_name.to_sym, Pathname(path)]
    end

    start_type_check(request: request, last_request: nil)

  when CustomMethods::TypeCheckGroups::METHOD
    params = message[:params] #: CustomMethods::TypeCheckGroups::params

    groups = params.fetch(:groups)

    progress = work_done_progress(SecureRandom.uuid)
    progress.begin("Type checking #{groups.empty? ? "project" : groups.join(", ")}", request_id: fresh_request_id)

    request = controller.make_group_request(groups, progress: progress)
    request.needs_response = false
    start_type_check(request: request, last_request: current_type_check_request, report_progress_threshold: 0)

  when "$/ping"
    enqueue_write_job SendMessageJob.to_client(
      message: {
          id: message[:id],
          result: message[:params]
      }
    )

  when CustomMethods::Groups::METHOD
    groups = [] #: Array[String]

    project.targets.each do |target|
      unless target.source_pattern.empty? && target.signature_pattern.empty?
        groups << target.name.to_s
      end

      target.groups.each do |group|
        unless group.source_pattern.empty? && group.signature_pattern.empty?
          groups << "#{target.name}.#{group.name}"
        end
      end
    end

    enqueue_write_job(SendMessageJob.to_client(
      message: CustomMethods::Groups.response(message[:id], groups)
    ))

  when "shutdown"
    start_type_checking_queue.cancel

    result_controller << group_request do |group|
      each_worker do |worker|
        group << send_request(method: "shutdown", worker: worker)
      end

      group.on_completion do
        enqueue_write_job SendMessageJob.to_client(message: { id: message[:id], result: nil })
      end
    end

  when "exit"
    broadcast_notification(message)
  end
end

#process_message_from_worker(message, worker:) ⇒ Object



677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
# File 'lib/steep/server/master.rb', line 677

def process_message_from_worker(message, worker:)
  Steep.logger.tagged "#process_message_from_worker (worker=#{worker.name})" do
    Steep.logger.info { "Processing message from worker: method=#{message[:method] || "-"}, id=#{message[:id] || "*"}" }

    case
    when message.key?(:id) && !message.key?(:method)
      Steep.logger.tagged "response(id=#{message[:id]})" do
        Steep.logger.error { "Received unexpected response" }
        Steep.logger.debug { "result = #{message[:result].inspect}" }
      end
    when message.key?(:method) && !message.key?(:id)
      case message[:method]
      when CustomMethods::TypeCheck__Progress::METHOD
        params = message[:params] #: CustomMethods::TypeCheck__Progress::params
        target = project.targets.find {|target| target.name.to_s == params[:target] } or raise
        on_type_check_update(
          guid: params[:guid],
          path: Pathname(params[:path]),
          target: target,
          diagnostics: params[:diagnostics]
        )
      else
        # Forward other notifications
        enqueue_write_job SendMessageJob.to_client(message: message)
      end
    end
  end
end

#push_diagnostics(path, diagnostics) ⇒ Object



858
859
860
861
862
863
864
865
866
867
# File 'lib/steep/server/master.rb', line 858

def push_diagnostics(path, diagnostics)
  if diagnostics
    write_queue.push SendMessageJob.to_client(
      message: {
        method: :"textDocument/publishDiagnostics",
        params: { uri: Steep::PathHelper.to_uri(path).to_s, diagnostics: diagnostics }
      }
    )
  end
end

#send_notification(message, worker:) ⇒ Object



809
810
811
812
# File 'lib/steep/server/master.rb', line 809

def send_notification(message, worker:)
  Steep.logger.info "Sending notification #{message[:method]} to #{worker.name}"
  enqueue_write_job SendMessageJob.new(dest: worker, message: message)
end

#send_request(method:, id: fresh_request_id(), params: nil, worker:, &block) ⇒ Object



818
819
820
821
822
823
824
825
826
827
# File 'lib/steep/server/master.rb', line 818

def send_request(method:, id: fresh_request_id(), params: nil, worker:, &block)
  Steep.logger.info "Sending request #{method}(#{id}) to #{worker.name}"

  # @type var message: lsp_request
  message = { method: method, id: id, params: params }
  ResultHandler.new(request: message).tap do |handler|
    yield handler if block
    enqueue_write_job SendMessageJob.to_worker(worker, message: message)
  end
end

#setup_file_system_watcherObject



869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
# File 'lib/steep/server/master.rb', line 869

def setup_file_system_watcher()
  patterns = [] #: Array[String]

  project.targets.each do |target|
    patterns.concat(paths_to_watch(target.source_pattern, extname: ".rb"))
    patterns.concat(paths_to_watch(target.signature_pattern, extname: ".rbs"))
    target.groups.each do |group|
      patterns.concat(paths_to_watch(group.source_pattern, extname: ".rb"))
      patterns.concat(paths_to_watch(group.signature_pattern, extname: ".rbs"))
    end
  end
  patterns.sort!
  patterns.uniq!

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

  enqueue_write_job 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

#startObject



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/steep/server/master.rb', line 205

def start
  Steep.logger.tagged "master" do
    tags = Steep.logger.formatter.current_tags.dup

    # @type var worker_threads: Array[Thread]
    worker_threads = []

    if interaction_worker
      worker_threads << Thread.new do
        Steep.logger.formatter.push_tags(*tags, "from-worker@interaction")
        interaction_worker.reader.read do |message|
          job_queue << ReceiveMessageJob.new(source: interaction_worker, message: message)
        end
      end
    end

    typecheck_workers.each do |worker|
      worker_threads << Thread.new do
        Steep.logger.formatter.push_tags(*tags, "from-worker@#{worker.name}")
        worker.reader.read do |message|
          job_queue << ReceiveMessageJob.new(source: worker, message: message)
        end
      end
    end

    read_client_thread = Thread.new do
      reader.read do |message|
        job_queue << ReceiveMessageJob.new(source: :client, message: message)
        break if message[:method] == "exit"
      end
    end

    write_thread = Thread.new do
      Steep.logger.formatter.push_tags(*tags)
      Steep.logger.tagged "write" do
        while job = write_queue.deq
          # @type var job: SendMessageJob
          case job.dest
          when :client
            Steep.logger.info { "Processing SendMessageJob: dest=client, method=#{job.message[:method] || "-"}, id=#{job.message[:id] || "-"}" }
            writer.write job.message
          when WorkerProcess
            Steep.logger.info { "Processing SendMessageJob: dest=#{job.dest.name}, method=#{job.message[:method] || "-"}, id=#{job.message[:id] || "-"}" }
            job.dest << job.message
          end
        end
      end
    end

    loop_thread = Thread.new do
      Steep.logger.formatter.push_tags(*tags)
      Steep.logger.tagged "main" do
        while job = job_queue.deq
          case job
          when ReceiveMessageJob
            src = case job.source
                  when :client
                    :client
                  else
                    job.source.name
                  end
            Steep.logger.tagged("ReceiveMessageJob(#{src}/#{job.message[:method]}/#{job.message[:id]})") do
              if job.response? && result_controller.process_response(job.message)
                # nop
                Steep.logger.info { "Processed by ResultController" }
              else
                case job.source
                when :client
                  process_message_from_client(job.message)

                  if job.message[:method] == "exit"
                    job_queue.close()
                  end
                when WorkerProcess
                  process_message_from_worker(job.message, worker: job.source)
                end
              end
            end
          when Proc
            job.call()
          end
        end
      end
    end

    waiter = ThreadWaiter.new
    each_worker do |worker|
      waiter << worker.wait_thread
    end
    waiter.wait_one()

    unless job_queue.closed?
      # Exit by error
      each_worker do |worker|
        worker.kill(force: true)
      end
      raise "Unexpected worker process exit"
    end

    write_queue.close()
    write_thread.join

    read_client_thread.join()
    worker_threads.each do |thread|
      thread.join
    end

    loop_thread.join
  end
end

#start_type_check(request: nil, last_request:, progress: nil, include_unchanged: false, report_progress_threshold: 10, needs_response: nil) ⇒ Object



732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
# File 'lib/steep/server/master.rb', line 732

def start_type_check(request: nil, last_request:, progress: nil, include_unchanged: false, report_progress_threshold: 10, needs_response: nil)
  Steep.logger.tagged "#start_type_check(#{progress&.guid || request&.guid}, #{last_request&.guid}" do
    if last_request
      finish_type_check(last_request)
    end

    unless request
      progress or raise
      request = controller.make_request(guid: progress.guid, include_unchanged: include_unchanged, progress: progress) or return
      request.needs_response = needs_response ? true : false
    end

    if last_request
      request.merge!(last_request)
    end

    if request.total > report_progress_threshold
      request.report_progress!
    end

    if request.each_unchecked_target_path.to_a.empty?
      finish_type_check(request)
      @current_type_check_request = nil
      return
    end

    Steep.logger.info "Starting new progress..."

    @current_type_check_request = request

    if progress
      # If `request:` keyword arg is not given
      request.work_done_progress.begin("Type checking", request_id: fresh_request_id)
    end

    Steep.logger.info "Sending $/typecheck/start notifications"
    typecheck_workers.each do |worker|
      assignment = Services::PathAssignment.new(
        max_index: typecheck_workers.size,
        index: worker.index || raise
      )

      enqueue_write_job SendMessageJob.to_worker(
        worker,
        message: CustomMethods::TypeCheck__Start.notification(request.as_json(assignment: assignment))
      )
    end
  end
end

#work_done_progress(guid) ⇒ Object



846
847
848
849
850
851
852
853
854
855
856
# File 'lib/steep/server/master.rb', line 846

def work_done_progress(guid)
  if work_done_progress_supported?
    WorkDoneProgress.new(guid) do |message|
      enqueue_write_job SendMessageJob.to_client(message: message)
    end
  else
    WorkDoneProgress.new(guid) do |message|
      # nop
    end
  end
end

#work_done_progress_supported?Boolean

Returns:

  • (Boolean)


333
334
335
336
# File 'lib/steep/server/master.rb', line 333

def work_done_progress_supported?
  initialize_params or raise "`initialize` request is not receiged yet"
  initialize_params.dig(:capabilities, :window, :workDoneProgress) ? true : false
end