Fix parsing LSP URIs containing spaces (#38)

pull/41/head
Cormac Relf 3 years ago committed by GitHub
parent 83b14ca827
commit 3664734bda
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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
}
}

@ -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
}
Loading…
Cancel
Save