Skip to content

Commit 7248376

Browse files
keep spaces in comments (#15)
1 parent 893e741 commit 7248376

File tree

6 files changed

+54
-20
lines changed

6 files changed

+54
-20
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ install:
55
go install .
66

77
rerun-install:
8-
rerun -watch . -ignore out -run sh -c 'go install .'
8+
go run github.com/goware/rerun/cmd/rerun -watch . -ignore out -run sh -c 'go install .'
99

1010
rerun-1:
11-
rerun -watch . -ignore out -run sh -c 'go run . -s _examples/e1.ridl'
11+
go run github.com/goware/rerun/cmd/rerun -watch . -ignore out -run sh -c 'go run . -s _examples/e1.ridl'
1212

1313
rerun-2:
14-
rerun -watch . -ignore out -run sh -c 'go run . -s _examples/e2.ridl'
14+
go run github.com/goware/rerun/cmd/rerun -watch . -ignore out -run sh -c 'go run . -s _examples/e2.ridl'
1515

1616
test:
1717
go test -v -coverprofile=coverage.txt -covermode=atomic ./...

formatter/comments.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,25 @@ func parseComment(s string) *comment {
1919
count := 1
2020

2121
content := parts[1]
22-
if strings.HasPrefix(content, " ") {
23-
content = strings.TrimSpace(strings.SplitN(parts[1], " ", 2)[1])
24-
} else if strings.HasPrefix(content, "!") {
22+
23+
if strings.HasPrefix(content, "!") {
2524
hidden = true
26-
content = strings.TrimSpace(strings.SplitN(parts[1], "!", 2)[1])
25+
content = strings.SplitN(content, "!", 2)[1]
2726
} else if strings.HasPrefix(content, "#") {
2827
content, count = countHashes(content, count)
2928
sub, found := strings.CutPrefix(content, "!")
3029
if found {
3130
hidden = true
32-
content = strings.TrimSpace(sub)
31+
content = sub
3332
}
3433
}
3534

35+
if !strings.HasPrefix(content, " ") {
36+
content = " " + content
37+
}
38+
3639
c := comment{
37-
content: strings.TrimSpace(content),
40+
content: strings.TrimRight(content, " "),
3841
hidden: hidden,
3942
hashCount: count,
4043
original: parts[1],
@@ -60,9 +63,9 @@ func parseAndDivideInlineComment(s string) (string, *comment) {
6063
func (c comment) getString() string {
6164
var s string
6265
if c.hidden {
63-
s = fmt.Sprintf("%s! %s", strings.Repeat("#", c.hashCount), c.content)
66+
s = fmt.Sprintf("%s!%s", strings.Repeat("#", c.hashCount), c.content)
6467
} else {
65-
s = fmt.Sprintf("%s %s", strings.Repeat("#", c.hashCount), c.content)
68+
s = fmt.Sprintf("%s%s", strings.Repeat("#", c.hashCount), c.content)
6669
}
6770

6871
return strings.TrimSpace(s)

formatter/processor.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bufio"
55
"fmt"
66
"io"
7-
"regexp"
87
"sort"
98
"strconv"
109
"strings"
@@ -375,7 +374,7 @@ func (f *form) removeDoubleLines(s string) string {
375374
var modifiedLines []string
376375
var emptyLine bool
377376

378-
for _, line := range strings.Split(s, "\n") {
377+
for line := range strings.SplitSeq(s, "\n") {
379378
if line != "" || !emptyLine {
380379
modifiedLines = append(modifiedLines, line)
381380
emptyLine = line == ""
@@ -386,8 +385,27 @@ func (f *form) removeDoubleLines(s string) string {
386385
}
387386

388387
func reduceSpaces(input string) string {
389-
pattern := regexp.MustCompile(`\s+`)
390-
return pattern.ReplaceAllString(input, " ")
388+
// Find the position of the '#' character
389+
hashIndex := strings.Index(input, "#")
390+
if hashIndex == -1 {
391+
// If there's no '#', just reduce spaces in the entire string
392+
return reduceSpacesInString(input)
393+
}
394+
395+
// Separate the part before and after the '#'
396+
beforeHash := input[:hashIndex]
397+
afterHash := input[hashIndex:]
398+
399+
// Reduce spaces in the part before the '#'
400+
reduced := reduceSpacesInString(beforeHash)
401+
402+
return reduced + afterHash
403+
}
404+
405+
func reduceSpacesInString(s string) string {
406+
// Split the string and join using a single space
407+
parts := strings.Fields(s)
408+
return strings.Join(parts, " ")
391409
}
392410

393411
func removeSpaces(input string) string {

go.mod

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
module github.com/webrpc/ridlfmt
22

3-
go 1.20
3+
go 1.24.0
44

55
require github.com/stretchr/testify v1.9.0
66

77
require (
88
github.com/davecgh/go-spew v1.1.1 // indirect
9+
github.com/fsnotify/fsnotify v1.9.0 // indirect
10+
github.com/goware/rerun v0.1.0 // indirect
911
github.com/pmezard/go-difflib v1.0.0 // indirect
12+
golang.org/x/sys v0.37.0 // indirect
1013
gopkg.in/yaml.v3 v3.0.1 // indirect
1114
)
15+
16+
tool github.com/goware/rerun/cmd/rerun

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
22
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k=
4+
github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
5+
github.com/goware/rerun v0.1.0 h1:DF+CyZIkARtU7FyvUZLKzwXARxRqx/lEre8V9sGjAK4=
6+
github.com/goware/rerun v0.1.0/go.mod h1:EcV6fsyl4+4qV298L42JErY+s6WQbQaUtcHh6ztOR6s=
37
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
48
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
59
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
610
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
11+
golang.org/x/sys v0.37.0 h1:fdNQudmxPjkdUTPnLn5mdQv7Zwvbvpaxqs831goi9kQ=
12+
golang.org/x/sys v0.37.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
713
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
814
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
915
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

main_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func testHelpFlag(t *testing.T) {
8383
}
8484

8585
const testInput string = `
86-
webrpc = v1 # version of webrpc schema format (ridl or json)
86+
webrpc = v1 # version of webrpc schema format (ridl or json)
8787
name = example # name of your backend app
8888
version=v0.0.1#version of your schema
8989
@@ -94,6 +94,7 @@ enum Intent: string
9494
- closeSession
9595
9696
enum Kind: uint32
97+
# user
9798
- USER
9899
# admin
99100
- ADMIN
@@ -156,7 +157,7 @@ service ExampleService # oof
156157
@ auth : ApiKeyAuth @ who dsa : J W T ## dadsadadsa
157158
- Ping()
158159
- Status() => (status: bool)
159-
@ internal @ public @ stringo : " string with spaces " ## dsada s dsa
160+
@ internal @ public @ stringo : " string with spaces " ## multiple hashes and spaces
160161
- Version() => (version: Version)
161162
@public
162163
- GetUser ( header : map < string , string > , userID : uint64 ) => ( code : uint32 , user : User )
@@ -186,6 +187,7 @@ enum Intent: string
186187
- closeSession
187188
188189
enum Kind: uint32
190+
# user
189191
- USER
190192
# admin
191193
- ADMIN
@@ -201,7 +203,7 @@ struct User
201203
202204
- username: string
203205
+ json = USERNAME
204-
+ go.tag.db = username #! far away
206+
+ go.tag.db = username #! far away
205207
206208
#! role?
207209
#! role!
@@ -248,7 +250,7 @@ service ExampleService # oof
248250
@auth:ApiKeyAuth @whodsa:JWT ## dadsadadsa
249251
- Ping()
250252
- Status() => (status: bool)
251-
@internal @public @stringo:" string with spaces " ## dsada s dsa
253+
@internal @public @stringo:" string with spaces " ## multiple hashes and spaces
252254
- Version() => (version: Version)
253255
@public
254256
- GetUser(header: map<string,string>, userID: uint64) => (code: uint32, user: User)

0 commit comments

Comments
 (0)