Skip to content

Commit 747668f

Browse files
authored
Control formatting options via setting dialog (#139)
* Prepare for setting in Json Viewer * Fix some ui issue * Control format setting via setting dialog
1 parent 029553d commit 747668f

21 files changed

+589
-80
lines changed

NppJSONViewer/NppJsonViewer/AboutDlg.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22
#include "StaticDialog.h"
33

4-
class AboutDlg :public StaticDialog
4+
class AboutDlg : public StaticDialog
55
{
66
public:
77
AboutDlg(HINSTANCE hIntance, HWND hParent, int nCmdId);

NppJSONViewer/NppJsonViewer/Define.h

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22
#include "PluginInterface.h"
33

44
// Define the number of plugin commands here
5-
enum class CallBackID :int { SHOW_DOC_PANEL = 0, FORMAT, COMPRESS, SEP_1, /*OPTION,*/ ABOUT };
6-
const int nTotalCommandCount = static_cast<int>(CallBackID::ABOUT) + 1;
5+
enum class CallBackID :int { SHOW_DOC_PANEL = 0, FORMAT, COMPRESS, SEP_1, SETTING, ABOUT };
6+
constexpr const int nTotalCommandCount = static_cast<int>(CallBackID::ABOUT) + 1;
77

88
// Define plugin name here
99
const TCHAR PLUGIN_NAME[] = TEXT("JSON Viewer");
10+
const TCHAR PLUGIN_CONFIG[] = TEXT("JSONViewer.ini");
1011

1112
// Text which can be considered for localization
1213
const TCHAR TITLE_JSON_PANEL[] = TEXT("JSON Viewer Panel");
1314
const TCHAR MENU_SHOW_JSON_PANEL[] = TEXT("Show &JSON Viewer");
1415
const TCHAR MENU_FORMAT_JSON[] = TEXT("&Format JSON");
1516
const TCHAR MENU_COMPRESS_JSON[] = TEXT("&Compress JSON");
16-
const TCHAR MENU_OPTION[] = TEXT("&Option");
17+
const TCHAR MENU_SETTING[] = TEXT("&Setting");
1718
const TCHAR MENU_ABOUT[] = TEXT("&About");
1819
const TCHAR MENU_SEPERATOR[] = TEXT("-SEPARATOR-");
1920

@@ -38,9 +39,32 @@ const TCHAR JSON_ERR_VALIDATE_SUCCESS[] = TEXT("JSON looks good. No error found
3839

3940
const TCHAR STR_VERSION[] = TEXT("Version: ");
4041
const TCHAR STR_COPY[] = TEXT("Copy");
41-
const TCHAR STR_COPYNAME[] = TEXT("Copy name");
42+
const TCHAR STR_COPYNAME[] = TEXT("Copy name");
4243
const TCHAR STR_COPYVALUE[] = TEXT("Copy value");
4344
const TCHAR STR_COPYPATH[] = TEXT("Copy path");
4445
const TCHAR STR_EXPANDALL[] = TEXT("Expand all");
4546
const TCHAR STR_COLLAPSEALL[] = TEXT("Collapse all");
4647

48+
const TCHAR STR_INI_FORMATTING_SEC[] = TEXT("FORMATTING");
49+
const TCHAR STR_INI_FORMATTING_EOL[] = TEXT("EOL");
50+
const TCHAR STR_INI_FORMATTING_LINE[] = TEXT("LINE_FORMATTING");
51+
const TCHAR STR_INI_FORMATTING_INDENT[] = TEXT("INDENTATION");
52+
const TCHAR STR_INI_FORMATTING_INDENTCOUNT[] = TEXT("INDENTATION_COUNT");
53+
54+
enum class IndentStyle { AUTO, TAB, SPACE };
55+
struct Indent
56+
{
57+
unsigned len = 0;
58+
IndentStyle style = IndentStyle::AUTO;
59+
};
60+
61+
enum class LineEnding { AUTO, WINDOWS, UNIX, MAC };
62+
enum class LineFormat { DEFAULT, SINGLELINE };
63+
64+
struct Setting
65+
{
66+
LineEnding le = LineEnding::AUTO;
67+
LineFormat lf = LineFormat::DEFAULT;
68+
Indent indent{};
69+
};
70+

NppJSONViewer/NppJsonViewer/JsonHandler.cpp

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "JsonHandler.h"
22

33
#include "rapidjson/reader.h"
4-
#include "rapidjson/prettywriter.h"
54
#include "rapidjson/stringbuffer.h"
65
#include "rapidjson/document.h"
76
#include "rapidjson/error/en.h"
@@ -11,7 +10,7 @@ auto JsonHandler::GetCompressedJson(const std::string& jsonText)-> const Result
1110
return ParseJson(jsonText);
1211
}
1312

14-
auto JsonHandler::FormatJson(const std::string& jsonText, unsigned eol, unsigned indentLen, char indentChar)-> const Result
13+
auto JsonHandler::FormatJson(const std::string& jsonText, LE le, LF lf, char indentChar, unsigned indentLen)-> const Result
1514
{
1615
Result retVal{};
1716

@@ -20,19 +19,8 @@ auto JsonHandler::FormatJson(const std::string& jsonText, unsigned eol, unsigned
2019
rapidjson::StringStream ss(jsonText.c_str());
2120
rapidjson::Reader reader;
2221

23-
switch (eol)
24-
{
25-
case 0:
26-
pw.SetLineEnding(rapidjson::LineEndingOption::kCrLf);
27-
break;
28-
case 1:
29-
pw.SetLineEnding(rapidjson::LineEndingOption::kCr);
30-
break;
31-
default:
32-
pw.SetLineEnding(rapidjson::LineEndingOption::kLf);
33-
break;
34-
}
35-
22+
pw.SetLineEnding(le);
23+
pw.SetFormatOptions(lf);
3624
pw.SetIndent(indentChar, indentLen);
3725

3826
if (reader.Parse<rapidjson::kParseFullPrecisionFlag | rapidjson::kParseCommentsFlag |

NppJSONViewer/NppJsonViewer/JsonHandler.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22

33
#include <string>
4-
4+
#include "rapidjson/prettywriter.h"
55

66
struct Result
77
{
@@ -12,14 +12,17 @@ struct Result
1212
std::string response;
1313
};
1414

15+
using LE = rapidjson::LineEndingOption;
16+
using LF = rapidjson::PrettyFormatOptions;
17+
1518
class JsonHandler
1619
{
1720
public:
1821
JsonHandler() = default;
1922
~JsonHandler() = default;
2023

2124
auto GetCompressedJson(const std::string& jsonText)->const Result;
22-
auto FormatJson(const std::string& jsonText, unsigned eol, unsigned indentLen, char indentChar)->const Result;
25+
auto FormatJson(const std::string& jsonText, LE le, LF lf, char indentChar, unsigned indentLen)->const Result;
2326
auto ValidateJson(const std::string& jsonText)->const Result;
2427

2528
private:

NppJSONViewer/NppJsonViewer/JsonViewDlg.cpp

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@
44
#include "StringHelper.h"
55
#include "RapidJsonHandler.h"
66
#include "ScintillaEditor.h"
7-
#include "JsonHandler.h"
7+
#include "Profile.h"
88
#include <format>
99

1010

11-
JsonViewDlg::JsonViewDlg(HINSTANCE hIntance, const NppData& nppData, int nCmdId) :
12-
m_NppData(nppData),
13-
DockingDlgInterface(IDD_TREEDLG),
14-
m_nDlgId(nCmdId),
15-
m_Editor(std::make_unique<ScintillaEditor>(nppData)),
16-
m_hTreeView(std::make_unique<TreeViewCtrl>())
11+
JsonViewDlg::JsonViewDlg(HINSTANCE hIntance, const NppData& nppData, int nCmdId, const std::wstring& path)
12+
: m_NppData(nppData)
13+
, DockingDlgInterface(IDD_TREEDLG)
14+
, m_nDlgId(nCmdId)
15+
, m_configPath(path)
16+
, m_Editor(std::make_unique<ScintillaEditor>(nppData))
17+
, m_hTreeView(std::make_unique<TreeViewCtrl>())
1718
{
1819
_hParent = nppData._nppHandle;
1920
_hInst = hIntance;
@@ -62,12 +63,10 @@ void JsonViewDlg::ShowDlg(bool bShow)
6263

6364
void JsonViewDlg::FormatJson()
6465
{
65-
// Get the current scintilla
66-
const auto eol = m_Editor->GetEOL();
67-
const auto indent = m_Editor->GetIndent();
6866
const auto selectedText = m_Editor->GetJsonText();
67+
const auto [le, lf, indentChar, indentLen] = GetFormatSetting();
6968

70-
Result res = JsonHandler().FormatJson(selectedText, eol, indent.len, indent.ch);
69+
Result res = JsonHandler().FormatJson(selectedText, le, lf, indentChar, indentLen);
7170

7271
if (res.success)
7372
{
@@ -523,6 +522,50 @@ void JsonViewDlg::HandleTreeEvents(LPARAM lParam)
523522
}
524523
}
525524

525+
auto JsonViewDlg::GetFormatSetting() const -> std::tuple<LE, LF, char, unsigned>
526+
{
527+
// Default scintilla setting
528+
const auto eol = m_Editor->GetEOL();
529+
auto [indentChar, indentLen] = m_Editor->GetIndent();
530+
531+
LE le = LE::kCrLf;
532+
LF lf = LF::kFormatDefault;
533+
534+
switch (eol)
535+
{
536+
case 0: le = LE::kCrLf; break;
537+
case 1: le = LE::kCr; break;
538+
default: le = LE::kLf; break;
539+
}
540+
541+
542+
std::unique_ptr<Setting> pInfo = std::make_unique<Setting>();
543+
if (ProfileSetting(m_configPath).GetSettings(*pInfo))
544+
{
545+
lf = static_cast<LF>(pInfo->lf);
546+
547+
switch (pInfo->le)
548+
{
549+
case LineEnding::WINDOWS: le = LE::kCrLf; break;
550+
case LineEnding::UNIX: le = LE::kLf; break;
551+
case LineEnding::MAC: le = LE::kCr; break;
552+
case LineEnding::AUTO: break; // Takes from Notepad++
553+
default: break; // Takes from Notepad++
554+
}
555+
556+
557+
switch (pInfo->indent.style)
558+
{
559+
case IndentStyle::TAB: indentChar = '\t'; indentLen = 1; break;
560+
case IndentStyle::SPACE: indentChar = ' '; indentLen = pInfo->indent.len; break;
561+
case IndentStyle::AUTO: break; // Takes from Notepad++
562+
default: break; // Takes from Notepad++
563+
}
564+
}
565+
566+
return std::tuple<LE, LF, char, unsigned>(le, lf, indentChar, indentLen);
567+
}
568+
526569
INT_PTR JsonViewDlg::run_dlgProc(UINT message, WPARAM wParam, LPARAM lParam)
527570
{
528571
switch (message)

NppJSONViewer/NppJsonViewer/JsonViewDlg.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "resource.h"
55
#include "TreeViewCtrl.h"
66
#include "ScintillaEditor.h"
7+
#include "JsonHandler.h"
78
#include <string>
89
#include <memory>
910
#include <vector>
@@ -12,7 +13,7 @@ class JsonViewDlg : public DockingDlgInterface
1213
{
1314
enum class eButton { eRefresh, eValidate, eFormat, eSearch };
1415
public:
15-
JsonViewDlg(HINSTANCE hIntance, const NppData& nppData, int nCmdId);
16+
JsonViewDlg(HINSTANCE hIntance, const NppData& nppData, int nCmdId, const std::wstring& path);
1617
virtual ~JsonViewDlg();
1718

1819
void ShowDlg(bool bShow);
@@ -55,6 +56,8 @@ class JsonViewDlg : public DockingDlgInterface
5556

5657
void HandleTreeEvents(LPARAM lParam);
5758

59+
auto GetFormatSetting() const->std::tuple<LE, LF, char, unsigned>;
60+
5861
protected:
5962
virtual INT_PTR CALLBACK run_dlgProc(UINT message, WPARAM wParam, LPARAM lParam);
6063

@@ -71,6 +74,7 @@ class JsonViewDlg : public DockingDlgInterface
7174
RECT m_rcInitialWindowRect = {};
7275

7376
bool m_isSilent = false;
77+
std::wstring m_configPath;
7478

7579
std::unique_ptr<ScintillaEditor> m_Editor = nullptr;
7680
std::unique_ptr <TreeViewCtrl> m_hTreeView = nullptr;

NppJSONViewer/NppJsonViewer/NPPJSONViewer.vcxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,10 @@
258258
<ClCompile Include="JsonHandler.cpp" />
259259
<ClCompile Include="JsonViewDlg.cpp" />
260260
<ClCompile Include="NppJsonPlugin.cpp" />
261+
<ClCompile Include="Profile.cpp" />
261262
<ClCompile Include="RapidJsonHandler.cpp" />
262263
<ClCompile Include="ScintillaEditor.cpp" />
264+
<ClCompile Include="SettingsDlg.cpp" />
263265
<ClCompile Include="ShortcutCommand.cpp" />
264266
<ClCompile Include="TreeViewCtrl.cpp" />
265267
</ItemGroup>
@@ -279,9 +281,11 @@
279281
<ClInclude Include="JsonNode.h" />
280282
<ClInclude Include="JsonViewDlg.h" />
281283
<ClInclude Include="NppJsonPlugin.h" />
284+
<ClInclude Include="Profile.h" />
282285
<ClInclude Include="RapidJsonHandler.h" />
283286
<ClInclude Include="resource.h" />
284287
<ClInclude Include="ScintillaEditor.h" />
288+
<ClInclude Include="SettingsDlg.h" />
285289
<ClInclude Include="ShortcutCommand.h" />
286290
<ClInclude Include="StopWatch.h" />
287291
<ClInclude Include="TreeViewCtrl.h" />

NppJSONViewer/NppJsonViewer/NPPJSONViewer.vcxproj.filters

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@
5151
<ClCompile Include="RapidJsonHandler.cpp">
5252
<Filter>Source Files</Filter>
5353
</ClCompile>
54+
<ClCompile Include="SettingsDlg.cpp">
55+
<Filter>Source Files</Filter>
56+
</ClCompile>
57+
<ClCompile Include="Profile.cpp">
58+
<Filter>Source Files</Filter>
59+
</ClCompile>
5460
</ItemGroup>
5561
<ItemGroup>
5662
<ClInclude Include="resource.h">
@@ -116,6 +122,12 @@
116122
<ClInclude Include="RapidJsonHandler.h">
117123
<Filter>Header Files</Filter>
118124
</ClInclude>
125+
<ClInclude Include="SettingsDlg.h">
126+
<Filter>Header Files</Filter>
127+
</ClInclude>
128+
<ClInclude Include="Profile.h">
129+
<Filter>Header Files</Filter>
130+
</ClInclude>
119131
</ItemGroup>
120132
<ItemGroup>
121133
<ResourceCompile Include="resource.rc">

0 commit comments

Comments
 (0)