@@ -77,9 +77,16 @@ void commandMenuInit()
7777 formatJSONsk->_isCtrl = TRUE ;
7878 formatJSONsk->_isShift = TRUE ;
7979 formatJSONsk->_key = ' M' ;
80+
81+ ShortcutKey* compressJSONsk = new ShortcutKey ();
82+ compressJSONsk->_isAlt = TRUE ;
83+ compressJSONsk->_isCtrl = TRUE ;
84+ compressJSONsk->_isShift = TRUE ;
85+ compressJSONsk->_key = ' C' ;
8086 setCommand (0 , TEXT (" Show &JSON Viewer" ), openJSONDialog, openJSONsk, false );
8187 setCommand (1 , TEXT (" &Format JSON" ), formatSelectedJSON, formatJSONsk, false );
82- setCommand (2 , TEXT (" &About" ), openAboutDlg, NULL , false );
88+ setCommand (2 , TEXT (" &Compress JSON" ), compressSelectedJSON, compressJSONsk, false );
89+ setCommand (3 , TEXT (" &About" ), openAboutDlg, NULL , false );
8390}
8491
8592void setVersion (HWND hwndDlg)
@@ -131,6 +138,7 @@ void commandMenuCleanUp()
131138 // Don't forget to deallocate your shortcut here
132139 delete funcItem[0 ]._pShKey ;
133140 delete funcItem[1 ]._pShKey ;
141+ delete funcItem[2 ]._pShKey ;
134142}
135143
136144
@@ -286,3 +294,52 @@ void formatSelectedJSON()
286294
287295 delete[] curJSON;
288296}
297+
298+ void compressSelectedJSON ()
299+ {
300+ // Get the current scintilla
301+ int which = -1 ;
302+ ::SendMessage (nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0 , (LPARAM)&which);
303+ if (which == -1 )
304+ return ;
305+
306+ HWND curScintilla = (which == 0 ) ? nppData._scintillaMainHandle : nppData._scintillaSecondHandle ;
307+ size_t start = ::SendMessage (curScintilla, SCI_GETSELECTIONSTART, 0 , 0 );
308+ size_t end = ::SendMessage (curScintilla, SCI_GETSELECTIONEND, 0 , 0 );
309+ if (end < start)
310+ {
311+ size_t tmp = start;
312+ start = end;
313+ end = tmp;
314+ }
315+
316+ size_t asciiTextLen = end - start;
317+ selectAllIfUnselectedAndSetCurJSON (asciiTextLen, curScintilla);
318+
319+ rapidjson::StringBuffer sb;
320+ rapidjson::Writer<rapidjson::StringBuffer> pw (sb);
321+ rapidjson::StringStream ss (curJSON);
322+ rapidjson::Reader reader;
323+
324+ if (reader.Parse <rapidjson::kParseFullPrecisionFlag >(ss, pw))
325+ {
326+ const char * fJson = sb.GetString ();
327+ ::SendMessage (curScintilla, SCI_REPLACESEL, 0 , (LPARAM)fJson);
328+ }
329+ else
330+ {
331+ // Mark the error position
332+ // Get the current scintilla
333+ start = ::SendMessage (curScintilla, SCI_GETSELECTIONSTART, 0 , 0 );
334+
335+ size_t errPosition = start + reader.GetErrorOffset ();
336+ ::SendMessage (curScintilla, SCI_SETSEL, errPosition, errPosition + end);
337+
338+ // Intimate user
339+ ::MessageBox (nppData._nppHandle,
340+ TEXT (" There was an error while parsing JSON, refer the current selection for possible problematic area." ),
341+ TEXT(" JSON Viewer" ), MB_OK | MB_ICONERROR);
342+ }
343+
344+ delete[] curJSON;
345+ }
0 commit comments