-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Move addDocComment() into separate helper function * Reformat comments * Add blank lines before comment blocks * Use Regexp to translate <br/> * Use Index() and LastIndex() to simplify choosing breakpoints in lines * Clarify comment Co-authored-by: Dave Fellows <dave.fellows@microsoft.com> * Fix comment * Change method to internal, but document it anyway * Fix search/replace typo & clarify comments * Update golden files with modified comments * Move definition of brRegex next to formatDocComment() * Add missing file headers Co-authored-by: Dave Fellows <dave.fellows@microsoft.com> Co-authored-by: George Pollard <gpollard@microsoft.com>
- Loading branch information
1 parent
e255d6a
commit 9f5d363
Showing
24 changed files
with
280 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Copyright (c) Microsoft Corporation. | ||
* Licensed under the MIT license. | ||
*/ | ||
|
||
package astmodel | ||
|
||
import ( | ||
"go/ast" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
// Utility methods for adding comments | ||
|
||
func addDocComment(commentList *[]*ast.Comment, comment string, width int) { | ||
for _, c := range formatDocComment(comment, width) { | ||
line := strings.TrimSpace(c) | ||
if !strings.HasPrefix(line, "//") { | ||
line = "//" + line | ||
} | ||
|
||
if *commentList == nil { | ||
line = "\n" + line | ||
} | ||
|
||
*commentList = append(*commentList, &ast.Comment{ | ||
Text: line, | ||
}) | ||
} | ||
} | ||
|
||
// formatDocComment splits the supplied comment string up ready for use as a documentation comment | ||
func formatDocComment(comment string, width int) []string { | ||
// Remove markdown bolding | ||
text := strings.ReplaceAll(comment, "**", "") | ||
|
||
// Turn <br> and <br/> into \n | ||
text = brRegex.ReplaceAllLiteralString(text, "\n") | ||
|
||
// Split into individual lines | ||
lines := strings.Split(text, "\n") | ||
|
||
// Trim whitespace | ||
for i, l := range lines { | ||
lines[i] = strings.TrimSpace(l) | ||
} | ||
|
||
// Wordwrap and return | ||
return docCommentWrap(lines, width) | ||
} | ||
|
||
var brRegex = regexp.MustCompile("<br[^/>]*/?>") | ||
|
||
func docCommentWrap(lines []string, width int) []string { | ||
var result []string | ||
for _, l := range lines { | ||
result = append(result, wordWrap(l, width)...) | ||
} | ||
|
||
return result | ||
} | ||
|
||
func wordWrap(text string, width int) []string { | ||
var result []string | ||
|
||
start := 0 | ||
for start < len(text) { | ||
finish := findBreakPoint(text, start, width) | ||
result = append(result, text[start:finish+1]) | ||
start = finish + 1 | ||
} | ||
|
||
return result | ||
} | ||
|
||
// findBreakPoint finds the character at which to break two lines | ||
// Returned index points to the last character that should be included on the line | ||
// If breaking at a space, this will give a trailing space, but allows for | ||
// breaking at other points too as no characters will be omitted. | ||
func findBreakPoint(line string, start int, width int) int { | ||
limit := start + width + 1 | ||
if limit >= len(line) { | ||
return len(line) - 1 | ||
} | ||
|
||
// Look for a word break within the line | ||
index := strings.LastIndex(line[start:limit], " ") | ||
if index >= 0 { | ||
return start + index | ||
} | ||
|
||
// Line contains continuous text, we don't want to break it in two, so find the end of it | ||
index = strings.Index(line[limit:], " ") | ||
if index >= 0 { | ||
return limit + index | ||
} | ||
|
||
return len(line) - 1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright (c) Microsoft Corporation. | ||
* Licensed under the MIT license. | ||
*/ | ||
|
||
package astmodel | ||
|
||
import ( | ||
. "github.com/onsi/gomega" | ||
"testing" | ||
) | ||
|
||
func TestDocumentationCommentFormatting(t *testing.T) { | ||
cases := []struct { | ||
comment string | ||
results []string | ||
}{ | ||
// Expect short single line comments to be unchanged | ||
{"foo", []string{"foo"}}, | ||
{"bar", []string{"bar"}}, | ||
{"baz", []string{"baz"}}, | ||
// Leading and trailing whitespace is trimmed | ||
{" foo", []string{"foo"}}, | ||
{"foo ", []string{"foo"}}, | ||
{" foo ", []string{"foo"}}, | ||
// Expect comments with embedded newlines to be split | ||
{"foo\nbar", []string{"foo", "bar"}}, | ||
{"foo\nbar\nbaz", []string{"foo", "bar", "baz"}}, | ||
// Expect comments with html style <br> to be split | ||
{"foo<br>bar", []string{"foo", "bar"}}, | ||
{"foo<br>bar<br>baz", []string{"foo", "bar", "baz"}}, | ||
{"foo<br/>bar", []string{"foo", "bar"}}, | ||
{"foo<br/>bar<br/>baz", []string{"foo", "bar", "baz"}}, | ||
// Expect markdown bold to be removed | ||
{"**foo**\nbar", []string{"foo", "bar"}}, | ||
{"foo\n**bar**", []string{"foo", "bar"}}, | ||
{"foo\n**bar**\nbaz", []string{"foo", "bar", "baz"}}, | ||
// Expect long lines to be wrapped | ||
{ | ||
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", | ||
[]string{ | ||
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do ", | ||
"eiusmod tempor incididunt ut labore et dolore magna aliqua.", | ||
}}, | ||
} | ||
|
||
for _, c := range cases { | ||
c := c | ||
t.Run(c.comment, func(t *testing.T) { | ||
g := NewGomegaWithT(t) | ||
lines := formatDocComment(c.comment, 64) | ||
g.Expect(lines).To(Equal(c.results)) | ||
}) | ||
} | ||
} | ||
|
||
func TestWordWrap(t *testing.T) { | ||
cases := []struct { | ||
text string | ||
width int | ||
results []string | ||
}{ | ||
{"this is a simple line of text", 15, []string{"this is a ", "simple line of ", "text"}}, | ||
{"this is a simple line of text", 16, []string{"this is a simple ", "line of text"}}, | ||
{"this is a simple line of text", 20, []string{"this is a simple ", "line of text"}}, | ||
{"this is a simple line of text", 21, []string{"this is a simple line ", "of text"}}, | ||
} | ||
|
||
for _, c := range cases { | ||
c := c | ||
t.Run(c.text, func(t *testing.T) { | ||
g := NewGomegaWithT(t) | ||
lines := wordWrap(c.text, c.width) | ||
g.Expect(lines).To(Equal(c.results)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.