Skip to content

Commit

Permalink
Fix parsing LSP URIs containing spaces (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
cormacrelf authored May 5, 2021
1 parent 83b14ca commit 3664734
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
20 changes: 16 additions & 4 deletions internal/adapter/lsp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
}

Expand Down
27 changes: 27 additions & 0 deletions internal/adapter/lsp/util.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 3664734

Please sign in to comment.