@@ -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
7280INT_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+ }
0 commit comments