-
Notifications
You must be signed in to change notification settings - Fork 137
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 1 commit
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,91 @@ | ||
package diagnostics | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"github.com/creachadair/jrpc2" | ||
"github.com/hashicorp/hcl/v2" | ||
"github.com/hashicorp/hcl/v2/hclparse" | ||
ilsp "github.com/hashicorp/terraform-ls/internal/lsp" | ||
"github.com/sourcegraph/go-lsp" | ||
) | ||
|
||
type documentContext struct { | ||
ctx context.Context | ||
uri lsp.DocumentURI | ||
text []byte | ||
} | ||
|
||
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} | ||
} | ||
|
||
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 | ||
case n.hclDocs <- documentContext{ctx: ctx, uri: uri, text: text}: | ||
} | ||
} | ||
|
||
func hclDiags(docs <-chan documentContext) { | ||
for d := range docs { | ||
diags := []lsp.Diagnostic{} | ||
|
||
_, hclDiags := hclparse.NewParser().ParseHCL(d.text, string(d.uri)) | ||
for _, hclDiag := range hclDiags { | ||
if hclDiag.Subject != nil { | ||
diags = append(diags, lsp.Diagnostic{ | ||
Range: ilsp.HCLRangeToLSP(*hclDiag.Subject), | ||
Severity: hclSeverityToLSP(hclDiag.Severity), | ||
Source: "HCL", | ||
Message: lspMessage(hclDiag), | ||
}) | ||
} | ||
} | ||
|
||
if err := jrpc2.PushNotify(d.ctx, "textDocument/publishDiagnostics", lsp.PublishDiagnosticsParams{ | ||
URI: d.uri, | ||
Diagnostics: diags, | ||
}); !acceptableError(err) { | ||
panic(err) | ||
} | ||
} | ||
} | ||
|
||
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 | ||
} | ||
|
||
func lspMessage(diag *hcl.Diagnostic) string { | ||
m := diag.Summary | ||
if diag.Detail != "" { | ||
m += ": " + diag.Detail | ||
} | ||
return m | ||
} | ||
appilon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
func acceptableError(err error) bool { | ||
return true | ||
} |
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).