Module: Canopus::Workspace::DebugAware

Defined in:
lib/canopus/workspace/debug_aware.rb

Constant Summary collapse

DEBUG_HOVER_EXPRESSION_LIMIT =
256
DEBUG_HOVER_TEXT_LIMIT =
4_096
DEBUG_HOVER_SCOPE_LIMIT =
16
DEBUG_HOVER_VARIABLE_LIMIT =
256
DEBUG_HOVER_TOKEN_LIMIT =
4_096
DEBUG_HOVER_ROW_LIMIT =
256
DEBUG_HOVER_NAME =
/\A[A-Za-z_][A-Za-z0-9_]*\z/n
DEBUG_HOVER_DIRECT_NAME =
/\A(?:@@?|\$)[A-Za-z_][A-Za-z0-9_]*\z/n
DEBUG_HOVER_DIRECT_TOKENS =
%w[Name.Variable.Instance Name.Variable.Class Name.Variable.Global].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#debug_console ⇒ Object (readonly)

Returns the value of attribute debug_console.



42
43
44
# File 'lib/canopus/workspace/debug_aware.rb', line 42

def debug_console
  @debug_console
end

#debug_panel ⇒ Object (readonly)

Returns the value of attribute debug_panel.



42
43
44
# File 'lib/canopus/workspace/debug_aware.rb', line 42

def debug_panel
  @debug_panel
end

#debug_position ⇒ Object (readonly)

Returns the value of attribute debug_position.



42
43
44
# File 'lib/canopus/workspace/debug_aware.rb', line 42

def debug_position
  @debug_position
end

#debug_session ⇒ Object (readonly)

Returns the value of attribute debug_session.



42
43
44
# File 'lib/canopus/workspace/debug_aware.rb', line 42

def debug_session
  @debug_session
end

Instance Method Details

#accept_breakpoint_palette ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/canopus/workspace/debug_aware.rb', line 163

def accept_breakpoint_palette
  state = @palette.fetch(:breakpoint)
  unless breakpoint_palette_current?(state)
    self.palette = nil
    self.message = "Buffer changed; open the breakpoint menu again"
    return
  end
  if @palette[:kind] == :breakpoint_edit
    value = @palette[:query].empty? ? nil : @palette[:query]
    entry = breakpoint_entry(state)
    options = {@palette.fetch(:field) => value}
    self.palette = nil
    return entry ? @breakpoints.update(state[:path], state[:line], **options) :
      @breakpoints.add(state[:path], state[:line], **options)
  end

  action = @palette.fetch(:actions).fetch(@palette[:index])
  entry = breakpoint_entry(state)
  case action
  when :add
    self.palette = nil
    @breakpoints.add(state[:path], state[:line])
  when :remove
    self.palette = nil
    @breakpoints.remove(state[:path], state[:line])
  when :enabled
    raise Error, "breakpoint does not exist" unless entry
    self.palette = nil
    @breakpoints.update(state[:path], state[:line], enabled: !entry.enabled)
  else
    value = entry&.public_send(action)
    self.palette = {kind: :breakpoint_edit, query: +(value || ""), index: 0, matches: [],
                    breakpoint: state, field: action}
  end
end

#accept_debug_console_palette ⇒ Object



84
85
86
87
88
# File 'lib/canopus/workspace/debug_aware.rb', line 84

def accept_debug_console_palette
  expression = @palette.fetch(:query)
  self.palette = nil
  @debug_console.evaluate(expression)
end

#accept_debug_watch_palette ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/canopus/workspace/debug_aware.rb', line 65

def accept_debug_watch_palette
  state = @palette
  self.palette = nil
  if state[:kind] == :debug_watch_add
    add_debug_watch(state[:query])
  else
    index = state[:indices] ? state[:indices][state[:index]] : state[:index]
    expression = index && state[:items][index]
    expression && remove_debug_watch(expression)
  end
end

#add_debug_watch(expression) ⇒ Object



48
# File 'lib/canopus/workspace/debug_aware.rb', line 48

def add_debug_watch(expression) = @debug_panel.add_watch(expression)

#attach_breakpoints(buffer) ⇒ Object



199
200
201
# File 'lib/canopus/workspace/debug_aware.rb', line 199

def attach_breakpoints(buffer)
  @breakpoints.attach(buffer) if breakpoint_buffer?(buffer)
end

#breakpoint_decorations(buffer, rows, current = nil) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/canopus/workspace/debug_aware.rb', line 120

