Step 539: add C/C++ constructive edit adapter
This commit is contained in:
@@ -3730,4 +3730,13 @@ target_link_libraries(step538_test PRIVATE
|
||||
tree_sitter_javascript tree_sitter_typescript
|
||||
tree_sitter_java tree_sitter_rust tree_sitter_go)
|
||||
|
||||
add_executable(step539_test tests/step539_test.cpp)
|
||||
target_include_directories(step539_test PRIVATE src)
|
||||
target_link_libraries(step539_test PRIVATE
|
||||
nlohmann_json::nlohmann_json
|
||||
unofficial::tree-sitter::tree-sitter
|
||||
tree_sitter_python tree_sitter_cpp tree_sitter_elisp
|
||||
tree_sitter_javascript tree_sitter_typescript
|
||||
tree_sitter_java tree_sitter_rust tree_sitter_go)
|
||||
|
||||
# Step 12: Dear ImGui shell scaffolding created (main.cpp exists but not built due to dependencies)
|
||||
|
||||
90
editor/src/CppConstructiveEditAdapter.h
Normal file
90
editor/src/CppConstructiveEditAdapter.h
Normal file
@@ -0,0 +1,90 @@
|
||||
#pragma once
|
||||
// Step 539: C/C++ Constructive Edit Adapter
|
||||
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
struct CppConstructiveEditRequest {
|
||||
std::string language; // c or cpp
|
||||
std::string constructKind; // declaration/definition/include/macro/class/member
|
||||
std::string fileType; // header/source
|
||||
std::string operation;
|
||||
};
|
||||
|
||||
struct CppConstructiveEditResult {
|
||||
bool supported = false;
|
||||
bool allowed = false;
|
||||
std::vector<std::string> legalOperations;
|
||||
std::vector<std::string> diagnostics;
|
||||
};
|
||||
|
||||
class CppConstructiveEditAdapter {
|
||||
public:
|
||||
static CppConstructiveEditResult evaluate(const CppConstructiveEditRequest& request) {
|
||||
CppConstructiveEditResult result;
|
||||
if (request.language != "c" && request.language != "cpp") {
|
||||
result.diagnostics.push_back("unsupported_language");
|
||||
return result;
|
||||
}
|
||||
|
||||
result.supported = true;
|
||||
result.legalOperations = legalOps(request.constructKind, request.fileType);
|
||||
if (result.legalOperations.empty()) {
|
||||
result.supported = false;
|
||||
result.diagnostics.push_back("unsupported_construct");
|
||||
return result;
|
||||
}
|
||||
|
||||
if (!contains(result.legalOperations, request.operation)) {
|
||||
result.diagnostics.push_back("operation_not_legal_for_construct");
|
||||
return result;
|
||||
}
|
||||
|
||||
auto boundary = boundaryGuard(request);
|
||||
if (!boundary.empty()) {
|
||||
result.diagnostics.push_back(boundary);
|
||||
return result;
|
||||
}
|
||||
|
||||
result.allowed = true;
|
||||
return result;
|
||||
}
|
||||
|
||||
private:
|
||||
static std::vector<std::string> legalOps(const std::string& kind,
|
||||
const std::string& fileType) {
|
||||
if (kind == "declaration") return {"rename", "update", "delete", "extract"};
|
||||
if (kind == "definition") return {"rename", "update", "extract", "inline"};
|
||||
if (kind == "include") return {"insert", "delete", "reorder"};
|
||||
if (kind == "macro") return {"rename", "update", "delete"};
|
||||
if (kind == "class") return {"rename", "update", "extract"};
|
||||
if (kind == "member") {
|
||||
if (fileType == "header") return {"rename", "update", "extract"};
|
||||
return {"rename", "update"};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
static std::string boundaryGuard(const CppConstructiveEditRequest& request) {
|
||||
// Header/source safety: definitions and include edits should stay in safe domains.
|
||||
if (request.constructKind == "definition" && request.fileType == "header" &&
|
||||
request.operation == "inline") {
|
||||
return "header_source_boundary_violation";
|
||||
}
|
||||
if (request.constructKind == "include" && request.fileType == "source" &&
|
||||
request.operation == "reorder") {
|
||||
return "source_include_reorder_blocked";
|
||||
}
|
||||
if (request.constructKind == "macro" && request.fileType == "source" &&
|
||||
request.operation == "delete") {
|
||||
return "macro_delete_requires_header_review";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
static bool contains(const std::vector<std::string>& values,
|
||||
const std::string& value) {
|
||||
return std::set<std::string>(values.begin(), values.end()).count(value) != 0;
|
||||
}
|
||||
};
|
||||
126
editor/tests/step539_test.cpp
Normal file
126
editor/tests/step539_test.cpp
Normal file
@@ -0,0 +1,126 @@
|
||||
// Step 539: C/C++ Constructive Edit Adapter (12 tests)
|
||||
|
||||
#include "CppConstructiveEditAdapter.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
static int passed = 0, failed = 0;
|
||||
#define TEST(name) { std::cout << " " << #name << "... "; }
|
||||
#define PASS() { std::cout << "PASS\n"; ++passed; }
|
||||
#define FAIL(msg) { std::cout << "FAIL: " << msg << "\n"; ++failed; }
|
||||
#define CHECK(cond, msg) if (!(cond)) { FAIL(msg); return; } else {}
|
||||
|
||||
static bool hasDiag(const CppConstructiveEditResult& r, const std::string& d) {
|
||||
for (const auto& x : r.diagnostics) if (x == d) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
void test_cpp_declaration_rename_allowed() {
|
||||
TEST(cpp_declaration_rename_allowed);
|
||||
auto r = CppConstructiveEditAdapter::evaluate({"cpp", "declaration", "header", "rename"});
|
||||
CHECK(r.supported && r.allowed, "declaration rename should be allowed");
|
||||
PASS();
|
||||
}
|
||||
|
||||
void test_cpp_definition_extract_allowed_in_source() {
|
||||
TEST(cpp_definition_extract_allowed_in_source);
|
||||
auto r = CppConstructiveEditAdapter::evaluate({"cpp", "definition", "source", "extract"});
|
||||
CHECK(r.allowed, "source definition extract should be allowed");
|
||||
PASS();
|
||||
}
|
||||
|
||||
void test_include_insert_allowed() {
|
||||
TEST(include_insert_allowed);
|
||||
auto r = CppConstructiveEditAdapter::evaluate({"cpp", "include", "header", "insert"});
|
||||
CHECK(r.allowed, "include insert should be allowed");
|
||||
PASS();
|
||||
}
|
||||
|
||||
void test_include_reorder_blocked_in_source() {
|
||||
TEST(include_reorder_blocked_in_source);
|
||||
auto r = CppConstructiveEditAdapter::evaluate({"cpp", "include", "source", "reorder"});
|
||||
CHECK(!r.allowed, "source include reorder should be blocked");
|
||||
CHECK(hasDiag(r, "source_include_reorder_blocked"), "boundary diagnostic expected");
|
||||
PASS();
|
||||
}
|
||||
|
||||
void test_macro_update_allowed() {
|
||||
TEST(macro_update_allowed);
|
||||
auto r = CppConstructiveEditAdapter::evaluate({"cpp", "macro", "header", "update"});
|
||||
CHECK(r.allowed, "macro update should be allowed");
|
||||
PASS();
|
||||
}
|
||||
|
||||
void test_macro_delete_blocked_in_source() {
|
||||
TEST(macro_delete_blocked_in_source);
|
||||
auto r = CppConstructiveEditAdapter::evaluate({"cpp", "macro", "source", "delete"});
|
||||
CHECK(!r.allowed, "macro delete in source should be blocked");
|
||||
CHECK(hasDiag(r, "macro_delete_requires_header_review"), "macro boundary diagnostic expected");
|
||||
PASS();
|
||||
}
|
||||
|
||||
void test_member_extract_allowed_in_header() {
|
||||
TEST(member_extract_allowed_in_header);
|
||||
auto r = CppConstructiveEditAdapter::evaluate({"cpp", "member", "header", "extract"});
|
||||
CHECK(r.allowed, "header member extract should be allowed");
|
||||
PASS();
|
||||
}
|
||||
|
||||
void test_member_extract_blocked_in_source() {
|
||||
TEST(member_extract_blocked_in_source);
|
||||
auto r = CppConstructiveEditAdapter::evaluate({"cpp", "member", "source", "extract"});
|
||||
CHECK(!r.allowed, "source member extract should be illegal");
|
||||
CHECK(hasDiag(r, "operation_not_legal_for_construct"), "illegal-op diagnostic expected");
|
||||
PASS();
|
||||
}
|
||||
|
||||
void test_definition_inline_blocked_in_header() {
|
||||
TEST(definition_inline_blocked_in_header);
|
||||
auto r = CppConstructiveEditAdapter::evaluate({"cpp", "definition", "header", "inline"});
|
||||
CHECK(!r.allowed, "header definition inline should be blocked");
|
||||
CHECK(hasDiag(r, "header_source_boundary_violation"), "boundary diagnostic expected");
|
||||
PASS();
|
||||
}
|
||||
|
||||
void test_unsupported_construct_rejected() {
|
||||
TEST(unsupported_construct_rejected);
|
||||
auto r = CppConstructiveEditAdapter::evaluate({"cpp", "template_magic", "header", "rename"});
|
||||
CHECK(!r.supported, "unsupported construct should not be supported");
|
||||
CHECK(hasDiag(r, "unsupported_construct"), "unsupported construct diagnostic expected");
|
||||
PASS();
|
||||
}
|
||||
|
||||
void test_unsupported_language_rejected() {
|
||||
TEST(unsupported_language_rejected);
|
||||
auto r = CppConstructiveEditAdapter::evaluate({"typescript", "definition", "source", "update"});
|
||||
CHECK(!r.supported, "unsupported language should not be supported");
|
||||
CHECK(hasDiag(r, "unsupported_language"), "unsupported language diagnostic expected");
|
||||
PASS();
|
||||
}
|
||||
|
||||
void test_c_language_paths_supported() {
|
||||
TEST(c_language_paths_supported);
|
||||
auto r = CppConstructiveEditAdapter::evaluate({"c", "definition", "source", "update"});
|
||||
CHECK(r.supported && r.allowed, "c definition update should be supported");
|
||||
PASS();
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::cout << "Step 539: C/C++ Constructive Edit Adapter\n";
|
||||
|
||||
test_cpp_declaration_rename_allowed(); // 1
|
||||
test_cpp_definition_extract_allowed_in_source();// 2
|
||||
test_include_insert_allowed(); // 3
|
||||
test_include_reorder_blocked_in_source(); // 4
|
||||
test_macro_update_allowed(); // 5
|
||||
test_macro_delete_blocked_in_source(); // 6
|
||||
test_member_extract_allowed_in_header(); // 7
|
||||
test_member_extract_blocked_in_source(); // 8
|
||||
test_definition_inline_blocked_in_header(); // 9
|
||||
test_unsupported_construct_rejected(); // 10
|
||||
test_unsupported_language_rejected(); // 11
|
||||
test_c_language_paths_supported(); // 12
|
||||
|
||||
std::cout << "\nResults: " << passed << "/" << (passed + failed) << " passed\n";
|
||||
return failed == 0 ? 0 : 1;
|
||||
}
|
||||
Reference in New Issue
Block a user