-
Notifications
You must be signed in to change notification settings - Fork 135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement hcl parse diagnostics #269
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package lsp | ||
|
||
import ( | ||
"github.com/hashicorp/hcl/v2" | ||
lsp "github.com/sourcegraph/go-lsp" | ||
) | ||
|
||
func HCLRangeToLSP(hclRng hcl.Range) lsp.Range { | ||
return lsp.Range{ | ||
Start: lsp.Position{ | ||
Character: hclRng.Start.Column - 1, | ||
Line: hclRng.Start.Line - 1, | ||
}, | ||
End: lsp.Position{ | ||
Character: hclRng.End.Column - 1, | ||
Line: hclRng.End.Line - 1, | ||
}, | ||
} | ||
} | ||
|
||
func lspRangeToHCL(lspRng lsp.Range, f File) (*hcl.Range, error) { | ||
startPos, err := lspPositionToHCL(f.Lines(), lspRng.Start) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
endPos, err := lspPositionToHCL(f.Lines(), lspRng.End) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &hcl.Range{ | ||
Filename: f.Filename(), | ||
Start: startPos, | ||
End: endPos, | ||
}, nil | ||
} | ||
|
||
func HCLSeverityToLSP(severity hcl.DiagnosticSeverity) lsp.DiagnosticSeverity { | ||
var sev lsp.DiagnosticSeverity | ||
switch severity { | ||
case hcl.DiagError: | ||
sev = lsp.Error | ||
case hcl.DiagWarning: | ||
sev = lsp.Warning | ||
case hcl.DiagInvalid: | ||
panic("invalid diagnostic") | ||
} | ||
return sev | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package diagnostics | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"github.com/creachadair/jrpc2" | ||
"github.com/hashicorp/hcl/v2/hclparse" | ||
ilsp "github.com/hashicorp/terraform-ls/internal/lsp" | ||
"github.com/sourcegraph/go-lsp" | ||
) | ||
|
||
// documentContext encapsulates the data needed to diagnose the file and push diagnostics to the client | ||
type documentContext struct { | ||
ctx context.Context | ||
uri lsp.DocumentURI | ||
text []byte | ||
} | ||
|
||
// Notifier is a type responsible for processing documents and pushing diagnostics to the client | ||
type Notifier struct { | ||
sessCtx context.Context | ||
hclDocs chan documentContext | ||
closeHclDocsOnce sync.Once | ||
} | ||
|
||
func NewNotifier(sessCtx context.Context) *Notifier { | ||
hclDocs := make(chan documentContext, 10) | ||
go hclDiags(hclDocs) | ||
return &Notifier{hclDocs: hclDocs, sessCtx: sessCtx} | ||
} | ||
|
||
// DiagnoseHCL enqueues the document for HCL parsing. Documents will be parsed and notifications delivered in order that | ||
// they are enqueued. Files that are actively changing should be enqueued in order, so that diagnostics remain insync with | ||
// the current content of the file. This is the responsibility of the caller. | ||
func (n *Notifier) DiagnoseHCL(ctx context.Context, uri lsp.DocumentURI, text []byte) { | ||
select { | ||
case <-n.sessCtx.Done(): | ||
n.closeHclDocsOnce.Do(func() { | ||
close(n.hclDocs) | ||
}) | ||
return | ||
default: | ||
} | ||
n.hclDocs <- documentContext{ctx: ctx, uri: uri, text: text} | ||
} | ||
|
||
func hclParse(doc documentContext) []lsp.Diagnostic { | ||
diags := []lsp.Diagnostic{} | ||
|
||
_, hclDiags := hclparse.NewParser().ParseHCL(doc.text, string(doc.uri)) | ||
for _, hclDiag := range hclDiags { | ||
// only process diagnostics with an attributable spot in the code | ||
if hclDiag.Subject != nil { | ||
msg := hclDiag.Summary | ||
if hclDiag.Detail != "" { | ||
msg += ": " + hclDiag.Detail | ||
} | ||
diags = append(diags, lsp.Diagnostic{ | ||
Range: ilsp.HCLRangeToLSP(*hclDiag.Subject), | ||
Severity: ilsp.HCLSeverityToLSP(hclDiag.Severity), | ||
Source: "HCL", | ||
Message: msg, | ||
}) | ||
} | ||
} | ||
return diags | ||
} | ||
|
||
func hclDiags(docs <-chan documentContext) { | ||
for doc := range docs { | ||
// always push diagnostics, even if the slice is empty, this is how previous diagnostics are cleared | ||
// any push error will result in a panic since this is executing in its own thread and we can't bubble | ||
// an error to a jrpc response | ||
if err := jrpc2.PushNotify(doc.ctx, "textDocument/publishDiagnostics", lsp.PublishDiagnosticsParams{ | ||
URI: doc.uri, | ||
Diagnostics: hclParse(doc), | ||
}); err != nil { | ||
panic(err) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package diagnostics | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
) | ||
|
||
func TestDiagnoseHCL_Closes(t *testing.T) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
n := NewNotifier(ctx) | ||
cancel() | ||
n.DiagnoseHCL(context.Background(), "", []byte{}) | ||
if _, open := <-n.hclDocs; open { | ||
t.Fatal("documents channel should be closed") | ||
} | ||
} | ||
|
||
func TestDiagnoseHCL_DoesNotSendAfterClose(t *testing.T) { | ||
defer func() { | ||
if err := recover(); err != nil { | ||
t.Fatal(err) | ||
} | ||
}() | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
n := NewNotifier(ctx) | ||
cancel() | ||
n.DiagnoseHCL(context.Background(), "", []byte{}) | ||
n.DiagnoseHCL(context.Background(), "", []byte{}) | ||
} | ||
|
||
func TestHCLParse_ReturnsEmptySliceWhenValid(t *testing.T) { | ||
diags := hclParse(documentContext{ctx: context.Background(), uri: "test", text: hcl(`provider "test" {}`)}) | ||
if diags == nil { | ||
t.Fatal("slice needs to be initialized") | ||
} | ||
if len(diags) > 0 { | ||
t.Fatalf("valid hcl should return an empty slice: %v", diags) | ||
} | ||
} | ||
|
||
func TestHCLParse_ReturnsDiagsWhenInvalid(t *testing.T) { | ||
diags := hclParse(documentContext{ctx: context.Background(), uri: "test", text: hcl(`provider test" {}`)}) | ||
if len(diags) == 0 { | ||
t.Fatal("invalid hcl should return diags") | ||
} | ||
} | ||
|
||
func hcl(text string) []byte { | ||
return append([]byte(text), '\n') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason we need the asynchronicity?
I mean each request is already being processed in a dedicated goroutine and parallelism is controlled in the jRPC library and that's limited to 1 for ordering reasons, so we'd never actually process more than 1 at a time anyway and even if for some reason we do in the future, then I reckon the performance/parallelism would be handled by the jRPC library on handler level?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are push requests limited by the parallelism? Seems like this is just pushing each chan value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea is we have a diagnostics worker thread that takes in document requests and pushes diags to the client as soon as possible, but it does not ever block or add milliseconds to the
didOpen
anddidChange
handlers (and upcomingdidSave
for validation).