Skip to content

Commit 4c3c3d0

Browse files
committed
Rename to cut
1 parent b2d7541 commit 4c3c3d0

File tree

6 files changed

+66
-15
lines changed

6 files changed

+66
-15
lines changed
File renamed without changes.

.snapshot/TestPatch_Skip

Lines changed: 0 additions & 2 deletions
This file was deleted.

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,50 @@ Flags:
5555
Use "patchr [command] --help" for more information about a command.
5656
```
5757

58+
### Directives
59+
60+
Currently, we have 5 directives.
61+
62+
1. replace
63+
* repalce next line
64+
2. add
65+
* add next line
66+
3. remove
67+
* remove next line
68+
4. template
69+
* expand template using go/template
70+
5. cut
71+
* cut a whole block
72+
73+
```go
74+
func main() {
75+
// You can use go text/template in replace/add/template directives
76+
77+
// `replace` directive replace next line
78+
// patchr:replace fmt.Println("Hello, {{ .Name }}!")
79+
fmt.Println("Hello World!")
80+
81+
// `add` directive add next line
82+
// patchr:add fmt.Println("Hello, {{ .Name }}!")
83+
84+
// `template` directive can expand text/template here
85+
// patchr:template-start
86+
// {{ if .Enabled }}
87+
// fmt.Println("Enabled!")
88+
// {{ end }}
89+
// patchr:template-end
90+
91+
// `remove` directive remove next line
92+
// patchr:remove
93+
fmt.Println("this will be removed")
94+
95+
// `cut` directive cut the whole block
96+
// patchr:cut-start
97+
panic("this will be cut")
98+
// patchr:cut-end
99+
}
100+
```
101+
58102
## Example
59103

60104
See [testdata](./testdata/).

patch.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ const (
2929
commentDirectiveTemplateStart = "patchr:template-start"
3030
// end of template block.
3131
commentDirectiveTemplateEnd = "patchr:template-end"
32-
// start of skip block.
33-
commentDirectiveSkipStart = "patchr:skip-start"
34-
// end of skip block.
35-
commentDirectiveSkipEnd = "patchr:skip-end"
32+
// start of cut block.
33+
commentDirectiveCutStart = "patchr:cut-start"
34+
// end of cut block.
35+
commentDirectiveCutEnd = "patchr:cut-end"
3636
)
3737

3838
var commentDirectives = []patchCommentDirective{
@@ -41,8 +41,8 @@ var commentDirectives = []patchCommentDirective{
4141
commentDirectiveRemove,
4242
commentDirectiveTemplateStart,
4343
commentDirectiveTemplateEnd,
44-
commentDirectiveSkipStart,
45-
commentDirectiveSkipEnd,
44+
commentDirectiveCutStart,
45+
commentDirectiveCutEnd,
4646
}
4747

4848
type Patcher struct {
@@ -103,9 +103,9 @@ func (p *Patcher) visit(line string, dst *bufio.Writer, scanner *bufio.Scanner,
103103
return p.visitTemplateStart(line, dst, scanner, data)
104104
case commentDirectiveTemplateEnd:
105105
return errors.New("unexpected end directive")
106-
case commentDirectiveSkipStart:
107-
return p.visitSkipStart(line, dst, scanner, data)
108-
case commentDirectiveSkipEnd:
106+
case commentDirectiveCutStart:
107+
return p.visitCutStart(line, dst, scanner, data)
108+
case commentDirectiveCutEnd:
109109
return errors.New("unexpected end directive")
110110
}
111111
}
@@ -189,10 +189,10 @@ func (p *Patcher) visitTemplateStart(line string, dst *bufio.Writer, scanner *bu
189189
return writeNewLine(t, dst)
190190
}
191191

192-
func (p *Patcher) visitSkipStart(line string, dst *bufio.Writer, scanner *bufio.Scanner, data any) error {
192+
func (p *Patcher) visitCutStart(line string, dst *bufio.Writer, scanner *bufio.Scanner, data any) error {
193193
for scanner.Scan() {
194194
t := scanner.Text()
195-
if strings.Contains(t, p.directives[commentDirectiveSkipEnd]) {
195+
if strings.Contains(t, p.directives[commentDirectiveCutEnd]) {
196196
break
197197
}
198198
}

patch_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ func TestPatch_Add(t *testing.T) {
4747
s.Assert(t)
4848
}
4949

50-
func TestPatch_Skip(t *testing.T) {
50+
func TestPatch_Cut(t *testing.T) {
5151
t.Parallel()
5252
p := NewPatcher("//", map[string]string{})
5353
s := snap.New()
54-
f, err := os.Open("testdata/skip.go")
54+
f, err := os.Open("testdata/cut.go")
5555
if err != nil {
5656
t.Fatal(err)
5757
}

testdata/cut.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package main
2+
3+
// patchr:skip-start
4+
// Sample is a sample struct.
5+
type Sample struct {
6+
Name string
7+
}
8+
9+
// patchr:skip-end

0 commit comments

Comments
 (0)