editor_core 0.2.0 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ec52d9db82e48638cbc82762edbfc019f825beda5eafa66475f18254c69e1f4e
4
- data.tar.gz: 354a5ae6fb35b9bb195df0ace5c579a1643b61dbd0e8449ce7428c62b9fefd98
3
+ metadata.gz: 29e9723ed92a39ab0c10d9b0e6c849fdf05c041886ad185e7c66c642fdec7e2f
4
+ data.tar.gz: 62c86e0365372fbda74a9d7b303cdb0d41ba0993890fcc213a569ae84692231c
5
5
  SHA512:
6
- metadata.gz: 1d65ef8b4ca8ca3138a67e0684bd3f315569423d715fc6fff022edb2d1c3b717db1efedfb4892711992107056c9afe29e30114beef2fb7c4d3c5350ea3ea8ba1
7
- data.tar.gz: 483e3d2feb7dda1c78b5634612b92eb3f946c86921ae7898a81a3c73b665fd2d26c1edac0a724faed232b4ee190fcc1c3a1f37f72e979862132c95c400262775
6
+ metadata.gz: 97bf696d944a8c50fc5c961e925e6604ffafb806b3e430a4d6755921d3090ef4fa2b6fdf73b520a8896fe8faddb3f2d07f1e625df6aeabe68e48fa27221e306b
7
+ data.tar.gz: 14c4caf205a01dcb4328840df72d1037db7ec780d2cb2dace5d23bfa9831310b7cdb981cf6dd223e6d47f2951d5e171f0be6f5acd97ce3f02fcfe9ce3a11f8e6
data/README.md CHANGED
@@ -28,7 +28,11 @@ TODO: Write usage instructions here
28
28
 
