src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/diag/range.go (about)

     1  package diag
     2  
     3  // Ranger wraps the Range method.
     4  type Ranger interface {
     5  	// Range returns the range associated with the value.
     6  	Range() Ranging
     7  }
     8  
     9  // Ranging represents a range [From, To) within an indexable sequence. Structs
    10  // can embed Ranging to satisfy the [Ranger] interface.
    11  //
    12  // Ideally, this type would be called Range. However, doing that means structs
    13  // embedding this type will have Range as a field instead of a method, thus not
    14  // implementing the [Ranger] interface.
    15  type Ranging struct {
    16  	From int
    17  	To   int
    18  }
    19  
    20  // Range returns the Ranging itself.
    21  func (r Ranging) Range() Ranging { return r }
    22  
    23  // PointRanging returns a zero-width Ranging at the given point.
    24  func PointRanging(p int) Ranging {
    25  	return Ranging{p, p}
    26  }
    27  
    28  // MixedRanging returns a Ranging from the start position of a to the end
    29  // position of b.
    30  func MixedRanging(a, b Ranger) Ranging {
    31  	return Ranging{a.Range().From, b.Range().To}
    32  }