Skip to content

Commit e1a26ac

Browse files
committed
Remove duplicate code
1 parent 8059a99 commit e1a26ac

File tree

2 files changed

+23
-33
lines changed

2 files changed

+23
-33
lines changed

NppJSONViewer/NppJsonViewer/JsonViewDlg.cpp

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,7 @@ void JsonViewDlg::FormatJson()
7676
}
7777
else
7878
{
79-
// Mark the error position
80-
size_t start = m_Editor->GetSelectionStart() + res.error_pos;
81-
size_t end = start + m_Editor->GetSelectionEnd();
82-
m_Editor->MakeSelection(start, end);
83-
84-
// Intimate user
85-
std::string err = std::format("\n\nError: ({} : {})", res.error_code, res.error_str);
86-
87-
ShowMessage(JSON_ERROR_TITLE, (JSON_ERR_VALIDATE + StringHelper::ToWstring(err)).c_str(), MB_OK | MB_ICONERROR);
79+
ReportError(res);
8880
}
8981
}
9082

@@ -101,15 +93,7 @@ void JsonViewDlg::CompressJson()
10193
}
10294
else
10395
{
104-
// Mark the error position
105-
size_t start = m_Editor->GetSelectionStart() + res.error_pos;
106-
size_t end = start + m_Editor->GetSelectionEnd();
107-
m_Editor->MakeSelection(start, end);
108-
109-
// Intimate user
110-
std::string err = std::format("\n\nError: ({} : {})", res.error_code, res.error_str);
111-
112-
ShowMessage(JSON_ERROR_TITLE, (JSON_ERR_VALIDATE + StringHelper::ToWstring(err)).c_str(), MB_OK | MB_ICONERROR);
96+
ReportError(res);
11397
}
11498
}
11599

@@ -143,15 +127,7 @@ void JsonViewDlg::ValidateJson()
143127
}
144128
else
145129
{
146-
// Mark the error position
147-
size_t start = m_Editor->GetSelectionStart() + res.error_pos;
148-
size_t end = start + m_Editor->GetSelectionEnd();
149-
m_Editor->MakeSelection(start, end);
150-
151-
// Intimate user
152-
std::string err = std::format("\n\nError: ({} : {})", res.error_code, res.error_str);
153-
154-
ShowMessage(JSON_ERROR_TITLE, (JSON_ERR_VALIDATE + StringHelper::ToWstring(err)).c_str(), MB_OK | MB_ICONERROR);
130+
ReportError(res);
155131
}
156132
}
157133

@@ -189,18 +165,18 @@ void JsonViewDlg::PopulateTreeUsingSax(HTREEITEM tree_root, const std::string &j
189165
Result res = JsonHandler(m_pSetting->parseOptions).ParseJson<flgBaseReader>(jsonText, sb, handler);
190166
if (!res.success)
191167
{
192-
// Mark the error position
193-
size_t start = m_Editor->GetSelectionStart();
194-
size_t errPosition = start + static_cast<size_t>(res.error_pos);
195-
m_Editor->MakeSelection(errPosition, errPosition + 1);
196-
197168
// Intimate user
198169
if (jsonText.empty())
199170
{
200171
ShowMessage(JSON_ERROR_TITLE, JSON_ERR_PARSE, MB_OK | MB_ICONERROR);
201172
}
202173
else
203174
{
175+
// Mark the error position
176+
size_t start = m_Editor->GetSelectionStart();
177+
size_t errPosition = start + static_cast<size_t>(res.error_pos);
178+
m_Editor->MakeSelection(errPosition, errPosition + 1);
179+
204180
std::string err = std::format("\n\nError: ({} : {})", res.error_code, res.error_str);
205181
ShowMessage(JSON_ERROR_TITLE, (JSON_ERR_VALIDATE + StringHelper::ToWstring(err)).c_str(), MB_OK | MB_ICONERROR);
206182
}
@@ -587,6 +563,19 @@ int JsonViewDlg::ShowMessage(const std::wstring &title, const std::wstring &msg,
587563
return !bDontShow ? ::MessageBox(_hParent, msg.c_str(), title.c_str(), flag) : IDOK;
588564
}
589565

566+
void JsonViewDlg::ReportError(const Result &result)
567+
{
568+
// Mark the error position
569+
size_t start = m_Editor->GetSelectionStart() + result.error_pos;
570+
size_t end = start + m_Editor->GetSelectionEnd();
571+
m_Editor->MakeSelection(start, end);
572+
573+
// Intimate user
574+
std::string err = std::format("\n\nError: ({} : {})", result.error_code, result.error_str);
575+
576+
ShowMessage(JSON_ERROR_TITLE, (JSON_ERR_VALIDATE + StringHelper::ToWstring(err)).c_str(), MB_OK | MB_ICONERROR);
577+
}
578+
590579
void JsonViewDlg::ToggleMenuItemState(bool bVisible)
591580
{
592581
::SendMessage(_hParent, NPPM_SETMENUITEMCHECK, static_cast<WPARAM>(m_nDlgId), bVisible);

NppJSONViewer/NppJsonViewer/JsonViewDlg.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ class JsonViewDlg : public DockingDlgInterface
5656
auto CopyValue() const -> std::wstring;
5757
auto CopyPath() const -> std::wstring;
5858

59-
int ShowMessage(const std::wstring &title, const std::wstring &msg, int flag, bool bDontShow = false);
59+
int ShowMessage(const std::wstring &title, const std::wstring &msg, int flag, bool bDontShow = false);
60+
void ReportError(const Result& result);
6061

6162
void ToggleMenuItemState(bool bVisible);
6263

0 commit comments

Comments
 (0)