z_mirror/luapls

git clone https://git.raiguard.me/z_mirror/luapls

Caleb HeuerConvert LSP methods to new environment, fix bugs in file graph generationd0e4705

main
899 B40 linesraw
1package util
2
3import (
4	"errors"
5	"fmt"
6	"net/url"
7	"os"
8	"path/filepath"
9
10	protocol "github.com/tliron/glsp/protocol_3_16"
11)
12
13// Ptr returns a pointer to the given value.
14func Ptr[T any](value T) *T {
15	return &value
16}
17
18// FileExists returns whether the given file exists on the filesystem.
19func FileExists(path string) bool {
20	_, err := os.Stat(path)
21	return err == nil
22}
23
24// URIToPath returns a path from the given URI.
25func URIToPath(uri protocol.URI) (string, error) {
26	u, err := url.ParseRequestURI(uri)
27	if err != nil {
28		return "", errors.New(fmt.Sprintf("Failed to parse file URI: %s", err))
29	}
30	return u.Path, nil
31}
32
33// PathToURI returns an absolute URI from a file path.
34func PathToURI(path string) (protocol.URI, error) {
35	abs, err := filepath.Abs(path)
36	if err != nil {
37		return "", errors.New(fmt.Sprintf("Failed to convert filepath to URI: %s", err))
38	}
39	return "file://" + abs, nil
40}