Skip to content

Commit

Permalink
fix(concurrency): use sync.map
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed Mar 10, 2024
1 parent 166165a commit e5d73ba
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions internal/lsp/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"strings"
"sync"

"github.com/mrjosh/helm-ls/internal/util"
sitter "github.com/smacker/go-tree-sitter"
Expand All @@ -13,20 +14,21 @@ import (

// documentStore holds opened documents.
type DocumentStore struct {
documents map[string]*Document
documents sync.Map
}

func NewDocumentStore() *DocumentStore {
return &DocumentStore{
documents: map[string]*Document{},
documents: sync.Map{},
}
}

func (s *DocumentStore) GetAllDocs() []*Document {
var docs []*Document
for _, doc := range s.documents {
docs = append(docs, doc)
}
s.documents.Range(func(_, v interface{}) bool {
docs = append(docs, v.(*Document))
return true
})
return docs
}

Expand All @@ -42,14 +44,17 @@ func (s *DocumentStore) DidOpen(params lsp.DidOpenTextDocumentParams, helmlsConf
Ast: ParseAst(nil, params.TextDocument.Text),
DiagnosticsCache: NewDiagnosticsCache(helmlsConfig),
}
s.documents[path] = doc
s.documents.Store(path, doc)
return doc, nil
}

func (s *DocumentStore) Get(docuri uri.URI) (*Document, bool) {
path := docuri.Filename()
d, ok := s.documents[path]
return d, ok
d, ok := s.documents.Load(path)
if !ok {
return nil, false
}
return d.(*Document), ok
}

// Document represents an opened file.
Expand Down

0 comments on commit e5d73ba

Please sign in to comment.