Skip to content

Commit a51adb2

Browse files
committed
get indentation settings from npp settings
1 parent cabe68e commit a51adb2

File tree

1 file changed

+45
-40
lines changed

1 file changed

+45
-40
lines changed

NppJSONViewer/PluginDefinition.cpp

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#include "rapidjson/document.h"
2626

2727
HANDLE g_hMod;
28-
CHAR * curJSON=NULL;
28+
CHAR * curJSON = NULL;
2929
JSONDialog jsonDialog;
3030
//
3131
// The plugin data that Notepad++ needs
@@ -42,8 +42,8 @@ NppData nppData;
4242
// It will be called while plugin loading
4343
void pluginInit(HANDLE hModule)
4444
{
45-
g_hMod=hModule;
46-
jsonDialog.init((HINSTANCE)g_hMod,nppData._nppHandle);
45+
g_hMod = hModule;
46+
jsonDialog.init((HINSTANCE)g_hMod, nppData._nppHandle);
4747
}
4848

4949
//
@@ -65,38 +65,38 @@ void commandMenuInit()
6565
// bool check0nInit // optional. Make this menu item be checked visually
6666
// );
6767

68-
ShortcutKey *openJSONsk=new ShortcutKey();
69-
openJSONsk->_isAlt=TRUE;
70-
openJSONsk->_isCtrl=TRUE;
71-
openJSONsk->_isShift=TRUE;
72-
openJSONsk->_key='J';
73-
74-
ShortcutKey *formatJSONsk=new ShortcutKey();
75-
formatJSONsk->_isAlt=TRUE;
76-
formatJSONsk->_isCtrl=TRUE;
77-
formatJSONsk->_isShift=TRUE;
78-
formatJSONsk->_key='M';
79-
setCommand(0, TEXT("Show &JSON Viewer"), openJSONDialog,openJSONsk , false);
80-
setCommand(1, TEXT("&Format JSON"), formatSelectedJSON,formatJSONsk , false);
81-
setCommand(2, TEXT("&About"), openAboutDlg,NULL , false);
68+
ShortcutKey *openJSONsk = new ShortcutKey();
69+
openJSONsk->_isAlt = TRUE;
70+
openJSONsk->_isCtrl = TRUE;
71+
openJSONsk->_isShift = TRUE;
72+
openJSONsk->_key = 'J';
73+
74+
ShortcutKey *formatJSONsk = new ShortcutKey();
75+
formatJSONsk->_isAlt = TRUE;
76+
formatJSONsk->_isCtrl = TRUE;
77+
formatJSONsk->_isShift = TRUE;
78+
formatJSONsk->_key = 'M';
79+
setCommand(0, TEXT("Show &JSON Viewer"), openJSONDialog, openJSONsk, false);
80+
setCommand(1, TEXT("&Format JSON"), formatSelectedJSON, formatJSONsk, false);
81+
setCommand(2, TEXT("&About"), openAboutDlg, NULL, false);
8282
}
8383

