Skip to content

Commit

Permalink
feat(workspaces): go-to-defintion works for subcharts and multiple
Browse files Browse the repository at this point in the history
valuesFiles
  • Loading branch information
qvalentin committed Jan 13, 2024
1 parent a74628c commit 6da5a3f
Show file tree
Hide file tree
Showing 14 changed files with 181 additions and 45 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/bin
/dist
__debug_bin*
.coverage
12 changes: 12 additions & 0 deletions internal/charts/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,15 @@ func NewChart(rootURI uri.URI) *Chart {
ParentChart: getParentChart(rootURI),
}
}

// ResolveValueFiles returns a list of all values files in the chart
// and all parent charts if the query tries to access global values
func (c *Chart) ResolveValueFiles(query []string, chartStore *ChartStore) []*ValuesFiles {
if len(query) > 0 && query[0] == "global" {
parentChart := c.ParentChart.GetParentChart(chartStore)
if parentChart != nil {
return append([]*ValuesFiles{c.ValuesFiles}, parentChart.ResolveValueFiles(query, chartStore)...)
}
}
return []*ValuesFiles{c.ValuesFiles}
}
6 changes: 3 additions & 3 deletions internal/charts/chart_for_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (s *ChartStore) GetChartForDoc(uri lsp.DocumentURI) (*Chart, error) {
return chart, nil
}

chart = s.getChartFromFilesystem(uri.Filename())
chart = s.getChartFromFilesystemForTemplates(uri.Filename())

