summaryrefslogtreecommitdiff
path: root/internal/showcase/code_extractor.go
blob: fbf17f66f5c45cef7d13d22d3ef1e7025581fead (plain)
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
package showcase

import (
	"bufio"
	"fmt"
	"math/rand"
	"os"
	"path/filepath"
	"strings"
	"time"
)

func init() {
	rand.Seed(time.Now().UnixNano())
}

// extractCodeSnippet extracts a random code snippet from the repository
func extractCodeSnippet(repoPath string, languages []LanguageStats) (string, string, error) {
	if len(languages) == 0 {
		return "", "", fmt.Errorf("no programming languages found")
	}

	// Get the primary language (highest percentage)
	primaryLang := languages[0].Name

	// Define file extensions for each language
	langExtensions := map[string][]string{
		"Go":         {".go"},
		"Python":     {".py"},
		"JavaScript": {".js"},
		"TypeScript": {".ts"},
		"Java":       {".java"},
		"C":          {".c", ".h"},
		"C++":        {".cpp", ".cc", ".cxx", ".hpp"},
		"C/C++":      {".h"},
		"C#":         {".cs"},
		"Ruby":       {".rb"},
		"PHP":        {".php"},
		"Swift":      {".swift"},
		"Kotlin":     {".kt"},
		"Rust":       {".rs"},
		"Shell":      {".sh", ".bash"},
		"Perl":       {".pl", ".pm"},
		"Raku":       {".raku", ".rakumod", ".p6", ".pm6"},
		"Haskell":    {".hs"},
		"Lua":        {".lua"},
		"HTML":       {".html", ".htm"},
		"CSS":        {".css"},
		"SQL":        {".sql"},
		"Make":       {"Makefile", "makefile", "GNUmakefile"},
		"HCL":        {".tf", ".tfvars", ".hcl"},
		"AWK":        {".awk", ".cgi"}, // .cgi files can be AWK scripts
	}

	// Get file extensions for the primary language
	extensions, ok := langExtensions[primaryLang]
	if !ok {
		// Try other languages if primary doesn't have extensions defined
		for _, lang := range languages {
			if exts, exists := langExtensions[lang.Name]; exists {
				extensions = exts
				primaryLang = lang.Name
				break
			}
		}
		if len(extensions) == 0 {
			return "", "", fmt.Errorf("no known file extensions for languages")
		}
	}

	// Find all files matching the extensions
	var codeFiles []string
	err := filepath.Walk(repoPath, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return nil
		}

		// Skip directories
		if info.IsDir() {
			name := info.Name()
			// Skip hidden directories and common non-code directories
			if strings.HasPrefix(name, ".") && name != "." ||
				name == "node_modules" ||
				name == "vendor" ||
				name == "target" ||
				name == "dist" ||
				name == "build" ||
				name == "__pycache__" {
				return filepath.SkipDir
			}
			return nil
		}

		// Skip files that are too large
		if info.Size() > 1*1024*1024 { // 1MB
			return nil
		}

		// Check if file matches extensions
		basename := filepath.Base(path)
		ext := filepath.Ext(path)

		matched := false
		for _, validExt := range extensions {
			if validExt == basename || (strings.HasPrefix(validExt, ".") && ext == validExt) {
				matched = true
				break
			}
		}

		// For executable files, also check shebang if primary language is AWK and file has .cgi extension
		if !matched && primaryLang == "AWK" && ext == ".cgi" && info.Mode()&0111 != 0 {
			if file, err := os.Open(path); err == nil {
				scanner := bufio.NewScanner(file)
				if scanner.Scan() {
					firstLine := scanner.Text()
					if strings.Contains(firstLine, "awk") || strings.Contains(firstLine, "gawk") {
						matched = true
					}
				}
				file.Close()
			}
		}

		if matched {
			// Skip test files and generated files
			if !strings.Contains(basename, "_test") &&
				!strings.Contains(basename, ".test.") &&
				!strings.Contains(basename, ".min.") &&
				!strings.Contains(path, "/test/") &&
				!strings.Contains(path, "/tests/") {
				codeFiles = append(codeFiles, path)
			}
		}

		return nil
	})

	if err != nil {
		return "", "", err
	}

	if len(codeFiles) == 0 {
		return "", "", fmt.Errorf("no code files found")
	}

	// Try multiple files to find one with good line lengths
	rand.Shuffle(len(codeFiles), func(i, j int) {
		codeFiles[i], codeFiles[j] = codeFiles[j], codeFiles[i]
	})

	var snippet string
	var selectedFile string

	// Try up to 5 files to find a good snippet
	for i := 0; i < len(codeFiles) && i < 5; i++ {
		candidateFile := codeFiles[i]
		candidateSnippet, err := extractSnippetFromFile(candidateFile, 10, 15)
		if err != nil {
			continue
		}

		// Check if this snippet has acceptable line lengths
		if hasAcceptableLineLength(candidateSnippet, 80) {
			snippet = candidateSnippet
			selectedFile = candidateFile
			break
		}

		// Keep the first valid snippet as fallback
		if snippet == "" {
			snippet = candidateSnippet
			selectedFile = candidateFile
		}
	}

	if snippet == "" {
		return "", "", fmt.Errorf("no valid code snippets found")
	}

	// Get relative path for display
	relPath, _ := filepath.Rel(repoPath, selectedFile)

	return snippet, fmt.Sprintf("%s from `%s`", primaryLang, relPath), nil
}

