Skip to content

Commit

Permalink
internal/document: clean up handle logic & normalize URIs before saving
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Feb 21, 2022
1 parent 2bb1bf3 commit 5af49be
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
12 changes: 2 additions & 10 deletions internal/document/dir_handle.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package document

import (
"fmt"
"os"
"strings"

"github.com/hashicorp/terraform-ls/internal/uri"
)

Expand All @@ -26,8 +22,6 @@ func (dh DirHandle) Path() string {
// It is however outside the scope of the function to verify
// this is actually the case or whether the directory exists.
func DirHandleFromPath(dirPath string) DirHandle {
dirPath = strings.TrimSuffix(dirPath, fmt.Sprintf("%c", os.PathSeparator))

return DirHandle{
URI: uri.FromPath(dirPath),
}
Expand All @@ -39,10 +33,8 @@ func DirHandleFromPath(dirPath string) DirHandle {
// It is however outside the scope of the function to verify
// this is actually the case or whether the directory exists.
func DirHandleFromURI(dirUri string) DirHandle {
// Dir URIs are usually without trailing separator already
// but we double check anyway, so we deal with the same URI
// regardless of language client differences
dirUri = strings.TrimSuffix(string(dirUri), "/")
// Normalize the raw URI to account for any escaping differences
dirUri = uri.MustParseURI(dirUri)

return DirHandle{
URI: dirUri,
Expand Down
6 changes: 6 additions & 0 deletions internal/document/dir_handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ func TestDirHandleFromURI(t *testing.T) {
URI: "file:///C:/random/path",
},
},
{
RawURI: "file:///C%3A/random/path",
ExpectedHandle: DirHandle{
URI: "file:///C:/random/path",
},
},
}

for i, tc := range testCases {
Expand Down
17 changes: 7 additions & 10 deletions internal/document/handle.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package document

import (
"fmt"
"os"
"path"
"path/filepath"
"strings"

"github.com/hashicorp/terraform-ls/internal/uri"
)

// Handle represents a document location
Expand All @@ -22,13 +23,11 @@ type Handle struct {
// It is however outside the scope of the function to verify
// this is actually the case or whether the file exists.
func HandleFromURI(docUri string) Handle {
path := uri.MustPathFromURI(docUri)

filename := filepath.Base(path)
filename := path.Base(docUri)
dirUri := strings.TrimSuffix(docUri, "/"+filename)

return Handle{
Dir: DirHandle{URI: dirUri},
Dir: DirHandleFromURI(dirUri),
Filename: filename,
}
}
Expand All @@ -39,13 +38,11 @@ func HandleFromURI(docUri string) Handle {
// It is however outside the scope of the function to verify
// this is actually the case or whether the file exists.
func HandleFromPath(docPath string) Handle {
docUri := uri.FromPath(docPath)

filename := filepath.Base(docPath)
dirUri := strings.TrimSuffix(docUri, "/"+filename)
dirPath := strings.TrimSuffix(docPath, fmt.Sprintf("%c%s", os.PathSeparator, filename))

return Handle{
Dir: DirHandle{URI: dirUri},
Dir: DirHandleFromPath(dirPath),
Filename: filename,
}
}
Expand Down
7 changes: 7 additions & 0 deletions internal/document/handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ func TestHandleFromURI(t *testing.T) {
Filename: "config.tf",
},
},
{
RawURI: "file:///C%3A/random/path/to/config.tf",
ExpectedHandle: Handle{
Dir: DirHandle{URI: "file:///C:/random/path/to"},
Filename: "config.tf",
},
},
}

for i, tc := range testCases {
Expand Down

0 comments on commit 5af49be

Please sign in to comment.