Enhance debug visualization for linked list with custom node table view
This commit is contained in:
17
cpp/main.cpp
17
cpp/main.cpp
@@ -50,20 +50,25 @@ public:
|
||||
return head_ ? &head_->data : nullptr;
|
||||
}
|
||||
|
||||
// Build a JSON string describing the list as a graph, for the VS Code
|
||||
// "Debug Visualizer" extension: watch `list.toDebugGraph()` while paused.
|
||||
// Schema: https://hediet.github.io/visualization/docs/visualization-data-schema.json
|
||||
// Build a JSON string describing the list for the VS Code "Debug
|
||||
// Visualizer" extension's custom "node table" view (see
|
||||
// vis-plugins/node-table-visualizer.js): watch `list.toDebugGraph()`
|
||||
// while paused. Each node is rendered as its own address-labeled box
|
||||
// with a field table -- data as a plain value, next as a hex pointer.
|
||||
std::string toDebugGraph() const {
|
||||
std::ostringstream json;
|
||||
json << "{\"kind\":{\"graph\":true},\"nodes\":[";
|
||||
json << "{\"kind\":{\"nodeTable\":true},\"nodes\":[";
|
||||
for (Node<T>* n = head_; n; n = n->next) {
|
||||
if (n != head_) json << ",";
|
||||
json << "{\"id\":\"" << n << "\",\"label\":\"" << n->data << "\"}";
|
||||
json << "{\"id\":\"" << n << "\",\"fields\":["
|
||||
<< "{\"name\":\"data\",\"value\":\"" << n->data << "\"},"
|
||||
<< "{\"name\":\"next\",\"value\":\"" << n->next << "\",\"isPointer\":true}"
|
||||
<< "]}";
|
||||
}
|
||||
json << "],\"edges\":[";
|
||||
for (Node<T>* n = head_; n && n->next; n = n->next) {
|
||||
if (n != head_) json << ",";
|
||||
json << "{\"from\":\"" << n << "\",\"to\":\"" << n->next << "\"}";
|
||||
json << "{\"from\":\"" << n << "\",\"to\":\"" << n->next << "\",\"label\":\"next\"}";
|
||||
}
|
||||
json << "]}";
|
||||
return json.str();
|
||||
|
||||
Reference in New Issue
Block a user