Class: RubyIndexer::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_indexer/lib/ruby_indexer/entry.rb

Defined Under Namespace

Classes: Accessor, BlockParameter, Class, ClassVariable, Constant, ConstantAlias, ForwardingParameter, GlobalVariable, Include, InstanceVariable, KeywordParameter, KeywordRestParameter, Member, Method, MethodAlias, Module, ModuleOperation, Namespace, OptionalKeywordParameter, OptionalParameter, Parameter, Prepend, RequiredParameter, RestParameter, Signature, SingletonClass, UnresolvedConstantAlias, UnresolvedMethodAlias

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, uri, location, comments) ⇒ Entry

: (String name, URI::Generic uri, Location location, String? comments) -> void



21
22
23
24
25
26
27
# File 'lib/ruby_indexer/lib/ruby_indexer/entry.rb', line 21

def initialize(name, uri, location, comments)
  @name = name
  @uri = uri
  @comments = comments
  @visibility = :public #: Symbol
  @location = location
end

Instance Attribute Details

#locationObject (readonly) Also known as: name_location

: RubyIndexer::Location



13
14
15
# File 'lib/ruby_indexer/lib/ruby_indexer/entry.rb', line 13

def location
  @location
end

#nameObject (readonly)

: String



7
8
9
# File 'lib/ruby_indexer/lib/ruby_indexer/entry.rb', line 7

def name
  @name
end

#uriObject (readonly)

: URI::Generic



10
11
12
# File 'lib/ruby_indexer/lib/ruby_indexer/entry.rb', line 10

def uri
  @uri
end

#visibilityObject

: Symbol



18
19
20
# File 'lib/ruby_indexer/lib/ruby_indexer/entry.rb', line 18

def visibility
  @visibility
end

Instance Method Details

#commentsObject

: -> String



61
62
63
64
65
66
67
68
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
# File 'lib/ruby_indexer/lib/ruby_indexer/entry.rb', line 61

def comments
  @comments ||= begin
    # Parse only the comments based on the file path, which is much faster than parsing the entire file
    path = file_path
    parsed_comments = path ? Prism.parse_file_comments(path) : []

    # Group comments based on whether they belong to a single block of comments
    grouped = parsed_comments.slice_when do |left, right|
      left.location.start_line + 1 != right.location.start_line
    end

    # Find the group that is either immediately or two lines above the current entry
    correct_group = grouped.find do |group|
      comment_end_line = group.last.location.start_line
      (comment_end_line..comment_end_line + 1).cover?(@location.start_line - 1)
    end

    # If we found something, we join the comments together. Otherwise, the entry has no documentation and we don't
    # want to accidentally re-parse it, so we set it to an empty string. If an entry is updated, the entire entry
    # object is dropped, so this will not prevent updates
    if correct_group
      correct_group.filter_map do |comment|
        content = comment.slice.chomp

        if content.valid_encoding?
          content.delete_prefix!("#")
          content.delete_prefix!(" ")
          content
        end
      end.join("\n")
    else
      ""
    end
  rescue Errno::ENOENT
    # If the file was deleted, but the entry hasn't been removed yet (could happen due to concurrency), then we do
    # not want to fail. Just set the comments to an empty string
    ""
  end
end

#file_nameObject

: -> String



45
46
47
48
49
50
51
52
53
# File 'lib/ruby_indexer/lib/ruby_indexer/entry.rb', line 45

def file_name
  if @uri.scheme == "untitled"
    @uri.opaque #: as !nil
  else
    File.basename(
      file_path, #: as !nil
    )
  end
end

#file_pathObject

: -> String?



56
57
58
# File 'lib/ruby_indexer/lib/ruby_indexer/entry.rb', line 56

def file_path
  @uri.full_path
end

#private?Boolean

: -> bool

Returns:

  • (Boolean)


40
41
42
# File 'lib/ruby_indexer/lib/ruby_indexer/entry.rb', line 40

def private?
  @visibility == :private
end

#protected?Boolean

: -> bool

Returns:

  • (Boolean)


35
36
37
# File 'lib/ruby_indexer/lib/ruby_indexer/entry.rb', line 35

def protected?
  @visibility == :protected
end

#public?Boolean

: -> bool

Returns:

  • (Boolean)


30
31
32
# File 'lib/ruby_indexer/lib/ruby_indexer/entry.rb', line 30

def public?
  @visibility == :public
end