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.



177
178
179
180
181
182
# File 'lib/steep/server/type_check_controller.rb', line 177

def initialize(project:)
  @project = project
  @priority_paths = Set[]
  @changed_paths = Set[]
  @files = TargetGroupFiles.new(project)
end

Instance Attribute Details

#changed_pathsObject (readonly)

Returns the value of attribute changed_paths.



174
175
176
# File 'lib/steep/server/type_check_controller.rb', line 174

def changed_paths
  @changed_paths
end

#filesObject (readonly)

Returns the value of attribute files.



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

def files
  @files
end

#priority_pathsObject (readonly)

Returns the value of attribute priority_paths.



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

def priority_paths
  @priority_paths
end

#projectObject (readonly)

Returns the value of attribute project.



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

def project
  @project
end

Instance Method Details

#active_target?(target_group) ⇒ Boolean

Returns:

  • (Boolean)


220
221
222
223
224
225
226
# File 'lib/steep/server/type_check_controller.rb', line 220

def active_target?(target_group)
  priority_paths.any? do |path|
    if open_target = files.signature_paths.fetch(path, nil) || files.source_paths.fetch(path, nil)
      open_target == target_group
    end
  end
end

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

Yields:



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/steep/server/type_check_controller.rb', line 184

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|
      path = project.absolute_path(path)
      self.files.add_path(path)
      files[project.relative_path(path).to_s] = path.read
      if files.size > 1000
        yield files.dup
        files.clear
      end
    end
  end

  changed_paths.merge(self.files.each_project_signature_path(nil))
  changed_paths.merge(self.files.each_project_source_path(nil))

  yield files.dup unless files.empty?
end

#make_group_request(groups, progress:) ⇒ Object



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

def make_group_request(groups, progress:)
  TypeCheckController::Request.new(guid: progress.guid, progress: progress).tap do |request|
    if groups.empty?
      files.signature_paths.each do |path, target_group|
        target_group = target_group.target if target_group.is_a?(Project::Group)
        request.signature_paths << [target_group.name, path]
      end
      files.source_paths.each do |path, target_group|
        target_group = target_group.target if target_group.is_a?(Project::Group)
        request.code_paths << [target_group.name, path]
      end
    else
      group_set = groups.filter_map do |group_name|
        target_name, group_name = group_name.split(".", 2)
        target_name or raise

        target_name = target_name.to_sym
        group_name = group_name.to_sym if group_name

        if group_name
          if target = project.targets.find {|target| target.name == target_name }
            target.groups.find {|group| group.name == group_name }
          end
        else
          project.targets.find {|target| target.name == target_name }
        end
      end.to_set

      files.signature_paths.each do |path, target_group|
        if group_set.include?(target_group)
          target_group = target_group.target if target_group.is_a?(Project::Group)
          request.signature_paths << [target_group.name, path]
        end
      end
      files.source_paths.each do |path, target_group|
        if group_set.include?(target_group)
          target_group = target_group.target if target_group.is_a?(Project::Group)
          request.code_paths << [target_group.name, path]
        end
      end
    end
  end
end

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



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/steep/server/type_check_controller.rb', line 301

def make_request(guid: SecureRandom.uuid, include_unchanged: false, progress:)
  TypeCheckController::Request.new(guid: guid, progress: progress).tap do |request|
    if include_unchanged
      files.signature_paths.each do |path, target_group|
        target_group = target_group.target if target_group.is_a?(Project::Group)
        request.signature_paths << [target_group.name, path]
      end
      files.source_paths.each do |path, target_group|
        target_group = target_group.target if target_group.is_a?(Project::Group)
        request.code_paths << [target_group.name, path]
      end
    else
      changed_paths.each do |path|
        if target_group = files.signature_paths.fetch(path, nil)
          case target_group
          when Project::Group
            target = target_group.target

            files.each_group_signature_path(target_group) do |path|
              request.signature_paths << [target.name, path]
            end

            files.each_group_source_path(target_group) do |path|
              request.code_paths << [target.name, path]
            end
          when Project::Target
            files.each_target_signature_path(target_group, nil) do |path|
              request.signature_paths << [target_group.name, path]
            end

            files.each_target_source_path(target_group, nil) do |path|
              request.code_paths << [target_group.name, path]
            end
          end
        end

        if target = files.source_path_target(path)
          request.code_paths << [target.name, path]
        end
      end

      unless request.signature_paths.empty?
        non_unref_targets = project.targets.reject { _1.unreferenced }.map(&:name).to_set
        if request.signature_paths.any? {|target_name, _| non_unref_targets.include?(target_name) }
          priority_paths.each do |path|
            if target = files.signature_path_target(path)
              request.signature_paths << [target.name, path]
              request.priority_paths << path
            end
            if target = files.source_path_target(path)
              request.code_paths << [target.name, path]
              request.priority_paths << path
            end
          end
        end
      end
    end

    changed_paths.clear()

    return nil if request.empty?
  end
end

#push_changes(path) ⇒ Object



212
213
214
215
216
217
218
# File 'lib/steep/server/type_check_controller.rb', line 212

def push_changes(path)
  return if files.library_path?(path)

  if files.add_path(path)
    changed_paths << path
  end
end

#push_changes_for_target(target_group) ⇒ Object



228
229
230
231
232
233
234
235
236
# File 'lib/steep/server/type_check_controller.rb', line 228

def push_changes_for_target(target_group)
  files.each_group_signature_path(target_group) do |path|
    push_changes path
  end

  files.each_group_source_path(target_group) do |path|
    push_changes path
  end
end

#update_priority(open: nil, close: nil) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/steep/server/type_check_controller.rb', line 238

def update_priority(open: nil, close: nil)
  path = open || close or raise

  return if files.library_path?(path)
  files.add_path(path)

  case
  when open
    target_group = files.signature_paths.fetch(path, nil) || files.source_paths.fetch(path, nil) or return

    unless active_target?(target_group)
      push_changes_for_target(target_group)
    end
    priority_paths << path
  when close
    priority_paths.delete path
  end
end