Repository navigation
Expand file tree
/
Copy pathdemo_advanced_lsp.cpp
More file actions
333 lines (273 loc) · 10.9 KB
/
Copy pathdemo_advanced_lsp.cpp
File metadata and controls
333 lines (273 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/**
* @file demo_advanced_lsp.cpp
* @brief Demo application testing all advanced LSP features
*
* Tests:
* - Go-to-Definition (F12)
* - Find References (Shift+F12)
* - Document Symbols (Ctrl+Shift+O)
* - Code Formatting (Shift+Alt+F)
*/
#include <chrono>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <thread>
#include "bolt/editor/lsp_client.hpp"
#include "bolt/editor/lsp_protocol.hpp"
#include "bolt/editor/lsp_server_configs.hpp"
using namespace bolt::lsp; // NOLINT(build/namespaces)
// Test file content with multiple symbols and references
// clang-format off
const char* TEST_CPP_CODE = R"(
#include <iostream>
#include <vector>
#include <string>
// A simple class with methods
class Calculator {
public:
int add(int a, int b) {
return a + b;
}
int subtract(int a, int b) {
return a - b;
}
int multiply(int a, int b) {
return a * b;
}
private:
int result_;
};
// A function that uses Calculator
void testCalculator() {
Calculator calc;
int sum = calc.add(5, 3);
int diff = calc.subtract(10, 4);
int product = calc.multiply(2, 6);
std::cout << "Sum: " << sum << std::endl;
std::cout << "Diff: " << diff << std::endl;
std::cout << "Product: " << product << std::endl;
}
int main() {
testCalculator();
Calculator calc2;
std::cout << "Result: " << calc2.add(100, 200) << std::endl;
return 0;
}
)";
// clang-format on
void printSeparator(const std::string& title) {
std::cout << "\n" << std::string(60, '=') << "\n";
std::cout << " " << title << "\n";
std::cout << std::string(60, '=') << "\n";
}
void printLocation(const Location& loc) {
std::cout << " 📍 " << loc.uri << "\n";
std::cout << " Line " << loc.range.start.line + 1 << ", Col " << loc.range.start.character + 1 << "\n";
}
void printSymbol(const DocumentSymbol& sym, int indent = 0) {
std::string prefix(indent * 2, ' ');
std::cout << prefix << " 📦 " << sym.name;
if (sym.detail.has_value() && !sym.detail->empty()) {
std::cout << " (" << *sym.detail << ")";
}
std::cout << " [" << static_cast<int>(sym.kind) << "]";
std::cout << " @ line " << sym.selectionRange.start.line + 1 << "\n";
for (const auto& child : sym.children) {
printSymbol(child, indent + 1);
}
}
void printTextEdit(const TextEdit& edit) {
std::cout << " ✏️ Lines " << edit.range.start.line + 1 << "-" << edit.range.end.line + 1 << ": ";
if (edit.newText.length() > 50) {
std::cout << edit.newText.substr(0, 50) << "...\n";
} else {
std::cout << "\"" << edit.newText << "\"\n";
}
}
int main() {
std::cout << R"(
╔══════════════════════════════════════════════════════════╗
║ Bolt C++ Advanced LSP Features Demo ║
║ Testing Definition, References, Symbols, Formatting ║
╚══════════════════════════════════════════════════════════╝
)" << std::endl;
// Create test directory and file
std::string testDir = "/tmp/bolt_advanced_lsp_test";
std::filesystem::create_directories(testDir);
std::string testFile = testDir + "/test_advanced.cpp";
{
std::ofstream ofs(testFile);
ofs << TEST_CPP_CODE;
}
std::cout << "Created test file: " << testFile << "\n";
// Check if clangd is available
if (!LspServerConfigs::isServerAvailable("cpp")) {
std::cerr << "❌ clangd not available. Please install clangd.\n";
return 1;
}
auto config = LspServerConfigs::getConfigForLanguage("cpp");
config.rootPath = testDir;
std::cout << "✅ clangd available: " << config.serverCommand << "\n";
// Create and connect LSP client
auto connection = std::make_unique<ProcessLspConnection>();
LspClient client(std::move(connection));
if (!client.connect(config)) {
std::cerr << "❌ Failed to connect to clangd\n";
return 1;
}
std::cout << "✅ Connected to clangd\n";
// Initialize
bool initSuccess = false;
client.initialize([&initSuccess](bool success) { initSuccess = success; });
std::this_thread::sleep_for(std::chrono::milliseconds(500));
if (!initSuccess) {
std::cerr << "❌ Failed to initialize LSP session\n";
return 1;
}
std::cout << "✅ LSP session initialized\n";
// Open document
TextDocumentItem doc;
doc.uri = "file://" + testFile;
doc.languageId = "cpp";
doc.version = 1;
doc.text = TEST_CPP_CODE;
client.didOpenTextDocument(doc);
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
std::cout << "✅ Document opened\n";
// ========================================================================
// Test 1: Go-to-Definition
// ========================================================================
printSeparator("Test 1: Go-to-Definition");
std::cout << "Testing definition of 'Calculator' at line 27 (calc.add)\n";
std::vector<Location> definitions;
bool defReceived = false;
TextDocumentPositionParams defParams;
defParams.textDocument.uri = doc.uri;
defParams.position.line = 26; // Line with "calc.add"
defParams.position.character = 4; // Position of "calc"
client.definition(defParams, [&definitions, &defReceived](const std::vector<Location>& locs) {
definitions = locs;
defReceived = true;
});
// Wait for response
for (int i = 0; i < 50 && !defReceived; ++i) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
if (defReceived && !definitions.empty()) {
std::cout << "✅ Go-to-Definition: Found " << definitions.size() << " definition(s)\n";
for (const auto& loc : definitions) {
printLocation(loc);
}
} else {
std::cout << "⚠️ Go-to-Definition: No definitions found (may need indexing time)\n";
}
// ========================================================================
// Test 2: Find References
// ========================================================================
printSeparator("Test 2: Find References");
std::cout << "Testing references to 'add' method\n";
std::vector<Location> references;
bool refsReceived = false;
TextDocumentPositionParams refParams;
refParams.textDocument.uri = doc.uri;
refParams.position.line = 8; // Line with "int add(int a, int b)"
refParams.position.character = 8; // Position of "add"
client.references(refParams, [&references, &refsReceived](const std::vector<Location>& locs) {
references = locs;
refsReceived = true;
});
// Wait for response
for (int i = 0; i < 50 && !refsReceived; ++i) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
if (refsReceived && !references.empty()) {
std::cout << "✅ Find References: Found " << references.size() << " reference(s)\n";
for (const auto& loc : references) {
printLocation(loc);
}
} else {
std::cout << "⚠️ Find References: No references found (may need indexing time)\n";
}
// ========================================================================
// Test 3: Document Symbols
// ========================================================================
printSeparator("Test 3: Document Symbols");
std::cout << "Testing document symbols (outline)\n";
std::vector<DocumentSymbol> symbols;
bool symsReceived = false;
TextDocumentIdentifier symDocId;
symDocId.uri = doc.uri;
client.documentSymbol(symDocId, [&symbols, &symsReceived](const std::vector<DocumentSymbol>& syms) {
symbols = syms;
symsReceived = true;
});
// Wait for response
for (int i = 0; i < 50 && !symsReceived; ++i) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
if (symsReceived && !symbols.empty()) {
std::cout << "✅ Document Symbols: Found " << symbols.size() << " top-level symbol(s)\n";
for (const auto& sym : symbols) {
printSymbol(sym);
}
} else {
std::cout << "⚠️ Document Symbols: No symbols found\n";
}
// ========================================================================
// Test 4: Code Formatting
// ========================================================================
printSeparator("Test 4: Code Formatting");
std::cout << "Testing document formatting\n";
std::vector<TextEdit> edits;
bool fmtReceived = false;
TextDocumentIdentifier fmtDocId;
fmtDocId.uri = doc.uri;
FormattingOptions fmtOpts;
fmtOpts.tabSize = 4;
fmtOpts.insertSpaces = true;
client.formatting(fmtDocId, fmtOpts, [&edits, &fmtReceived](const std::vector<TextEdit>& e) {
edits = e;
fmtReceived = true;
});
// Wait for response
for (int i = 0; i < 50 && !fmtReceived; ++i) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
if (fmtReceived) {
if (edits.empty()) {
std::cout << "✅ Code Formatting: Document already formatted (no changes needed)\n";
} else {
std::cout << "✅ Code Formatting: " << edits.size() << " edit(s) suggested\n";
for (const auto& edit : edits) {
printTextEdit(edit);
}
}
} else {
std::cout << "⚠️ Code Formatting: No response received\n";
}
// ========================================================================
// Cleanup
// ========================================================================
printSeparator("Cleanup");
TextDocumentIdentifier closeDocId;
closeDocId.uri = doc.uri;
client.didCloseTextDocument(closeDocId);
std::cout << "✅ Document closed\n";
client.shutdown([](bool success) {});
std::this_thread::sleep_for(std::chrono::milliseconds(200));
client.disconnect();
std::cout << "✅ LSP session shutdown\n";
// Summary
printSeparator("Summary");
const auto verdict = [](bool passed) { return passed ? "✅ PASSED" : "⚠️ PARTIAL"; };
std::cout << "Advanced LSP Features Test Results:\n";
std::cout << " • Go-to-Definition: " << verdict(defReceived && !definitions.empty()) << "\n";
std::cout << " • Find References: " << verdict(refsReceived && !references.empty()) << "\n";
std::cout << " • Document Symbols: " << verdict(symsReceived && !symbols.empty()) << "\n";
std::cout << " • Code Formatting: " << verdict(fmtReceived) << "\n";
std::cout << "\nNote: Some features may show partial results on first run due to\n";
std::cout << "clangd needing time to build its index. Re-running typically shows\n";
std::cout << "complete results.\n";
return 0;
}