Skip to content

Commit 5fbba37

Browse files
author
Kapil Ratnani
committed
added new command to format JSON
1 parent 32c43ca commit 5fbba37

File tree

2 files changed

+57
-10
lines changed

2 files changed

+57
-10
lines changed

NppJSONViewer/PluginDefinition.cpp

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,21 @@ void commandMenuInit()
6060
// ShortcutKey *shortcut, // optional. Define a shortcut to trigger this command
6161
// bool check0nInit // optional. Make this menu item be checked visually
6262
// );
63-
ShortcutKey *sk=new ShortcutKey();
64-
sk->_isAlt=TRUE;
65-
sk->_isCtrl=TRUE;
66-
sk->_isShift=TRUE;
67-
sk->_key=0x4A;
68-
setCommand(0, TEXT("Show &JSON Viewer"), openJSONDialog,sk , false);
69-
setCommand(1, TEXT("&About"), openAboutDlg,NULL , false);
63+
64+
ShortcutKey *openJSONsk=new ShortcutKey();
65+
openJSONsk->_isAlt=TRUE;
66+
openJSONsk->_isCtrl=TRUE;
67+
openJSONsk->_isShift=TRUE;
68+
openJSONsk->_key='J';
69+
70+
ShortcutKey *formatJSONsk=new ShortcutKey();
71+
formatJSONsk->_isAlt=TRUE;
72+
formatJSONsk->_isCtrl=TRUE;
73+
formatJSONsk->_isShift=TRUE;
74+
formatJSONsk->_key='M';
75+
setCommand(0, TEXT("Show &JSON Viewer"), openJSONDialog,openJSONsk , false);
76+
setCommand(1, TEXT("&Format JSON"), formatSelectedJSON,formatJSONsk , false);
77+
setCommand(2, TEXT("&About"), openAboutDlg,NULL , false);
7078
}
7179

7280
INT_PTR CALLBACK abtDlgProc(HWND hwndDlg,UINT uMsg,WPARAM wParam, LPARAM lParam)
@@ -103,6 +111,7 @@ void commandMenuCleanUp()
103111
{
104112
// Don't forget to deallocate your shortcut here
105113
delete funcItem[0]._pShKey;
114+
delete funcItem[1]._pShKey;
106115
}
107116

108117

@@ -171,15 +180,53 @@ void openJSONDialog()
171180
}
172181

173182
size_t asciiTextLen = end - start;
183+
/*
174184
if (asciiTextLen == 0)
175185
{
176-
MessageBox(nppData._nppHandle,TEXT("Please select a JSON string."),TEXT("JSON Viewer"),MB_OK|MB_ICONINFORMATION);
186+
//MessageBox(nppData._nppHandle,TEXT("Please select a JSON string."),TEXT("JSON Viewer"),MB_OK|MB_ICONINFORMATION);
177187
return;
178188
}
189+
*/
190+
179191

180192
curJSON = new CHAR[asciiTextLen+1];
181193

182194
::SendMessage(curScintilla, SCI_GETSELTEXT, 0, (LPARAM)curJSON);
183195
showJSONDialog(curJSON);
184196
delete curJSON;
185197
}
198+
199+
void formatSelectedJSON(){
200+
// Get the current scintilla
201+
int which = -1;
202+
::SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)&which);
203+
if (which == -1)
204+
return;
205+
206+
HWND curScintilla = (which == 0)?nppData._scintillaMainHandle:nppData._scintillaSecondHandle;
207+
size_t start = ::SendMessage(curScintilla, SCI_GETSELECTIONSTART, 0, 0);
208+
size_t end = ::SendMessage(curScintilla, SCI_GETSELECTIONEND, 0, 0);
209+
if (end < start)
210+
{
211+
size_t tmp = start;
212+
start = end;
213+
end = tmp;
214+
}
215+
216+
size_t asciiTextLen = end - start;
217+
218+
if (asciiTextLen == 0)
219+
{
220+
//MessageBox(nppData._nppHandle,TEXT("Please select a JSON string."),TEXT("JSON Viewer"),MB_OK|MB_ICONINFORMATION);
221+
return;
222+
}
223+
224+
curJSON = new CHAR[asciiTextLen+1];
225+
226+
::SendMessage(curScintilla, SCI_GETSELTEXT, 0, (LPARAM)curJSON);
227+
CHAR* fJson=json_format_string(curJSON);
228+
::SendMessage(curScintilla,SCI_REPLACESEL,0,(LPARAM)fJson);
229+
230+
free(fJson);
231+
delete curJSON;
232+
}

NppJSONViewer/PluginDefinition.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const TCHAR NPP_PLUGIN_NAME[] = TEXT("JSON Viewer");
3333
//
3434
// Here define the number of your plugin commands
3535
//
36-
const int nbFunc = 2;
36+
const int nbFunc = 3;
3737

3838

3939
//
@@ -69,7 +69,7 @@ bool setCommand(size_t index, TCHAR *cmdName, PFUNCPLUGINCMD pFunc, ShortcutKey
6969
//
7070
void openJSONDialog();
7171
void openAboutDlg();
72-
72+
void formatSelectedJSON();
7373
#define CUSTOM_MARK_ERROR (WM_USER + 1)
7474

7575
#endif //PLUGINDEFINITION_H

0 commit comments

Comments
 (0)