Class: Steep::Services::SignatureService

Inherits:
Object
  • Object
show all
Defined in:
lib/steep/services/signature_service.rb

Defined Under Namespace

Classes: AncestorErrorStatus, LoadedStatus, SyntaxErrorStatus

Constant Summary collapse

FileStatus =
_ = Struct.new(:path, :content, :signature, keyword_init: true)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env:, implicitly_returns_nil:) ⇒ SignatureService

Returns a new instance of SignatureService.



83
84
85
86
87
# File 'lib/steep/services/signature_service.rb', line 83

def initialize(env:, implicitly_returns_nil:)
  builder = RBS::DefinitionBuilder.new(env: env)
  @status = LoadedStatus.new(builder: builder, files: {}, implicitly_returns_nil: implicitly_returns_nil)
  @implicitly_returns_nil = implicitly_returns_nil
end

Instance Attribute Details

#implicitly_returns_nilObject (readonly)

Returns the value of attribute implicitly_returns_nil.



81
82
83
# File 'lib/steep/services/signature_service.rb', line 81

def implicitly_returns_nil
  @implicitly_returns_nil
end

#statusObject (readonly)

Returns the value of attribute status.



4
5
6
# File 'lib/steep/services/signature_service.rb', line 4

def status
  @status
end

Class Method Details

.load_from(loader, implicitly_returns_nil:) ⇒ Object



89
90
91
92
# File 'lib/steep/services/signature_service.rb', line 89

def self.load_from(loader, implicitly_returns_nil:)
  env = RBS::Environment.from_loader(loader).resolve_type_names
  new(env: env, implicitly_returns_nil: implicitly_returns_nil)
end

Instance Method Details

#add_descendants(graph:, names:, set:) ⇒ Object



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/steep/services/signature_service.rb', line 384

def add_descendants(graph:, names:, set:)
  set.merge(names)
  names.each do |name|
    case
    when name.interface?
      graph.each_descendant(RBS::AncestorGraph::InstanceNode.new(type_name: name)) do |node|
        set << node.type_name
      end
    when name.class?
      graph.each_descendant(RBS::AncestorGraph::InstanceNode.new(type_name: name)) do |node|
        set << node.type_name
      end
      graph.each_descendant(RBS::AncestorGraph::SingletonNode.new(type_name: name)) do |node|
        set << node.type_name
      end
    end
  end
end

#add_nested_decls(env:, names:, set:) ⇒ Object



403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# File 'lib/steep/services/signature_service.rb', line 403

def add_nested_decls(env:, names:, set:)
  tops = names.each_with_object(Set[]) do |name, tops|
    unless name.namespace.empty?
      tops << name.namespace.path[0]
    end
  end

  env.class_decls.each_key do |name|
    unless name.namespace.empty?
      if tops.include?(name.namespace.path[0])
        set << name
      end
    end
  end

  env.interface_decls.each_key do |name|
    unless name.namespace.empty?
      if tops.include?(name.namespace.path[0])
        set << name
      end
    end
  end
end

#apply_changes(files, changes) ⇒ Object



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
# File 'lib/steep/services/signature_service.rb', line 154

def apply_changes(files, changes)
  Steep.logger.tagged "#apply_changes" do
    Steep.measure2 "Applying change" do |sampler|
      changes.each.with_object({}) do |pair, update|  # $ Hash[Pathname, FileStatus]
        path, cs = pair
        sampler.sample "#{path}" do
          old_text = files[path]&.content
          content = cs.inject(old_text || "") {|text, change| change.apply_to(text) }

          content ||= "" # It was not clear why `content` can be `nil`, but it happens with `master_test`.
          buffer = RBS::Buffer.new(name: path, content: content)

          update[path] =
            begin
              FileStatus.new(path: path, content: content, signature: RBS::Parser.parse_signature(buffer))
            rescue ArgumentError => exn
              error = Diagnostic::Signature::UnexpectedError.new(
                message: exn.message,
                location: RBS::Location.new(buffer: buffer, start_pos: 0, end_pos: content.size)
              )
              FileStatus.new(path: path, content: content, signature: error)
            rescue RBS::ParsingError => exn
              FileStatus.new(path: path, content: content, signature: exn)
            end
        end
      end
    end
  end
end

#const_decls(paths:, env:) ⇒ Object



351
352
353
354
355
356
357
# File 'lib/steep/services/signature_service.rb', line 351

def const_decls(paths:, env:)
  env.constant_decls.filter do |_, entry|
    if location = entry.decl.location
      paths.include?(Pathname(location.buffer.name))
    end
  end
