Skip to content

Commit 890dce6

Browse files
committed
Added some more otions in setting
1 parent a516e03 commit 890dce6

File tree

9 files changed

+79
-44
lines changed

9 files changed

+79
-44
lines changed

NppJSONViewer/NppJsonViewer/Define.h

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,23 +55,31 @@ const TCHAR STR_INI_FORMATTING_INDENTCOUNT[] = TEXT("INDENTATION_COUNT");
5555
const TCHAR STR_INI_OTHER_SEC[] = TEXT("Others");
5656
const TCHAR STR_INI_OTHER_FOLLOW_TAB[] = TEXT("FOLLOW_TAB");
5757
const TCHAR STR_INI_OTHER_AUTO_FORMAT[] = TEXT("AUTO_FORMAT");
58+
const TCHAR STR_INI_OTHER_IGNORE_COMMENT[] = TEXT("IGNORE_COMMENT");
59+
const TCHAR STR_INI_OTHER_IGNORE_COMMA[] = TEXT("IGNORE_TRAILLING_COMMA");
5860

61+
enum class LineEnding { AUTO, WINDOWS, UNIX, MAC };
62+
enum class LineFormat { DEFAULT, SINGLELINE };
5963
enum class IndentStyle { AUTO, TAB, SPACE };
6064
struct Indent
6165
{
62-
unsigned len = 0;
66+
unsigned len = 4;
6367
IndentStyle style = IndentStyle::AUTO;
6468
};
6569

66-
enum class LineEnding { AUTO, WINDOWS, UNIX, MAC };
67-
enum class LineFormat { DEFAULT, SINGLELINE };
70+
struct ParseOptions
71+
{
72+
bool bIgnoreComment = true;
73+
bool bIgnoreTraillingComma = true;
74+
};
6875

6976
struct Setting
7077
{
71-
LineEnding le = LineEnding::AUTO;
72-
LineFormat lf = LineFormat::DEFAULT;
78+
LineEnding lineEnding = LineEnding::AUTO;
79+
LineFormat lineFormat = LineFormat::DEFAULT;
7380
Indent indent{};
74-
bool follow_current_tab = false;
75-
bool auto_format_on_open = false;
81+
bool bFollowCurrentTab = false;
82+
bool bAutoFormat = false;
83+
ParseOptions parseOptions{};
7684
};
7785

NppJSONViewer/NppJsonViewer/JsonHandler.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
#include "rapidjson/document.h"
66
#include "rapidjson/error/en.h"
77

