summaryrefslogtreecommitdiff
path: root/internal/hexaiaction/parse.go
blob: 33fc4af3ef45e6e3ef2c15d76cdf9ac074d50ab0 (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
package hexaiaction

import (
	"bufio"
	"io"
	"strings"

	"codeberg.org/snonux/hexai/internal/textutil"
)

// ParseInput splits raw stdin into optional diagnostics and selection/code.
// Format:
//
//	Diagnostics:\n
//	<one per line>\n
//	<blank line> (optional)\n
//	<rest is selection/code>
//
// If the header is absent, the entire input is treated as selection.
func ParseInput(r io.Reader) (InputParts, error) {
	b, err := io.ReadAll(bufio.NewReader(r))
	if err != nil {
		return InputParts{}, err
	}
	raw := strings.TrimSpace(string(b))
	if raw == "" {
		return InputParts{Selection: ""}, nil
	}
	lines := strings.Split(raw, "\n")
	// find a case-insensitive line equal to "diagnostics:"
	diagsIdx := -1
	for i, ln := range lines {
		t := strings.TrimSpace(strings.ToLower(ln))
		if t == "diagnostics:" {
			diagsIdx = i
			break
		}
	}
	if diagsIdx < 0 {
		return InputParts{Selection: raw}, nil
	}
	// collect diagnostics until a blank line or EOF
	diags := []string{}
	i := diagsIdx + 1
	for ; i < len(lines); i++ {
		t := strings.TrimSpace(lines[i])
		if t == "" {
			i++
			break
		}
		diags = append(diags, t)
	}
	sel := strings.Join(lines[i:], "\n")
	sel = strings.TrimSpace(sel)
	return InputParts{Selection: sel, Diagnostics: diags}, nil
}

// ExtractInstruction mirrors the LSP instructionFromSelection behavior (subset),
// scanning the first line for an instruction marker and removing it from the selection.
func ExtractInstruction(sel string) (string, string) { return textutil.InstructionFromSelection(sel) }

// findFirstInstructionInLine follows the same precedence as LSP:
// - ;text; (strict)
// - /* text */ (single-line)
// - <!-- text --> (single-line)
// - // text
// - # text
// - -- text
// helpers moved to textutil