@@ -73,10 +73,19 @@ void JsonViewDlg::ShowDlg(bool bShow)
73
73
74
74
void JsonViewDlg::FormatJson ()
75
75
{
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
+
77
86
auto [le, lf, indentChar, indentLen] = GetFormatSetting ();
78
87
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);
80
89
81
90
if (res.success )
82
91
{
@@ -85,7 +94,7 @@ void JsonViewDlg::FormatJson()
85
94
}
86
95
else
87
96
{
88
- if (CheckForTokenUndefined (JsonViewDlg::eMethod::FormatJson, selectedText, res, NULL ))
97
+ if (CheckForTokenUndefined (JsonViewDlg::eMethod::FormatJson, selectedText. value () , res, NULL ))
89
98
return ;
90
99
91
100
ReportError (res);
@@ -94,10 +103,17 @@ void JsonViewDlg::FormatJson()
94
103
95
104
void JsonViewDlg::CompressJson ()
96
105
{
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 );
99
108
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 ());
101
117
102
118
if (res.success )
103
119
{
@@ -106,7 +122,7 @@ void JsonViewDlg::CompressJson()
106
122
}
107
123
else
108
124
{
109
- if (CheckForTokenUndefined (JsonViewDlg::eMethod::GetCompressedJson, selectedText, res, NULL ))
125
+ if (CheckForTokenUndefined (JsonViewDlg::eMethod::GetCompressedJson, selectedText. value () , res, NULL ))
110
126
return ;
111
127
112
128
ReportError (res);
@@ -115,10 +131,19 @@ void JsonViewDlg::CompressJson()
115
131
116
132
void JsonViewDlg::SortJsonByKey ()
117
133
{
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
+
119
144
auto [le, lf, indentChar, indentLen] = GetFormatSetting ();
120
145
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);
122
147
123
148
if (res.success )
124
149
{
@@ -127,7 +152,7 @@ void JsonViewDlg::SortJsonByKey()
127
152
}
128
153
else
129
154
{
130
- if (CheckForTokenUndefined (JsonViewDlg::eMethod::SortJsonByKey, selectedText, res, NULL ))
155
+ if (CheckForTokenUndefined (JsonViewDlg::eMethod::SortJsonByKey, selectedText. value () , res, NULL ))
131
156
return ;
132
157
133
158
ReportError (res);
@@ -199,6 +224,52 @@ bool JsonViewDlg::CheckForTokenUndefined(eMethod method, std::string selectedTex
199
224
return false ;
200
225
}
201
226
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
+
202
273
void JsonViewDlg::HandleTabActivated ()
203
274
{
204
275
const bool bIsVisible = isCreated () && isVisible ();
@@ -223,18 +294,25 @@ void JsonViewDlg::HandleTabActivated()
223
294
224
295
void JsonViewDlg::ValidateJson ()
225
296
{
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
+ }
228
306
229
- Result res = JsonHandler (m_pSetting->parseOptions ).ValidateJson (selectedText);
307
+ Result res = JsonHandler (m_pSetting->parseOptions ).ValidateJson (selectedText. value () );
230
308
231
309
if (res.success )
232
310
{
233
311
ShowMessage (JSON_INFO_TITLE, JSON_ERR_VALIDATE_SUCCESS, MB_OK | MB_ICONINFORMATION);
234
312
}
235
313
else
236
314
{
237
- if (CheckForTokenUndefined (JsonViewDlg::eMethod::ValidateJson, selectedText, res, NULL ))
315
+ if (CheckForTokenUndefined (JsonViewDlg::eMethod::ValidateJson, selectedText. value () , res, NULL ))
238
316
{
239
317
ShowMessage (JSON_INFO_TITLE, JSON_ERR_VALIDATE_SUCCESS, MB_OK | MB_ICONINFORMATION);
240
318
return ;
@@ -255,15 +333,21 @@ void JsonViewDlg::DrawJsonTree()
255
333
256
334
// Refresh the view
257
335
m_pEditor->RefreshViewHandle ();
258
- const std::string txtForParsing = m_pEditor->GetJsonText ();
336
+ const auto selectedData = m_pEditor->GetJsonText ();
337
+ const auto selectedText = IsSelectionValidJson (selectedData);
259
338
260
- if (txtForParsing .empty ())
339
+ if (!selectedText. has_value () || selectedText. value () .empty ())
261
340
{
262
341
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
+ }
263
347
}
264
348
else
265
349
{
266
- auto res = PopulateTreeUsingSax (rootNode, txtForParsing );
350
+ auto res = PopulateTreeUsingSax (rootNode, selectedText. value () );
267
351
if (res.has_value ())
268
352
{
269
353
// This is the case when Notepad++ has JsonViewer Window opened for previous instance
0 commit comments