end

#current_subtypingObject



148
149
150
151
152
# File 'lib/steep/services/signature_service.rb', line 148

def current_subtyping
  if status.is_a?(LoadedStatus)
    status.subtyping
  end
end

#each_rbs_path(&block) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/steep/services/signature_service.rb', line 100

def each_rbs_path(&block)
  if block
    env_rbs_paths.each do |path|
      unless files.key?(path)
        yield path
      end
    end

    files.each_key(&block)
  else
    enum_for :each_rbs_path
  end
end

#env_rbs_pathsObject



94
95
96
97
98
# File 'lib/steep/services/signature_service.rb', line 94

def env_rbs_paths
  @env_rbs_paths ||= latest_env.buffers.each.with_object(Set[]) do |buffer, set|
    set << Pathname(buffer.name)
  end
end

#filesObject



114
115
116
# File 'lib/steep/services/signature_service.rb', line 114

def files
  status.files
end

#global_decls(paths:, env: latest_env) ⇒ Object



359
360
361
362
363
364
365
# File 'lib/steep/services/signature_service.rb', line 359

def global_decls(paths:, env: latest_env)
  env.global_decls.filter do |_, entry|
    if location = entry.decl.location
      paths.include?(Pathname(location.buffer.name))
    end
  end
end

#latest_builderObject



131
132
133
134
135
136
137
138
# File 'lib/steep/services/signature_service.rb', line 131

def latest_builder
  case status = status()
  when LoadedStatus
    status.builder
  when SyntaxErrorStatus, AncestorErrorStatus
    status.last_builder
  end
end

#latest_constant_resolverObject



144
145
146
# File 'lib/steep/services/signature_service.rb', line 144

def latest_constant_resolver
  status.constant_resolver
end

#latest_envObject



127
128
129
# File 'lib/steep/services/signature_service.rb', line 127

def latest_env
  latest_builder.env
end

#latest_rbs_indexObject



140
141
142
# File 'lib/steep/services/signature_service.rb', line 140

def latest_rbs_index
  status.rbs_index
end

#pending_changed_pathsObject



118
119
120
121
122
123
124
125
# File 'lib/steep/services/signature_service.rb', line 118

def pending_changed_paths
  case status = status()
  when LoadedStatus
    Set[]
  when SyntaxErrorStatus, AncestorErrorStatus
    Set.new(status.changed_paths)
  end
end

#rescue_rbs_error(errors) ⇒ Object



306
307
308
309
310
311
312
# File 'lib/steep/services/signature_service.rb', line 306

def rescue_rbs_error(errors)
  begin
    yield
  rescue RBS::BaseError => exn
    errors << exn
  end
end

#type_name_from_decl(decl, set:) ⇒ Object



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/steep/services/signature_service.rb', line 367

def type_name_from_decl(decl, set:)
  case decl
  when RBS::AST::Declarations::Class, RBS::AST::Declarations::Module, RBS::AST::Declarations::Interface
    set << decl.name

    decl.members.each do |member|
      if member.is_a?(RBS::AST::Declarations::Base)
        type_name_from_decl(member, set: set)
      end
    end
  when RBS::AST::Declarations::TypeAlias
    set << decl.name
  when RBS::AST::Declarations::ClassAlias, RBS::AST::Declarations::ModuleAlias
    set << decl.new_name
  end
end

#type_names(paths:, env:) ⇒ Object



341
342
343
344
345
346
347
348
349
# File 'lib/steep/services/signature_service.rb', line 341

def type_names(paths:, env:)
  env.declarations.each.with_object(Set[]) do |decl, set|
    if decl.location
      if paths.include?(Pathname(decl.location.buffer.name))
        type_name_from_decl(decl, set: set)
      end
    end
  end
end

#update(changes) ⇒ Object



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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/steep/services/signature_service.rb', line 184

