| 1 | // Copyright 2023 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. |
| 4 | |
| 5 | // package tokeninternal provides convenient helpers for the go/token package. |
| 6 | package tokeninternal |
| 7 | |
| 8 | import ( |
| 9 | "go/token" |
| 10 | "slices" |
| 11 | ) |
| 12 | |
| 13 | // FileSetFor returns a new FileSet containing a sequence of new Files with |
| 14 | // the same base, size, and line as the input files, for use in APIs that |
| 15 | // require a FileSet. |
| 16 | // |
| 17 | // Precondition: the input files must be non-overlapping, and sorted in order |
| 18 | // of their Base. |
| 19 | func FileSetFor(files ...*token.File) *token.FileSet { |
| 20 | fset := token.NewFileSet() |
| 21 | fset.AddExistingFiles(files...) |
| 22 | return fset |
| 23 | } |
| 24 | |
| 25 | // CloneFileSet creates a new FileSet holding all files in fset. It does not |
| 26 | // create copies of the token.Files in fset: they are added to the resulting |
| 27 | // FileSet unmodified. |
| 28 | func CloneFileSet(fset *token.FileSet) *token.FileSet { |
| 29 | return FileSetFor(slices.Collect(fset.Iterate)...) |
| 30 | } |