Class: Steep::Server::TypeCheckController

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

Defined Under Namespace

Classes: Request

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project:) ⇒ TypeCheckController

Returns a new instance of TypeCheckController.



197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/steep/server/type_check_controller.rb', line 197

def initialize(project:)
  @project = project

  @files = TargetGroupFiles.new(project)
  @open_paths = Set[]
  @active_groups = Set[].compare_by_identity
  @new_active_groups = Set[].compare_by_identity
  @dirty_code_paths = Set[]
  @dirty_signature_paths = Set[]
  @dirty_inline_paths = Set[]
  @inline_path_changes = InlineSourceChangeDetector.new
end

Instance Attribute Details

#active_groupsObject (readonly)

Returns the value of attribute active_groups.



191
192
193
# File 'lib/steep/server/type_check_controller.rb', line 191

def active_groups
  @active_groups
end

#dirty_code_pathsObject (readonly)

Returns the value of attribute dirty_code_paths.



193
194
195
# File 'lib/steep/server/type_check_controller.rb', line 193

def dirty_code_paths
  @dirty_code_paths
end

#dirty_inline_pathsObject (readonly)

Returns the value of attribute dirty_inline_paths.



193
194
195
# File 'lib/steep/server/type_check_controller.rb', line 193

def dirty_inline_paths
  @dirty_inline_paths
end

#dirty_signature_pathsObject (readonly)

Returns the value of attribute dirty_signature_paths.



193
194
195
# File 'lib/steep/server/type_check_controller.rb', line 193

def dirty_signature_paths
  @dirty_signature_paths
end

#filesObject (readonly)

Returns the value of attribute files.



194
195
196
# File 'lib/steep/server/type_check_controller.rb', line 194

def files
  @files
end

#inline_path_changesObject (readonly)

Returns the value of attribute inline_path_changes.



195
196
197
# File 'lib/steep/server/type_check_controller.rb', line 195

def inline_path_changes
  @inline_path_changes
end

#new_active_groupsObject (readonly)

Returns the value of attribute new_active_groups.



192
193
194
# File 'lib/steep/server/type_check_controller.rb', line 192

def new_active_groups
  @new_active_groups
end

#open_pathsObject (readonly)

Returns the value of attribute open_paths.



190
191
192
# File 'lib/steep/server/type_check_controller.rb', line 190

def open_paths
  @open_paths
end

#projectObject (readonly)

Returns the value of attribute project.



189
190
191
# File 'lib/steep/server/type_check_controller.rb', line 189

def project
  @project
end

Instance Method Details

#active_group?(group) ⇒ Boolean

Returns:

  • (Boolean)


315
316
317
# File 'lib/steep/server/type_check_controller.rb', line 315

def active_group?(group)
  active_groups.include?(group)
end

#add_dirty_code_path(path) ⇒ Object



257
258
259
260
261
262
263
# File 'lib/steep/server/type_check_controller.rb', line 257

def add_dirty_code_path(path)
  return if files.library_path?(path)
  if code_path?(path)
    files.add_path(path)
    dirty_code_paths << path
  end
end

#add_dirty_inline_path(path, update) ⇒ Object



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/steep/server/type_check_controller.rb', line 273

def add_dirty_inline_path(path, update)
  return if files.library_path?(path)
  if inline_path?(path)
    files.add_path(path)
    dirty_inline_paths << path

    unless inline_path_changes.has_source?(path)
      inline_path_changes.add_source(path, "")
    end

    if update.is_a?(String)
      inline_path_changes.replace_source(path, update)
    else
      inline_path_changes.accumulate_change(path, update)
    end
  end
end

#add_dirty_signature_path(path) ⇒ Object



265
266
267
268
269
270
271
# File 'lib/steep/server/type_check_controller.rb', line 265

def add_dirty_signature_path(path)
  return if files.library_path?(path)
  if signature_path?(path)
    files.add_path(path)
    dirty_signature_paths << path
  end
end

#close_path(path) ⇒ Object



355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/steep/server/type_check_controller.rb', line 355

def close_path(path)
  if open_paths.include?(path)
    closed_path_group = group_of(path) or raise

    open_paths.delete(path)

    if open_paths.none? { group_of(_1) == closed_path_group }
      active_groups.delete(closed_path_group)
      new_active_groups.delete(closed_path_group)
    end
  end
