// MergeLine will panic if given an invalid line number.
//
func (f *File) MergeLine(line int) {
- if line <= 0 {
+ if line < 1 {
panic("illegal line number (line numbering starts at 1)")
}
f.mutex.Lock()
f.mutex.Unlock()
}
+// LineStart returns the Pos value of the start of the specified line.
+// It ignores any alternative positions set using AddLineColumnInfo.
+// LineStart panics if the 1-based line number is invalid.
+func (f *File) LineStart(line int) Pos {
+ if line < 1 {
+ panic("illegal line number (line numbering starts at 1)")
+ }
+ f.mutex.Lock()
+ defer f.mutex.Unlock()
+ if line > len(f.lines) {
+ panic("illegal line number")
+ }
+ return Pos(f.base + f.lines[line-1])
+}
+
// A lineInfo object describes alternative file, line, and column
// number information (such as provided via a //line directive)
// for a given file offset.
checkPos(t, "3. Position", got3, want)
}
}
+
+func TestLineStart(t *testing.T) {
+ const src = "one\ntwo\nthree\n"
+ fset := NewFileSet()
+ f := fset.AddFile("input", -1, len(src))
+ f.SetLinesForContent([]byte(src))
+
+ for line := 1; line <= 3; line++ {
+ pos := f.LineStart(line)
+ position := fset.Position(pos)
+ if position.Line != line || position.Column != 1 {
+ t.Errorf("LineStart(%d) returned wrong pos %d: %s", line, pos, position)
+ }
+ }
+}