Skip to content

Commit d602ab4

Browse files
committed
Minor code enhancement
1 parent 66bfef1 commit d602ab4

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

NppJSONViewer/NppJsonViewer/JsonViewDlg.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,11 @@ void JsonViewDlg::DrawJsonTree()
156156
}
157157
else
158158
{
159-
PopulateTreeUsingSax(rootNode, txtForParsing);
159+
auto res = PopulateTreeUsingSax(rootNode, txtForParsing);
160+
if (res.has_value())
161+
{
162+
ShowMessage(JSON_ERROR_TITLE, res.value(), MB_OK | MB_ICONERROR);
163+
}
160164
}
161165

162166
m_hTreeView->Expand(rootNode);
@@ -172,8 +176,10 @@ void JsonViewDlg::HightlightAsJson(bool bForcefully) const
172176
m_Editor->SetLangAsJson();
173177
}
174178

175-
void JsonViewDlg::PopulateTreeUsingSax(HTREEITEM tree_root, const std::string &jsonText)
179+
auto JsonViewDlg::PopulateTreeUsingSax(HTREEITEM tree_root, const std::string &jsonText) -> std::optional<std::wstring>
176180
{
181+
std::optional<std::wstring> retVal = std::nullopt;
182+
177183
RapidJsonHandler handler(this, tree_root);
178184
rapidjson::StringBuffer sb;
179185

@@ -183,7 +189,7 @@ void JsonViewDlg::PopulateTreeUsingSax(HTREEITEM tree_root, const std::string &j
183189
// Intimate user
184190
if (jsonText.empty())
185191
{
186-
ShowMessage(JSON_ERROR_TITLE, JSON_ERR_PARSE, MB_OK | MB_ICONERROR);
192+
retVal = std::make_optional<std::wstring>(JSON_ERR_PARSE);
187193
}
188194
else
189195
{
@@ -193,13 +199,15 @@ void JsonViewDlg::PopulateTreeUsingSax(HTREEITEM tree_root, const std::string &j
193199
m_Editor->MakeSelection(errPosition, errPosition + 1);
194200

195201
std::string err = std::format("\n\nError: ({} : {})", res.error_code, res.error_str);
196-
ShowMessage(JSON_ERROR_TITLE, (JSON_ERR_VALIDATE + StringHelper::ToWstring(err)).c_str(), MB_OK | MB_ICONERROR);
202+
retVal = std::make_optional<std::wstring>((JSON_ERR_VALIDATE + StringHelper::ToWstring(err)));
197203
}
198204
}
199205
else
200206
{
201207
HightlightAsJson();
202208
}
209+
210+
return retVal;
203211
}
204212

205213
HTREEITEM JsonViewDlg::InsertToTree(HTREEITEM parent, const std::string &text)

NppJSONViewer/NppJsonViewer/JsonViewDlg.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <string>
99
#include <memory>
1010
#include <vector>
11+
#include <optional>
1112

1213
class JsonViewDlg : public DockingDlgInterface
1314
{
@@ -34,7 +35,7 @@ class JsonViewDlg : public DockingDlgInterface
3435
private:
3536
void DrawJsonTree();
3637
void HightlightAsJson(bool bForcefully = false) const;
37-
void PopulateTreeUsingSax(HTREEITEM tree_root, const std::string &jsonText);
38+
auto PopulateTreeUsingSax(HTREEITEM tree_root, const std::string &jsonText) -> std::optional<std::wstring>;
3839

3940
void ValidateJson();
4041

0 commit comments

Comments
 (0)