Skip to content

Commit

Permalink
Enable completion at trailing empty line near EOF
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed May 19, 2020
1 parent 906c368 commit a397452
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
15 changes: 13 additions & 2 deletions internal/hcl/hcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,13 @@ func (f *file) blockAtPosition(pos hcllib.Pos) (*hcllib.Block, error) {
if body.SrcRange.Empty() && pos != hcllib.InitialPos {
return nil, &InvalidHclPosErr{pos, body.SrcRange}
}
if !body.SrcRange.Empty() && !body.SrcRange.ContainsPos(pos) {
return nil, &InvalidHclPosErr{pos, body.SrcRange}
if !body.SrcRange.Empty() {
if posIsEqual(body.SrcRange.End, pos) {
return nil, &NoBlockFoundErr{pos}
}
if !body.SrcRange.ContainsPos(pos) {
return nil, &InvalidHclPosErr{pos, body.SrcRange}
}
}
}

Expand All @@ -64,3 +69,9 @@ func (f *file) blockAtPosition(pos hcllib.Pos) (*hcllib.Block, error) {

return block, nil
}

func posIsEqual(a, b hcllib.Pos) bool {
return a.Byte == b.Byte &&
a.Column == b.Column &&
a.Line == b.Line
}
14 changes: 14 additions & 0 deletions internal/hcl/hcl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,20 @@ func TestFile_BlockAtPosition(t *testing.T) {
},
nil,
},
{
"valid config and EOF position",
`provider "aws" {
}
`,
hcl.Pos{
Line: 4,
Column: 1,
Byte: 20,
},
&NoBlockFoundErr{AtPos: hcl.Pos{Line: 4, Column: 1, Byte: 20}},
nil,
},
}

opts := cmp.Options{
Expand Down
19 changes: 16 additions & 3 deletions internal/source/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,31 @@ func (l sourceLine) Bytes() []byte {

func MakeSourceLines(filename string, s []byte) []Line {
var ret []Line
if len(s) == 0 {
return ret
}

lastRng := hcl.Range{
Filename: filename,
Start: hcl.InitialPos,
End: hcl.InitialPos,
}
sc := hcl.NewRangeScanner(s, filename, scanLines)
for sc.Scan() {
ret = append(ret, sourceLine{
content: sc.Bytes(),
rng: sc.Range(),
})
lastRng = sc.Range()
}

// Account for the last (virtual) user-percieved line
ret = append(ret, sourceLine{
content: []byte{},
rng: hcl.Range{
Filename: lastRng.Filename,
Start: lastRng.End,
End: lastRng.End,
},
})

return ret
}

Expand Down
6 changes: 3 additions & 3 deletions internal/source/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import (

func TestMakeSourceLines_empty(t *testing.T) {
lines := MakeSourceLines("/test.tf", []byte{})
if len(lines) != 0 {
t.Fatalf("Expected no lines from empty file, %d parsed:\n%#v",
if len(lines) != 1 {
t.Fatalf("Expected 1 line from empty file, %d parsed:\n%#v",
len(lines), lines)
}
}

func TestMakeSourceLines_success(t *testing.T) {
lines := MakeSourceLines("/test.tf", []byte("\n\n\n\n"))
expectedLines := 4
expectedLines := 5
if len(lines) != expectedLines {
t.Fatalf("Expected exactly %d lines, %d parsed",
expectedLines, len(lines))
Expand Down

0 comments on commit a397452

Please sign in to comment.