textbringer-auto-revert 0.3.4 → 0.5.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +83 -0
- data/CLAUDE.md +34 -0
- data/README.md +16 -4
- data/lib/textbringer/auto-revert/version.rb +1 -1
- data/lib/textbringer/auto-revert.rb +83 -5
- data/test/test_helper.rb +74 -2
- data/test/textbringer_auto_revert_test.rb +199 -0
- metadata +7 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c6145f87af690763652f23b9222bd0113b179ecd533185ee7c87ed1ffdff4b14
|
|
4
|
+
data.tar.gz: 54e721a81c540d04dfb6b83e0ef33328d312c6dcbe86f7188710aed79247ac0d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 79e2e3472e941b48d8c8856b45399e42fdecdd20dc6615a1f2b8ad6ea8e59230aa99c613eb029b916395559ccee2d210beac253f12cb0fb3e7cf91826af1458e
|
|
7
|
+
data.tar.gz: 0afc4d24d5953bbb18b3395dfb2bf9566f574a5a50005ef4e460e24aaafcaefcf9813c33841d6664dd83e95db9b3c82cfc679e90bba1adfed3135d2ebdf3e522
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [0.5.0] - 2026-11-21
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- Timer-based idle revert: a background thread scans all file-backed buffers
|
|
13
|
+
every `CONFIG[:auto_revert_interval]` seconds (default 5), so external
|
|
14
|
+
changes are picked up without any command execution (#6)
|
|
15
|
+
- `CONFIG[:auto_revert_interval]` configuration key (#6)
|
|
16
|
+
|
|
17
|
+
### Fixed
|
|
18
|
+
|
|
19
|
+
- A file deleted or renamed on disk silently killed global auto-revert mode
|
|
20
|
+
for the whole editor session; it now warns once per buffer and resumes when
|
|
21
|
+
the file comes back (#4)
|
|
22
|
+
- Cursor position (point) is preserved across auto-revert instead of jumping
|
|
23
|
+
to the beginning of the buffer (#5)
|
|
24
|
+
- `M-x global_auto_revert_mode` raised `NoMethodError`; the plugin no longer
|
|
25
|
+
overrides the toggle command that textbringer auto-defines (#9)
|
|
26
|
+
|
|
27
|
+
### Changed
|
|
28
|
+
|
|
29
|
+
- Minimum supported textbringer version is now 15 (the release that
|
|
30
|
+
introduced `GlobalMinorMode`, which this plugin has always required) (#9)
|
|
31
|
+
|
|
32
|
+
## [0.3.4] - 2026-02-06
|
|
33
|
+
|
|
34
|
+
### Changed
|
|
35
|
+
|
|
36
|
+
- Refined auto-revert verbosity: warn once (not repeatedly) when a buffer has
|
|
37
|
+
unsaved changes while the file changed on disk
|
|
38
|
+
|
|
39
|
+
## [0.3.3] - 2026-01-30
|
|
40
|
+
|
|
41
|
+
### Fixed
|
|
42
|
+
|
|
43
|
+
- Support auto-revert for read-only buffers
|
|
44
|
+
|
|
45
|
+
## [0.3.2] - 2026-01-30
|
|
46
|
+
|
|
47
|
+
### Added
|
|
48
|
+
|
|
49
|
+
- lefthook pre-commit test hook (development only)
|
|
50
|
+
|
|
51
|
+
## [0.3.1] - 2026-01-30
|
|
52
|
+
|
|
53
|
+
### Added
|
|
54
|
+
|
|
55
|
+
- `global_auto_revert_mode` command definition
|
|
56
|
+
|
|
57
|
+
## [0.3.0] - 2026-01-30
|
|
58
|
+
|
|
59
|
+
### Changed
|
|
60
|
+
|
|
61
|
+
- Auto-revert mode is enabled by default on plugin load
|
|
62
|
+
|
|
63
|
+
## [0.2.0] - 2026-01-30
|
|
64
|
+
|
|
65
|
+
### Fixed
|
|
66
|
+
|
|
67
|
+
- Use `GlobalMinorMode` for proper hook management
|
|
68
|
+
|
|
69
|
+
## [0.1.0] - 2026-01-30
|
|
70
|
+
|
|
71
|
+
### Added
|
|
72
|
+
|
|
73
|
+
- Initial implementation: revert unmodified buffers after command execution
|
|
74
|
+
when the visited file changed on disk
|
|
75
|
+
|
|
76
|
+
[0.5.0]: https://github.com/yancya/textbringer-auto-revert/compare/v0.3.4...v0.5.0
|
|
77
|
+
[0.3.4]: https://github.com/yancya/textbringer-auto-revert/compare/v0.3.3...v0.3.4
|
|
78
|
+
[0.3.3]: https://github.com/yancya/textbringer-auto-revert/compare/v0.3.2...v0.3.3
|
|
79
|
+
[0.3.2]: https://github.com/yancya/textbringer-auto-revert/compare/v0.3.1...v0.3.2
|
|
80
|
+
[0.3.1]: https://github.com/yancya/textbringer-auto-revert/compare/v0.3.0...v0.3.1
|
|
81
|
+
[0.3.0]: https://github.com/yancya/textbringer-auto-revert/compare/v0.2.0...v0.3.0
|
|
82
|
+
[0.2.0]: https://github.com/yancya/textbringer-auto-revert/compare/v0.1.0...v0.2.0
|
|
83
|
+
[0.1.0]: https://github.com/yancya/textbringer-auto-revert/releases/tag/v0.1.0
|
data/CLAUDE.md
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# textbringer-auto-revert 開発メモ
|
|
2
|
+
|
|
3
|
+
## テスト実行
|
|
4
|
+
|
|
5
|
+
```
|
|
6
|
+
bundle exec rake test
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
テストは `test/test_helper.rb` の mock Textbringer で完結する(本物の textbringer / curses 不要)。mock は本物の挙動を再現している前提なので、textbringer 本体の API を触る変更をするときは、インストール済み gem のソース(`bundle show textbringer`)で実際の挙動を確認してから mock に反映すること。
|
|
10
|
+
|
|
11
|
+
## textbringer 本体の罠(一次ソースで確認済み・2026-07 時点 v26)
|
|
12
|
+
|
|
13
|
+
- `post_command_hook` は `run_hooks(:post_command_hook, remove_on_error: true)` で走る。**フック内で例外が漏れると、そのフックはエディタ全体から静かに除去される**。このプラグインのフック/スキャンが全例外を rescue しているのはこのため。外すな
|
|
14
|
+
- `GlobalMinorMode.inherited`(textbringer >= 15)がクラス名から toggle コマンド(`global_auto_revert_mode`)を**自動定義する**。プラグイン側で同名の `define_command` を書くと正しい自動定義を上書きして壊す(#9 で一度やらかした)
|
|
15
|
+
- `Buffer#file_modified?` は `File.mtime` 直呼びで、ファイルがディスクに無いと `Errno::ENOENT` を投げる
|
|
16
|
+
- `Buffer#revert` は `clear` + `set_contents` なので point が 0 に飛ぶ。point 復元は `goto_char`(範囲外で RangeError、マルチバイト文字の途中で ArgumentError)で行う
|
|
17
|
+
- idle 処理のイディオム: `background { }`(素の Thread)で sleep だけして、バッファ操作は `foreground { }`(= `next_tick`、self-pipe でアイドル中のイベントループを起こす)でメインループに寄せる。ロック不要
|
|
18
|
+
|
|
19
|
+
## 実機スモークテスト(tmux)
|
|
20
|
+
|
|
21
|
+
TUI の動作確認はヘッドレスでできる:
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
tmux new-session -d -s tb-smoke "bundle exec txtb /path/to/file"
|
|
25
|
+
tmux send-keys -t tb-smoke Escape x # M-x
|
|
26
|
+
tmux capture-pane -t tb-smoke -p
|
|
27
|
+
tmux kill-session -t tb-smoke
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
idle revert の検証は「send-keys せずにファイルを外部から書き換えて interval+α 待って capture」でできる。
|
|
31
|
+
|
|
32
|
+
## リリース
|
|
33
|
+
|
|
34
|
+
`v*` タグを push すると `.github/workflows/release.yml`(RubyGems Trusted Publishing)が走る。手順: version.rb bump → CHANGELOG の Unreleased を版に付け替え → commit → タグ push。リリースはユーザーの明示 GO が必要。
|
data/README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# Textbringer Auto Revert
|
|
2
2
|
|
|
3
|
+
[](https://github.com/yancya/textbringer-auto-revert/actions/workflows/ci.yml)
|
|
4
|
+
|
|
3
5
|
A Textbringer plugin that automatically reverts buffers when files are modified externally.
|
|
4
6
|
|
|
5
7
|
## Installation
|
|
@@ -20,7 +22,9 @@ bundle add textbringer-auto-revert
|
|
|
20
22
|
|
|
21
23
|
**Auto-revert mode is enabled by default** when the plugin is loaded.
|
|
22
24
|
|
|
23
|
-
When a file is modified externally and the buffer has no unsaved changes, the buffer
|
|
25
|
+
When a file is modified externally and the buffer has no unsaved changes, the buffer automatically reverts to the file's contents. This happens both after any command execution and periodically while the editor sits idle (every `:auto_revert_interval` seconds), so changes are picked up without touching the keyboard. The cursor position is preserved across reverts.
|
|
26
|
+
|
|
27
|
+
If the buffer has unsaved changes, it is never reverted; instead a warning (`Buffer has unsaved changes; file changed on disk`) is shown once per change. If the visited file disappears from disk (deleted or renamed), auto-revert warns once (`Auto-revert failed: ...`) and resumes automatically when the file comes back.
|
|
24
28
|
|
|
25
29
|
### Commands
|
|
26
30
|
|
|
@@ -41,9 +45,17 @@ Textbringer::GlobalAutoRevertMode.enabled = false
|
|
|
41
45
|
|
|
42
46
|
### Configuration
|
|
43
47
|
|
|
44
|
-
| Key | Default | Description |
|
|
45
|
-
|
|
46
|
-
| `:auto_revert_verbose` | `true` | Show
|
|
48
|
+
| Key | Type | Default | Description |
|
|
49
|
+
|-----|------|---------|-------------|
|
|
50
|
+
| `:auto_revert_verbose` | Boolean | `true` | Show messages when a buffer is reverted, has unsaved changes while the file changed on disk, or its file went missing |
|
|
51
|
+
| `:auto_revert_interval` | Numeric (seconds) | `5` | How often the idle timer scans all file-backed buffers for external changes |
|
|
52
|
+
|
|
53
|
+
Set them in your `~/.textbringer.rb`:
|
|
54
|
+
|
|
55
|
+
```ruby
|
|
56
|
+
Textbringer::CONFIG[:auto_revert_verbose] = false
|
|
57
|
+
Textbringer::CONFIG[:auto_revert_interval] = 2
|
|
58
|
+
```
|
|
47
59
|
|
|
48
60
|
## Development
|
|
49
61
|
|
|
@@ -4,23 +4,34 @@ require_relative "auto-revert/version"
|
|
|
4
4
|
|
|
5
5
|
module Textbringer
|
|
6
6
|
CONFIG[:auto_revert_verbose] ||= true
|
|
7
|
+
CONFIG[:auto_revert_interval] ||= 5
|
|
7
8
|
|
|
8
9
|
class GlobalAutoRevertMode < GlobalMinorMode
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
# Check one buffer and revert it if the file changed on disk.
|
|
11
|
+
# Shared by the post-command hook and the idle timer, and must never
|
|
12
|
+
# raise: post_command_hook runs with remove_on_error: true, so an
|
|
13
|
+
# escaped exception (e.g. Errno::ENOENT from file_modified? after the
|
|
14
|
+
# file was deleted on disk) would silently unregister the hook for the
|
|
15
|
+
# whole editor session. Warn once per buffer instead.
|
|
16
|
+
def self.check_buffer(buffer)
|
|
11
17
|
return unless buffer.file_name && !buffer.name.start_with?("*")
|
|
18
|
+
|
|
12
19
|
unless buffer.file_modified?
|
|
13
20
|
buffer[:auto_revert_warned] = false if buffer[:auto_revert_warned]
|
|
21
|
+
buffer[:auto_revert_error_warned] = false if buffer[:auto_revert_error_warned]
|
|
14
22
|
return
|
|
15
23
|
end
|
|
16
24
|
|
|
17
25
|
if !buffer.modified?
|
|
26
|
+
saved_point = buffer.point
|
|
18
27
|
if buffer.read_only?
|
|
19
28
|
buffer.read_only_edit { buffer.revert }
|
|
20
29
|
else
|
|
21
30
|
buffer.revert
|
|
22
31
|
end
|
|
32
|
+
restore_point(buffer, saved_point)
|
|
23
33
|
buffer[:auto_revert_warned] = false
|
|
34
|
+
buffer[:auto_revert_error_warned] = false
|
|
24
35
|
message("Reverted buffer from file") if CONFIG[:auto_revert_verbose]
|
|
25
36
|
else
|
|
26
37
|
if CONFIG[:auto_revert_verbose] && !buffer[:auto_revert_warned]
|
|
@@ -28,22 +39,89 @@ module Textbringer
|
|
|
28
39
|
buffer[:auto_revert_warned] = true
|
|
29
40
|
end
|
|
30
41
|
end
|
|
42
|
+
rescue StandardError => e
|
|
43
|
+
if CONFIG[:auto_revert_verbose] && !buffer[:auto_revert_error_warned]
|
|
44
|
+
message("Auto-revert failed: #{e.message}")
|
|
45
|
+
buffer[:auto_revert_error_warned] = true
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def self.check_all_buffers
|
|
50
|
+
Buffer.list.each do |buffer|
|
|
51
|
+
check_buffer(buffer)
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
POST_COMMAND_HOOK = -> {
|
|
56
|
+
GlobalAutoRevertMode.check_buffer(Buffer.current)
|
|
31
57
|
}
|
|
32
58
|
|
|
59
|
+
# Buffer#revert resets point to 0; put it back where it was, clamped to
|
|
60
|
+
# the new buffer size. The reverted content may have shifted so the old
|
|
61
|
+
# byte offset can land inside a multi-byte character (goto_char raises
|
|
62
|
+
# ArgumentError) — step back until it sticks.
|
|
63
|
+
def self.restore_point(buffer, pos)
|
|
64
|
+
pos = pos.clamp(0, buffer.size)
|
|
65
|
+
begin
|
|
66
|
+
buffer.goto_char(pos)
|
|
67
|
+
rescue ArgumentError
|
|
68
|
+
pos -= 1
|
|
69
|
+
retry if pos >= 0
|
|
70
|
+
rescue RangeError
|
|
71
|
+
# size changed between clamp and goto_char; stay at 0
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
class << self
|
|
76
|
+
attr_reader :timer_thread
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# Pick up files changed while the editor sits idle: a background thread
|
|
80
|
+
# sleeps, then schedules the scan on the main event loop via foreground
|
|
81
|
+
# (which wakes the idle event loop through its self-pipe). All buffer
|
|
82
|
+
# access happens on the main loop, so no locking is needed.
|
|
83
|
+
def self.start_timer
|
|
84
|
+
return if @timer_thread&.alive?
|
|
85
|
+
@timer_thread = background do
|
|
86
|
+
loop do
|
|
87
|
+
sleep(CONFIG[:auto_revert_interval])
|
|
88
|
+
# The enabled? guard covers a scan already queued when the mode
|
|
89
|
+
# is disabled: the sleeper thread dies, but the queued block
|
|
90
|
+
# still runs on the main loop.
|
|
91
|
+
foreground do
|
|
92
|
+
GlobalAutoRevertMode.check_all_buffers if GlobalAutoRevertMode.enabled?
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# Thread#kill only stops the sleeper; a scan it already queued via
|
|
99
|
+
# foreground may still run once afterwards, which the enabled? guard
|
|
100
|
+
# above turns into a no-op. The join keeps a rapid disable→enable from
|
|
101
|
+
# seeing the dying thread as alive and skipping the restart.
|
|
102
|
+
def self.stop_timer
|
|
103
|
+
if @timer_thread
|
|
104
|
+
@timer_thread.kill
|
|
105
|
+
@timer_thread.join(1)
|
|
106
|
+
@timer_thread = nil
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
33
110
|
def self.enable
|
|
34
111
|
add_hook(:post_command_hook, POST_COMMAND_HOOK)
|
|
112
|
+
start_timer
|
|
35
113
|
message("Global auto-revert mode enabled")
|
|
36
114
|
end
|
|
37
115
|
|
|
38
116
|
def self.disable
|
|
39
117
|
remove_hook(:post_command_hook, POST_COMMAND_HOOK)
|
|
118
|
+
stop_timer
|
|
40
119
|
message("Global auto-revert mode disabled")
|
|
41
120
|
end
|
|
42
121
|
end
|
|
43
122
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
end
|
|
123
|
+
# The global_auto_revert_mode toggle command is auto-defined by
|
|
124
|
+
# GlobalMinorMode.inherited (textbringer >= 15) — do not redefine it here.
|
|
47
125
|
|
|
48
126
|
# Enable by default on plugin load
|
|
49
127
|
GlobalAutoRevertMode.enable
|
data/test/test_helper.rb
CHANGED
|
@@ -21,8 +21,9 @@ module Textbringer
|
|
|
21
21
|
attr_accessor :current, :list
|
|
22
22
|
end
|
|
23
23
|
|
|
24
|
-
attr_accessor :file_name, :name, :read_only
|
|
25
|
-
|
|
24
|
+
attr_accessor :file_name, :name, :read_only, :file_missing, :revert_error,
|
|
25
|
+
:size
|
|
26
|
+
attr_reader :reverted, :properties, :point
|
|
26
27
|
|
|
27
28
|
def initialize(name: nil, file_name: nil)
|
|
28
29
|
@name = name || "test"
|
|
@@ -31,7 +32,24 @@ module Textbringer
|
|
|
31
32
|
@file_modified = false
|
|
32
33
|
@reverted = false
|
|
33
34
|
@read_only = false
|
|
35
|
+
@file_missing = false
|
|
36
|
+
@revert_error = nil
|
|
34
37
|
@properties = {}
|
|
38
|
+
@point = 0
|
|
39
|
+
@size = 100
|
|
40
|
+
@invalid_positions = nil
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
attr_accessor :invalid_positions # byte offsets that fall mid-character
|
|
44
|
+
|
|
45
|
+
# Mirrors real Buffer#goto_char: raises RangeError when out of range,
|
|
46
|
+
# ArgumentError when the position is in the middle of a character.
|
|
47
|
+
def goto_char(pos)
|
|
48
|
+
raise RangeError, "Out of buffer" if pos < 0 || pos > @size
|
|
49
|
+
if @invalid_positions&.include?(pos)
|
|
50
|
+
raise ArgumentError, "Position is in the middle of a character"
|
|
51
|
+
end
|
|
52
|
+
@point = pos
|
|
35
53
|
end
|
|
36
54
|
|
|
37
55
|
def read_only?
|
|
@@ -62,7 +80,10 @@ module Textbringer
|
|
|
62
80
|
@modified = val
|
|
63
81
|
end
|
|
64
82
|
|
|
83
|
+
# Real Buffer#file_modified? calls File.mtime, which raises
|
|
84
|
+
# Errno::ENOENT when the visited file was deleted/renamed on disk.
|
|
65
85
|
def file_modified?
|
|
86
|
+
raise Errno::ENOENT, @file_name if @file_missing
|
|
66
87
|
@file_modified
|
|
67
88
|
end
|
|
68
89
|
|
|
@@ -70,9 +91,12 @@ module Textbringer
|
|
|
70
91
|
@file_modified = val
|
|
71
92
|
end
|
|
72
93
|
|
|
94
|
+
# Real Buffer#revert clears the buffer, which resets point to 0.
|
|
73
95
|
def revert
|
|
74
96
|
raise EditorError, "Read-only buffer" if @read_only
|
|
97
|
+
raise @revert_error if @revert_error
|
|
75
98
|
@reverted = true
|
|
99
|
+
@point = 0
|
|
76
100
|
end
|
|
77
101
|
end
|
|
78
102
|
|
|
@@ -90,6 +114,17 @@ module Textbringer
|
|
|
90
114
|
Textbringer.messages << msg
|
|
91
115
|
end
|
|
92
116
|
|
|
117
|
+
# Real Utils#background starts a plain Thread.
|
|
118
|
+
def background(&block)
|
|
119
|
+
Thread.start(&block)
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
# Real Utils#foreground queues the block onto the main event loop; the
|
|
123
|
+
# mock runs it synchronously, which is equivalent for these tests.
|
|
124
|
+
def foreground(&block)
|
|
125
|
+
block.call
|
|
126
|
+
end
|
|
127
|
+
|
|
93
128
|
module Commands
|
|
94
129
|
def self.define_command(name, doc: nil, &block)
|
|
95
130
|
define_method(name, &block)
|
|
@@ -107,6 +142,35 @@ module Textbringer
|
|
|
107
142
|
alias enabled? enabled
|
|
108
143
|
end
|
|
109
144
|
|
|
145
|
+
# Real GlobalMinorMode.inherited (textbringer >= 15) auto-defines a
|
|
146
|
+
# toggle command named after the subclass.
|
|
147
|
+
def self.inherited(child)
|
|
148
|
+
super
|
|
149
|
+
child.instance_variable_set(:@enabled, false)
|
|
150
|
+
base_name = child.name.slice(/[^:]*\z/)
|
|
151
|
+
command_name = base_name.sub(/\A[A-Z]/) { |s| s.downcase }
|
|
152
|
+
.gsub(/(?<=[a-z])([A-Z])/) { "_#{$1.downcase}" }
|
|
153
|
+
Textbringer.define_command(command_name.intern) do |arg = nil|
|
|
154
|
+
enable =
|
|
155
|
+
case arg
|
|
156
|
+
when true, false
|
|
157
|
+
next if child.enabled? == arg
|
|
158
|
+
arg
|
|
159
|
+
when nil
|
|
160
|
+
!child.enabled?
|
|
161
|
+
else
|
|
162
|
+
raise ArgumentError, "wrong argument #{arg.inspect} (expected true, false, or nil)"
|
|
163
|
+
end
|
|
164
|
+
if enable
|
|
165
|
+
child.enable
|
|
166
|
+
child.enabled = true
|
|
167
|
+
else
|
|
168
|
+
child.disable
|
|
169
|
+
child.enabled = false
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
|
|
110
174
|
def self.add_hook(name, hook)
|
|
111
175
|
Textbringer.add_hook(name, hook)
|
|
112
176
|
end
|
|
@@ -118,6 +182,14 @@ module Textbringer
|
|
|
118
182
|
def self.message(msg)
|
|
119
183
|
Textbringer.message(msg)
|
|
120
184
|
end
|
|
185
|
+
|
|
186
|
+
def self.background(&block)
|
|
187
|
+
Textbringer.background(&block)
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
def self.foreground(&block)
|
|
191
|
+
Textbringer.foreground(&block)
|
|
192
|
+
end
|
|
121
193
|
end
|
|
122
194
|
end
|
|
123
195
|
|
|
@@ -12,6 +12,17 @@ class Textbringer::GlobalAutoRevertModeTest < Test::Unit::TestCase
|
|
|
12
12
|
Textbringer.messages = []
|
|
13
13
|
Textbringer::HOOKS.clear
|
|
14
14
|
Textbringer::CONFIG[:auto_revert_verbose] = true
|
|
15
|
+
stop_timer
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def teardown
|
|
19
|
+
stop_timer
|
|
20
|
+
Textbringer::CONFIG[:auto_revert_interval] = 5
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private def stop_timer
|
|
24
|
+
mode = Textbringer::GlobalAutoRevertMode
|
|
25
|
+
mode.stop_timer if mode.respond_to?(:stop_timer)
|
|
15
26
|
end
|
|
16
27
|
|
|
17
28
|
test "VERSION is defined" do
|
|
@@ -123,4 +134,192 @@ class Textbringer::GlobalAutoRevertModeTest < Test::Unit::TestCase
|
|
|
123
134
|
assert_equal true, @buffer.reverted
|
|
124
135
|
assert_equal true, @buffer.read_only?, "Buffer should remain read-only after revert"
|
|
125
136
|
end
|
|
137
|
+
|
|
138
|
+
test "does not raise when the visited file is missing on disk" do
|
|
139
|
+
Textbringer::GlobalAutoRevertMode.enable
|
|
140
|
+
@buffer.file_missing = true
|
|
141
|
+
|
|
142
|
+
assert_nothing_raised do
|
|
143
|
+
Textbringer::GlobalAutoRevertMode::POST_COMMAND_HOOK.call
|
|
144
|
+
end
|
|
145
|
+
assert_equal false, @buffer.reverted
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
test "warns only once while the file remains missing" do
|
|
149
|
+
Textbringer::GlobalAutoRevertMode.enable
|
|
150
|
+
@buffer.file_missing = true
|
|
151
|
+
|
|
152
|
+
Textbringer::GlobalAutoRevertMode::POST_COMMAND_HOOK.call
|
|
153
|
+
Textbringer::GlobalAutoRevertMode::POST_COMMAND_HOOK.call
|
|
154
|
+
|
|
155
|
+
warnings = Textbringer.messages.grep(/Auto-revert failed/)
|
|
156
|
+
assert_equal 1, warnings.size
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
test "auto-revert works again and warns anew after the file reappears" do
|
|
160
|
+
Textbringer::GlobalAutoRevertMode.enable
|
|
161
|
+
@buffer.file_missing = true
|
|
162
|
+
Textbringer::GlobalAutoRevertMode::POST_COMMAND_HOOK.call
|
|
163
|
+
|
|
164
|
+
@buffer.file_missing = false
|
|
165
|
+
@buffer.file_modified = true
|
|
166
|
+
Textbringer::GlobalAutoRevertMode::POST_COMMAND_HOOK.call
|
|
167
|
+
assert_equal true, @buffer.reverted
|
|
168
|
+
|
|
169
|
+
@buffer.file_missing = true
|
|
170
|
+
Textbringer::GlobalAutoRevertMode::POST_COMMAND_HOOK.call
|
|
171
|
+
warnings = Textbringer.messages.grep(/Auto-revert failed/)
|
|
172
|
+
assert_equal 2, warnings.size
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
test "does not raise when revert itself fails (file deleted mid-revert)" do
|
|
176
|
+
Textbringer::GlobalAutoRevertMode.enable
|
|
177
|
+
@buffer.file_modified = true
|
|
178
|
+
@buffer.revert_error = Errno::ENOENT.new("/tmp/test.txt")
|
|
179
|
+
|
|
180
|
+
assert_nothing_raised do
|
|
181
|
+
Textbringer::GlobalAutoRevertMode::POST_COMMAND_HOOK.call
|
|
182
|
+
end
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
test "preserves point across auto-revert" do
|
|
186
|
+
Textbringer::GlobalAutoRevertMode.enable
|
|
187
|
+
@buffer.file_modified = true
|
|
188
|
+
@buffer.goto_char(42)
|
|
189
|
+
|
|
190
|
+
Textbringer::GlobalAutoRevertMode::POST_COMMAND_HOOK.call
|
|
191
|
+
|
|
192
|
+
assert_equal true, @buffer.reverted
|
|
193
|
+
assert_equal 42, @buffer.point
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
test "clamps point to buffer size when the file shrank below old point" do
|
|
197
|
+
Textbringer::GlobalAutoRevertMode.enable
|
|
198
|
+
@buffer.file_modified = true
|
|
199
|
+
@buffer.goto_char(90)
|
|
200
|
+
@buffer.size = 10 # reverted content is shorter than old point
|
|
201
|
+
|
|
202
|
+
assert_nothing_raised do
|
|
203
|
+
Textbringer::GlobalAutoRevertMode::POST_COMMAND_HOOK.call
|
|
204
|
+
end
|
|
205
|
+
assert_equal true, @buffer.reverted
|
|
206
|
+
assert_equal 10, @buffer.point
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
test "steps back to a character boundary when old point lands mid-character" do
|
|
210
|
+
Textbringer::GlobalAutoRevertMode.enable
|
|
211
|
+
@buffer.file_modified = true
|
|
212
|
+
@buffer.goto_char(42)
|
|
213
|
+
@buffer.invalid_positions = [42, 41] # content shifted; 42 is now inside a multi-byte char
|
|
214
|
+
|
|
215
|
+
assert_nothing_raised do
|
|
216
|
+
Textbringer::GlobalAutoRevertMode::POST_COMMAND_HOOK.call
|
|
217
|
+
end
|
|
218
|
+
assert_equal true, @buffer.reverted
|
|
219
|
+
assert_equal 40, @buffer.point
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
test "suppresses missing-file warnings when verbose is disabled" do
|
|
223
|
+
Textbringer::GlobalAutoRevertMode.enable
|
|
224
|
+
Textbringer::CONFIG[:auto_revert_verbose] = false
|
|
225
|
+
@buffer.file_missing = true
|
|
226
|
+
|
|
227
|
+
Textbringer::GlobalAutoRevertMode::POST_COMMAND_HOOK.call
|
|
228
|
+
|
|
229
|
+
assert_empty Textbringer.messages.grep(/Auto-revert failed/)
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
test "default auto_revert_interval is 5 seconds" do
|
|
233
|
+
assert_equal 5, Textbringer::CONFIG[:auto_revert_interval]
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
test "check_all_buffers reverts every stale file-backed buffer, not just the current one" do
|
|
237
|
+
other = Textbringer::Buffer.new(name: "other.txt", file_name: "/tmp/other.txt")
|
|
238
|
+
special = Textbringer::Buffer.new(name: "*scratch*", file_name: "/tmp/scratch")
|
|
239
|
+
fileless = Textbringer::Buffer.new(name: "no-file")
|
|
240
|
+
[@buffer, other, special, fileless].each { |b| b.file_modified = true }
|
|
241
|
+
Textbringer::Buffer.list = [@buffer, other, special, fileless]
|
|
242
|
+
|
|
243
|
+
Textbringer::GlobalAutoRevertMode.check_all_buffers
|
|
244
|
+
|
|
245
|
+
assert_equal true, @buffer.reverted
|
|
246
|
+
assert_equal true, other.reverted
|
|
247
|
+
assert_equal false, special.reverted
|
|
248
|
+
assert_equal false, fileless.reverted
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
test "a broken buffer does not stop the scan of the remaining buffers" do
|
|
252
|
+
broken = Textbringer::Buffer.new(name: "broken.txt", file_name: "/tmp/broken.txt")
|
|
253
|
+
broken.file_missing = true
|
|
254
|
+
ok = Textbringer::Buffer.new(name: "ok.txt", file_name: "/tmp/ok.txt")
|
|
255
|
+
ok.file_modified = true
|
|
256
|
+
Textbringer::Buffer.list = [broken, ok]
|
|
257
|
+
|
|
258
|
+
assert_nothing_raised do
|
|
259
|
+
Textbringer::GlobalAutoRevertMode.check_all_buffers
|
|
260
|
+
end
|
|
261
|
+
assert_equal true, ok.reverted
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
test "enable starts the idle timer thread and disable stops it" do
|
|
265
|
+
Textbringer::GlobalAutoRevertMode.enable
|
|
266
|
+
thread = Textbringer::GlobalAutoRevertMode.timer_thread
|
|
267
|
+
assert_not_nil thread
|
|
268
|
+
assert_true thread.alive?
|
|
269
|
+
|
|
270
|
+
Textbringer::GlobalAutoRevertMode.disable
|
|
271
|
+
thread.join(1)
|
|
272
|
+
assert_false thread.alive?
|
|
273
|
+
assert_nil Textbringer::GlobalAutoRevertMode.timer_thread
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
test "enabling twice keeps exactly one timer thread" do
|
|
277
|
+
Textbringer::GlobalAutoRevertMode.enable
|
|
278
|
+
first = Textbringer::GlobalAutoRevertMode.timer_thread
|
|
279
|
+
Textbringer::GlobalAutoRevertMode.enable
|
|
280
|
+
|
|
281
|
+
assert_same first, Textbringer::GlobalAutoRevertMode.timer_thread
|
|
282
|
+
end
|
|
283
|
+
|
|
284
|
+
test "global_auto_revert_mode command toggles the mode without raising" do
|
|
285
|
+
Textbringer::GlobalAutoRevertMode.enable
|
|
286
|
+
Textbringer::GlobalAutoRevertMode.enabled = true
|
|
287
|
+
|
|
288
|
+
assert_nothing_raised do
|
|
289
|
+
Textbringer::Commands.global_auto_revert_mode
|
|
290
|
+
end
|
|
291
|
+
assert_false Textbringer::GlobalAutoRevertMode.enabled?
|
|
292
|
+
refute_includes Textbringer::HOOKS[:post_command_hook],
|
|
293
|
+
Textbringer::GlobalAutoRevertMode::POST_COMMAND_HOOK
|
|
294
|
+
|
|
295
|
+
Textbringer::Commands.global_auto_revert_mode
|
|
296
|
+
assert_true Textbringer::GlobalAutoRevertMode.enabled?
|
|
297
|
+
assert_includes Textbringer::HOOKS[:post_command_hook],
|
|
298
|
+
Textbringer::GlobalAutoRevertMode::POST_COMMAND_HOOK
|
|
299
|
+
end
|
|
300
|
+
|
|
301
|
+
test "idle timer reverts a stale buffer without any command execution" do
|
|
302
|
+
Textbringer::CONFIG[:auto_revert_interval] = 0.01
|
|
303
|
+
@buffer.file_modified = true
|
|
304
|
+
|
|
305
|
+
Textbringer::GlobalAutoRevertMode.enable
|
|
306
|
+
Textbringer::GlobalAutoRevertMode.enabled = true
|
|
307
|
+
deadline = Time.now + 2
|
|
308
|
+
sleep 0.01 until @buffer.reverted || Time.now > deadline
|
|
309
|
+
|
|
310
|
+
assert_equal true, @buffer.reverted
|
|
311
|
+
end
|
|
312
|
+
|
|
313
|
+
test "idle scan is a no-op while the mode is flagged off" do
|
|
314
|
+
Textbringer::CONFIG[:auto_revert_interval] = 0.01
|
|
315
|
+
@buffer.file_modified = true
|
|
316
|
+
|
|
317
|
+
Textbringer::GlobalAutoRevertMode.enable
|
|
318
|
+
Textbringer::GlobalAutoRevertMode.enabled = false
|
|
319
|
+
sleep 0.1
|
|
320
|
+
|
|
321
|
+
assert_equal false, @buffer.reverted
|
|
322
|
+
ensure
|
|
323
|
+
Textbringer::GlobalAutoRevertMode.enabled = true
|
|
324
|
+
end
|
|
126
325
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: textbringer-auto-revert
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.5.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- yancya
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-11-20 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: textbringer
|
|
@@ -16,14 +16,14 @@ dependencies:
|
|
|
16
16
|
requirements:
|
|
17
17
|
- - ">="
|
|
18
18
|
- !ruby/object:Gem::Version
|
|
19
|
-
version: '
|
|
19
|
+
version: '15'
|
|
20
20
|
type: :runtime
|
|
21
21
|
prerelease: false
|
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
|
23
23
|
requirements:
|
|
24
24
|
- - ">="
|
|
25
25
|
- !ruby/object:Gem::Version
|
|
26
|
-
version: '
|
|
26
|
+
version: '15'
|
|
27
27
|
description: A Textbringer plugin that automatically reverts buffers when files are
|
|
28
28
|
modified externally.
|
|
29
29
|
email:
|
|
@@ -32,6 +32,8 @@ executables: []
|
|
|
32
32
|
extensions: []
|
|
33
33
|
extra_rdoc_files: []
|
|
34
34
|
files:
|
|
35
|
+
- CHANGELOG.md
|
|
36
|
+
- CLAUDE.md
|
|
35
37
|
- LICENSE.txt
|
|
36
38
|
- README.md
|
|
37
39
|
- Rakefile
|
|
@@ -48,6 +50,7 @@ metadata:
|
|
|
48
50
|
allowed_push_host: https://rubygems.org
|
|
49
51
|
homepage_uri: https://github.com/yancya/textbringer-auto-revert
|
|
50
52
|
source_code_uri: https://github.com/yancya/textbringer-auto-revert
|
|
53
|
+
changelog_uri: https://github.com/yancya/textbringer-auto-revert/blob/main/CHANGELOG.md
|
|
51
54
|
post_install_message:
|
|
52
55
|
rdoc_options: []
|
|
53
56
|
require_paths:
|