diff --git a/format/format.go b/format/format.go index 3fc3bdd..7be3ed0 100644 --- a/format/format.go +++ b/format/format.go @@ -436,11 +436,16 @@ func (f *fumpter) applyPre(c *astutil.Cursor) { } } // If none of the comment group's lines look like a - // directive or code, add spaces, if needed. + // directive or code, add or remove spaces, as appropriate. for _, comment := range group.List { body := strings.TrimPrefix(comment.Text, "//") r, _ := utf8.DecodeRuneInString(body) - if !unicode.IsSpace(r) { + if unicode.IsSpace(r) { + body = strings.TrimLeftFunc(body, unicode.IsSpace) + if rxCommentDirective.MatchString(body) { + comment.Text = "//" + body + } + } else { comment.Text = "// " + body } } diff --git a/testdata/script/comment-spaced.txtar b/testdata/script/comment-spaced.txtar index aac33b1..caa8263 100644 --- a/testdata/script/comment-spaced.txtar +++ b/testdata/script/comment-spaced.txtar @@ -32,6 +32,8 @@ package p //noinspection foo,bar +// directive:withleadingspace + //not actually: a directive //https://just.one/url @@ -99,6 +101,8 @@ package p //noinspection foo,bar +//directive:withleadingspace + // not actually: a directive // https://just.one/url