Skip to content

Commit 3d85bd1

Browse files
committed
Excplicit callout for no support on multiline selection
#fixes #197 #closes #197,
1 parent 98dae7d commit 3d85bd1

File tree

5 files changed

+130
-24
lines changed

5 files changed

+130
-24
lines changed

src/NppJsonViewer/Define.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ const TCHAR JSON_ERROR_TITLE[] = TEXT("JSON Viewer: Error");
4242
const TCHAR JSON_WARNING_TITLE[] = TEXT("JSON Viewer: Warning");
4343
const TCHAR JSON_INFO_TITLE[] = TEXT("JSON Viewer: Information");
4444

45-
const TCHAR JSON_ERR_PARSE[] = TEXT("Cannot parse JSON. Please select a JSON string.");
46-
const TCHAR JSON_ERR_VALIDATE[] = TEXT("There was an error while parsing JSON. Refer to the current selection for possible problematic area.");
47-
const TCHAR JSON_ERR_VALIDATE_SUCCESS[] = TEXT("JSON looks good. No errors found while validating it.");
48-
const TCHAR JSON_ERR_SAVE_SETTING[] = TEXT("Failed to save the settings. Please try again.");
45+
const TCHAR JSON_ERR_PARSE[] = TEXT("Unable to parse JSON. Please ensure a valid JSON string is selected.");
46+
const TCHAR JSON_ERR_VALIDATE[] = TEXT("An error occurred while parsing the JSON. Check the current selection for the potential issue.");
47+
const TCHAR JSON_ERR_VALIDATE_SUCCESS[] = TEXT("The JSON appears valid. No errors were found during validation.");
48+
const TCHAR JSON_ERR_SAVE_SETTING[] = TEXT("Could not save the settings. Please try again.");
49+
const TCHAR JSON_ERR_MULTI_SELECTION[] = TEXT("Multiline selection is not currently supported in Json Viewer.");
4950

5051
const TCHAR STR_VERSION[] = TEXT("Version: ");
5152
const TCHAR STR_COPY[] = TEXT("Copy");

src/NppJsonViewer/JsonViewDlg.cpp

Lines changed: 101 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,19 @@ void JsonViewDlg::ShowDlg(bool bShow)
7373

7474
void 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

95104
void 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

116132
void 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+
202273
void JsonViewDlg::HandleTabActivated()
203274
{
204275
const bool bIsVisible = isCreated() && isVisible();
@@ -223,18 +294,25 @@ void JsonViewDlg::HandleTabActivated()
223294

224295
void 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

src/NppJsonViewer/JsonViewDlg.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ class JsonViewDlg : public DockingDlgInterface
8484

8585
bool CheckForTokenUndefined(eMethod method, std::string selectedText, Result &res, HTREEITEM tree_root);
8686

87+
bool IsMultiSelection(const ScintillaData &scintillaData) const;
88+
auto IsSelectionValidJson(const ScintillaData &scintillaData) const -> std::optional<std::string>;
89+
void ProcessScintillaData(const ScintillaData &scintillaData, std::string &text, ScintillaCode &code) const;
90+
8791
protected:
8892
virtual INT_PTR CALLBACK run_dlgProc(UINT message, WPARAM wParam, LPARAM lParam) override;
8993

src/NppJsonViewer/ScintillaEditor.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@ void ScintillaEditor::RefreshViewHandle()
1717
m_hScintilla = (which == 0) ? m_NppData._scintillaMainHandle : m_NppData._scintillaSecondHandle;
1818
}
1919

20-
auto ScintillaEditor::GetJsonText() -> std::string
20+
auto ScintillaEditor::GetJsonText() -> ScintillaData
2121
{
2222
if (!m_hScintilla)
23-
return std::string();
23+
return ScintillaCode::NotInitialized;
24+
25+
// Multi selection is not supported
26+
size_t nSelections = ::SendMessage(m_hScintilla, SCI_GETSELECTIONS, 0, 0);
27+
if (nSelections > 1)
28+
return ScintillaCode::MultiLineSelection;
2429

2530
// Adjust the selection position
2631
RefreshSelectionPos();

src/NppJsonViewer/ScintillaEditor.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22
#include "Define.h"
33
#include <string>
44
#include <tuple>
5+
#include <variant>
6+
7+
enum class ScintillaCode : short
8+
{
9+
Unknown,
10+
Success,
11+
NotInitialized,
12+
NoSelection,
13+
MultiLineSelection
14+
};
15+
16+
using ScintillaData = std::variant<std::string, ScintillaCode>;
517

618
class ScintillaEditor
719
{
@@ -10,7 +22,7 @@ class ScintillaEditor
1022
~ScintillaEditor() = default;
1123

1224
void RefreshViewHandle();
13-
auto GetJsonText() -> std::string;
25+
auto GetJsonText() -> ScintillaData;
1426
void SetLangAsJson() const;
1527
bool IsJsonFile() const;
1628
auto GetCurrentFileName() const -> std::wstring;

0 commit comments

Comments
 (0)