Skip to content

Commit 541706e

Browse files
committed
Added more features
1. Follow current document 2. Format json on open/changing tab #fixes #86 #closes #86
1 parent 8d8132f commit 541706e

File tree

10 files changed

+120
-46
lines changed

10 files changed

+120
-46
lines changed

NppJSONViewer/NppJsonViewer/JsonViewDlg.cpp

Lines changed: 66 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
#include <format>
99

1010

11-
JsonViewDlg::JsonViewDlg(HINSTANCE hIntance, const NppData& nppData, int nCmdId, const std::wstring& path)
11+
JsonViewDlg::JsonViewDlg(HINSTANCE hIntance, const NppData& nppData, int nCmdId, std::shared_ptr<Setting>& pSetting)
1212
: m_NppData(nppData)
1313
, DockingDlgInterface(IDD_TREEDLG)
1414
, m_nDlgId(nCmdId)
15-
, m_configPath(path)
1615
, m_Editor(std::make_unique<ScintillaEditor>(nppData))
1716
, m_hTreeView(std::make_unique<TreeViewCtrl>())
17+
, m_pSetting(pSetting)
1818
{
1919
_hParent = nppData._nppHandle;
2020
_hInst = hIntance;
@@ -112,6 +112,23 @@ void JsonViewDlg::CompressJson()
112112
}
113113
}
114114