def breakpoint_decorations(buffer, rows, current = nil)
  return [] unless breakpoint_buffer?(buffer)
  last = [rows.end, buffer.line_count].min
  entries = @breakpoints.for_path(buffer.path).filter_map do |entry|
    row = entry.line - 1
    [row, entry] if row.between?(rows.begin, last - 1)
  end.to_h
  requested = current.is_a?(Editor) && current.buffer.equal?(buffer) ? (rows.begin...last) : entries.keys.sort
  requested.map do |row|
    entry = entries[row]
    state = entry ? (entry.enabled ? "Enabled" : "Disabled") : "Set"
    details = entry && [entry.condition, entry.hit_condition, entry.log_message].compact
    label = "#{state} breakpoint on line #{row + 1}"
    label += ": #{details.join(', ')}" if details && !details.empty?
    color = entry ? (entry.enabled ? :error : :muted) : "#0000"
    style = {color: color, gutter_offset: 7, gutter_width: 3, hit_offset: 7, hit_width: 4}
    Decoration::Item.new(:gutter, nil, row, label, style, 20, :breakpoint, @breakpoint_click)
  end
end

#debug_console_tree ⇒ Object



45
# File 'lib/canopus/workspace/debug_aware.rb', line 45

def debug_console_tree = @debug_console.tree

#debug_hover(current = editor, offset = current&.primary&.head) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/canopus/workspace/debug_aware.rb', line 90

def debug_hover(current = editor, offset = current&.primary&.head)
  session, frame = @debug_session, @debug_panel.selected_frame
  unless session && frame && debug_hover_editor?(current) && offset.is_a?(Integer)
    clear_debug_hover
    return false
  end
  candidate = debug_hover_expression(current, offset)
  unless candidate
    clear_debug_hover
    return false
  end
  expression, direct = candidate
  key = [session, @debug_generation, frame.id, current, current.buffer.version, expression].freeze
  return true if @debug_hover_key == key

  clear_debug_hover
  token = @debug_hover_token = Object.new.freeze
  @debug_hover_key = key
  direct ? request_debug_hover_evaluate(token, key) : request_debug_hover_scopes(token, key)
  true
rescue StandardError
  clear_debug_hover
  false
end

#debug_position_decorations(buffer, rows) ⇒ Object



267
268
269
270
271
272
# File 'lib/canopus/workspace/debug_aware.rb', line 267

def debug_position_decorations(buffer, rows)
  position = @debug_position
  return [] unless position && buffer.path == position[:path] && rows.cover?(position[:row])

  [Decoration::Item.new(:line, nil, position[:row], nil, "#f9758333", 10, :debug_position, nil)]
end

#debug_tree ⇒ Object



44
# File 'lib/canopus/workspace/debug_aware.rb', line 44

def debug_tree = @debug_panel.tree

#debug_watches ⇒ Object



46
# File 'lib/canopus/workspace/debug_aware.rb', line 46

def debug_watches = @debug_panel.watches

#detach_breakpoints(buffer) ⇒ Object



209
# File 'lib/canopus/workspace/debug_aware.rb', line 209

def detach_breakpoints(buffer) = @breakpoints.detach(buffer)

#dismiss_hover ⇒ Object



115
116
117
118
# File 'lib/canopus/workspace/debug_aware.rb', line 115

def dismiss_hover
  clear_debug_hover
  super
end

#initialize_breakpoints ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/canopus/workspace/debug_aware.rb', line 22

def initialize_breakpoints
  @breakpoints = Debug::Breakpoints.new(root: @root) do |_path|
    @decorations.invalidate(:breakpoint)
    @debug_panel&.refresh_breakpoints
    @window&.request_frame
  end
  @breakpoint_click = BreakpointClick.new(self)
  @decorations.register(:breakpoint) { |buffer, rows, current| breakpoint_decorations(buffer, rows, current) }
  @debug_generation = 0
  @debug_session = @debug_thread = @debug_position = nil
  @debug_panel = Debug::Panel.new(breakpoints: @breakpoints,
    post: ->(&block) { post(&block) },
    select_frame: ->(session, frame) { select_debug_frame(session, frame) },
    select_breakpoint: ->(entry) { select_debug_breakpoint(entry) },
    report: ->(text) { notify(text) }, request_frame: -> { @window&.request_frame })
  @debug_console = Debug::Console.new(post: ->(&block) { post(&block) },
    request_frame: -> { @window&.request_frame })
  @decorations.register(:debug_position) { |buffer, rows| debug_position_decorations(buffer, rows) }
end

#relocate_breakpoints(buffer) ⇒ Object



203
204
205
206
207
# File 'lib/canopus/workspace/debug_aware.rb', line 203

def relocate_breakpoints(buffer)
  @breakpoints.detach(buffer)
  attach_breakpoints(buffer)
  @decorations.invalidate(:breakpoint, buffer: buffer)
end

