Skip to content

Commit

Permalink
refactor: split up lsp package
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed Nov 10, 2024
1 parent 3c2d914 commit 855df28
Show file tree
Hide file tree
Showing 57 changed files with 183 additions and 163 deletions.
5 changes: 3 additions & 2 deletions internal/adapter/yamlls/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions internal/adapter/yamlls/diagnostics_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 5 additions & 4 deletions internal/adapter/yamlls/document_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions internal/adapter/yamlls/integration_test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions internal/adapter/yamlls/yamlls.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions internal/adapter/yamlls/yamlls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"

Expand Down
14 changes: 7 additions & 7 deletions internal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand All @@ -45,17 +45,17 @@ 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,
linterName: "helm-lint",
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")
Expand Down
4 changes: 2 additions & 2 deletions internal/handler/template_handler/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions internal/handler/template_handler/completion_main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions internal/handler/template_handler/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions internal/handler/template_handler/definition_chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)

Expand Down
8 changes: 4 additions & 4 deletions internal/handler/template_handler/definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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("/")

Expand Down Expand Up @@ -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("/")

Expand Down Expand Up @@ -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("/")

Expand Down
6 changes: 3 additions & 3 deletions internal/handler/template_handler/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ 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"

lsp "go.lsp.dev/protocol"
)

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
Expand Down
4 changes: 2 additions & 2 deletions internal/handler/template_handler/hover_main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions internal/handler/template_handler/references.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions internal/handler/template_handler/references_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")

Expand Down
Loading

0 comments on commit 855df28

Please sign in to comment.