115+
void JsonViewDlg::HandleTabActivated()
116+
{
117+
const bool bIsVisible = isCreated() && isVisible();
118+
if (bIsVisible && m_Editor->IsJsonFile())
119+
{
120+
if (m_pSetting->follow_current_tab)
121+
{
122+
DrawJsonTree();
123+
}
124+
125+
if (m_pSetting->auto_format_on_open)
126+
{
127+
FormatJson();
128+
}
129+
}
130+
}
131+
115132
void JsonViewDlg::ValidateJson()
116133
{
117134
// Get the current scintilla
@@ -468,7 +485,7 @@ auto JsonViewDlg::CopyPath() const -> std::wstring
468485

469486
int JsonViewDlg::ShowMessage(const std::wstring& title, const std::wstring& msg, int flag, bool bForceShow)
470487
{
471-
return (!m_isSilent || bForceShow) ? ::MessageBox(_hParent, msg.c_str(), title.c_str(), flag) : IDOK;
488+
return bForceShow ? ::MessageBox(_hParent, msg.c_str(), title.c_str(), flag) : IDOK;
472489
}
473490

474491
void JsonViewDlg::ToggleMenuItemState(bool bVisible)
@@ -511,43 +528,63 @@ void JsonViewDlg::HandleTreeEvents(LPARAM lParam)
511528

512529
auto JsonViewDlg::GetFormatSetting() const -> std::tuple<LE, LF, char, unsigned>
513530
{
514-
// Default scintilla setting
515-
const auto eol = m_Editor->GetEOL();
516-
auto [indentChar, indentLen] = m_Editor->GetIndent();
517-
518531
LE le = LE::kCrLf;
519532
LF lf = LF::kFormatDefault;
533+
char indentChar = ' ';
534+
unsigned indentLen = 0;
535+
536+
// Line formatting options
537+
lf = static_cast<LF>(m_pSetting->lf);
520538

521-
switch (eol)
539+
// End of line options
540+
switch (m_pSetting->le)
522541
{
523-
case 0: le = LE::kCrLf; break;
524-
case 1: le = LE::kCr; break;
525-
default: le = LE::kLf; break;
526-
}
542+
case LineEnding::WINDOWS:
543+
le = LE::kCrLf;
544+
break;
527545

546+
case LineEnding::UNIX:
547+
le = LE::kLf;
548+
break;
528549

529-
std::unique_ptr<Setting> pInfo = std::make_unique<Setting>();
530-
if (ProfileSetting(m_configPath).GetSettings(*pInfo))
531-
{
532-
lf = static_cast<LF>(pInfo->lf);
550+
case LineEnding::MAC:
551+
le = LE::kCr;
552+
break;
533553

534-
switch (pInfo->le)
554+
// Takes from Notepad++
555+
case LineEnding::AUTO:
556+
default:
557+
{
558+
const auto eol = m_Editor->GetEOL();
559+
switch (eol)
535560
{
536-
case LineEnding::WINDOWS: le = LE::kCrLf; break;
537-
case LineEnding::UNIX: le = LE::kLf; break;
538-
case LineEnding::MAC: le = LE::kCr; break;
539-
case LineEnding::AUTO: break; // Takes from Notepad++
540-
default: break; // Takes from Notepad++
561+
case 0: le = LE::kCrLf; break;
562+
case 1: le = LE::kCr; break;
563+
default: le = LE::kLf; break;
541564
}
565+
}
566+
}
542567

568+
// Indentation options
569+
switch (m_pSetting->indent.style)
570+
{
571+
case IndentStyle::TAB:
572+
indentChar = '\t';
573+
indentLen = 1;
574+
break;
543575

544-
switch (pInfo->indent.style)
545-
{
546-
case IndentStyle::TAB: indentChar = '\t'; indentLen = 1; break;
547-
case IndentStyle::SPACE: indentChar = ' '; indentLen = pInfo->indent.len; break;
548-
case IndentStyle::AUTO: break; // Takes from Notepad++
549-
default: break; // Takes from Notepad++
550-
}
576+
case IndentStyle::SPACE:
577+
indentChar = ' ';
578+
indentLen = m_pSetting->indent.len;
579+
break;
580+
581+
// Takes from Notepad++
582+
case IndentStyle::AUTO:
583+
default:
584+
const auto [c, l] = m_Editor->GetIndent();
585+
indentChar = c;
586+
indentLen = l;
587+
break;
551588
}
552589

553590
return std::tuple<LE, LF, char, unsigned>(le, lf, indentChar, indentLen);

NppJSONViewer/NppJsonViewer/JsonViewDlg.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ class JsonViewDlg : public DockingDlgInterface
1313
{
1414
enum class eButton { eRefresh, eValidate, eFormat, eSearch };
1515
public:
16-
JsonViewDlg(HINSTANCE hIntance, const NppData& nppData, int nCmdId, const std::wstring& path);
16+
JsonViewDlg(HINSTANCE hIntance, const NppData& nppData, int nCmdId, std::shared_ptr<Setting>& pSetting);
1717
virtual ~JsonViewDlg();
1818

1919
void ShowDlg(bool bShow);
2020
void FormatJson();
2121
void CompressJson();
22+
void HandleTabActivated();
2223

2324
HTREEITEM InsertToTree(HTREEITEM parent, const std::string& text);
2425

@@ -71,10 +72,8 @@ class JsonViewDlg : public DockingDlgInterface
7172
LONG m_lfInitialClientHeight = 0;
7273
RECT m_rcInitialWindowRect = {};
7374

74-
bool m_isSilent = false;
75-
std::wstring m_configPath;
76-
7775
std::unique_ptr<ScintillaEditor> m_Editor = nullptr;
7876
std::unique_ptr <TreeViewCtrl> m_hTreeView = nullptr;
77+
std::shared_ptr<Setting> m_pSetting = nullptr;
7978
};
8079

NppJSONViewer/NppJsonViewer/NppJsonPlugin.cpp

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "NppJsonPlugin.h"
22
#include "resource.h"
3+
#include "Profile.h"
34
#include <tchar.h>
45

56
NppJsonPlugin* NppJsonPlugin::Callback::m_pNppJsonPlugin = nullptr;
@@ -53,6 +54,20 @@ void NppJsonPlugin::ProcessNotification(const SCNotification* notifyCode)
5354
break;
5455
}
5556

57+
case NPPN_BUFFERACTIVATED:
58+
{
59+
if (m_pJsonViewDlg && !m_bAboutToClose)
60+
{
61+
m_pJsonViewDlg->HandleTabActivated();
62+
}
63+
break;
64+
}
65+
66+
case NPPN_BEFORESHUTDOWN:
67+
{
68+
m_bAboutToClose = true;
69+
}
70+
5671
default:
5772
return;
5873
}
@@ -119,18 +134,28 @@ void NppJsonPlugin::InitConfigPath()
119134
m_configPath = std::wstring(szPath) + TEXT("\\") + PLUGIN_CONFIG;
120135
}
121136

