Repository navigation
Expand file tree
/
Copy pathconfig.go
More file actions
250 lines (226 loc) · 6.77 KB
/
Copy pathconfig.go
File metadata and controls
250 lines (226 loc) · 6.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// SPDX-License-Identifier: MIT
package main
import (
"fmt"
"strings"
"time"
"github.com/boyter/cs/v3/pkg/common"
"github.com/boyter/cs/v3/pkg/ranker"
"github.com/boyter/cs/v3/pkg/search"
"github.com/boyter/cs/v3/pkg/snippet"
"github.com/boyter/gocodewalker"
)
// resolveSnippetMode returns the effective snippet mode for a file.
// If globalMode is "auto", it selects based on the file extension.
func resolveSnippetMode(globalMode, filename string) string {
if globalMode != "auto" {
return globalMode
}
return snippet.SnippetModeForExtension(gocodewalker.GetExtension(filename))
}
// Config holds all CLI-configurable fields for the search tool.
type Config struct {
// Search
SearchString []string
CaseSensitive bool
SnippetLength int
SnippetCount int
SnippetMode string
Ranker string
Profile string
GravityIntent string
NoiseIntent string
TestPenalty float64
ResultLimit int
LineLimit int
// DefaultOperator combines adjacent terms with no explicit AND/OR between
// them: "and" (default) requires all terms, "or" matches any term.
DefaultOperator string
// File walker
Directory string
FindRoot bool
AllowListExtensions []string
LanguageTypes []string
PathDenylist []string
LocationExcludePattern []string
IgnoreGitIgnore bool
IgnoreIgnoreFile bool
IncludeHidden bool
IncludeBinaryFiles bool
IncludeMinified bool
MinifiedLineByteLength int
MaxReadSizeBytes int64
// Structural ranker weights
WeightCode float64
WeightComment float64
WeightString float64
OnlyCode bool
OnlyComments bool
OnlyStrings bool
OnlyDeclarations bool
OnlyUsages bool
// Grep context
ContextBefore int // -B: lines before each match
ContextAfter int // -A: lines after each match
ContextAround int // -C: lines before and after (sets both)
// Output
Format string
FileOutput string
Verbose bool
NoSyntax bool // disable syntax highlighting
Dedup bool // collapse byte-identical matches
Color string // color mode: auto, always, never
Reverse bool // reverse result order (lowest score first)
// Query complexity limits (0 = no limit)
MaxQueryChars int
MaxQueryTerms int
// MCP
MCPServer bool
// MCPLockDir restricts the MCP server to Directory: the per-call "path"
// search root and any get_file path outside that tree are rejected.
MCPLockDir bool
// HTTP
Address string
HttpServer bool
SearchTemplate string
DisplayTemplate string
TemplateStyle string
// Git sync
GitSync bool
GitSyncInterval time.Duration
GitSyncWorkers int
}
// DefaultConfig returns a Config with sensible defaults matching the root-level globals.
func DefaultConfig() Config {
defaults := ranker.DefaultStructuralConfig()
return Config{
SnippetLength: 300,
SnippetCount: 1,
SnippetMode: "auto",
Ranker: "structural",
GravityIntent: "default",
NoiseIntent: "default",
TestPenalty: 0.4,
ResultLimit: -1,
LineLimit: -1,
DefaultOperator: "and",
PathDenylist: []string{".git", ".hg", ".svn"},
MinifiedLineByteLength: 255,
MaxReadSizeBytes: 1_000_000,
WeightCode: defaults.WeightCode,
WeightComment: defaults.WeightComment,
WeightString: defaults.WeightString,
MaxQueryChars: common.MaxQueryCharsDefault,
MaxQueryTerms: common.MaxQueryTermsDefault,
Format: "text",
Address: ":8080",
TemplateStyle: "dark",
}
}
// StructuralRankerConfig returns a StructuralConfig populated from this Config.
func (c *Config) StructuralRankerConfig() *ranker.StructuralConfig {
return &ranker.StructuralConfig{
WeightCode: c.WeightCode,
WeightComment: c.WeightComment,
WeightString: c.WeightString,
OnlyCode: c.OnlyCode,
OnlyComments: c.OnlyComments,
OnlyStrings: c.OnlyStrings,
}
}
// HasContentFilter returns true if any content-type filter is active.
func (c *Config) HasContentFilter() bool {
return c.OnlyCode || c.OnlyComments || c.OnlyStrings || c.OnlyDeclarations || c.OnlyUsages
}
// ContentFilterCachePrefix returns a cache key prefix for the active content filter.
func (c *Config) ContentFilterCachePrefix() string {
switch {
case c.OnlyCode:
return "[[only-code]]"
case c.OnlyComments:
return "[[only-comments]]"
case c.OnlyStrings:
return "[[only-strings]]"
case c.OnlyDeclarations:
return "[[only-declarations]]"
case c.OnlyUsages:
return "[[only-usages]]"
default:
return ""
}
}
// ResolveContext returns the effective before/after context line counts.
// -C sets both; -B/-A override individually (matching grep semantics).
func (c *Config) ResolveContext() (before, after int) {
before = c.ContextAround
after = c.ContextAround
if c.ContextBefore > 0 {
before = c.ContextBefore
}
if c.ContextAfter > 0 {
after = c.ContextAfter
}
return
}
// ResolveRankingProfile returns a RankingProfile for ranking. When a named
// profile is set (--profile), it is used directly. Otherwise one is built from
// the individual Config fields so existing CLI flags continue to work.
func (c *Config) ResolveRankingProfile() *ranker.RankingProfile {
if c.Profile != "" {
return ranker.ResolveProfileByName(c.Profile)
}
return &ranker.RankingProfile{
K1: 1.2,
B: 0.75,
GravityStrength: c.ResolveGravityStrength(),
NoiseSensitivity: c.ResolveNoiseSensitivity(),
TestPenalty: c.TestPenalty,
}
}
// ResolveNoiseSensitivity maps the NoiseIntent string to a numeric sensitivity value
// used by the signal-to-noise penalty.
func (c *Config) ResolveNoiseSensitivity() float64 {
switch strings.ToLower(c.NoiseIntent) {
case "silence":
return 0.1
case "quiet":
return 0.5
case "default", "":
return 1.0
case "loud":
return 2.0
case "raw":
return 100.0
default:
return 1.0
}
}
// ResolveDefaultOperator maps the DefaultOperator string to the search package
// enum. It returns an error for unrecognised values.
func (c *Config) ResolveDefaultOperator() (search.DefaultOperator, error) {
switch strings.ToLower(strings.TrimSpace(c.DefaultOperator)) {
case "and", "":
return search.DefaultAnd, nil
case "or":
return search.DefaultOr, nil
default:
return search.DefaultAnd, fmt.Errorf("unknown default-operator %q (valid: and, or)", c.DefaultOperator)
}
}
// ResolveGravityStrength maps the GravityIntent string to a numeric strength value.
func (c *Config) ResolveGravityStrength() float64 {
switch strings.ToLower(c.GravityIntent) {
case "brain":
return 2.5
case "logic":
return 1.5
case "default", "":
return 1.0
case "low":
return 0.2
case "off":
return 0.0
default:
return 1.0
}
}