// extractSnippetFromFile extracts a code snippet from a file
func extractSnippetFromFile(filePath string, minLines, maxLines int) (string, error) {
	file, err := os.Open(filePath)
	if err != nil {
		return "", err
	}
	defer file.Close()

	// Read all lines
	var lines []string
	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		lines = append(lines, scanner.Text())
	}

	if err := scanner.Err(); err != nil {
		return "", err
	}

	totalLines := len(lines)
	if totalLines == 0 {
		return "", fmt.Errorf("file is empty")
	}

	// Try to find the smallest complete function
	bestFunction := findSmallestCompleteFunction(lines)
	if bestFunction != "" {
		return stripComments(bestFunction), nil
	}

	// If no complete function found, try to find a complete function/method
	functionStart, functionEnd := findCompleteFunctionOrMethod(lines, minLines, maxLines*2) // Allow larger functions
	if functionStart >= 0 && functionEnd >= 0 {
		snippet := strings.Join(lines[functionStart:functionEnd+1], "\n")
		return stripComments(snippet), nil
	}

	// Fallback to finding an interesting start with at least minLines
	interestingStart := findInterestingStart(lines, minLines)
	if interestingStart >= 0 {
		endLine := interestingStart + minLines
		if endLine > totalLines {
			endLine = totalLines
		}
		snippet := strings.Join(lines[interestingStart:endLine], "\n")
		return stripComments(snippet), nil
	}

	// Last resort: return first minLines (skip imports if possible)
	skipLines := 0
	for i, line := range lines {
		trimmed := strings.TrimSpace(line)
		if trimmed != "" && !strings.HasPrefix(trimmed, "import") &&
			!strings.HasPrefix(trimmed, "package") && !strings.HasPrefix(trimmed, "using") &&
			!strings.HasPrefix(trimmed, "#include") && !strings.HasPrefix(trimmed, "from") {
			skipLines = i
			break
		}
	}

	endLine := skipLines + minLines
	if endLine > totalLines {
		endLine = totalLines
	}

	snippet := strings.Join(lines[skipLines:endLine], "\n")
	return stripComments(snippet), nil
}

