FormatError formats a CompilerError with Rust-like rendering
(err CompilerError)
| 43 | |
| 44 | // FormatError formats a CompilerError with Rust-like rendering |
| 45 | func FormatError(err CompilerError) string { |
| 46 | consoleLog.Printf("Formatting error: type=%s, file=%s, line=%d", err.Type, err.Position.File, err.Position.Line) |
| 47 | var output strings.Builder |
| 48 | |
| 49 | // Get style based on error type |
| 50 | var typeStyle lipgloss.Style |
| 51 | var prefix string |
| 52 | switch err.Type { |
| 53 | case "warning": |
| 54 | typeStyle = styles.Warning |
| 55 | prefix = "warning" |
| 56 | case "info": |
| 57 | typeStyle = styles.Info |
| 58 | prefix = "info" |
| 59 | default: |
| 60 | typeStyle = styles.Error |
| 61 | prefix = "error" |
| 62 | } |
| 63 | |
| 64 | // IDE-parseable format: file:line:column: type: message |
| 65 | // Only include line:column when a meaningful position is known (line > 0) |
| 66 | if err.Position.File != "" { |
| 67 | relativePath := ToRelativePath(err.Position.File) |
| 68 | var location string |
| 69 | if err.Position.Line > 0 { |
| 70 | location = fmt.Sprintf("%s:%d:%d:", |
| 71 | relativePath, |
| 72 | err.Position.Line, |
| 73 | err.Position.Column) |
| 74 | } else { |
| 75 | location = relativePath + ":" |
| 76 | } |
| 77 | output.WriteString(applyStyle(styles.FilePath, location)) |
| 78 | output.WriteString(" ") |
| 79 | } |
| 80 | |
| 81 | // Error type and message |
| 82 | output.WriteString(applyStyle(typeStyle, prefix+":")) |
| 83 | output.WriteString(" ") |
| 84 | output.WriteString(err.Message) |
| 85 | output.WriteString("\n") |
| 86 | |
| 87 | // Context lines (Rust-like error rendering) |
| 88 | if len(err.Context) > 0 && err.Position.Line > 0 { |
| 89 | output.WriteString(renderContext(err)) |
| 90 | } |
| 91 | |
| 92 | // Hint for fixing the error |
| 93 | // Note: we intentionally use styles.Info (cyan) for hints since there is no |
| 94 | // dedicated Hint style; Info is visually distinct and non-alarming, which is |
| 95 | // appropriate for actionable guidance. |
| 96 | if err.Hint != "" { |
| 97 | output.WriteString(applyStyle(styles.Info, "hint: ")) |
| 98 | output.WriteString(err.Hint) |
| 99 | output.WriteString("\n") |
| 100 | } |
| 101 | |
| 102 | return output.String() |