diff --git a/uppsrc/ide/Designers/Designers.h b/uppsrc/ide/Designers/Designers.h index 7d62fa599..4c3de8136 100644 --- a/uppsrc/ide/Designers/Designers.h +++ b/uppsrc/ide/Designers/Designers.h @@ -167,13 +167,15 @@ struct XmlViewDes : TreeViewDes { INITIALIZE(XmlViewDes) +void SetupJsonTree(TreeCtrl& tree); +void CopyJsonPath(TreeCtrl& tree); +String LoadJson(TreeCtrl& tree, const String& json); + struct JsonViewDes : TreeViewDes { virtual String GetId() { return "JSON"; } virtual String Load0(const String& json); virtual void CopyPath(); - int AddNode(int parent_id, const Value& id, const String& name, const Value& v); - JsonViewDes(); }; diff --git a/uppsrc/ide/Designers/Json.cpp b/uppsrc/ide/Designers/Json.cpp index af7489686..59b5c7e0f 100644 --- a/uppsrc/ide/Designers/Json.cpp +++ b/uppsrc/ide/Designers/Json.cpp @@ -1,18 +1,13 @@ #include "Designers.h" -JsonViewDes::JsonViewDes() -{ - tree.SetDisplay(QTFDisplay()); -} - -void JsonViewDes::CopyPath() +void CopyJsonPath(TreeCtrl& tree) { int id = tree.GetCursor(); String path; while(id) { Value k = tree.Get(id); if(!IsNull(k)) { - if(IsNumber(k)) + if(IsNumber(k) && tree.GetChildCount(id)) path = "[" + AsString(k) + "]" + path; if(IsString(k)) path = "[" + AsCString(String(k)) + "]" + path; @@ -22,22 +17,15 @@ void JsonViewDes::CopyPath() WriteClipboardText(path); } -String JsonViewDes::Load0(const String& json) +void CopyJsonNode(TreeCtrl& tree) { - String parsingError; - - CParser p(json); - try { - tree.Open(AddNode(0, Null, "JSON", ParseJSON(p))); - } - catch(const CParser::Error& e) { - parsingError << e; - } - - return parsingError; + int id = tree.GetCursor(); + if(id) + WriteClipboardText(ParseQTF(~tree.GetValue(id)).GetPlainText().ToString()); } -int JsonViewDes::AddNode(int parent_id, const Value& id, const String& name, const Value& v) +static +int sAddNode(TreeCtrl& tree, int parent_id, const Value& id, const String& name, const Value& v) { if(IsError(v)) { // TODO: Replace with JsonExc or something that is more accurate in this situation. @@ -50,13 +38,13 @@ int JsonViewDes::AddNode(int parent_id, const Value& id, const String& name, con ValueMap m = v; parent_id = tree.Add(parent_id, IdeCommonImg::JsonStruct(), id, "[G1 [* " + name); for(int i = 0; i < m.GetCount(); i++) - AddNode(parent_id, m.GetKey(i), "[@B \1" + String(m.GetKey(i)) + "\1:]", m.GetValue(i)); + sAddNode(tree, parent_id, m.GetKey(i), "[@B \1" + String(m.GetKey(i)) + "\1:]", m.GetValue(i)); } else if(v.Is()) { parent_id = tree.Add(parent_id, IdeCommonImg::JsonArray(), id, "[G1 [* " + name); for(int i = 0; i < v.GetCount(); i++) - AddNode(parent_id, i, "[@c " + AsString(i) + ":]", v[i]); + sAddNode(tree, parent_id, i, "[@c " + AsString(i) + ":]", v[i]); } else { String qtf = "[G1 [* " + name + "]"; @@ -81,6 +69,46 @@ int JsonViewDes::AddNode(int parent_id, const Value& id, const String& name, con return parent_id; } +String LoadJson(TreeCtrl& tree, const String& json) +{ + String parsingError; + + CParser p(json); + try { + tree.Open(sAddNode(tree, 0, Null, "JSON", ParseJSON(p))); + } + catch(const CParser::Error& e) { + parsingError << e; + } + + return parsingError; +} + +void SetupJsonTree(TreeCtrl& tree) +{ + tree.SetDisplay(QTFDisplay()); + tree.WhenLeftDouble = [=, &tree] { CopyJsonPath(tree); }; + tree.WhenBar = [=, &tree](Bar& bar) { + bar.Add(CtrlImg::copy(), "Copy", [=, &tree] { CopyJsonNode(tree); }).Key(K_CTRL_C); + bar.Add("Copy path\t[double-click]", [=, &tree] { CopyJsonPath(tree); }); + }; +} + +JsonViewDes::JsonViewDes() +{ + SetupJsonTree(tree); +} + +void JsonViewDes::CopyPath() +{ + CopyJsonPath(tree); +} + +String JsonViewDes::Load0(const String& json) +{ + return LoadJson(tree, json); +} + struct JsonDesModule : public IdeModule { virtual String GetID() { return "JsonDesModule"; } diff --git a/uppsrc/ide/Json.cpp b/uppsrc/ide/Json.cpp index bc6ab9ead..df927dfd9 100644 --- a/uppsrc/ide/Json.cpp +++ b/uppsrc/ide/Json.cpp @@ -23,8 +23,6 @@ public: private: void Reset(); String Load0(const String& json); - - int AddNode(int parent_id, const Value& id, const String& name, const Value& v); }; JsonView::JsonView() @@ -40,9 +38,8 @@ JsonView::JsonView() errorbg.Height(25).Add(error.SizePos()); view.SetReadOnly(); view.SetColor(LineEdit::PAPER_READONLY, SColorPaper()); - tree.SetDisplay(QTFDisplay()); tree.NoRoot(); - tree.WhenLeftDouble = THISBACK(CopyPath); + SetupJsonTree(tree); } bool JsonView::Key(dword key, int count) @@ -76,19 +73,7 @@ void JsonView::Load(const String& json) void JsonView::CopyPath() { - int id = tree.GetCursor(); - String path; - while(id) { - Value k = tree.Get(id); - if(!IsNull(k)) { - if(IsNumber(k)) - path = "[" + AsString(k) + "]" + path; - if(IsString(k)) - path = "[" + AsCString(String(k)) + "]" + path; - } - id = tree.GetParent(id); - } - WriteClipboardText(path); + ::CopyJsonPath(tree); } void JsonView::Serialize(Stream& s) @@ -115,61 +100,7 @@ void JsonView::Reset() String JsonView::Load0(const String& json) { - String parsingError; - - CParser p(json); - try { - tree.Open(AddNode(0, Null, "JSON", ParseJSON(p))); - } - catch(const CParser::Error& e) { - parsingError << e; - } - - return parsingError; -} - -int JsonView::AddNode(int parent_id, const Value& id, const String& name, const Value& v) -{ - if(IsError(v)) { - // TODO: Replace with JsonExc or something that is more accurate in this situation. - String errorText = GetErrorText(v); - errorText.Remove(0, errorText.Find(" ") + 1); - throw Exc(errorText); - } - else - if(v.Is()) { - ValueMap m = v; - parent_id = tree.Add(parent_id, IdeCommonImg::JsonStruct(), id, "[G1 [* " + name); - for(int i = 0; i < m.GetCount(); i++) - AddNode(parent_id, m.GetKey(i), "[@B \1" + String(m.GetKey(i)) + "\1:]", m.GetValue(i)); - } - else - if(v.Is()) { - parent_id = tree.Add(parent_id, IdeCommonImg::JsonArray(), id, "[G1 [* " + name); - for(int i = 0; i < v.GetCount(); i++) - AddNode(parent_id, i, "[@c " + AsString(i) + ":]", v[i]); - } - else { - String qtf = "[G1 [* " + name + "]"; - Image img = IdeCommonImg::JsonNumber(); - if(IsString(v)) { - img = IdeCommonImg::JsonString(); - if(IsNull(v)) - qtf << "[*@g Null"; - else - qtf << "[@r \1 " + AsCString(String(v)); - } - else { - if(v.Is()) - img = IdeCommonImg::JsonBool(); - if(IsNull(v)) - qtf << "[*@g Null"; - else - qtf << "\1 " + AsString(v); - } - parent_id = tree.Add(parent_id, img, id, qtf); - } - return parent_id; + return LoadJson(tree, json); } void Ide::Json()