webtools/cmds/golines/format.go

30 lines
739 B
Go
Raw Normal View History

2026-09-06 09:23:55 +10:00
package main
import (
"fmt"
"github.com/golangci/golines/shorten"
)
func formatSource(source string, maxLen int) (output []byte, err error) {
// Dependency panics must not escape the JS callback and terminate the WASM runtime.
defer func() {
if recovered := recover(); recovered != nil {
output = nil
err = fmt.Errorf("golines could not format this input (internal panic: %v). Ensure the input is a complete Go file, including a package declaration", recovered)
}
}()
tabLen := 4
if maxLen < 80 {
tabLen = 2
}
return shorten.NewShortener(&shorten.Config{
MaxLen: maxLen,
TabLen: tabLen,
ShortenComments: true,
ReformatTags: true,
ChainSplitDots: true,
}).Process([]byte(source))
}