// findSmallestCompleteFunction finds the smallest complete function in the file
func findSmallestCompleteFunction(lines []string) string {
	type functionInfo struct {
		start int
		end   int
		size  int
	}

	var functions []functionInfo

	// Keywords that typically start functions/methods
	functionKeywords := []string{
		"func ", "function ", "def ", "public ", "private ", "protected ",
		"static ", "async ", "procedure ", "sub ", "method ",
	}

	// Find all complete functions
	for i := 0; i < len(lines); i++ {
		line := strings.TrimSpace(lines[i])

		// Check if this line starts a function
		isFunction := false
		for _, keyword := range functionKeywords {
			if strings.Contains(line, keyword) && !strings.HasPrefix(line, "//") && !strings.HasPrefix(line, "#") {
				isFunction = true
				break
			}
		}

		if !isFunction {
			continue
		}

		// Try to find the end of this function
		functionEnd := findFunctionEnd(lines, i)
		if functionEnd > i {
			size := functionEnd - i + 1
			// Only consider functions between 5 and 50 lines
			if size >= 5 && size <= 50 {
				functions = append(functions, functionInfo{
					start: i,
					end:   functionEnd,
					size:  size,
				})
			}
		}
	}

	// Find the smallest function with acceptable line lengths
	if len(functions) > 0 {
		// First try to find a function with all lines <= 80 chars
		for _, f := range functions {
			snippet := strings.Join(lines[f.start:f.end+1], "\n")
			if hasAcceptableLineLength(snippet, 80) {
				return snippet
			}
		}

		// If none found, return the smallest function (will be broken later)
		smallest := functions[0]
		for _, f := range functions[1:] {
			if f.size < smallest.size {
				smallest = f
			}
		}
		return strings.Join(lines[smallest.start:smallest.end+1], "\n")
	}

	return ""
}

// findFunctionEnd finds the end of a function starting at the given line
func findFunctionEnd(lines []string, start int) int {
	if start >= len(lines) {
		return -1
	}

	// For brace-based languages
	braceCount := 0
	inFunction := false

	// For Python - track initial indentation
	isPython := strings.Contains(lines[start], "def ") || strings.Contains(lines[start], "class ")
	var initialIndent int
	if isPython && start < len(lines)-1 {
		// Get indentation of first line after def
		for i := start + 1; i < len(lines); i++ {
			if strings.TrimSpace(lines[i]) != "" {
				initialIndent = len(lines[i]) - len(strings.TrimLeft(lines[i], " \t"))
				break
			}
		}
	}

	for i := start; i < len(lines); i++ {
		line := lines[i]
		trimmed := strings.TrimSpace(line)

		// Handle Python indentation
		if isPython && i > start {
			if trimmed == "" {
				continue
			}
			currentIndent := len(line) - len(strings.TrimLeft(line, " \t"))
			if currentIndent < initialIndent {
				return i - 1
			}
		}

		// Handle brace-based languages
		for _, ch := range line {
			if ch == '{' {
				braceCount++
				inFunction = true
			} else if ch == '}' {
				braceCount--
				if braceCount == 0 && inFunction {
					return i
				}
			}
		}
	}

	// If we're in Python and reached the end, return the last line
	if isPython {
		return len(lines) - 1
	}

	return -1
}

// findCompleteFunctionOrMethod finds a complete function or method within size constraints
func findCompleteFunctionOrMethod(lines []string, minLines, maxLines int) (int, int) {
	// Keywords that typically start functions/methods
	functionKeywords := []string{
		"func ", "function ", "def ", "public ", "private ", "protected ",
		"static ", "async ", "procedure ", "sub ", "method ",
	}

	// Try to find a function that fits within our size constraints
	for i := 0; i < len(lines); i++ {
		line := strings.TrimSpace(lines[i])

		// Check if this line starts a function
		isFunction := false
		for _, keyword := range functionKeywords {
			if strings.Contains(line, keyword) && !strings.HasPrefix(line, "//") && !strings.HasPrefix(line, "#") {
				isFunction = true
				break
			}
		}

		if !isFunction {
			continue
		}

		// Try to find the end of this function
		functionEnd := findFunctionEnd(lines, i)
		if functionEnd > i {
			functionLength := functionEnd - i + 1
			if functionLength >= minLines && functionLength <= maxLines {
				return i, functionEnd
			}
		}
	}

	return -1, -1
}

