Skip to content

Commit ae68cbf

Browse files
committed
Added code-completion support
1 parent c2c1bf1 commit ae68cbf

File tree

4 files changed

+33
-16
lines changed

4 files changed

+33
-16
lines changed

arduino-builder/main.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ const VERSION = "1.3.25"
5454
const FLAG_ACTION_COMPILE = "compile"
5555
const FLAG_ACTION_PREPROCESS = "preprocess"
5656
const FLAG_ACTION_DUMP_PREFS = "dump-prefs"
57+
const FLAG_ACTION_CODE_COMPLETE_AT = "code-complete-at"
5758
const FLAG_BUILD_OPTIONS_FILE = "build-options-file"
5859
const FLAG_HARDWARE = "hardware"
5960
const FLAG_TOOLS = "tools"
@@ -118,6 +119,7 @@ func (h *propertiesFlag) Set(value string) error {
118119
var compileFlag *bool
119120
var preprocessFlag *bool
120121
var dumpPrefsFlag *bool
122+
var codeCompleteAtFlag *string
121123
var buildOptionsFileFlag *string
122124
var hardwareFoldersFlag foldersFlag
123125
var toolsFoldersFlag foldersFlag
@@ -141,6 +143,7 @@ func init() {
141143
compileFlag = flag.Bool(FLAG_ACTION_COMPILE, false, "compiles the given sketch")
142144
preprocessFlag = flag.Bool(FLAG_ACTION_PREPROCESS, false, "preprocess the given sketch")
143145
dumpPrefsFlag = flag.Bool(FLAG_ACTION_DUMP_PREFS, false, "dumps build properties used when compiling")
146+
codeCompleteAtFlag = flag.String(FLAG_ACTION_CODE_COMPLETE_AT, "", "output code completions for sketch at a specific location. Location format is \"file:line:col\"")
144147
buildOptionsFileFlag = flag.String(FLAG_BUILD_OPTIONS_FILE, "", "Instead of specifying --"+FLAG_HARDWARE+", --"+FLAG_TOOLS+" etc every time, you can load all such options from a file")
145148
flag.Var(&hardwareFoldersFlag, FLAG_HARDWARE, "Specify a 'hardware' folder. Can be added multiple times for specifying multiple 'hardware' folders")
146149
flag.Var(&toolsFoldersFlag, FLAG_TOOLS, "Specify a 'tools' folder. Can be added multiple times for specifying multiple 'tools' folders")
@@ -330,7 +333,8 @@ func main() {
330333

331334
if *dumpPrefsFlag {
332335
err = builder.RunParseHardwareAndDumpBuildProperties(ctx)
333-
} else if *preprocessFlag {
336+
} else if *preprocessFlag || *codeCompleteAtFlag != "" {
337+
ctx.CodeCompleteAt = *codeCompleteAtFlag
334338
err = builder.RunPreprocess(ctx)
335339
} else {
336340
if flag.NArg() == 0 {

hardware/platform.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
tools.arduino-preprocessor.path={runtime.tools.arduino-preprocessor.path}
55
tools.arduino-preprocessor.cmd.path={path}/bin/arduino-preprocessor
6-
tools.arduino-preprocessor.pattern="{cmd.path}" "{source_file}" -- -std=gnu++11
6+
tools.arduino-preprocessor.pattern="{cmd.path}" "{source_file}" "{codecomplete}" -- -std=gnu++11
77

88
# ctags
99
# ------------------------------

src/arduino.cc/builder/preprocess_sketch.go

+25-14
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ import (
3333
"fmt"
3434
"path/filepath"
3535

36-
"io/ioutil"
37-
3836
"arduino.cc/builder/constants"
3937
"arduino.cc/builder/i18n"
4038
"arduino.cc/builder/types"
@@ -48,7 +46,12 @@ func (s *PreprocessSketch) Run(ctx *types.Context) error {
4846
commands := []types.Command{
4947
&GCCPreprocRunner{SourceFilePath: sourceFile, TargetFileName: constants.FILE_CTAGS_TARGET_FOR_GCC_MINUS_E, Includes: ctx.IncludeFolders},
5048
&ArduinoPreprocessorRunner{},
51-
&SketchSaver{},
49+
}
50+
51+
if ctx.CodeCompleteAt != "" {
52+
commands = append(commands, &OutputCodeCompletions{})
53+
} else {
54+
commands = append(commands, &SketchSaver{})
5255
}
5356

5457
for _, command := range commands {
@@ -73,6 +76,11 @@ func (s *ArduinoPreprocessorRunner) Run(ctx *types.Context) error {
7376
toolProps := buildProperties.SubTree("tools").SubTree("arduino-preprocessor")
7477
properties.Merge(toolProps)
7578
properties[constants.BUILD_PROPERTIES_SOURCE_FILE] = targetFilePath
79+
if ctx.CodeCompleteAt != "" {
80+
properties["codecomplete"] = "-output-code-completions=" + ctx.CodeCompleteAt
81+
} else {
82+
properties["codecomplete"] = ""
83+
}
7684

7785
pattern := properties[constants.BUILD_PROPERTIES_PATTERN]
7886
if pattern == constants.EMPTY_STRING {
@@ -90,20 +98,23 @@ func (s *ArduinoPreprocessorRunner) Run(ctx *types.Context) error {
9098
fmt.Println(commandLine)
9199
}
92100

93-
sourceBytes, err := command.Output()
101+
buf, err := command.Output()
94102
if err != nil {
95103
return i18n.WrapError(err)
96104
}
97-
98-
ctx.Source = string(sourceBytes)
99-
if ctx.Source == "" {
100-
// If the output is empty the original sketch may pass through
101-
buf, err := ioutil.ReadFile(targetFilePath)
102-
if err != nil {
103-
return i18n.WrapError(err)
104-
}
105-
ctx.Source = string(buf)
105+
output := string(buf)
106+
//fmt.Printf("PREPROCESSOR OUTPUT:\n%s\n", output)
107+
if ctx.CodeCompleteAt != "" {
108+
ctx.CodeCompletions = output
109+
} else {
110+
ctx.Source = output
106111
}
107-
//fmt.Printf("PREPROCESSOR OUTPUT:\n%s\n", ctx.Source)
112+
return nil
113+
}
114+
115+
type OutputCodeCompletions struct{}
116+
117+
func (s *OutputCodeCompletions) Run(ctx *types.Context) error {
118+
fmt.Println(ctx.CodeCompletions)
108119
return nil
109120
}

types/context.go

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type Context struct {
1818
SketchLocation string
1919
ArduinoAPIVersion string
2020
FQBN string
21+
CodeCompleteAt string
2122

2223
// Build options are serialized here
2324
BuildOptionsJson string
@@ -53,6 +54,7 @@ type Context struct {
5354
Sketch *Sketch
5455
Source string
5556
SourceGccMinusE string
57+
CodeCompletions string
5658

5759
WarningsLevel string
5860

0 commit comments

Comments
 (0)