Skip to content
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

decoder: Introduce helper functions for completion inside ObjectConsExpr #211

Merged
merged 3 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions decoder/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package decoder

import (
"context"
"unicode/utf8"

"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl-lang/reference"
Expand Down Expand Up @@ -167,3 +168,39 @@ func newEmptyExpressionAtPos(filename string, pos hcl.Pos) hcl.Expression {
},
}
}

// recoverLeftBytes seeks left from given pos in given slice of bytes
// and recovers all bytes up until f matches, including that match.
// This allows recovery of incomplete configuration which is not
// present in the parsed AST during completion.
//
// Zero bytes is returned if no match was found.
func recoverLeftBytes(b []byte, pos hcl.Pos, f func(byteOffset int, r rune) bool) []byte {
firstRune, size := utf8.DecodeLastRune(b[:pos.Byte])
offset := pos.Byte - size

// check for early match
if f(pos.Byte, firstRune) {
return b[offset:pos.Byte]
}

for offset > 0 {
nextRune, size := utf8.DecodeLastRune(b[:offset])
if f(offset, nextRune) {
// record the matched offset
// and include the matched last rune
startByte := offset - size
return b[startByte:pos.Byte]
}
offset -= size
}

return []byte{}
}

// isObjectItemTerminatingRune returns true if the given rune
// is considered a left terminating character for an item
// in hclsyntax.ObjectConsExpr.
func isObjectItemTerminatingRune(r rune) bool {
return r == '\n' || r == ',' || r == '{'
}
63 changes: 63 additions & 0 deletions decoder/expression_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package decoder

import (
"bytes"
"fmt"
"testing"
"unicode"

"github.com/hashicorp/hcl/v2"
)

var (
_ Expression = Any{}
_ Expression = Keyword{}
Expand Down Expand Up @@ -33,3 +42,57 @@ var (
_ ReferenceTargetsExpression = Reference{}
_ ReferenceTargetsExpression = Tuple{}
)

func TestRecoverLeftBytes(t *testing.T) {
testCases := []struct {
b []byte
pos hcl.Pos
f func(int, rune) bool
expectedBytes []byte
}{
{
[]byte(`toot foobar`),
hcl.Pos{Line: 1, Column: 13, Byte: 12},
func(i int, r rune) bool {
return unicode.IsSpace(r)
},
[]byte(` foobar`),
},
{
[]byte(`hello👋world and other planets`),
hcl.Pos{Line: 1, Column: 15, Byte: 14},
func(i int, r rune) bool {
return r == '👋'
},
[]byte(`👋world`),
},
{
[]byte(`hello world👋and other planets`),
hcl.Pos{Line: 1, Column: 16, Byte: 15},
func(i int, r rune) bool {
return r == '👋'
},
[]byte(`👋`),
},
{
[]byte(`attr = {
foo = "foo",
bar =
}
`),
hcl.Pos{Line: 3, Column: 9, Byte: 32},
func(i int, r rune) bool {
return r == '\n' || r == ','
},
[]byte("\n bar = "),
},
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
recoveredBytes := recoverLeftBytes(tc.b, tc.pos, tc.f)
if !bytes.Equal(tc.expectedBytes, recoveredBytes) {
t.Fatalf("mismatch!\nexpected: %q\nrecovered: %q\n", string(tc.expectedBytes), string(recoveredBytes))
}
})
}
}