|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "log/slog" |
| 8 | + "net/http" |
| 9 | + "os" |
| 10 | + "os/exec" |
| 11 | + "path/filepath" |
| 12 | + "strings" |
| 13 | + "time" |
| 14 | + |
| 15 | + "grepnavi/search" |
| 16 | +) |
| 17 | + |
| 18 | +// findCtagsBin は Universal Ctags を優先して ctags バイナリのパスを返す。 |
| 19 | +// Universal Ctags が見つからない場合は PATH 上の ctags を返す。 |
| 20 | +func findCtagsBin() (string, bool) { |
| 21 | + // 候補パスを順に試す(Universal Ctags を優先) |
| 22 | + candidates := []string{} |
| 23 | + |
| 24 | + // Scoop のシムパス |
| 25 | + if home, err := os.UserHomeDir(); err == nil { |
| 26 | + candidates = append(candidates, |
| 27 | + filepath.Join(home, "scoop", "shims", "ctags.exe"), |
| 28 | + filepath.Join(home, "scoop", "shims", "ctags"), |
| 29 | + ) |
| 30 | + } |
| 31 | + |
| 32 | + // PATH 上の全 ctags を探す |
| 33 | + if p, err := exec.LookPath("ctags"); err == nil { |
| 34 | + candidates = append(candidates, p) |
| 35 | + } |
| 36 | + |
| 37 | + // 各候補で Universal Ctags かチェック |
| 38 | + for _, p := range candidates { |
| 39 | + if _, err := os.Stat(p); err != nil { |
| 40 | + continue |
| 41 | + } |
| 42 | + out, err := exec.Command(p, "--version").Output() |
| 43 | + if err != nil { |
| 44 | + continue |
| 45 | + } |
| 46 | + if strings.Contains(string(out), "Universal Ctags") { |
| 47 | + return p, true |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + // Universal Ctags が見つからなければ PATH 上の ctags を使う |
| 52 | + if p, err := exec.LookPath("ctags"); err == nil { |
| 53 | + return p, true |
| 54 | + } |
| 55 | + return "", false |
| 56 | +} |
| 57 | + |
| 58 | +// handleCtagsMacros はファイルに出現するマクロ名をctagsキャッシュとの積集合で返す。 |
| 59 | +func (h *Handler) handleCtagsMacros(w http.ResponseWriter, r *http.Request) { |
| 60 | + h.mu.RLock() |
| 61 | + root := h.root |
| 62 | + h.mu.RUnlock() |
| 63 | + empty := map[string]interface{}{"macros": []string{}, "ready": false, "loading": false} |
| 64 | + if !search.CtagsIndexed(root) { |
| 65 | + jsonOK(w, map[string]interface{}{"macros": []string{}, "ready": true, "loading": false}) |
| 66 | + return |
| 67 | + } |
| 68 | + slog.Debug("ctags-macros request", "root", root) |
| 69 | + state := search.CtagsMacroNames(root) |
| 70 | + slog.Debug("ctags-macros state", "ready", state.Ready, "loading", state.Loading) |
| 71 | + if state.Loading { |
| 72 | + jsonOK(w, map[string]interface{}{"macros": []string{}, "ready": false, "loading": true}) |
| 73 | + return |
| 74 | + } |
| 75 | + if !state.Ready { |
| 76 | + jsonOK(w, empty) |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + file := r.URL.Query().Get("file") |
| 81 | + if file == "" { |
| 82 | + jsonOK(w, empty) |
| 83 | + return |
| 84 | + } |
| 85 | + if !strings.HasPrefix(filepath.Clean(file), filepath.Clean(root)) { |
| 86 | + jsonOK(w, empty) |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + // ファイルに出現するシンボルをkind別に返す |
| 91 | + slog.Debug("ctags-macros file", "file", file) |
| 92 | + syms := search.SymbolsInFile(file, state.Symbols) |
| 93 | + slog.Debug("ctags-macros result", "macros", len(syms.Macros)) |
| 94 | + jsonOK(w, map[string]interface{}{"macros": syms.Macros, "ready": true, "loading": false}) |
| 95 | +} |
| 96 | + |
| 97 | +// handleCtagsFileSymbols は指定ファイルの ctags シンボル一覧を返す。 |
| 98 | +func (h *Handler) handleCtagsFileSymbols(w http.ResponseWriter, r *http.Request) { |
| 99 | + h.mu.RLock() |
| 100 | + root := h.root |
| 101 | + h.mu.RUnlock() |
| 102 | + |
| 103 | + file := r.URL.Query().Get("file") |
| 104 | + if file == "" { |
| 105 | + http.Error(w, "file required", http.StatusBadRequest) |
| 106 | + return |
| 107 | + } |
| 108 | + if !search.CtagsIndexed(root) { |
| 109 | + jsonOK(w, []search.DefHit{}) |
| 110 | + return |
| 111 | + } |
| 112 | + hits, err := search.CtagsSymbolsForFile(file, root) |
| 113 | + if err != nil { |
| 114 | + jsonOK(w, []search.DefHit{}) |
| 115 | + return |
| 116 | + } |
| 117 | + jsonOK(w, hits) |
| 118 | +} |
| 119 | + |
| 120 | +func (h *Handler) handleCtagsStatus(w http.ResponseWriter, r *http.Request) { |
| 121 | + h.mu.RLock() |
| 122 | + root := h.root |
| 123 | + h.mu.RUnlock() |
| 124 | + _, installed := findCtagsBin() |
| 125 | + indexed := search.CtagsIndexed(root) |
| 126 | + jsonOK(w, map[string]interface{}{ |
| 127 | + "installed": installed, |
| 128 | + "indexed": indexed, |
| 129 | + }) |
| 130 | +} |
| 131 | + |
| 132 | +func (h *Handler) handleCtagsIndex(w http.ResponseWriter, r *http.Request) { |
| 133 | + h.mu.RLock() |
| 134 | + root := h.root |
| 135 | + h.mu.RUnlock() |
| 136 | + |
| 137 | + flusher, ok := w.(http.Flusher) |
| 138 | + if !ok { |
| 139 | + http.Error(w, "streaming unsupported", http.StatusInternalServerError) |
| 140 | + return |
| 141 | + } |
| 142 | + w.Header().Set("Content-Type", "text/event-stream") |
| 143 | + w.Header().Set("Cache-Control", "no-cache") |
| 144 | + w.Header().Set("Connection", "keep-alive") |
| 145 | + |
| 146 | + sendLine := func(line string) { |
| 147 | + fmt.Fprintf(w, "data: %s\n\n", line) |
| 148 | + flusher.Flush() |
| 149 | + } |
| 150 | + sendEvent := func(event, data string) { |
| 151 | + fmt.Fprintf(w, "event: %s\ndata: %s\n\n", event, data) |
| 152 | + flusher.Flush() |
| 153 | + } |
| 154 | + |
| 155 | + slog.Info("ctags-index start", "root", root) |
| 156 | + sendLine("--- ctags インデックス生成開始: " + root + " ---") |
| 157 | + |
| 158 | + ctagsBin, ok := findCtagsBin() |
| 159 | + if !ok { |
| 160 | + sendEvent("ctags-error", "ctags が見つかりません") |
| 161 | + return |
| 162 | + } |
| 163 | + sendLine("使用バイナリ: " + ctagsBin) |
| 164 | + |
| 165 | + var stderrBuf bytes.Buffer |
| 166 | + tagsPath := filepath.Join(root, "tags") |
| 167 | + cmd := exec.CommandContext(context.Background(), ctagsBin, "-R", "--fields=+n", "-f", tagsPath, root) |
| 168 | + cmd.Stderr = &stderrBuf |
| 169 | + |
| 170 | + if err := cmd.Start(); err != nil { |
| 171 | + sendEvent("ctags-error", err.Error()) |
| 172 | + return |
| 173 | + } |
| 174 | + |
| 175 | + // ctags は進捗出力がないので1秒ごとにハートビートを送る |
| 176 | + done := make(chan error, 1) |
| 177 | + go func() { done <- cmd.Wait() }() |
| 178 | + ticker := time.NewTicker(time.Second) |
| 179 | + defer ticker.Stop() |
| 180 | +loop: |
| 181 | + for { |
| 182 | + select { |
| 183 | + case err := <-done: |
| 184 | + if err != nil { |
| 185 | + msg := err.Error() |
| 186 | + if s := stderrBuf.String(); s != "" { |
| 187 | + msg += "\nstderr: " + s |
| 188 | + } |
| 189 | + // exit code 1 は警告扱い(tags ファイルは生成されている場合) |
| 190 | + if exitErr, ok := err.(*exec.ExitError); ok && exitErr.ExitCode() == 1 { |
| 191 | + sendLine("警告(exit=1): " + msg) |
| 192 | + break loop |
| 193 | + } |
| 194 | + sendEvent("ctags-error", msg) |
| 195 | + return |
| 196 | + } |
| 197 | + break loop |
| 198 | + case <-ticker.C: |
| 199 | + sendLine("... 生成中") |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + search.CtagsMacroWarmup(root) |
| 204 | + sendLine("--- 完了 ---") |
| 205 | + sendEvent("ctags-done", "ok") |
| 206 | +} |
0 commit comments