Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
encoding/jsonschema: wrap comment lines
Browse files Browse the repository at this point in the history
Change-Id: I482ab0266c66f226490cb93d385da5f34d7f48d2
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/5650
Reviewed-by: roger peppe <rogpeppe@gmail.com>
  • Loading branch information
mpvl committed Apr 15, 2020
1 parent 0059b2b commit ed4fc4d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
24 changes: 14 additions & 10 deletions encoding/jsonschema/testdata/basic.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,31 @@
"person": {
"description": "A person is a human being.",
"type": "object",
"required": [ "name" ],
"required": [ "name" ],
"properties": {
"name": { "type": "string" },
"address": {
"description": "where does this person live?",
"type": "string"
},
"address": {
"description": "where does this person live?",
"type": "string"
},
"children": {
"description": "A very large comment that will be wrapped after a certain line length. Let's keep on going and see what happens.",
"type": "array",
"items": { "type": "string" },
"default": []
},
"home phone": {
"type": "string",
"deprecated": true
}
"home phone": {
"type": "string",
"deprecated": true
}
}
}
}
}

-- out.cue --
// Main schema
//
//
// Specify who you are and all.
Schema :: _ @jsonschema(schema="http://json-schema.org/draft-07/schema#")
Schema :: {
Expand All @@ -43,6 +44,9 @@ Schema :: {

// where does this person live?
address?: string

// A very large comment that will be wrapped after a certain line
// length. Let's keep on going and see what happens.
children?: [...string]
"home phone"?: string @deprecated()
...
Expand Down
23 changes: 21 additions & 2 deletions internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,28 @@ func NewComment(isDoc bool, s string) *ast.CommentGroup {
}
scanner := bufio.NewScanner(strings.NewReader(s))
for scanner.Scan() {
cg.List = append(cg.List, &ast.Comment{Text: "// " + scanner.Text()})
scanner := bufio.NewScanner(strings.NewReader(scanner.Text()))
scanner.Split(bufio.ScanWords)
const maxRunesPerLine = 66
count := 2
buf := strings.Builder{}
buf.WriteString("//")
for scanner.Scan() {
s := scanner.Text()
n := len([]rune(s)) + 1
if count+n > maxRunesPerLine && count > 3 {
cg.List = append(cg.List, &ast.Comment{Text: buf.String()})
count = 3
buf.Reset()
buf.WriteString("//")
}
buf.WriteString(" ")
buf.WriteString(s)
count += n
}
cg.List = append(cg.List, &ast.Comment{Text: buf.String()})
}
if last := len(cg.List) - 1; cg.List[last].Text == "// " {
if last := len(cg.List) - 1; cg.List[last].Text == "//" {
cg.List = cg.List[:last]
}
return cg
Expand Down

0 comments on commit ed4fc4d

Please sign in to comment.