• Dmitri Shuralyov's avatar
    go/format, cmd/gofmt: fix issues with partial Go code with indent · 912ec199
    Dmitri Shuralyov authored
    Fixes #5551.
    Fixes #4449.
    
    Adds tests for both issues.
    
    Note that the two issues occur only when formatting partial Go code
    with indent.
    
    The best way to understand the change is as follows: I took the code
    of cmd/gofmt and go/format, combined it into one unified code that
    does not suffer from either 4449 nor 5551, and then applied that code
    to both cmd/gofmt and go/format.
    
    As a result, there is now much more identical code between the two
    packages, making future code deduplication easier (it was not possible
    to do that now without adding public APIs, which I was advised not to
    do at this time).
    
    More specifically, I took the parse() of cmd/gofmt which correctly
    preserves comments (issue 5551) and modified it to fix issue where
    it would sometimes modify literal values (issue 4449).
    
    I ended up removing the matchSpace() function because it no longer
    needed to do some of its work (insert indent), and a part of its work
    had to be done in advance (determining the indentation of first code
    line), because that calculation is required for cfg.Fprint() to run.
    
    adjustIndent is used to adjust the indent of cfg.Fprint() to compensate
    for the body of wrapper func being indented by one level. This allows
    to get rid of the bytes.Replace text manipulation of inner content,
    which was problematic and sometimes altered raw string literals (issue
    4449). This means that sometimes the value of cfg.Indent is negative,
    but that works as expected.
    
    So now the algorithm for formatting partial Go code is:
    
    1. Determine and prepend leading space of original source.
    2. Determine and prepend indentation of first code line.
    3. Format and write partial Go code (with all of its leading &
       trailing space trimmed).
    4. Determine and append trailing space of original source.
    
    LGTM=gri
    R=golang-codereviews, bradfitz, gri
    CC=golang-codereviews
    https://golang.org/cl/142360043
    912ec199
Name
Last commit
Last update
..
5a Loading commit data...
5c Loading commit data...
5g Loading commit data...
5l Loading commit data...
6a Loading commit data...
6c Loading commit data...
6g Loading commit data...
6l Loading commit data...
8a Loading commit data...
8c Loading commit data...
8g Loading commit data...
8l Loading commit data...
addr2line Loading commit data...
api Loading commit data...
cc Loading commit data...
cgo Loading commit data...
dist Loading commit data...
fix Loading commit data...
gc Loading commit data...
go Loading commit data...
gofmt Loading commit data...
internal/objfile Loading commit data...
ld Loading commit data...
link Loading commit data...
nm Loading commit data...
objdump Loading commit data...
pack Loading commit data...
yacc Loading commit data...