diff --git a/internal/adapter/yamlls/diagnostics.go b/internal/adapter/yamlls/diagnostics.go index 07d01c2b..e3b06c0c 100644 --- a/internal/adapter/yamlls/diagnostics.go +++ b/internal/adapter/yamlls/diagnostics.go @@ -7,6 +7,7 @@ import ( "strings" lsplocal "github.com/mrjosh/helm-ls/internal/lsp" + templateast "github.com/mrjosh/helm-ls/internal/lsp/template_ast" sitter "github.com/smacker/go-tree-sitter" "go.lsp.dev/protocol" lsp "go.lsp.dev/protocol" @@ -36,8 +37,8 @@ func filterDiagnostics(diagnostics []lsp.Diagnostic, ast *sitter.Tree, content [ filtered = []lsp.Diagnostic{} for _, diagnostic := range diagnostics { - node := lsplocal.NodeAtPosition(ast, diagnostic.Range.Start) - childNode := lsplocal.FindRelevantChildNode(ast.RootNode(), lsplocal.GetSitterPointForLspPos(diagnostic.Range.Start)) + node := templateast.NodeAtPosition(ast, diagnostic.Range.Start) + childNode := templateast.FindRelevantChildNode(ast.RootNode(), templateast.GetSitterPointForLspPos(diagnostic.Range.Start)) if node.Type() == "text" && childNode.Type() == "text" { logger.Debug("Diagnostic", diagnostic) diff --git a/internal/adapter/yamlls/diagnostics_integration_test.go b/internal/adapter/yamlls/diagnostics_integration_test.go index 345053e9..65237db0 100644 --- a/internal/adapter/yamlls/diagnostics_integration_test.go +++ b/internal/adapter/yamlls/diagnostics_integration_test.go @@ -11,7 +11,7 @@ import ( "testing" "time" - lsplocal "github.com/mrjosh/helm-ls/internal/lsp" + "github.com/mrjosh/helm-ls/internal/lsp/document" "github.com/mrjosh/helm-ls/internal/util" "github.com/stretchr/testify/assert" "go.lsp.dev/protocol" @@ -44,7 +44,7 @@ func readTestFiles(dir string, channel chan<- string, doneChan chan<- int) { doneChan <- count } -func sendTestFilesToYamlls(documents *lsplocal.DocumentStore, yamllsConnector *Connector, +func sendTestFilesToYamlls(documents *document.DocumentStore, yamllsConnector *Connector, doneReadingFilesChan <-chan int, doneSendingFilesChan chan<- int, filesChan <-chan string, diff --git a/internal/adapter/yamlls/document_sync.go b/internal/adapter/yamlls/document_sync.go index 38a932e6..a6d1583d 100644 --- a/internal/adapter/yamlls/document_sync.go +++ b/internal/adapter/yamlls/document_sync.go @@ -4,12 +4,13 @@ import ( "context" lsplocal "github.com/mrjosh/helm-ls/internal/lsp" + "github.com/mrjosh/helm-ls/internal/lsp/document" "github.com/mrjosh/helm-ls/internal/util" sitter "github.com/smacker/go-tree-sitter" lsp "go.lsp.dev/protocol" ) -func (yamllsConnector Connector) InitiallySyncOpenDocuments(docs []*lsplocal.TemplateDocument) { +func (yamllsConnector Connector) InitiallySyncOpenDocuments(docs []*document.TemplateDocument) { if yamllsConnector.server == nil { return } @@ -48,7 +49,7 @@ func (yamllsConnector Connector) DocumentDidOpen(ast *sitter.Tree, params lsp.Di } } -func (yamllsConnector Connector) DocumentDidSave(doc *lsplocal.TemplateDocument, params lsp.DidSaveTextDocumentParams) { +func (yamllsConnector Connector) DocumentDidSave(doc *document.TemplateDocument, params lsp.DidSaveTextDocumentParams) { if !yamllsConnector.shouldRun(doc.URI) { return } @@ -67,7 +68,7 @@ func (yamllsConnector Connector) DocumentDidSave(doc *lsplocal.TemplateDocument, }) } -func (yamllsConnector Connector) DocumentDidChange(doc *lsplocal.TemplateDocument, params lsp.DidChangeTextDocumentParams) { +func (yamllsConnector Connector) DocumentDidChange(doc *document.TemplateDocument, params lsp.DidChangeTextDocumentParams) { if !yamllsConnector.shouldRun(doc.URI) { return } @@ -96,7 +97,7 @@ func (yamllsConnector Connector) DocumentDidChange(doc *lsplocal.TemplateDocumen } } -func (yamllsConnector Connector) DocumentDidChangeFullSync(doc *lsplocal.TemplateDocument, params lsp.DidChangeTextDocumentParams) { +func (yamllsConnector Connector) DocumentDidChangeFullSync(doc *document.TemplateDocument, params lsp.DidChangeTextDocumentParams) { if !yamllsConnector.shouldRun(doc.URI) { return } diff --git a/internal/adapter/yamlls/integration_test_utils.go b/internal/adapter/yamlls/integration_test_utils.go index 93ec06c8..9bca680a 100644 --- a/internal/adapter/yamlls/integration_test_utils.go +++ b/internal/adapter/yamlls/integration_test_utils.go @@ -9,7 +9,7 @@ import ( "strings" "testing" - lsplocal "github.com/mrjosh/helm-ls/internal/lsp" + "github.com/mrjosh/helm-ls/internal/lsp/document" "github.com/mrjosh/helm-ls/internal/util" "go.lsp.dev/jsonrpc2" "go.lsp.dev/protocol" @@ -47,9 +47,9 @@ func (proc readWriteCloseMock) Close() error { return nil } -func getYamlLsConnector(t *testing.T, config util.YamllsConfiguration) (*Connector, *lsplocal.DocumentStore, chan lsp.PublishDiagnosticsParams) { +func getYamlLsConnector(t *testing.T, config util.YamllsConfiguration) (*Connector, *document.DocumentStore, chan lsp.PublishDiagnosticsParams) { dir := t.TempDir() - documents := lsplocal.NewDocumentStore() + documents := document.NewDocumentStore() diagnosticsChan := make(chan lsp.PublishDiagnosticsParams) con := jsonrpc2.NewConn(jsonrpc2.NewStream(readWriteCloseMock{diagnosticsChan})) zapLogger, _ := zap.NewProduction() @@ -66,7 +66,7 @@ func getYamlLsConnector(t *testing.T, config util.YamllsConfiguration) (*Connect return yamllsConnector, documents, diagnosticsChan } -func openFile(t *testing.T, documents *lsplocal.DocumentStore, path string, yamllsConnector *Connector) { +func openFile(t *testing.T, documents *document.DocumentStore, path string, yamllsConnector *Connector) { fileURI := uri.File(path) content, err := os.ReadFile(path) diff --git a/internal/adapter/yamlls/yamlls.go b/internal/adapter/yamlls/yamlls.go index 759d4401..3b90c434 100644 --- a/internal/adapter/yamlls/yamlls.go +++ b/internal/adapter/yamlls/yamlls.go @@ -8,7 +8,7 @@ import ( "github.com/gobwas/glob" "github.com/mrjosh/helm-ls/internal/log" - lsplocal "github.com/mrjosh/helm-ls/internal/lsp" + "github.com/mrjosh/helm-ls/internal/lsp/document" "github.com/mrjosh/helm-ls/internal/util" "go.lsp.dev/jsonrpc2" "go.lsp.dev/protocol" @@ -21,12 +21,12 @@ var logger = log.GetLogger() type Connector struct { config util.YamllsConfiguration server protocol.Server - documents *lsplocal.DocumentStore + documents *document.DocumentStore client protocol.Client EnabledForFilesGlobObject glob.Glob } -func NewConnector(ctx context.Context, yamllsConfiguration util.YamllsConfiguration, client protocol.Client, documents *lsplocal.DocumentStore) *Connector { +func NewConnector(ctx context.Context, yamllsConfiguration util.YamllsConfiguration, client protocol.Client, documents *document.DocumentStore) *Connector { yamllsCmd := exec.Command(yamllsConfiguration.Path, "--stdio") stdin, err := yamllsCmd.StdinPipe() diff --git a/internal/adapter/yamlls/yamlls_test.go b/internal/adapter/yamlls/yamlls_test.go index cfe6c50a..9b82b28f 100644 --- a/internal/adapter/yamlls/yamlls_test.go +++ b/internal/adapter/yamlls/yamlls_test.go @@ -4,7 +4,7 @@ import ( "os" "testing" - lsplocal "github.com/mrjosh/helm-ls/internal/lsp" + "github.com/mrjosh/helm-ls/internal/lsp/document" "github.com/mrjosh/helm-ls/internal/util" "github.com/stretchr/testify/assert" "go.lsp.dev/uri" @@ -17,7 +17,7 @@ func TestIsRelevantFile(t *testing.T) { }, } - connector.documents = lsplocal.NewDocumentStore() + connector.documents = document.NewDocumentStore() yamlFile := "../../../testdata/example/templates/deployment.yaml" nonYamlFile := "../../../testdata/example/templates/_helpers.tpl" diff --git a/internal/handler/handler.go b/internal/handler/handler.go index 6c41103f..fa2e08c9 100644 --- a/internal/handler/handler.go +++ b/internal/handler/handler.go @@ -7,7 +7,7 @@ import ( "github.com/mrjosh/helm-ls/internal/charts" templatehandler "github.com/mrjosh/helm-ls/internal/handler/template_handler" yamlhandler "github.com/mrjosh/helm-ls/internal/handler/yaml_handler" - lsplocal "github.com/mrjosh/helm-ls/internal/lsp" + "github.com/mrjosh/helm-ls/internal/lsp/document" "github.com/mrjosh/helm-ls/internal/util" "go.lsp.dev/jsonrpc2" "go.lsp.dev/protocol" @@ -23,10 +23,10 @@ type ServerHandler struct { client protocol.Client connPool jsonrpc2.Conn linterName string - documents *lsplocal.DocumentStore + documents *document.DocumentStore chartStore *charts.ChartStore helmlsConfig util.HelmlsConfiguration - langHandlers map[lsplocal.DocumentType]LangHandler + langHandlers map[document.DocumentType]LangHandler } func StartHandler(stream io.ReadWriteCloser) { @@ -45,7 +45,7 @@ func StartHandler(stream io.ReadWriteCloser) { } func newHandler(connPool jsonrpc2.Conn, client protocol.Client) *ServerHandler { - documents := lsplocal.NewDocumentStore() + documents := document.NewDocumentStore() var x LangHandler = yamlhandler.NewYamlHandler(client, documents, nil) handler := &ServerHandler{ client: client, @@ -53,9 +53,9 @@ func newHandler(connPool jsonrpc2.Conn, client protocol.Client) *ServerHandler { connPool: connPool, documents: documents, helmlsConfig: util.DefaultConfig, - langHandlers: map[lsplocal.DocumentType]LangHandler{ - lsplocal.TemplateDocumentType: templatehandler.NewTemplateHandler(client, documents, nil), - lsplocal.YamlDocumentType: x, + langHandlers: map[document.DocumentType]LangHandler{ + document.TemplateDocumentType: templatehandler.NewTemplateHandler(client, documents, nil), + document.YamlDocumentType: x, }, } logger.Printf("helm-lint-langserver: connections opened") diff --git a/internal/handler/template_handler/completion.go b/internal/handler/template_handler/completion.go index 4dba2592..64c3ab81 100644 --- a/internal/handler/template_handler/completion.go +++ b/internal/handler/template_handler/completion.go @@ -4,7 +4,7 @@ import ( "context" languagefeatures "github.com/mrjosh/helm-ls/internal/language_features" - lsplocal "github.com/mrjosh/helm-ls/internal/lsp" + templateast "github.com/mrjosh/helm-ls/internal/lsp/template_ast" "github.com/mrjosh/helm-ls/internal/protocol" lsp "go.lsp.dev/protocol" @@ -14,7 +14,7 @@ import ( func (h *TemplateHandler) Completion(ctx context.Context, params *lsp.CompletionParams) (result *lsp.CompletionList, err error) { logger.Debug("Running completion with params", params) - genericDocumentUseCase, err := h.NewGenericDocumentUseCase(params.TextDocumentPositionParams, lsplocal.NestedNodeAtPositionForCompletion) + genericDocumentUseCase, err := h.NewGenericDocumentUseCase(params.TextDocumentPositionParams, templateast.NestedNodeAtPositionForCompletion) if err != nil { return nil, err } diff --git a/internal/handler/template_handler/completion_main_test.go b/internal/handler/template_handler/completion_main_test.go index 2107e94a..5ab94e5a 100644 --- a/internal/handler/template_handler/completion_main_test.go +++ b/internal/handler/template_handler/completion_main_test.go @@ -9,7 +9,7 @@ import ( "github.com/mrjosh/helm-ls/internal/adapter/yamlls" "github.com/mrjosh/helm-ls/internal/charts" helmdocs "github.com/mrjosh/helm-ls/internal/documentation/helm" - lsplocal "github.com/mrjosh/helm-ls/internal/lsp" + "github.com/mrjosh/helm-ls/internal/lsp/document" "github.com/mrjosh/helm-ls/internal/util" "github.com/stretchr/testify/assert" lsp "go.lsp.dev/protocol" @@ -216,7 +216,7 @@ func TestCompletionMainSingleLines(t *testing.T) { } func completionTestCall(fileURI uri.URI, buf string, pos lsp.Position) (*lsp.CompletionList, error) { - documents := lsplocal.NewDocumentStore() + documents := document.NewDocumentStore() d := lsp.DidOpenTextDocumentParams{ TextDocument: lsp.TextDocumentItem{ URI: fileURI, diff --git a/internal/handler/template_handler/definition.go b/internal/handler/template_handler/definition.go index 53d5fdb9..4a0485a6 100644 --- a/internal/handler/template_handler/definition.go +++ b/internal/handler/template_handler/definition.go @@ -4,12 +4,12 @@ import ( "context" languagefeatures "github.com/mrjosh/helm-ls/internal/language_features" - lsplocal "github.com/mrjosh/helm-ls/internal/lsp" + templateast "github.com/mrjosh/helm-ls/internal/lsp/template_ast" lsp "go.lsp.dev/protocol" ) func (h *TemplateHandler) Definition(_ context.Context, params *lsp.DefinitionParams) (result []lsp.Location, err error) { - genericDocumentUseCase, err := h.NewGenericDocumentUseCase(params.TextDocumentPositionParams, lsplocal.NodeAtPosition) + genericDocumentUseCase, err := h.NewGenericDocumentUseCase(params.TextDocumentPositionParams, templateast.NodeAtPosition) if err != nil { return nil, err } diff --git a/internal/handler/template_handler/definition_chart_test.go b/internal/handler/template_handler/definition_chart_test.go index 401901c4..36c09b2c 100644 --- a/internal/handler/template_handler/definition_chart_test.go +++ b/internal/handler/template_handler/definition_chart_test.go @@ -10,7 +10,7 @@ import ( "github.com/mrjosh/helm-ls/internal/adapter/yamlls" "github.com/mrjosh/helm-ls/internal/charts" - lsplocal "github.com/mrjosh/helm-ls/internal/lsp" + "github.com/mrjosh/helm-ls/internal/lsp/document" "github.com/mrjosh/helm-ls/internal/util" "github.com/stretchr/testify/assert" lsp "go.lsp.dev/protocol" @@ -160,7 +160,7 @@ func TestDefinitionChart(t *testing.T) { t.Fatal(fmt.Sprintf("%s is not in the file %s", tc.templateLineWithMarker, fileURI.Filename())) } - documents := lsplocal.NewDocumentStore() + documents := document.NewDocumentStore() chart := charts.NewChart(rootUri, util.DefaultConfig.ValuesFilesConfig) diff --git a/internal/handler/template_handler/definition_test.go b/internal/handler/template_handler/definition_test.go index 3d4c4704..fa1d8b12 100644 --- a/internal/handler/template_handler/definition_test.go +++ b/internal/handler/template_handler/definition_test.go @@ -8,7 +8,7 @@ import ( "github.com/mrjosh/helm-ls/internal/adapter/yamlls" "github.com/mrjosh/helm-ls/internal/charts" - lsplocal "github.com/mrjosh/helm-ls/internal/lsp" + "github.com/mrjosh/helm-ls/internal/lsp/document" "github.com/mrjosh/helm-ls/internal/util" "github.com/stretchr/testify/assert" "go.lsp.dev/protocol" @@ -54,7 +54,7 @@ func genericDefinitionTest(t *testing.T, position lsp.Position, expectedLocation t.Fatal(err) } - documents := lsplocal.NewDocumentStore() + documents := document.NewDocumentStore() fileURI := testDocumentTemplateURI rootUri := uri.File("/") @@ -244,7 +244,7 @@ func genericDefinitionTestMultipleValuesFiles(t *testing.T, position lsp.Positio if err != nil { t.Fatal(err) } - documents := lsplocal.NewDocumentStore() + documents := document.NewDocumentStore() fileURI := testDocumentTemplateURI rootUri := uri.File("/") @@ -356,7 +356,7 @@ func TestDefinitionSingleLine(t *testing.T) { expectedColEnd := strings.Index(buf, "§") buf = strings.Replace(buf, "§", "", 1) - documents := lsplocal.NewDocumentStore() + documents := document.NewDocumentStore() fileURI := testDocumentTemplateURI rootUri := uri.File("/") diff --git a/internal/handler/template_handler/hover.go b/internal/handler/template_handler/hover.go index 7a256255..8a33a87f 100644 --- a/internal/handler/template_handler/hover.go +++ b/internal/handler/template_handler/hover.go @@ -4,7 +4,7 @@ import ( "context" languagefeatures "github.com/mrjosh/helm-ls/internal/language_features" - lsplocal "github.com/mrjosh/helm-ls/internal/lsp" + templateast "github.com/mrjosh/helm-ls/internal/lsp/template_ast" "github.com/mrjosh/helm-ls/internal/protocol" "github.com/mrjosh/helm-ls/internal/tree-sitter/gotemplate" @@ -12,12 +12,12 @@ import ( ) func (h *TemplateHandler) Hover(ctx context.Context, params *lsp.HoverParams) (result *lsp.Hover, err error) { - genericDocumentUseCase, err := h.NewGenericDocumentUseCase(params.TextDocumentPositionParams, lsplocal.NodeAtPosition) + genericDocumentUseCase, err := h.NewGenericDocumentUseCase(params.TextDocumentPositionParams, templateast.NodeAtPosition) if err != nil { return nil, err } - wordRange := lsplocal.GetLspRangeForNode(genericDocumentUseCase.Node) + wordRange := templateast.GetLspRangeForNode(genericDocumentUseCase.Node) usecases := []languagefeatures.HoverUseCase{ languagefeatures.NewBuiltInObjectsFeature(genericDocumentUseCase), // has to be before template context diff --git a/internal/handler/template_handler/hover_main_test.go b/internal/handler/template_handler/hover_main_test.go index ca5dfa6b..69743003 100644 --- a/internal/handler/template_handler/hover_main_test.go +++ b/internal/handler/template_handler/hover_main_test.go @@ -10,7 +10,7 @@ import ( "github.com/mrjosh/helm-ls/internal/adapter/yamlls" "github.com/mrjosh/helm-ls/internal/charts" - lsplocal "github.com/mrjosh/helm-ls/internal/lsp" + "github.com/mrjosh/helm-ls/internal/lsp/document" "github.com/mrjosh/helm-ls/internal/util" "github.com/stretchr/testify/assert" lsp "go.lsp.dev/protocol" @@ -165,7 +165,7 @@ func TestHoverMain(t *testing.T) { } for _, tt := range testCases { t.Run(tt.desc, func(t *testing.T) { - documents := lsplocal.NewDocumentStore() + documents := document.NewDocumentStore() path := "../../../testdata/example/templates/deployment.yaml" fileURI := uri.File(path) diff --git a/internal/handler/template_handler/references.go b/internal/handler/template_handler/references.go index 8869966e..486fdd24 100644 --- a/internal/handler/template_handler/references.go +++ b/internal/handler/template_handler/references.go @@ -4,12 +4,12 @@ import ( "context" languagefeatures "github.com/mrjosh/helm-ls/internal/language_features" - lsplocal "github.com/mrjosh/helm-ls/internal/lsp" + templateast "github.com/mrjosh/helm-ls/internal/lsp/template_ast" lsp "go.lsp.dev/protocol" ) func (h *TemplateHandler) References(_ context.Context, params *lsp.ReferenceParams) (result []lsp.Location, err error) { - genericDocumentUseCase, err := h.NewGenericDocumentUseCase(params.TextDocumentPositionParams, lsplocal.NodeAtPosition) + genericDocumentUseCase, err := h.NewGenericDocumentUseCase(params.TextDocumentPositionParams, templateast.NodeAtPosition) if err != nil { return nil, err } diff --git a/internal/handler/template_handler/references_test.go b/internal/handler/template_handler/references_test.go index 3daa783f..42bfff35 100644 --- a/internal/handler/template_handler/references_test.go +++ b/internal/handler/template_handler/references_test.go @@ -7,7 +7,7 @@ import ( "github.com/mrjosh/helm-ls/internal/adapter/yamlls" "github.com/mrjosh/helm-ls/internal/charts" - lsplocal "github.com/mrjosh/helm-ls/internal/lsp" + "github.com/mrjosh/helm-ls/internal/lsp/document" "github.com/mrjosh/helm-ls/internal/util" "github.com/stretchr/testify/assert" "go.lsp.dev/protocol" @@ -81,7 +81,7 @@ func TestRefercesTemplateContext(t *testing.T) { for _, tt := range testCases { t.Run(tt.desc, func(t *testing.T) { - documents := lsplocal.NewDocumentStore() + documents := document.NewDocumentStore() path := "/tmp/testfile.yaml" fileURI := uri.File(path) @@ -143,7 +143,7 @@ func TestRefercesTemplateContextWithTestFile(t *testing.T) { for _, tt := range testCases { t.Run(tt.desc, func(t *testing.T) { - documents := lsplocal.NewDocumentStore() + documents := document.NewDocumentStore() path := "../../../testdata/example/templates/deployment.yaml" fileURI := uri.File(path) @@ -207,7 +207,7 @@ func TestRefercesSingleLines(t *testing.T) { for _, tt := range testCases { t.Run(tt.templateWithMark, func(t *testing.T) { - documents := lsplocal.NewDocumentStore() + documents := document.NewDocumentStore() pos, buf := getPositionForMarkedTestLine(tt.templateWithMark) fileURI := uri.File("fake-testfile.yaml") diff --git a/internal/handler/template_handler/template_handler.go b/internal/handler/template_handler/template_handler.go index 377ad5db..df4204a6 100644 --- a/internal/handler/template_handler/template_handler.go +++ b/internal/handler/template_handler/template_handler.go @@ -4,7 +4,7 @@ import ( "github.com/mrjosh/helm-ls/internal/adapter/yamlls" "github.com/mrjosh/helm-ls/internal/charts" "github.com/mrjosh/helm-ls/internal/log" - lsplocal "github.com/mrjosh/helm-ls/internal/lsp" + "github.com/mrjosh/helm-ls/internal/lsp/document" "go.lsp.dev/protocol" ) @@ -12,12 +12,12 @@ var logger = log.GetLogger() type TemplateHandler struct { client protocol.Client - documents *lsplocal.DocumentStore + documents *document.DocumentStore chartStore *charts.ChartStore yamllsConnector *yamlls.Connector } -func NewTemplateHandler(client protocol.Client, documents *lsplocal.DocumentStore, chartStore *charts.ChartStore) *TemplateHandler { +func NewTemplateHandler(client protocol.Client, documents *document.DocumentStore, chartStore *charts.ChartStore) *TemplateHandler { return &TemplateHandler{ client: client, documents: documents, diff --git a/internal/handler/template_handler/text_document.go b/internal/handler/template_handler/text_document.go index bc660d0e..a823c398 100644 --- a/internal/handler/template_handler/text_document.go +++ b/internal/handler/template_handler/text_document.go @@ -4,7 +4,7 @@ import ( "context" "errors" - lsplocal "github.com/mrjosh/helm-ls/internal/lsp" + templateast "github.com/mrjosh/helm-ls/internal/lsp/template_ast" "github.com/mrjosh/helm-ls/internal/util" lsp "go.lsp.dev/protocol" ) @@ -40,7 +40,7 @@ func (h *TemplateHandler) PostDidChange(ctx context.Context, params *lsp.DidChan shouldSendFullUpdateToYamlls := false for _, change := range params.ContentChanges { - node := lsplocal.NodeAtPosition(doc.Ast, change.Range.Start) + node := templateast.NodeAtPosition(doc.Ast, change.Range.Start) if node.Type() != "text" { shouldSendFullUpdateToYamlls = true break diff --git a/internal/handler/text_document.go b/internal/handler/text_document.go index e4bdcc52..7a37fda6 100644 --- a/internal/handler/text_document.go +++ b/internal/handler/text_document.go @@ -5,12 +5,12 @@ import ( "errors" "github.com/mrjosh/helm-ls/internal/charts" - lsplocal "github.com/mrjosh/helm-ls/internal/lsp" + "github.com/mrjosh/helm-ls/internal/lsp/document" lsp "go.lsp.dev/protocol" ) func (h *ServerHandler) DidOpen(ctx context.Context, params *lsp.DidOpenTextDocumentParams) (err error) { - handler := h.langHandlers[lsplocal.TemplateDocumentTypeForLangID(params.TextDocument.LanguageID)] + handler := h.langHandlers[document.TemplateDocumentTypeForLangID(params.TextDocument.LanguageID)] if handler == nil { message := "Language not supported: " + string(params.TextDocument.LanguageID) diff --git a/internal/handler/text_document_test.go b/internal/handler/text_document_test.go index 4771f843..c7b3ed6b 100644 --- a/internal/handler/text_document_test.go +++ b/internal/handler/text_document_test.go @@ -6,7 +6,7 @@ import ( "testing" "github.com/mrjosh/helm-ls/internal/charts" - lsplocal "github.com/mrjosh/helm-ls/internal/lsp" + "github.com/mrjosh/helm-ls/internal/lsp/document" "github.com/mrjosh/helm-ls/internal/util" "github.com/stretchr/testify/assert" lsp "go.lsp.dev/protocol" @@ -32,7 +32,7 @@ func TestLoadDocsOnNewChart(t *testing.T) { } h := &ServerHandler{ - documents: lsplocal.NewDocumentStore(), + documents: document.NewDocumentStore(), helmlsConfig: util.DefaultConfig, } @@ -59,7 +59,7 @@ func TestLoadDocsOnNewChartDoesNotOverwrite(t *testing.T) { err = os.WriteFile(templateFile, []byte("This is a template file"), 0o644) assert.NoError(t, err) - docs := lsplocal.NewDocumentStore() + docs := document.NewDocumentStore() h := &ServerHandler{ documents: docs, helmlsConfig: util.DefaultConfig, @@ -84,7 +84,7 @@ func TestLoadDocsOnNewChartWorksForMissingTemplateDir(t *testing.T) { tempDir := t.TempDir() rootURI := uri.File(tempDir) - docs := lsplocal.NewDocumentStore() + docs := document.NewDocumentStore() h := &ServerHandler{ documents: docs, helmlsConfig: util.DefaultConfig, diff --git a/internal/handler/yaml_handler/yaml_handler.go b/internal/handler/yaml_handler/yaml_handler.go index 5a77d4c9..f877da5b 100644 --- a/internal/handler/yaml_handler/yaml_handler.go +++ b/internal/handler/yaml_handler/yaml_handler.go @@ -6,7 +6,7 @@ import ( "github.com/mrjosh/helm-ls/internal/adapter/yamlls" "github.com/mrjosh/helm-ls/internal/charts" "github.com/mrjosh/helm-ls/internal/log" - lsplocal "github.com/mrjosh/helm-ls/internal/lsp" + "github.com/mrjosh/helm-ls/internal/lsp/document" "github.com/mrjosh/helm-ls/internal/util" "go.lsp.dev/protocol" ) @@ -14,7 +14,7 @@ import ( var logger = log.GetLogger() type YamlHandler struct { - documents *lsplocal.DocumentStore + documents *document.DocumentStore chartStore *charts.ChartStore yamllsConnector *yamlls.Connector } @@ -41,7 +41,7 @@ func (h *YamlHandler) References(ctx context.Context, params *protocol.Reference // SetClient implements handler.LangHandler. func (h *YamlHandler) SetClient(client protocol.Client) {} -func NewYamlHandler(client protocol.Client, documents *lsplocal.DocumentStore, chartStore *charts.ChartStore) *YamlHandler { +func NewYamlHandler(client protocol.Client, documents *document.DocumentStore, chartStore *charts.ChartStore) *YamlHandler { return &YamlHandler{ documents: documents, chartStore: chartStore, diff --git a/internal/helm_lint/lint.go b/internal/helm_lint/lint.go index 50a4c47d..e7bd3f53 100644 --- a/internal/helm_lint/lint.go +++ b/internal/helm_lint/lint.go @@ -8,7 +8,7 @@ import ( "github.com/mrjosh/helm-ls/internal/charts" "github.com/mrjosh/helm-ls/internal/log" - lsplocal "github.com/mrjosh/helm-ls/internal/lsp" + "github.com/mrjosh/helm-ls/internal/lsp/document" "github.com/mrjosh/helm-ls/internal/util" lsp "go.lsp.dev/protocol" "go.lsp.dev/uri" @@ -19,7 +19,7 @@ import ( var logger = log.GetLogger() -func GetDiagnosticsNotifications(chart *charts.Chart, doc *lsplocal.TemplateDocument) []lsp.PublishDiagnosticsParams { +func GetDiagnosticsNotifications(chart *charts.Chart, doc *document.TemplateDocument) []lsp.PublishDiagnosticsParams { vals := chart.ValuesFiles.MainValuesFile.Values if chart.ValuesFiles.OverlayValuesFile != nil { vals = chartutil.CoalesceTables(chart.ValuesFiles.OverlayValuesFile.Values, chart.ValuesFiles.MainValuesFile.Values) diff --git a/internal/helm_lint/lint_test.go b/internal/helm_lint/lint_test.go index 9cf4fb71..111529dc 100644 --- a/internal/helm_lint/lint_test.go +++ b/internal/helm_lint/lint_test.go @@ -4,7 +4,7 @@ import ( "testing" "github.com/mrjosh/helm-ls/internal/charts" - lsplocal "github.com/mrjosh/helm-ls/internal/lsp" + "github.com/mrjosh/helm-ls/internal/lsp/document" "github.com/stretchr/testify/assert" "go.lsp.dev/uri" "helm.sh/helm/v3/pkg/chartutil" @@ -26,8 +26,8 @@ func TestLintNotifications(t *testing.T) { AdditionalValuesFiles: []*charts.ValuesFile{}, }, } - diagnostics := GetDiagnosticsNotifications(&chart, &lsplocal.TemplateDocument{ - Document: lsplocal.Document{ + diagnostics := GetDiagnosticsNotifications(&chart, &document.TemplateDocument{ + Document: document.Document{ URI: uri.File("../../testdata/example/templates/deployment-no-templates.yaml"), }, }) @@ -55,8 +55,8 @@ func TestLintNotificationsIncludesEmptyDiagnosticsForFixedIssues(t *testing.T) { AdditionalValuesFiles: []*charts.ValuesFile{}, }, } - diagnostics := GetDiagnosticsNotifications(&chart, &lsplocal.TemplateDocument{ - Document: lsplocal.Document{URI: uri.File("../../testdata/example/templates/deployment-no-templates.yaml")}, + diagnostics := GetDiagnosticsNotifications(&chart, &document.TemplateDocument{ + Document: document.Document{URI: uri.File("../../testdata/example/templates/deployment-no-templates.yaml")}, }, ) diff --git a/internal/language_features/built_in_objects.go b/internal/language_features/built_in_objects.go index 3acc79ff..247fd2a0 100644 --- a/internal/language_features/built_in_objects.go +++ b/internal/language_features/built_in_objects.go @@ -2,7 +2,7 @@ package languagefeatures import ( helmdocs "github.com/mrjosh/helm-ls/internal/documentation/helm" - lsplocal "github.com/mrjosh/helm-ls/internal/lsp" + symboltable "github.com/mrjosh/helm-ls/internal/lsp/symbol_table" "github.com/mrjosh/helm-ls/internal/tree-sitter/gotemplate" lsp "go.lsp.dev/protocol" ) @@ -51,7 +51,7 @@ func (f *BuiltInObjectsFeature) References() (result []lsp.Location, err error) return append(locations, f.getDefinitionLocations(templateContext)...), err } -func (f *BuiltInObjectsFeature) getDefinitionLocations(templateContext lsplocal.TemplateContext) []lsp.Location { +func (f *BuiltInObjectsFeature) getDefinitionLocations(templateContext symboltable.TemplateContext) []lsp.Location { locations := []lsp.Location{} switch templateContext[0] { diff --git a/internal/language_features/generic_document_usecase.go b/internal/language_features/generic_document_usecase.go index 3943cd98..abcb4130 100644 --- a/internal/language_features/generic_document_usecase.go +++ b/internal/language_features/generic_document_usecase.go @@ -2,13 +2,13 @@ package languagefeatures import ( "github.com/mrjosh/helm-ls/internal/charts" - lsplocal "github.com/mrjosh/helm-ls/internal/lsp" + "github.com/mrjosh/helm-ls/internal/lsp/document" sitter "github.com/smacker/go-tree-sitter" ) type GenericDocumentUseCase struct { - Document *lsplocal.TemplateDocument - DocumentStore *lsplocal.DocumentStore + Document *document.TemplateDocument + DocumentStore *document.DocumentStore Chart *charts.Chart Node *sitter.Node ChartStore *charts.ChartStore diff --git a/internal/language_features/generic_template_context.go b/internal/language_features/generic_template_context.go index d0704a87..10d73d08 100644 --- a/internal/language_features/generic_template_context.go +++ b/internal/language_features/generic_template_context.go @@ -4,7 +4,7 @@ import ( "fmt" helmdocs "github.com/mrjosh/helm-ls/internal/documentation/helm" - lsplocal "github.com/mrjosh/helm-ls/internal/lsp" + symboltable "github.com/mrjosh/helm-ls/internal/lsp/symbol_table" "github.com/mrjosh/helm-ls/internal/util" lsp "go.lsp.dev/protocol" ) @@ -13,11 +13,11 @@ type GenericTemplateContextFeature struct { *GenericDocumentUseCase } -func (f *GenericTemplateContextFeature) getTemplateContext() (lsplocal.TemplateContext, error) { - return f.GenericDocumentUseCase.Document.SymbolTable.GetTemplateContext(lsplocal.GetRangeForNode(f.Node)) +func (f *GenericTemplateContextFeature) getTemplateContext() (symboltable.TemplateContext, error) { + return f.GenericDocumentUseCase.Document.SymbolTable.GetTemplateContext(util.GetRangeForNode(f.Node)) } -func (f *GenericTemplateContextFeature) getReferencesFromSymbolTable(templateContext lsplocal.TemplateContext) []lsp.Location { +func (f *GenericTemplateContextFeature) getReferencesFromSymbolTable(templateContext symboltable.TemplateContext) []lsp.Location { locations := []lsp.Location{} for _, doc := range f.GenericDocumentUseCase.DocumentStore.GetAllTemplateDocs() { diff --git a/internal/language_features/includes.go b/internal/language_features/includes.go index ebb592d5..22fbac8d 100644 --- a/internal/language_features/includes.go +++ b/internal/language_features/includes.go @@ -4,7 +4,7 @@ import ( lsp "go.lsp.dev/protocol" "github.com/mrjosh/helm-ls/internal/charts" - lsplocal "github.com/mrjosh/helm-ls/internal/lsp" + symboltable "github.com/mrjosh/helm-ls/internal/lsp/symbol_table" "github.com/mrjosh/helm-ls/internal/protocol" "github.com/mrjosh/helm-ls/internal/tree-sitter/gotemplate" "github.com/mrjosh/helm-ls/internal/util" @@ -26,7 +26,7 @@ func (f *IncludesCallFeature) AppropriateForNode() bool { if functionCallNode == nil { return false } - _, err := lsplocal.ParseIncludeFunctionCall(functionCallNode, []byte(f.GenericDocumentUseCase.Document.Content)) + _, err := symboltable.ParseIncludeFunctionCall(functionCallNode, []byte(f.GenericDocumentUseCase.Document.Content)) return err == nil } @@ -76,7 +76,7 @@ func (f *IncludesCallFeature) References() (result []lsp.Location, err error) { func (f *IncludesCallFeature) getIncludeName() (string, error) { functionCallNode := f.getFunctionCallNode() - return lsplocal.ParseIncludeFunctionCall(functionCallNode, []byte(f.GenericDocumentUseCase.Document.Content)) + return symboltable.ParseIncludeFunctionCall(functionCallNode, []byte(f.GenericDocumentUseCase.Document.Content)) } func (f *IncludesDefinitionFeature) References() (result []lsp.Location, err error) { diff --git a/internal/language_features/template_context.go b/internal/language_features/template_context.go index 38e6dbf2..78eddec1 100644 --- a/internal/language_features/template_context.go +++ b/internal/language_features/template_context.go @@ -9,7 +9,7 @@ import ( "github.com/mrjosh/helm-ls/internal/charts" helmdocs "github.com/mrjosh/helm-ls/internal/documentation/helm" - lsplocal "github.com/mrjosh/helm-ls/internal/lsp" + symboltable "github.com/mrjosh/helm-ls/internal/lsp/symbol_table" "github.com/mrjosh/helm-ls/internal/protocol" "github.com/mrjosh/helm-ls/internal/tree-sitter/gotemplate" "github.com/mrjosh/helm-ls/internal/util" @@ -53,7 +53,7 @@ func (f *TemplateContextFeature) Definition() (result []lsp.Location, err error) return f.getDefinitionLocations(templateContext), nil } -func (f *TemplateContextFeature) getReferenceLocations(templateContext lsplocal.TemplateContext) []lsp.Location { +func (f *TemplateContextFeature) getReferenceLocations(templateContext symboltable.TemplateContext) []lsp.Location { locations := []lsp.Location{} for _, doc := range f.GenericDocumentUseCase.DocumentStore.GetAllTemplateDocs() { referenceRanges := doc.SymbolTable.GetTemplateContextRanges(templateContext) @@ -65,7 +65,7 @@ func (f *TemplateContextFeature) getReferenceLocations(templateContext lsplocal. return append(locations, f.getDefinitionLocations(templateContext)...) } -func (f *TemplateContextFeature) getDefinitionLocations(templateContext lsplocal.TemplateContext) []lsp.Location { +func (f *TemplateContextFeature) getDefinitionLocations(templateContext symboltable.TemplateContext) []lsp.Location { locations := []lsp.Location{} switch templateContext[0] { @@ -108,7 +108,7 @@ func (f *TemplateContextFeature) Hover() (string, error) { return templateContext.Format(), err } -func (f *TemplateContextFeature) valuesHover(templateContext lsplocal.TemplateContext) (string, error) { +func (f *TemplateContextFeature) valuesHover(templateContext symboltable.TemplateContext) (string, error) { var ( valuesFiles = f.Chart.ResolveValueFiles(templateContext, f.ChartStore) hoverResults = protocol.HoverResultsWithFiles{} diff --git a/internal/language_features/template_context_completion.go b/internal/language_features/template_context_completion.go index 815539d1..ede89e35 100644 --- a/internal/language_features/template_context_completion.go +++ b/internal/language_features/template_context_completion.go @@ -5,7 +5,7 @@ import ( "strings" helmdocs "github.com/mrjosh/helm-ls/internal/documentation/helm" - lsplocal "github.com/mrjosh/helm-ls/internal/lsp" + symboltable "github.com/mrjosh/helm-ls/internal/lsp/symbol_table" "github.com/mrjosh/helm-ls/internal/protocol" "github.com/mrjosh/helm-ls/internal/util" lsp "go.lsp.dev/protocol" @@ -44,7 +44,7 @@ func (f *TemplateContextFeature) Completion() (*lsp.CompletionList, error) { } // handels the completion for .Capabilities.KubeVersion.^ where the result should not contain KubeVersion again -func trimPrefixForNestedDocs(nestedDocs []helmdocs.HelmDocumentation, templateContext lsplocal.TemplateContext) []helmdocs.HelmDocumentation { +func trimPrefixForNestedDocs(nestedDocs []helmdocs.HelmDocumentation, templateContext symboltable.TemplateContext) []helmdocs.HelmDocumentation { adjustedDocs := []helmdocs.HelmDocumentation{} for _, v := range nestedDocs { if strings.HasPrefix(v.Name, templateContext.Tail().Format()) { @@ -55,7 +55,7 @@ func trimPrefixForNestedDocs(nestedDocs []helmdocs.HelmDocumentation, templateCo return adjustedDocs } -func (f *TemplateContextFeature) valuesCompletion(templateContext lsplocal.TemplateContext) (*lsp.CompletionList, error) { +func (f *TemplateContextFeature) valuesCompletion(templateContext symboltable.TemplateContext) (*lsp.CompletionList, error) { m := make(map[string]lsp.CompletionItem) for _, queriedValuesFiles := range f.Chart.ResolveValueFiles(templateContext.Tail(), f.ChartStore) { for _, valuesFile := range queriedValuesFiles.ValuesFiles.AllValuesFiles() { diff --git a/internal/lsp/ast_diagnostics_test.go b/internal/lsp/ast_diagnostics_test.go index c9183518..f22417d9 100644 --- a/internal/lsp/ast_diagnostics_test.go +++ b/internal/lsp/ast_diagnostics_test.go @@ -3,12 +3,13 @@ package lsp import ( "testing" + templateast "github.com/mrjosh/helm-ls/internal/lsp/template_ast" sitter "github.com/smacker/go-tree-sitter" ) func TestIsInElseBranch(t *testing.T) { template := `{{if pipeline}} t1 {{ else if pipeline }} t2 {{ else if pipeline2 }} t3 {{ else }} t4 {{ end }}` - ast := ParseAst(nil, []byte(template)) + ast := templateast.ParseAst(nil, []byte(template)) // (template [0, 0] - [1, 0] // (if_action [0, 0] - [0, 95] // condition: (function_call [0, 5] - [0, 13] @@ -22,8 +23,6 @@ func TestIsInElseBranch(t *testing.T) { // option: (text [0, 68] - [0, 71]) // alternative: (text [0, 82] - [0, 85]))) - logger.Println("RootNode:", ast.RootNode().String()) - t1_start := sitter.Point{Row: 0, Column: 16} t1 := ast.RootNode().NamedDescendantForPointRange(t1_start, t1_start) t2_start := sitter.Point{Row: 0, Column: 42} diff --git a/internal/lsp/ast_test.go b/internal/lsp/ast_test.go index 87718612..b6e17dd9 100644 --- a/internal/lsp/ast_test.go +++ b/internal/lsp/ast_test.go @@ -3,6 +3,7 @@ package lsp import ( "testing" + templateast "github.com/mrjosh/helm-ls/internal/lsp/template_ast" sitter "github.com/smacker/go-tree-sitter" "github.com/stretchr/testify/assert" ) @@ -16,11 +17,11 @@ func TestFindRelevantChildNodeCompletio(t *testing.T) { {{ .Chart.N }} {{ . }} ` - ast := ParseAst(nil, []byte(template)) + ast := templateast.ParseAst(nil, []byte(template)) logger.Println("RootNode:", ast.RootNode().String()) - node := FindRelevantChildNodeCompletion(ast.RootNode(), sitter.Point{ + node := templateast.FindRelevantChildNodeCompletion(ast.RootNode(), sitter.Point{ Row: 0, Column: 11, }) diff --git a/internal/lsp/ast_variable.go b/internal/lsp/ast_variable.go index f0fb2cfa..60dfca02 100644 --- a/internal/lsp/ast_variable.go +++ b/internal/lsp/ast_variable.go @@ -1,10 +1,13 @@ package lsp import ( + "github.com/mrjosh/helm-ls/internal/log" "github.com/mrjosh/helm-ls/internal/tree-sitter/gotemplate" sitter "github.com/smacker/go-tree-sitter" ) +var logger = log.GetLogger() + func GetVariableDefinition(variableName string, node *sitter.Node, template string) *sitter.Node { if node == nil { return nil diff --git a/internal/lsp/diagnostics_cache.go b/internal/lsp/document/diagnostics_cache.go similarity index 98% rename from internal/lsp/diagnostics_cache.go rename to internal/lsp/document/diagnostics_cache.go index 2dba0f25..23e48240 100644 --- a/internal/lsp/diagnostics_cache.go +++ b/internal/lsp/document/diagnostics_cache.go @@ -1,4 +1,4 @@ -package lsp +package document import ( "github.com/mrjosh/helm-ls/internal/util" @@ -42,7 +42,6 @@ func (d DiagnosticsCache) GetMergedDiagnostics() (merged []lsp.Diagnostic) { } func (d *DiagnosticsCache) ShouldShowDiagnosticsOnNewYamlDiagnostics() bool { - return d.yamlDiagnosticsCountReduced || // show the diagnostics when the count is reduced, this means an error was fixed and it should be shown to the user d.helmlsConfig.YamllsConfiguration.ShowDiagnosticsDirectly || // show the diagnostics directly when the user configured to show them d.gotYamlDiagnosticsTimes < 3 // show the diagnostics, when it are the initial diagnostics that are sent after opening a file. Initial diagnostics are sent twice from yamlls diff --git a/internal/lsp/diagnostics_cache_test.go b/internal/lsp/document/diagnostics_cache_test.go similarity index 99% rename from internal/lsp/diagnostics_cache_test.go rename to internal/lsp/document/diagnostics_cache_test.go index f2e83e1e..609dab25 100644 --- a/internal/lsp/diagnostics_cache_test.go +++ b/internal/lsp/document/diagnostics_cache_test.go @@ -1,11 +1,12 @@ -package lsp +package document import ( + "testing" + "github.com/mrjosh/helm-ls/internal/util" "github.com/stretchr/testify/assert" lsp "go.lsp.dev/protocol" - "testing" ) func TestDiagnosticsCache_SetYamlDiagnostics(t *testing.T) { diff --git a/internal/lsp/document.go b/internal/lsp/document/document.go similarity index 99% rename from internal/lsp/document.go rename to internal/lsp/document/document.go index 5db04425..5fff7deb 100644 --- a/internal/lsp/document.go +++ b/internal/lsp/document/document.go @@ -1,4 +1,4 @@ -package lsp +package document import ( "bytes" diff --git a/internal/lsp/document_store.go b/internal/lsp/document/document_store.go similarity index 99% rename from internal/lsp/document_store.go rename to internal/lsp/document/document_store.go index 23491d05..8ca1701a 100644 --- a/internal/lsp/document_store.go +++ b/internal/lsp/document/document_store.go @@ -1,4 +1,4 @@ -package lsp +package document import ( "fmt" diff --git a/internal/lsp/document_store_test.go b/internal/lsp/document/document_store_test.go similarity index 97% rename from internal/lsp/document_store_test.go rename to internal/lsp/document/document_store_test.go index 5149c65f..8fa9e9c3 100644 --- a/internal/lsp/document_store_test.go +++ b/internal/lsp/document/document_store_test.go @@ -1,4 +1,4 @@ -package lsp +package document import ( "testing" diff --git a/internal/lsp/document_test.go b/internal/lsp/document/document_test.go similarity index 97% rename from internal/lsp/document_test.go rename to internal/lsp/document/document_test.go index 27545d10..0b447a40 100644 --- a/internal/lsp/document_test.go +++ b/internal/lsp/document/document_test.go @@ -1,4 +1,4 @@ -package lsp +package document import ( "testing" diff --git a/internal/lsp/template_document.go b/internal/lsp/document/template_document.go similarity index 74% rename from internal/lsp/template_document.go rename to internal/lsp/document/template_document.go index 22f1ba7d..b5b6f32e 100644 --- a/internal/lsp/template_document.go +++ b/internal/lsp/document/template_document.go @@ -1,6 +1,8 @@ -package lsp +package document import ( + symboltable "github.com/mrjosh/helm-ls/internal/lsp/symbol_table" + templateast "github.com/mrjosh/helm-ls/internal/lsp/template_ast" "github.com/mrjosh/helm-ls/internal/util" sitter "github.com/smacker/go-tree-sitter" lsp "go.lsp.dev/protocol" @@ -13,7 +15,7 @@ type TemplateDocument struct { NeedsRefreshDiagnostics bool Ast *sitter.Tree DiagnosticsCache DiagnosticsCache - SymbolTable *SymbolTable + SymbolTable *symboltable.SymbolTable IsYaml bool } @@ -22,13 +24,13 @@ func (d *TemplateDocument) GetDocumentType() DocumentType { } func NewTemplateDocument(fileURI uri.URI, content []byte, isOpen bool, helmlsConfig util.HelmlsConfiguration) *TemplateDocument { - ast := ParseAst(nil, content) + ast := templateast.ParseAst(nil, content) return &TemplateDocument{ Document: *NewDocument(fileURI, content, isOpen), NeedsRefreshDiagnostics: false, Ast: ast, DiagnosticsCache: NewDiagnosticsCache(helmlsConfig), - SymbolTable: NewSymbolTable(ast, content), + SymbolTable: symboltable.NewSymbolTable(ast, content), IsYaml: IsYamllsEnabled(fileURI, helmlsConfig.YamllsConfiguration), } } @@ -38,7 +40,11 @@ func (d *TemplateDocument) ApplyChanges(changes []lsp.TextDocumentContentChangeE d.Document.ApplyChanges(changes) d.ApplyChangesToAst(d.Content) - d.SymbolTable = NewSymbolTable(d.Ast, d.Content) + d.SymbolTable = symboltable.NewSymbolTable(d.Ast, d.Content) +} + +func (d *TemplateDocument) ApplyChangesToAst(newContent []byte) { + d.Ast = templateast.ParseAst(nil, newContent) } func IsYamllsEnabled(uri lsp.URI, yamllsConfiguration util.YamllsConfiguration) bool { diff --git a/internal/lsp/yaml_document.go b/internal/lsp/document/yaml_document.go similarity index 98% rename from internal/lsp/yaml_document.go rename to internal/lsp/document/yaml_document.go index 62e8af1a..28433c55 100644 --- a/internal/lsp/yaml_document.go +++ b/internal/lsp/document/yaml_document.go @@ -1,4 +1,4 @@ -package lsp +package document import ( "github.com/mrjosh/helm-ls/internal/util" diff --git a/internal/lsp/yaml_document_test.go b/internal/lsp/document/yaml_document_test.go similarity index 96% rename from internal/lsp/yaml_document_test.go rename to internal/lsp/document/yaml_document_test.go index f69723ec..1d1539c7 100644 --- a/internal/lsp/yaml_document_test.go +++ b/internal/lsp/document/yaml_document_test.go @@ -1,4 +1,4 @@ -package lsp +package document import ( "testing" diff --git a/internal/lsp/symbol_table.go b/internal/lsp/symbol_table/symbol_table.go similarity index 97% rename from internal/lsp/symbol_table.go rename to internal/lsp/symbol_table/symbol_table.go index 1dd7e578..61b13e0a 100644 --- a/internal/lsp/symbol_table.go +++ b/internal/lsp/symbol_table/symbol_table.go @@ -1,12 +1,15 @@ -package lsp +package symboltable import ( "fmt" "strings" + "github.com/mrjosh/helm-ls/internal/log" sitter "github.com/smacker/go-tree-sitter" ) +var logger = log.GetLogger() + type TemplateContext []string func (t TemplateContext) Format() string { diff --git a/internal/lsp/symbol_table_includes.go b/internal/lsp/symbol_table/symbol_table_includes.go similarity index 94% rename from internal/lsp/symbol_table_includes.go rename to internal/lsp/symbol_table/symbol_table_includes.go index 22d6b794..44e40e30 100644 --- a/internal/lsp/symbol_table_includes.go +++ b/internal/lsp/symbol_table/symbol_table_includes.go @@ -1,4 +1,4 @@ -package lsp +package symboltable import ( "fmt" @@ -23,7 +23,7 @@ func NewIncludeDefinitionsVisitor(symbolTable *SymbolTable, content []byte) *Inc func (v *IncludeDefinitionsVisitor) Enter(node *sitter.Node) { if node.Type() == gotemplate.NodeTypeDefineAction { content := node.ChildByFieldName("name").Content(v.content) - v.symbolTable.AddIncludeDefinition(util.RemoveQuotes(content), GetRangeForNode(node)) + v.symbolTable.AddIncludeDefinition(util.RemoveQuotes(content), util.GetRangeForNode(node)) } if node.Type() == gotemplate.NodeTypeFunctionCall { @@ -37,7 +37,7 @@ func (v *IncludeDefinitionsVisitor) enterFunctionCall(node *sitter.Node) { return } - v.symbolTable.AddIncludeReference(includeName, GetRangeForNode(node)) + v.symbolTable.AddIncludeReference(includeName, util.GetRangeForNode(node)) } func ParseIncludeFunctionCall(node *sitter.Node, content []byte) (string, error) { diff --git a/internal/lsp/symbol_table_template_context.go b/internal/lsp/symbol_table/symbol_table_template_context.go similarity index 93% rename from internal/lsp/symbol_table_template_context.go rename to internal/lsp/symbol_table/symbol_table_template_context.go index 7886f8ff..0ee0138b 100644 --- a/internal/lsp/symbol_table_template_context.go +++ b/internal/lsp/symbol_table/symbol_table_template_context.go @@ -1,7 +1,8 @@ -package lsp +package symboltable import ( "github.com/mrjosh/helm-ls/internal/tree-sitter/gotemplate" + "github.com/mrjosh/helm-ls/internal/util" sitter "github.com/smacker/go-tree-sitter" ) @@ -54,13 +55,13 @@ func (v *TemplateContextVisitor) Enter(node *sitter.Node) { nodeType := node.Type() switch nodeType { case gotemplate.NodeTypeDot: - v.symbolTable.AddTemplateContext(v.currentContext, GetRangeForNode(node)) + v.symbolTable.AddTemplateContext(v.currentContext, util.GetRangeForNode(node)) case gotemplate.NodeTypeFieldIdentifier: content := node.Content(v.content) - v.symbolTable.AddTemplateContext(append(v.currentContext, content), GetRangeForNode(node)) + v.symbolTable.AddTemplateContext(append(v.currentContext, content), util.GetRangeForNode(node)) case gotemplate.NodeTypeField: content := node.ChildByFieldName("name").Content(v.content) - v.symbolTable.AddTemplateContext(append(v.currentContext, content), GetRangeForNode(node.ChildByFieldName("name"))) + v.symbolTable.AddTemplateContext(append(v.currentContext, content), util.GetRangeForNode(node.ChildByFieldName("name"))) case gotemplate.NodeTypeUnfinishedSelectorExpression: operandNode := node.ChildByFieldName("operand") content := getContextForSelectorExpression(operandNode, v.content) @@ -68,7 +69,7 @@ func (v *TemplateContextVisitor) Enter(node *sitter.Node) { content = append(v.currentContext, content...) } v.symbolTable.AddTemplateContext(append(content, ""), - GetRangeForNode(node.Child(int(node.ChildCount())-1))) + util.GetRangeForNode(node.Child(int(node.ChildCount())-1))) case gotemplate.NodeTypeSelectorExpression: operandNode := node.ChildByFieldName("operand") if operandNode.Type() == gotemplate.NodeTypeVariable { diff --git a/internal/lsp/symbol_table_template_context_test.go b/internal/lsp/symbol_table/symbol_table_template_context_test.go similarity index 92% rename from internal/lsp/symbol_table_template_context_test.go rename to internal/lsp/symbol_table/symbol_table_template_context_test.go index 06b241cb..6d8998fe 100644 --- a/internal/lsp/symbol_table_template_context_test.go +++ b/internal/lsp/symbol_table/symbol_table_template_context_test.go @@ -1,8 +1,9 @@ -package lsp +package symboltable import ( "testing" + templateast "github.com/mrjosh/helm-ls/internal/lsp/template_ast" "github.com/stretchr/testify/assert" ) @@ -52,7 +53,7 @@ func TestGetContextForSelectorExpression(t *testing.T) { } for _, tC := range testCases { t.Run(tC.desc, func(t *testing.T) { - ast := ParseAst(nil, []byte(tC.template)) + ast := templateast.ParseAst(nil, []byte(tC.template)) node := ast.RootNode().Child(1) assert.Equal(t, tC.nodeContent, node.Content([]byte(tC.template))) diff --git a/internal/lsp/symbol_table_template_context_variables.go b/internal/lsp/symbol_table/symbol_table_template_context_variables.go similarity index 98% rename from internal/lsp/symbol_table_template_context_variables.go rename to internal/lsp/symbol_table/symbol_table_template_context_variables.go index 65f41d8f..888efe2a 100644 --- a/internal/lsp/symbol_table_template_context_variables.go +++ b/internal/lsp/symbol_table/symbol_table_template_context_variables.go @@ -1,4 +1,4 @@ -package lsp +package symboltable import ( "fmt" diff --git a/internal/lsp/symbol_table_template_context_variables_test.go b/internal/lsp/symbol_table/symbol_table_template_context_variables_test.go similarity index 90% rename from internal/lsp/symbol_table_template_context_variables_test.go rename to internal/lsp/symbol_table/symbol_table_template_context_variables_test.go index ce2f7518..339aeea6 100644 --- a/internal/lsp/symbol_table_template_context_variables_test.go +++ b/internal/lsp/symbol_table/symbol_table_template_context_variables_test.go @@ -1,9 +1,10 @@ -package lsp +package symboltable import ( "strings" "testing" + templateast "github.com/mrjosh/helm-ls/internal/lsp/template_ast" sitter "github.com/smacker/go-tree-sitter" "github.com/stretchr/testify/assert" ) @@ -24,7 +25,7 @@ func TestResolveVariablesInTemplateContext(t *testing.T) { t.Run(tt.template, func(t *testing.T) { col := strings.Index(tt.template, "^") buf := strings.Replace(tt.template, "^", "", 1) - ast := ParseAst(nil, []byte(tt.template)) + ast := templateast.ParseAst(nil, []byte(tt.template)) symbolTable := NewSymbolTable(ast, []byte(buf)) result, err := symbolTable.ResolveVariablesInTemplateContext( diff --git a/internal/lsp/symbol_table_test.go b/internal/lsp/symbol_table/symbol_table_test.go similarity index 92% rename from internal/lsp/symbol_table_test.go rename to internal/lsp/symbol_table/symbol_table_test.go index e53d9480..d42486e5 100644 --- a/internal/lsp/symbol_table_test.go +++ b/internal/lsp/symbol_table/symbol_table_test.go @@ -1,9 +1,10 @@ -package lsp +package symboltable import ( "os" "testing" + templateast "github.com/mrjosh/helm-ls/internal/lsp/template_ast" sitter "github.com/smacker/go-tree-sitter" "github.com/stretchr/testify/assert" ) @@ -19,24 +20,26 @@ func TestSymbolTableForIncludeDefinitions(t *testing.T) { {{ end }} ` - ast := ParseAst(nil, []byte(content)) + ast := templateast.ParseAst(nil, []byte(content)) symbolTable := NewSymbolTable(ast, []byte(content)) assert.Len(t, symbolTable.includeDefinitions, 2) - assert.Equal(t, symbolTable.includeDefinitions["bar"], []sitter.Range{{ - StartPoint: sitter.Point{ - Row: 5, - Column: 0, - }, - EndPoint: sitter.Point{ - Row: 7, - Column: 10, + assert.Equal(t, symbolTable.includeDefinitions["bar"], []sitter.Range{ + { + StartPoint: sitter.Point{ + Row: 5, + Column: 0, + }, + EndPoint: sitter.Point{ + Row: 7, + Column: 10, + }, + StartByte: 56, + EndByte: 110, }, - StartByte: 56, - EndByte: 110, - }}) + }) } func TestSymbolTableForValues(t *testing.T) { @@ -68,7 +71,7 @@ func TestSymbolTableForValues(t *testing.T) { {{ end }} ` - ast := ParseAst(nil, []byte(content)) + ast := templateast.ParseAst(nil, []byte(content)) symbolTable := NewSymbolTable(ast, []byte(content)) type expectedValue struct { @@ -181,13 +184,13 @@ func TestSymbolTableForValues(t *testing.T) { } func TestSymbolTableForValuesTestFile(t *testing.T) { - path := "../../testdata/example/templates/deployment.yaml" + path := "../../../testdata/example/templates/deployment.yaml" content, err := os.ReadFile(path) if err != nil { t.Fatal("Could not read test file", err) } - ast := ParseAst(nil, content) + ast := templateast.ParseAst(nil, content) symbolTable := NewSymbolTable(ast, content) type expectedValue struct { @@ -324,7 +327,7 @@ func TestSymbolTableForValuesSingleTests(t *testing.T) { for _, v := range testCases { t.Run(v.template, func(t *testing.T) { - ast := ParseAst(nil, []byte(v.template)) + ast := templateast.ParseAst(nil, []byte(v.template)) symbolTable := NewSymbolTable(ast, []byte(v.template)) values := symbolTable.GetTemplateContextRanges(v.path) points := []sitter.Point{} diff --git a/internal/lsp/symbol_table_variables.go b/internal/lsp/symbol_table/symbol_table_variables.go similarity index 99% rename from internal/lsp/symbol_table_variables.go rename to internal/lsp/symbol_table/symbol_table_variables.go index 45249499..c992e7ff 100644 --- a/internal/lsp/symbol_table_variables.go +++ b/internal/lsp/symbol_table/symbol_table_variables.go @@ -1,4 +1,4 @@ -package lsp +package symboltable import ( "fmt" diff --git a/internal/lsp/symbol_table_variables_test.go b/internal/lsp/symbol_table/symbol_table_variables_test.go similarity index 92% rename from internal/lsp/symbol_table_variables_test.go rename to internal/lsp/symbol_table/symbol_table_variables_test.go index de14034d..f21172f1 100644 --- a/internal/lsp/symbol_table_variables_test.go +++ b/internal/lsp/symbol_table/symbol_table_variables_test.go @@ -1,9 +1,10 @@ -package lsp +package symboltable import ( "fmt" "testing" + templateast "github.com/mrjosh/helm-ls/internal/lsp/template_ast" sitter "github.com/smacker/go-tree-sitter" "github.com/stretchr/testify/assert" ) @@ -77,7 +78,7 @@ func TestGetVariableDefinition(t *testing.T) { } for _, tC := range testCases { t.Run(tC.template, func(t *testing.T) { - ast := ParseAst(nil, []byte(tC.template)) + ast := templateast.ParseAst(nil, []byte(tC.template)) symbolTable := NewSymbolTable(ast, []byte(tC.template)) result, err := symbolTable.getVariableDefinition(tC.variableName, tC.accessRange) assert.Equal(t, tC.expectedError, err) diff --git a/internal/lsp/variables_visitor.go b/internal/lsp/symbol_table/variables_visitor.go similarity index 99% rename from internal/lsp/variables_visitor.go rename to internal/lsp/symbol_table/variables_visitor.go index cf245cc9..42d43f0c 100644 --- a/internal/lsp/variables_visitor.go +++ b/internal/lsp/symbol_table/variables_visitor.go @@ -1,4 +1,4 @@ -package lsp +package symboltable import ( "github.com/mrjosh/helm-ls/internal/tree-sitter/gotemplate" diff --git a/internal/lsp/variables_visitor_test.go b/internal/lsp/symbol_table/variables_visitor_test.go similarity index 94% rename from internal/lsp/variables_visitor_test.go rename to internal/lsp/symbol_table/variables_visitor_test.go index f21f69ed..231a3702 100644 --- a/internal/lsp/variables_visitor_test.go +++ b/internal/lsp/symbol_table/variables_visitor_test.go @@ -1,9 +1,10 @@ -package lsp +package symboltable import ( "fmt" "testing" + templateast "github.com/mrjosh/helm-ls/internal/lsp/template_ast" sitter "github.com/smacker/go-tree-sitter" "github.com/stretchr/testify/assert" ) @@ -70,7 +71,7 @@ func TestSymbolTableForVariableDefinitions(t *testing.T) { for _, v := range testCases { t.Run(v.template, func(t *testing.T) { - ast := ParseAst(nil, []byte(v.template)) + ast := templateast.ParseAst(nil, []byte(v.template)) symbolTable := NewSymbolTable(ast, []byte(v.template)) assert.Equal(t, v.expectedVariableDefinitions, symbolTable.variableDefinitions, fmt.Sprintf("Ast was %s", ast.RootNode())) @@ -97,7 +98,7 @@ func TestSymbolTableForVariableUsages(t *testing.T) { for _, v := range testCases { t.Run(v.template, func(t *testing.T) { - ast := ParseAst(nil, []byte(v.template)) + ast := templateast.ParseAst(nil, []byte(v.template)) symbolTable := NewSymbolTable(ast, []byte(v.template)) assert.Equal(t, v.expectedVariableUsages, symbolTable.variableUsages, fmt.Sprintf("Ast was %s", ast.RootNode())) diff --git a/internal/lsp/visitor.go b/internal/lsp/symbol_table/visitor.go similarity index 99% rename from internal/lsp/visitor.go rename to internal/lsp/symbol_table/visitor.go index e93922fc..99e4b9c1 100644 --- a/internal/lsp/visitor.go +++ b/internal/lsp/symbol_table/visitor.go @@ -1,4 +1,4 @@ -package lsp +package symboltable import ( "github.com/mrjosh/helm-ls/internal/tree-sitter/gotemplate" diff --git a/internal/lsp/ast.go b/internal/lsp/template_ast/ast.go similarity index 95% rename from internal/lsp/ast.go rename to internal/lsp/template_ast/ast.go index b32f5122..ec779de9 100644 --- a/internal/lsp/ast.go +++ b/internal/lsp/template_ast/ast.go @@ -1,4 +1,4 @@ -package lsp +package templateast import ( "context" @@ -69,10 +69,6 @@ func isPointLargerOrEq(a sitter.Point, b sitter.Point) bool { return a.Row > b.Row } -func (d *TemplateDocument) ApplyChangesToAst(newContent []byte) { - d.Ast = ParseAst(nil, newContent) -} - func GetLspRangeForNode(node *sitter.Node) lsp.Range { start := node.StartPoint() end := node.EndPoint() diff --git a/internal/lsp/yaml_ast.go b/internal/lsp/yaml_ast.go index 9b0f54a3..2076fea2 100644 --- a/internal/lsp/yaml_ast.go +++ b/internal/lsp/yaml_ast.go @@ -2,25 +2,17 @@ package lsp import ( "github.com/mrjosh/helm-ls/internal/tree-sitter/gotemplate" + "github.com/mrjosh/helm-ls/internal/util" sitter "github.com/smacker/go-tree-sitter" ) -func GetRangeForNode(node *sitter.Node) sitter.Range { - return sitter.Range{ - StartPoint: node.StartPoint(), - EndPoint: node.EndPoint(), - StartByte: node.StartByte(), - EndByte: node.EndByte(), - } -} - func getTextNodeRanges(gotemplateNode *sitter.Node) []sitter.Range { textNodes := []sitter.Range{} for i := 0; i < int(gotemplateNode.ChildCount()); i++ { child := gotemplateNode.Child(i) if child.Type() == gotemplate.NodeTypeText { - textNodes = append(textNodes, GetRangeForNode(child)) + textNodes = append(textNodes, util.GetRangeForNode(child)) } else { textNodes = append(textNodes, getTextNodeRanges(child)...) } diff --git a/internal/lsp/yaml_ast_test.go b/internal/lsp/yaml_ast_test.go index 02d13e3f..4d3435a4 100644 --- a/internal/lsp/yaml_ast_test.go +++ b/internal/lsp/yaml_ast_test.go @@ -3,6 +3,7 @@ package lsp import ( "testing" + templateast "github.com/mrjosh/helm-ls/internal/lsp/template_ast" sitter "github.com/smacker/go-tree-sitter" "github.com/stretchr/testify/assert" ) @@ -78,7 +79,7 @@ b: not`, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := getTextNodeRanges(ParseAst(nil, []byte(tt.args.gotemplateString)).RootNode()) + got := getTextNodeRanges(templateast.ParseAst(nil, []byte(tt.args.gotemplateString)).RootNode()) assert.Equal(t, tt.want, got) }) } @@ -121,7 +122,7 @@ yaml: test } for _, tt := range tests { t.Run(tt.documentText, func(t *testing.T) { - gotemplateTree := ParseAst(nil, []byte(tt.documentText)) + gotemplateTree := templateast.ParseAst(nil, []byte(tt.documentText)) got := TrimTemplate(gotemplateTree, []byte(tt.documentText)) assert.Equal(t, tt.trimmedText, got) }) diff --git a/internal/util/points.go b/internal/util/points.go index cdec6cc6..3e11c9ca 100644 --- a/internal/util/points.go +++ b/internal/util/points.go @@ -23,3 +23,12 @@ func RangeToLocation(URI uri.URI, range_ sitter.Range) lsp.Location { }, } } + +func GetRangeForNode(node *sitter.Node) sitter.Range { + return sitter.Range{ + StartPoint: node.StartPoint(), + EndPoint: node.EndPoint(), + StartByte: node.StartByte(), + EndByte: node.EndByte(), + } +}