1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package span contains support for representing with positions and ranges in
15 // Span represents a source code range in standardized form.
20 // Point represents a single point within a file.
21 // In general this should only be used as part of a Span, as on its own it
22 // does not carry enough information.
29 Start point `json:"start"`
30 End point `json:"end"`
34 Line int `json:"line"`
35 Column int `json:"column"`
36 Offset int `json:"offset"`
39 // Invalid is a span that reports false from IsValid
40 var Invalid = Span{v: span{Start: invalidPoint.v, End: invalidPoint.v}}
42 var invalidPoint = Point{v: point{Line: 0, Column: 0, Offset: -1}}
44 // Converter is the interface to an object that can convert between line:column
45 // and offset forms for a single file.
46 type Converter interface {
47 //ToPosition converts from an offset to a line:column pair.
48 ToPosition(offset int) (int, int, error)
49 //ToOffset converts from a line:column pair to an offset.
50 ToOffset(line, col int) (int, error)
53 func New(uri URI, start Point, end Point) Span {
54 s := Span{v: span{URI: uri, Start: start.v, End: end.v}}
59 func NewPoint(line, col, offset int) Point {
60 p := Point{v: point{Line: line, Column: col, Offset: offset}}
65 func Compare(a, b Span) int {
66 if r := CompareURI(a.URI(), b.URI()); r != 0 {
69 if r := comparePoint(a.v.Start, b.v.Start); r != 0 {
72 return comparePoint(a.v.End, b.v.End)
75 func ComparePoint(a, b Point) int {
76 return comparePoint(a.v, b.v)
79 func comparePoint(a, b point) int {
81 if a.Offset < b.Offset {
84 if a.Offset > b.Offset {
95 if a.Column < b.Column {
98 if a.Column > b.Column {
104 func (s Span) HasPosition() bool { return s.v.Start.hasPosition() }
105 func (s Span) HasOffset() bool { return s.v.Start.hasOffset() }
106 func (s Span) IsValid() bool { return s.v.Start.isValid() }
107 func (s Span) IsPoint() bool { return s.v.Start == s.v.End }
108 func (s Span) URI() URI { return s.v.URI }
109 func (s Span) Start() Point { return Point{s.v.Start} }
110 func (s Span) End() Point { return Point{s.v.End} }
111 func (s *Span) MarshalJSON() ([]byte, error) { return json.Marshal(&s.v) }
112 func (s *Span) UnmarshalJSON(b []byte) error { return json.Unmarshal(b, &s.v) }
114 func (p Point) HasPosition() bool { return p.v.hasPosition() }
115 func (p Point) HasOffset() bool { return p.v.hasOffset() }
116 func (p Point) IsValid() bool { return p.v.isValid() }
117 func (p *Point) MarshalJSON() ([]byte, error) { return json.Marshal(&p.v) }
118 func (p *Point) UnmarshalJSON(b []byte) error { return json.Unmarshal(b, &p.v) }
119 func (p Point) Line() int {
120 if !p.v.hasPosition() {
121 panic(fmt.Errorf("position not set in %v", p.v))
125 func (p Point) Column() int {
126 if !p.v.hasPosition() {
127 panic(fmt.Errorf("position not set in %v", p.v))
131 func (p Point) Offset() int {
132 if !p.v.hasOffset() {
133 panic(fmt.Errorf("offset not set in %v", p.v))
138 func (p point) hasPosition() bool { return p.Line > 0 }
139 func (p point) hasOffset() bool { return p.Offset >= 0 }
140 func (p point) isValid() bool { return p.hasPosition() || p.hasOffset() }
141 func (p point) isZero() bool {
142 return (p.Line == 1 && p.Column == 1) || (!p.hasPosition() && p.Offset == 0)
145 func (s *span) clean() {
146 //this presumes the points are already clean
147 if !s.End.isValid() || (s.End == point{}) {
152 func (p *point) clean() {
163 if p.Offset == 0 && (p.Line > 1 || p.Column > 1) {
168 // Format implements fmt.Formatter to print the Location in a standard form.
169 // The format produced is one that can be read back in using Parse.
170 func (s Span) Format(f fmt.State, c rune) {
171 fullForm := f.Flag('+')
172 preferOffset := f.Flag('#')
173 // we should always have a uri, simplify if it is file format
174 //TODO: make sure the end of the uri is unambiguous
175 uri := string(s.v.URI)
178 } else if !fullForm {
179 uri = s.v.URI.Filename()
182 if !s.IsValid() || (!fullForm && s.v.Start.isZero() && s.v.End.isZero()) {
185 // see which bits of start to write
186 printOffset := s.HasOffset() && (fullForm || preferOffset || !s.HasPosition())
187 printLine := s.HasPosition() && (fullForm || !printOffset)
188 printColumn := printLine && (fullForm || (s.v.Start.Column > 1 || s.v.End.Column > 1))
191 fmt.Fprintf(f, "%d", s.v.Start.Line)
194 fmt.Fprintf(f, ":%d", s.v.Start.Column)
197 fmt.Fprintf(f, "#%d", s.v.Start.Offset)
199 // start is written, do we need end?
203 // we don't print the line if it did not change
204 printLine = fullForm || (printLine && s.v.End.Line > s.v.Start.Line)
207 fmt.Fprintf(f, "%d", s.v.End.Line)
213 fmt.Fprintf(f, "%d", s.v.End.Column)
216 fmt.Fprintf(f, "#%d", s.v.End.Offset)
220 func (s Span) WithPosition(c Converter) (Span, error) {
221 if err := s.update(c, true, false); err != nil {
227 func (s Span) WithOffset(c Converter) (Span, error) {
228 if err := s.update(c, false, true); err != nil {
234 func (s Span) WithAll(c Converter) (Span, error) {
235 if err := s.update(c, true, true); err != nil {
241 func (s *Span) update(c Converter, withPos, withOffset bool) error {
243 return fmt.Errorf("cannot add information to an invalid span")
245 if withPos && !s.HasPosition() {
246 if err := s.v.Start.updatePosition(c); err != nil {
249 if s.v.End.Offset == s.v.Start.Offset {
251 } else if err := s.v.End.updatePosition(c); err != nil {
255 if withOffset && (!s.HasOffset() || (s.v.End.hasPosition() && !s.v.End.hasOffset())) {
256 if err := s.v.Start.updateOffset(c); err != nil {
259 if s.v.End.Line == s.v.Start.Line && s.v.End.Column == s.v.Start.Column {
260 s.v.End.Offset = s.v.Start.Offset
261 } else if err := s.v.End.updateOffset(c); err != nil {
268 func (p *point) updatePosition(c Converter) error {
269 line, col, err := c.ToPosition(p.Offset)
278 func (p *point) updateOffset(c Converter) error {
279 offset, err := c.ToOffset(p.Line, p.Column)