Class: Steep::Drivers::Langserver
- Inherits:
-
Object
- Object
- Steep::Drivers::Langserver
- Includes:
- Utils::DriverHelper
- Defined in:
- lib/steep/drivers/langserver.rb
Defined Under Namespace
Classes: TypeCheckRequest
Instance Attribute Summary collapse
-
#latest_update_version ⇒ Object
readonly
Returns the value of attribute latest_update_version.
-
#stderr ⇒ Object
readonly
Returns the value of attribute stderr.
-
#stdin ⇒ Object
readonly
Returns the value of attribute stdin.
-
#stdout ⇒ Object
readonly
Returns the value of attribute stdout.
-
#type_check_queue ⇒ Object
readonly
Returns the value of attribute type_check_queue.
-
#type_check_thread ⇒ Object
readonly
Returns the value of attribute type_check_thread.
-
#write_mutex ⇒ Object
readonly
Returns the value of attribute write_mutex.
Attributes included from Utils::DriverHelper
Instance Method Summary collapse
- #diagnostic_for_type_error(error) ⇒ Object
- #diagnostic_for_validation_error(error) ⇒ Object
- #diagnostics_raw(message, loc) ⇒ Object
- #enqueue_type_check(version) ⇒ Object
- #format_completion_item(item) ⇒ Object
- #format_hover(content) ⇒ Object
- #handle_request(request) ⇒ Object
-
#initialize(stdout:, stderr:, stdin:) ⇒ Langserver
constructor
A new instance of Langserver.
- #method_type_to_snippet(method_type) ⇒ Object
- #params_to_snippet(fun) ⇒ Object
- #project ⇒ Object
- #reader ⇒ Object
- #report_diagnostics(path, diagnostics) ⇒ Object
- #response_to_hover(path:, line:, column:) ⇒ Object
- #run ⇒ Object
- #run_type_check ⇒ Object
- #start_type_check ⇒ Object
- #write(method:, params:) ⇒ Object
- #writer ⇒ Object
Methods included from Utils::DriverHelper
Constructor Details
#initialize(stdout:, stderr:, stdin:) ⇒ Langserver
Returns a new instance of Langserver.
16 17 18 19 20 21 22 |
# File 'lib/steep/drivers/langserver.rb', line 16 def initialize(stdout:, stderr:, stdin:) @stdout = stdout @stderr = stderr @stdin = stdin @write_mutex = Mutex.new @type_check_queue = Queue.new end |
Instance Attribute Details
#latest_update_version ⇒ Object (readonly)
Returns the value of attribute latest_update_version.
7 8 9 |
# File 'lib/steep/drivers/langserver.rb', line 7 def latest_update_version @latest_update_version end |
#stderr ⇒ Object (readonly)
Returns the value of attribute stderr.
5 6 7 |
# File 'lib/steep/drivers/langserver.rb', line 5 def stderr @stderr end |
#stdin ⇒ Object (readonly)
Returns the value of attribute stdin.
6 7 8 |
# File 'lib/steep/drivers/langserver.rb', line 6 def stdin @stdin end |
#stdout ⇒ Object (readonly)
Returns the value of attribute stdout.
4 5 6 |
# File 'lib/steep/drivers/langserver.rb', line 4 def stdout @stdout end |
#type_check_queue ⇒ Object (readonly)
Returns the value of attribute type_check_queue.
9 10 11 |
# File 'lib/steep/drivers/langserver.rb', line 9 def type_check_queue @type_check_queue end |
#type_check_thread ⇒ Object (readonly)
Returns the value of attribute type_check_thread.
10 11 12 |
# File 'lib/steep/drivers/langserver.rb', line 10 def type_check_thread @type_check_thread end |
#write_mutex ⇒ Object (readonly)
Returns the value of attribute write_mutex.
8 9 10 |
# File 'lib/steep/drivers/langserver.rb', line 8 def write_mutex @write_mutex end |
Instance Method Details
#diagnostic_for_type_error(error) ⇒ Object
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/steep/drivers/langserver.rb', line 296 def diagnostic_for_type_error(error) LanguageServer::Protocol::Interface::Diagnostic.new( message: error.to_s, severity: LanguageServer::Protocol::Constant::DiagnosticSeverity::ERROR, range: LanguageServer::Protocol::Interface::Range.new( start: LanguageServer::Protocol::Interface::Position.new( line: error.node.loc.line - 1, character: error.node.loc.column, ), end: LanguageServer::Protocol::Interface::Position.new( line: error.node.loc.last_line - 1, character: error.node.loc.last_column, ), ) ) end |
#diagnostic_for_validation_error(error) ⇒ Object
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/steep/drivers/langserver.rb', line 262 def diagnostic_for_validation_error(error) LanguageServer::Protocol::Interface::Diagnostic.new( message: StringIO.new("").tap {|io| error.puts(io) }.string, severity: LanguageServer::Protocol::Constant::DiagnosticSeverity::ERROR, range: LanguageServer::Protocol::Interface::Range.new( start: LanguageServer::Protocol::Interface::Position.new( line: error.location.start_line - 1, character: error.location.start_column, ), end: LanguageServer::Protocol::Interface::Position.new( line: error.location.end_line - 1, character: error.location.end_column, ), ) ) end |
#diagnostics_raw(message, loc) ⇒ Object
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/steep/drivers/langserver.rb', line 279 def diagnostics_raw(, loc) LanguageServer::Protocol::Interface::Diagnostic.new( message: , severity: LanguageServer::Protocol::Constant::DiagnosticSeverity::ERROR, range: LanguageServer::Protocol::Interface::Range.new( start: LanguageServer::Protocol::Interface::Position.new( line: loc.start_line - 1, character: loc.start_column, ), end: LanguageServer::Protocol::Interface::Position.new( line: loc.end_line - 1, character: loc.end_column, ), ) ) end |
#enqueue_type_check(version) ⇒ Object
36 37 38 39 |
# File 'lib/steep/drivers/langserver.rb', line 36 def enqueue_type_check(version) @latest_update_version = version type_check_queue << TypeCheckRequest.new(version: version) end |
#format_completion_item(item) ⇒ Object
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 |
# File 'lib/steep/drivers/langserver.rb', line 385 def format_completion_item(item) range = LanguageServer::Protocol::Interface::Range.new( start: LanguageServer::Protocol::Interface::Position.new( line: item.range.start.line-1, character: item.range.start.column ), end: LanguageServer::Protocol::Interface::Position.new( line: item.range.end.line-1, character: item.range.end.column ) ) case item when Project::CompletionProvider::LocalVariableItem LanguageServer::Protocol::Interface::CompletionItem.new( label: item.identifier, kind: LanguageServer::Protocol::Constant::CompletionItemKind::VARIABLE, detail: "#{item.identifier}: #{item.type}", text_edit: LanguageServer::Protocol::Interface::TextEdit.new( range: range, new_text: "#{item.identifier}" ) ) when Project::CompletionProvider::MethodNameItem label = "def #{item.identifier}: #{item.method_type}" method_type_snippet = method_type_to_snippet(item.method_type) LanguageServer::Protocol::Interface::CompletionItem.new( label: label, kind: LanguageServer::Protocol::Constant::CompletionItemKind::METHOD, text_edit: LanguageServer::Protocol::Interface::TextEdit.new( new_text: "#{item.identifier}#{method_type_snippet}", range: range ), documentation: item.definition.comment&.string, insert_text_format: LanguageServer::Protocol::Constant::InsertTextFormat::SNIPPET ) when Project::CompletionProvider::InstanceVariableItem label = "#{item.identifier}: #{item.type}" LanguageServer::Protocol::Interface::CompletionItem.new( label: label, kind: LanguageServer::Protocol::Constant::CompletionItemKind::FIELD, text_edit: LanguageServer::Protocol::Interface::TextEdit.new( range: range, new_text: item.identifier, ), insert_text_format: LanguageServer::Protocol::Constant::InsertTextFormat::SNIPPET ) end end |
#format_hover(content) ⇒ Object
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 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 |
# File 'lib/steep/drivers/langserver.rb', line 335 def format_hover(content) case content when Project::HoverContent::VariableContent "`#{content.name}`: `#{content.type.to_s}`" when Project::HoverContent::MethodCallContent method_name = case content.method_name when Project::HoverContent::InstanceMethodName "#{content.method_name.class_name}##{content.method_name.method_name}" when Project::HoverContent::SingletonMethodName "#{content.method_name.class_name}.#{content.method_name.method_name}" else nil end if method_name string = <<HOVER ``` #{method_name} ~> #{content.type} ``` HOVER if content.definition if content.definition.comment string << "\n----\n\n#{content.definition.comment.string}" end string << "\n----\n\n#{content.definition.method_types.map {|x| "- `#{x}`\n" }.join()}" end else "`#{content.type}`" end when Project::HoverContent::DefinitionContent string = <<HOVER ``` def #{content.method_name}: #{content.method_type} ``` HOVER if (comment = content.definition.comment) string << "\n----\n\n#{comment.string}\n" end if content.definition.method_types.size > 1 string << "\n----\n\n#{content.definition.method_types.map {|x| "- `#{x}`\n" }.join()}" end string when Project::HoverContent::TypeContent "`#{content.type}`" end end |
#handle_request(request) ⇒ Object
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 103 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 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 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/steep/drivers/langserver.rb', line 74 def handle_request(request) id = request[:id] method = request[:method].to_sym Steep.logger.tagged "id=#{id}, method=#{method}" do case method when :initialize yield id, LanguageServer::Protocol::Interface::InitializeResult.new( capabilities: LanguageServer::Protocol::Interface::ServerCapabilities.new( text_document_sync: LanguageServer::Protocol::Interface::TextDocumentSyncOptions.new( change: LanguageServer::Protocol::Constant::TextDocumentSyncKind::FULL ), hover_provider: true, completion_provider: LanguageServer::Protocol::Interface::CompletionOptions.new( trigger_characters: [".", "@"], ) ) ) enqueue_type_check nil when :"textDocument/completion" Steep.logger.error request.inspect begin params = request[:params] uri = URI.parse(params[:textDocument][:uri]) path = project.relative_path(Pathname(uri.path)) target = project.targets.find {|target| target.source_file?(path) } case (status = target&.status) when Project::Target::TypeCheckStatus subtyping = status.subtyping source = target.source_files[path] line, column = params[:position].yield_self {|hash| [hash[:line]+1, hash[:character]] } trigger = params[:context][:triggerCharacter] Steep.logger.error "line: #{line}, column: #{column}, trigger: #{trigger}" provider = Project::CompletionProvider.new(source_text: source.content, path: path, subtyping: subtyping) items = begin provider.run(line: line, column: column) rescue Parser::SyntaxError [] end completion_items = items.map do |item| format_completion_item(item) end Steep.logger.debug "items = #{completion_items.inspect}" yield id, LanguageServer::Protocol::Interface::CompletionList.new( is_incomplete: false, items: completion_items ) end rescue Typing::UnknownNodeError => exn Steep.log_error exn, message: "Failed to compute completion: #{exn.inspect}" yield id, nil end when :"textDocument/didChange" uri = URI.parse(request[:params][:textDocument][:uri]) path = project.relative_path(Pathname(uri.path)) text = request[:params][:contentChanges][0][:text] Steep.logger.debug { "path=#{path}, content=#{text.lines.first&.chomp}..." } project.targets.each do |target| Steep.logger.tagged "target=#{target.name}" do case when target.source_file?(path) if text.empty? && !path.file? Steep.logger.info { "Deleting source file: #{path}..." } target.remove_source(path) report_diagnostics path, [] else Steep.logger.info { "Updating source file: #{path}..." } target.update_source(path, text) end when target.possible_source_file?(path) Steep.logger.info { "Adding source file: #{path}..." } target.add_source(path, text) when target.signature_file?(path) if text.empty? && !path.file? Steep.logger.info { "Deleting signature file: #{path}..." } target.remove_signature(path) report_diagnostics path, [] else Steep.logger.info { "Updating signature file: #{path}..." } target.update_signature(path, text) end when target.possible_signature_file?(path) Steep.logger.info { "Adding signature file: #{path}..." } target.add_signature(path, text) end end end version = request[:params][:textDocument][:version] enqueue_type_check version when :"textDocument/hover" uri = URI.parse(request[:params][:textDocument][:uri]) path = project.relative_path(Pathname(uri.path)) line = request[:params][:position][:line] column = request[:params][:position][:character] yield id, response_to_hover(path: path, line: line, column: column) when :shutdown yield id, nil when :exit type_check_queue << nil type_check_thread.join exit end end end |
#method_type_to_snippet(method_type) ⇒ Object
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 |
# File 'lib/steep/drivers/langserver.rb', line 435 def method_type_to_snippet(method_type) params = if method_type.type.each_param.count == 0 "" else "(#{params_to_snippet(method_type.type)})" end block = if method_type.block open, space, close = if method_type.block.type.return_type.is_a?(Ruby::Signature::Types::Bases::Void) ["do", " ", "end"] else ["{", "", "}"] end if method_type.block.type.each_param.count == 0 " #{open} $0 #{close}" else " #{open}#{space}|#{params_to_snippet(method_type.block.type)}| $0 #{close}" end else "" end "#{params}#{block}" end |
#params_to_snippet(fun) ⇒ Object
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 |
# File 'lib/steep/drivers/langserver.rb', line 462 def params_to_snippet(fun) params = [] index = 1 fun.required_positionals.each do |param| if name = param.name params << "${#{index}:#{param.type}}" else params << "${#{index}:#{param.type}}" end index += 1 end if fun.rest_positionals params << "${#{index}:*#{fun.rest_positionals.type}}" index += 1 end fun.trailing_positionals.each do |param| if name = param.name params << "${#{index}:#{param.type}}" else params << "${#{index}:#{param.type}}" end index += 1 end fun.required_keywords.each do |keyword, param| if name = param.name params << "#{keyword}: ${#{index}:#{name}_}" else params << "#{keyword}: ${#{index}:#{param.type}_}" end index += 1 end params.join(", ") end |
#project ⇒ Object
32 33 34 |
# File 'lib/steep/drivers/langserver.rb', line 32 def project @project or raise "Empty #project" end |
#reader ⇒ Object
28 29 30 |
# File 'lib/steep/drivers/langserver.rb', line 28 def reader @reader ||= LanguageServer::Protocol::Transport::Io::Reader.new(stdin) end |
#report_diagnostics(path, diagnostics) ⇒ Object
251 252 253 254 255 256 257 258 259 260 |
# File 'lib/steep/drivers/langserver.rb', line 251 def report_diagnostics(path, diagnostics) Steep.logger.info { "Reporting #{diagnostics.size} diagnostics for #{path}..." } write( method: :"textDocument/publishDiagnostics", params: LanguageServer::Protocol::Interface::PublishDiagnosticsParams.new( uri: URI.parse(project.absolute_path(path).to_s).tap {|uri| uri.scheme = "file"}, diagnostics: diagnostics, ) ) end |
#response_to_hover(path:, line:, column:) ⇒ Object
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/steep/drivers/langserver.rb', line 313 def response_to_hover(path:, line:, column:) Steep.logger.info { "path=#{path}, line=#{line}, column=#{column}" } hover = Project::HoverContent.new(project: project) content = hover.content_for(path: path, line: line+1, column: column+1) if content range = content.location.yield_self do |location| start_position = { line: location.line - 1, character: location.column } end_position = { line: location.last_line - 1, character: location.last_column } { start: start_position, end: end_position } end LanguageServer::Protocol::Interface::Hover.new( contents: { kind: "markdown", value: format_hover(content) }, range: range ) end rescue Typing::UnknownNodeError => exn Steep.log_error exn, message: "Failed to compute hover: #{exn.inspect}" nil end |
#run ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/steep/drivers/langserver.rb', line 41 def run @project = load_config() loader = Project::FileLoader.new(project: project) loader.load_sources([]) loader.load_signatures() start_type_check() reader.read do |request| Steep.logger.tagged "lsp" do Steep.logger.debug { "Received a request: request=#{request.to_json}" } handle_request(request) do |id, result| if id write_mutex.synchronize do Steep.logger.debug { "Writing response to #{id}: #{result.to_json}" } writer.write(id: id, result: result) end end end end end 0 end |
#run_type_check ⇒ Object
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 |
# File 'lib/steep/drivers/langserver.rb', line 209 def run_type_check() Steep.logger.tagged "#run_type_check" do Steep.logger.info { "Running type check..." } type_check project Steep.logger.info { "Sending diagnostics..." } project.targets.each do |target| Steep.logger.tagged "target=#{target.name}, status=#{target.status.class}" do Steep.logger.info { "Clearing signature diagnostics..." } target.signature_files.each_value do |file| report_diagnostics file.path, [] end case (status = target.status) when Project::Target::SignatureValidationErrorStatus Steep.logger.info { "Signature validation error" } status.errors.group_by(&:path).each do |path, errors| diagnostics = errors.map {|error| diagnostic_for_validation_error(error) } report_diagnostics path, diagnostics end when Project::Target::TypeCheckStatus Steep.logger.info { "Type check" } status.type_check_sources.each do |source| diagnostics = case source.status when Project::SourceFile::TypeCheckStatus source.errors.map {|error| diagnostic_for_type_error(error) } when Project::SourceFile::AnnotationSyntaxErrorStatus [diagnostics_raw(source.status.error., source.status.location)] end if diagnostics report_diagnostics source.path, diagnostics end end when Project::Target::SignatureSyntaxErrorStatus Steep.logger.info { "Signature syntax error" } end end end end end |
#start_type_check ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/steep/drivers/langserver.rb', line 195 def start_type_check @type_check_thread = Thread.start do while request = type_check_queue.deq if @latest_update_version == nil || @latest_update_version == request.version begin run_type_check() rescue => exn Steep.log_error exn end end end end end |
#write(method:, params:) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/steep/drivers/langserver.rb', line 67 def write(method:, params:) write_mutex.synchronize do Steep.logger.debug { "Sending request: method=#{method}, params=#{params.to_json}"} writer.write(method: method, params: params) end end |
#writer ⇒ Object
24 25 26 |
# File 'lib/steep/drivers/langserver.rb', line 24 def writer @writer ||= LanguageServer::Protocol::Transport::Io::Writer.new(stdout) end |