1
0
mirror of https://github.com/golang/go synced 2024-10-01 18:38:34 -06:00
go/internal/lsp/source/uri.go
Ian Cottrell 9b5bafe36f internal/lsp: extract view to its own package
This allows us to write the lsp verbs in terms of a stable underlying source
management layer.
This should make it easier to refactor the underlying layer to add more powerful
caching and incremental modes as we go.

Change-Id: Iab97b061d80394a6fa6748a93a4c68f2deb46129
Reviewed-on: https://go-review.googlesource.com/c/147201
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2018-11-05 21:38:13 +00:00

33 lines
911 B
Go

// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package source
import (
"fmt"
"path/filepath"
"strings"
"golang.org/x/tools/internal/lsp/protocol"
)
const fileSchemePrefix = "file://"
// FromURI gets the file path for a given URI.
// It will return an error if the uri is not valid, or if the URI was not
// a file URI
func FromURI(uri protocol.DocumentURI) (string, error) {
s := string(uri)
if !strings.HasPrefix(s, fileSchemePrefix) {
return "", fmt.Errorf("only file URI's are supported, got %v", uri)
}
return filepath.FromSlash(s[len(fileSchemePrefix):]), nil
}
// ToURI returns a protocol URI for the supplied path.
// It will always have the file scheme.
func ToURI(path string) protocol.DocumentURI {
return protocol.DocumentURI(fileSchemePrefix + filepath.ToSlash(path))
}