github.com/hashicorp/hcl/v2@v2.20.0/hcled/navigation.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package hcled
     5  
     6  import (
     7  	"github.com/hashicorp/hcl/v2"
     8  )
     9  
    10  type contextStringer interface {
    11  	ContextString(offset int) string
    12  }
    13  
    14  // ContextString returns a string describing the context of the given byte
    15  // offset, if available. An empty string is returned if no such information
    16  // is available, or otherwise the returned string is in a form that depends
    17  // on the language used to write the referenced file.
    18  func ContextString(file *hcl.File, offset int) string {
    19  	if cser, ok := file.Nav.(contextStringer); ok {
    20  		return cser.ContextString(offset)
    21  	}
    22  	return ""
    23  }
    24  
    25  type contextDefRanger interface {
    26  	ContextDefRange(offset int) hcl.Range
    27  }
    28  
    29  func ContextDefRange(file *hcl.File, offset int) hcl.Range {
    30  	if cser, ok := file.Nav.(contextDefRanger); ok {
    31  		defRange := cser.ContextDefRange(offset)
    32  		if !defRange.Empty() {
    33  			return defRange
    34  		}
    35  	}
    36  	return file.Body.MissingItemRange()
    37  }