Skip to content

Commit

Permalink
fix(windows): fix some path seperator issues
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed Jan 27, 2024
1 parent 2f0cbd6 commit e751831
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 52 deletions.
9 changes: 5 additions & 4 deletions internal/handler/hover_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handler

import (
"path/filepath"
"testing"

"github.com/mrjosh/helm-ls/internal/charts"
Expand Down Expand Up @@ -125,7 +126,7 @@ nested: value
want: `### values.yaml
parentValue
### charts/subchart/values.yaml
### ` + filepath.Join("charts", "subchart", "values.yaml") + `
value
`,
Expand Down Expand Up @@ -159,7 +160,7 @@ value
want: `### values.yaml
parentValue
### charts/subchart/values.yaml
### ` + filepath.Join("charts", "subchart", "values.yaml") + `
value
`,
Expand Down Expand Up @@ -203,10 +204,10 @@ value
want: `### values.yaml
parentValue
### charts/subchart/values.yaml
### ` + filepath.Join("charts", "subchart", "values.yaml") + `
middleValue
### charts/subchart/charts/subsubchart/values.yaml
### ` + filepath.Join("charts", "subchart", "charts", "subsubchart", "values.yaml") + `
value
`,
Expand Down
26 changes: 4 additions & 22 deletions internal/lsp/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package lsp

import (
"bytes"
"fmt"
"strings"

"github.com/mrjosh/helm-ls/internal/util"
"github.com/pkg/errors"
sitter "github.com/smacker/go-tree-sitter"
lsp "go.lsp.dev/protocol"
"go.lsp.dev/uri"
Expand Down Expand Up @@ -33,16 +33,10 @@ func (s *DocumentStore) GetAllDocs() []*Document {
}

func (s *DocumentStore) DidOpen(params lsp.DidOpenTextDocumentParams,helmlsConfig util.HelmlsConfiguration) (*Document, error) {
//langID := params.TextDocument.LanguageID
//if langID != "markdown" && langID != "vimwiki" && langID != "pandoc" {
//return nil, nil
//}
logger.Debug(fmt.Sprintf("Opening document %s with langID %s", params.TextDocument.URI,params.TextDocument.LanguageID))

uri := params.TextDocument.URI
path, err := s.normalizePath(uri)
if err != nil {
return nil, err
}
path := uri.Filename()
doc := &Document{
URI: uri,
Path: path,
Expand All @@ -59,23 +53,11 @@ func (s *DocumentStore) Close(uri lsp.DocumentURI) {
}

func (s *DocumentStore) Get(docuri uri.URI) (*Document, bool) {
path, err := s.normalizePath(docuri)
if err != nil {
logger.Debug(err)
return nil, false
}
path := docuri.Filename()
d, ok := s.documents[path]
return d, ok
}

func (s *DocumentStore) normalizePath(docuri uri.URI) (string, error) {
path, err := util.URIToPath(docuri)
if err != nil {
return "", errors.Wrapf(err, "unable to parse URI: %s", docuri)
}
return s.fs.Canonical(path), nil
}

// Document represents an opened file.
type Document struct {
URI lsp.DocumentURI
Expand Down
26 changes: 0 additions & 26 deletions internal/util/strings.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package util

import (
"net/url"
"regexp"
"runtime"
"strings"

"github.com/mrjosh/helm-ls/internal/log"
"go.lsp.dev/protocol"
"go.lsp.dev/uri"
)

var (
Expand Down Expand Up @@ -46,29 +43,6 @@ func AfterStrings(value string, a string) string {
return value[adjustedPos:]
}

func URIToPath(docuri uri.URI) (string, error) {
parsed, err := url.Parse(docuri.Filename())
if err != nil {
return "", err
}

logger.Printf("Go file uri %s, path: %s", parsed, parsed.Path)
if runtime.GOOS == "windows" {

// In Windows "file:///c:/tmp/foo.md" is parsed to "/c:/tmp/foo.md".
// Strip the first character to get a valid path.
if strings.Contains(parsed.Path[1:], ":") {
// url.Parse() behaves differently with "file:///c:/..." and "file://c:/..."
return parsed.Path[1:], nil
}

// if the windows drive is not included in Path it will be in Host
return parsed.Host + "/" + parsed.Path[1:], nil
}

return parsed.Path, nil
}

// WordAt returns the word found at the given character position.
// Credit https://github.com/aca/neuron-language-server/blob/450a7cff71c14e291ee85ff8a0614fa9d4dd5145/utils.go#L13
func WordAt(str string, index int) string {
Expand Down

0 comments on commit e751831

Please sign in to comment.