diff --git a/internal/adapter/lsp/server.go b/internal/adapter/lsp/server.go index 8ff8e7e..a545d88 100644 --- a/internal/adapter/lsp/server.go +++ b/internal/adapter/lsp/server.go @@ -143,7 +143,12 @@ func NewServer(opts ServerOpts) *Server { return nil } - path := fs.Canonical(strings.TrimPrefix(params.TextDocument.URI, "file://")) + path, err := uriToPath(params.TextDocument.URI) + if err != nil { + server.logger.Printf("unable to parse URI: %v", err) + return nil + } + path = fs.Canonical(path) server.documents[params.TextDocument.URI] = &document{ Path: path, @@ -243,8 +248,14 @@ func NewServer(opts ServerOpts) *Server { return nil, err } - target = strings.TrimPrefix(target, "file://") - contents, err := ioutil.ReadFile(target) + path, err := uriToPath(target) + if err != nil { + server.logger.Printf("unable to parse URI: %v", err) + return nil, err + } + path = fs.Canonical(path) + + contents, err := ioutil.ReadFile(path) if err != nil { return nil, err } @@ -349,7 +360,8 @@ func (s *Server) targetForHref(href string, doc *document, notebook *core.Notebo if note == nil { return "", nil } - return "file://" + filepath.Join(notebook.Path, note.Path), nil + joined_path := filepath.Join(notebook.Path, note.Path) + return pathToURI(joined_path), nil } } diff --git a/internal/adapter/lsp/util.go b/internal/adapter/lsp/util.go new file mode 100644 index 0000000..2a064ff --- /dev/null +++ b/internal/adapter/lsp/util.go @@ -0,0 +1,27 @@ +package lsp + +import ( + "net/url" + "github.com/mickael-menu/zk/internal/util/errors" +) + +func pathToURI(path string) string { + u := &url.URL{ + Scheme: "file", + Path: path, + } + return u.String() +} + + +func uriToPath(uri string) (string, error) { + parsed, err := url.Parse(uri) + if err != nil { + return "", err + } + if parsed.Scheme != "file" { + return "", errors.New("URI was not a file:// URI") + } + return parsed.Path, nil +} +