29
29
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
30
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
31
+ To install this gem onto your local machine, run `bundle exec rake
32
+ install`. To release a new version, update the version number in
33
+ `version.rb`, and then run `bundle exec rake release`, which will create
34
+ a git tag for the version, push git commits and tags, and push the `.gem`
35
+ file to [rubygems.org](https://rubygems.org).
32
36
 
33
37
  ## Contributing
34
38
 
@@ -10,9 +10,11 @@ module EditorCore
10
10
 
11
11
  attr_reader :name, :created_at, :history
12
12
  attr_accessor :buffer_id
13
- attr_writer :created_at
14
13
 
15
- def initialize(id,name, lines, created_at = 0)
14
+ def created_at= t
15
+ @created_at = t
16
+ end
17
+ def initialize(id,name, lines, created_at = Time.at(0))
16
18
  @buffer_id = id # An id for this buffer unique for this session
17
19
  @name = name
18
20
  @history = History.new
@@ -20,12 +22,12 @@ module EditorCore
20
22
  @created_at = case created_at
21
23
  when Numeric
22
24
  Time.at(created_at)
23
- when Time
25
+ when Time, DateTime
24
26
  created_at
25
27
  when String
26
28
  DateTime.parse(created_at)
27
29
  else
28
- raise "Unknown time format: #{created_at}"
30
+ raise "Unknown time format: #{created_at.inspect} / #{created_at.class}"
29
31
  end
30
32
  end
31
33
 
@@ -79,7 +81,7 @@ module EditorCore
79
81
 
80
82
  def join_lines(cursor,offset=0)
81
83
  row=cursor.row+offset
82
- modify(cursor, row..row+1) {|l| l.join }
84
+ modify(cursor, row..row+1, row..row) {|l| l.join }
83
85
  end
84
86
 
85
87
  def indent(cursor, row, pos)
@@ -101,31 +103,31 @@ module EditorCore
101
103
  end
102
104
 
103
105
  def undo(old_cursor)
104
- cursor, rowrange, rows = @history.undo_snapshot
105
- store_snapshot(old_cursor,rowrange, false)
106
- cursor, rowrange, rows = @history.undo
107
- @lines[rowrange] = rows
106
+ cursor, oldrowrange, newrowrange, rows = @history.undo_snapshot
107
+ store_snapshot(old_cursor,oldrowrange, newrowrange, false)
108
+ cursor, oldrowrange, newrowrange, rows = @history.undo
109
+ @lines[newrowrange] = rows
108
110
  cursor
109
111
  end
110
112
 
111
113
  def redo
112
- cursor, rowrange, rows = @history.redo
113
- @lines[rowrange] = rows
114
+ cursor, oldrowrange, newrowrange, rows = @history.redo
115
+ @lines[oldrowrange] = rows
114
116
  cursor
115
117
  end
116
118
 
117
119
  private
118
120
 
119
- def store_snapshot(cursor, rowrange, advance = true)
120
- @history.save([cursor, rowrange, @lines[rowrange]], advance)
121
+ def store_snapshot(cursor, oldrowrange, newrowrange, advance = true)
122
+ @history.save([cursor, oldrowrange, newrowrange, @lines[oldrowrange]], advance)
121
123
  end
122
124
 
123
- def modify(cursor, rowrange)
124
- lines = @lines[rowrange].dup
125
+ def modify(cursor, oldrowrange, newrowrange = oldrowrange)
126
+ lines = @lines[oldrowrange].dup
125
127
  lines ||= ""
126
128
  new_lines = yield(lines)
127
- store_snapshot(cursor, rowrange, true)
128
- @lines[rowrange] = new_lines
129
+ store_snapshot(cursor, oldrowrange, newrowrange, true)
130
+ @lines[oldrowrange] = new_lines
129
131
  changed
130
132
  notify_observers(self)
131
133
  end
@@ -58,7 +58,7 @@ module EditorCore
58
58
 
59
59
  def page_down
60
60
  lines = view_down(view.height-1)
61
- @cursor = cursor.down(buffer, lines)
61
+ #@cursor = cursor.down(buffer, lines)
62
62
  end
63
63
 
64
64
  def page_up
@@ -70,6 +70,7 @@ module EditorCore
70
70
  oldtop = view.top
71
71
  view.top -= offset
72
72
  view.top = 0 if view.top < 0
73
+ up(offset)
73
74
  oldtop - view.top
74
75
  end
75
76
 
@@ -79,6 +80,7 @@ module EditorCore
79
80
  if view.top > buffer.lines_count
80
81
  view.top = buffer.lines_count
81
82
  end
83
+ down(offset)
82
84
  view.top - oldtop
83
85
  end
84
86
 
@@ -91,6 +93,8 @@ module EditorCore
91
93
  end
92
94
 
93
95
  # ## Complex navigation ##
96
+
97
+ # FIXME: Rewrite like prev_word
94
98
  def next_word
95
99
  line = current_line
96
100
  c = cursor.col
@@ -114,25 +118,55 @@ module EditorCore
114
118
  off
115
119
  end
116
120
 
121
+ def delete_next_word
122
+ start = cursor
123
+ next_word
124
+ finish = cursor
125
+ startcol = start.col
126
+ finishcol = finish.row > start.row ? -1 : finish.col-1
127
+ @cursor = start
128
+ buffer.delete(cursor, startcol, finishcol)
129
+ end
117
130
 
118
- def prev_word
119
- return if cursor.col == 0
120
- line = current_line
121
- c = cursor.col
122
- if c > 0
123
- c -= 1
124
- end
125
- while c > 0 && line[c] && line[c].match(/[ \t]/)
126
- c -= 1
131
+ def delete_prev_word
132
+ prev_word
133
+ delete_next_word
134
+ end
135
+
136
+ def beginning_of_file?
137
+ cursor&.beginning_of_file?
138
+ end
139
+
140
+ def current_char
141
+ current_line[cursor.col]
142
+ end
143
+
144
+ def left_until_match(r)
145
+ off = -1
146
+ loop do
147
+ left
148
+ off +=1
149
+ break if beginning_of_file? || current_char&.match(r)
127
150
  end
128
- while c > 0 && !(line[c-1].match(/[ \t\-]/))
129
- c -= 1
151
+ off
152
+ end
153
+
154
+ def left_while_match(r)
155
+ off = 0
156
+ while !beginning_of_file? && current_char&.match(r)
157
+ left
158
+ off +=1
130
159
  end
131
- off = cursor.col - c
132
- @cursor = Cursor.new(cursor.row, c)
160
+ # Because we'll advance once too far left
161
+ right if !current_char&.match(r)
133
162
  off
134
163
  end
135
164
 
165
+ def prev_word
166
+ word = /[A-Za-z]/
167
+ left_until_match(word) + left_while_match(word)
168
+ end
169
+
136
170
  # ## Mutation ##
137
171
 
138
172
  def delete_before
@@ -1,3 +1,3 @@
1
1
  module EditorCore
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: editor_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vidar Hokstad
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-11-26 00:00:00.000000000 Z
11
+ date: 2025-06-07 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -53,7 +53,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  requirements: []
56
- rubygems_version: 3.1.4
56
+ rubygems_version: 3.4.10
57
57
  signing_key:
58
58
  specification_version: 4
59
59
  summary: Core model functionality for a simple editor