2017-09-11 23:40:37 +00:00
|
|
|
package hclsyntax
|
2017-06-04 16:52:16 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2018-11-26 23:35:46 +00:00
|
|
|
|
2019-09-09 23:08:19 +00:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
2017-06-04 16:52:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type navigation struct {
|
|
|
|
root *Body
|
|
|
|
}
|
|
|
|
|
2018-01-27 18:42:01 +00:00
|
|
|
// Implementation of hcled.ContextString
|
2017-06-04 16:52:16 +00:00
|
|
|
func (n navigation) ContextString(offset int) string {
|
|
|
|
// We will walk our top-level blocks until we find one that contains
|
|
|
|
// the given offset, and then construct a representation of the header
|
|
|
|
// of the block.
|
|
|
|
|
|
|
|
var block *Block
|
|
|
|
for _, candidate := range n.root.Blocks {
|
|
|
|
if candidate.Range().ContainsOffset(offset) {
|
|
|
|
block = candidate
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if block == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(block.Labels) == 0 {
|
|
|
|
// Easy case!
|
|
|
|
return block.Type
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
buf.WriteString(block.Type)
|
|
|
|
for _, label := range block.Labels {
|
|
|
|
fmt.Fprintf(buf, " %q", label)
|
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|
2018-11-26 23:35:46 +00:00
|
|
|
|
|
|
|
func (n navigation) ContextDefRange(offset int) hcl.Range {
|
|
|
|
var block *Block
|
|
|
|
for _, candidate := range n.root.Blocks {
|
|
|
|
if candidate.Range().ContainsOffset(offset) {
|
|
|
|
block = candidate
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if block == nil {
|
|
|
|
return hcl.Range{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return block.DefRange()
|
|
|
|
}
|