Skip to content

Commit

Permalink
refactor(documents): clean up document.go and test documentStore
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed Jan 28, 2024
1 parent f366026 commit 0724d9d
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 273 deletions.
1 change: 0 additions & 1 deletion internal/adapter/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ func (fs *FileStorage) Canonical(path string) string {
}

func (fs *FileStorage) FileExists(path string) (bool, error) {

fi, err := fs.fileInfo(path)
if err != nil {
return false, err
Expand Down
6 changes: 3 additions & 3 deletions internal/adapter/yamlls/trimTemplate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,12 @@ func TestTrimTemplate(t *testing.T) {
func testTrimTemplateWithTestData(t *testing.T, testData TrimTemplateTestData) {
doc := &lsplocal.Document{
Content: testData.documentText,
Ast: lsplocal.ParseAst(testData.documentText),
Ast: lsplocal.ParseAst(nil, testData.documentText),
}

var trimmed = trimTemplateForYamllsFromAst(doc.Ast, testData.documentText)
trimmed := trimTemplateForYamllsFromAst(doc.Ast, testData.documentText)

var result = trimmed == testData.trimmedText
result := trimmed == testData.trimmedText

if !result {
t.Errorf("Trimmed templated was not as expected but was %s ", trimmed)
Expand Down
36 changes: 21 additions & 15 deletions internal/charts/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ type Chart struct {

func NewChart(rootURI uri.URI, valuesFilesConfig util.ValuesFilesConfig) *Chart {
return &Chart{
ValuesFiles: NewValuesFiles(rootURI, valuesFilesConfig.MainValuesFileName, valuesFilesConfig.LintOverlayValuesFileName, valuesFilesConfig.AdditionalValuesFilesGlobPattern),
ValuesFiles: NewValuesFiles(rootURI,
valuesFilesConfig.MainValuesFileName,
valuesFilesConfig.LintOverlayValuesFileName,
valuesFilesConfig.AdditionalValuesFilesGlobPattern),
ChartMetadata: NewChartMetadata(rootURI),
RootURI: rootURI,
ParentChart: newParentChart(rootURI),
Expand All @@ -32,20 +35,23 @@ type QueriedValuesFiles struct {
// 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) []*QueriedValuesFiles {
if len(query) > 0 && query[0] == "global" {
parentChart := c.ParentChart.GetParentChart(chartStore)
if parentChart != nil {
return append([]*QueriedValuesFiles{{Selector: query, ValuesFiles: c.ValuesFiles}}, parentChart.ResolveValueFiles(query, chartStore)...)
}
ownResult := []*QueriedValuesFiles{{Selector: query, ValuesFiles: c.ValuesFiles}}
if len(query) == 0 {
return ownResult
}
chartName := c.ChartMetadata.Metadata.Name
if len(query) > 0 {
parentChart := c.ParentChart.GetParentChart(chartStore)
if parentChart != nil {
extendedQuery := append([]string{chartName}, query...)
return append([]*QueriedValuesFiles{{Selector: query, ValuesFiles: c.ValuesFiles}},
parentChart.ResolveValueFiles(extendedQuery, chartStore)...)
}

parentChart := c.ParentChart.GetParentChart(chartStore)
if parentChart == nil {
return ownResult
}

if query[0] == "global" {
return append(ownResult,
parentChart.ResolveValueFiles(query, chartStore)...)
}
return []*QueriedValuesFiles{{Selector: query, ValuesFiles: c.ValuesFiles}}

chartName := c.ChartMetadata.Metadata.Name
extendedQuery := append([]string{chartName}, query...)
return append(ownResult,
parentChart.ResolveValueFiles(extendedQuery, chartStore)...)
}
4 changes: 1 addition & 3 deletions internal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"errors"

"github.com/mrjosh/helm-ls/internal/adapter/fs"
"github.com/mrjosh/helm-ls/internal/adapter/yamlls"
"github.com/mrjosh/helm-ls/internal/charts"
lsplocal "github.com/mrjosh/helm-ls/internal/lsp"
Expand All @@ -28,8 +27,7 @@ type langHandler struct {
}

func NewHandler(connPool jsonrpc2.Conn) jsonrpc2.Handler {
fileStorage, _ := fs.NewFileStorage("")
documents := lsplocal.NewDocumentStore(fileStorage)
documents := lsplocal.NewDocumentStore()
handler := &langHandler{
linterName: "helm-lint",
connPool: connPool,
Expand Down
6 changes: 3 additions & 3 deletions internal/lsp/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
lsp "go.lsp.dev/protocol"
)

func ParseAst(content string) *sitter.Tree {
func ParseAst(oldTree *sitter.Tree, content string) *sitter.Tree {
parser := sitter.NewParser()
parser.SetLanguage(gotemplate.GetLanguage())
tree, _ := parser.ParseCtx(context.Background(), nil, []byte(content))
tree, _ := parser.ParseCtx(context.Background(), oldTree, []byte(content))
return tree
}

Expand Down Expand Up @@ -106,7 +106,7 @@ func TraverseIdentifierPathUp(node *sitter.Node, doc *Document) string {
}

func (d *Document) ApplyChangesToAst(newContent string) {
d.Ast = ParseAst(newContent)
d.Ast = ParseAst(d.Ast, newContent)
}

func GetLspRangeForNode(node *sitter.Node) lsp.Range {
Expand Down
3 changes: 1 addition & 2 deletions internal/lsp/ast_diagnostics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func TestIsInElseBranch(t *testing.T) {
template := `{{if pipeline}} t1 {{ else if pipeline }} t2 {{ else if pipeline2 }} t3 {{ else }} t4 {{ end }}`
var ast = ParseAst(template)
ast := ParseAst(nil, template)
// (template [0, 0] - [1, 0]
// (if_action [0, 0] - [0, 95]
// condition: (function_call [0, 5] - [0, 13]
Expand Down Expand Up @@ -52,5 +52,4 @@ func TestIsInElseBranch(t *testing.T) {
if !IsInElseBranch(t4) {
t.Errorf("t4 was incorrectly identified as not in else branch")
}

}
8 changes: 4 additions & 4 deletions internal/lsp/ast_field_identifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func TestGetFieldIdentifierPathSimple(t *testing.T) {
template := `{{ .Values.test }}`

var ast = ParseAst(template)
ast := ParseAst(nil, template)
// (template [0, 0] - [1, 0]
// (selector_expression [0, 3] - [0, 15]
// operand: (field [0, 3] - [0, 10]
Expand All @@ -37,7 +37,7 @@ func TestGetFieldIdentifierPathSimple(t *testing.T) {
func TestGetFieldIdentifierPathWith(t *testing.T) {
template := `{{ with .Values }}{{ .test }} {{ end }}`

var ast = ParseAst(template)
ast := ParseAst(nil, template)
// (template [0, 0] - [1, 0]
// (with_action [0, 0] - [0, 39]
// condition: (field [0, 8] - [0, 15]
Expand All @@ -64,7 +64,7 @@ func TestGetFieldIdentifierPathWith(t *testing.T) {
func TestGetFieldIdentifierPathFunction(t *testing.T) {
template := `{{ and .Values.test1 .Values.test2 }}`

var ast = ParseAst(template)
ast := ParseAst(nil, template)
// (template [0, 0] - [1, 0]
// (function_call [0, 3] - [0, 35]
// function: (identifier [0, 3] - [0, 6])
Expand Down Expand Up @@ -102,7 +102,7 @@ func TestGetFieldIdentifierPathFunctionForCompletion(t *testing.T) {
template := `{{ and .Values.image .Values. }}`
// | -> complete at dot

var ast = ParseAst(template)
ast := ParseAst(nil, template)

var (
position = lsp.Position{Line: 0, Character: 29}
Expand Down
Loading

0 comments on commit 0724d9d

Please sign in to comment.