Add change-with-nth action to dynamically change --with-nth (#4691)

This commit is contained in:
Junegunn Choi
2026-03-01 16:09:54 +09:00
committed by GitHub
parent 5887edc6ba
commit 3cfee281b4
9 changed files with 548 additions and 181 deletions

View File

@@ -45,6 +45,8 @@ type Matcher struct {
sortBuf [][]Result
mergerCache map[string]MatchResult
revision revision
scanMutex sync.Mutex
cancelScan *util.AtomicBool
}
const (
@@ -70,7 +72,8 @@ func NewMatcher(cache *ChunkCache, patternBuilder func([]rune) *Pattern,
slab: make([]*util.Slab, partitions),
sortBuf: make([][]Result, partitions),
mergerCache: make(map[string]MatchResult),
revision: revision}
revision: revision,
cancelScan: util.NewAtomicBool(false)}
}
// Loop puts Matcher in action
@@ -130,7 +133,9 @@ func (m *Matcher) Loop() {
}
if result.merger == nil {
m.scanMutex.Lock()
result = m.scan(request)
m.scanMutex.Unlock()
}
if !result.cancelled {
@@ -238,7 +243,7 @@ func (m *Matcher) scan(request MatchRequest) MatchResult {
break
}
if m.reqBox.Peek(reqReset) {
if m.cancelScan.Get() || m.reqBox.Peek(reqReset) {
return MatchResult{nil, nil, wait()}
}
@@ -269,6 +274,20 @@ func (m *Matcher) Reset(chunks []*Chunk, patternRunes []rune, cancel bool, final
m.reqBox.Set(event, MatchRequest{chunks, pattern, final, sort, revision})
}
// CancelScan cancels any in-flight scan, waits for it to finish,
// and prevents new scans from starting until ResumeScan is called.
// This is used to safely mutate shared items (e.g., during with-nth changes).
func (m *Matcher) CancelScan() {
m.cancelScan.Set(true)
m.scanMutex.Lock()
m.cancelScan.Set(false)
}
// ResumeScan allows scans to proceed again after CancelScan.
func (m *Matcher) ResumeScan() {
m.scanMutex.Unlock()
}
func (m *Matcher) Stop() {
m.reqBox.Set(reqQuit, nil)
}