// findInterestingStart tries to find a good starting point for the snippet
func findInterestingStart(lines []string, snippetSize int) int {
	// Look for function/class definitions
	keywords := []string{
		"func ", "function ", "def ", "class ", "public class",
		"interface ", "struct ", "type ", "const ", "var ",
		"procedure ", "sub ", "method ",
	}

	for i := 0; i < len(lines)-snippetSize; i++ {
		line := strings.TrimSpace(lines[i])
		// Skip empty lines and comments
		if line == "" || strings.HasPrefix(line, "//") || strings.HasPrefix(line, "#") ||
			strings.HasPrefix(line, "/*") || strings.HasPrefix(line, "*") {
			continue
		}

		// Check for interesting keywords
		for _, keyword := range keywords {
			if strings.Contains(line, keyword) {
				// Found something interesting, start here
				return i
			}
		}
	}

	// No interesting start found
	return -1
}

// stripComments removes comment lines from code snippets to make them more concise
func stripComments(code string) string {
	lines := strings.Split(code, "\n")
	var result []string
	inMultilineComment := false

	for _, line := range lines {
		trimmed := strings.TrimSpace(line)

		// Handle multi-line comments for C-style languages
		if strings.Contains(line, "/*") {
			inMultilineComment = true
			// If comment ends on same line, process the rest
			if strings.Contains(line, "*/") {
				inMultilineComment = false
				// Skip this line entirely if it's just a comment
				if strings.TrimSpace(strings.Split(line, "/*")[0]) == "" {
					continue
				}
			} else {
				continue
			}
		}

		if inMultilineComment {
			if strings.Contains(line, "*/") {
				inMultilineComment = false
			}
			continue
		}

		// Skip single-line comments
		if trimmed == "" {
			// Keep empty lines for readability
			result = append(result, line)
		} else if strings.HasPrefix(trimmed, "//") ||
			strings.HasPrefix(trimmed, "#") && !strings.HasPrefix(trimmed, "#include") && !strings.HasPrefix(trimmed, "#define") ||
			strings.HasPrefix(trimmed, "<!--") ||
			strings.HasPrefix(trimmed, "*") && len(trimmed) > 1 && trimmed[1] == ' ' {
			// Skip comment lines
			continue
		} else if strings.HasPrefix(trimmed, "\"\"\"") || strings.HasPrefix(trimmed, "'''") {
			// Skip Python docstrings (simplified - doesn't handle all cases)
			continue
		} else {
			// Keep the line but remove inline comments for some languages
			// This is a simple approach - doesn't handle all edge cases
			if idx := strings.Index(line, " //"); idx > 0 {
				// Check if it's not inside a string (very basic check)
				beforeComment := line[:idx]
				if strings.Count(beforeComment, "\"")%2 == 0 && strings.Count(beforeComment, "'")%2 == 0 {
					line = strings.TrimRight(line[:idx], " \t")
				}
			}
			result = append(result, line)
		}
	}

	// Remove leading and trailing empty lines
	for len(result) > 0 && strings.TrimSpace(result[0]) == "" {
		result = result[1:]
	}
	for len(result) > 0 && strings.TrimSpace(result[len(result)-1]) == "" {
		result = result[:len(result)-1]
	}

	// Remove unnecessary indentation
	result = removeCommonIndentation(result)

	// Break long lines
	result = breakLongLines(result, 80)

	return strings.Join(result, "\n")
}

// removeCommonIndentation removes common leading whitespace from all lines
func removeCommonIndentation(lines []string) []string {
	if len(lines) == 0 {
		return lines
	}

	// Find the common prefix of whitespace
	var commonPrefix string
	firstNonEmpty := -1

	// Find first non-empty line to use as reference
	for i, line := range lines {
		if strings.TrimSpace(line) != "" {
			firstNonEmpty = i
			break
		}
	}

	if firstNonEmpty == -1 {
		return lines
	}

	// Get the whitespace prefix of the first non-empty line
	firstLine := lines[firstNonEmpty]
	for i, ch := range firstLine {
		if ch != ' ' && ch != '\t' {
			commonPrefix = firstLine[:i]
			break
		}
	}

	// If the first line has no indentation, return as-is
	if commonPrefix == "" {
		return lines
	}

	// Find the actual common prefix among all non-empty lines
	for _, line := range lines {
		if strings.TrimSpace(line) == "" {
			continue
		}

		// Reduce commonPrefix to what this line shares
		for i := 0; i < len(commonPrefix); i++ {
			if i >= len(line) || line[i] != commonPrefix[i] {
				commonPrefix = commonPrefix[:i]
				break
			}
		}

		if commonPrefix == "" {
			break
		}
	}

	// If no common prefix found, return as-is
	if commonPrefix == "" {
		return lines
	}

	// Remove common prefix from all lines
	result := make([]string, len(lines))
	prefixLen := len(commonPrefix)
	for i, line := range lines {
		if strings.TrimSpace(line) == "" {
			result[i] = ""
		} else if strings.HasPrefix(line, commonPrefix) {
			result[i] = line[prefixLen:]
		} else {
			result[i] = line
		}
	}

	return result
}

