Module: Solargraph::Rails::Util

Defined in:
lib/solargraph/rails/util.rb

Class Method Summary collapse

Class Method Details

.build_location(ast, path) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/solargraph/rails/util.rb', line 71

def self.build_location(ast, path)
  Solargraph::Location.new(
    File.expand_path(path),
    Solargraph::Range.from_to(
      ast.location.first_line,
      0,
      ast.location.last_line,
      ast.location.column
    )
  )
end

.build_module_extend(ns, module_name, location) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/solargraph/rails/util.rb', line 56

def self.build_module_extend(ns, module_name, location)
  Solargraph::Pin::Reference::Extend.new(
    closure: ns,
    name: module_name,
    location: location
  )
end

.build_module_include(ns, module_name, location) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/solargraph/rails/util.rb', line 48

def self.build_module_include(ns, module_name, location)
  Solargraph::Pin::Reference::Include.new(
    closure: ns,
    name: module_name,
    location: location
  )
end

.build_public_method(ns, name, comments: nil, parameters: [], types: nil, params: {}, location: nil, attribute: false, scope: :instance) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
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
# File 'lib/solargraph/rails/util.rb', line 4

def self.build_public_method(
  ns,
  name,
  comments: nil,
  parameters: [],
  types: nil,
  params: {},
  location: nil,
  attribute: false,
  scope: :instance
)
  opts = {
    name: name,
    parameters: parameters,
    location: location,
    closure: ns,
    scope: scope,
    attribute: attribute
  }

  comments_arr = [comments].compact
  params.each do |name, types|
    comments_arr << "@param [#{types.join(',')}] #{name}"
  end
  comments_arr << "@return [#{types.join(',')}]" if types

  opts[:comments] ||= comments_arr.join("\n")

  m = Solargraph::Pin::Method.new(**opts)
  parameters = parameters + params.map do |name, type|
      Solargraph::Pin::Parameter.new(
        location: nil,
        closure: m,
        comments: '',
        name: name,
        presence: nil,
        decl: :arg,
        asgn_code: nil
      )
  end
  m.parameters.concat(parameters)
  m
end

.dummy_location(path) ⇒ Object



64
65
66
67
68
69
# File 'lib/solargraph/rails/util.rb', line 64

def self.dummy_location(path)
  Solargraph::Location.new(
    File.expand_path(path),
    Solargraph::Range.from_to(0, 0, 0, 0)
  )
end

.extract_option(call_node, option_name) ⇒ AST::Node?

Extract the value of a given option from a :send syntax node.

E.g. given an AST node for ‘foo(:bar, baz: qux)`, you can use `extract_option(node, :baz)` to get the AST node for qux.

Parameters:

  • call_node (AST::Node)
  • option_name (Symbol)

Returns:

  • (AST::Node, nil)


95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/solargraph/rails/util.rb', line 95

def self.extract_option(call_node, option_name)
  options = call_node.children[3..-1].find { |n| n.type == :hash }
  return unless options

  pair =
    options.children.find do |n|
      n.children[0] && n.children[0].deconstruct == [:sym, option_name]
    end
  return unless pair

  pair.children[1]
end

.method_return(path, type) ⇒ Object



83
84
85
# File 'lib/solargraph/rails/util.rb', line 83

def self.method_return(path, type)
  Solargraph::Pin::Reference::Override.method_return(path, type)
end