From 64d2a7c8945ba05af859901f5e248f1befdd8621 Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Tue, 3 Dec 2013 09:43:15 +0100 Subject: Imported Upstream version 1.2 --- src/pkg/go/token/position.go | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) (limited to 'src/pkg/go/token/position.go') diff --git a/src/pkg/go/token/position.go b/src/pkg/go/token/position.go index f5d999561..e6f0ae6a6 100644 --- a/src/pkg/go/token/position.go +++ b/src/pkg/go/token/position.go @@ -97,7 +97,7 @@ type File struct { size int // file size as provided to AddFile // lines and infos are protected by set.mutex - lines []int + lines []int // lines contains the offset of the first character for each line (the first entry is always 0) infos []lineInfo } @@ -136,6 +136,29 @@ func (f *File) AddLine(offset int) { f.set.mutex.Unlock() } +// MergeLine merges a line with the following line. It is akin to replacing +// the newline character at the end of the line with a space (to not change the +// remaining offsets). To obtain the line number, consult e.g. Position.Line. +// MergeLine will panic if given an invalid line number. +// +func (f *File) MergeLine(line int) { + if line <= 0 { + panic("illegal line number (line numbering starts at 1)") + } + f.set.mutex.Lock() + defer f.set.mutex.Unlock() + if line >= len(f.lines) { + panic("illegal line number") + } + // To merge the line numbered with the line numbered , + // we need to remove the entry in lines corresponding to the line + // numbered . The entry in lines corresponding to the line + // numbered is located at index , since indices in lines + // are 0-based and line numbers are 1-based. + copy(f.lines[line:], f.lines[line+1:]) + f.lines = f.lines[:len(f.lines)-1] +} + // SetLines sets the line offsets for a file and returns true if successful. // The line offsets are the offsets of the first character of each line; // for instance for the content "ab\nc\n" the line offsets are {0, 3}. @@ -314,7 +337,8 @@ func (s *FileSet) Base() int { // AddFile adds a new file with a given filename, base offset, and file size // to the file set s and returns the file. Multiple files may have the same // name. The base offset must not be smaller than the FileSet's Base(), and -// size must not be negative. +// size must not be negative. As a special case, if a negative base is provided, +// the current value of the FileSet's Base() is used instead. // // Adding the file will set the file set's Base() value to base + size + 1 // as the minimum base value for the next file. The following relationship @@ -329,6 +353,9 @@ func (s *FileSet) Base() int { func (s *FileSet) AddFile(filename string, base, size int) *File { s.mutex.Lock() defer s.mutex.Unlock() + if base < 0 { + base = s.base + } if base < s.base || size < 0 { panic("illegal base or size") } -- cgit v1.2.3