137+
void NppJsonPlugin::ToggleMenuItemState(int nCmdId, bool bVisible)
138+
{
139+
::SendMessage(m_NppData._nppHandle, NPPM_SETMENUITEMCHECK, static_cast<WPARAM>(nCmdId), bVisible);
140+
}
141+
122142
void NppJsonPlugin::ConstructJsonDlg()
123143
{
124144
if (!m_pJsonViewDlg)
125145
{
146+
ConstructSetting();
126147
auto nCmdId = m_shortcutCommands.GetCommandID(CallBackID::SHOW_DOC_PANEL);
127-
m_pJsonViewDlg = std::make_unique<JsonViewDlg>(reinterpret_cast<HINSTANCE>(m_hModule), m_NppData, nCmdId, m_configPath);
148+
m_pJsonViewDlg = std::make_unique<JsonViewDlg>(reinterpret_cast<HINSTANCE>(m_hModule), m_NppData, nCmdId, m_pSetting);
128149
}
129150
}
130151

131-
void NppJsonPlugin::ToggleMenuItemState(int nCmdId, bool bVisible)
152+
void NppJsonPlugin::ConstructSetting()
132153
{
133-
::SendMessage(m_NppData._nppHandle, NPPM_SETMENUITEMCHECK, static_cast<WPARAM>(nCmdId), bVisible);
154+
if (!m_pSetting)
155+
{
156+
m_pSetting = std::make_shared<Setting>();
157+
ProfileSetting(m_configPath).GetSettings(*m_pSetting);
158+
}
134159
}
135160

136161
void NppJsonPlugin::ShowJsonDlg()
@@ -166,10 +191,11 @@ void NppJsonPlugin::CompressJson()
166191

167192
void NppJsonPlugin::OpenSettingDlg()
168193
{
194+
ConstructSetting();
169195
auto nCmdId = m_shortcutCommands.GetCommandID(CallBackID::SETTING);
170196

171197
if (!m_pAboutDlg)
172-
m_pSettingsDlg = std::make_unique<SettingsDlg>(reinterpret_cast<HINSTANCE>(m_hModule), m_NppData._nppHandle, nCmdId, m_configPath);
198+
m_pSettingsDlg = std::make_unique<SettingsDlg>(reinterpret_cast<HINSTANCE>(m_hModule), m_NppData._nppHandle, nCmdId, m_configPath, m_pSetting);
173199
bool isShown = m_pSettingsDlg->ShowDlg(true);
174200

175201
ToggleMenuItemState(nCmdId, isShown);

NppJSONViewer/NppJsonViewer/NppJsonPlugin.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
#include <string>
44
#include <memory>
55
#include "Define.h"
6-
#include "JsonViewDlg.h"
76
#include "Notepad_plus_msgs.h"
87
#include "ShortcutCommand.h"
98
#include "AboutDlg.h"
9+
#include "JsonViewDlg.h"
1010
#include "SettingsDlg.h"
1111

1212

@@ -53,9 +53,11 @@ class NppJsonPlugin
5353
void InitToolbarIcon();
5454
void InitConfigPath();
5555

56-
void ConstructJsonDlg();
5756
void ToggleMenuItemState(int nCmdId, bool bVisible);
5857

58+
void ConstructJsonDlg();
59+
void ConstructSetting();
60+
5961
void ShowJsonDlg();
6062
void FormatJson();
6163
void CompressJson();
@@ -68,8 +70,10 @@ class NppJsonPlugin
6870
ShortcutCommand m_shortcutCommands;
6971
NppData m_NppData = {};
7072
std::wstring m_configPath;
71-
std::unique_ptr<JsonViewDlg> m_pJsonViewDlg = nullptr;
73+
bool m_bAboutToClose = false;
7274
std::unique_ptr<AboutDlg> m_pAboutDlg = nullptr;
75+
std::unique_ptr<JsonViewDlg> m_pJsonViewDlg = nullptr;
7376
std::unique_ptr<SettingsDlg> m_pSettingsDlg = nullptr;
77+
std::shared_ptr<Setting> m_pSetting = nullptr;
7478
};
7579

