Skip to content

Commit

Permalink
[Fix] prevent concurrency errors
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed Nov 30, 2023
1 parent 316b4ae commit 4c449db
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 20 deletions.
4 changes: 2 additions & 2 deletions internal/adapter/yamlls/documentSync.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
lsp "go.lsp.dev/protocol"
)

func (yamllsConnector YamllsConnector) InitiallySyncOpenDocuments() {
func (yamllsConnector YamllsConnector) InitiallySyncOpenDocuments(docs []*lsplocal.Document) {
if yamllsConnector.Conn == nil {
return
}
for _, doc := range yamllsConnector.documents.GetAllDocs() {
for _, doc := range docs {
yamllsConnector.DocumentDidOpen(doc.Ast, lsp.DidOpenTextDocumentParams{
TextDocument: lsp.TextDocumentItem{
URI: doc.URI,
Expand Down
6 changes: 2 additions & 4 deletions internal/adapter/yamlls/yamlls.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ import (
var logger = log.GetLogger()

type YamllsConnector struct {

Check failure on line 15 in internal/adapter/yamlls/yamlls.go

View workflow job for this annotation

GitHub Actions / lint (1.19.1, ubuntu-latest)

exported: type name will be used as yamlls.YamllsConnector by other packages, and that stutters; consider calling this Connector (revive)
Conn *jsonrpc2.Conn
documents *lsplocal.DocumentStore
config util.YamllsConfiguration
Conn *jsonrpc2.Conn
config util.YamllsConfiguration
}

func NewConnector(yamllsConfiguration util.YamllsConfiguration, clientConn jsonrpc2.Conn, documents *lsplocal.DocumentStore) *YamllsConnector {
Expand Down Expand Up @@ -55,7 +54,6 @@ func NewConnector(yamllsConfiguration util.YamllsConfiguration, clientConn jsonr
conn := jsonrpc2.NewConn(jsonrpc2.NewStream(readWriteCloser))
yamllsConnector.config = yamllsConfiguration
conn.Go(context.Background(), yamllsConnector.yamllsHandler(clientConn, documents))
yamllsConnector.documents = documents
yamllsConnector.Conn = &conn
return &yamllsConnector
}
10 changes: 8 additions & 2 deletions internal/handler/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
)

func (h *langHandler) handleWorkspaceDidChangeConfiguration(ctx context.Context, reply jsonrpc2.Replier, req jsonrpc2.Request) (err error) {
go h.retrieveWorkspaceConfiguration(ctx)
// go h.retrieveWorkspaceConfiguration(ctx)
logger.Println("Changing workspace config is not implemented")
return reply(ctx, nil, nil)
}

Expand All @@ -28,6 +29,11 @@ func (h *langHandler) retrieveWorkspaceConfiguration(ctx context.Context) {
logger.Println("Workspace configuration:", result)
}

h.helmlsConfig = result[0]
if len(result) == 0 {
logger.Println("Workspace configuration is empty")
return
}

h.setConfig(result[0])
h.initializationWithConfig(ctx)
}
16 changes: 15 additions & 1 deletion internal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"sync"

"github.com/mrjosh/helm-ls/internal/adapter/fs"
"github.com/mrjosh/helm-ls/internal/adapter/yamlls"
Expand All @@ -21,6 +22,7 @@ import (
var logger = log.GetLogger()

type langHandler struct {
sync.RWMutex
connPool jsonrpc2.Conn
linterName string
documents *lsplocal.DocumentStore
Expand All @@ -33,6 +35,18 @@ type langHandler struct {
helmlsConfig util.HelmlsConfiguration
}

func (h *langHandler) setConfig(config util.HelmlsConfiguration) {
h.Lock()
defer h.Unlock()
h.helmlsConfig = config
}

func (h *langHandler) getConfig() (config util.HelmlsConfiguration) {
h.Lock()
defer h.Unlock()
return h.helmlsConfig
}

func NewHandler(connPool jsonrpc2.Conn) jsonrpc2.Handler {
fileStorage, _ := fs.NewFileStorage("")
documents := lsplocal.NewDocumentStore(fileStorage)
Expand Down Expand Up @@ -96,7 +110,7 @@ func (h *langHandler) handleTextDocumentDidOpen(ctx context.Context, reply jsonr
return reply(ctx, nil, err)
}

doc, err := h.documents.DidOpen(params, &h.helmlsConfig)
doc, err := h.documents.DidOpen(params, h.getConfig())
if err != nil {
logger.Println(err)
return reply(ctx, nil, err)
Expand Down
9 changes: 5 additions & 4 deletions internal/handler/initialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,16 @@ func (h *langHandler) handleInitialize(ctx context.Context, reply jsonrpc2.Repli
}

func (h *langHandler) initializationWithConfig(ctx context.Context) {
configureLogLevel(h.helmlsConfig)
configureLogLevel(h.getConfig())
configureYamlls(h)
}

func configureYamlls(h *langHandler) {
if h.helmlsConfig.YamllsConfiguration.Enabled {
h.yamllsConnector = yamlls.NewConnector(h.helmlsConfig.YamllsConfiguration, h.connPool, h.documents)
config := h.getConfig()
if config.YamllsConfiguration.Enabled {
h.yamllsConnector = yamlls.NewConnector(config.YamllsConfiguration, h.connPool, h.documents)
h.yamllsConnector.CallInitialize(h.projectFiles.RootURI)
h.yamllsConnector.InitiallySyncOpenDocuments()
h.yamllsConnector.InitiallySyncOpenDocuments(h.documents.GetAllDocs())
}
}

Expand Down
4 changes: 2 additions & 2 deletions internal/lsp/diagnostics_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
type diagnosticsCache struct {
YamlDiagnostics []lsp.Diagnostic
HelmDiagnostics []lsp.Diagnostic
helmlsConfig *util.HelmlsConfiguration
helmlsConfig util.HelmlsConfiguration
gotYamlDiagnosticsTimes int
yamlDiagnosticsCountReduced bool
}

func NewDiagnosticsCache(helmlsConfig *util.HelmlsConfiguration) diagnosticsCache {
func NewDiagnosticsCache(helmlsConfig util.HelmlsConfiguration) diagnosticsCache {
return diagnosticsCache{
[]lsp.Diagnostic{},
[]lsp.Diagnostic{},
Expand Down
8 changes: 4 additions & 4 deletions internal/lsp/diagnostics_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestDiagnosticsCache_SetYamlDiagnostics(t *testing.T) {
helmlsConfig := &util.HelmlsConfiguration{}
helmlsConfig := util.HelmlsConfiguration{}
cache := NewDiagnosticsCache(helmlsConfig)

diagnostics := []lsp.Diagnostic{
Expand All @@ -34,7 +34,7 @@ func TestDiagnosticsCache_SetYamlDiagnostics(t *testing.T) {
}

func TestDiagnosticsCache_GetMergedDiagnostics(t *testing.T) {
helmlsConfig := &util.DefaultConfig
helmlsConfig := util.DefaultConfig
cache := NewDiagnosticsCache(helmlsConfig)

yamlDiagnostics := []lsp.Diagnostic{
Expand Down Expand Up @@ -72,7 +72,7 @@ func TestDiagnosticsCache_GetMergedDiagnostics(t *testing.T) {
}

func TestDiagnosticsCache_ShouldShowDiagnosticsOnNewYamlDiagnostics(t *testing.T) {
helmlsConfig := &util.HelmlsConfiguration{}
helmlsConfig := util.HelmlsConfiguration{}
cache := NewDiagnosticsCache(helmlsConfig)
yamlDiagnostics := []lsp.Diagnostic{
{
Expand Down Expand Up @@ -102,7 +102,7 @@ func TestDiagnosticsCache_ShouldShowDiagnosticsOnNewYamlDiagnostics(t *testing.T
}

func TestDiagnosticsCache_ShouldShowDiagnosticsOnNewYamlDiagnosticsForInitialDiagnostics(t *testing.T) {
helmlsConfig := &util.HelmlsConfiguration{}
helmlsConfig := util.HelmlsConfiguration{}
cache := NewDiagnosticsCache(helmlsConfig)
yamlDiagnostics := []lsp.Diagnostic{
{
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (s *DocumentStore) GetAllDocs() []*Document {
return docs
}

func (s *DocumentStore) DidOpen(params lsp.DidOpenTextDocumentParams,helmlsConfig *util.HelmlsConfiguration) (*Document, error) {
func (s *DocumentStore) DidOpen(params lsp.DidOpenTextDocumentParams,helmlsConfig util.HelmlsConfiguration) (*Document, error) {
//langID := params.TextDocument.LanguageID
//if langID != "markdown" && langID != "vimwiki" && langID != "pandoc" {
//return nil, nil
Expand Down

0 comments on commit 4c449db

Please sign in to comment.