1package hclsyntax 2 3import ( 4 "bytes" 5 "fmt" 6 7 "github.com/hashicorp/hcl2/hcl" 8) 9 10type navigation struct { 11 root *Body 12} 13 14// Implementation of hcled.ContextString 15func (n navigation) ContextString(offset int) string { 16 // We will walk our top-level blocks until we find one that contains 17 // the given offset, and then construct a representation of the header 18 // of the block. 19 20 var block *Block 21 for _, candidate := range n.root.Blocks { 22 if candidate.Range().ContainsOffset(offset) { 23 block = candidate 24 break 25 } 26 } 27 28 if block == nil { 29 return "" 30 } 31 32 if len(block.Labels) == 0 { 33 // Easy case! 34 return block.Type 35 } 36 37 buf := &bytes.Buffer{} 38 buf.WriteString(block.Type) 39 for _, label := range block.Labels { 40 fmt.Fprintf(buf, " %q", label) 41 } 42 return buf.String() 43} 44 45func (n navigation) ContextDefRange(offset int) hcl.Range { 46 var block *Block 47 for _, candidate := range n.root.Blocks { 48 if candidate.Range().ContainsOffset(offset) { 49 block = candidate 50 break 51 } 52 } 53 54 if block == nil { 55 return hcl.Range{} 56 } 57 58 return block.DefRange() 59} 60