Phase 3c: Classical editing mode (Steps 50-53)

Implement TextASTSync and TextEditor — the core text editing layer
that bridges raw text and the structured AST. TextASTSync provides
bidirectional text-AST sync with debounce via TreeSitterParser and
generators. TextEditor wraps a text buffer with edit operations,
undo/redo that tracks AST state, find/replace, and selection.

New files: TextASTSync.h, TextEditor.h, step50_test.cpp
Updated: step51/53 tests (replaced stubs with real includes),
CMakeLists.txt (tree-sitter linking), PROGRESS.md
Fixed: step53 find-position off-by-one, canRedo assert logic

19/19 tests pass (step50: 8, step51: 5, step53: 6)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Bill
2026-02-08 15:45:59 -07:00
parent 41166812b0
commit 742c33d813
7 changed files with 380 additions and 86 deletions

View File

@@ -1,52 +1,28 @@
// Step 51 TDD Test: Text AST synchronization
// Step 51 TDD Test: Text -> AST synchronization
//
// Tests bidirectional sync between text editing and AST:
// 1. Text change re-parse via tree-sitter AST updates
// 2. AST change (structured edit) regenerate text
// 3. Round-trip: text AST text produces semantically equivalent output
// 1. Text change -> re-parse via tree-sitter -> AST updates
// 2. AST change (structured edit) -> regenerate text
// 3. Round-trip: text -> AST -> text produces semantically equivalent output
// 4. Debouncing: rapid text changes don't cause excessive re-parsing
//
// Will fail until the text-AST sync layer is implemented.
// Depends on: TextASTSync, TreeSitterParser, PythonGenerator, CppGenerator
#include <iostream>
#include <string>
#include <cassert>
#include <memory>
#include "ast/Parser.h"
#include "ast/Generator.h"
#include "TextASTSync.h"
static bool contains(const std::string& haystack, const std::string& needle) {
return haystack.find(needle) != std::string::npos;
}
// Forward declaration for the sync class that doesn't exist yet
// This will need to be implemented as part of Step 51
class TextASTSync {
public:
// Set the source text; triggers re-parse (possibly debounced)
void setText(const std::string& text, const std::string& language);
// Get the current AST root
Module* getAST() const;
// Mutate the AST; triggers text regeneration
void setAST(std::unique_ptr<Module> module);
// Get the current text representation
std::string getText() const;
// Force immediate sync (skip debounce)
void syncNow();
// Check if a re-parse is pending (debounced but not yet executed)
bool isParsePending() const;
};
int main() {
int passed = 0;
int failed = 0;
// --- Test 1: Set text AST updates ---
// --- Test 1: Set text -> AST updates ---
{
TextASTSync sync;
sync.setText("def greet(name):\n return name\n", "python");
@@ -61,11 +37,11 @@ int main() {
auto* fn = static_cast<Function*>(functions[0]);
assert(fn->name == "greet" && "Function name should be 'greet'");
std::cout << "Test 1 PASS: setText AST has correct function" << std::endl;
std::cout << "Test 1 PASS: setText -> AST has correct function" << std::endl;
++passed;
}
// --- Test 2: AST change text updates ---
// --- Test 2: AST change -> text updates ---
{
TextASTSync sync;
sync.setText("def f(x):\n return x\n", "python");
@@ -83,11 +59,11 @@ int main() {
assert(contains(newText, "compute") && "Text should reflect renamed function");
assert(!contains(newText, "def f(") && "Old function name should be gone");
std::cout << "Test 2 PASS: AST change text updates" << std::endl;
std::cout << "Test 2 PASS: AST change -> text updates" << std::endl;
++passed;
}
// --- Test 3: Round-trip text AST text ---
// --- Test 3: Round-trip text -> AST -> text ---
{
std::string original = "def add(a, b):\n return a + b\n";
TextASTSync sync;
@@ -108,11 +84,11 @@ int main() {
++passed;
}
// --- Test 4: Debounce rapid changes don't cause immediate re-parse ---
// --- Test 4: Debounce --- rapid changes don't cause immediate re-parse ---
{
TextASTSync sync;
sync.setText("def a():\n pass\n", "python");
// Don't call syncNow simulate rapid edits
// Don't call syncNow --- simulate rapid edits
sync.setText("def ab():\n pass\n", "python");
sync.setText("def abc():\n pass\n", "python");
@@ -133,7 +109,7 @@ int main() {
++passed;
}
// --- Test 5: C++ text AST sync ---
// --- Test 5: C++ text -> AST sync ---
{
TextASTSync sync;
sync.setText("int f(int x) { return x + 1; }", "cpp");
@@ -146,7 +122,7 @@ int main() {
auto functions = ast->getChildren("functions");
assert(!functions.empty() && "Should parse C++ function");
std::cout << "Test 5 PASS: C++ text AST sync works" << std::endl;
std::cout << "Test 5 PASS: C++ text -> AST sync works" << std::endl;
++passed;
}