88#include < format>
99#include < regex>
1010
11+ constexpr int FILENAME_LEN_IN_TITLE = 16 ;
1112
1213JsonViewDlg::JsonViewDlg (HINSTANCE hInstance, const NppData &nppData, const bool &isReady, int nCmdId, std::shared_ptr<Setting> &pSetting)
1314 : DockingDlgInterface(IDD_TREEDLG)
1415 , m_NppData(nppData)
1516 , m_IsNppReady(isReady)
1617 , m_nDlgId(nCmdId)
17- , m_Editor (std::make_unique<ScintillaEditor>(nppData))
18+ , m_pEditor (std::make_unique<ScintillaEditor>(nppData))
1819 , m_hTreeView(std::make_unique<TreeViewCtrl>())
1920 , m_pSetting(pSetting)
21+ , m_pCurrFileName(std::make_unique<wchar_t []>(FILENAME_LEN_IN_TITLE))
2022{
2123 _hParent = nppData._nppHandle ;
2224 _hInst = hInstance;
@@ -48,10 +50,11 @@ void JsonViewDlg::ShowDlg(bool bShow)
4850 m_lfInitialClientHeight = rc.bottom - rc.top ;
4951
5052 // define the default docking behaviour
51- data.uMask = DWS_DF_CONT_LEFT | DWS_ICONTAB;
53+ data.uMask = DWS_DF_CONT_LEFT | DWS_ICONTAB | DWS_ADDINFO ;
5254 data.pszModuleName = getPluginFileName ();
5355 data.pszName = const_cast <TCHAR *>(TITLE_JSON_PANEL);
5456 data.hIconTab = static_cast <HICON>(LoadImage (_hInst, MAKEINTRESOURCE (IDI_ICON_TOOLBAR), IMAGE_ICON, 32 , 32 , LR_LOADMAP3DCOLORS | LR_LOADTRANSPARENT));
57+ data.pszAddInfo = m_pCurrFileName.get ();
5558
5659 // the dlgDlg should be the index of funcItem where the current function pointer is
5760 data.dlgID = static_cast <int >(CallBackID::SHOW_DOC_PANEL);
@@ -64,19 +67,20 @@ void JsonViewDlg::ShowDlg(bool bShow)
6467 DrawJsonTree ();
6568 }
6669
70+ UpdateTitle ();
6771 DockingDlgInterface::display (bShow);
6872}
6973
7074void JsonViewDlg::FormatJson ()
7175{
72- const auto selectedText = m_Editor ->GetJsonText ();
76+ const auto selectedText = m_pEditor ->GetJsonText ();
7377 auto [le, lf, indentChar, indentLen] = GetFormatSetting ();
7478
7579 Result res = JsonHandler (m_pSetting->parseOptions ).FormatJson (selectedText, le, lf, indentChar, indentLen);
7680
7781 if (res.success )
7882 {
79- m_Editor ->ReplaceSelection (res.response );
83+ m_pEditor ->ReplaceSelection (res.response );
8084 HighlightAsJson ();
8185 }
8286 else
@@ -91,13 +95,13 @@ void JsonViewDlg::FormatJson()
9195void JsonViewDlg::CompressJson ()
9296{
9397 // Get the current scintilla
94- const auto selectedText = m_Editor ->GetJsonText ();
98+ const auto selectedText = m_pEditor ->GetJsonText ();
9599
96100 Result res = JsonHandler (m_pSetting->parseOptions ).GetCompressedJson (selectedText);
97101
98102 if (res.success )
99103 {
100- m_Editor ->ReplaceSelection (res.response );
104+ m_pEditor ->ReplaceSelection (res.response );
101105 HighlightAsJson ();
102106 }
103107 else
@@ -111,14 +115,14 @@ void JsonViewDlg::CompressJson()
111115
112116void JsonViewDlg::SortJsonByKey ()
113117{
114- const auto selectedText = m_Editor ->GetJsonText ();
118+ const auto selectedText = m_pEditor ->GetJsonText ();
115119 auto [le, lf, indentChar, indentLen] = GetFormatSetting ();
116120
117121 Result res = JsonHandler (m_pSetting->parseOptions ).SortJsonByKey (selectedText, le, lf, indentChar, indentLen);
118122
119123 if (res.success )
120124 {
121- m_Editor ->ReplaceSelection (res.response );
125+ m_pEditor ->ReplaceSelection (res.response );
122126 HighlightAsJson ();
123127 }
124128 else
@@ -176,15 +180,15 @@ bool JsonViewDlg::CheckForTokenUndefined(eMethod method, std::string selectedTex
176180 if (res.success )
177181 {
178182 bool bShouldReplace = method == eMethod::ParseJson || method == eMethod::ValidateJson || method == eMethod::SortJsonByKey;
179- m_Editor ->ReplaceSelection (bShouldReplace ? text : res.response );
183+ m_pEditor ->ReplaceSelection (bShouldReplace ? text : res.response );
180184 HighlightAsJson ();
181185 return true ;
182186 }
183187 else
184188 {
185- m_Editor ->ReplaceSelection (text);
186- m_Editor ->MakeSelection (m_Editor ->GetSelectionStart (), static_cast <int >(text.length ()));
187- m_Editor ->RefreshSelectionPos ();
189+ m_pEditor ->ReplaceSelection (text);
190+ m_pEditor ->MakeSelection (m_pEditor ->GetSelectionStart (), static_cast <int >(text.length ()));
191+ m_pEditor ->RefreshSelectionPos ();
188192 }
189193 }
190194 catch (const std::exception &)
@@ -200,8 +204,8 @@ void JsonViewDlg::HandleTabActivated()
200204 const bool bIsVisible = isCreated () && isVisible ();
201205 if (bIsVisible)
202206 {
203- m_Editor ->RefreshViewHandle ();
204- if (m_Editor ->IsJsonFile ())
207+ m_pEditor ->RefreshViewHandle ();
208+ if (m_pEditor ->IsJsonFile ())
205209 {
206210 if (m_pSetting->bFollowCurrentTab )
207211 {
@@ -212,14 +216,15 @@ void JsonViewDlg::HandleTabActivated()
212216 {
213217 FormatJson ();
214218 }
219+ UpdateTitle ();
215220 }
216221 }
217222}
218223
219224void JsonViewDlg::ValidateJson ()
220225{
221226 // Get the current scintilla
222- const auto selectedText = m_Editor ->GetJsonText ();
227+ const auto selectedText = m_pEditor ->GetJsonText ();
223228
224229 Result res = JsonHandler (m_pSetting->parseOptions ).ValidateJson (selectedText);
225230
@@ -249,8 +254,8 @@ void JsonViewDlg::DrawJsonTree()
249254 rootNode = m_hTreeView->InitTree ();
250255
251256 // Refresh the view
252- m_Editor ->RefreshViewHandle ();
253- const std::string txtForParsing = m_Editor ->GetJsonText ();
257+ m_pEditor ->RefreshViewHandle ();
258+ const std::string txtForParsing = m_pEditor ->GetJsonText ();
254259
255260 if (txtForParsing.empty ())
256261 {
@@ -286,7 +291,7 @@ void JsonViewDlg::HighlightAsJson(bool bForcefully) const
286291{
287292 bool setJsonLang = bForcefully || m_pSetting->bUseJsonHighlight ;
288293 if (setJsonLang)
289- m_Editor ->SetLangAsJson ();
294+ m_pEditor ->SetLangAsJson ();
290295}
291296
292297auto JsonViewDlg::PopulateTreeUsingSax (HTREEITEM tree_root, const std::string &jsonText) -> std::optional<std::wstring>
@@ -310,9 +315,9 @@ auto JsonViewDlg::PopulateTreeUsingSax(HTREEITEM tree_root, const std::string &j
310315 else
311316 {
312317 // Mark the error position
313- size_t start = m_Editor ->GetSelectionStart ();
318+ size_t start = m_pEditor ->GetSelectionStart ();
314319 size_t errPosition = start + static_cast <size_t >(res.error_pos );
315- m_Editor ->MakeSelection (errPosition, errPosition + 1 );
320+ m_pEditor ->MakeSelection (errPosition, errPosition + 1 );
316321
317322 std::string err = std::format (" \n\n Error: ({} : {})" , res.error_code , res.error_str );
318323 retVal = std::make_optional<std::wstring>((JSON_ERR_VALIDATE + StringHelper::ToWstring (err)));
@@ -435,6 +440,32 @@ void JsonViewDlg::SearchInTree()
435440 }
436441}
437442
443+ void JsonViewDlg::UpdateTitle ()
444+ {
445+ GetTitleFileName ();
446+ updateDockingDlg ();
447+ }
448+
449+ void JsonViewDlg::GetTitleFileName ()
450+ {
451+ if (!m_pCurrFileName)
452+ {
453+ m_pCurrFileName = std::make_unique<wchar_t []>(FILENAME_LEN_IN_TITLE);
454+ }
455+
456+ auto currFile = m_pEditor->GetCurrentFileName ();
457+ if (currFile.length () >= FILENAME_LEN_IN_TITLE)
458+ {
459+ // If the filename is too long, truncate it and add "..."
460+ currFile = currFile.substr (0 , FILENAME_LEN_IN_TITLE - 4 ) + L" ..." ;
461+ }
462+
463+ memset (m_pCurrFileName.get (), 0 , FILENAME_LEN_IN_TITLE);
464+ wcsncpy_s (m_pCurrFileName.get (), FILENAME_LEN_IN_TITLE, currFile.c_str (), _TRUNCATE);
465+
466+ updateDockingDlg ();
467+ }
468+
438469void JsonViewDlg::PrepareButtons ()
439470{
440471 // Refresh Button
@@ -707,9 +738,9 @@ int JsonViewDlg::ShowMessage(const std::wstring &title, const std::wstring &msg,
707738void JsonViewDlg::ReportError (const Result &result)
708739{
709740 // Mark the error position
710- size_t start = m_Editor ->GetSelectionStart () + result.error_pos ;
711- size_t end = m_Editor ->GetSelectionEnd ();
712- m_Editor ->MakeSelection (start, end);
741+ size_t start = m_pEditor ->GetSelectionStart () + result.error_pos ;
742+ size_t end = m_pEditor ->GetSelectionEnd ();
743+ m_pEditor ->MakeSelection (start, end);
713744
714745 // Intimate user
715746 std::string err = std::format (" \n\n Error: ({} : {})" , result.error_code , result.error_str );
@@ -784,7 +815,7 @@ auto JsonViewDlg::GetFormatSetting() const -> std::tuple<LE, LF, char, unsigned>
784815 case LineEnding::AUTO:
785816 default :
786817 {
787- const auto eol = m_Editor ->GetEOL ();
818+ const auto eol = m_pEditor ->GetEOL ();
788819 switch (eol)
789820 {
790821 case 0 :
@@ -816,7 +847,7 @@ auto JsonViewDlg::GetFormatSetting() const -> std::tuple<LE, LF, char, unsigned>
816847 // Takes from Notepad++
817848 case IndentStyle::AUTO:
818849 default :
819- auto [c, l] = m_Editor ->GetIndent ();
850+ auto [c, l] = m_pEditor ->GetIndent ();
820851 indentChar = c;
821852 indentLen = l;
822853 break ;
0 commit comments