end

#code_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


240
241
242
# File 'lib/steep/server/type_check_controller.rb', line 240

def code_path?(path)
  files.source_paths.registered_path?(path) || project.target_for_source_path(path) != nil
end

#each_active_group(&block) ⇒ Object



319
320
321
322
323
324
325
# File 'lib/steep/server/type_check_controller.rb', line 319

def each_active_group(&block)
  if block
    active_groups.each(&block)
  else
    enum_for(_ = __method__)
  end
end

#each_dirty_path(&block) ⇒ Object



305
306
307
308
309
310
311
312
313
# File 'lib/steep/server/type_check_controller.rb', line 305

def each_dirty_path(&block)
  if block
    dirty_code_paths.each(&block)
    dirty_signature_paths.each(&block)
    dirty_inline_paths.each(&block)
  else
    enum_for(:each_dirty_path)
  end
end

#group_of(path) ⇒ Object



368
369
370
371
372
# File 'lib/steep/server/type_check_controller.rb', line 368

def group_of(path)
  files.signature_paths[path] ||
    files.source_paths[path] ||
    files.inline_paths[path]
end

#inline_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


248
249
250
251
252
253
254
255
# File 'lib/steep/server/type_check_controller.rb', line 248

def inline_path?(path)
  Steep.logger.debug { {
    path: path,
    inline: files.inline_paths.paths.map(&:to_s),
    target: project.target_for_inline_source_path(path)&.name
  }.inspect }
  files.inline_paths.registered_path?(path) || project.target_for_inline_source_path(path) != nil
end

#load(command_line_args:) {|files.dup| ... } ⇒ Object

Yields:



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
# File 'lib/steep/server/type_check_controller.rb', line 210

def load(command_line_args:)
  loader = Services::FileLoader.new(base_dir: project.base_dir)

  project.targets.each do |target|
    signature_service = Services::SignatureService.load_from(target.new_env_loader(), implicitly_returns_nil: target.implicitly_returns_nil)
    files.add_library_path(target, *signature_service.env_rbs_paths.to_a)
  end

  files = {} #: Hash[String, String]

  project.targets.each do |target|
    loader.each_path_in_target(target, command_line_args) do |path|
      absolute_path = project.absolute_path(path)
      self.files.add_path(absolute_path)
      content = absolute_path.read
      files[path.to_s] = content
      if files.size > 1000
        yield files.dup
        files.clear
      end

      if inline_path?(path)
        inline_path_changes.add_source(path, content)
      end
    end
  end

  yield files.dup unless files.empty?
end

#make_all_request(guid: SecureRandom.uuid, progress:) ⇒ Object



421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'lib/steep/server/type_check_controller.rb', line 421

def make_all_request(guid: SecureRandom.uuid, progress:)
  TypeCheckController::Request.new(guid: guid, progress: progress).tap do |request|
    files.signature_paths.each do |path, target|
      request.signature_paths << [target.name, path]
    end
    files.source_paths.each do |path, target|
      request.code_paths << [target.name, path]
    end
    files.inline_paths.each do |path, target|
      request.inline_paths << [target.name, path]
    end

    request.priority_paths.merge(open_paths)

    reset()
  end
end

#make_group_request(groups, guid: SecureRandom.uuid, progress:) ⇒ Object



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
# File 'lib/steep/server/type_check_controller.rb', line 384

def make_group_request(groups, guid: SecureRandom.uuid, progress:)
  TypeCheckController::Request.new(guid: progress.guid, progress: progress).tap do |request|
    raise "At least one group/target must be specified" if groups.empty?

    name_group_map = {} #: Hash[String, group]

    project.targets.each do |target|
      name_group_map[target.name.to_s] = target

      target.groups.each do |group|
        name_group_map["#{target.name}.#{group.name}"] = group
      end
    end

    groups.each do |group|
      type_check_group = name_group_map.fetch(group)

      new_active_groups.delete(type_check_group)

      files.signature_paths.each_group_path(type_check_group) do |path, target|
        request.signature_paths << [target.name, path]
        dirty_signature_paths.delete(path)
      end
      files.inline_paths.each_group_path(type_check_group) do |path, target|
        request.inline_paths << [target.name, path]
        dirty_inline_paths.delete(path)
      end
      files.source_paths.each_group_path(type_check_group) do |path, target|
        request.code_paths << [target.name, path]
        dirty_code_paths.delete(path)
      end
    end

    request.priority_paths.merge(open_paths)
  end
