-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchunker.go
More file actions
1026 lines (940 loc) · 25 KB
/
chunker.go
File metadata and controls
1026 lines (940 loc) · 25 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package tok
import (
"path/filepath"
"regexp"
"sort"
"strings"
"sync"
)
// CodeChunk represents a semantically meaningful chunk of source code.
type CodeChunk struct {
Content string `json:"content"`
StartLine int `json:"start_line"`
EndLine int `json:"end_line"`
Symbol string `json:"symbol,omitempty"`
Tokens int `json:"tokens"`
}
// SeparatorKeep controls what happens to boundary separators during splitting.
type SeparatorKeep int
const (
SepLeft SeparatorKeep = iota // separator stays with preceding chunk (default)
SepRight // separator stays with following chunk
SepDiscard // separator is removed from output
)
// ChunkOptions configures the code chunking behavior.
type ChunkOptions struct {
MaxTokens int
MinTokens int
MinChunkSize int // hard minimum; chunks below this get heavy DP penalty (default: DefaultMinChunkSize)
Language string
Overlap int // number of tokens worth of content to repeat from previous chunk
KeepSeparator SeparatorKeep // controls boundary line placement (default: SepLeft)
}
// DefaultMinChunkSize is the default minimum chunk size in tokens.
const DefaultMinChunkSize = 250
// DefaultChunkOptions returns sensible defaults for code chunking.
func DefaultChunkOptions() ChunkOptions {
return ChunkOptions{MaxTokens: 500, MinTokens: 50, MinChunkSize: DefaultMinChunkSize, Language: "", Overlap: 50, KeepSeparator: SepLeft}
}
// Language boundary patterns.
var (
goBoundary = regexp.MustCompile(`^(?:func |type \w+ (?:struct|interface))`)
pyBoundary = regexp.MustCompile(`^(?:def |class |async def )`)
tsBoundary = regexp.MustCompile(`^(?:function |class |export |const \w+ = )`)
rustBoundary = regexp.MustCompile(`^(?:fn |pub fn |struct |impl |trait )`)
javaBoundary = regexp.MustCompile(`(?:public |private |protected ).*(?:class |interface |enum |\w+\s*\()`)
)
// symbolNamePatterns extract the symbol name from the first boundary line.
var (
goSymbolRe = regexp.MustCompile(`^(?:func\s+(?:\([^)]+\)\s+)?(\w+)|type\s+(\w+))`)
pySymbolRe = regexp.MustCompile(`^(?:async\s+def|def|class)\s+(\w+)`)
tsSymbolRe = regexp.MustCompile(`^(?:export\s+)?(?:function|class|const)\s+(\w+)`)
rustSymbolRe = regexp.MustCompile(`^(?:pub\s+)?(?:fn|struct|impl|trait)\s+(\w+)`)
javaSymbolRe = regexp.MustCompile(`(?:class|interface|enum)\s+(\w+)`)
)
// ---- Feature 5: Custom Language Regex Config ----
var (
customLanguagePatterns = map[string][]string{} // lang -> boundary regexes
customLanguagePatternsMu sync.RWMutex
)
// RegisterLanguagePatterns registers custom boundary patterns for a language.
// These override the built-in patterns when looking up boundaries.
func RegisterLanguagePatterns(lang string, patterns []string) {
customLanguagePatternsMu.Lock()
defer customLanguagePatternsMu.Unlock()
customLanguagePatterns[strings.ToLower(lang)] = patterns
}
// GetLanguagePatterns returns custom patterns if registered, else built-in pattern strings.
func GetLanguagePatterns(lang string) []string {
customLanguagePatternsMu.RLock()
defer customLanguagePatternsMu.RUnlock()
lower := strings.ToLower(lang)
if patterns, ok := customLanguagePatterns[lower]; ok {
return patterns
}
// Return built-in pattern string representation
b := builtinBoundaryForLang(lower)
if b != nil {
return []string{b.String()}
}
return nil
}
// ---- Feature 1: Six-Level Separator Hierarchy ----
var separatorLevels = []string{
"\n\n", // Level 1: paragraph breaks
"\n", // Level 2: line breaks
". ", "? ", "! ", "。", "?", "!", // Level 3: sentence endings (incl CJK)
"; ", ": ", "— ", ";", ":", // Level 4: clause breaks
", ", ",", // Level 5: comma breaks
" ", // Level 6: word breaks
}
// separatorsByLevel groups separators into their hierarchical levels.
var separatorsByLevel = [][]string{
{"\n\n"},
{"\n"},
{". ", "? ", "! ", "。", "?", "!"},
{"; ", ": ", "— ", ";", ":"},
{", ", ","},
{" "},
}
// splitBySeparatorLevels splits content using a multi-level separator hierarchy.
// It tries level 0 first, then for any piece that exceeds maxTokens, splits
// with the next level, recursively.
func splitBySeparatorLevels(content string, maxTokens int) []string {
return splitBySepLevel(content, maxTokens, 0)
}
func splitBySepLevel(content string, maxTokens int, level int) []string {
if level >= len(separatorsByLevel) {
// No more levels; return as-is
return []string{content}
}
tokens := EstimateTokensPrecise(content)
if tokens <= maxTokens {
return []string{content}
}
seps := separatorsByLevel[level]
pieces := splitByAnySeparator(content, seps)
if len(pieces) <= 1 {
// This level didn't help; try the next level
return splitBySepLevel(content, maxTokens, level+1)
}
// Greedily merge pieces to stay within maxTokens
var result []string
current := pieces[0]
for i := 1; i < len(pieces); i++ {
combined := current + pieces[i]
if EstimateTokensPrecise(combined) <= maxTokens {
current = combined
} else {
result = append(result, current)
current = pieces[i]
}
}
if current != "" {
result = append(result, current)
}
// For any piece still too large, recurse to next level
var final []string
for _, piece := range result {
if EstimateTokensPrecise(piece) > maxTokens {
final = append(final, splitBySepLevel(piece, maxTokens, level+1)...)
} else {
final = append(final, piece)
}
}
return final
}
// splitByAnySeparator splits content at any of the given separators,
// keeping the separator attached to the left piece.
func splitByAnySeparator(content string, seps []string) []string {
if len(seps) == 0 {
return []string{content}
}
type splitPoint struct {
index int
sep string
}
// Find all split points
var points []splitPoint
for _, sep := range seps {
start := 0
for {
idx := strings.Index(content[start:], sep)
if idx < 0 {
break
}
absIdx := start + idx
points = append(points, splitPoint{absIdx, sep})
start = absIdx + len(sep)
}
}
if len(points) == 0 {
return []string{content}
}
// Sort split points by index
sort.Slice(points, func(i, j int) bool {
return points[i].index < points[j].index
})
// Split content at these points, keeping separator with left piece
var pieces []string
prev := 0
for _, sp := range points {
end := sp.index + len(sp.sep)
if end > len(content) {
end = len(content)
}
if end > prev {
pieces = append(pieces, content[prev:end])
prev = end
}
}
if prev < len(content) {
pieces = append(pieces, content[prev:])
}
return pieces
}
// ---- Feature 4: DP Cost-Optimized Chunk Merging ----
const (
costTooSmall = 1000 // penalty for chunk below MinTokens
costTooLarge = 2000 // penalty for chunk above MaxTokens
costBelowMinChunk = 1000000 // heavy penalty for chunk below MinChunkSize
costMissingOverlap = 512 // penalty for missing overlap
costBoundarySplit = 300 // penalty for splitting mid-syntax
dpMaxAtoms = 500 // fall back to greedy above this
)
func chunkCost(tokens int, opts ChunkOptions) int {
cost := 0
if tokens < opts.MinTokens {
cost += costTooSmall
}
if tokens > opts.MaxTokens {
cost += costTooLarge
}
return cost
}
// dpSegmentCost computes the DP cost for merging atoms[i..j) into one chunk.
func dpSegmentCost(chunks []rawChunk, i, j int, tokenSums []int, opts ChunkOptions) int {
tokens := tokenSums[j] - tokenSums[i]
cost := 0
// Hard constraint: never exceed MaxTokens
if tokens > opts.MaxTokens {
cost += costBelowMinChunk
}
// Penalize chunks below MinTokens (strong — these should be merged)
if tokens < opts.MinTokens {
cost += costTooSmall
}
// Penalize merging across boundaries (preserves semantic structure)
numBoundaries := j - i - 1
if numBoundaries > 0 {
cost += numBoundaries * costBoundarySplit
}
return cost
}
// optimizeMerging uses dynamic programming to find the minimum-cost partition
// of chunks. Falls back to greedy for large inputs.
func optimizeMerging(chunks []rawChunk, opts ChunkOptions) []rawChunk {
if len(chunks) <= 1 {
return chunks
}
if len(chunks) > dpMaxAtoms {
return greedyMerging(chunks, opts)
}
n := len(chunks)
// Precompute prefix sums of token counts
tokenSums := make([]int, n+1)
for i, c := range chunks {
tokenSums[i+1] = tokenSums[i] + EstimateTokensPrecise(c.content)
}
const inf = 1<<31 - 1
// dp[i] = min cost to partition chunks[0..i)
dp := make([]int, n+1)
split := make([]int, n+1) // split[i] = start of last segment ending at i
for i := 1; i <= n; i++ {
dp[i] = inf
}
for i := 1; i <= n; i++ {
for j := 0; j < i; j++ {
segCost := dpSegmentCost(chunks, j, i, tokenSums, opts)
total := dp[j] + segCost
if total < dp[i] {
dp[i] = total
split[i] = j
}
}
}
// Reconstruct segments
var segments [][2]int // [start, end) pairs
pos := n
for pos > 0 {
s := split[pos]
segments = append(segments, [2]int{s, pos})
pos = s
}
// Reverse
for l, r := 0, len(segments)-1; l < r; l, r = l+1, r-1 {
segments[l], segments[r] = segments[r], segments[l]
}
// Build merged chunks
result := make([]rawChunk, 0, len(segments))
for _, seg := range segments {
merged := mergeRawChunks(chunks[seg[0]:seg[1]])
result = append(result, merged)
}
return result
}
// mergeRawChunks combines a slice of rawChunks into one.
func mergeRawChunks(chunks []rawChunk) rawChunk {
if len(chunks) == 1 {
return chunks[0]
}
var parts []string
sym := ""
for _, c := range chunks {
parts = append(parts, c.content)
if sym == "" {
sym = c.symbol
}
}
return rawChunk{
content: strings.Join(parts, "\n"),
startLine: chunks[0].startLine,
endLine: chunks[len(chunks)-1].endLine,
symbol: sym,
}
}
// greedyMerging is the original greedy fallback for large inputs.
func greedyMerging(chunks []rawChunk, opts ChunkOptions) []rawChunk {
if len(chunks) <= 1 {
return chunks
}
result := make([]rawChunk, len(chunks))
copy(result, chunks)
changed := true
for changed {
changed = false
var next []rawChunk
i := 0
for i < len(result) {
if i+1 >= len(result) {
next = append(next, result[i])
i++
continue
}
curTokens := EstimateTokensPrecise(result[i].content)
nextTokens := EstimateTokensPrecise(result[i+1].content)
combined := result[i].content + "\n" + result[i+1].content
combinedTokens := EstimateTokensPrecise(combined)
costSeparate := chunkCost(curTokens, opts) + chunkCost(nextTokens, opts)
costMerged := chunkCost(combinedTokens, opts)
if costMerged < costSeparate && combinedTokens <= opts.MaxTokens {
merged := rawChunk{
content: combined,
startLine: result[i].startLine,
endLine: result[i+1].endLine,
symbol: result[i].symbol,
}
if merged.symbol == "" {
merged.symbol = result[i+1].symbol
}
next = append(next, merged)
i += 2
changed = true
} else {
next = append(next, result[i])
i++
}
}
result = next
}
return result
}
// ---- Pluggable Chunker Registry ----
// ChunkerFunc is a custom chunker that takes a file path and content, returning
// the detected language and code chunks.
type ChunkerFunc func(path, content string) (language string, chunks []CodeChunk)
var (
chunkerRegistry = map[string]ChunkerFunc{}
chunkerRegistryMu sync.RWMutex
)
// RegisterChunker registers a custom chunker for a file extension (e.g. ".go").
func RegisterChunker(ext string, fn ChunkerFunc) {
chunkerRegistryMu.Lock()
defer chunkerRegistryMu.Unlock()
chunkerRegistry[ext] = fn
}
// ChunkCode splits source code into semantically meaningful chunks based on
// language-aware boundary detection (function/class/method definitions).
// If a custom chunker is registered for the file extension in opts.Language,
// it is used instead of the default pipeline.
func ChunkCode(source string, opts ChunkOptions) []CodeChunk {
return ChunkCodePath("", source, opts)
}
// ChunkCodePath is like ChunkCode but accepts a file path for registry lookup.
func ChunkCodePath(path, source string, opts ChunkOptions) []CodeChunk {
if source == "" {
return nil
}
// Check pluggable registry by file extension
if path != "" {
ext := filepath.Ext(path)
chunkerRegistryMu.RLock()
fn, ok := chunkerRegistry[ext]
chunkerRegistryMu.RUnlock()
if ok {
_, chunks := fn(path, source)
return chunks
}
}
if opts.MaxTokens <= 0 {
opts.MaxTokens = 500
}
if opts.MinTokens <= 0 {
opts.MinTokens = 50
}
if opts.MinChunkSize <= 0 {
opts.MinChunkSize = DefaultMinChunkSize
}
lang := opts.Language
if lang == "" {
lang = detectLanguage(source)
}
boundary := boundaryForLang(lang)
symbolRe := symbolReForLang(lang)
lines := strings.Split(source, "\n")
rawChunks := splitAtBoundaries(lines, boundary, opts.KeepSeparator)
// Split oversized chunks: first try blank lines, then separator hierarchy
var splitChunks []rawChunk
for _, rc := range rawChunks {
tokens := EstimateTokensPrecise(rc.content)
if tokens > opts.MaxTokens {
blankSplit := splitAtBlankLines(rc, opts.MaxTokens)
// Check if any piece is still too large; apply separator hierarchy
for _, piece := range blankSplit {
if EstimateTokensPrecise(piece.content) > opts.MaxTokens {
subPieces := splitBySeparatorLevels(piece.content, opts.MaxTokens)
lineOffset := piece.startLine
for _, sp := range subPieces {
lineCount := strings.Count(sp, "\n") + 1
splitChunks = append(splitChunks, rawChunk{
content: sp,
startLine: lineOffset,
endLine: lineOffset + lineCount - 1,
})
lineOffset += lineCount
}
} else {
splitChunks = append(splitChunks, piece)
}
}
} else {
splitChunks = append(splitChunks, rc)
}
}
// Cost-optimized merging (Feature 4) replaces the old greedy merge
merged := optimizeMerging(splitChunks, opts)
// Apply overlap: for each chunk after the first, prepend trailing content
// from the previous chunk worth up to opts.Overlap tokens.
overlap := opts.Overlap
if overlap < 0 {
overlap = 0
}
if overlap > 0 && len(merged) > 1 {
for i := 1; i < len(merged); i++ {
prevLines := strings.Split(merged[i-1].content, "\n")
// Collect lines from the end of previous chunk until we reach overlap tokens
var overlapLines []string
tokenCount := 0
for j := len(prevLines) - 1; j >= 0; j-- {
lineTokens := EstimateTokensPrecise(prevLines[j])
if tokenCount+lineTokens > overlap && len(overlapLines) > 0 {
break
}
overlapLines = append([]string{prevLines[j]}, overlapLines...)
tokenCount += lineTokens
}
if len(overlapLines) > 0 {
merged[i].content = strings.Join(overlapLines, "\n") + "\n" + merged[i].content
}
}
}
// Build final CodeChunks with token counts and symbol extraction
var result []CodeChunk
for _, rc := range merged {
tokens := EstimateTokensPrecise(rc.content)
symbol := rc.symbol
if symbol == "" && symbolRe != nil {
symbol = extractSymbol(rc.content, symbolRe)
}
result = append(result, CodeChunk{
Content: rc.content,
StartLine: rc.startLine,
EndLine: rc.endLine,
Symbol: symbol,
Tokens: tokens,
})
}
return result
}
type rawChunk struct {
content string
startLine int
endLine int
symbol string
}
func splitAtBoundaries(lines []string, boundary *regexp.Regexp, keepSep SeparatorKeep) []rawChunk {
if boundary == nil {
// No boundary pattern: treat the entire source as one chunk.
content := strings.Join(lines, "\n")
return []rawChunk{{content: content, startLine: 1, endLine: len(lines)}}
}
var chunks []rawChunk
var currentLines []string
startLine := 1
for i, line := range lines {
trimmed := strings.TrimSpace(line)
if boundary.MatchString(trimmed) && len(currentLines) > 0 {
switch keepSep {
case SepDiscard:
// Flush accumulated lines without the boundary line
chunks = append(chunks, rawChunk{
content: strings.Join(currentLines, "\n"),
startLine: startLine,
endLine: i,
})
currentLines = nil
startLine = i + 2 // skip the boundary line
// Do not add the boundary line to currentLines
continue
case SepRight:
// Flush accumulated lines; boundary goes to next chunk
chunks = append(chunks, rawChunk{
content: strings.Join(currentLines, "\n"),
startLine: startLine,
endLine: i,
})
currentLines = nil
startLine = i + 1
// Fall through to add boundary line to next chunk
default: // SepLeft
// Default behavior: flush, boundary starts the next chunk
chunks = append(chunks, rawChunk{
content: strings.Join(currentLines, "\n"),
startLine: startLine,
endLine: i,
})
currentLines = nil
startLine = i + 1
}
}
currentLines = append(currentLines, line)
}
// Flush remaining
if len(currentLines) > 0 {
chunks = append(chunks, rawChunk{
content: strings.Join(currentLines, "\n"),
startLine: startLine,
endLine: len(lines),
})
}
return chunks
}
func splitAtBlankLines(rc rawChunk, maxTokens int) []rawChunk {
lines := strings.Split(rc.content, "\n")
var result []rawChunk
var currentLines []string
startLine := rc.startLine
for i, line := range lines {
currentLines = append(currentLines, line)
content := strings.Join(currentLines, "\n")
tokens := EstimateTokensPrecise(content)
if tokens > maxTokens && len(currentLines) > 1 {
// Try to find a blank line to split at
splitIdx := -1
for j := len(currentLines) - 2; j >= 0; j-- {
if strings.TrimSpace(currentLines[j]) == "" {
splitIdx = j
break
}
}
if splitIdx >= 0 {
before := strings.Join(currentLines[:splitIdx], "\n")
result = append(result, rawChunk{
content: before,
startLine: startLine,
endLine: startLine + splitIdx - 1,
})
currentLines = currentLines[splitIdx:]
startLine = startLine + splitIdx
} else {
// No blank line found; force split at current position minus 1
before := strings.Join(currentLines[:len(currentLines)-1], "\n")
result = append(result, rawChunk{
content: before,
startLine: startLine,
endLine: rc.startLine + i - 1,
})
currentLines = []string{line}
startLine = rc.startLine + i
}
}
}
if len(currentLines) > 0 {
result = append(result, rawChunk{
content: strings.Join(currentLines, "\n"),
startLine: startLine,
endLine: rc.endLine,
})
}
return result
}
func extractSymbol(content string, re *regexp.Regexp) string {
// Look at the first few lines for a symbol
lines := strings.SplitN(content, "\n", 5)
for _, line := range lines {
trimmed := strings.TrimSpace(line)
if m := re.FindStringSubmatch(trimmed); m != nil {
// Return first non-empty submatch
for _, g := range m[1:] {
if g != "" {
return g
}
}
}
}
return ""
}
func detectLanguage(source string) string {
lines := strings.SplitN(source, "\n", 20)
for _, line := range lines {
trimmed := strings.TrimSpace(line)
if strings.HasPrefix(trimmed, "package ") {
return "go"
}
if strings.HasPrefix(trimmed, "import ") && strings.Contains(source, "def ") {
return "python"
}
if strings.HasPrefix(trimmed, "def ") || strings.HasPrefix(trimmed, "class ") && strings.Contains(trimmed, ":") {
return "python"
}
if strings.HasPrefix(trimmed, "fn ") || strings.HasPrefix(trimmed, "pub fn ") {
return "rust"
}
if strings.Contains(trimmed, "function ") || strings.Contains(trimmed, "import {") {
return "typescript"
}
}
return ""
}
// builtinBoundaryForLang returns the built-in boundary regex for a language,
// ignoring custom patterns.
func builtinBoundaryForLang(lang string) *regexp.Regexp {
switch strings.ToLower(lang) {
case "go":
return goBoundary
case "python", "py":
return pyBoundary
case "typescript", "ts", "javascript", "js", "tsx", "jsx":
return tsBoundary
case "rust", "rs":
return rustBoundary
case "java":
return javaBoundary
default:
return nil
}
}
// boundaryForLang returns the boundary regex for a language, checking custom
// patterns first before falling back to built-in patterns.
func boundaryForLang(lang string) *regexp.Regexp {
customLanguagePatternsMu.RLock()
patterns, ok := customLanguagePatterns[strings.ToLower(lang)]
customLanguagePatternsMu.RUnlock()
if ok && len(patterns) > 0 {
// Combine custom patterns into a single alternation regex
combined := strings.Join(patterns, "|")
re, err := regexp.Compile(combined)
if err == nil {
return re
}
}
return builtinBoundaryForLang(lang)
}
func symbolReForLang(lang string) *regexp.Regexp {
switch strings.ToLower(lang) {
case "go":
return goSymbolRe
case "python", "py":
return pySymbolRe
case "typescript", "ts", "javascript", "js", "tsx", "jsx":
return tsSymbolRe
case "rust", "rs":
return rustSymbolRe
case "java":
return javaSymbolRe
default:
return nil
}
}
// extensionLanguageMap maps file extensions to language names.
var extensionLanguageMap = map[string]string{
// Go
".go": "go",
// Python
".py": "python",
".pyw": "python",
// TypeScript / JavaScript
".ts": "typescript",
".tsx": "tsx",
".js": "javascript",
".jsx": "jsx",
// Rust
".rs": "rust",
// Java
".java": "java",
// Ruby
".rb": "ruby",
// PHP
".php": "php",
// C
".c": "c",
".h": "c",
// C++
".cpp": "cpp",
".cc": "cpp",
".cxx": "cpp",
".hpp": "cpp",
// C#
".cs": "csharp",
// Kotlin
".kt": "kotlin",
".kts": "kotlin",
// Swift
".swift": "swift",
// Dart
".dart": "dart",
// Shell
".sh": "bash",
".bash": "bash",
// SQL
".sql": "sql",
// HTML
".html": "html",
".htm": "html",
// CSS
".css": "css",
// Vue
".vue": "vue",
// Zig
".zig": "zig",
// Elixir
".ex": "elixir",
".exs": "elixir",
// Scala
".scala": "scala",
// Lua
".lua": "lua",
// R
".r": "r",
".R": "r",
// Markdown
".md": "markdown",
// Objective-C / Objective-C++
".m": "objective-c",
".mm": "objective-cpp",
// Perl
".pl": "perl",
".pm": "perl",
// Haskell
".hs": "haskell",
".lhs": "haskell",
// Erlang
".erl": "erlang",
".hrl": "erlang",
// Clojure
".clj": "clojure",
".cljs": "clojure",
// F#
".fs": "fsharp",
".fsx": "fsharp",
// OCaml
".ml": "ocaml",
".mli": "ocaml",
// Julia
".jl": "julia",
// Nim
".nim": "nim",
// Crystal
".cr": "crystal",
// V
".v": "v",
// VHDL
".vhdl": "vhdl",
// Solidity
".sol": "solidity",
// Protobuf
".proto": "protobuf",
// GraphQL
".graphql": "graphql",
".gql": "graphql",
// Terraform / HCL
".tf": "terraform",
".hcl": "hcl",
// CMake
".cmake": "cmake",
// Gradle
".gradle": "gradle",
// Groovy
".groovy": "groovy",
// PowerShell
".ps1": "powershell",
".psm1": "powershell",
// Batch
".bat": "batch",
".cmd": "batch",
// Fish
".fish": "fish",
// Vim
".vim": "vim",
// Emacs Lisp
".el": "elisp",
// Racket
".rkt": "racket",
// Pascal
".pas": "pascal",
// D
".d": "d",
// Ada
".ada": "ada",
".adb": "ada",
// Fortran
".f90": "fortran",
".f95": "fortran",
// COBOL
".cob": "cobol",
// Svelte
".svelte": "svelte",
// Astro
".astro": "astro",
// Prisma
".prisma": "prisma",
// dotenv
".env": "dotenv",
// INI / Config
".ini": "ini",
".cfg": "ini",
// Config files
".conf": "conf",
// Nginx
".nginx": "nginx",
// Dockerfile
".dockerfile": "dockerfile",
".containerfile": "dockerfile",
// Nix
".nix": "nix",
// Dhall
".dhall": "dhall",
// Jsonnet
".jsonnet": "jsonnet",
// Starlark
".starlark": "starlark",
".bzl": "starlark",
// WebAssembly
".wasm": "wasm",
".wat": "wat",
// YAML
".yaml": "yaml",
".yml": "yaml",
// JSON
".json": "json",
// TOML
".toml": "toml",
// XML
".xml": "xml",
// SCSS / SASS / LESS
".scss": "scss",
".sass": "sass",
".less": "less",
// CoffeeScript
".coffee": "coffeescript",
// Elm
".elm": "elm",
// PureScript
".purs": "purescript",
// Assembly
".asm": "assembly",
".s": "assembly",
// Makefile
".mk": "makefile",
// Diff / Patch
".diff": "diff",
".patch": "diff",
// LaTeX
".tex": "latex",
// reStructuredText
".rst": "rst",
// Org-mode
".org": "org",
// CSV / TSV
".csv": "csv",
".tsv": "tsv",
// Dockerfile special
".Dockerfile": "dockerfile",
// Scheme
".scm": "scheme",
// Common Lisp
".lisp": "commonlisp",
".cl": "commonlisp",
// Prolog
".pro": "prolog",
// Tcl
".tcl": "tcl",
// R Markdown
".rmd": "rmarkdown",
// Jupyter
".ipynb": "jupyter",
// Terraform vars
".tfvars": "terraform",
// Protocol Buffers
".pb": "protobuf",
// Thrift
".thrift": "thrift",
// Avro
".avsc": "avro",
// GLSL / HLSL
".glsl": "glsl",
".hlsl": "hlsl",
// Cuda
".cu": "cuda",
".cuh": "cuda",
// CMakeLists
".cmake.in": "cmake",
// Vagrant
".vagrantfile": "ruby",
// Puppet
".pp": "puppet",