Class: Steep::Services::CompletionProvider
- Includes:
- NodeHelper
- Defined in:
- lib/steep/services/completion_provider.rb
Defined Under Namespace
Classes: TextItem, TypeNameItem
Constant Summary collapse
- Position =
_ = Struct.new(:line, :column, keyword_init: true) do # @implements Position def -(size) Position.new(line: line, column: column - size) end end
- Range =
_ = Struct.new(:start, :end, keyword_init: true)
- InstanceVariableItem =
_ = Struct.new(:identifier, :range, :type, keyword_init: true)
- KeywordArgumentItem =
_ = Struct.new(:identifier, :range, keyword_init: true)
- LocalVariableItem =
_ = Struct.new(:identifier, :range, :type, keyword_init: true)
- ConstantItem =
_ = Struct.new(:env, :identifier, :range, :type, :full_name, keyword_init: true) do # @implements ConstantItem def class? env.class_entry(full_name) ? true : false end def module? env.module_entry(full_name) ? true : false end def comments case entry = env.constant_entry(full_name) when RBS::Environment::ConstantEntry [entry.decl.comment] when RBS::Environment::ClassEntry, RBS::Environment::ModuleEntry entry.decls.map {|d| d.decl.comment } when RBS::Environment::ClassAliasEntry, RBS::Environment::ModuleAliasEntry [entry.decl.comment] else raise end end def decl case entry = env.constant_entry(full_name) when RBS::Environment::ConstantEntry entry.decl when RBS::Environment::ClassEntry, RBS::Environment::ModuleEntry entry.primary.decl when RBS::Environment::ClassAliasEntry, RBS::Environment::ModuleAliasEntry entry.decl else raise end end end
- SimpleMethodNameItem =
_ = Struct.new(:identifier, :range, :receiver_type, :method_types, :method_member, :method_name, keyword_init: true) do # @implements SimpleMethodNameItem def comment method_member.comment end end
- ComplexMethodNameItem =
_ = Struct.new(:identifier, :range, :receiver_type, :method_types, :method_decls, keyword_init: true) do # @implements ComplexMethodNameItem def method_names method_definitions.keys end def method_definitions method_decls.each.with_object({}) do |decl, hash| #$ Hash[method_name, RBS::Definition::Method::method_member] method_name = defining_method_name( decl.method_def.defined_in, decl.method_name.method_name, decl.method_def.member ) hash[method_name] = decl.method_def.member end end def defining_method_name(type_name, name, member) case member when RBS::AST::Members::MethodDefinition if member.instance? InstanceMethodName.new(type_name: type_name, method_name: name) else SingletonMethodName.new(type_name: type_name, method_name: name) end when RBS::AST::Members::Attribute if member.kind == :instance InstanceMethodName.new(type_name: type_name, method_name: name) else SingletonMethodName.new(type_name: type_name, method_name: name) end end end end
- GeneratedMethodNameItem =
_ = Struct.new(:identifier, :range, :receiver_type, :method_types, keyword_init: true) do # @implements GeneratedMethodNameItem end
Instance Attribute Summary collapse
-
#modified_text ⇒ Object
readonly
Returns the value of attribute modified_text.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
-
#source_text ⇒ Object
readonly
Returns the value of attribute source_text.
-
#subtyping ⇒ Object
readonly
Returns the value of attribute subtyping.
-
#typing ⇒ Object
readonly
Returns the value of attribute typing.
Instance Method Summary collapse
- #at_comment?(position) ⇒ Boolean
- #at_end?(pos, of:) ⇒ Boolean
- #constant_items_for_context(context, parent: nil, position:, prefix:, items:) ⇒ Object
- #disallowed_method?(name) ⇒ Boolean
- #env ⇒ Object
- #index_for(string, line:, column:) ⇒ Object
-
#initialize(source_text:, path:, subtyping:) ⇒ CompletionProvider
constructor
A new instance of CompletionProvider.
- #instance_variable_items_for_context(context, position:, prefix:, items:) ⇒ Object
- #items_for_atmark(position:) ⇒ Object
- #items_for_colon2(position:) ⇒ Object
- #items_for_dot(position:) ⇒ Object
- #items_for_following_keyword_arguments(text, index:, line:, column:, items:) ⇒ Object
- #items_for_qcall(position:) ⇒ Object
- #items_for_rbs(position:, buffer:) ⇒ Object
- #items_for_trigger(position:) ⇒ Object
- #keyword_argument_items_for_method(call_node:, send_node:, position:, prefix:, items:) ⇒ Object
- #local_variable_items_for_context(context, position:, prefix:, items:) ⇒ Object
- #method_items_for_receiver_type(type, include_private:, prefix:, position:, items:) ⇒ Object
- #range_for(position, prefix: "") ⇒ Object
- #range_from_loc(loc) ⇒ Object
- #run(line:, column:) ⇒ Object
- #type_check!(text, line:, column:) ⇒ Object
- #unwrap_optional(type) ⇒ Object
- #word_name?(name) ⇒ Boolean
Methods included from NodeHelper
#clone_node, #deconstruct_case_node, #deconstruct_case_node!, #deconstruct_if_node, #deconstruct_if_node!, #deconstruct_resbody_node, #deconstruct_resbody_node!, #deconstruct_rescue_node, #deconstruct_rescue_node!, #deconstruct_send_node, #deconstruct_send_node!, #deconstruct_sendish_and_block_nodes, #deconstruct_when_node, #deconstruct_when_node!, #deconstruct_whileish_node, #deconstruct_whileish_node!, #each_child_node, #each_descendant_node, #private_send?, #test_case_node, #test_if_node, #test_resbody_node, #test_rescue_node, #test_send_node, #test_when_node, #test_whileish_node, #value_node?
Constructor Details
#initialize(source_text:, path:, subtyping:) ⇒ CompletionProvider
Returns a new instance of CompletionProvider.
170 171 172 173 174 |
# File 'lib/steep/services/completion_provider.rb', line 170 def initialize(source_text:, path:, subtyping:) @source_text = source_text @path = path @subtyping = subtyping end |
Instance Attribute Details
#modified_text ⇒ Object (readonly)
Returns the value of attribute modified_text.
166 167 168 |
# File 'lib/steep/services/completion_provider.rb', line 166 def modified_text @modified_text end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
164 165 166 |
# File 'lib/steep/services/completion_provider.rb', line 164 def path @path end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
167 168 169 |
# File 'lib/steep/services/completion_provider.rb', line 167 def source @source end |
#source_text ⇒ Object (readonly)
Returns the value of attribute source_text.
163 164 165 |
# File 'lib/steep/services/completion_provider.rb', line 163 def source_text @source_text end |
#subtyping ⇒ Object (readonly)
Returns the value of attribute subtyping.
165 166 167 |
# File 'lib/steep/services/completion_provider.rb', line 165 def subtyping @subtyping end |
#typing ⇒ Object (readonly)
Returns the value of attribute typing.
168 169 170 |
# File 'lib/steep/services/completion_provider.rb', line 168 def typing @typing end |
Instance Method Details
#at_comment?(position) ⇒ Boolean
332 333 334 |
# File 'lib/steep/services/completion_provider.rb', line 332 def at_comment?(position) source.find_comment(line: position.line, column: position.column) end |
#at_end?(pos, of:) ⇒ Boolean
326 327 328 329 330 |
# File 'lib/steep/services/completion_provider.rb', line 326 def at_end?(pos, of:) if of of.last_line == pos.line && of.last_column == pos.column end end |
#constant_items_for_context(context, parent: nil, position:, prefix:, items:) ⇒ Object
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 |
# File 'lib/steep/services/completion_provider.rb', line 696 def constant_items_for_context(context, parent: nil, position:, prefix:, items:) range = range_for(position, prefix: prefix) if parent case parent.type when :const const_name = typing.source_index.reference(constant_node: parent) or raise "Unknown node in source_index: #{parent}" consts = context.type_env.constant_env.children(const_name) end else consts = context.type_env.constant_env.constants end if consts consts.each do |name, tuple| type, full_name, _ = tuple if name.to_s.start_with?(prefix) items << ConstantItem.new(env: env, identifier: name, range: range, type: type, full_name: full_name) end end end end |
#disallowed_method?(name) ⇒ Boolean
785 786 787 788 789 790 |
# File 'lib/steep/services/completion_provider.rb', line 785 def disallowed_method?(name) # initialize isn't invoked by developers when creating # instances of new classes, so don't show it as # an LSP option name == :initialize end |
#env ⇒ Object
192 193 194 |
# File 'lib/steep/services/completion_provider.rb', line 192 def env subtyping.factory.env end |
#index_for(string, line:, column:) ⇒ Object
770 771 772 773 774 775 776 777 778 779 780 781 782 783 |
# File 'lib/steep/services/completion_provider.rb', line 770 def index_for(string, line:, column:) index = 0 string.each_line.with_index do |s, i| if i+1 == line index += column break else index += s.size end end index end |
#instance_variable_items_for_context(context, position:, prefix:, items:) ⇒ Object
720 721 722 723 724 725 726 727 |
# File 'lib/steep/services/completion_provider.rb', line 720 def instance_variable_items_for_context(context, position:, prefix:, items:) range = range_for(position, prefix: prefix) context.type_env.instance_variable_types.each do |name, type| if name.to_s.start_with?(prefix) items << InstanceVariableItem.new(identifier: name, range: range, type: type) end end end |
#items_for_atmark(position:) ⇒ Object
548 549 550 551 552 553 554 555 556 557 558 559 560 |
# File 'lib/steep/services/completion_provider.rb', line 548 def items_for_atmark(position:) # @ ← shift_pos = position-1 node, *_ = source.find_nodes(line: shift_pos.line, column: shift_pos.column) node ||= source.node return [] unless node context = typing.cursor_context.context or raise items = [] #: Array[item] instance_variable_items_for_context(context, prefix: "@", position: position, items: items) items end |
#items_for_colon2(position:) ⇒ Object
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 |
# File 'lib/steep/services/completion_provider.rb', line 523 def items_for_colon2(position:) # :: ← shift_pos = position-2 node, *_ = source.find_nodes(line: shift_pos.line, column: shift_pos.column) node ||= source.node items = [] #: Array[item] case node&.type when :const # Constant:: ← context = typing.cursor_context.context or raise constant_items_for_context(context, parent: node, position: position, items: items, prefix: "") when nil # :: ← context = typing.cursor_context.context or raise constant_items_for_context(context, parent: nil, position: position, items: items, prefix: "") end if node items.push(*items_for_dot(position: position - 1)) end items end |
#items_for_dot(position:) ⇒ Object
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 |
# File 'lib/steep/services/completion_provider.rb', line 463 def items_for_dot(position:) # foo. ← shift_pos = position-1 node, *_parents = source.find_nodes(line: shift_pos.line, column: shift_pos.column) node ||= source.node return [] unless node if at_end?(shift_pos, of: node.loc) begin context = typing.cursor_context.context or raise receiver_type = case (type = typing.type_of(node: node)) when AST::Types::Self context.self_type else type end items = [] #: Array[item] method_items_for_receiver_type(receiver_type, include_private: false, prefix: "", position: position, items: items) items rescue Typing::UnknownNodeError [] end else [] end end |
#items_for_following_keyword_arguments(text, index:, line:, column:, items:) ⇒ Object
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 |
# File 'lib/steep/services/completion_provider.rb', line 580 def items_for_following_keyword_arguments(text, index:, line:, column:, items:) return if text[index - 1] !~ /[a-zA-Z0-9]/ text = text.dup argname = [] #: Array[String] while text[index - 1] =~ /[a-zA-Z0-9]/ argname.unshift(text[index - 1] || '') source_text[index - 1] = " " index -= 1 end begin type_check!(source_text, line: line, column: column) rescue Parser::SyntaxError return end if nodes = source.find_nodes(line: line, column: column) if (send_node, block_node = deconstruct_sendish_and_block_nodes(*nodes)) position = Position.new(line: line, column: column) keyword_argument_items_for_method( call_node: block_node || send_node, send_node: send_node, position: position, prefix: argname.join, items: items ) end end end |
#items_for_qcall(position:) ⇒ Object
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 |
# File 'lib/steep/services/completion_provider.rb', line 493 def items_for_qcall(position:) # foo&. ← shift_pos = position-2 node, *_parents = source.find_nodes(line: shift_pos.line, column: shift_pos.column) node ||= source.node return [] unless node if at_end?(shift_pos, of: node.loc) begin context = typing.cursor_context.context or raise receiver_type = case (type = typing.type_of(node: node)) when AST::Types::Self context.self_type else unwrap_optional(type) end items = [] #: Array[item] method_items_for_receiver_type(receiver_type, include_private: false, prefix: "", position: position, items: items) items rescue Typing::UnknownNodeError [] end else [] end end |
#items_for_rbs(position:, buffer:) ⇒ Object
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 |
# File 'lib/steep/services/completion_provider.rb', line 562 def items_for_rbs(position:, buffer:) items = [] #: Array[item] context = typing.cursor_context.context or raise completion = TypeNameCompletion.new(env: context.env, context: context.module_context.nesting, dirs: []) prefix = TypeNameCompletion::Prefix.parse(buffer, line: position.line, column: position.column) size = prefix&.size || 0 range = Range.new(start: position - size, end: position) completion.find_type_names(prefix).each do |name| absolute, relative = completion.resolve_name_in_context(name) items << TypeNameItem.new(relative_type_name: relative, absolute_type_name: absolute, env: context.env, range: range) end items end |
#items_for_trigger(position:) ⇒ Object
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 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 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 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 461 |
# File 'lib/steep/services/completion_provider.rb', line 344 def items_for_trigger(position:) node, *parents = source.find_nodes(line: position.line, column: position.column) node ||= source.node return [] unless node && parents items = [] #: Array[item] context = typing.cursor_context.context or raise case when node.type == :send && node.children[0] == nil && at_end?(position, of: (_ = node.loc).selector) # foo ← prefix = node.children[1].to_s method_items_for_receiver_type(context.self_type, include_private: true, prefix: prefix, position: position, items: items) local_variable_items_for_context(context, position: position, prefix: prefix, items: items) if (send_node, block_node = deconstruct_sendish_and_block_nodes(*parents)) keyword_argument_items_for_method( call_node: block_node || send_node, send_node: send_node, position: position, prefix: prefix, items: items ) end when node.type == :lvar && at_end?(position, of: node.loc) # foo ← (lvar) local_variable_items_for_context(context, position: position, prefix: node.children[0].to_s, items: items) when node.type == :send && node.children[0] && at_end?(position, of: (_ = node.loc).selector) # foo.ba ← receiver_type = case (type = typing.type_of(node: node.children[0])) when AST::Types::Self context.self_type else type end prefix = node.children[1].to_s method_items_for_receiver_type(receiver_type, include_private: false, prefix: prefix, position: position, items: items) when node.type == :csend && node.children[0] && at_end?(position, of: (_ = node.loc).selector) # foo&.ba ← receiver_type = case (type = typing.type_of(node: node.children[0])) when AST::Types::Self context.self_type else unwrap_optional(type) end prefix = node.children[1].to_s method_items_for_receiver_type(receiver_type, include_private: false, prefix: prefix, position: position, items: items) when node.type == :const && node.children[0] == nil && at_end?(position, of: node.loc) # Foo ← (const) prefix = node.children[1].to_s method_items_for_receiver_type(context.self_type, include_private: false, prefix: prefix, position: position, items: items) constant_items_for_context(context, prefix: prefix, position: position, items: items) when node.type == :const && node.children[0] && at_end?(position, of: node.loc) # Foo::Ba ← (const) parent_node = node.children[0] parent_type = typing.type_of(node: parent_node) if parent_type prefix = node.children[1].to_s method_items_for_receiver_type(parent_type, include_private: false, prefix: prefix, position: position, items: items) constant_items_for_context(context, parent: parent_node, prefix: prefix, position: position, items: items) end when node.type == :send && at_end?(position, of: (_ = node.loc).dot) && (_ = node.loc).dot.source == "." # foo.← ba receiver_type = case (type = typing.type_of(node: node.children[0])) when AST::Types::Self context.self_type else type end method_items_for_receiver_type(receiver_type, include_private: false, prefix: "", position: position, items: items) when node.type == :send && at_end?(position, of: (_ = node.loc).dot) && (_ = node.loc).dot.source == "::" # foo::← ba items.push(*items_for_colon2(position: position)) when node.type == :csend && at_end?(position, of: (_ = node.loc).dot) # foo&.← ba receiver_type = case (type = typing.type_of(node: node.children[0])) when AST::Types::Self context.self_type else unwrap_optional(type) end method_items_for_receiver_type(receiver_type, include_private: false, prefix: "", position: position, items: items) when node.type == :ivar && at_end?(position, of: node.loc) # @fo ← instance_variable_items_for_context(context, position: position, prefix: node.children[0].to_s, items: items) else method_items_for_receiver_type(context.self_type, include_private: true, prefix: "", position: position, items: items) local_variable_items_for_context(context, position: position, prefix: "", items: items) instance_variable_items_for_context(context, position: position, prefix: "", items: items) constant_items_for_context(context, position: position, prefix: "", items: items) end items end |
#keyword_argument_items_for_method(call_node:, send_node:, position:, prefix:, items:) ⇒ Object
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 |
# File 'lib/steep/services/completion_provider.rb', line 729 def keyword_argument_items_for_method(call_node:, send_node:, position:, prefix:, items:) _receiver_node, _method_name, argument_nodes = deconstruct_send_node!(send_node) call = typing.call_of(node: call_node) case call when TypeInference::MethodCall::Typed, TypeInference::MethodCall::Error context = typing.cursor_context.context or raise type = call.receiver_type type = type.subst(Interface::Substitution.build([], self_type: context.self_type, module_type: context.module_context&.module_type, instance_type: context.module_context&.instance_type)) config = Interface::Builder::Config.new(self_type: type, variable_bounds: context.variable_context.upper_bounds) if shape = subtyping.builder.shape(type, config) shape = shape.public_shape if private_send?(call_node) if method = shape.methods[call.method_name] method.overloads.each.with_index do |overload, i| defn = overload.method_decls(call.method_name).to_a[0]&.method_def if defn && defn.type.type range = range_for(position, prefix: prefix) kwargs = argument_nodes.find { |arg| arg.type == :kwargs }&.children || [] used_kwargs = kwargs.filter_map { |arg| arg.type == :pair && arg.children.first.children.first } if defn.type.type.is_a?(RBS::Types::UntypedFunction) kwargs = [] #: Array[Symbol] else kwargs = defn.type.type.required_keywords.keys + defn.type.type.optional_keywords.keys end kwargs.each do |name| if name.to_s.start_with?(prefix) && !used_kwargs.include?(name) items << KeywordArgumentItem.new(identifier: "#{name}:", range: range) end end end end end end end end |
#local_variable_items_for_context(context, position:, prefix:, items:) ⇒ Object
685 686 687 688 689 690 691 692 693 694 |
# File 'lib/steep/services/completion_provider.rb', line 685 def local_variable_items_for_context(context, position:, prefix:, items:) range = range_for(position, prefix: prefix) context.type_env.local_variable_types.each do |name, pair| type, _ = pair if name.to_s.start_with?(prefix) items << LocalVariableItem.new(identifier: name, range: range, type: type) end end end |
#method_items_for_receiver_type(type, include_private:, prefix:, position:, items:) ⇒ Object
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 |
# File 'lib/steep/services/completion_provider.rb', line 611 def method_items_for_receiver_type(type, include_private:, prefix:, position:, items:) range = range_for(position, prefix: prefix) context = typing.cursor_context.context or raise config = if (module_type = context.module_context&.module_type) && (instance_type = context.module_context&.instance_type) Interface::Builder::Config.new( self_type: context.self_type, class_type: module_type, instance_type: instance_type, variable_bounds: context.variable_context.upper_bounds ) else Interface::Builder::Config.new(self_type: context.self_type, variable_bounds: context.variable_context.upper_bounds) end if shape = subtyping.builder.shape(type, config) shape = shape.public_shape unless include_private shape.methods.each do |name, method_entry| next if disallowed_method?(name) if name.to_s.start_with?(prefix) if word_name?(name.to_s) case type when AST::Types::Name::Instance, AST::Types::Name::Interface, AST::Types::Name::Singleton # Simple method type all_decls = Set.new(method_entry.overloads.flat_map {|overload| overload.method_decls(name) }).sort_by {|decl| decl.method_name.to_s } all_members = Set.new(all_decls.flat_map {|decl| decl.method_def.member }) all_members.each do |member| associated_decl = all_decls.find {|decl| decl.method_def.member == member } or next overloads = method_entry.overloads.select {|overload| overload.method_defs.any? {|defn| defn.member == member }} items << SimpleMethodNameItem.new( identifier: name, range: range, receiver_type: type, method_name: associated_decl.method_name, method_types: overloads.map {|overload| subtyping.factory.method_type_1(overload.method_type) }, method_member: member ) end else generated_overloads, defined_overloads = method_entry.overloads.partition {|overload| overload.method_defs.empty? } unless defined_overloads.empty? items << ComplexMethodNameItem.new( identifier: name, range: range, receiver_type: type, method_types: defined_overloads.map { subtyping.factory.method_type_1(_1.method_type) }, method_decls: defined_overloads.flat_map { _1.method_decls(name).to_a }.sort_by {|decl| decl.method_name.to_s } ) end unless generated_overloads.empty? items << GeneratedMethodNameItem.new( identifier: name, range: range, receiver_type: type, method_types: generated_overloads.map { subtyping.factory.method_type_1(_1.method_type) } ) end end end end end end end |
#range_for(position, prefix: "") ⇒ Object
336 337 338 339 340 341 342 |
# File 'lib/steep/services/completion_provider.rb', line 336 def range_for(position, prefix: "") if prefix.empty? Range.new(start: position, end: position) else Range.new(start: position - prefix.size, end: position) end end |
#range_from_loc(loc) ⇒ Object
319 320 321 322 323 324 |
# File 'lib/steep/services/completion_provider.rb', line 319 def range_from_loc(loc) Range.new( start: Position.new(line: loc.line, column: loc.column), end: Position.new(line: loc.last_line, column: loc.last_line) ) end |
#run(line:, column:) ⇒ Object
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 234 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 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/steep/services/completion_provider.rb', line 196 def run(line:, column:) source_text = self.source_text.dup index = index_for(source_text, line:line, column: column) possible_trigger = source_text[index-1] Steep.logger.debug "possible_trigger: #{possible_trigger.inspect}" position = Position.new(line: line, column: column) begin Steep.logger.tagged "completion_provider#run(line: #{line}, column: #{column})" do Steep.measure "type_check!" do type_check!(source_text, line: line, column: column) end end if comment = at_comment?(position) node, *parents = source.find_nodes(line: position.line, column: position.column) case when node&.type == :assertion # continue node or raise assertion = node.children[1] #: AST::Node::TypeAssertion return items_for_rbs(position: position, buffer: assertion.location.buffer) when node && parents && tapp_node = ([node] + parents).find {|n| n.type == :tapp } tapp = tapp_node.children[1] #: AST::Node::TypeApplication type_range = tapp.type_location.range if type_range.begin < index && index <= type_range.end return items_for_rbs(position: position, buffer: tapp.location.buffer) end else annotation = source.each_annotation.flat_map {|_, annots| annots }.find do |a| if a.location a.location.start_pos < index && index <= a.location.end_pos end end if annotation annotation.location or raise return items_for_rbs(position: position, buffer: annotation.location.buffer) end comment_content = comment.text[1..] || "" comment_content.strip! range = Range.new( start: Position.new(line: line, column: column), end: Position.new(line: line, column: column) ) items = [ TextItem.new(label: "steep:ignore:start", text: "steep:ignore:start", help_text: "Open ignore block", range: range), TextItem.new(label: "steep:ignore:end", text: "steep:ignore:end", help_text: "Close ignore block", range: range), TextItem.new(label: "steep:ignore", text: "steep:ignore ${1:optional diagnostics}", help_text: "Ignore line", range: range), TextItem.new(label: "@type var x: T", text: "@type var ${1:variable}: ${2:var type}", help_text: "Type of local variable", range: range), TextItem.new(label: "@type self: T", text: "@type self: ${1:self type}", help_text: "Type of `self`", range: range), TextItem.new(label: "@type block: T", text: "@type block: ${1:block type}", help_text: "Type of `block`", range: range), TextItem.new(label: "@type break: T", text: "@type break: ${1:break type}", help_text: "Type of `block`", range: range), ] items = items.filter_map do |item| if item.text.start_with?(comment_content) TextItem.new( label: item.label, text: item.text, help_text: item.help_text, range: Range.new( start: Position.new(line: item.range.start.line, column: item.range.start.column - comment_content.size), end: item.range.end ) ) end end return items end end Steep.measure "completion item collection" do items_for_trigger(position: position) end rescue Parser::SyntaxError => exn Steep.logger.info "recovering syntax error: #{exn.inspect}" @source_text = source_text.dup case possible_trigger when "." if source_text[index-2] == "&" source_text[index-1] = " " source_text[index-2] = " " type_check!(source_text, line: line, column: column) items_for_qcall(position: position) else source_text[index-1] = " " type_check!(source_text, line: line, column: column) items_for_dot(position: position) end when "@" source_text[index-1] = " " type_check!(source_text, line: line, column: column) items_for_atmark(position: position) when ":" if source_text[index-2] == ":" source_text[index-1] = " " source_text[index-2] = " " type_check!(source_text, line: line, column: column) items_for_colon2(position: position) else [] end else items = [] #: Array[item] items_for_following_keyword_arguments(source_text, index: index, line: line, column: column, items: items) items end end end |
#type_check!(text, line:, column:) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/steep/services/completion_provider.rb', line 176 def type_check!(text, line:, column:) @modified_text = text Steep.measure "parsing" do @source = Source .parse(text, path: path, factory: subtyping.factory) .(line: line, column: column) end Steep.measure "typechecking" do location = source.buffer.loc_to_pos([line, column]) resolver = RBS::Resolver::ConstantResolver.new(builder: subtyping.factory.definition_builder) @typing = TypeCheckService.type_check(source: source, subtyping: subtyping, constant_resolver: resolver, cursor: location) end end |
#unwrap_optional(type) ⇒ Object
792 793 794 795 796 797 798 799 |
# File 'lib/steep/services/completion_provider.rb', line 792 def unwrap_optional(type) if type.is_a?(AST::Types::Union) && type.types.include?(AST::Builtin.nil_type) types = type.types.reject { |t| t == AST::Builtin.nil_type } AST::Types::Union.new(types: types) else type end end |
#word_name?(name) ⇒ Boolean
681 682 683 |
# File 'lib/steep/services/completion_provider.rb', line 681 def word_name?(name) name =~ /\w/ ? true : false end |