Skip to content

Commit 9faaa35

Browse files
committed
Added cmd line options for code-completion
1 parent 2ca869a commit 9faaa35

File tree

3 files changed

+101
-3
lines changed

3 files changed

+101
-3
lines changed

CommandLine.cpp

+35-3
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,22 @@
2929

3030
#include "CommandLine.hpp"
3131
#include "Config.hpp"
32+
#include "utils.hpp"
3233

3334
#include <iostream>
34-
35-
using namespace std;
35+
#include <sstream>
3636

3737
bool debugOutput;
3838
bool outputDiagnostics;
3939
bool outputOnlyNeededPrototypes;
4040
bool outputPreprocessedSketch = true;
4141

42+
// Code completion parameters
43+
bool outputCodeCompletions;
44+
string codeCompleteFilename;
45+
int codeCompleteLine;
46+
int codeCompleteCol;
47+
4248
static cl::OptionCategory arduinoToolCategory("Arduino options");
4349
// TODO: add complete help
4450
static cl::extrahelp arduinoHelp("\n"
@@ -49,6 +55,7 @@ static cl::extrahelp commonHelp(CommonOptionsParser::HelpMessage);
4955
static cl::opt<bool> debugOutputOpt("debug");
5056
static cl::opt<bool> outputOnlyNeededPrototypesOpt("output-only-needed-prototypes");
5157
static cl::opt<bool> outputDiagnosticsOpt("output-diagnostics");
58+
static cl::opt<string> outputCodeCompletionsOpt("output-code-completions");
5259

5360
static void printVersion() {
5461
cout << "Arduino (https://www.arduino.cc/):\n";
@@ -68,14 +75,39 @@ CommonOptionsParser doCommandLineParsing(int argc, const char **argv) {
6875
outputDiagnosticsOpt.setInitialValue(false);
6976
outputDiagnosticsOpt.setDescription("Output diagnostics (warnings/errors) in json format");
7077

78+
outputCodeCompletionsOpt.setCategory(arduinoToolCategory);
79+
outputCodeCompletionsOpt.setInitialValue("");
80+
outputCodeCompletionsOpt.setDescription(
81+
"Output code completions (suggestions) in json format.\n"
82+
"This option requires the cursor position in the format \"filename:line:col\"");
83+
7184
cl::AddExtraVersionPrinter(printVersion);
7285

7386
CommonOptionsParser optParser(argc, argv, arduinoToolCategory);
7487

88+
/* Parse outputCodeCompletion parameter */
89+
if (outputCodeCompletionsOpt.getValue() != "") {
90+
vector<string> spl = split(outputCodeCompletionsOpt.getValue(), ':');
91+
if (spl.size() != 3) {
92+
cerr << "code completion requires parameter in the form \"filename:line:col\"\n";
93+
exit(1);
94+
}
95+
codeCompleteFilename = spl[0];
96+
if (!stringToInt(spl[1], &codeCompleteLine)) {
97+
cerr << "code completion requires 'line' to be a positive integer parameter in the form \"filename:line:col\"\n";
98+
exit(1);
99+
}
100+
if (!stringToInt(spl[2], &codeCompleteCol)) {
101+
cerr << "code completion requires 'col' to be a positive integer parameter in the form \"filename:line:col\"\n";
102+
exit(1);
103+
}
104+
outputCodeCompletions = true;
105+
}
106+
75107
debugOutput = debugOutputOpt.getValue();
76108
outputOnlyNeededPrototypes = outputOnlyNeededPrototypesOpt.getValue();
77109
outputDiagnostics = outputDiagnosticsOpt.getValue();
78-
if (outputDiagnostics) {
110+
if (outputDiagnostics || outputCodeCompletions) {
79111
outputPreprocessedSketch = false;
80112
}
81113
return optParser;

CommandLine.hpp

+7
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,17 @@
3535
using namespace clang;
3636
using namespace clang::tooling;
3737
using namespace llvm;
38+
using namespace std;
3839

3940
extern bool debugOutput;
4041
extern bool outputOnlyNeededPrototypes;
4142
extern bool outputDiagnostics;
4243
extern bool outputPreprocessedSketch;
4344

45+
// Code completion parameters
46+
extern bool outputCodeCompletions;
47+
extern string codeCompleteFilename;
48+
extern int codeCompleteLine;
49+
extern int codeCompleteCol;
50+
4451
CommonOptionsParser doCommandLineParsing(int argc, const char **argv);

utils.hpp

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* This file is part of arduino-preprocessor.
3+
*
4+
* Copyright 2017 BCMI LABS SA
5+
*
6+
* arduino-preprocessor is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*
20+
* As a special exception, you may use this file as part of a free software
21+
* library without restriction. Specifically, if other files instantiate
22+
* templates or use macros or inline functions from this file, or you compile
23+
* this file and link it with other files to produce an executable, this
24+
* file does not by itself cause the resulting executable to be covered by
25+
* the GNU General Public License. This exception does not however
26+
* invalidate any other reasons why the executable file might be covered by
27+
* the GNU General Public License.
28+
*/
29+
30+
#pragma once
31+
32+
#include <vector>
33+
#include <sstream>
34+
35+
using namespace std;
36+
37+
vector<string> split(const string &in, const char sep) {
38+
vector<string> res;
39+
istringstream params(in);
40+
string s;
41+
while (getline(params, s, sep)) {
42+
res.push_back(s);
43+
}
44+
return res;
45+
}
46+
47+
bool stringToInt(const string &in, int *out) {
48+
stringstream ss(in);
49+
ss >> *out;
50+
if (!ss.fail()) {
51+
return true;
52+
}
53+
// Tolerate trailing white space
54+
ss >> ws;
55+
if (ss.eof()) {
56+
return true;
57+
}
58+
return false;
59+
}

0 commit comments

Comments
 (0)