Skip to content

Commit 08de1bd

Browse files
committed
Added -format-conf-path flag
1 parent 59f0539 commit 08de1bd

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

handler/handler.go

+18-7
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,16 @@ import (
3030

3131
var globalCliPath string
3232
var globalClangdPath string
33+
var globalFormatterConf *paths.Path
3334
var enableLogging bool
3435

3536
// Setup initializes global variables.
36-
func Setup(cliPath string, clangdPath string, _enableLogging bool) {
37+
func Setup(cliPath string, clangdPath string, formatFilePath string, _enableLogging bool) {
3738
globalCliPath = cliPath
3839
globalClangdPath = clangdPath
40+
if formatFilePath != "" {
41+
globalFormatterConf = paths.New(formatFilePath)
42+
}
3943
enableLogging = _enableLogging
4044
}
4145

@@ -1769,15 +1773,22 @@ func (handler *InoHandler) createClangdFormatterConfig(cppuri lsp.DocumentURI) (
17691773
AllowShortFunctionsOnASingleLine: None
17701774
`
17711775

1772-
// If a custom config is present in the sketch folder, use that one
1773-
customConfigFile := handler.sketchRoot.Join(".clang-format")
1774-
if customConfigFile.Exist() {
1775-
if c, err := customConfigFile.ReadFile(); err != nil {
1776-
log.Printf(" error reading custom formatter config file %s: %s", customConfigFile, err)
1776+
try := func(conf *paths.Path) bool {
1777+
if c, err := conf.ReadFile(); err != nil {
1778+
log.Printf(" error reading custom formatter config file %s: %s", conf, err)
17771779
} else {
1778-
log.Printf(" using custom formatter config file %s", customConfigFile)
1780+
log.Printf(" using custom formatter config file %s", conf)
17791781
config = string(c)
17801782
}
1783+
return true
1784+
}
1785+
1786+
if sketchFormatterConf := handler.sketchRoot.Join(".clang-format"); sketchFormatterConf.Exist() {
1787+
// If a custom config is present in the sketch folder, use that one
1788+
try(sketchFormatterConf)
1789+
} else if globalFormatterConf != nil && globalFormatterConf.Exist() {
1790+
// Otherwise if a global config file is present, use that one
1791+
try(globalFormatterConf)
17811792
}
17821793

17831794
targetFile := cppuri.AsPath()

main.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var initialFqbn string
2020
var initialBoardName string
2121
var enableLogging bool
2222
var loggingBasePath string
23+
var formatFilePath string
2324

2425
func main() {
2526
flag.StringVar(&clangdPath, "clangd", "clangd", "Path to clangd executable")
@@ -29,6 +30,7 @@ func main() {
2930
flag.StringVar(&initialBoardName, "board-name", "", "User-friendly board name to use initially (can be changed via JSON-RPC)")
3031
flag.BoolVar(&enableLogging, "log", false, "Enable logging to files")
3132
flag.StringVar(&loggingBasePath, "logpath", ".", "Location where to write logging files to when logging is enabled")
33+
flag.StringVar(&formatFilePath, "format-conf-path", "", "Path to global clang-format configuration file")
3234
flag.Parse()
3335

3436
if loggingBasePath != "" {
@@ -45,7 +47,7 @@ func main() {
4547
log.SetOutput(os.Stderr)
4648
}
4749

48-
handler.Setup(cliPath, clangdPath, enableLogging)
50+
handler.Setup(cliPath, clangdPath, formatFilePath, enableLogging)
4951
initialBoard := lsp.Board{Fqbn: initialFqbn, Name: initialBoardName}
5052

5153
stdio := streams.NewReadWriteCloser(os.Stdin, os.Stdout)

0 commit comments

Comments
 (0)