Class: EditorCore::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/editor_core/core.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bufferObject

Returns the value of attribute buffer.



24
25
26
# File 'lib/editor_core/core.rb', line 24

def buffer
  @buffer
end

#cursorObject

Returns the value of attribute cursor.



24
25
26
# File 'lib/editor_core/core.rb', line 24

def cursor
  @cursor
end

#viewObject

Returns the value of attribute view.



24
25
26
# File 'lib/editor_core/core.rb', line 24

def view
  @view
end

Instance Method Details

#backspaceObject



181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/editor_core/core.rb', line 181

def backspace
  return if cursor.beginning_of_file?

  if cursor.col == 0
    cursor_left = buffer.lines(cursor.row).size + 1
    buffer.join_lines(cursor,-1)
    cursor_left.times { left }
  else
    buffer.delete(cursor, cursor.col - 1)
    left
  end
end

#beginning_of_file?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/editor_core/core.rb', line 122

def beginning_of_file?
  cursor&.beginning_of_file?
end

#buffer_endObject



54
55
56
57
# File 'lib/editor_core/core.rb', line 54

def buffer_end
  view_end
  @cursor = cursor.move(buffer, buffer.lines_count, cursor.col)
end

#buffer_homeObject

View-relative cursor movement ##



49
50
51
52
# File 'lib/editor_core/core.rb', line 49

def buffer_home
  view_home
  @cursor = cursor.move(buffer, 0, cursor.col)
end

#current_charObject



126
127
128
# File 'lib/editor_core/core.rb', line 126

def current_char
  current_line[cursor.col]
end

#current_lineObject

## Querying ##



39
40
41
# File 'lib/editor_core/core.rb', line 39

def current_line
  @buffer.lines(cursor.row)
end

#deleteObject



167
168
169
170
171
172
173
174
175
# File 'lib/editor_core/core.rb', line 167

def delete
  return if cursor.end_of_file?(buffer)

  if cursor.end_of_line?(buffer)
    buffer.join_lines(cursor)
  else
    buffer.delete(cursor, cursor.col)
  end
end

#delete_afterObject



163
164
165
# File 'lib/editor_core/core.rb', line 163

def delete_after
  buffer.delete(cursor, cursor.col,-1)
end

#delete_beforeObject

## Mutation ##



158
159
160
161
# File 'lib/editor_core/core.rb', line 158

def delete_before
  buffer.delete(cursor, 0, cursor.col)
  line_home
end

#down(off = 1) ⇒ Object



32
# File 'lib/editor_core/core.rb', line 32

def down(off=1);    @cursor = cursor.down(buffer,off.to_i) end

#get_afterObject



43
44
45
# File 'lib/editor_core/core.rb', line 43

def get_after
  @buffer.lines(cursor.row)[@cursor.col..-1]
end

#join_lineObject



177
178
179
# File 'lib/editor_core/core.rb', line 177

def join_line
  buffer.join_lines(cursor)
end

#left(off = 1) ⇒ Object



30
# File 'lib/editor_core/core.rb', line 30

def left(off=1);    @cursor = cursor.left(buffer,off.to_i) end

#left_until_match(r) ⇒ Object



130
131
132
133
134
135
136
137
138
# File 'lib/editor_core/core.rb', line 130

def left_until_match(r)
  off = -1
  loop do
    left
    off +=1
    break if beginning_of_file? || current_char&.match(r)
  end
  off
end

#left_while_match(r) ⇒ Object



140
141
142
143
144
145
146
147
148
149
# File 'lib/editor_core/core.rb', line 140

def left_while_match(r)
  off = 0
  while !beginning_of_file? && current_char&.match(r)
    left
    off +=1
  end
  # Because we'll advance once too far left
  right if !current_char&.match(r)
  off
end

#line_endObject



35
# File 'lib/editor_core/core.rb', line 35

def line_end;       @cursor = cursor.line_end(buffer) end

#line_homeObject



34
# File 'lib/editor_core/core.rb', line 34

def line_home;      @cursor = cursor.line_home end

#move(row, col) ⇒ Object

## Basic cursor movement ##



28
# File 'lib/editor_core/core.rb', line 28

def move(row, col); @cursor = cursor.move(buffer, row.to_i, col.to_i) end

#next_wordObject

FIXME: Rewrite like prev_word



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/editor_core/core.rb', line 98

def next_word
  line = current_line
  c = cursor.col
  m = line.length
  while c<m && line[c]&.match(/[^a-zA-Z]/)
    c += 1
  end
  if c >= m
    # FIXME: Pathological cases can cause stack issue here.
    @cursor = Cursor.new(cursor.row,m)
    right
    return next_word
  end

  if run = line[c..-1]&.match(/([a-zA-Z]+)/)
    c += run[0].length
    #pry([line,c,run])
  end
  off = c - cursor.col
  right(off)
  off
end

#page_downObject



59
60
61
62
# File 'lib/editor_core/core.rb', line 59

def page_down
  lines = view_down(view.height-1)
  #@cursor = cursor.down(buffer, lines)
end

#page_upObject



64
65
66
# File 'lib/editor_core/core.rb', line 64

def page_up
  @cursor = cursor.up(buffer, view_up(view.height-1))
end

#prev_wordObject



151
152
153
154
# File 'lib/editor_core/core.rb', line 151

def prev_word
  word = /[A-Za-z]/
  left_until_match(word) + left_while_match(word)
end

#right(off = 1) ⇒ Object



29
# File 'lib/editor_core/core.rb', line 29

def right(off=1);   @cursor = cursor.right(buffer,off.to_i) end

#rstrip_lineObject



195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/editor_core/core.rb', line 195

def rstrip_line
  line = current_line
  stripped = current_line.rstrip
  return if line.length == stripped.length
  col = cursor.col
  oldc = cursor
  move(cursor.row, stripped.length)
  delete_after
  if col < stripped.length
    @cursor = oldc
  end
end

#up(off = 1) ⇒ Object



31
# File 'lib/editor_core/core.rb', line 31

def up(off=1);      @cursor = cursor.up(buffer,off.to_i) end

#view_down(offset = 1) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/editor_core/core.rb', line 77

def view_down(offset = 1)
  oldtop = view.top
  view.top += offset
  if view.top > buffer.lines_count
    view.top = buffer.lines_count
  end
  down(offset)
  view.top - oldtop
end

#view_endObject



91
92
93
# File 'lib/editor_core/core.rb', line 91

def view_end
  view.top = buffer.lines_count - view.height
end

#view_homeObject



87
88
89
# File 'lib/editor_core/core.rb', line 87

def view_home
  view.top = 0
end

#view_up(offset = 1) ⇒ Object

View movement



69
70
71
72
73
74
75
# File 'lib/editor_core/core.rb', line 69

def view_up(offset = 1)
  oldtop = view.top
  view.top -= offset
  view.top = 0 if view.top < 0
  up(offset)
  oldtop - view.top
end