// hasAcceptableLineLength checks if all lines in the snippet are within maxLength
func hasAcceptableLineLength(snippet string, maxLength int) bool {
	lines := strings.Split(snippet, "\n")
	for _, line := range lines {
		if len(line) > maxLength {
			return false
		}
	}
	return true
}

// breakLongLines breaks lines that exceed maxLength at appropriate points
func breakLongLines(lines []string, maxLength int) []string {
	var result []string

	for _, line := range lines {
		if len(line) <= maxLength {
			result = append(result, line)
			continue
		}

		// Try to break the line intelligently
		broken := breakLine(line, maxLength)
		result = append(result, broken...)
	}

	return result
}

// breakLine breaks a single line at appropriate points
func breakLine(line string, maxLength int) []string {
	// If the line is short enough, return as-is
	if len(line) <= maxLength {
		return []string{line}
	}

	// Get the indentation of the original line
	indent := ""
	for _, ch := range line {
		if ch == ' ' || ch == '\t' {
			indent += string(ch)
		} else {
			break
		}
	}

	// Common break points in order of preference
	breakPoints := []string{
		", ",   // After comma
		" && ", // Before logical operators
		" || ",
		" + ", // Before arithmetic operators
		" - ",
		" * ",
		" / ",
		" = ", // Before assignment
		" := ",
		" == ", // Before comparison
		" != ",
		" < ",
		" > ",
		" <= ",
		" >= ",
		"(", // After opening parenthesis
		" ", // Any space
	}

	var result []string
	remaining := line
	isFirstLine := true

	for len(remaining) > maxLength {
		// Find the best break point
		bestBreak := -1

		for _, breakPoint := range breakPoints {
			// Look for break point before maxLength
			searchIn := remaining
			if len(searchIn) > maxLength {
				searchIn = remaining[:maxLength]
			}

			idx := strings.LastIndex(searchIn, breakPoint)
			if idx > 0 && idx < maxLength {
				// For some break points, we want to break after them
				if breakPoint == ", " || breakPoint == "(" {
					idx += len(breakPoint)
				}
				if idx > bestBreak {
					bestBreak = idx
				}
			}
		}

		// If no good break point found, break at maxLength
		if bestBreak == -1 {
			bestBreak = maxLength
		}

		// Add the line
		lineToAdd := remaining[:bestBreak]
		if !isFirstLine && !strings.HasPrefix(strings.TrimSpace(lineToAdd), "//") {
			// Add extra indentation for continuation
			lineToAdd = indent + "  " + strings.TrimLeft(lineToAdd, " \t")
		}
		result = append(result, strings.TrimRight(lineToAdd, " "))

		// Update remaining
		remaining = remaining[bestBreak:]
		if !isFirstLine && !strings.HasPrefix(strings.TrimSpace(remaining), "//") {
			remaining = strings.TrimLeft(remaining, " ")
		}
		isFirstLine = false
	}

	// Add the last part
	if len(remaining) > 0 {
		if !isFirstLine && !strings.HasPrefix(strings.TrimSpace(remaining), "//") {
			remaining = indent + "  " + strings.TrimLeft(remaining, " \t")
		}
		result = append(result, remaining)
	}

	return result
}