Skip to content

Commit

Permalink
hcl: Ignore parsing errors
Browse files Browse the repository at this point in the history
This doesn't actually enable completion for invalid configs yet
but it paves the way for #56 and #57
  • Loading branch information
radeksimko committed Apr 21, 2020
1 parent 3f65bad commit a8ab05a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 29 deletions.
12 changes: 3 additions & 9 deletions internal/hcl/hcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,10 @@ func (f *file) ast() (*hcllib.File, error) {
return f.f, nil
}

hf, diags := hclsyntax.ParseConfig(f.content, f.filename, hcllib.InitialPos)
if diags.HasErrors() {
return nil, diags
}
hf, err := hclsyntax.ParseConfig(f.content, f.filename, hcllib.InitialPos)
f.f = hf

return hf, nil
return f.f, err
}

func (f *file) BlockAtPosition(filePos filesystem.FilePosition) (*hcllib.Block, hcllib.Pos, error) {
Expand All @@ -49,10 +46,7 @@ func (f *file) BlockAtPosition(filePos filesystem.FilePosition) (*hcllib.Block,
}

func (f *file) blockAtPosition(pos hcllib.Pos) (*hcllib.Block, error) {
ast, err := f.ast()
if err != nil {
return nil, err
}
ast, _ := f.ast()

if body, ok := ast.Body.(*hclsyntax.Body); ok {
if body.SrcRange.Empty() && pos != hcllib.InitialPos {
Expand Down
23 changes: 4 additions & 19 deletions internal/hcl/hcl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,11 @@ func TestFile_BlockAtPosition(t *testing.T) {
Column: 1,
Byte: 0,
},
hcl.Diagnostics{
&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Argument or block definition required",
Detail: "An argument or block definition is required here.",
Subject: &hcl.Range{
Start: hcl.Pos{
Byte: 0,
Column: 1,
Line: 1,
},
End: hcl.Pos{
Byte: 3,
Column: 4,
Line: 1,
},
},
},
nil, // Expect errors to be ignored
&hcl.Block{
Type: "provider",
Labels: []string{"aws"},
},
nil,
},
{
"valid config and position",
Expand Down
3 changes: 2 additions & 1 deletion langserver/handlers/complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (h *logHandler) TextDocumentComplete(ctx context.Context, params lsp.Comple

hclBlock, hclPos, err := hclFile.BlockAtPosition(fPos)
if err != nil {
return list, fmt.Errorf("finding config block failed: %s", err)
return list, fmt.Errorf("finding HCL block failed: %s", err)
}
h.logger.Printf("HCL block found at HCL pos %#v", hclPos)

Expand All @@ -63,6 +63,7 @@ func (h *logHandler) TextDocumentComplete(ctx context.Context, params lsp.Comple
if err != nil {
return list, fmt.Errorf("finding config block failed: %w", err)
}
h.logger.Printf("Configuration block %q parsed", cfgBlock.BlockType())

candidates, err := cfgBlock.CompletionCandidatesAtPos(hclPos)
if err != nil {
Expand Down

0 comments on commit a8ab05a

Please sign in to comment.