Class: Steep::Server::TypeCheckWorker

Inherits:
BaseWorker show all
Includes:
ChangeBuffer
Defined in:
lib/steep/server/type_check_worker.rb

Defined Under Namespace

Classes: GotoJob

Constant Summary collapse

WorkspaceSymbolJob =
_ = Struct.new(:query, :id, keyword_init: true)
StatsJob =
_ = Struct.new(:id, keyword_init: true)
StartTypeCheckJob =
_ = Struct.new(:guid, :changes, keyword_init: true)
TypeCheckCodeJob =
_ = Struct.new(:guid, :path, :target, keyword_init: true)
ValidateAppSignatureJob =
_ = Struct.new(:guid, :path, :target, keyword_init: true)
ValidateLibrarySignatureJob =
_ = Struct.new(:guid, :path, :target, keyword_init: true)

Constants inherited from BaseWorker

BaseWorker::LSP, BaseWorker::ShutdownJob

Instance Attribute Summary collapse

Attributes included from ChangeBuffer

#buffered_changes, #mutex

Attributes inherited from BaseWorker

#queue, #reader, #writer

Instance Method Summary collapse

Methods included from ChangeBuffer

#collect_changes, #load_files, #pop_buffer, #push_buffer, #reset_change

Methods inherited from BaseWorker

#run, #skip_job?, #skip_jobs_after_shutdown!, #skip_jobs_after_shutdown?

Constructor Details

#initialize(project:, reader:, writer:, assignment:, commandline_args:) ⇒ TypeCheckWorker

Returns a new instance of TypeCheckWorker.



54
55
56
57
58
59
60
61
62
63
# File 'lib/steep/server/type_check_worker.rb', line 54

def initialize(project:, reader:, writer:, assignment:, commandline_args:)
  super(project: project, reader: reader, writer: writer)

  @assignment = assignment
  @buffered_changes = {}
  @mutex = Mutex.new()
  @queue = Queue.new
  @commandline_args = commandline_args
  @current_type_check_guid = nil
end

Instance Attribute Details

#assignmentObject (readonly)

Returns the value of attribute assignment.



4
5
6
# File 'lib/steep/server/type_check_worker.rb', line 4

def assignment
  @assignment
end

#commandline_argsObject (readonly)

Returns the value of attribute commandline_args.



5
6
7
# File 'lib/steep/server/type_check_worker.rb', line 5

def commandline_args
  @commandline_args
end

#current_type_check_guidObject (readonly)

Returns the value of attribute current_type_check_guid.



6
7
8
# File 'lib/steep/server/type_check_worker.rb', line 6

def current_type_check_guid
  @current_type_check_guid
end

#projectObject (readonly)

Returns the value of attribute project.



4
5
6
# File 'lib/steep/server/type_check_worker.rb', line 4

def project
  @project
end

#serviceObject (readonly)

Returns the value of attribute service.



4
5
6
# File 'lib/steep/server/type_check_worker.rb', line 4

def service
  @service
end

Instance Method Details

#enqueue_typecheck_jobs(params) ⇒ Object



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

