| 1 | // Copyright 2025 CUE Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package rangesetscip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/ |
| 16 | |
| 17 | import ( |
| 18 | "fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/" |
| 19 | "slicesscip-go gomod github.com/golang/go/src go1.24.0 slices/" |
| 20 | "sortscip-go gomod github.com/golang/go/src go1.24.0 sort/" |
| 21 | ) |
| 22 | |
| 23 | // Range represents a single continuous interval [Start, End). The |
| 24 | // interval includes Start but excludes End. |
| 25 | type Rangescip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/Range# struct { |
| 26 | Startscip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/Range#Start. int |
| 27 | Endscip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/Range#End. int |
| 28 | } |
| 29 | |
| 30 | func (r Rangescip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/Range#) Stringscip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/Range#String().() string { |
| 31 | return fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Sprintf().("%d-%d", r.Startscip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/Range#Start., r.Endscip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/Range#End.) |
| 32 | } |
| 33 | |
| 34 | // RangeSet holds a collection of sorted, non-overlapping ranges. |
| 35 | type RangeSetscip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/RangeSet# struct { |
| 36 | rangesscip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/RangeSet#ranges. []Rangescip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/Range# |
| 37 | } |
| 38 | |
| 39 | // NewRangeSet creates and returns a new, empty RangeSet. |
| 40 | func NewRangeSetscip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/NewRangeSet().() *RangeSetscip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/RangeSet# { |
| 41 | return &RangeSetscip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/RangeSet#{} |
| 42 | } |
| 43 | |
| 44 | // Add incorporates a new range into the set. It finds all existing |
| 45 | // ranges that overlap or are adjacent to the new range and merges |
| 46 | // them into a single, larger range. |
| 47 | func (rs *RangeSetscip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/RangeSet#) Addscip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/RangeSet#Add().(start, end int) { |
| 48 | if start >= end { |
| 49 | return // Ignore empty or invalid ranges. |
| 50 | } |
| 51 | |
| 52 | newRange := Rangescip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/Range#{Startscip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/Range#Start.: start, Endscip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/Range#End.: end} |
| 53 | ranges := rs.rangesscip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/RangeSet#ranges. |
| 54 | |
| 55 | // Find the insertion/merge start point using binary search. |
| 56 | // i is the index of the first range that we might need to merge |
| 57 | // with. This is the first range r where r.End >= newRange.Start. |
| 58 | i := sortscip-go gomod github.com/golang/go/src go1.24.0 sort/.Searchscip-go gomod github.com/golang/go/src go1.24.0 sort/Search().(len(ranges), func(k int) bool { |
| 59 | return ranges[k].Endscip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/Range#End. >= newRange.Startscip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/Range#Start. |
| 60 | }) |
| 61 | |
| 62 | // Find the insertion/merge end point. j is the index of the first |
| 63 | // range that starts after our new range. |
| 64 | j := sortscip-go gomod github.com/golang/go/src go1.24.0 sort/.Searchscip-go gomod github.com/golang/go/src go1.24.0 sort/Search().(len(ranges), func(k int) bool { |
| 65 | return ranges[k].Startscip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/Range#Start. > newRange.Endscip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/Range#End. |
| 66 | }) |
| 67 | |
| 68 | // Note that sort.Search returns its first argument if the func |
| 69 | // never returns true. So for example, if there is no range that |
| 70 | // either starts or ends beyond start then i = j = len(ranges). |
| 71 | |
| 72 | // If i < j, newRange overlaps with ranges[i:j]. |
| 73 | if i < j { |
| 74 | // Merge the new range with all the overlapping ranges. |
| 75 | // The merged range starts at the minimum of the starts. |
| 76 | if ranges[i].Startscip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/Range#Start. < newRange.Startscip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/Range#Start. { |
| 77 | newRange.Startscip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/Range#Start. = ranges[i].Startscip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/Range#Start. |
| 78 | } |
| 79 | // And ends at the maximum of the ends. |
| 80 | if ranges[j-1].Endscip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/Range#End. > newRange.Endscip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/Range#End. { |
| 81 | newRange.Endscip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/Range#End. = ranges[j-1].Endscip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/Range#End. |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // Replace the ranges[i:j] with the single newRange. |
| 86 | suffix := ranges[j:] |
| 87 | ranges = ranges[:i] |
| 88 | ranges = slicesscip-go gomod github.com/golang/go/src go1.24.0 slices/.Growscip-go gomod github.com/golang/go/src go1.24.0 slices/Grow().(ranges, 1+len(suffix)) |
| 89 | ranges = ranges[:i+1+len(suffix)] |
| 90 | copy(ranges[i+1:], suffix) |
| 91 | ranges[i] = newRange |
| 92 | rs.rangesscip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/RangeSet#ranges. = ranges |
| 93 | } |
| 94 | |
| 95 | // Contains reports if an offset is within any of the ranges in the |
| 96 | // set. |
| 97 | func (rs *RangeSetscip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/RangeSet#) Containsscip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/RangeSet#Contains().(offset int) bool { |
| 98 | // Use sort.Search to find the index of the first range that |
| 99 | // *could* contain the offset. This would be the first range whose |
| 100 | // end is > offset. |
| 101 | ranges := rs.rangesscip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/RangeSet#ranges. |
| 102 | i := sortscip-go gomod github.com/golang/go/src go1.24.0 sort/.Searchscip-go gomod github.com/golang/go/src go1.24.0 sort/Search().(len(ranges), func(k int) bool { |
| 103 | return ranges[k].Endscip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/Range#End. > offset |
| 104 | }) |
| 105 | |
| 106 | if i < len(ranges) { |
| 107 | return ranges[i].Startscip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/Range#Start. <= offset |
| 108 | } |
| 109 | |
| 110 | return false |
| 111 | } |
| 112 | |
| 113 | // Ranges returns the sorted ranges that make up this range set. |
| 114 | func (rs *RangeSetscip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/RangeSet#) Rangesscip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/RangeSet#Ranges().() []Rangescip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/Range# { |
| 115 | return slicesscip-go gomod github.com/golang/go/src go1.24.0 slices/.Clonescip-go gomod github.com/golang/go/src go1.24.0 slices/Clone().(rs.rangesscip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/RangeSet#ranges.) |
| 116 | } |
| 117 | |
| 118 | func (rs *RangeSetscip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/RangeSet#) Stringscip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/RangeSet#String().() string { |
| 119 | return fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Sprintf().("RangeSet{%v}", rs.rangesscip-go gomod cuelang.org/go . `cuelang.org/go/internal/lsp/rangeset`/RangeSet#ranges.) |
| 120 | } |