From 7d91e18ed405fad6c42d00ad58ec36e95eaa942b Mon Sep 17 00:00:00 2001 From: Serag Ahmed Date: Tue, 17 Nov 2026 17:04:05 +0300 Subject: [PATCH] fix: A run that wraps in a list item is placed where it is mapRun searched for a run as written, but a run that wraps inside a list item or blockquote has the item's indentation or `>` after each line break in the source. The search missed it, or found a copy further down and moved the source cursor past every run after it, so their alerts were placed by searching the whole file. Place such a run with wrappedRuns, as locateWrapped already does for blocks. Fixes #1227 Assisted-by: Claude Code --- internal/lint/walk.go | 16 ++++++++++++++++ internal/lint/walk_test.go | 23 +++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/internal/lint/walk.go b/internal/lint/walk.go index d34423a7..e1711d61 100644 --- a/internal/lint/walk.go +++ b/internal/lint/walk.go @@ -699,6 +699,22 @@ func (w *walker) mapRun(at int, raw string) { return } + // A run that wraps inside a list item or blockquote has the item's + // indentation or `>` after each line break in the source, which raw does + // not. Searching for raw as written misses it, or finds a copy further + // down and drags the cursor past every run after it, leaving each of + // their alerts to be placed by searching the file. See #1227. + if strings.Contains(raw, "\n") { + if runs, end := wrappedRuns(raw, ctx, w.srcCursor); runs != nil { + for _, r := range runs { + r.At += at + w.spans = append(w.spans, r) + } + w.srcCursor = end + return + } + } + i := strings.Index(ctx[w.srcCursor:], raw) if i < 0 { w.mapWords(at, raw, ctx) diff --git a/internal/lint/walk_test.go b/internal/lint/walk_test.go index c67480fc..65fa5d92 100644 --- a/internal/lint/walk_test.go +++ b/internal/lint/walk_test.go @@ -304,6 +304,29 @@ func TestWrappedRuns(t *testing.T) { } } +// A run that wraps inside a list item is mapped where it is, not to a later +// copy outside the item, and the cursor stops there. See #1227. +func TestMapRunWrapped(t *testing.T) { + ctx := "- Hello there. This is some text,\n and this is some more.\n\nMore text, unrelated,\nand yet more.\n" + raw := ",\nand" + + w := &walker{context: []byte(ctx)} + w.mapRun(5, raw) + + want := []nlp.Run{{At: 5, Src: 32, N: 1}, {At: 7, Src: 36, N: 3}} + if !reflect.DeepEqual(w.spans, want) { + t.Fatalf("spans = %v, want %v", w.spans, want) + } + if w.srcCursor != 39 { + t.Errorf("srcCursor = %d, want 39", w.srcCursor) + } + + w.mapRun(0, "More text") + if w.spans[len(w.spans)-1].Src != 60 { + t.Errorf("the run after the list item was placed at %d, want 60", w.spans[len(w.spans)-1].Src) + } +} + // A run the converter rewrote is mapped by its words, so a match in it is // placed where the word is rather than wherever the text first appears. func TestMapWords(t *testing.T) {