Skip to content

Commit

Permalink
clean up internals with typedefs, surface WasInitialized error
Browse files Browse the repository at this point in the history
  • Loading branch information
appilon committed Dec 8, 2020
1 parent 3bd844d commit e6711a9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
17 changes: 11 additions & 6 deletions internal/langserver/diagnostics/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@ type diagContext struct {
diags []lsp.Diagnostic
}

type diagnosticSource string

type fileDiagnostics map[diagnosticSource][]lsp.Diagnostic

// Notifier is a type responsible for queueing hcl diagnostics to be converted
// and sent to the client
type Notifier struct {
logger *log.Logger
sessCtx context.Context
diags chan diagContext
diagsCache map[string]map[string][]lsp.Diagnostic
diagsCache map[lsp.DocumentURI]fileDiagnostics
closeDiagsOnce sync.Once
}

Expand All @@ -35,7 +39,7 @@ func NewNotifier(sessCtx context.Context, logger *log.Logger) *Notifier {
logger: logger,
sessCtx: sessCtx,
diags: make(chan diagContext, 50),
diagsCache: make(map[string]map[string][]lsp.Diagnostic),
diagsCache: make(map[lsp.DocumentURI]fileDiagnostics),
}
go n.notify()
return n
Expand Down Expand Up @@ -67,7 +71,7 @@ func (n *Notifier) notify() {
for d := range n.diags {
if err := jrpc2.PushNotify(d.ctx, "textDocument/publishDiagnostics", lsp.PublishDiagnosticsParams{
URI: d.uri,
Diagnostics: n.mergeDiags(string(d.uri), d.source, d.diags),
Diagnostics: n.mergeDiags(d.uri, d.source, d.diags),
}); err != nil {
n.logger.Printf("Error pushing diagnostics: %s", err)
}
Expand All @@ -77,13 +81,14 @@ func (n *Notifier) notify() {
// mergeDiags will return all diags from all cached sources for a given uri.
// the passed diags overwrites the cached entry for the passed source key
// even if empty
func (n *Notifier) mergeDiags(uri string, source string, diags []lsp.Diagnostic) []lsp.Diagnostic {
func (n *Notifier) mergeDiags(uri lsp.DocumentURI, source string, diags []lsp.Diagnostic) []lsp.Diagnostic {

fileDiags, ok := n.diagsCache[uri]
if !ok {
fileDiags = make(map[string][]lsp.Diagnostic)
fileDiags = make(fileDiagnostics)
}

fileDiags[source] = diags
fileDiags[diagnosticSource(source)] = diags
n.diagsCache[uri] = fileDiags

all := []lsp.Diagnostic{}
Expand Down
4 changes: 2 additions & 2 deletions internal/langserver/diagnostics/diagnostics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestPublish_DoesNotSendAfterClose(t *testing.T) {
}

func TestMergeDiags_CachesMultipleSourcesPerURI(t *testing.T) {
uri := "test.tf"
uri := lsp.DocumentURI("test.tf")

n := NewNotifier(context.Background(), discardLogger)

Expand All @@ -77,7 +77,7 @@ func TestMergeDiags_CachesMultipleSourcesPerURI(t *testing.T) {
}

func TestMergeDiags_OverwritesSource_EvenWithEmptySlice(t *testing.T) {
uri := "test.tf"
uri := lsp.DocumentURI("test.tf")

n := NewNotifier(context.Background(), discardLogger)

Expand Down
5 changes: 4 additions & 1 deletion internal/langserver/handlers/command/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ func TerraformValidateHandler(ctx context.Context, args cmd.CommandArgs) (interf
return nil, err
}

wasInit, _ := rm.WasInitialized()
wasInit, err := rm.WasInitialized()
if err != nil {
return nil, fmt.Errorf("error checking if %s was initialized: %s", dirUri, err)
}
if !wasInit {
return nil, fmt.Errorf("%s is not an initialized module, terraform validate cannot be called", dirUri)
}
Expand Down

0 comments on commit e6711a9

Please sign in to comment.