Skip to content

Commit 24b77c3

Browse files
committedOct 17, 2019
Several improvements
- Copy header files from sketch to temp dir - Filter lines beginning with ERROR: or WARNING: - Hide diagnostics that cannot be mapped to a source line - Improved error message when header file cannot be found
1 parent 3a51dda commit 24b77c3

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
lines changed
 

‎handler/builder.go

+43
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ func generateCpp(inoCode []byte, sourcePath, fqbn string) (cppPath string, cppCo
4040
log.Println("Source file written to", inoPath)
4141
}
4242

43+
// Copy all header files to temp dir
44+
err = copyHeaderFiles(filepath.Dir(sourcePath), tempDir)
45+
if err != nil {
46+
return
47+
}
48+
4349
// Generate compile_flags.txt
4450
cppPath = filepath.Join(tempDir, name+".cpp")
4551
flagsPath, err := generateCompileFlags(tempDir, inoPath, sourcePath, fqbn)
@@ -55,6 +61,27 @@ func generateCpp(inoCode []byte, sourcePath, fqbn string) (cppPath string, cppCo
5561
return
5662
}
5763

64+
func copyHeaderFiles(sourceDir string, destDir string) error {
65+
fileInfos, err := ioutil.ReadDir(sourceDir)
66+
if err != nil {
67+
return err
68+
}
69+
for _, fileInfo := range fileInfos {
70+
if !fileInfo.IsDir() && strings.HasSuffix(fileInfo.Name(), ".h") {
71+
input, err := ioutil.ReadFile(filepath.Join(sourceDir, fileInfo.Name()))
72+
if err != nil {
73+
return err
74+
}
75+
76+
err = ioutil.WriteFile(filepath.Join(destDir, fileInfo.Name()), input, 0644)
77+
if err != nil {
78+
return err
79+
}
80+
}
81+
}
82+
return nil
83+
}
84+
5885
func updateCpp(inoCode []byte, sourcePath, fqbn string, fqbnChanged bool, cppPath string) (cppCode []byte, err error) {
5986
tempDir := filepath.Dir(cppPath)
6087
inoPath := strings.TrimSuffix(cppPath, ".cpp")
@@ -132,6 +159,9 @@ func generateTargetFile(tempDir, inoPath, cppPath, fqbn string) (cppCode []byte,
132159
return
133160
}
134161

162+
// Filter lines beginning with ERROR or WARNING
163+
cppCode = []byte(filterErrorsAndWarnings(cppCode))
164+
135165
err = ioutil.WriteFile(cppPath, cppCode, 0600)
136166
if err != nil {
137167
err = errors.Wrap(err, "Error while writing target file to temporary directory.")
@@ -141,6 +171,19 @@ func generateTargetFile(tempDir, inoPath, cppPath, fqbn string) (cppCode []byte,
141171
return
142172
}
143173

174+
func filterErrorsAndWarnings(cppCode []byte) string {
175+
var sb strings.Builder
176+
scanner := bufio.NewScanner(bytes.NewReader(cppCode))
177+
for scanner.Scan() {
178+
lineStr := scanner.Text()
179+
if !(strings.HasPrefix(lineStr, "ERROR:") || strings.HasPrefix(lineStr, "WARNING:")) {
180+
sb.WriteString(lineStr)
181+
sb.WriteRune('\n')
182+
}
183+
}
184+
return sb.String()
185+
}
186+
144187
func copyIno2Cpp(inoCode string, cppPath string) (cppCode []byte, err error) {
145188
inoPath := strings.TrimSuffix(cppPath, ".cpp")
146189
filePrefix := "#include <Arduino.h>\n#line 1 \"" + inoPath + "\"\n"

‎handler/handler.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,8 @@ func (handler *InoHandler) handleError(ctx context.Context, err error) error {
398398
panic(regexpErr)
399399
}
400400
submatch := exp.FindStringSubmatch(errorStr)
401-
message = "Editor support may be inaccurate because the library `" + submatch[1] + "` is not installed."
402-
message += " Use the Library Manager to install it"
401+
message = "Editor support may be inaccurate because the header `" + submatch[1] + ".h` was not found."
402+
message += " If it is part of a library, use the Library Manager to install it"
403403
} else {
404404
message = "Could not start editor support.\n" + errorStr
405405
}
@@ -783,11 +783,16 @@ func (handler *InoHandler) transformParamsToStdio(method string, raw *json.RawMe
783783
func (handler *InoHandler) cpp2inoPublishDiagnosticsParams(params *lsp.PublishDiagnosticsParams) error {
784784
if data, ok := handler.data[params.URI]; ok {
785785
params.URI = data.sourceURI
786+
newDiagnostics := make([]lsp.Diagnostic, 0, len(params.Diagnostics))
786787
for index := range params.Diagnostics {
787788
r := &params.Diagnostics[index].Range
788-
r.Start.Line = data.sourceLineMap[r.Start.Line]
789-
r.End.Line = data.sourceLineMap[r.End.Line]
789+
if startLine, ok := data.sourceLineMap[r.Start.Line]; ok {
790+
r.Start.Line = startLine
791+
r.End.Line = data.sourceLineMap[r.End.Line]
792+
newDiagnostics = append(newDiagnostics, params.Diagnostics[index])
793+
}
790794
}
795+
params.Diagnostics = newDiagnostics
791796
}
792797
return nil
793798
}

0 commit comments

Comments
 (0)
Please sign in to comment.