if chart != nil {
s.Charts[chart.RootURI] = chart
Expand All @@ -38,7 +38,7 @@ func (s *ChartStore) getChartFromCache(uri lsp.DocumentURI) *Chart {
return nil
}

func (s *ChartStore) getChartFromFilesystem(path string) *Chart {
func (s *ChartStore) getChartFromFilesystemForTemplates(path string) *Chart {
directory := filepath.Dir(path)
if filepath.Base(directory) == "templates" {
templatesDir := directory
Expand All @@ -55,7 +55,7 @@ func (s *ChartStore) getChartFromFilesystem(path string) *Chart {
return nil
}

return s.getChartFromFilesystem(directory)
return s.getChartFromFilesystemForTemplates(directory)
}

func isChartDirectory(expectedChartDir string) bool {
Expand Down
21 changes: 21 additions & 0 deletions internal/charts/chart_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,24 @@ func NewChartStore(rootURI uri.URI, newChart func(uri.URI) *Chart) *ChartStore {
newChart: newChart,
}
}

func (s *ChartStore) GetChartForURI(fileURI uri.URI) (*Chart, error) {
if chart, ok := s.Charts[fileURI]; ok {
return chart, nil
}

var chart *Chart
expectedChartDir := fileURI.Filename()
if isChartDirectory(expectedChartDir) {
chart = s.newChart(uri.New("file://" + expectedChartDir))
}

if chart != nil {
s.Charts[chart.RootURI] = chart
return chart, nil
}

return nil, ErrChartNotFound{
URI: fileURI,
}
}
38 changes: 35 additions & 3 deletions internal/charts/chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type: application`
_ = os.WriteFile(chartFile, []byte(chartYaml), 0644)

chart := charts.NewChart(uri.New("file://" + tempDir))
assert.Equal(t, "hello-world", chart.ChartMetadata.ChartMetadata.Name)
assert.Equal(t, "hello-world", chart.ChartMetadata.Metadata.Name)
}

func TestNewChartsLoadsDefaultMetadataOnError(t *testing.T) {
Expand All @@ -34,7 +34,7 @@ func TestNewChartsLoadsDefaultMetadataOnError(t *testing.T) {
_ = os.WriteFile(chartFile, []byte(chartYaml), 0644)

chart := charts.NewChart(uri.New("file://" + tempDir))
assert.Equal(t, "", chart.ChartMetadata.ChartMetadata.Name)
assert.Equal(t, "", chart.ChartMetadata.Metadata.Name)
}

func TestNewChartsSetsParentChartURI(t *testing.T) {
Expand All @@ -50,9 +50,41 @@ func TestNewChartsSetsParentChartURI(t *testing.T) {
func TestNewChartsSetsParentChartURIToDefault(t *testing.T) {
tempDir := t.TempDir()

chartFile := filepath.Join(tempDir, "Chart.yaqml")
chartFile := filepath.Join(tempDir, "Chart.yaml")
_ = os.WriteFile(chartFile, []byte{}, 0644)

chart := charts.NewChart(uri.New("file://" + tempDir))
assert.False(t, chart.ParentChart.HasParent)
}

func TestResolvesValuesFileOfParent(t *testing.T) {

tempDir := t.TempDir()

chartFile := filepath.Join(tempDir, "Chart.yaml")
_ = os.WriteFile(chartFile, []byte{}, 0644)
valuesFile := filepath.Join(tempDir, "values.yaml")
_ = os.WriteFile(valuesFile, []byte{}, 0644)

subChartValuesFile := filepath.Join(tempDir, "charts", "subchart", "values.yaml")
subChartChartFile := filepath.Join(tempDir, "charts", "subchart", "Chart.yaml")
var err = os.MkdirAll(filepath.Dir(subChartValuesFile), 0755)
assert.NoError(t, err)
err = os.WriteFile(subChartValuesFile, []byte{}, 0644)
assert.NoError(t, err)
err = os.WriteFile(subChartChartFile, []byte{}, 0644)
assert.NoError(t, err)

chart := charts.NewChart(uri.New("file://" + filepath.Join(tempDir, "charts", "subchart")))

expectedChart := &charts.Chart{
RootURI: uri.New("file://" + tempDir),
}
newChartFunc := func(_ uri.URI) *charts.Chart { return expectedChart }
chartStore := charts.NewChartStore(uri.New("file://"+tempDir), newChartFunc)

valueFiles := chart.ResolveValueFiles([]string{"global", "foo"}, chartStore)

assert.Equal(t, 2, len(valueFiles))

}
8 changes: 4 additions & 4 deletions internal/charts/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
)

type ChartMetadata struct {
ChartFileNode yaml.Node
ChartMetadata chart.Metadata
YamlNode yaml.Node
Metadata chart.Metadata
}

func NewChartMetadata(rootURI uri.URI) *ChartMetadata {
Expand All @@ -22,8 +22,8 @@ func NewChartMetadata(rootURI uri.URI) *ChartMetadata {
}

return &ChartMetadata{
ChartMetadata: loadChartMetadata(filePath),
ChartFileNode: chartNode,
Metadata: loadChartMetadata(filePath),
YamlNode: chartNode,
}
}

Expand Down
12 changes: 12 additions & 0 deletions internal/charts/parent_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,15 @@ func getParentChart(rootURI uri.URI) ParentChart {
}
return ParentChart{}
}

func (p *ParentChart) GetParentChart(chartStore *ChartStore) *Chart {
if !p.HasParent {
return nil
}
chart, err := chartStore.GetChartForURI(p.ParentChartURI)
if err != nil {
logger.Error("Error getting parent chart ", err)
return nil
}
return chart
}
4 changes: 4 additions & 0 deletions internal/charts/values_file.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package charts

import (
"github.com/mrjosh/helm-ls/internal/util"
"github.com/mrjosh/helm-ls/pkg/chartutil"
"go.lsp.dev/uri"

"gopkg.in/yaml.v3"
)

type ValuesFile struct {
Values chartutil.Values
ValueNode yaml.Node
URI uri.URI
}

func NewValuesFile(filePath string) *ValuesFile {
Expand All @@ -26,5 +29,6 @@ func NewValuesFile(filePath string) *ValuesFile {
return &ValuesFile{
ValueNode: valueNodes,
Values: vals,
URI: uri.New(util.FileURIScheme + filePath),
}
}
28 changes: 26 additions & 2 deletions internal/charts/values_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package charts
import (
"path/filepath"

"github.com/mrjosh/helm-ls/internal/util"
lsp "go.lsp.dev/protocol"

"go.lsp.dev/uri"
)

Expand All @@ -12,15 +15,15 @@ type ValuesFiles struct {
}

func NewValuesFiles(rootURI uri.URI, mainValuesFileName string, additionalValuesFilesGlob string) *ValuesFiles {
additionalValuesFiles := getAdditionalValuesFiles(additionalValuesFilesGlob, rootURI)
additionalValuesFiles := getAdditionalValuesFiles(additionalValuesFilesGlob, rootURI, mainValuesFileName)

return &ValuesFiles{
MainValuesFile: NewValuesFile(filepath.Join(rootURI.Filename(), mainValuesFileName)),
AdditionalValuesFiles: additionalValuesFiles,
}
}

func getAdditionalValuesFiles(additionalValuesFilesGlob string, rootURI uri.URI) []*ValuesFile {
func getAdditionalValuesFiles(additionalValuesFilesGlob string, rootURI uri.URI, mainValuesFileName string) []*ValuesFile {
additionalValuesFiles := []*ValuesFile{}
if additionalValuesFilesGlob != "" {

Expand All @@ -30,9 +33,30 @@ func getAdditionalValuesFiles(additionalValuesFilesGlob string, rootURI uri.URI)
} else {

for _, match := range matches {
if match == filepath.Join(rootURI.Filename(), mainValuesFileName) {
continue
}
additionalValuesFiles = append(additionalValuesFiles, NewValuesFile(match))
}
}
}
return additionalValuesFiles
}

func (v *ValuesFiles) AllValuesFiles() []*ValuesFile {
return append([]*ValuesFile{v.MainValuesFile}, v.AdditionalValuesFiles...)
}

func (v *ValuesFiles) GetPositionsForValue(query []string) []lsp.Location {
var result = []lsp.Location{}
for _, value := range v.AllValuesFiles() {
pos, err := util.GetPositionOfNode(&value.ValueNode, query)
if err != nil {
logger.Error("Error getting position for value", value, query, err)
continue
}
result = append(result, lsp.Location{URI: value.URI, Range: lsp.Range{Start: pos, End: pos}})
}

return result
}
26 changes: 26 additions & 0 deletions internal/charts/values_files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/mrjosh/helm-ls/internal/charts"
"github.com/stretchr/testify/assert"
lsp "go.lsp.dev/protocol"
"go.lsp.dev/uri"
)

Expand All @@ -23,4 +24,29 @@ func TestNewValuesFiles(t *testing.T) {

assert.Equal(t, "bar", valuesFiles.MainValuesFile.Values["foo"])
assert.Equal(t, "baz", valuesFiles.AdditionalValuesFiles[0].Values["bar"])
assert.Equal(t, 1, len(valuesFiles.AdditionalValuesFiles))
}

func TestGetPositionsForValue(t *testing.T) {
tempDir := t.TempDir()

valuesContent := `foo: bar`
additionalValuesContent := `
other: value
foo: baz`

_ = os.WriteFile(filepath.Join(tempDir, "values.yaml"), []byte(valuesContent), 0644)
_ = os.WriteFile(filepath.Join(tempDir, "values-additional.yaml"), []byte(additionalValuesContent), 0644)

valuesFiles := charts.NewValuesFiles(uri.New("file://"+tempDir), "values.yaml", "values*.yaml")

assert.Equal(t, []lsp.Location{
{
URI: uri.New("file://" + filepath.Join(tempDir, "values.yaml")),
Range: lsp.Range{Start: lsp.Position{Line: 0, Character: 0}, End: lsp.Position{Line: 0, Character: 0}},
},
{
URI: uri.New("file://" + filepath.Join(tempDir, "values-additional.yaml")),
Range: lsp.Range{Start: lsp.Position{Line: 2, Character: 0}, End: lsp.Position{Line: 2, Character: 0}},
}}, valuesFiles.GetPositionsForValue([]string{"foo"}))
}
Loading

0 comments on commit 6da5a3f

Please sign in to comment.