def enqueue_typecheck_jobs(params)
  guid = params[:guid]

  @current_type_check_guid = guid

  pop_buffer() do |changes|
    Steep.logger.info { "Enqueueing StartTypeCheckJob for guid=#{guid}" }
    queue << StartTypeCheckJob.new(guid: guid, changes: changes)
  end

  targets = project.targets.each.with_object({}) do |target, hash| #$ Hash[String, Project::Target]
    hash[target.name.to_s] = target
  end

  priority_paths = Set.new(params[:priority_uris].map {|uri| Steep::PathHelper.to_pathname!(uri) })
  libraries = params[:library_uris].map {|target_name, uri| [targets.fetch(target_name), Steep::PathHelper.to_pathname!(uri)] } #: Array[[Project::Target, Pathname]]
  signatures = params[:signature_uris].map {|target_name, uri| [targets.fetch(target_name), Steep::PathHelper.to_pathname!(uri)] } #: Array[[Project::Target, Pathname]]
  codes = params[:code_uris].map {|target_name, uri| [targets.fetch(target_name), Steep::PathHelper.to_pathname!(uri)] } #: Array[[Project::Target, Pathname]]

  priority_libs, non_priority_libs = libraries.partition {|_, path| priority_paths.include?(path) }
  priority_sigs, non_priority_sigs = signatures.partition {|_, path| priority_paths.include?(path) }
  priority_codes, non_priority_codes = codes.partition {|_, path| priority_paths.include?(path) }

  priority_codes.each do |target, path|
    Steep.logger.info { "Enqueueing TypeCheckCodeJob for guid=#{guid}, path=#{path}, target=#{target.name}" }
    queue << TypeCheckCodeJob.new(guid: guid, path: path, target: target)
  end

  priority_sigs.each do |target, path|
    Steep.logger.info { "Enqueueing ValidateAppSignatureJob for guid=#{guid}, path=#{path}, target=#{target.name}" }
    queue << ValidateAppSignatureJob.new(guid: guid, path: path, target: target)
  end

  priority_libs.each do |target, path|
    Steep.logger.info { "Enqueueing ValidateLibrarySignatureJob for guid=#{guid}, path=#{path}, target=#{target.name}" }
    queue << ValidateLibrarySignatureJob.new(guid: guid, path: path, target: target)
  end

  non_priority_codes.each do |target, path|
    Steep.logger.info { "Enqueueing TypeCheckCodeJob for guid=#{guid}, path=#{path}, target=#{target.name}" }
    queue << TypeCheckCodeJob.new(guid: guid, path: path, target: target)
  end

  non_priority_sigs.each do |target, path|
    Steep.logger.info { "Enqueueing ValidateAppSignatureJob for guid=#{guid}, path=#{path}, target=#{target.name}" }
    queue << ValidateAppSignatureJob.new(guid: guid, path: path, target: target)
  end

  non_priority_libs.each do |target, path|
    Steep.logger.info { "Enqueueing ValidateLibrarySignatureJob for guid=#{guid}, path=#{path}, target=#{target.name}" }
    queue << ValidateLibrarySignatureJob.new(guid: guid, path: path, target: target)
  end
end

#goto(job) ⇒ Object



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

def goto(job)
  path = Steep::PathHelper.to_pathname(job.params[:textDocument][:uri]) or return []
  line = job.params[:position][:line] + 1
  column = job.params[:position][:character]

  goto_service = Services::GotoService.new(type_check: service, assignment: assignment)
  locations =
    case
    when job.definition?
      goto_service.definition(path: path, line: line, column: column)
    when job.implementation?
      goto_service.implementation(path: path, line: line, column: column)
    when job.type_definition?
      goto_service.type_definition(path: path, line: line, column: column)
    else
      raise
    end

  locations.map do |loc|
    path =
      case loc
      when RBS::Location
        Pathname(loc.buffer.name)
      else
        Pathname(loc.source_buffer.name)
      end

    path = project.absolute_path(path)

    {
      uri: Steep::PathHelper.to_uri(path).to_s,
      range: loc.as_lsp_range
    }
  end
end

#handle_job(job) ⇒ Object



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
208
209
210
211
212
213
214
215
216
# File 'lib/steep/server/type_check_worker.rb', line 158