def update(changes)
  Steep.logger.tagged "#update" do
    updates = apply_changes(files, changes)
    paths = Set.new(updates.each_key)
    paths.merge(pending_changed_paths)

    if updates.each_value.any? {|file| !file.signature.is_a?(Array) }
      diagnostics = [] #: Array[Diagnostic::Signature::Base]

      updates.each_value do |file|
        unless file.signature.is_a?(Array)
          diagnostic = if file.signature.is_a?(Diagnostic::Signature::Base)
                         file.signature
                       else
                         # factory is not used here because the error is a syntax error.
                         Diagnostic::Signature.from_rbs_error(file.signature, factory: _ = nil)
                       end
          diagnostics << diagnostic
        end
      end

      @status = SyntaxErrorStatus.new(
        files: self.files.merge(updates),
        diagnostics: diagnostics,
        last_builder: latest_builder,
        changed_paths: paths
      )
    else
      files = self.files.merge(updates)
      updated_files = files.slice(*paths.to_a)
      result =
        Steep.measure "#update_env with updated #{paths.size} files" do
          update_env(updated_files, paths: paths)
        end

      @status = case result
                when Array
                  AncestorErrorStatus.new(
                    changed_paths: paths,
                    last_builder: latest_builder,
                    diagnostics: result,
                    files: files
                  )
                when RBS::DefinitionBuilder::AncestorBuilder
                  builder2 = update_builder(ancestor_builder: result, paths: paths)
                  LoadedStatus.new(builder: builder2, files: files, implicitly_returns_nil: implicitly_returns_nil)
                end
    end
  end
end

#update_builder(ancestor_builder:, paths:) ⇒ Object



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
# File 'lib/steep/services/signature_service.rb', line 314

def update_builder(ancestor_builder:, paths:)
  Steep.measure "#update_builder with #{paths.size} files" do
    changed_names = Set[]

    old_definition_builder = latest_builder
    old_env = old_definition_builder.env
    old_names = type_names(paths: paths, env: old_env)
    old_ancestor_builder = old_definition_builder.ancestor_builder
    old_graph = RBS::AncestorGraph.new(env: old_env, ancestor_builder: old_ancestor_builder)
    add_descendants(graph: old_graph, names: old_names, set: changed_names)
    add_nested_decls(env: old_env, names: old_names, set: changed_names)

    new_env = ancestor_builder.env
    new_ancestor_builder = ancestor_builder
    new_names = type_names(paths: paths, env: new_env)
    new_graph = RBS::AncestorGraph.new(env: new_env, ancestor_builder: new_ancestor_builder)
    add_descendants(graph: new_graph, names: new_names, set: changed_names)
    add_nested_decls(env: new_env, names: new_names, set: changed_names)

    old_definition_builder.update(
      env: new_env,
      ancestor_builder: new_ancestor_builder,
      except: changed_names
    )
  end
end

#update_env(updated_files, paths:) ⇒ Object



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
# File 'lib/steep/services/signature_service.rb', line 235

def update_env(updated_files, paths:)
  Steep.logger.tagged "#update_env" do
    errors = [] #: Array[RBS::BaseError]
    new_decls = Set[].compare_by_identity #: Set[RBS::AST::Declarations::t]

    env =
      Steep.measure "Deleting out of date decls" do
        bufs = latest_env.buffers.select {|buf| paths.include?(buf.name) }
        latest_env.unload(Set.new(bufs))
      end

    Steep.measure "Loading new decls" do
      updated_files.each_value do |content|
        case content.signature
        when RBS::ParsingError
          errors << content.signature
        when Diagnostic::Signature::UnexpectedError
          return [content.signature]
        else
          begin
            buffer, dirs, decls = content.signature
            env.add_signature(buffer: buffer, directives: dirs, decls: decls)
            new_decls.merge(decls)
          rescue RBS::LoadingError => exn
            errors << exn
          end
        end
      end
    end

    Steep.measure "validate type params" do
      begin
        env.validate_type_params
      rescue RBS::LoadingError => exn
        errors << exn
      end
    end

    unless errors.empty?
      return errors.map {|error|
        # Factory will not be used because of the possible error types.
        Diagnostic::Signature.from_rbs_error(error, factory: _ = nil)
      }
    end

    Steep.measure "resolve type names with #{new_decls.size} top-level decls" do
      env = env.resolve_type_names(only: new_decls)
    end

    builder = RBS::DefinitionBuilder::AncestorBuilder.new(env: env)

    Steep.measure("Pre-loading one ancestors") do
      builder.env.class_decls.each_key do |type_name|
        rescue_rbs_error(errors) { builder.one_instance_ancestors(type_name) }
        rescue_rbs_error(errors) { builder.one_singleton_ancestors(type_name) }
      end
      builder.env.interface_decls.each_key do |type_name|
        rescue_rbs_error(errors) { builder.one_interface_ancestors(type_name) }
      end
    end

    unless errors.empty?
      # Builder won't be used.
      factory = AST::Types::Factory.new(builder: _ = nil)
      return errors.map {|error| Diagnostic::Signature.from_rbs_error(error, factory: factory) }
    end

    builder
  end
end