Skip to content

Commit ac2943a

Browse files
committed
added "Stringify selected JSON" command
1 parent 6ad6ea3 commit ac2943a

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* stringifyJSONsk = new ShortcutKey();
82+
stringifyJSONsk->_isAlt = TRUE;
83+
stringifyJSONsk->_isCtrl = TRUE;
84+
stringifyJSONsk->_isShift = TRUE;
85+
stringifyJSONsk->_key = 'S';
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("&Stringify JSON"), stringifySelectedJSON, stringifyJSONsk, 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

@@ -271,3 +279,52 @@ void formatSelectedJSON()
271279

272280
delete[] curJSON;
273281
}
282+
283+
void stringifySelectedJSON()
284+
{
285+
// Get the current scintilla
286+
int which = -1;
287+
::SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)&which);
288+
if (which == -1)
289+
return;
290+
291+
HWND curScintilla = (which == 0) ? nppData._scintillaMainHandle : nppData._scintillaSecondHandle;
292+
size_t start = ::SendMessage(curScintilla, SCI_GETSELECTIONSTART, 0, 0);
293+
size_t end = ::SendMessage(curScintilla, SCI_GETSELECTIONEND, 0, 0);
294+
if (end < start)
295+
{
296+
size_t tmp = start;
297+
start = end;
298+
end = tmp;
299+
}
300+
301+
size_t asciiTextLen = end - start;
302+
selectAllIfUnselectedAndSetCurJSON(asciiTextLen, curScintilla);
303+
304+
rapidjson::StringBuffer sb;
305+
rapidjson::Writer<rapidjson::StringBuffer> pw(sb);
306+
rapidjson::StringStream ss(curJSON);
307+
rapidjson::Reader reader;
308+
309+
if (reader.Parse<rapidjson::kParseFullPrecisionFlag >(ss, pw))
310+
{
311+
const char* fJson = sb.GetString();
312+
::SendMessage(curScintilla, SCI_REPLACESEL, 0, (LPARAM)fJson);
313+
}
314+
else
315+
{
316+
// Mark the error position
317+
// Get the current scintilla
318+
start = ::SendMessage(curScintilla, SCI_GETSELECTIONSTART, 0, 0);
319+
320+
size_t errPosition = start + reader.GetErrorOffset();
321+
::SendMessage(curScintilla, SCI_SETSEL, errPosition, errPosition + end);
322+
323+
// Intimate user
324+
::MessageBox(nppData._nppHandle,
325+
TEXT("There was an error while parsing JSON, refer the current selection for possible problematic area."),
326+
TEXT("JSON Viewer"), MB_OK | MB_ICONERROR);
327+
}
328+
329+
delete[] curJSON;
330+
}

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 stringifySelectedJSON();
7677
#define CUSTOM_MARK_ERROR (WM_USER + 1)
7778

7879
#endif //PLUGINDEFINITION_H

0 commit comments

Comments
 (0)