Skip to content

Commit 94d263d

Browse files
authored
Merge pull request #69 from neoarc/master
added "Compress selected JSON" command
2 parents 68da4a4 + 6725f6d commit 94d263d

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

NppJSONViewer/PluginDefinition.cpp

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

8592
void 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+
}

NppJSONViewer/PluginDefinition.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const TCHAR NPP_PLUGIN_NAME[] = TEXT("JSON Viewer");
3636
//
3737
// Here define the number of your plugin commands
3838
//
39-
const int nbFunc = 3;
39+
const int nbFunc = 4;
4040

4141

4242
//
@@ -73,6 +73,7 @@ bool setCommand(size_t index, TCHAR *cmdName, PFUNCPLUGINCMD pFunc, ShortcutKey
7373
void openJSONDialog();
7474
void openAboutDlg();
7575
void formatSelectedJSON();
76+
void compressSelectedJSON();
7677
#define CUSTOM_MARK_ERROR (WM_USER + 1)
7778

7879
#endif //PLUGINDEFINITION_H

0 commit comments

Comments
 (0)