Refactor and significantly clean up the code.

There should not be any behavior changes. Issues 5551 and 4449 continue
to be fixed, but this code is much cleaner and easier to understand.
Achieve parity and commonality between common code of
cmd/gofmt/gofmt.go and go/format/format.go, paving the way to future
code deduplication.
Consolidate two helpers into one.