8+
JsonHandler::JsonHandler(const ParseOptions& options)
9+
: m_parseOptions(options)
10+
{
11+
}
12+
813
auto JsonHandler::GetCompressedJson(const std::string& jsonText)-> const Result
914
{
1015
return ParseJson(jsonText);

NppJSONViewer/NppJsonViewer/JsonHandler.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <string>
44
#include "rapidjson/prettywriter.h"
5+
#include "Define.h"
56

67
struct Result
78
{
@@ -17,8 +18,9 @@ using LF = rapidjson::PrettyFormatOptions;
1718

1819
class JsonHandler
1920
{
21+
ParseOptions m_parseOptions{};
2022
public:
21-
JsonHandler() = default;
23+
JsonHandler(const ParseOptions& options);
2224
~JsonHandler() = default;
2325

2426
auto GetCompressedJson(const std::string& jsonText)->const Result;

NppJSONViewer/NppJsonViewer/JsonViewDlg.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void JsonViewDlg::FormatJson()
6767
const auto selectedText = m_Editor->GetJsonText();
6868
const auto [le, lf, indentChar, indentLen] = GetFormatSetting();
6969

70-
Result res = JsonHandler().FormatJson(selectedText, le, lf, indentChar, indentLen);
70+
Result res = JsonHandler(m_pSetting->parseOptions).FormatJson(selectedText, le, lf, indentChar, indentLen);
7171

7272
if (res.success)
7373
{
@@ -93,7 +93,7 @@ void JsonViewDlg::CompressJson()
9393
// Get the current scintilla
9494
const auto selectedText = m_Editor->GetJsonText();
9595

96-
Result res = JsonHandler().GetCompressedJson(selectedText);
96+
Result res = JsonHandler(m_pSetting->parseOptions).GetCompressedJson(selectedText);
9797

9898
if (res.success)
9999
{
@@ -118,12 +118,12 @@ void JsonViewDlg::HandleTabActivated()
118118
const bool bIsVisible = isCreated() && isVisible();
119119
if (bIsVisible && m_Editor->IsJsonFile())
120120
{
121-
if (m_pSetting->follow_current_tab)
121+
if (m_pSetting->bFollowCurrentTab)
122122
{
123123
DrawJsonTree();
124124
}
125125

126-
if (m_pSetting->auto_format_on_open)
126+
if (m_pSetting->bAutoFormat)
127127
{
128128
FormatJson();
129129
}
@@ -135,7 +135,7 @@ void JsonViewDlg::ValidateJson()
135135
// Get the current scintilla
136136
const auto selectedText = m_Editor->GetJsonText();
137137

138-
Result res = JsonHandler().ValidateJson(selectedText);
138+
Result res = JsonHandler(m_pSetting->parseOptions).ValidateJson(selectedText);
139139

140140
if (res.success)
141141
{
@@ -535,10 +535,10 @@ auto JsonViewDlg::GetFormatSetting() const -> std::tuple<LE, LF, char, unsigned>
535535
unsigned indentLen = 0;
536536

537537
// Line formatting options
538-
lf = static_cast<LF>(m_pSetting->lf);
538+
lf = static_cast<LF>(m_pSetting->lineFormat);
539539

540540
// End of line options
541-
switch (m_pSetting->le)
541+
switch (m_pSetting->lineEnding)
542542
{
543543
case LineEnding::WINDOWS:
544544
le = LE::kCrLf;

NppJSONViewer/NppJsonViewer/Profile.cpp

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@ Profile::Profile(const std::wstring& path)
1212
Init();
1313
}
1414

15-
bool Profile::ReadValue(const std::wstring& section, const std::wstring& key, int& retVal) const
15+
bool Profile::ReadValue(const std::wstring& section, const std::wstring& key, int& retVal, int defaultVal) const
1616
{
17-
retVal = GetPrivateProfileInt(section.c_str(), key.c_str(), 0, m_ProfileFilePath.c_str());
17+
retVal = GetPrivateProfileInt(section.c_str(), key.c_str(), defaultVal, m_ProfileFilePath.c_str());
1818

1919
return true;
2020
}
2121

22-
bool Profile::ReadValue(const std::wstring& section, const std::wstring& key, std::wstring& retVal) const
22+
bool Profile::ReadValue(const std::wstring& section, const std::wstring& key, std::wstring& retVal, const std::wstring& defaultVal) const
2323
{
2424
bool bRetVal = false;
2525

2626
// Try with MAX_PATH
2727
constexpr DWORD nBufSize = MAX_PATH * 2;
2828
auto pData = std::make_unique<TCHAR[]>(nBufSize);
29-
GetPrivateProfileString(section.c_str(), key.c_str(), nullptr, pData.get(), nBufSize, m_ProfileFilePath.c_str());
29+
GetPrivateProfileString(section.c_str(), key.c_str(), defaultVal.c_str(), pData.get(), nBufSize, m_ProfileFilePath.c_str());
3030

3131
if (pData)
3232
{
@@ -76,27 +76,35 @@ bool ProfileSetting::GetSettings(Setting& info) const
7676
int nVal = 0;
7777
bRetVal &= ReadValue(STR_INI_FORMATTING_SEC, STR_INI_FORMATTING_EOL, nVal);
7878
if (bRetVal)
79-
info.le = static_cast<LineEnding>(nVal);
79+
info.lineEnding = static_cast<LineEnding>(nVal);
8080

8181
bRetVal &= ReadValue(STR_INI_FORMATTING_SEC, STR_INI_FORMATTING_LINE, nVal);
8282
if (bRetVal)
83-
info.lf = static_cast<LineFormat>(nVal);
83+
info.lineFormat = static_cast<LineFormat>(nVal);
8484

8585
bRetVal &= ReadValue(STR_INI_FORMATTING_SEC, STR_INI_FORMATTING_INDENT, nVal);
8686
if (bRetVal)
8787
info.indent.style = static_cast<IndentStyle>(nVal);
8888

89-
bRetVal &= ReadValue(STR_INI_FORMATTING_SEC, STR_INI_FORMATTING_INDENTCOUNT, nVal);
89+
bRetVal &= ReadValue(STR_INI_FORMATTING_SEC, STR_INI_FORMATTING_INDENTCOUNT, nVal, info.indent.len);
9090
if (bRetVal)
9191
info.indent.len = nVal;
9292

9393
bRetVal &= ReadValue(STR_INI_OTHER_SEC, STR_INI_OTHER_FOLLOW_TAB, nVal);
9494
if (bRetVal)
95-
info.follow_current_tab = static_cast<bool>(nVal);
95+
info.bFollowCurrentTab = static_cast<bool>(nVal);
9696

9797
bRetVal &= ReadValue(STR_INI_OTHER_SEC, STR_INI_OTHER_AUTO_FORMAT, nVal);
9898
if (bRetVal)
99-
info.auto_format_on_open = static_cast<bool>(nVal);
99+
info.bAutoFormat = static_cast<bool>(nVal);
100+
101+
bRetVal &= ReadValue(STR_INI_OTHER_SEC, STR_INI_OTHER_IGNORE_COMMENT, nVal, info.parseOptions.bIgnoreComment);
102+
if (bRetVal)
103+
info.parseOptions.bIgnoreComment = static_cast<bool>(nVal);
104+
105+
bRetVal &= ReadValue(STR_INI_OTHER_SEC, STR_INI_OTHER_IGNORE_COMMA, nVal, info.parseOptions.bIgnoreTraillingComma);
106+
if (bRetVal)
107+
info.parseOptions.bIgnoreTraillingComma = static_cast<bool>(nVal);
100108

101109
return bRetVal;
102110
}
@@ -105,12 +113,15 @@ bool ProfileSetting::SetSettings(const Setting& info) const
105113
{
106114
bool bRetVal = true;
107115

108-
bRetVal &= WriteValue(STR_INI_FORMATTING_SEC, STR_INI_FORMATTING_EOL, static_cast<int>(info.le));
109-
bRetVal &= WriteValue(STR_INI_FORMATTING_SEC, STR_INI_FORMATTING_LINE, static_cast<int>(info.lf));
116+
bRetVal &= WriteValue(STR_INI_FORMATTING_SEC, STR_INI_FORMATTING_EOL, static_cast<int>(info.lineEnding));
117+
bRetVal &= WriteValue(STR_INI_FORMATTING_SEC, STR_INI_FORMATTING_LINE, static_cast<int>(info.lineFormat));
110118
bRetVal &= WriteValue(STR_INI_FORMATTING_SEC, STR_INI_FORMATTING_INDENT, static_cast<int>(info.indent.style));
111119
bRetVal &= WriteValue(STR_INI_FORMATTING_SEC, STR_INI_FORMATTING_INDENTCOUNT, info.indent.len);
112-
bRetVal &= WriteValue(STR_INI_OTHER_SEC, STR_INI_OTHER_FOLLOW_TAB, info.follow_current_tab);
113-
bRetVal &= WriteValue(STR_INI_OTHER_SEC, STR_INI_OTHER_AUTO_FORMAT, info.auto_format_on_open);
120+
121+
bRetVal &= WriteValue(STR_INI_OTHER_SEC, STR_INI_OTHER_FOLLOW_TAB, info.bFollowCurrentTab);
122+
bRetVal &= WriteValue(STR_INI_OTHER_SEC, STR_INI_OTHER_AUTO_FORMAT, info.bAutoFormat);
123+
bRetVal &= WriteValue(STR_INI_OTHER_SEC, STR_INI_OTHER_IGNORE_COMMENT, info.parseOptions.bIgnoreComment);
124+
bRetVal &= WriteValue(STR_INI_OTHER_SEC, STR_INI_OTHER_IGNORE_COMMA, info.parseOptions.bIgnoreTraillingComma);
114125

115126
return bRetVal;
116127
}

NppJSONViewer/NppJsonViewer/Profile.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ class Profile
1111
virtual ~Profile() = default;
1212

1313
protected:
14-
bool ReadValue(const std::wstring& section, const std::wstring& key, int& retVal) const;
15-
bool ReadValue(const std::wstring& section, const std::wstring& key, std::wstring& retVal) const;
14+
bool ReadValue(const std::wstring& section, const std::wstring& key, int& retVal, int defaultVal = 0) const;
15+
bool ReadValue(const std::wstring& section, const std::wstring& key, std::wstring& retVal, const std::wstring& defaultVal = {}) const;
1616

1717
bool WriteValue(const std::wstring& section, const std::wstring& key, int value) const;
1818
bool WriteValue(const std::wstring& section, const std::wstring& key, const std::wstring& value) const;

NppJSONViewer/NppJsonViewer/SettingsDlg.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,13 @@ bool SettingsDlg::Apply()
9595

9696
// Line Ending setting
9797
if (IsDlgButtonChecked(_hSelf, IDC_RADIO_LINE_AUTO))
98-
m_pSetting->le = LineEnding::AUTO;
98+
m_pSetting->lineEnding = LineEnding::AUTO;
9999
else if (IsDlgButtonChecked(_hSelf, IDC_RADIO_LINE_WINDOW))
100-
m_pSetting->le = LineEnding::WINDOWS;
100+
m_pSetting->lineEnding = LineEnding::WINDOWS;
101101
else if (IsDlgButtonChecked(_hSelf, IDC_RADIO_LINE_UNIX))
102-
m_pSetting->le = LineEnding::UNIX;
102+
m_pSetting->lineEnding = LineEnding::UNIX;
103103
else if (IsDlgButtonChecked(_hSelf, IDC_RADIO_LINE_MAC))
104-
m_pSetting->le = LineEnding::MAC;
104+
m_pSetting->lineEnding = LineEnding::MAC;
105105

106106
// Indentation setting
107107
if (IsDlgButtonChecked(_hSelf, IDC_RADIO_INDENT_AUTO))
@@ -119,12 +119,14 @@ bool SettingsDlg::Apply()
119119

120120
// Line Ending setting
121121
if (IsDlgButtonChecked(_hSelf, IDC_RADIO_LINEFORMAT_DEFAULT))
122-
m_pSetting->lf = LineFormat::DEFAULT;
122+
m_pSetting->lineFormat = LineFormat::DEFAULT;
123123
else if (IsDlgButtonChecked(_hSelf, IDC_RADIO_LINEFORMAT_SINGLE))
124-
m_pSetting->lf = LineFormat::SINGLELINE;
124+
m_pSetting->lineFormat = LineFormat::SINGLELINE;
125125

126-
m_pSetting->follow_current_tab = CUtility::GetCheckboxStatus(::GetDlgItem(_hSelf, IDC_CHK_FOLLOW_CURRENT_DOC));
127-
m_pSetting->auto_format_on_open = CUtility::GetCheckboxStatus(::GetDlgItem(_hSelf, IDC_CHK_FORMAT_ON_OPEN));
126+
m_pSetting->bFollowCurrentTab = CUtility::GetCheckboxStatus(::GetDlgItem(_hSelf, IDC_CHK_FOLLOW_CURRENT_DOC));
127+
m_pSetting->bAutoFormat = CUtility::GetCheckboxStatus(::GetDlgItem(_hSelf, IDC_CHK_FORMAT_ON_OPEN));
128+
m_pSetting->parseOptions.bIgnoreTraillingComma = CUtility::GetCheckboxStatus(::GetDlgItem(_hSelf, IDC_CHK_IGNORE_COMMA));
129+
m_pSetting->parseOptions.bIgnoreComment = CUtility::GetCheckboxStatus(::GetDlgItem(_hSelf, IDC_CHK_IGNORE_COMMENT));
128130

129131
return WriteINI();
130132
}
@@ -150,7 +152,7 @@ void SettingsDlg::InitDlg()
150152
// Set UI control state here
151153

152154
int nCtrlID = IDC_RADIO_LINE_AUTO;
153-
switch (m_pSetting->le)
155+
switch (m_pSetting->lineEnding)
154156
{
155157
case LineEnding::WINDOWS: nCtrlID = IDC_RADIO_LINE_WINDOW; break;
156158
case LineEnding::UNIX: nCtrlID = IDC_RADIO_LINE_UNIX; break;
@@ -162,7 +164,7 @@ void SettingsDlg::InitDlg()
162164

163165

164166
nCtrlID = IDC_RADIO_LINEFORMAT_DEFAULT;
165-
switch (m_pSetting->lf)
167+
switch (m_pSetting->lineFormat)
166168
{
167169
case LineFormat::SINGLELINE: nCtrlID = IDC_RADIO_LINEFORMAT_SINGLE; break;
168170
case LineFormat::DEFAULT: nCtrlID = IDC_RADIO_LINEFORMAT_DEFAULT; break;
@@ -182,8 +184,10 @@ void SettingsDlg::InitDlg()
182184
CUtility::SetEditCtrlText(::GetDlgItem(_hSelf, IDC_EDT_INDENT_SPACECOUNT), std::to_wstring(m_pSetting->indent.len));
183185
ShowSpaceCountCtrls(m_pSetting->indent.style == IndentStyle::SPACE);
184186

185-
CUtility::SetCheckboxStatus(::GetDlgItem(_hSelf, IDC_CHK_FOLLOW_CURRENT_DOC), m_pSetting->follow_current_tab);
186-
CUtility::SetCheckboxStatus(::GetDlgItem(_hSelf, IDC_CHK_FORMAT_ON_OPEN), m_pSetting->auto_format_on_open);
187+
CUtility::SetCheckboxStatus(::GetDlgItem(_hSelf, IDC_CHK_FOLLOW_CURRENT_DOC), m_pSetting->bFollowCurrentTab);
188+
CUtility::SetCheckboxStatus(::GetDlgItem(_hSelf, IDC_CHK_FORMAT_ON_OPEN), m_pSetting->bAutoFormat);
189+
CUtility::SetCheckboxStatus(::GetDlgItem(_hSelf, IDC_CHK_IGNORE_COMMA), m_pSetting->parseOptions.bIgnoreTraillingComma);
190+
CUtility::SetCheckboxStatus(::GetDlgItem(_hSelf, IDC_CHK_IGNORE_COMMENT), m_pSetting->parseOptions.bIgnoreComment);
187191
}
188192

189193
void SettingsDlg::ShowSpaceCountCtrls(bool bShow)

NppJSONViewer/NppJsonViewer/resource.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
#define IDC_RADIO_LINEFORMAT_SINGLE 1026
3939
#define IDC_CHK_FOLLOW_CURRENT_DOC 1028
4040
#define IDC_CHK_FORMAT_ON_OPEN 1029
41+
#define IDC_CHK_IGNORE_COMMA 1030
42+
#define IDC_CHK_IGNORE_COMMENT 1031
4143
#define IDM_COPY_TREEITEM 40001
4244
#define IDM_COPY_NODENAME 40002
4345
#define IDM_COPY_NODEVALUE 40003
@@ -51,7 +53,7 @@
5153
#ifndef APSTUDIO_READONLY_SYMBOLS
5254
#define _APS_NEXT_RESOURCE_VALUE 110
5355
#define _APS_NEXT_COMMAND_VALUE 40007
54-
#define _APS_NEXT_CONTROL_VALUE 1030
56+
#define _APS_NEXT_CONTROL_VALUE 1032
5557
#define _APS_NEXT_SYMED_VALUE 101
5658
#endif
5759
#endif

NppJSONViewer/NppJsonViewer/resource.rc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,12 @@ CAPTION "JSON Viewer Settings"
9797
FONT 8, "MS Shell Dlg", 400, 0, 0x1
9898
BEGIN
9999
PUSHBUTTON "",IDC_HOR_BAR_TOP,0,1,300,1,WS_DISABLED,WS_EX_STATICEDGE
100-
CONTROL "Follow current tab if it is json file",IDC_CHK_FOLLOW_CURRENT_DOC,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,5,7,140,10
100+
CONTROL "Follow current tab if it is json file",IDC_CHK_FOLLOW_CURRENT_DOC,
101+
"Button",BS_AUTOCHECKBOX | WS_TABSTOP,5,7,140,10
101102
CONTROL "Auto format josn file when openend",IDC_CHK_FORMAT_ON_OPEN,
102103
"Button",BS_AUTOCHECKBOX | WS_TABSTOP,5,20,140,10
104+
CONTROL "Ignore tralling comma",IDC_CHK_IGNORE_COMMA,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,5,33,140,10
105+
CONTROL "Ignore comments in json",IDC_CHK_IGNORE_COMMENT,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,5,46,140,10
103106
GROUPBOX " Indentation: ",IDC_STATIC,153,7,140,43
104107
CONTROL "Auto detect",IDC_RADIO_INDENT_AUTO,"Button",BS_AUTORADIOBUTTON | WS_GROUP | WS_TABSTOP,160,21,50,10
105108
CONTROL "Use tab",IDC_RADIO_INDENT_TAB,"Button",BS_AUTORADIOBUTTON,240,21,50,10

0 commit comments

Comments
 (0)