Class: Solargraph::YardMap

Inherits:
Object
  • Object
show all
Defined in:
lib/solargraph/yard_map.rb,
lib/solargraph/yard_map/cache.rb,
lib/solargraph/yard_map/mapper.rb,
lib/solargraph/yard_map/helpers.rb,
lib/solargraph/yard_map/core_gen.rb,
lib/solargraph/yard_map/core_docs.rb,
lib/solargraph/yard_map/to_method.rb,
lib/solargraph/yard_map/core_fills.rb,
lib/solargraph/yard_map/rdoc_to_yard.rb,
lib/solargraph/yard_map/stdlib_fills.rb,
lib/solargraph/yard_map/mapper/to_method.rb,
lib/solargraph/yard_map/mapper/to_constant.rb,
lib/solargraph/yard_map/mapper/to_namespace.rb

Overview

The YardMap provides access to YARD documentation for the Ruby core, the stdlib, and gems.

Defined Under Namespace

Modules: CoreDocs, CoreFills, CoreGen, Helpers, RdocToYard, StdlibFills Classes: Cache, Mapper, ToMethod

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(required: [], gemset: {}, with_dependencies: true) ⇒ YardMap

Returns a new instance of YardMap.

Parameters:

  • required (Array<String>) (defaults to: [])
  • gemset (Hash{String => String}) (defaults to: {})
  • with_dependencies (Boolean) (defaults to: true)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/solargraph/yard_map.rb', line 54

def initialize(required: [], gemset: {}, with_dependencies: true)
  # HACK: YardMap needs its own copy of this array
  @required = required.clone
  # HACK: Hardcoded YAML handling
  @required.push 'psych' if @required.include?('yaml')
  @with_dependencies = with_dependencies
  @gem_paths = {}
  @stdlib_namespaces = []
  @gemset = gemset
  @source_gems = []
  process_requires
  yardocs.uniq!
  @pin_select_cache = {}
end

Instance Attribute Details

#gemsetHash{String => String} (readonly)

A hash of gem names and the version numbers to include in the map.

Returns:

  • (Hash{String => String})


49
50
51
# File 'lib/solargraph/yard_map.rb', line 49

def gemset
  @gemset
end

#requiredArray<String> (readonly)

Returns:



41
42
43
# File 'lib/solargraph/yard_map.rb', line 41

def required
  @required
end

#with_dependencies=(value) ⇒ Boolean (writeonly)

Returns:

  • (Boolean)


44
45
46
# File 'lib/solargraph/yard_map.rb', line 44

def with_dependencies=(value)
  @with_dependencies = value
end

Instance Method Details

#change(new_requires, new_gemset, source_gems = []) ⇒ Boolean

Parameters:

  • new_requires (Array<String>)
  • new_gemset (Hash{String => String})

Returns:

  • (Boolean)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/solargraph/yard_map.rb', line 82

def change new_requires, new_gemset, source_gems = []
  # HACK: Hardcoded YAML handling
  new_requires.push 'psych' if new_requires.include?('yaml')
  if new_requires.uniq.sort == required.uniq.sort && new_gemset == gemset && @source_gems.uniq.sort == source_gems.uniq.sort
    false
  else
    required.clear
    required.concat new_requires
    @gemset = new_gemset
    @source_gems = source_gems
    process_requires
    @rebindable_method_names = nil
    @pin_class_hash = nil
    @pin_select_cache = {}
    true
  end
end

#core_pinsArray<Solargraph::Pin::Base>



134
135
136
137
# File 'lib/solargraph/yard_map.rb', line 134

def core_pins
  # Using a class variable to reduce loads
  @@core_pins ||= load_core_pins
end

#load_yardoc(y) ⇒ YARD::Registry

Parameters:

  • y (String)

Returns:

  • (YARD::Registry)


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

def load_yardoc y
  if y.is_a?(Array)
    YARD::Registry.load y, true
  else
    YARD::Registry.load! y
  end
rescue StandardError => e
  Solargraph::Logging.logger.warn "Error loading yardoc '#{y}' #{e.class} #{e.message}"
  yardocs.delete y
  nil
end

#path_pin(path) ⇒ Pin::Base

Parameters:

  • path (String)

Returns:



141
142
143
# File 'lib/solargraph/yard_map.rb', line 141

def path_pin path
  pins.select { |p| p.path == path }.first
end

#pinsArray<Solargraph::Pin::Base>



70
71
72
# File 'lib/solargraph/yard_map.rb', line 70

def pins
  @pins ||= []
end

#rebindable_method_namesSet<String>

Returns:

  • (Set<String>)


101
102
103
104
105
106
107
# File 'lib/solargraph/yard_map.rb', line 101

def rebindable_method_names
  @rebindable_method_names ||= pins_by_class(Pin::Method)
    .select { |pin| pin.comments && pin.comments.include?('@yieldself') }
    .map(&:name)
    .concat(['instance_eval', 'instance_exec', 'class_eval', 'class_exec', 'module_eval', 'module_exec'])
    .to_set
end

#require_reference(path) ⇒ Location

Get the location of a file referenced by a require path.

Parameters:

  • path (String)

Returns:



149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/solargraph/yard_map.rb', line 149

def require_reference path
  # @type [Gem::Specification]
  spec = spec_for_require(path)
  spec.full_require_paths.each do |rp|
    file = File.join(rp, "#{path}.rb")
    next unless File.file?(file)
    return Solargraph::Location.new(file, Solargraph::Range.from_to(0, 0, 0, 0))
  end
  nil
rescue Gem::LoadError
  nil
end

#stdlib_pathsObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/solargraph/yard_map.rb', line 25

def stdlib_paths
  @@stdlib_paths ||= begin
    result = {}
    YARD::Registry.load! CoreDocs.yardoc_stdlib_file
    YARD::Registry.all.each do |co|
      next if co.file.nil?
      path = co.file.sub(/^(ext|lib)\//, '').sub(/\.(rb|c)$/, '')
      base = path.split('/').first
      result[base] ||= []
      result[base].push co
    end
    result
  end
end

#stdlib_pinsObject



162
163
164
# File 'lib/solargraph/yard_map.rb', line 162

def stdlib_pins
  @stdlib_pins ||= []
end

#unresolved_requiresArray<String>

Returns:



115
116
117
# File 'lib/solargraph/yard_map.rb', line 115

def unresolved_requires
  @unresolved_requires ||= []
end

#with_dependencies?Boolean

Returns:

  • (Boolean)


74
75
76
77
# File 'lib/solargraph/yard_map.rb', line 74

def with_dependencies?
  @with_dependencies ||= true unless @with_dependencies == false
  @with_dependencies
end

#yardocsArray<String>

Returns:



110
111
112
# File 'lib/solargraph/yard_map.rb', line 110

def yardocs
  @yardocs ||= []
end