@@ -73,10 +73,19 @@ void JsonViewDlg::ShowDlg(bool bShow)
7373
7474void JsonViewDlg::FormatJson ()
7575{
76- const auto selectedText = m_pEditor->GetJsonText ();
76+ const auto selectedData = m_pEditor->GetJsonText ();
77+ const auto selectedText = IsSelectionValidJson (selectedData);
78+
79+ if (!selectedText.has_value () || selectedText.value ().empty ())
80+ {
81+ const std::wstring msg = IsMultiSelection (selectedData) ? JSON_ERR_MULTI_SELECTION : JSON_ERR_PARSE;
82+ ShowMessage (JSON_INFO_TITLE, msg, MB_OK | MB_ICONINFORMATION);
83+ return ;
84+ }
85+
7786 auto [le, lf, indentChar, indentLen] = GetFormatSetting ();
7887
79- Result res = JsonHandler (m_pSetting->parseOptions ).FormatJson (selectedText, le, lf, indentChar, indentLen);
88+ Result res = JsonHandler (m_pSetting->parseOptions ).FormatJson (selectedText. value () , le, lf, indentChar, indentLen);
8089
8190 if (res.success )
8291 {
@@ -85,7 +94,7 @@ void JsonViewDlg::FormatJson()
8594 }
8695 else
8796 {
88- if (CheckForTokenUndefined (JsonViewDlg::eMethod::FormatJson, selectedText, res, NULL ))
97+ if (CheckForTokenUndefined (JsonViewDlg::eMethod::FormatJson, selectedText. value () , res, NULL ))
8998 return ;
9099
91100 ReportError (res);
@@ -94,10 +103,17 @@ void JsonViewDlg::FormatJson()
94103
95104void JsonViewDlg::CompressJson ()
96105{
97- // Get the current scintilla
98- const auto selectedText = m_pEditor-> GetJsonText ( );
106+ const auto selectedData = m_pEditor-> GetJsonText ();
107+ const auto selectedText = IsSelectionValidJson (selectedData );
99108
100- Result res = JsonHandler (m_pSetting->parseOptions ).GetCompressedJson (selectedText);
109+ if (!selectedText.has_value () || selectedText.value ().empty ())
110+ {
111+ const std::wstring msg = IsMultiSelection (selectedData) ? JSON_ERR_MULTI_SELECTION : JSON_ERR_PARSE;
112+ ShowMessage (JSON_INFO_TITLE, msg, MB_OK | MB_ICONINFORMATION);
113+ return ;
114+ }
115+
116+ Result res = JsonHandler (m_pSetting->parseOptions ).GetCompressedJson (selectedText.value ());
101117
102118 if (res.success )
103119 {
@@ -106,7 +122,7 @@ void JsonViewDlg::CompressJson()
106122 }
107123 else
108124 {
109- if (CheckForTokenUndefined (JsonViewDlg::eMethod::GetCompressedJson, selectedText, res, NULL ))
125+ if (CheckForTokenUndefined (JsonViewDlg::eMethod::GetCompressedJson, selectedText. value () , res, NULL ))
110126 return ;
111127
112128 ReportError (res);
@@ -115,10 +131,19 @@ void JsonViewDlg::CompressJson()
115131
116132void JsonViewDlg::SortJsonByKey ()
117133{
118- const auto selectedText = m_pEditor->GetJsonText ();
134+ const auto selectedData = m_pEditor->GetJsonText ();
135+ const auto selectedText = IsSelectionValidJson (selectedData);
136+
137+ if (!selectedText.has_value () || selectedText.value ().empty ())
138+ {
139+ const std::wstring msg = IsMultiSelection (selectedData) ? JSON_ERR_MULTI_SELECTION : JSON_ERR_PARSE;
140+ ShowMessage (JSON_INFO_TITLE, msg, MB_OK | MB_ICONINFORMATION);
141+ return ;
142+ }
143+
119144 auto [le, lf, indentChar, indentLen] = GetFormatSetting ();
120145
121- Result res = JsonHandler (m_pSetting->parseOptions ).SortJsonByKey (selectedText, le, lf, indentChar, indentLen);
146+ Result res = JsonHandler (m_pSetting->parseOptions ).SortJsonByKey (selectedText. value () , le, lf, indentChar, indentLen);
122147
123148 if (res.success )
124149 {
@@ -127,7 +152,7 @@ void JsonViewDlg::SortJsonByKey()
127152 }
128153 else
129154 {
130- if (CheckForTokenUndefined (JsonViewDlg::eMethod::SortJsonByKey, selectedText, res, NULL ))
155+ if (CheckForTokenUndefined (JsonViewDlg::eMethod::SortJsonByKey, selectedText. value () , res, NULL ))
131156 return ;
132157
133158 ReportError (res);
@@ -199,6 +224,52 @@ bool JsonViewDlg::CheckForTokenUndefined(eMethod method, std::string selectedTex
199224 return false ;
200225}
201226
227+ bool JsonViewDlg::IsMultiSelection (const ScintillaData &scintillaData) const
228+ {
229+ std::string text;
230+ ScintillaCode code = ScintillaCode::Unknown;
231+
232+ ProcessScintillaData (scintillaData, text, code);
233+
234+ bool bRetVal = code == ScintillaCode::MultiLineSelection ? true : false ;
235+ return bRetVal;
236+ }
237+
238+ auto JsonViewDlg::IsSelectionValidJson (const ScintillaData &scintillaData) const -> std::optional<std::string>
239+ {
240+ std::string text;
241+ ScintillaCode code = ScintillaCode::Unknown;
242+
243+ ProcessScintillaData (scintillaData, text, code);
244+
245+ if (code == ScintillaCode::Success)
246+ return text;
247+
248+ return std::nullopt ;
249+ }
250+
251+ void JsonViewDlg::ProcessScintillaData (const ScintillaData &scintillaData, std::string &text, ScintillaCode &code) const
252+ {
253+ text.clear ();
254+ code = ScintillaCode::Unknown;
255+
256+ std::visit (
257+ [&text, &code](auto &&arg)
258+ {
259+ using T = std::decay_t <decltype (arg)>;
260+ if constexpr (std::is_same_v<T, std::string>)
261+ {
262+ text = arg;
263+ code = ScintillaCode::Success;
264+ }
265+ else if constexpr (std::is_same_v<T, ScintillaCode>)
266+ {
267+ code = arg;
268+ }
269+ },
270+ scintillaData);
271+ }
272+
202273void JsonViewDlg::HandleTabActivated ()
203274{
204275 const bool bIsVisible = isCreated () && isVisible ();
@@ -223,18 +294,25 @@ void JsonViewDlg::HandleTabActivated()
223294
224295void JsonViewDlg::ValidateJson ()
225296{
226- // Get the current scintilla
227- const auto selectedText = m_pEditor->GetJsonText ();
297+ const auto selectedData = m_pEditor->GetJsonText ();
298+ const auto selectedText = IsSelectionValidJson (selectedData);
299+
300+ if (!selectedText.has_value () || selectedText.value ().empty ())
301+ {
302+ const std::wstring msg = IsMultiSelection (selectedData) ? JSON_ERR_MULTI_SELECTION : JSON_ERR_PARSE;
303+ ShowMessage (JSON_INFO_TITLE, msg, MB_OK | MB_ICONINFORMATION);
304+ return ;
305+ }
228306
229- Result res = JsonHandler (m_pSetting->parseOptions ).ValidateJson (selectedText);
307+ Result res = JsonHandler (m_pSetting->parseOptions ).ValidateJson (selectedText. value () );
230308
231309 if (res.success )
232310 {
233311 ShowMessage (JSON_INFO_TITLE, JSON_ERR_VALIDATE_SUCCESS, MB_OK | MB_ICONINFORMATION);
234312 }
235313 else
236314 {
237- if (CheckForTokenUndefined (JsonViewDlg::eMethod::ValidateJson, selectedText, res, NULL ))
315+ if (CheckForTokenUndefined (JsonViewDlg::eMethod::ValidateJson, selectedText. value () , res, NULL ))
238316 {
239317 ShowMessage (JSON_INFO_TITLE, JSON_ERR_VALIDATE_SUCCESS, MB_OK | MB_ICONINFORMATION);
240318 return ;
@@ -255,15 +333,21 @@ void JsonViewDlg::DrawJsonTree()
255333
256334 // Refresh the view
257335 m_pEditor->RefreshViewHandle ();
258- const std::string txtForParsing = m_pEditor->GetJsonText ();
336+ const auto selectedData = m_pEditor->GetJsonText ();
337+ const auto selectedText = IsSelectionValidJson (selectedData);
259338
260- if (txtForParsing .empty ())
339+ if (!selectedText. has_value () || selectedText. value () .empty ())
261340 {
262341 m_hTreeView->InsertNode (JSON_ERR_PARSE, NULL , rootNode);
342+
343+ if (IsMultiSelection (selectedData))
344+ {
345+ ShowMessage (JSON_INFO_TITLE, JSON_ERR_MULTI_SELECTION, MB_OK | MB_ICONINFORMATION);
346+ }
263347 }
264348 else
265349 {
266- auto res = PopulateTreeUsingSax (rootNode, txtForParsing );
350+ auto res = PopulateTreeUsingSax (rootNode, selectedText. value () );
267351 if (res.has_value ())
268352 {
269353 // This is the case when Notepad++ has JsonViewer Window opened for previous instance
0 commit comments