Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions packages/cxx-frontend/src/ASTCursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,6 @@ class StackEntry {
}
}

interface AcceptArgs {
node: AST | Token;
depth: number;
slot?: ASTSlot;
}

/**
* AST cursor.
*
Expand Down
11 changes: 11 additions & 0 deletions packages/cxx-gen-ast/src/gen_ast_h.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,17 @@ export function gen_ast_h({ ast, output }: { ast: AST; output: string }) {
emit(` default: cxx_runtime_error("unexpected ${variantName}");`);
emit(` } // switch`);
emit(`}`);
emit();
emit(`[[nodiscard]] inline auto is${variantName}(AST* ast) -> bool {`);
emit(` if (!ast) return false;`);
emit(` switch (ast->kind()) {`);
nodes.forEach(({ name }) => {
emit(` case ${name}::Kind: `);
});
emit(` return true;`);
emit(` default: return false;`);
emit(` } // switch`);
emit(`}`);
});

const out = `${cpy_header}
Expand Down
40 changes: 40 additions & 0 deletions src/frontend/cxx/cxx_document.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "cxx_document.h"

#include <cxx/ast.h>
#include <cxx/ast_slot.h>
#include <cxx/ast_visitor.h>
#include <cxx/control.h>
#include <cxx/gcc_linux_toolchain.h>
Expand Down Expand Up @@ -208,4 +209,43 @@ auto CxxDocument::translationUnit() const -> TranslationUnit* {
return &d->unit;
}

namespace {

struct Visit {
CxxDocument* doc_;
std::function<bool(AST*)> visitor;
ASTSlot slotInfo;

void preVisit(AST* ast) {
if (!ast) return;

if (visitor(ast)) return;

// do a pre-visit using the low-level AST API

const auto slotCount = slotInfo(ast, 0).slotCount;

for (int index = 0; index < slotCount; ++index) {
const auto childInfo = slotInfo(ast, index);

if (childInfo.kind == ASTSlotKind::kNode) {
auto child = reinterpret_cast<AST*>(childInfo.handle);
if (child) preVisit(child);
} else if (childInfo.kind == ASTSlotKind::kNodeList) {
auto list = reinterpret_cast<List<AST*>*>(childInfo.handle);
for (auto node : ListView{list}) {
preVisit(node);
}
}
}
}
};

} // namespace

void CxxDocument::preVisit(std::function<bool(AST*)> visitor) {
auto ast = d->unit.ast();
Visit{this, std::move(visitor)}.preVisit(ast);
}

} // namespace cxx::lsp
3 changes: 3 additions & 0 deletions src/frontend/cxx/cxx_document.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <cxx/lsp/fwd.h>
#include <cxx/translation_unit.h>

#include <functional>
#include <memory>
#include <string>

Expand All @@ -42,6 +43,8 @@ class CxxDocument {

[[nodiscard]] auto translationUnit() const -> TranslationUnit*;

void preVisit(std::function<bool(AST*)> visitor);

private:
struct Private;
std::unique_ptr<Private> d;
Expand Down
28 changes: 21 additions & 7 deletions src/frontend/cxx/lsp_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -320,13 +320,17 @@ void Server::operator()(InitializeRequest request) {

withUnsafeJson([&](json storage) {
InitializeResponse response{storage};
std::cerr << std::format("initializing response to {}\n",
request.get().dump());

response.id(*request.id());

auto serverInfo = response.result().serverInfo<ServerInfo>();

serverInfo.name("cxx-lsp").version(CXX_VERSION);

auto capabilities = response.result().capabilities();
response.result().serverInfo<ServerInfo>().name("cxx-lsp").version(
CXX_VERSION);
capabilities.textDocumentSync(TextDocumentSyncKind::kIncremental);
capabilities.documentSymbolProvider(true);

sendToClient(response);
});
}
Expand Down Expand Up @@ -424,8 +428,18 @@ auto Server::latestDocument(const std::string& uri)
return documents_[uri];
}

void Server::operator()(DocumentDiagnosticRequest request) {
logTrace(std::format("Did receive DocumentDiagnosticRequest"));
void Server::operator()(DocumentSymbolRequest request) {
logTrace(std::format("Did receive DocumentSymbolRequest"));

auto uri = request.params().textDocument().uri();
auto doc = latestDocument(uri);

withUnsafeJson([&](json storage) {
DocumentSymbolResponse response(storage);
response.id(request.id());
(void)response.result();
sendToClient(response);
});
}

void Server::operator()(CancelNotification notification) {
Expand All @@ -448,7 +462,7 @@ void Server::operator()(SetTraceNotification notification) {
trace_ = notification.params().value();

if (trace_ != TraceValue::kOff) {
logTrace("Trace level set to {}\n", to_string(trace_));
logTrace("Trace level set to {}", to_string(trace_));
return;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/cxx/lsp_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Server {
void operator()(DidCloseTextDocumentNotification notification);
void operator()(DidChangeTextDocumentNotification notification);

void operator()(DocumentDiagnosticRequest request);
void operator()(DocumentSymbolRequest request);

void operator()(SetTraceNotification notification);

Expand Down
Loading