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(configuration, name, uri, location, comments) ⇒ Entry

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



24
25
26
27
28
29
30
31
# File 'lib/ruby_indexer/lib/ruby_indexer/entry.rb', line 24

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

Instance Attribute Details

#configurationObject (readonly)

: Configuration



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

def configuration
  @configuration
end

#locationObject (readonly) Also known as: name_location

: RubyIndexer::Location



16
17
18
# File 'lib/ruby_indexer/lib/ruby_indexer/entry.rb', line 16

def location
  @location
end

#nameObject (readonly)

: String



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

def name
  @name
end

#uriObject (readonly)

: URI::Generic



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

def uri
  @uri
end

#visibilityObject

: Symbol



21
22
23
# File 'lib/ruby_indexer/lib/ruby_indexer/entry.rb', line 21

def visibility
  @visibility
end

Instance Method Details

#commentsObject

: -> String



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
100
101
102
103
# File 'lib/ruby_indexer/lib/ruby_indexer/entry.rb', line 65

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.match?(@configuration.magic_comment_regex)
          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



49
50
51
52
53
54
55
56
57
# File 'lib/ruby_indexer/lib/ruby_indexer/entry.rb', line 49

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

#file_pathObject

: -> String?



60
61
62
# File 'lib/ruby_indexer/lib/ruby_indexer/entry.rb', line 60

def file_path
  @uri.full_path
end

#private?Boolean

: -> bool



44
45
46
# File 'lib/ruby_indexer/lib/ruby_indexer/entry.rb', line 44

def private?
  @visibility == :private
end

#protected?Boolean

: -> bool



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

def protected?
  @visibility == :protected
end

#public?Boolean

: -> bool



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

def public?
  @visibility == :public
end