84-
INT_PTR CALLBACK abtDlgProc(HWND hwndDlg,UINT uMsg,WPARAM wParam, LPARAM /*lParam*/)
84+
INT_PTR CALLBACK abtDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM /*lParam*/)
8585
{
86-
switch(uMsg)
86+
switch (uMsg)
8787
{
8888
case WM_INITDIALOG:
89-
ConvertStaticToHyperlink(hwndDlg,IDC_WEB);
89+
ConvertStaticToHyperlink(hwndDlg, IDC_WEB);
9090
return TRUE;
9191
case WM_COMMAND:
92-
switch(LOWORD(wParam))
92+
switch (LOWORD(wParam))
9393
{
9494
case IDCANCEL: // Close this dialog when clicking to close button
9595
case IDOK:
96-
EndDialog(hwndDlg,wParam);
96+
EndDialog(hwndDlg, wParam);
9797
return TRUE;
9898
case IDC_WEB:
99-
ShellExecute(hwndDlg, TEXT("open"),TEXT("https://sourceforge.net/projects/nppjsonviewer/"),NULL, NULL, SW_SHOWNORMAL);
99+
ShellExecute(hwndDlg, TEXT("open"), TEXT("https://sourceforge.net/projects/nppjsonviewer/"), NULL, NULL, SW_SHOWNORMAL);
100100
return TRUE;
101101
}
102102
}
@@ -105,7 +105,7 @@ INT_PTR CALLBACK abtDlgProc(HWND hwndDlg,UINT uMsg,WPARAM wParam, LPARAM /*lPara
105105

106106
void openAboutDlg()
107107
{
108-
::CreateDialog((HINSTANCE)g_hMod,MAKEINTRESOURCE(IDD_ABOUTDLG),nppData._nppHandle,abtDlgProc);
108+
::CreateDialog((HINSTANCE)g_hMod, MAKEINTRESOURCE(IDD_ABOUTDLG), nppData._nppHandle, abtDlgProc);
109109
}
110110

111111

@@ -123,7 +123,7 @@ void commandMenuCleanUp()
123123
//
124124
// This function help you to initialize your plugin commands
125125
//
126-
bool setCommand(size_t index, TCHAR *cmdName, PFUNCPLUGINCMD pFunc, ShortcutKey *sk, bool check0nInit)
126+
bool setCommand(size_t index, TCHAR *cmdName, PFUNCPLUGINCMD pFunc, ShortcutKey *sk, bool check0nInit)
127127
{
128128
if (index >= nbFunc)
129129
return false;
@@ -147,7 +147,7 @@ void showJSONDialog(char *json)
147147
{
148148
jsonDialog.setParent(nppData._nppHandle);
149149
jsonDialog.setJSON(json);
150-
tTbData data = {0};
150+
tTbData data = { 0 };
151151

152152
if (!jsonDialog.isCreated())
153153
{
@@ -157,7 +157,7 @@ void showJSONDialog(char *json)
157157
data.uMask = DWS_DF_CONT_LEFT;
158158

159159
data.pszModuleName = jsonDialog.getPluginFileName();
160-
data.pszName=L"JSON Viewer";
160+
data.pszName = L"JSON Viewer";
161161

162162
// the dlgDlg should be the index of funcItem where the current function pointer is
163163
data.dlgID = 0;
@@ -171,9 +171,10 @@ void selectAllIfUnselectedAndSetCurJSON(size_t selectedTextLength, HWND curScint
171171
size_t allTextlength = ::SendMessage(curScintilla, SCI_GETLENGTH, 0, (LPARAM)curJSON);
172172
::SendMessage(curScintilla, SCI_SETSELECTIONSTART, 0, (LPARAM)curJSON);
173173
::SendMessage(curScintilla, SCI_SETSELECTIONEND, allTextlength, (LPARAM)curJSON);
174-
curJSON = new CHAR[allTextlength+1];
175-
} else {
176-
curJSON = new CHAR[selectedTextLength+1];
174+
curJSON = new CHAR[allTextlength + 1];
175+
}
176+
else {
177+
curJSON = new CHAR[selectedTextLength + 1];
177178
}
178179

179180
::SendMessage(curScintilla, SCI_GETSELTEXT, 0, (LPARAM)curJSON);
@@ -187,7 +188,7 @@ void openJSONDialog()
187188
if (which == -1)
188189
return;
189190

190-
HWND curScintilla = (which == 0)?nppData._scintillaMainHandle:nppData._scintillaSecondHandle;
191+
HWND curScintilla = (which == 0) ? nppData._scintillaMainHandle : nppData._scintillaSecondHandle;
191192
size_t start = ::SendMessage(curScintilla, SCI_GETSELECTIONSTART, 0, 0);
192193
size_t end = ::SendMessage(curScintilla, SCI_GETSELECTIONEND, 0, 0);
193194
if (end < start)
@@ -198,20 +199,20 @@ void openJSONDialog()
198199
}
199200

200201
size_t asciiTextLen = end - start;
201-
selectAllIfUnselectedAndSetCurJSON(asciiTextLen, curScintilla);
202-
202+
selectAllIfUnselectedAndSetCurJSON(asciiTextLen, curScintilla);
203+
203204
showJSONDialog(curJSON);
204-
delete [] curJSON;
205+
delete[] curJSON;
205206
}
206207

207-
void formatSelectedJSON(){
208+
void formatSelectedJSON() {
208209
// Get the current scintilla
209210
int which = -1;
210211
::SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)&which);
211212
if (which == -1)
212213
return;
213214

214-
HWND curScintilla = (which == 0)?nppData._scintillaMainHandle:nppData._scintillaSecondHandle;
215+
HWND curScintilla = (which == 0) ? nppData._scintillaMainHandle : nppData._scintillaSecondHandle;
215216
size_t start = ::SendMessage(curScintilla, SCI_GETSELECTIONSTART, 0, 0);
216217
size_t end = ::SendMessage(curScintilla, SCI_GETSELECTIONEND, 0, 0);
217218
if (end < start)
@@ -222,16 +223,20 @@ void formatSelectedJSON(){
222223
}
223224

224225
size_t asciiTextLen = end - start;
225-
selectAllIfUnselectedAndSetCurJSON(asciiTextLen, curScintilla);
226+
selectAllIfUnselectedAndSetCurJSON(asciiTextLen, curScintilla);
226227

227228
rapidjson::StringBuffer sb;
228229
rapidjson::PrettyWriter<rapidjson::StringBuffer> pw(sb);
230+
bool useTabs = ::SendMessage(curScintilla, SCI_GETUSETABS, 0, 0);
231+
int indentLen = useTabs ? 1 : ::SendMessage(curScintilla, SCI_GETTABWIDTH, 0, 0);
232+
char indentChar = useTabs ? '\t' : ' ';
233+
pw.SetIndent(indentChar, indentLen);
229234
rapidjson::StringStream ss(curJSON);
230235
rapidjson::Reader reader;
231236

232237
reader.Parse(ss, pw);
233238
const char* fJson = sb.GetString();
234-
::SendMessage(curScintilla,SCI_REPLACESEL,0,(LPARAM)fJson);
235-
236-
delete [] curJSON;
239+
::SendMessage(curScintilla, SCI_REPLACESEL, 0, (LPARAM)fJson);
240+
241+
delete[] curJSON;
237242
}

0 commit comments

Comments
 (0)