diff options
author | Robert Griesemer <gri@golang.org> | 2010-12-13 17:08:40 -0800 |
---|---|---|
committer | Robert Griesemer <gri@golang.org> | 2010-12-13 17:08:40 -0800 |
commit | 6fcae0f04431b6a02959c9d6476051bed967523d (patch) | |
tree | e8129ea81c6481b4e792b788273095b9c35b9654 | |
parent | 52c9fb6f3dd0648c6ccb87ee20818b09ab43f9c9 (diff) | |
download | go-6fcae0f04431b6a02959c9d6476051bed967523d.tar.gz go-6fcae0f04431b6a02959c9d6476051bed967523d.zip |
token/position: provide files iterator
R=golang-dev, r2
CC=golang-dev
https://golang.org/cl/3541044
-rw-r--r-- | src/pkg/go/token/position.go | 22 | ||||
-rw-r--r-- | src/pkg/go/token/position_test.go | 18 |
2 files changed, 40 insertions, 0 deletions
diff --git a/src/pkg/go/token/position.go b/src/pkg/go/token/position.go index 8eb8d138e6..0044a0ed77 100644 --- a/src/pkg/go/token/position.go +++ b/src/pkg/go/token/position.go @@ -385,3 +385,25 @@ func (s *FileSet) AddFile(filename string, base, size int) *File { s.files = append(s.files, f) return f } + + +// Files returns the files added to the file set. +func (s *FileSet) Files() <-chan *File { + ch := make(chan *File) + go func() { + for i := 0; ; i++ { + var f *File + s.mutex.RLock() + if i < len(s.files) { + f = s.files[i] + } + s.mutex.RUnlock() + if f == nil { + break + } + ch <- f + } + close(ch) + }() + return ch +} diff --git a/src/pkg/go/token/position_test.go b/src/pkg/go/token/position_test.go index bc10ef6c0a..1cffcc3c27 100644 --- a/src/pkg/go/token/position_test.go +++ b/src/pkg/go/token/position_test.go @@ -138,3 +138,21 @@ func TestLineInfo(t *testing.T) { checkPos(t, msg, fset.Position(p), Position{"bar", offs, 42, col}) } } + + +func TestFiles(t *testing.T) { + fset := NewFileSet() + for i, test := range tests { + fset.AddFile(test.filename, fset.Base(), test.size) + j := 0 + for g := range fset.Files() { + if g.Name() != tests[j].filename { + t.Errorf("expected filename = %s; got %s", tests[j].filename, g.Name()) + } + j++ + } + if j != i+1 { + t.Errorf("expected %d files; got %d", i+1, j) + } + } +} |