createDiagnosticFromError creates a diagnostic from an error
(content string, err error, defaultLine int)
| 436 | |
| 437 | // createDiagnosticFromError creates a diagnostic from an error |
| 438 | func (h *Handler) createDiagnosticFromError(content string, err error, defaultLine int) Diagnostic { |
| 439 | var line, char int |
| 440 | var code interface{} |
| 441 | errMsg := err.Error() |
| 442 | |
| 443 | // Extract position from recovery ParseError if available |
| 444 | if pe, ok := err.(*parser.ParseError); ok { |
| 445 | line = pe.Line - 1 |
| 446 | if line < 0 { |
| 447 | line = 0 |
| 448 | } |
| 449 | char = pe.Column - 1 |
| 450 | if char < 0 { |
| 451 | char = 0 |
| 452 | } |
| 453 | errMsg = pe.Msg |
| 454 | } else if e, ok := err.(*errors.Error); ok { |
| 455 | // Use position from structured error (convert to 0-based) |
| 456 | line = e.Location.Line - 1 |
| 457 | if line < 0 { |
| 458 | line = 0 |
| 459 | } |
| 460 | char = e.Location.Column - 1 |
| 461 | if char < 0 { |
| 462 | char = 0 |
| 463 | } |
| 464 | // Extract error code |
| 465 | code = string(e.Code) |
| 466 | // Use the cleaner message without context |
| 467 | errMsg = e.Message |
| 468 | } else { |
| 469 | // Fallback to regex extraction for non-structured errors |
| 470 | line, char = extractPositionFromError(errMsg, content, defaultLine) |
| 471 | } |
| 472 | |
| 473 | // Calculate end position (end of line or reasonable span) |
| 474 | lines := strings.Split(content, "\n") |
| 475 | endChar := char + 1 |
| 476 | if line < len(lines) { |
| 477 | // Extend to end of word or reasonable span |
| 478 | lineContent := lines[line] |
| 479 | if char < len(lineContent) { |
| 480 | // Find end of current word/token |
| 481 | end := char |
| 482 | for end < len(lineContent) && !isWhitespace(lineContent[end]) { |
| 483 | end++ |
| 484 | } |
| 485 | if end > char { |
| 486 | endChar = end |
| 487 | } else { |
| 488 | endChar = len(lineContent) |
| 489 | } |
| 490 | } else { |
| 491 | endChar = len(lineContent) |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | return Diagnostic{ |