shared.vim 1.0.0

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.
data/test/selftest.rb ADDED
@@ -0,0 +1,190 @@
1
+ #!/usr/bin/env ruby
2
+ # Random self-test of the core. Run: ruby test/selftest.rb
3
+ require_relative '../lib/shared_vim_core'
4
+ include SharedVimCore
5
+
6
+ ALPHA = %w[a b ä € x] + ["\n"]
7
+ def rnd_str(n) = Array.new(n) { ALPHA.sample }.join
8
+ def rnd_lines(n) = Array.new(n) { rnd_str(rand(0..4)).delete("\n") }
9
+ def doc(t) = t.lines.join("\n")
10
+
11
+ # A random edit of a client, the way Vim's listener reports it.
12
+ def rnd_edit(t, insert_only: false)
13
+ n = t.lines.size - 1 # buffer lines (model has a final "")
14
+ a = rand(0..n)
15
+ e = insert_only ? a : rand(a..[a + 2, n].min)
16
+ old = t.lines[a...e]
17
+ nl = rnd_lines(rand(0..2))
18
+ if insert_only && a < n # insert some chars into one line
19
+ l = t.lines[a].dup
20
+ l.insert(rand(0..l.length), rnd_str(rand(1..3)).delete("\n"))
21
+ return t.replace_op(a, a + 1, [l])
22
+ end
23
+ t.replace_op(a, e, insert_only ? nl.reject(&:empty?) : nl) || (old.empty? ? nil : nil)
24
+ end
25
+
26
+ # 1. line model
27
+ 3000.times do
28
+ t = Text.new(rnd_lines(rand(0..5)) + [''])
29
+ before = doc(t)
30
+ authors = t.authors.map(&:dup)
31
+ a = rand(0..t.lines.size - 1)
32
+ e = rand(a..t.lines.size - 1)
33
+ nl = rnd_lines(rand(0..3))
34
+ expect = (t.lines[0...a] + nl + t.lines[e..]).join("\n")
35
+ op = t.replace_op(a, e, nl) or next
36
+ inv, = t.apply(op, 7)
37
+ raise "replace #{before.inspect} #{a}...#{e} #{nl.inspect}: #{doc(t).inspect}" unless doc(t) == expect
38
+ raise 'length' unless t.length == expect.length
39
+ raise 'authors shape' unless t.authors.zip(t.lines).all? { |a, l| a.sum { |_, n| n } <= l.length && Text.compact(a) == a }
40
+ t.apply(inv, nil)
41
+ raise "inverse: #{doc(t).inspect} != #{before.inspect}" unless doc(t) == before && t.authors == authors
42
+ end
43
+
44
+ # 1b. author runs match a per-char reference ("\n" has no author)
45
+ def flat(t)
46
+ t.lines.each_with_index.flat_map do |l, i|
47
+ a = t.authors[i].flat_map { |s, n| [s] * n }
48
+ a += [nil] * (l.length - a.size)
49
+ i == 0 ? a : [nil] + a
50
+ end
51
+ end
52
+ 1000.times do
53
+ t = Text.new(rnd_lines(rand(0..5)) + [''])
54
+ ref = flat(t)
55
+ 20.times do
56
+ op = rnd_edit(t) or next
57
+ slot = [nil, 0, 1, 2].sample
58
+ pos = 0
59
+ op.each do |c|
60
+ if c.is_a?(String) then ref[pos, 0] = [slot] * c.length; pos += c.length
61
+ elsif c > 0 then pos += c
62
+ else ref[pos, -c] = []
63
+ end
64
+ end
65
+ t.apply(op, slot)
66
+ doc(t).each_char.with_index { |ch, i| ref[i] = nil if ch == "\n" }
67
+ raise "runs: #{t.authors.inspect} vs #{ref.inspect}" unless flat(t) == ref && t.authors.all? { |a| Text.compact(a) == a }
68
+ end
69
+ end
70
+
71
+ # Simulation: server rejects ops based on an old revision.
72
+ def simulate(rounds, start, undo_by: nil)
73
+ server = Text.new(start.dup)
74
+ srev = 0
75
+ clients = Array.new(3) { |i| Doc.new(Text.new(start.dup), 0, i) }
76
+ up = Array.new(3) { [] }
77
+ down = Array.new(3) { [] }
78
+ send = ->(i) { up[i] << [clients[i].rev, clients[i].pending.first] }
79
+ step = lambda do |allow_edits, &edit|
80
+ i = rand(3)
81
+ case rand(3)
82
+ when 0
83
+ return unless allow_edits
84
+ d = clients[i]
85
+ idle = d.pending.empty?
86
+ edit.(i, d)
87
+ send.(i) if idle && d.pending.any?
88
+ when 1
89
+ return if up[i].empty?
90
+ rev, op = up[i].shift
91
+ if rev == srev
92
+ server.apply(op, i)
93
+ srev += 1
94
+ 3.times { |j| down[j] << (j == i ? [:ack] : [:op, op, i]) }
95
+ else
96
+ down[i] << [:nack]
97
+ end
98
+ when 2
99
+ return if down[i].empty?
100
+ m = down[i].shift
101
+ d = clients[i]
102
+ case m[0]
103
+ when :ack then send.(i) if d.ack
104
+ when :nack then send.(i) if d.nack
105
+ else d.remote(m[1], m[2])
106
+ end
107
+ end
108
+ end
109
+ drain = -> { step.(false) {} until up.all?(&:empty?) && down.all?(&:empty?) }
110
+ rounds.times { step.(true) { |i, d| yield(i, d) } }
111
+ drain.()
112
+ if undo_by
113
+ d = clients[undo_by]
114
+ idle = d.pending.empty?
115
+ true while d.undo![0]
116
+ send.(undo_by) if idle && d.pending.any?
117
+ drain.()
118
+ end
119
+ [server, clients]
120
+ end
121
+
122
+ # 2. convergence of 3 clients, random message delays
123
+ 300.times do |round|
124
+ server, clients = simulate(60, rnd_lines(rand(0..4)) + ['']) do |_, d|
125
+ d.own(rnd_edit(d.text), rand(1000))
126
+ end
127
+ clients.each_with_index do |c, i|
128
+ raise "round #{round}: client #{i} #{doc(c.text).inspect} != #{doc(server).inspect}" unless doc(c.text) == doc(server)
129
+ raise "round #{round}: client #{i} authors differ" unless c.text.authors == server.authors
130
+ end
131
+ end
132
+
133
+ # 3. undoing all of client 0's inserts removes exactly its text
134
+ 300.times do |round|
135
+ start = rnd_lines(rand(1..4)) + ['']
136
+ server, clients = simulate(60, start, undo_by: 0) do |i, d|
137
+ d.own(rnd_edit(d.text, insert_only: i == 0), "k#{rand(1000)}")
138
+ end
139
+ clients.each { |c| raise "undo round #{round}: diverged" unless doc(c.text) == doc(server) }
140
+ raise "undo round #{round}: client 0 text left" if server.authors.any? { |a| a.any? { |s, _| s == 0 } }
141
+ end
142
+
143
+ # 3b. same with mixed edits: still converges
144
+ 300.times do |round|
145
+ server, clients = simulate(60, rnd_lines(rand(1..4)) + [''], undo_by: 0) do |_, d|
146
+ d.own(rnd_edit(d.text), "k#{rand(1000)}")
147
+ end
148
+ clients.each { |c| raise "mixed undo round #{round}: diverged" unless doc(c.text) == doc(server) && c.text.authors == server.authors }
149
+ end
150
+
151
+ # 4. alone, undo restores the start exactly (nothing dropped)
152
+ 300.times do |round|
153
+ start = rnd_lines(rand(1..4)) + ['']
154
+ d = Doc.new(Text.new(start.dup), 0, 0)
155
+ 20.times { |k| d.own(rnd_edit(d.text), k) }
156
+ true while d.undo![0]
157
+ raise "alone round #{round}: #{doc(d.text).inspect} != #{start.join("\n").inspect}" unless doc(d.text) == start.join("\n")
158
+ true while d.undo!(true)[0]
159
+ true while d.undo![0]
160
+ raise "alone redo round #{round}" unless doc(d.text) == start.join("\n")
161
+ end
162
+
163
+ # 5. A edits, B deletes around it, A undoes
164
+ def pair(start)
165
+ a = Doc.new(Text.new(start.dup), 0, 0)
166
+ b = Doc.new(Text.new(start.dup), 0, 1)
167
+ sync = ->(from, to) { to.remote(from.pending.first, from.slot); from.ack }
168
+ [a, b, sync]
169
+ end
170
+ a, b, sync = pair(['x y', '']) # A types foo, B deletes it
171
+ a.own(a.text.replace_op(0, 1, ['x foo y']), 1); sync.(a, b)
172
+ b.own(b.text.replace_op(0, 1, ['x y']), 2); sync.(b, a)
173
+ done, skipped = a.undo!
174
+ raise 'case 1: expected a skipped group' unless done.nil? && skipped == 1
175
+ a, b, sync = pair(['foo bar baz', 'next', '']) # A deletes bar, B deletes the line
176
+ a.own(a.text.replace_op(0, 1, ['foo baz']), 1); sync.(a, b)
177
+ b.own(b.text.replace_op(0, 1, []), 2); sync.(b, a)
178
+ done, skipped = a.undo!
179
+ raise "case 3: #{doc(a.text).inspect}" unless done.nil? && skipped == 1 && doc(a.text) == "next\n"
180
+ a, b, sync = pair(['foo bar baz', '']) # B deletes only " baz": keep the undo
181
+ a.own(a.text.replace_op(0, 1, ['foo baz']), 1); sync.(a, b)
182
+ b.own(b.text.replace_op(0, 1, ['foo ']), 2); sync.(b, a)
183
+ a.undo!
184
+ raise "neighbour kept: #{doc(a.text).inspect}" unless doc(a.text) == "foo bar\n"
185
+
186
+ # 6. cursor mapping
187
+ raise 'xform' unless OT.xform_index([2, 'XY', 3], 3) == 5
188
+ raise 'xform' unless OT.xform_index([1, -2, 2], 2) == 1
189
+
190
+ puts 'selftest ok'
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env ruby
2
+ # Installs a stub plugin into Vim's package directory. The stub finds the
3
+ # newest installed shared.vim gem through RubyGems at each Vim start, so gem
4
+ # updates need no reinstall.
5
+ # Usage: shared-vim-install [vim dir] (default ~/.vim)
6
+
7
+ require 'fileutils'
8
+
9
+ MARK = '" shared.vim stub, written by shared-vim-install'
10
+ STUB = <<~VIM
11
+ #{MARK}: loads the plugin from the
12
+ " newest installed shared.vim gem.
13
+ if has('ruby')
14
+ ruby VIM.command('source ' + File.join(Gem::Specification.find_by_name('shared.vim').gem_dir, 'plugin', 'shared.vim').gsub(' ', '\\ ')) rescue nil
15
+ endif
16
+ VIM
17
+
18
+ root = File.expand_path('..', __dir__)
19
+ unless Gem::Specification.any? { |s| s.name == 'shared.vim' && s.gem_dir == root }
20
+ puts 'Not an installed gem. To use this checkout, add to your vimrc:', '', " set rtp+=#{root}"
21
+ exit
22
+ end
23
+
24
+ file = File.join(File.expand_path(ARGV[0] || '~/.vim'), 'pack', 'shared', 'start', 'shared.vim', 'plugin', 'shared.vim')
25
+ if File.exist?(file) && !File.read(file).start_with?(MARK)
26
+ abort "#{file} exists and was not written by shared-vim-install, leaving it alone."
27
+ end
28
+ FileUtils.mkdir_p(File.dirname(file))
29
+ File.write(file, STUB)
30
+ puts "Installed #{file}", 'Nothing to add to your vimrc. To uninstall, delete that file.'
@@ -0,0 +1,137 @@
1
+ #!/usr/bin/env ruby
2
+ # shared.vim server: one process for all users, on a Unix domain socket.
3
+ # Usage: shared-vim-server [socket] (default /tmp/shared-vim/sock)
4
+ # Protocol: one JSON object per line, see README.md.
5
+ # Ops not based on the current revision are rejected (nack); the client
6
+ # transforms and resends them. Exits after IDLE seconds without connections.
7
+
8
+ require 'socket'
9
+ require 'json'
10
+ require_relative '../lib/shared_vim_core'
11
+
12
+ Text = SharedVimCore::Text
13
+ SOCK = ARGV[0] || '/tmp/shared-vim/sock'
14
+ DIR = File.dirname(SOCK)
15
+
16
+ begin
17
+ Dir.mkdir(DIR)
18
+ File.chmod(01777, DIR)
19
+ rescue SystemCallError
20
+ end
21
+ lock = File.open(File.join(DIR, 'lock'), File::RDWR | File::CREAT, 0666)
22
+ File.chmod(0666, lock.path) rescue nil
23
+ exit 0 unless lock.flock(File::LOCK_EX | File::LOCK_NB) # another server runs
24
+ begin
25
+ $stderr.reopen(File.join(DIR, "server-#{Process.uid}.log"), 'a')
26
+ $stderr.sync = true
27
+ rescue SystemCallError
28
+ end
29
+
30
+ File.unlink(SOCK) if File.socket?(SOCK)
31
+ SERVER = UNIXServer.new(SOCK)
32
+ File.chmod(0666, SOCK)
33
+
34
+ Member = Struct.new(:io, :user, :num, :slot) do
35
+ def name = "#{user}#{num}"
36
+ end
37
+ Doc = Struct.new(:text, :rev, :members)
38
+
39
+ IDLE = 60 # seconds without any connection before the server exits
40
+ DOCS = {} # path => Doc
41
+ INBUF = {} # io => String (binary)
42
+
43
+ def log(msg) = $stderr.puts("#{Time.now.strftime('%F %T')} #{msg}")
44
+
45
+ def out(io, h)
46
+ io.write(JSON.generate(h) + "\n")
47
+ rescue SystemCallError, IOError
48
+ end
49
+
50
+ def peers(path)
51
+ d = DOCS[path] or return
52
+ list = d.members.map { |m| [m.name, m.slot] }
53
+ d.members.each { |m| out(m.io, t: 'peers', path: path, peers: list) }
54
+ end
55
+
56
+ def leave(io, path)
57
+ d = DOCS[path] or return
58
+ d.members.reject! { |m| m.io == io }
59
+ if d.members.empty? then DOCS.delete(path)
60
+ else peers(path)
61
+ end
62
+ end
63
+
64
+ def handle(io, m)
65
+ path = m['path']
66
+ d = DOCS[path]
67
+ me = d&.members&.find { |x| x.io == io }
68
+ case m['t']
69
+ when 'join'
70
+ return if me
71
+ created = d.nil?
72
+ d = DOCS[path] ||= Doc.new(Text.new(m['lines']), 0, [])
73
+ slots = d.members.map(&:slot)
74
+ nums = d.members.select { |x| x.user == m['user'] }.map(&:num)
75
+ me = Member.new(io, m['user'], (1..).find { |n| !nums.include?(n) }, (0..).find { |s| !slots.include?(s) })
76
+ d.members << me
77
+ out(io, t: 'joined', path: path, name: me.name, slot: me.slot, rev: d.rev,
78
+ lines: d.text.lines, authors: d.text.authors, created: created)
79
+ peers(path)
80
+ when 'op'
81
+ return unless me
82
+ return out(io, t: 'nack', path: path) unless m['rev'] == d.rev
83
+ d.text.apply(m['op'], me.slot)
84
+ d.rev += 1
85
+ d.members.each do |x|
86
+ if x.io == io then out(io, t: 'ack', path: path)
87
+ else out(x.io, t: 'op', path: path, op: m['op'], slot: me.slot)
88
+ end
89
+ end
90
+ when 'saved'
91
+ return unless me
92
+ d.text.saved!
93
+ d.members.each { |x| out(x.io, t: 'saved', path: path) unless x.io == io }
94
+ when 'leave'
95
+ leave(io, path) if me
96
+ end
97
+ end
98
+
99
+ def drop(io)
100
+ DOCS.keys.each { |p| leave(io, p) }
101
+ INBUF.delete(io)
102
+ io.close rescue nil
103
+ end
104
+
105
+ log "listening on #{SOCK}"
106
+ loop do
107
+ ready, = IO.select([SERVER, *INBUF.keys], nil, nil, INBUF.empty? ? IDLE : nil)
108
+ unless ready
109
+ # a Vim connecting right now sees its channel close and starts a new server
110
+ File.unlink(SOCK) rescue nil
111
+ log 'idle, exiting'
112
+ exit
113
+ end
114
+ ready.each do |io|
115
+ if io == SERVER
116
+ c = SERVER.accept_nonblock(exception: false)
117
+ INBUF[c] = ''.b if c.is_a?(UNIXSocket)
118
+ next
119
+ end
120
+ begin
121
+ INBUF[io] << io.read_nonblock(1 << 16)
122
+ rescue IO::WaitReadable
123
+ next
124
+ rescue EOFError, IOError, SystemCallError
125
+ drop(io)
126
+ next
127
+ end
128
+ while (i = INBUF[io]&.index("\n"))
129
+ line = INBUF[io].slice!(0..i).force_encoding('UTF-8')
130
+ begin
131
+ handle(io, JSON.parse(line))
132
+ rescue StandardError => e
133
+ log "#{e.class}: #{e.message} on #{line[0, 200]}"
134
+ end
135
+ end
136
+ end
137
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shared.vim
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Juergen eTM Mangler
8
+ bindir: tools
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: see https://github.com/etm/shared.vim
13
+ email: juergen.mangler@gmail.com
14
+ executables:
15
+ - shared-vim-install
16
+ - shared-vim-server
17
+ extensions: []
18
+ extra_rdoc_files:
19
+ - README.md
20
+ files:
21
+ - AUTHORS
22
+ - LICENSE
23
+ - README.md
24
+ - Rakefile
25
+ - lib/shared_vim.rb
26
+ - lib/shared_vim_core.rb
27
+ - plugin/shared.vim
28
+ - shared.vim.gemspec
29
+ - test/selftest.rb
30
+ - tools/shared-vim-install
31
+ - tools/shared-vim-server
32
+ homepage: https://github.com/etm/shared.vim
33
+ licenses:
34
+ - GPL-3.0-or-later
35
+ metadata: {}
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 3.0.0
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubygems_version: 3.6.9
51
+ specification_version: 4
52
+ summary: shared.vim - Live co-editing in Vim, for everybody on the same computer.
53
+ test_files: []