Method: AuthorEngine::CodeEditor::Cursor#move

Defined in:
lib/author_engine/code_editor/cursor.rb

#move(direction) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/author_engine/code_editor/cursor.rb', line 300

def move(direction)
  pos = @text_input.caret_pos
  line = nil

  if direction == :up
    return if @active_line == 0
    line  = @newline_data.dig(@active_line-1)
    return unless line # no line at index
    # current_offset = column_at(@x, (@active_line), true) # current line offset
    above_offset = column_at(@x, (@active_line-1), true) # line up offset

    # right_offset = current_offset
    # right_offset = above_offset if current_offset >= above_offset
    right_offset = above_offset

    pos = (line[:position_end_of_line] - line[:text_length]) + right_offset

  elsif direction == :down
    return if @text_input.caret_pos == @text_input.text.size
    return unless @newline_data[@active_line+1]
    line  = @newline_data.dig(@active_line+1)
    return unless line # no line at index
    # current_offset = column_at(@x, (@active_line), true) # current line offset
    below_offset = column_at(@x, (@active_line+1), true) # line down offset

    # right_offset = current_offset
    # right_offset = below_offset if current_offset >= below_offset
    right_offset = below_offset

    pos = (line[:position_end_of_line] - line[:text_length]) + right_offset

  else
    raise ":up or :down please."
  end

  set_position(pos)
end