elvish/pkg/testutil/dedent.go
Qi Xiao 980cf009ca Parse comment blocks preceding var and fn declarations as doc comments.
Convert all .elv files (including .d.elv files) to use this new format.
2022-11-22 22:59:31 +00:00

25 lines
734 B
Go

package testutil
import (
"fmt"
"strings"
)
// Dedent removes an optional leading newline, and removes the indentation
// present in the first line from all subsequent non-empty lines.
//
// Dedent panics if any non-empty line does not start with the same indentation
// as the first line.
func Dedent(text string) string {
lines := strings.Split(strings.TrimPrefix(text, "\n"), "\n")
line0 := lines[0]
indent := line0[:len(line0)-len(strings.TrimLeft(lines[0], " \t"))]
for i, line := range lines {
if !strings.HasPrefix(line, indent) && line != "" {
panic(fmt.Sprintf("line %d is not empty but doesn't start with %q", i, indent))
}
lines[i] = strings.TrimPrefix(line, indent)
}
return strings.Join(lines, "\n")
}