1+ #include < format>
2+ #include < regex>
3+
14#include " JsonViewDlg.h"
25#include " Define.h"
36#include " Utility.h"
47#include " StringHelper.h"
58#include " RapidJsonHandler.h"
69#include " ScintillaEditor.h"
710#include " Profile.h"
8- #include < format>
9- #include < regex>
11+
1012
1113constexpr int FILENAME_LEN_IN_TITLE = 16 ;
1214
@@ -100,6 +102,8 @@ void JsonViewDlg::FormatJson()
100102
101103 ReportError (res);
102104 }
105+
106+ ReDrawJsonTree ();
103107}
104108
105109void JsonViewDlg::CompressJson ()
@@ -130,6 +134,8 @@ void JsonViewDlg::CompressJson()
130134
131135 ReportError (res);
132136 }
137+
138+ ReDrawJsonTree ();
133139}
134140
135141void JsonViewDlg::SortJsonByKey ()
@@ -162,6 +168,8 @@ void JsonViewDlg::SortJsonByKey()
162168
163169 ReportError (res);
164170 }
171+
172+ ReDrawJsonTree ();
165173}
166174
167175bool JsonViewDlg::CheckForTokenUndefined (eMethod method, std::string selectedText, Result& res, HTREEITEM tree_root)
@@ -171,14 +179,8 @@ bool JsonViewDlg::CheckForTokenUndefined(eMethod method, std::string selectedTex
171179 if (m_pSetting->parseOptions .bReplaceUndefined )
172180 {
173181 auto text = selectedText.substr (res.error_pos , 9 );
174- std::transform (
175- text.begin (),
176- text.end (),
177- text.begin (),
178- [](unsigned char c)
179- {
180- return (unsigned char )std::tolower (c);
181- });
182+ StringHelper::ToLower (text);
183+
182184 if (text == " undefined" )
183185 {
184186 try
@@ -217,7 +219,7 @@ bool JsonViewDlg::CheckForTokenUndefined(eMethod method, std::string selectedTex
217219 else
218220 {
219221 m_pEditor->ReplaceSelection (text);
220- m_pEditor->MakeSelection (m_pEditor->GetSelectionStart (), static_cast < int >( text.length () ));
222+ m_pEditor->MakeSelection (m_pEditor->GetSelectionStart (), text.length ());
221223 m_pEditor->RefreshSelectionPos ();
222224 }
223225 }
@@ -326,6 +328,8 @@ void JsonViewDlg::ValidateJson()
326328
327329 ReportError (res);
328330 }
331+
332+ DrawJsonTree ();
329333}
330334
331335void JsonViewDlg::DrawJsonTree ()
@@ -379,6 +383,16 @@ void JsonViewDlg::DrawJsonTree()
379383 EnableControls (ctrls, true );
380384}
381385
386+ void JsonViewDlg::ReDrawJsonTree (bool bForce)
387+ {
388+ const bool bIsVisible = isCreated () && isVisible ();
389+ const bool bReDraw = bForce || bIsVisible;
390+ if (bReDraw)
391+ {
392+ DrawJsonTree ();
393+ }
394+ }
395+
382396void JsonViewDlg::HighlightAsJson (bool bForcefully) const
383397{
384398 bool setJsonLang = bForcefully || m_pSetting->bUseJsonHighlight ;
@@ -390,10 +404,11 @@ auto JsonViewDlg::PopulateTreeUsingSax(HTREEITEM tree_root, const std::string& j
390404{
391405 std::optional<std::wstring> retVal = std::nullopt ;
392406
393- RapidJsonHandler handler (this , tree_root);
407+ auto pTS = std::make_shared<TrackingStream>(jsonText);
408+ RapidJsonHandler handler (this , tree_root, pTS);
394409 rapidjson::StringBuffer sb;
395410
396- Result res = JsonHandler (m_pSetting->parseOptions ).ParseJson <flgBaseReader>(jsonText, sb, handler);
411+ Result res = JsonHandler (m_pSetting->parseOptions ).ParseJson <flgBaseReader>(jsonText, sb, handler, pTS );
397412 if (!res.success )
398413 {
399414 if (CheckForTokenUndefined (JsonViewDlg::eMethod::ParseJson, jsonText, res, tree_root))
@@ -429,6 +444,13 @@ HTREEITEM JsonViewDlg::InsertToTree(HTREEITEM parent, const std::string& text)
429444 return m_hTreeView->InsertNode (wText, NULL , parent);
430445}
431446
447+ HTREEITEM JsonViewDlg::InsertToTree (HTREEITEM parent, const std::string& text, const Position& pos)
448+ {
449+ auto wText = StringHelper::ToWstring (text, CP_UTF8);
450+ auto lparam = new Position (pos);
451+ return m_hTreeView->InsertNode (wText, reinterpret_cast <LPARAM>(lparam), parent);
452+ }
453+
432454void JsonViewDlg::AppendNodeCount (HTREEITEM node, unsigned elementCount, bool bArray)
433455{
434456 if (!node)
@@ -450,6 +472,16 @@ void JsonViewDlg::UpdateNodePath(HTREEITEM htiNode)
450472 CUtility::SetEditCtrlText (::GetDlgItem (_hSelf, IDC_EDT_NODEPATH), nodePath);
451473}
452474
475+ void JsonViewDlg::GoToLine (size_t nLineToGo)
476+ {
477+ m_pEditor->GoToLine (nLineToGo);
478+ }
479+
480+ void JsonViewDlg::GoToPosition (size_t nLineToGo, size_t nPos)
481+ {
482+ m_pEditor->GoToPosition (nLineToGo, nPos);
483+ }
484+
453485void JsonViewDlg::SearchInTree ()
454486{
455487 std::wstring itemToSearch = CUtility::GetEditCtrlText (::GetDlgItem (_hSelf, IDC_EDT_SEARCH));
@@ -878,6 +910,24 @@ void JsonViewDlg::HandleTreeEvents(LPARAM lParam)
878910 if (hItem && (pnmtv->action == TVC_BYMOUSE || pnmtv->action == TVC_BYKEYBOARD))
879911 {
880912 UpdateNodePath (hItem);
913+
914+ auto pPosition = m_hTreeView->GetNodePosition (hItem);
915+ if (pPosition != nullptr )
916+ {
917+ GoToLine (pPosition->nLine );
918+ }
919+ }
920+ }
921+ break ;
922+
923+ case NM_DBLCLK:
924+ {
925+ HTREEITEM hItem = m_hTreeView->GetSelection ();
926+
927+ auto pPosition = m_hTreeView->GetNodePosition (hItem);
928+ if (pPosition != nullptr )
929+ {
930+ GoToPosition (pPosition->nLine , pPosition->nColumn );
881931 }
882932 }
883933 break ;
0 commit comments