gitstack

grindlemire/go-tui code browser

3.0 KB Go 114 lines 2026-06-03 · d15bb9f raw
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
package provider

import (
	"strings"

	"github.com/grindlemire/go-tui/internal/lsp/log"
)

// DiagnosticSeverity represents the severity of a diagnostic.
type DiagnosticSeverity int

const (
	DiagnosticSeverityError       DiagnosticSeverity = 1
	DiagnosticSeverityWarning     DiagnosticSeverity = 2
	DiagnosticSeverityInformation DiagnosticSeverity = 3
	DiagnosticSeverityHint        DiagnosticSeverity = 4
)

// Diagnostic represents a diagnostic, such as a compiler error or warning.
type Diagnostic struct {
	Range    Range              `json:"range"`
	Severity DiagnosticSeverity `json:"severity,omitempty"`
	Code     string             `json:"code,omitempty"`
	Source   string             `json:"source,omitempty"`
	Message  string             `json:"message"`
}

// diagnosticsProvider implements DiagnosticsProvider.
type diagnosticsProvider struct{}

// NewDiagnosticsProvider creates a new diagnostics provider.
func NewDiagnosticsProvider() DiagnosticsProvider {
	return &diagnosticsProvider{}
}

func (d *diagnosticsProvider) Diagnose(doc *Document) ([]Diagnostic, error) {
	log.Server("Diagnostics provider for %s", doc.URI)

	if doc.AST == nil && len(doc.Errors) == 0 {
		return []Diagnostic{}, nil
	}

	diagnostics := make([]Diagnostic, 0, len(doc.Errors))

	for _, err := range doc.Errors {
		var rng Range
		if err.EndPos.Line > 0 || err.EndPos.Column > 0 {
			rng = Range{
				Start: Position{
					Line:      err.Pos.Line - 1,
					Character: err.Pos.Column - 1,
				},
				End: Position{
					Line:      err.EndPos.Line - 1,
					Character: err.EndPos.Column - 1,
				},
			}
		} else {
			length := estimateErrorLength(err.Message)
			rng = Range{
				Start: Position{
					Line:      err.Pos.Line - 1,
					Character: err.Pos.Column - 1,
				},
				End: Position{
					Line:      err.Pos.Line - 1,
					Character: err.Pos.Column - 1 + length,
				},
			}
		}

		diag := Diagnostic{
			Range:    rng,
			Severity: DiagnosticSeverityError,
			Source:   "gsx",
			Message:  err.Message,
		}
		if err.Hint != "" {
			diag.Message = err.Message + " (" + err.Hint + ")"
		}
		diagnostics = append(diagnostics, diag)
	}

	return diagnostics, nil
}

// estimateErrorLength estimates the length of text to highlight for an error.
// It extracts quoted tokens from the message (e.g., "unexpected 'foo'") and uses
// their length, or falls back to the first word length if no quotes are found.
func estimateErrorLength(message string) int {
	// Try to extract a quoted token from the message (single or double quotes).
	for _, q := range []byte{'\'', '"', '`'} {
		start := strings.IndexByte(message, q)
		if start >= 0 {
			end := strings.IndexByte(message[start+1:], q)
			if end > 0 {
				return end
			}
		}
	}

	// Try to extract the last meaningful word (often the problematic token).
	words := strings.Fields(message)
	if len(words) > 0 {
		last := words[len(words)-1]
		// Strip trailing punctuation
		last = strings.TrimRight(last, ".,;:!?")
		if len(last) > 0 {
			return len(last)
		}
	}

	return 1 // Minimum highlight of 1 character
}