Class: Rvim::Buffer

Inherits:
Object
  • Object
show all
Defined in:
lib/rvim/buffer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, filepath = nil, encoding: Encoding::UTF_8) ⇒ Buffer

Returns a new instance of Buffer.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rvim/buffer.rb', line 13

def initialize(id, filepath = nil, encoding: Encoding::UTF_8)
  @id = id
  @filepath = filepath
  @fileformat = 'unix'
  @fileencoding = encoding.to_s.downcase
  @mtime = nil
  if filepath && File.exist?(filepath)
    raw = File.binread(filepath)
    @mtime = File.mtime(filepath)
    @fileformat = detect_fileformat(raw)
    decoded = decode_with_encoding(raw, encoding)
    @lines = split_with_format(decoded, @fileformat).map { |l| String.new(l, encoding: encoding) }
  else
    @lines = [String.new('', encoding: encoding)]
  end
  @lines = [String.new('', encoding: encoding)] if @lines.empty?
  @modified = false
  @marks = Rvim::Marks.new
  @line_index = 0
  @byte_pointer = 0
  # Seed the undo history with the file's actual loaded content so the
  # very first undo from a post-edit state restores the on-disk view,
  # not Reline's empty default.
  @undo_redo_history = [[@lines.map(&:dup), 0, 0]]
  @undo_redo_index = 0
  @last_visual = nil
  @local_settings = {}
  @folds = Rvim::Folds.new
  @diff_active = false
  @diff_status = nil
  @vars = {}
end

Instance Attribute Details

#byte_pointerObject

Returns the value of attribute byte_pointer.



6
7
8
# File 'lib/rvim/buffer.rb', line 6

def byte_pointer
  @byte_pointer
end

#diff_activeObject

Returns the value of attribute diff_active.



9
10
11
# File 'lib/rvim/buffer.rb', line 9

def diff_active
  @diff_active
end

#diff_statusObject

Returns the value of attribute diff_status.



9
10
11
# File 'lib/rvim/buffer.rb', line 9

def diff_status
  @diff_status
end

#fileencodingObject

Returns the value of attribute fileencoding.



10
11
12
# File 'lib/rvim/buffer.rb', line 10

def fileencoding
  @fileencoding
end

#fileformatObject

Returns the value of attribute fileformat.



10
11
12
# File 'lib/rvim/buffer.rb', line 10

def fileformat
  @fileformat
end

#filepathObject

Returns the value of attribute filepath.



5
6
7
# File 'lib/rvim/buffer.rb', line 5

def filepath
  @filepath
end

#foldsObject

Returns the value of attribute folds.



8
9
10
# File 'lib/rvim/buffer.rb', line 8

def folds
  @folds
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/rvim/buffer.rb', line 5

def id
  @id
end

#last_visualObject

Returns the value of attribute last_visual.



7
8
9
# File 'lib/rvim/buffer.rb', line 7

def last_visual
  @last_visual
end

#line_indexObject

Returns the value of attribute line_index.



6
7
8
# File 'lib/rvim/buffer.rb', line 6

def line_index
  @line_index
end

#linesObject

Returns the value of attribute lines.



5
6
7
# File 'lib/rvim/buffer.rb', line 5

def lines
  @lines
end

#local_settingsObject

Returns the value of attribute local_settings.



8
9
10
# File 'lib/rvim/buffer.rb', line 8

def local_settings
  @local_settings
end

#marksObject

Returns the value of attribute marks.



6
7
8
# File 'lib/rvim/buffer.rb', line 6

def marks
  @marks
end

#modifiedObject

Returns the value of attribute modified.



5
6
7
# File 'lib/rvim/buffer.rb', line 5

def modified
  @modified
end

#mtimeObject

Returns the value of attribute mtime.



10
11
12
# File 'lib/rvim/buffer.rb', line 10

def mtime
  @mtime
end

#undo_redo_historyObject

Returns the value of attribute undo_redo_history.



7
8
9
# File 'lib/rvim/buffer.rb', line 7

def undo_redo_history
  @undo_redo_history
end

#undo_redo_indexObject

Returns the value of attribute undo_redo_index.



7
8
9
# File 'lib/rvim/buffer.rb', line 7

def undo_redo_index
  @undo_redo_index
end

#varsObject

Returns the value of attribute vars.



11
12
13
# File 'lib/rvim/buffer.rb', line 11

def vars
  @vars
end

Instance Method Details

#display_nameObject



46
47
48
# File 'lib/rvim/buffer.rb', line 46

def display_name
  @filepath || '[No Name]'
end

#file_changed_externally?Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
# File 'lib/rvim/buffer.rb', line 50

def file_changed_externally?
  return false unless @filepath && File.exist?(@filepath)
  return false if @mtime.nil?

  File.mtime(@filepath) > @mtime
end

#reload(encoding: Encoding::UTF_8) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rvim/buffer.rb', line 57

def reload(encoding: Encoding::UTF_8)
  return unless @filepath && File.exist?(@filepath)

  raw = File.binread(@filepath)
  @mtime = File.mtime(@filepath)
  @fileformat = detect_fileformat(raw)
  decoded = decode_with_encoding(raw, encoding)
  @lines = split_with_format(decoded, @fileformat).map { |l| String.new(l, encoding: encoding) }
  @lines = [String.new('', encoding: encoding)] if @lines.empty?
  @line_index = @line_index.clamp(0, [@lines.size - 1, 0].max)
  target = @lines[@line_index] || ''
  @byte_pointer = @byte_pointer.clamp(0, [target.bytesize - 1, 0].max)
end