def handle_job(job)
  case job
  when StartTypeCheckJob
    Steep.logger.info { "Processing StartTypeCheckJob for guid=#{job.guid}" }
    service.update(changes: job.changes)

  when ValidateAppSignatureJob
    if job.guid == current_type_check_guid
      Steep.logger.info { "Processing ValidateAppSignature for guid=#{job.guid}, path=#{job.path}" }

      formatter = Diagnostic::LSPFormatter.new({}, **{})

      diagnostics = service.validate_signature(path: project.relative_path(job.path), target: job.target)

      typecheck_progress(
        path: job.path,
        guid: job.guid,
        target: job.target,
        diagnostics: diagnostics.filter_map { formatter.format(_1) }
      )
    end

  when ValidateLibrarySignatureJob
    if job.guid == current_type_check_guid
      Steep.logger.info { "Processing ValidateLibrarySignature for guid=#{job.guid}, path=#{job.path}" }

      formatter = Diagnostic::LSPFormatter.new({}, **{})
      diagnostics = service.validate_signature(path: job.path, target: job.target)

      typecheck_progress(path: job.path, guid: job.guid, target: job.target, diagnostics: diagnostics.filter_map { formatter.format(_1) })
    end

  when TypeCheckCodeJob
    if job.guid == current_type_check_guid
      Steep.logger.info { "Processing TypeCheckCodeJob for guid=#{job.guid}, path=#{job.path}, target=#{job.target.name}" }
      group_target = project.group_for_source_path(job.path) || job.target
      formatter = Diagnostic::LSPFormatter.new(group_target.code_diagnostics_config)
      relative_path = project.relative_path(job.path)
      diagnostics = service.typecheck_source(path: relative_path, target: job.target)
      typecheck_progress(path: job.path, guid: job.guid, target: job.target, diagnostics: diagnostics&.filter_map { formatter.format(_1) })
    end

  when WorkspaceSymbolJob
    writer.write(
      id: job.id,
      result: workspace_symbol_result(job.query)
    )
  when StatsJob
    writer.write(
      id: job.id,
      result: stats_result().map(&:as_json)
    )
  when GotoJob
    writer.write(
      id: job.id,
      result: goto(job)
    )
  end
end

#handle_request(request) ⇒ Object



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

def handle_request(request)
  case request[:method]
  when "initialize"
    writer.write({ id: request[:id], result: nil})

  when "textDocument/didChange"
    collect_changes(request)

  when CustomMethods::FileLoad::METHOD
    input = request[:params][:content]
    load_files(input)

  when CustomMethods::FileReset::METHOD
    params = request[:params] #: CustomMethods::FileReset::params
    uri = params[:uri]
    text = params[:content]
    reset_change(uri: uri, text: text)

  when "workspace/symbol"
    query = request[:params][:query]
    queue << WorkspaceSymbolJob.new(id: request[:id], query: query)
  when CustomMethods::Stats::METHOD
    queue << StatsJob.new(id: request[:id])
  when CustomMethods::TypeCheck__Start::METHOD
    params = request[:params] #: CustomMethods::TypeCheck__Start::params
    enqueue_typecheck_jobs(params)
  when "textDocument/definition"
    queue << GotoJob.definition(id: request[:id], params: request[:params])
  when "textDocument/implementation"
    queue << GotoJob.implementation(id: request[:id], params: request[:params])
  when "textDocument/typeDefinition"
    queue << GotoJob.type_definition(id: request[:id], params: request[:params])
  end
end

#stats_resultObject



252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/steep/server/type_check_worker.rb', line 252

def stats_result
  calculator = Services::StatsCalculator.new(service: service)

  project.targets.each.with_object([]) do |target, stats|
    service.source_files.each_value do |file|
      next unless target.possible_source_file?(file.path)
      absolute_path = project.absolute_path(file.path)
      next unless assignment =~ [target, absolute_path]

      stats << calculator.calc_stats(target, file: file)
    end
  end
end

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



218
219
220
# File 'lib/steep/server/type_check_worker.rb', line 218

def typecheck_progress(guid:, path:, target:, diagnostics:)
  writer.write(CustomMethods::TypeCheck__Progress.notification({ guid: guid, path: path.to_s, target: target.name.to_s, diagnostics: diagnostics }))
end

#workspace_symbol_result(query) ⇒ Object



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

def workspace_symbol_result(query)
  Steep.measure "Generating workspace symbol list for query=`#{query}`" do
    provider = Index::SignatureSymbolProvider.new(project: project, assignment: assignment)
    project.targets.each do |target|
      index = service.signature_services.fetch(target.name).latest_rbs_index
      provider.indexes[target] = index
    end

    symbols = provider.query_symbol(query)

    symbols.map do |symbol|
      LSP::Interface::SymbolInformation.new(
        name: symbol.name,
        kind: symbol.kind,
        location: symbol.location.yield_self do |location|
          path = Pathname(location.buffer.name)
          {
            uri: Steep::PathHelper.to_uri(project.absolute_path(path)),
            range: {
              start: { line: location.start_line - 1, character: location.start_column },
              end: { line: location.end_line - 1, character: location.end_column }
            }
          }
        end,
        container_name: symbol.container_name
      )
    end
  end
end