end

#make_request(guid: SecureRandom.uuid, progress:) ⇒ Object



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
# File 'lib/steep/server/type_check_controller.rb', line 439

def make_request(guid: SecureRandom.uuid, progress:)
  TypeCheckController::Request.new(guid: guid, progress: progress).tap do |request|
    code_paths = Set[] #: Set[[group, Pathname]]
    signature_paths = Set[] #: Set[[group, Pathname]]
    inline_paths = Set[] #: Set[[group, Pathname]]

    dirty_code_paths.each do |path|
      group = files.source_paths[path] or raise "#{path} is not a code path"
      code_paths << [group, path]
    end

    dirty_signature_paths.each do |path|
      group = files.signature_paths[path] or raise "#{path} is not a signature path"
      signature_paths << [group, path]
    end

    dirty_inline_paths.each do |path|
      group = files.inline_paths[path] or raise "#{path} is not an inline path"
      inline_paths << [group, path]
    end

    signature_updated_groups = Set[] #: Set[group]

    signature_paths.each do |group, path|
      signature_updated_groups << group
    end

    updated_inline_paths = inline_path_changes.type_updated_paths(dirty_inline_paths)
    inline_paths.each do |group, path|
      if updated_inline_paths.include?(path)
        signature_updated_groups << group
      end
    end

    type_check_groups = Set[] #: Set[group]

    type_check_groups.merge(signature_updated_groups)

    unless signature_updated_groups.all? { unreferenced?(_1) }
      type_check_groups.merge(active_groups)
    end

    type_check_groups.merge(new_active_groups)

    type_check_groups.each do |group|
      files.signature_paths.each_group_path(group) do |path, target|
        signature_paths << [target, path]
      end
      files.inline_paths.each_group_path(group) do |path, target|
        inline_paths << [target, path]
      end
      files.source_paths.each_group_path(group) do |path, target|
        code_paths << [target, path]
      end
    end

    signature_paths.each do |_, path|
      target = target_of(path) or raise
      request.signature_paths << [target.name, path]
    end

    inline_paths.each do |_, path|
      target = target_of(path) or raise
      request.inline_paths << [target.name, path]
    end

    code_paths.each do |_, path|
      target = target_of(path) or raise
      request.code_paths << [target.name, path]
    end

    request.priority_paths.merge(open_paths)

    reset()

    return nil if request.empty?
  end
end

#open_inline_path(path, content) ⇒ Object



291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/steep/server/type_check_controller.rb', line 291

def open_inline_path(path, content)
  return if files.library_path?(path)

  if inline_path?(path)
    open_path(path)

    if inline_path_changes.has_source?(path)
      inline_path_changes.replace_source(path, content)
    else
      inline_path_changes.add_source(path, content)
    end
  end
end

#open_path(path) ⇒ Object



340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/steep/server/type_check_controller.rb', line 340

def open_path(path)
  return if open_paths.include?(path)

  files.add_path(path) or return

  if group = group_of(path)
    open_paths << path

    unless active_groups.include?(group)
      new_active_groups << group
      active_groups << group
    end
  end
end

#resetObject



332
333
334
335
336
337
338
# File 'lib/steep/server/type_check_controller.rb', line 332

def reset()
  dirty_code_paths.clear()
  dirty_signature_paths.clear()
  dirty_inline_paths.clear()
  new_active_groups.clear()
  inline_path_changes.reset
end

#signature_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


244
245
246
# File 'lib/steep/server/type_check_controller.rb', line 244

def signature_path?(path)
  files.signature_paths.registered_path?(path) || project.target_for_signature_path(path) != nil
end

#target_of(path) ⇒ Object



374
375
376
377
378
379
380
381
382
# File 'lib/steep/server/type_check_controller.rb', line 374

def target_of(path)
  if group = group_of(path)
    if group.is_a?(Project::Group)
      group.target
    else
      group
    end
  end
end

#unreferenced?(group) ⇒ Boolean

Returns:

  • (Boolean)


327
328
329
330
# File 'lib/steep/server/type_check_controller.rb', line 327

def unreferenced?(group)
  group = group.target if group.is_a?(Project::Group)
  group.unreferenced
end