MCPcopy
hub / github.com/hashicorp/hcl / partitionTokens

Function partitionTokens

hclwrite/parser.go:542–571  ·  view source on GitHub ↗

partitionTokens takes a sequence of tokens and a hcl.Range and returns two indices within the token sequence that correspond with the range boundaries, such that the slice operator could be used to produce three token sequences for before, within, and after respectively: start, end := partitionTok

(toks hclsyntax.Tokens, rng hcl.Range)

Source from the content-addressed store, hash-verified

540// The tokens are assumed to be in source order and non-overlapping, which
541// will be true if the token sequence from the scanner is used directly.
542func partitionTokens(toks hclsyntax.Tokens, rng hcl.Range) (start, end int) {
543 // We use a linear search here because we assume that in most cases our
544 // target range is close to the beginning of the sequence, and the sequences
545 // are generally small for most reasonable files anyway.
546 for i := 0; ; i++ {
547 if i >= len(toks) {
548 // No tokens for the given range at all!
549 return len(toks), len(toks)
550 }
551
552 if toks[i].Range.Start.Byte >= rng.Start.Byte {
553 start = i
554 break
555 }
556 }
557
558 for i := start; ; i++ {
559 if i >= len(toks) {
560 // The range "hangs off" the end of the token sequence
561 return start, len(toks)
562 }
563
564 if toks[i].Range.Start.Byte >= rng.End.Byte {
565 end = i // end marker is exclusive
566 break
567 }
568 }
569
570 return start, end
571}
572
573// partitionLeadCommentTokens takes a sequence of tokens that is assumed
574// to immediately precede a construct that can have lead comment tokens,

Callers 3

TestPartitionTokensFunction · 0.85
PartitionMethod · 0.85

Calls

no outgoing calls

Tested by 1

TestPartitionTokensFunction · 0.68