▍ humdrum codex / glint v1.1.2
license AGPL-3.0
1.1 KB 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
package grammar

import (
	"fmt"
	"net/url"
	"os"
	"path/filepath"
	"strconv"
	"strings"
)

// pathToURI turns an editor file path into an LSP document URI. Unnamed buffers
// (empty path) get a stable synthetic URI so harper still checks them.
func pathToURI(path string) string {
	if strings.TrimSpace(path) == "" {
		return "file:///glint-untitled.md"
	}
	abs := path
	if p, err := filepath.Abs(path); err == nil {
		abs = p
	}
	return "file://" + (&url.URL{Path: abs}).EscapedPath()
}

// osGetwd returns the working directory for the initialize rootUri, falling back
// to the temp dir so a missing cwd never breaks startup.
func osGetwd() string {
	if wd, err := os.Getwd(); err == nil {
		return wd
	}
	return os.TempDir()
}

func itoa(n int) string { return strconv.Itoa(n) }

// codeString renders an LSP diagnostic code, which may be a string or a number,
// as a string.
func codeString(code any) string {
	switch v := code.(type) {
	case string:
		return v
	case float64:
		return strconv.FormatFloat(v, 'f', -1, 64)
	case nil:
		return ""
	default:
		return fmt.Sprint(v)
	}
}