Module: Solargraph::YardMap::Directives::ScopeDirective
- Defined in:
- lib/solargraph/yard_map/directives/scope_directive.rb
Overview
Implements YARD's @!scope directive.
As described in YARD::Tags::ScopeDirective, a @!scope directive that appears in a docstring attached to a method definition applies only to that method. Anywhere else it applies to all of the methods that follow it within the namespace it was defined in.
class Example
# @!scope class
def foo; end # => Example.foo
def ; end # => Example#bar
end
class Other
# @!scope class
def foo; end # => Other.foo
def ; end # => Other.bar
end
Constant Summary collapse
- VALID_SCOPES =
%i[class instance].freeze
- SCOPED_DIRECTIVES =
Directives that define an object whose scope is inherited from a contains already declares the scope of everything it defines.
%w[method attribute].freeze
Class Method Summary collapse
-
.apply_nested(directive, pins) ⇒ void
Apply a @!scope directive from the indented text of another directive to the pins that directive generated.
-
.apply_scope(pins, kind) ⇒ void
Set the scope of pins generated by a directive.
-
.apply_to_namespace(pins, kind, source_position) ⇒ void
Applies the scope to each method that follows the directive within the namespace the directive appears in.
-
.attached_method(pins, source_position) ⇒ Pin::Method?
The method defined on the line the comment is attached to, if any.
- .namespace_at(pins, position) ⇒ Pin::Namespace?
-
.nested_scope(directive) ⇒ ::Symbol?
The scope set by a @!scope directive that appears in the indented text of another directive:.
- .no_empty_lines?(code, line1, line2) ⇒ Boolean
-
.parse_scope(directive) ⇒ ::Symbol?
:class, :instance, or nil if the directive is malformed.
- .process_directive(source, pins, source_position, comment_position, directive) ⇒ Array<Solargraph::Pin::Base>
-
.scopes_directive?(directive) ⇒ Boolean
True if the scope of the object defined by this directive can be set by a @!scope directive.
Class Method Details
.apply_nested(directive, pins) ⇒ void
This method returns an undefined value.
Apply a @!scope directive from the indented text of another directive to the pins that directive generated.
121 122 123 124 125 |
# File 'lib/solargraph/yard_map/directives/scope_directive.rb', line 121 def apply_nested directive, pins return unless scopes_directive? directive apply_scope pins, nested_scope(directive) end |
.apply_scope(pins, kind) ⇒ void
This method returns an undefined value.
Set the scope of pins generated by a directive.
107 108 109 110 111 112 113 |
# File 'lib/solargraph/yard_map/directives/scope_directive.rb', line 107 def apply_scope pins, kind return if kind.nil? # Only methods have a scope. Applying one to any other kind of pin, # such as a namespace generated by @!parse, would corrupt its path. pins.each { |pin| pin.scope = kind if pin.is_a?(Pin::Method) } end |
.apply_to_namespace(pins, kind, source_position) ⇒ void
This method returns an undefined value.
Applies the scope to each method that follows the directive within the namespace the directive appears in. Methods in nested namespaces are excluded, since the directive only affects its own namespace.
135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/solargraph/yard_map/directives/scope_directive.rb', line 135 def apply_to_namespace pins, kind, source_position namespace = namespace_at pins, source_position return if namespace.nil? pins.each do |pin| next unless pin.is_a?(Pin::Method) next unless pin.namespace == namespace.path # @sg-ignore flow sensitive typing should narrow location after the nil check next if pin.location.nil? || pin.location.range.start.line < source_position.line pin.scope = kind end end |
.attached_method(pins, source_position) ⇒ Pin::Method?
The method defined on the line the comment is attached to, if any.
154 155 156 |
# File 'lib/solargraph/yard_map/directives/scope_directive.rb', line 154 def attached_method pins, source_position pins.select { |pin| pin.is_a?(Pin::Method) && pin.location&.range&.contain?(source_position) }.last end |
.namespace_at(pins, position) ⇒ Pin::Namespace?
161 162 163 |
# File 'lib/solargraph/yard_map/directives/scope_directive.rb', line 161 def namespace_at pins, position pins.select { |pin| pin.is_a?(Pin::Namespace) && pin.location&.range&.contain?(position) }.last end |
.nested_scope(directive) ⇒ ::Symbol?
The scope set by a @!scope directive that appears in the indented text of another directive:
@!method foo
@!scope class
YARD parses the indented text of a directive with a state of its own, so a directive nested this way applies only to the object the enclosing directive defines. It never affects the objects that follow.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/solargraph/yard_map/directives/scope_directive.rb', line 87 def nested_scope directive text = directive.tag.text.to_s return nil unless text.include? '@!scope' kind = nil Solargraph::Source.parse_docstring(text).directives.each do |nested| next unless nested.tag.tag_name == 'scope' # A later directive overrides an earlier one, but an invalid value # leaves the current one in place, as it does in YARD. kind = parse_scope(nested) || kind end kind end |
.no_empty_lines?(code, line1, line2) ⇒ Boolean
169 170 171 172 173 174 |
# File 'lib/solargraph/yard_map/directives/scope_directive.rb', line 169 def no_empty_lines? code, line1, line2 lines = code.lines[line1..line2] return false if lines.nil? lines.none? { |line| line.strip.empty? } end |
.parse_scope(directive) ⇒ ::Symbol?
Returns :class, :instance, or nil if the directive is malformed.
60 61 62 63 |
# File 'lib/solargraph/yard_map/directives/scope_directive.rb', line 60 def parse_scope directive kind = directive.tag.text.to_s.strip.downcase.to_sym VALID_SCOPES.include?(kind) ? kind : nil end |
.process_directive(source, pins, source_position, comment_position, directive) ⇒ Array<Solargraph::Pin::Base>
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/solargraph/yard_map/directives/scope_directive.rb', line 44 def process_directive source, pins, source_position, comment_position, directive kind = parse_scope directive return [] if kind.nil? attached = attached_method pins, source_position if attached && no_empty_lines?(source.code, comment_position.line, source_position.line) attached.scope = kind else apply_to_namespace pins, kind, source_position end [] end |
.scopes_directive?(directive) ⇒ Boolean
True if the scope of the object defined by this directive can be set by a @!scope directive.
70 71 72 |
# File 'lib/solargraph/yard_map/directives/scope_directive.rb', line 70 def scopes_directive? directive SCOPED_DIRECTIVES.include? directive.tag.tag_name end |