Module: Solargraph::YardMap::RdocToYard

Extended by:
ApiMap::SourceToYard
Defined in:
lib/solargraph/yard_map/rdoc_to_yard.rb

Class Method Summary collapse

Methods included from ApiMap::SourceToYard

code_object_at, code_object_paths, rake_yard

Class Method Details

.base_name(mod) ⇒ Object



104
105
106
# File 'lib/solargraph/yard_map/rdoc_to_yard.rb', line 104

def self.base_name mod
  mod.full_name.split('::')[0..-2].join('::')
end

.commentary(cmnt) ⇒ String

Parameters:

  • cmnt (RDoc::Comment)

Returns:

  • (String)


110
111
112
113
114
115
116
117
118
# File 'lib/solargraph/yard_map/rdoc_to_yard.rb', line 110

def self.commentary cmnt
  return cmnt if cmnt.is_a?(String)
  return cmnt.text if cmnt.is_a?(RDoc::Comment)
  result = []
  cmnt.parts.each do |part|
    result.push RDoc::Markup::ToHtml.new({}).to_html(part.text) if part.respond_to?(:text)
  end
  result.join("\n\n")
end

.find_file(obj) ⇒ Object



131
132
133
134
135
136
137
# File 'lib/solargraph/yard_map/rdoc_to_yard.rb', line 131

def self.find_file obj
  if obj.respond_to?(:in_files) && !obj.in_files.empty?
    [obj.in_files.first.to_s.sub(/^file /, ''), obj.line]
  else
    [obj.file, obj.line]
  end
end

.locate(obj) ⇒ Object

Parameters:

  • obj (RDoc::Context)


121
122
123
124
125
126
127
128
129
# File 'lib/solargraph/yard_map/rdoc_to_yard.rb', line 121

def self.locate obj
  # @todo line is always nil for some reason
  file, line = find_file(obj)
  return nil if file.nil?
  Location.new(
    file,
    Range.from_to(line || 1, 0, line || 1, 0)
  )
end

.run(spec, cache_dir: nil) ⇒ void

This method returns an undefined value.

Parameters:

  • spec (Gem::Specification)
  • cache_dir (String) (defaults to: nil)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
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
# File 'lib/solargraph/yard_map/rdoc_to_yard.rb', line 16

def self.run spec, cache_dir: nil
  Dir.mktmpdir do |tmpdir|
    rdir = File.join(tmpdir, 'sg_tmp_rdoc')
    FileUtils.cp_r Dir.glob(File.join(spec.full_gem_path, '*')), tmpdir
    Dir.chdir tmpdir do
      pins = []
      pins.push Solargraph::Pin::ROOT_PIN
      name_hash = {}

      argv = ['-q', '-r', '-N', '-o', rdir]
      spec.load_paths.each do |path|
        argv.concat ['-i', path]
      end
      rdoc = RDoc::RDoc.new
      rdoc.document argv
      # @type [RDoc::Store]
      store = rdoc.store
      store.path = rdir
      store.cache[:modules].each do |mod|
        # @type [RDoc::NormalClass]
        mod = store.find_class_or_module(mod)
        closure = pins.select { |pin| pin.path == mod.full_name.split('::')[0..-2].join('::') }.first || pins.first
        namepin = Solargraph::Pin::Namespace.new(
          type: (mod.module? ? :module : :class),
          name: mod.name,
          comments: commentary(mod.comment),
          closure: closure,
          location: locate(mod)
        )
        mod.parse(mod.comment_location)
        # @param inc [RDoc::Include]
        mod.includes.each do |inc|
          pins.push Solargraph::Pin::Reference::Include.new(
            location: locate(inc),
            name: inc.name,
            closure: namepin
          )
        end
        # @param ext [RDoc::Extend]
        mod.extends.each do |ext|
          pins.push Solargraph::Pin::Reference::Extend.new(
            location: locate(ext),
            name: ext.name,
            closure: namepin
          )
        end
        pins.push namepin
        name_hash[mod.full_name] = namepin
        # @param met [RDoc::AnyMethod]
        mod.each_method do |met|
          pin = Solargraph::SourceMap.load_string("def Object.tmp#{met.param_seq};end").first_pin('Object.tmp') || Solargraph::Pin::Method.new
          pins.push Solargraph::Pin::Method.new(
            name: met.name,
            closure: namepin,
            comments: commentary(met.comment),
            scope: met.type.to_sym,
            parameters: pin.parameters,
            visibility: met.visibility,
            location: locate(met)
          )
        end
        # @param const [RDoc::Constant]
        mod.each_constant do |const|
          pins.push Solargraph::Pin::Constant.new(
            name: const.name,
            closure: namepin,
            comments: commentary(const.comment),
            location: locate(const)
          )
        end
      end
      mapstore = Solargraph::ApiMap::Store.new(pins)
      rake_yard(mapstore)
      YARD::Registry.clear
      code_object_map.values.each do |co|
        YARD::Registry.register(co)
      end
      cache_dir ||= File.join(Solargraph::YardMap::CoreDocs.cache_dir, 'gems', "#{spec.name}-#{spec.version}", "yardoc")
      FileUtils.remove_entry_secure cache_dir if File.exist?(cache_dir)
      FileUtils.mkdir_p cache_dir
      # @todo Should merge be true?
      YARD::Registry.save true, cache_dir
      # Clear the serialized cache if it exists
      FileUtils.safe_unlink File.join(CoreDocs.cache_dir, 'gems', "#{spec.name}-#{spec.version}.ser")
    end
  end
end