Class: RuVim::Window

Inherits:
Object
  • Object
show all
Defined in:
lib/ruvim/window.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, buffer_id:) ⇒ Window

Returns a new instance of Window.



10
11
12
13
14
15
16
17
18
19
# File 'lib/ruvim/window.rb', line 10

def initialize(id:, buffer_id:)
  @id = id
  @buffer_id = buffer_id
  @cursor_x = 0
  @cursor_y = 0
  @row_offset = 0
  @col_offset = 0
  @preferred_x = nil
  @options = {}
end

Instance Attribute Details

#buffer_idObject

Returns the value of attribute buffer_id.



6
7
8
# File 'lib/ruvim/window.rb', line 6

def buffer_id
  @buffer_id
end

#col_offsetObject

Returns the value of attribute col_offset.



6
7
8
# File 'lib/ruvim/window.rb', line 6

def col_offset
  @col_offset
end

#cursor_xObject

Returns the value of attribute cursor_x.



7
8
9
# File 'lib/ruvim/window.rb', line 7

def cursor_x
  @cursor_x
end

#cursor_yObject

Returns the value of attribute cursor_y.



7
8
9
# File 'lib/ruvim/window.rb', line 7

def cursor_y
  @cursor_y
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/ruvim/window.rb', line 5

def id
  @id
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/ruvim/window.rb', line 8

def options
  @options
end

#row_offsetObject

Returns the value of attribute row_offset.



6
7
8
# File 'lib/ruvim/window.rb', line 6

def row_offset
  @row_offset
end

Instance Method Details

#clamp_to_buffer(buffer, max_extra_col: 0) ⇒ Object



30
31
32
33
34
35
# File 'lib/ruvim/window.rb', line 30

def clamp_to_buffer(buffer, max_extra_col: 0)
  @cursor_y = [[@cursor_y, 0].max, buffer.line_count - 1].min
  max_col = buffer.line_length(@cursor_y) + [max_extra_col, 0].max
  @cursor_x = [[@cursor_x, 0].max, max_col].min
  self
end

#ensure_visible(buffer, height:, width:, tabstop: 2, scrolloff: 0, sidescrolloff: 0) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ruvim/window.rb', line 72

def ensure_visible(buffer, height:, width:, tabstop: 2, scrolloff: 0, sidescrolloff: 0)
  clamp_to_buffer(buffer)
  so = [[scrolloff, 0].max, [height - 1, 0].max].min

  top_target = @cursor_y - so
  bottom_target = @cursor_y + so
  @row_offset = top_target if top_target < @row_offset
  @row_offset = bottom_target - height + 1 if bottom_target >= @row_offset + height
  @row_offset = 0 if @row_offset.negative?

  line = buffer.line_at(@cursor_y)
  cursor_screen_col = RuVim::TextMetrics.screen_col_for_char_index(line, @cursor_x, tabstop:)
  offset_screen_col = RuVim::TextMetrics.screen_col_for_char_index(line, @col_offset, tabstop:)
  sso = [[sidescrolloff, 0].max, [width - 1, 0].max].min

  if cursor_screen_col < offset_screen_col + sso
    target_left = [cursor_screen_col - sso, 0].max
    @col_offset = RuVim::TextMetrics.char_index_for_screen_col(line, target_left, tabstop:)
  elsif cursor_screen_col >= offset_screen_col + width - sso
    target_left = cursor_screen_col - width + sso + 1
    @col_offset = RuVim::TextMetrics.char_index_for_screen_col(line, target_left, tabstop:, align: :ceil)
  end
  @col_offset = 0 if @col_offset.negative?
end

#move_down(buffer, count = 1) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/ruvim/window.rb', line 64

def move_down(buffer, count = 1)
  desired_x = @preferred_x || @cursor_x
  @cursor_y += count
  clamp_to_buffer(buffer)
  @cursor_x = [desired_x, buffer.line_length(@cursor_y)].min
  @preferred_x = desired_x
end

#move_left(buffer, count = 1) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/ruvim/window.rb', line 37

def move_left(buffer, count = 1)
  @preferred_x = nil
  count.times do
    break if @cursor_x <= 0
    @cursor_x = RuVim::TextMetrics.previous_grapheme_char_index(buffer.line_at(@cursor_y), @cursor_x)
  end
  clamp_to_buffer(buffer)
end

#move_right(buffer, count = 1) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/ruvim/window.rb', line 46

def move_right(buffer, count = 1)
  @preferred_x = nil
  count.times do
    line = buffer.line_at(@cursor_y)
    break if @cursor_x >= line.length
    @cursor_x = RuVim::TextMetrics.next_grapheme_char_index(line, @cursor_x)
  end
  clamp_to_buffer(buffer)
end

#move_up(buffer, count = 1) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/ruvim/window.rb', line 56

def move_up(buffer, count = 1)
  desired_x = @preferred_x || @cursor_x
  @cursor_y -= count
  clamp_to_buffer(buffer)
  @cursor_x = [desired_x, buffer.line_length(@cursor_y)].min
  @preferred_x = desired_x
end