Skip to content

Commit c1f121c

Browse files
committed
wip
wip 1hit
1 parent 3c620fe commit c1f121c

3 files changed

Lines changed: 31 additions & 11 deletions

File tree

api/handlers_analysis.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,12 @@ func (h *Handler) handleDefinition(w http.ResponseWriter, r *http.Request) {
107107
if len(h) == 0 && e == nil {
108108
slog.Debug("definition gtags miss, fallback to rg", "word", word)
109109
t0 = time.Now()
110-
h, e = search.FindDefinitions(r.Context(), word, dir, glob)
110+
currentFile := q.Get("file")
111+
if currentFile != "" {
112+
h, e = search.FindDefinitionsSmart(r.Context(), word, currentFile, hroot, glob)
113+
} else {
114+
h, e = search.FindDefinitions(r.Context(), word, dir, glob)
115+
}
111116
eng = "rg"
112117
slog.Debug("definition rg fallback result", "word", word, "hits", len(h), "elapsed", time.Since(t0))
113118
}

search/definition.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package search
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"log/slog"
78
"os"
@@ -10,6 +11,8 @@ import (
1011
"regexp"
1112
"strings"
1213
"time"
14+
15+
"grepnavi/graph"
1316
)
1417

1518
// DefHit は定義箇所の1件。
@@ -109,7 +112,7 @@ func FindDefinitionsSmart(ctx context.Context, word, currentFile, root, glob str
109112
var hits []DefHit
110113
var err error
111114
if dir == root {
112-
hits, err = FindDefinitionsN(innerCtx, word, root, glob, 50)
115+
hits, err = FindDefinitionsN(innerCtx, word, root, glob, 1)
113116
} else {
114117
files := listFilesInDir(dir, glob)
115118
if len(files) > 0 {
@@ -279,27 +282,37 @@ func FindDefinitionsN(ctx context.Context, word, dir, glob string, maxPerQuery i
279282
Regex: true,
280283
CaseSensitive: true,
281284
ContextLines: -1,
282-
MaxResults: maxPerQuery,
283-
}
284-
matches, err := Search(ctx, opts)
285-
if err != nil {
286-
return nil, err
287285
}
288286

287+
// SearchStream を使い、実態(定義)を maxPerQuery 件見つけた時点で rg を即 kill する。
288+
// 宣言(行末 ; など)はカウントせず収集のみ行う。
289+
// これにより「.h の宣言を先に見つけても続行し、.c の定義で即終了」が実現できる。
289290
seen := map[string]bool{}
290291
var results []DefHit
291-
for _, m := range matches {
292+
defCount := 0
293+
errDone := errors.New("done")
294+
if err := SearchStream(ctx, opts, func(m graph.Match) error {
292295
key := fmt.Sprintf("%s:%d", m.File, m.Line)
293296
if seen[key] {
294-
continue
297+
return nil
295298
}
296299
seen[key] = true
297-
results = append(results, DefHit{
300+
hit := DefHit{
298301
File: m.File,
299302
Line: m.Line,
300303
Text: strings.TrimSpace(m.Text),
301304
Kind: classifyDefKind(m.Text, word),
302-
})
305+
}
306+
results = append(results, hit)
307+
if isDefinitionHit(hit) {
308+
defCount++
309+
if defCount >= maxPerQuery {
310+
return errDone // 実態を maxPerQuery 件確認した時点で rg を kill
311+
}
312+
}
313+
return nil
314+
}); err != nil {
315+
return nil, err
303316
}
304317

305318
// K&R スタイル: 関数名と ( が別行のケース(例: "void func\n(\n...")

search/ripgrep.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ func matchID(file string, line, col int) string {
235235
func SearchStream(ctx context.Context, opts Options, callback func(graph.Match) error) error {
236236
if opts.ContextLines == 0 {
237237
opts.ContextLines = 6
238+
} else if opts.ContextLines < 0 {
239+
opts.ContextLines = 0
238240
}
239241

240242
innerCtx, cancel := context.WithCancel(ctx)

0 commit comments

Comments
 (0)