Class: Steep::Typing::CursorContext

Inherits:
Object
  • Object
show all
Defined in:
lib/steep/typing.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index) ⇒ CursorContext

Returns a new instance of CursorContext.



19
20
21
# File 'lib/steep/typing.rb', line 19

def initialize(index)
  @index = index
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



17
18
19
# File 'lib/steep/typing.rb', line 17

def data
  @data
end

#indexObject (readonly)

Returns the value of attribute index.



15
16
17
# File 'lib/steep/typing.rb', line 15

def index
  @index
end

Instance Method Details

#block_range(node) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/steep/typing.rb', line 125

def block_range(node)
  case node.type
  when :block
    send_node, args_node, _ = node.children
    begin_pos = if send_node.type != :lambda && args_node.loc.expression
                  args_node.loc.expression.end_pos
                else
                  node.loc.begin.end_pos # steep:ignore NoMethod
                end
    end_pos = node.loc.end.begin_pos # steep:ignore NoMethod
  when :numblock
    send_node, _ = node.children
    begin_pos = node.loc.begin.end_pos # steep:ignore NoMethod
    end_pos = node.loc.end.begin_pos # steep:ignore NoMethod
  end

  begin_pos..end_pos
end

#contextObject



149
150
151
152
# File 'lib/steep/typing.rb', line 149

def context
  _, context = data
  context
end

#rangeObject



144
145
146
147
# File 'lib/steep/typing.rb', line 144

def range
  range, _ = data
  range
end

#set(range, context = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/steep/typing.rb', line 23

def set(range, context = nil)
  if range.is_a?(CursorContext)
    range, context = range.data
    range or return
    context or return
  end

  context or raise
  return unless index

  if current_range = self.range
    if range.begin <= index && index <= range.end
      if current_range.begin <= range.begin && range.end <= current_range.end
        @data = [range, context]
      end
    end
  else
    @data = [range, context]
  end
end

#set_body_context(node, context) ⇒ Object



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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/steep/typing.rb', line 51

def set_body_context(node, context)
  case node.type
  when :class
    name_node, super_node, _ = node.children
    begin_pos = if super_node
                  super_node.loc.expression.end_pos
                else
                  name_node.loc.expression.end_pos
                end
    end_pos = node.loc.end.begin_pos # steep:ignore NoMethod

    set(begin_pos..end_pos, context)

  when :module
    name_node = node.children[0]
    begin_pos = name_node.loc.expression.end_pos
    end_pos = node.loc.end.begin_pos # steep:ignore NoMethod
    set(begin_pos..end_pos, context)

  when :sclass
    name_node = node.children[0]
    begin_pos = name_node.loc.expression.end_pos
    end_pos = node.loc.end.begin_pos # steep:ignore NoMethod
    set(begin_pos..end_pos, context)

  when :def, :defs
    if node.children.last
      args_node =
        case node.type
        when :def
          node.children[1]
        when :defs
          node.children[2]
        end

      body_begin_pos =
        case
        when node.loc.assignment # steep:ignore NoMethod
          # endless def
          node.loc.assignment.end_pos # steep:ignore NoMethod
        when args_node.loc.expression
          # with args
          args_node.loc.expression.end_pos
        else
          # without args
          node.loc.name.end_pos # steep:ignore NoMethod
        end

      body_end_pos =
        if node.loc.end # steep:ignore NoMethod
          node.loc.end.begin_pos # steep:ignore NoMethod
        else
          node.loc.expression.end_pos
        end

      set(body_begin_pos..body_end_pos, context)
    end

  when :block, :numblock
    range = block_range(node)
    set(range, context)

  when :for
    _, collection, _ = node.children

    begin_pos = collection.loc.expression.end_pos
    end_pos = node.loc.end.begin_pos # steep:ignore NoMethod

    set(begin_pos..end_pos, context)
  else
    raise "Unexpected node for insert_context: #{node.type}"
  end
end

#set_node_context(node, context) ⇒ Object



44
45
46
47
48
49
# File 'lib/steep/typing.rb', line 44

def set_node_context(node, context)
  begin_pos = node.loc.expression.begin_pos
  end_pos = node.loc.expression.end_pos

  set(begin_pos..end_pos, context)
end