NppJSONViewer/NppJsonViewer/ScintillaEditor.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ void ScintillaEditor::SetLangAsJson() const
4242
::SendMessage(m_hScintilla, NPPM_SETCURRENTLANGTYPE, 0, LangType::L_JSON);
4343
}
4444

45+
bool ScintillaEditor::IsJsonFile() const
46+
{
47+
unsigned langType = 0;
48+
::SendMessage(m_NppData._nppHandle, NPPM_GETCURRENTLANGTYPE, 0, reinterpret_cast<LPARAM>(&langType));
49+
return langType == LangType::L_JSON;
50+
}
51+
4552
void ScintillaEditor::ReplaceSelection(const std::string& text) const
4653
{
4754
::SendMessage(m_hScintilla, SCI_REPLACESEL, 0, reinterpret_cast<LPARAM>(text.c_str()));

NppJSONViewer/NppJsonViewer/ScintillaEditor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class ScintillaEditor
1111

1212
std::string GetJsonText();
1313
void SetLangAsJson() const;
14+
bool IsJsonFile() const;
1415

1516
void ReplaceSelection(const std::string& text) const;
1617

NppJSONViewer/NppJsonViewer/SettingsDlg.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
#include <Uxtheme.h>
77

88

9-
SettingsDlg::SettingsDlg(HINSTANCE hIntance, HWND hParent, int nCmdId, const std::wstring& configPath)
9+
SettingsDlg::SettingsDlg(HINSTANCE hIntance, HWND hParent, int nCmdId, const std::wstring& configPath, std::shared_ptr<Setting>& pSetting)
1010
: m_nCmdId(nCmdId)
1111
, m_configPath(configPath)
1212
, StaticDialog()
13-
, m_pSetting(std::make_unique<Setting>())
13+
, m_pSetting(pSetting)
1414
{
1515
init(hIntance, hParent);
1616
}

NppJSONViewer/NppJsonViewer/SettingsDlg.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
class SettingsDlg : public StaticDialog
88
{
99
public:
10-
SettingsDlg(HINSTANCE hIntance, HWND hParent, int nCmdId, const std::wstring& configPath);
10+
SettingsDlg(HINSTANCE hIntance, HWND hParent, int nCmdId, const std::wstring& configPath, std::shared_ptr<Setting>& pSetting);
1111
~SettingsDlg() = default;
1212

1313
bool ShowDlg(bool bShow);
@@ -26,6 +26,6 @@ class SettingsDlg : public StaticDialog
2626
private:
2727
int m_nCmdId = -1;
2828
std::wstring m_configPath;
29-
std::unique_ptr<Setting> m_pSetting = nullptr;
29+
std::shared_ptr<Setting> m_pSetting = nullptr;
3030
};
3131

NppJSONViewer/NppJsonViewer/resource.rc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ 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",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,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,5,7,140,10
101101
CONTROL "Auto format josn file when openend",IDC_CHK_FORMAT_ON_OPEN,
102102
"Button",BS_AUTOCHECKBOX | WS_TABSTOP,5,20,140,10
103103
GROUPBOX " Indentation: ",IDC_STATIC,153,7,140,43

NppJSONViewer/external/npp/StaticDialog.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public :
6060
};
6161

6262
protected :
63-
RECT _rc;
63+
RECT _rc{};
6464
static INT_PTR CALLBACK dlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
6565
virtual INT_PTR CALLBACK run_dlgProc(UINT message, WPARAM wParam, LPARAM lParam) = 0;
6666

0 commit comments

Comments
 (0)