#remove_debug_watch(expression) ⇒ Object



49
# File 'lib/canopus/workspace/debug_aware.rb', line 49

def remove_debug_watch(expression) = @debug_panel.remove_watch(expression)

#show_breakpoint_menu(current, row) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/canopus/workspace/debug_aware.rb', line 145

def show_breakpoint_menu(current, row)
  return unless breakpoint_row?(current, row)
  path, line = current.buffer.path, row + 1
  entry = @breakpoints.for_path(path).find { |item| item.line == line }
  actions = if entry
    [[:condition, "Edit condition"], [:hit_condition, "Edit hit count"],
      [:log_message, "Edit log message"], [:enabled, entry.enabled ? "Disable breakpoint" : "Enable breakpoint"],
      [:remove, "Remove breakpoint"]]
  else
    [[:add, "Add breakpoint"], [:condition, "Add conditional breakpoint"],
      [:hit_condition, "Add hit-count breakpoint"], [:log_message, "Add logpoint"]]
  end
  self.palette = {kind: :breakpoint_actions, query: +"", index: 0, matches: actions.map(&:last),
                  actions: actions.map(&:first),
                  breakpoint: {path: path, line: line, buffer: current.buffer,
                               version: current.buffer.version, entry: entry}}
end

#show_debug_console ⇒ Object

Raises:



77
78
79
80
81
82
# File 'lib/canopus/workspace/debug_aware.rb', line 77

def show_debug_console
  raise Error, "debug session is not stopped" unless @debug_session && @debug_panel.selected_frame

  @panels.show("debug_console")
  self.palette = {kind: :debug_console, query: +"", index: 0, matches: []}
end

#show_debug_watch_add ⇒ Object



51
52
53
# File 'lib/canopus/workspace/debug_aware.rb', line 51

def show_debug_watch_add
  self.palette = {kind: :debug_watch_add, query: +"", index: 0, matches: []}
end

#show_debug_watch_remove ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/canopus/workspace/debug_aware.rb', line 55

def show_debug_watch_remove
  if debug_watches.empty?
    self.message = "No debug watch expressions"
    return false
  end
  self.palette = {kind: :debug_watch_remove, query: +"", index: 0,
    matches: debug_watches.dup, items: debug_watches}
  update_palette
end

#start_debugging(configuration = nil) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/canopus/workspace/debug_aware.rb', line 211

def start_debugging(configuration = nil)
  loader = Debug::Configuration.new(root: @root, settings: @settings)
  if configuration.nil?
    configurations = loader.configurations
    raise Error, "no debug configurations are available" if configurations.empty?
    if configurations.length > 1
      self.palette = {kind: :debug_configurations, query: +"", index: 0,
        matches: configurations.map { |item| item.fetch("name") }, items: configurations}
      return nil
    end
    configuration = configurations.first
  end
  context = debug_configuration_context
  resolved = loader.resolve(configuration, **context)
  adapter = loader.adapter(resolved.fetch("type"), **context)
  stop_debugging if @debug_session
  generation = @debug_generation += 1
  session = build_debug_session(configuration: resolved, adapter: adapter)
  session.on(:stopped) { |frame| post { handle_debug_stop(session, generation, frame) } }
  session.on(:continued) { post { handle_debug_continue(session, generation) } }
  session.on(:output) { |event| handle_debug_output(session, generation, event) }
  session.on(:terminated) { post { finish_debug_session(session, generation) } }
  session.on(:error) { |error| post { report_debug_error(session, generation, error) } }
  @debug_session = session
  @debug_console.attach(session)
  @debug_thread = Thread.new do
    session.start
    post { notify("Debug session started: #{resolved.fetch('name')}") if current_debug_session?(session, generation) }
  rescue StandardError => error
    post { fail_debug_session(session, generation, error) }
  end
  @debug_thread.report_on_exception = false
  session
end

#stop_debugging ⇒ Object



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/canopus/workspace/debug_aware.rb', line 246

def stop_debugging
  session, thread = @debug_session, @debug_thread
  @debug_generation += 1
  @debug_session = @debug_thread = nil
  @debug_panel.clear
  @debug_console.detach
  clear_debug_hover
  clear_debug_position
  failure = nil
  begin
    session&.close
  rescue StandardError => error
    failure = error
  ensure
    thread.join if thread && thread != Thread.current
  end
  raise failure if failure

  session
end

#toggle_breakpoint(current, row) ⇒ Object



140
141
142
143
# File 'lib/canopus/workspace/debug_aware.rb', line 140

def toggle_breakpoint(current, row)
  return unless breakpoint_row?(current, row)
  @breakpoints.toggle(current.buffer.path, row + 1)
end