Skip to content

Commit 5cbb80d

Browse files
committed
Converted AddPrototypes into a function
1 parent 5856d30 commit 5cbb80d

File tree

2 files changed

+9
-29
lines changed

2 files changed

+9
-29
lines changed

legacy/builder/container_add_prototypes.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,10 @@ func PreprocessSketchWithCtags(ctx *types.Context) error {
5656
ctx.SketchSourceAfterCppPreprocessing = bldr.FilterSketchSource(ctx.Sketch, bytes.NewReader(src), false)
5757
}
5858

59-
commands := []types.Command{
60-
&CTagsRunner{Source: &ctx.SketchSourceAfterCppPreprocessing, TargetFileName: "sketch_merged.cpp"},
61-
&PrototypesAdder{},
62-
}
63-
64-
for _, command := range commands {
65-
PrintRingNameIfDebug(ctx, command)
66-
err := command.Run(ctx)
67-
if err != nil {
68-
return errors.WithStack(err)
69-
}
59+
if err := (&CTagsRunner{Source: &ctx.SketchSourceAfterCppPreprocessing, TargetFileName: "sketch_merged.cpp"}).Run(ctx); err != nil {
60+
return errors.WithStack(err)
7061
}
62+
ctx.SketchSourceAfterArduinoPreprocessing, ctx.PrototypesSection = PrototypesAdder(ctx.SketchSourceMerged, ctx.PrototypesLineWhereToInsert, ctx.LineOffset, ctx.Prototypes, ctx.DebugPreprocessor)
7163

7264
if err := bldr.SketchSaveItemCpp(ctx.Sketch.MainFile, []byte(ctx.SketchSourceAfterArduinoPreprocessing), ctx.SketchBuildPath); err != nil {
7365
return errors.WithStack(err)

legacy/builder/prototypes_adder.go

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,21 @@ import (
2222

2323
"github.com/arduino/arduino-cli/legacy/builder/constants"
2424
"github.com/arduino/arduino-cli/legacy/builder/ctags"
25-
"github.com/arduino/arduino-cli/legacy/builder/types"
2625
"github.com/arduino/arduino-cli/legacy/builder/utils"
2726
)
2827

29-
type PrototypesAdder struct{}
30-
31-
func (s *PrototypesAdder) Run(ctx *types.Context) error {
32-
debugOutput := ctx.DebugPreprocessor
33-
34-
source := ctx.SketchSourceMerged
28+
func PrototypesAdder(source string, firstFunctionLine, lineOffset int, prototypes []*ctags.Prototype, debugOutput bool) (preprocessedSource, prototypeSection string) {
3529
source = strings.Replace(source, "\r\n", "\n", -1)
3630
source = strings.Replace(source, "\r", "\n", -1)
37-
3831
sourceRows := strings.Split(source, "\n")
39-
40-
firstFunctionLine := ctx.PrototypesLineWhereToInsert
4132
if isFirstFunctionOutsideOfSource(firstFunctionLine, sourceRows) {
42-
return nil
33+
return
4334
}
4435

45-
insertionLine := firstFunctionLine + ctx.LineOffset - 1
36+
insertionLine := firstFunctionLine + lineOffset - 1
4637
firstFunctionChar := len(strings.Join(sourceRows[:insertionLine], "\n")) + 1
47-
prototypeSection := composePrototypeSection(firstFunctionLine, ctx.Prototypes)
48-
ctx.PrototypesSection = prototypeSection
49-
source = source[:firstFunctionChar] + prototypeSection + source[firstFunctionChar:]
38+
prototypeSection = composePrototypeSection(firstFunctionLine, prototypes)
39+
preprocessedSource = source[:firstFunctionChar] + prototypeSection + source[firstFunctionChar:]
5040

5141
if debugOutput {
5242
fmt.Println("#PREPROCESSED SOURCE")
@@ -63,9 +53,7 @@ func (s *PrototypesAdder) Run(ctx *types.Context) error {
6353
}
6454
fmt.Println("#END OF PREPROCESSED SOURCE")
6555
}
66-
ctx.SketchSourceAfterArduinoPreprocessing = source
67-
68-
return nil
56+
return
6957
}
7058

7159
func composePrototypeSection(line int, prototypes []*ctags.Prototype) string {

0 commit comments

Comments
 (0)