Class: Solargraph::Suggestion

Inherits:
Object
  • Object
show all
Defined in:
lib/solargraph/suggestion.rb

Overview

Information about a class, module, method, or variable.

Constant Summary collapse

CLASS =
'Class'
CONSTANT =
'Constant'
FIELD =
'Field'
KEYWORD =
'Keyword'
METHOD =
'Method'
MODULE =
'Module'
PROPERTY =
'Property'
SNIPPET =
'Snippet'
VARIABLE =
'Variable'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label, kind: KEYWORD, insert: nil, detail: nil, docstring: nil, code_object: nil, location: nil, arguments: [], return_type: nil, path: nil) ⇒ Suggestion

Returns a new instance of Suggestion.



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/solargraph/suggestion.rb', line 41

def initialize label, kind: KEYWORD, insert: nil, detail: nil, docstring: nil, code_object: nil, location: nil, arguments: [], return_type: nil, path: nil
  @helper = Server::Helpers.new
  @label = label.to_s
  @kind = kind
  @insert = insert || @label
  @detail = detail
  @code_object = code_object
  @docstring = docstring
  @location = location
  @arguments = arguments
  @return_type = return_type
  @path = path
end

Instance Attribute Details

#arguments ⇒ Array<String> (readonly)

Returns:

  • (Array<String>)


36
37
38
# File 'lib/solargraph/suggestion.rb', line 36

def arguments
  @arguments
end

#code_object ⇒ YARD::CodeObjects::Base (readonly)

Returns:

  • (YARD::CodeObjects::Base)


39
40
41
# File 'lib/solargraph/suggestion.rb', line 39

def code_object
  @code_object
end

#detail ⇒ String (readonly)

Returns:

  • (String)


27
28
29
# File 'lib/solargraph/suggestion.rb', line 27

def detail
  @detail
end

#insert ⇒ String (readonly)

Returns:

  • (String)


24
25
26
# File 'lib/solargraph/suggestion.rb', line 24

def insert
  @insert
end

#kind ⇒ String (readonly)

Returns:

  • (String)


21
22
23
# File 'lib/solargraph/suggestion.rb', line 21

def kind
  @kind
end

#label ⇒ String (readonly)

Returns:

  • (String)


18
19
20
# File 'lib/solargraph/suggestion.rb', line 18

def label
  @label
end

#location ⇒ String (readonly)

Returns:

  • (String)


33
34
35
# File 'lib/solargraph/suggestion.rb', line 33

def location
  @location
end

Class Method Details

.pull(pin, return_type = nil) ⇒ Object

Generate a suggestion from a pin.

Parameters:



144
145
146
# File 'lib/solargraph/suggestion.rb', line 144

def self.pull pin, return_type = nil
  Suggestion.new(pin.name, insert: pin.name.gsub(/=/, ' = '), kind: pin.kind, docstring: pin.docstring, detail: pin.namespace, arguments: pin.parameters, path: pin.path, return_type: return_type || pin.return_type, location: pin.location)
end

Instance Method Details

#as_json(args = {}) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/solargraph/suggestion.rb', line 120

def as_json args = {}
  result = {
    label: @label,
    kind: @kind,
    insert: @insert,
    detail: @detail,
    path: path,
    location: (@location.nil? ? nil : @location.to_s),
    arguments: @arguments,
    params: params,
    return_type: return_type,
    has_doc: has_doc?
  }
  result[:documentation] = documentation if args[:all]
  result
end

#docstring ⇒ YARD::Docstring

Returns:

  • (YARD::Docstring)


84
85
86
# File 'lib/solargraph/suggestion.rb', line 84

def docstring
  @docstring ||= @code_object.nil? ? nil : @code_object.docstring
end

#documentation ⇒ String

Returns:

  • (String)


89
90
91
# File 'lib/solargraph/suggestion.rb', line 89

def documentation
  @documentation ||= (docstring.nil? ? '' : @helper.html_markup_rdoc(docstring))
end

#has_doc? ⇒ Boolean

True if the suggestion has documentation. Useful for determining whether a client should resolve a suggestion's path to retrieve more information about it.

Returns:

  • (Boolean)


116
117
118
# File 'lib/solargraph/suggestion.rb', line 116

def has_doc?
  !documentation.empty?
end

#params ⇒ Array<String>

Returns:

  • (Array<String>)


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/solargraph/suggestion.rb', line 94

def params
  if @params.nil?
    @params = []
    return @params if docstring.nil?
    param_tags = docstring.tags(:param)
    unless param_tags.empty?
      param_tags.each do |t|
        txt = t.name.to_s
        txt += " [#{t.types.join(',')}]" unless t.types.nil? or t.types.empty?
        txt += " #{t.text}" unless t.text.nil? or t.text.empty?
        @params.push txt
      end
    end
  end
  @params
end

#path ⇒ String

The full path of the suggestion.

Examples:

  • MyClass
  • MyModule::MyClass
  • MyClass#instance_method
  • MyModule.singleton_method

Returns:

  • (String)


64
65
66
# File 'lib/solargraph/suggestion.rb', line 64

def path
  @path ||= (code_object.nil? ? label : code_object.path)
end

#return_type ⇒ String

Returns:

  • (String)


74
75
76
77
78
79
80
81
# File 'lib/solargraph/suggestion.rb', line 74

def return_type
  if @return_type.nil? and !docstring.nil?
    ol = docstring.tag(:overload)
    t = ol.nil? ? docstring.tag(:return) : ol.tag(:return)
    @return_type = t.types[0] unless t.nil? or t.types.nil?
  end
  @return_type
end

#to_json(args = {}) ⇒ Object



137
138
139
# File 'lib/solargraph/suggestion.rb', line 137

def to_json args = {}
  as_json.to_json(args)
end

#to_s ⇒ String

Returns:

  • (String)


69
70
71
# File 'lib/solargraph/suggestion.rb', line 69

def to_s
  label
end