forked from dail8859/DoxyIt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPluginDefinition.cpp
More file actions
436 lines (345 loc) · 14.4 KB
/
PluginDefinition.cpp
File metadata and controls
436 lines (345 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
// This file is part of DoxyIt.
//
// Copyright (C)2013 Justin Dailey <dail8859@yahoo.com>
//
// DoxyIt is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#include "PluginDefinition.h"
#include "Utils.h"
#include "Parsers.h"
#include "Version.h"
#include "SettingsDialog.h"
#include "AboutDialog.h"
// --- Local variables ---
static bool do_active_commenting; // active commenting - create or extend a document block
//static bool do_active_wrapping; // active wrapping - wrap text inside of document blocks...todo
static NppData nppData;
static SciFnDirect pSciMsg; // For direct scintilla call
static sptr_t pSciWndData; // For direct scintilla call
static SettingsDialog sd; // The settings dialog
static HANDLE _hModule; // For dialog initialization
// --- Menu callbacks ---
static void doxyItFunction();
static void doxyItFile();
static void activeCommenting();
//static void activeWrapping();
static void showSettings();
static void showAbout();
// --- Global variables ---
ShortcutKey sk = {true, true, true, 'D'};
FuncItem funcItem[nbFunc] = {
{TEXT("DoxyIt - Function"), doxyItFunction, 0, false, &sk},
{TEXT("DoxyIt - File"), doxyItFile, 0, false, NULL},
{TEXT(""), NULL, 0, false, NULL}, // separator
{TEXT("Active commenting"), activeCommenting, 0, false, NULL},
{TEXT(""), NULL, 0, false, NULL}, // separator
{TEXT("Settings..."), showSettings, 0, false, NULL},
{TEXT("About..."), showAbout, 0, false, NULL}
};
inline LRESULT SendScintilla(UINT Msg, WPARAM wParam, LPARAM lParam)
{
return pSciMsg(pSciWndData, Msg, wParam, lParam);
}
inline LRESULT SendNpp(UINT Msg, WPARAM wParam, LPARAM lParam)
{
return SendMessage(nppData._nppHandle, Msg, wParam, lParam);
}
bool updateScintilla()
{
HWND curScintilla;
// Get the current scintilla
int which = -1;
SendNpp(NPPM_GETCURRENTSCINTILLA, SCI_UNUSED, (LPARAM)&which);
if(which == -1) return false;
curScintilla = (which == 0) ? nppData._scintillaMainHandle : nppData._scintillaSecondHandle;
// Get the function and pointer to it for more efficient calls
pSciMsg = (SciFnDirect) SendMessage(curScintilla, SCI_GETDIRECTFUNCTION, 0, 0);
pSciWndData = (sptr_t) SendMessage(curScintilla, SCI_GETDIRECTPOINTER, 0, 0);
return true;
}
// --- Configuration ---
void getIniFilePath(wchar_t *iniPath, int size)
{
SendNpp(NPPM_GETPLUGINSCONFIGDIR, size, (LPARAM) iniPath);
wcscat_s(iniPath, size, TEXT("\\"));
wcscat_s(iniPath, size, NPP_PLUGIN_NAME);
wcscat_s(iniPath, size, TEXT(".ini"));
}
void configSave()
{
wchar_t iniPath[MAX_PATH];
std::wstring ws;
getIniFilePath(iniPath, MAX_PATH);
// Completely delete the file
DeleteFile(iniPath);
// [DoxyIt]
WritePrivateProfileString(NPP_PLUGIN_NAME, TEXT("active_commenting"), BOOLTOSTR(do_active_commenting), iniPath);
WritePrivateProfileString(NPP_PLUGIN_NAME, TEXT("version"), VERSION_LINEAR_TEXT, iniPath);
WritePrivateProfileString(NPP_PLUGIN_NAME, TEXT("version_stage"), VERSION_STAGE, iniPath);
for(auto const &p : parsers)
{
const ParserSettings *ps = &(p.ps);
// Wrap everything in quotes to preserve whitespace
ws = TEXT("\"") + toWideString(ps->doc_start) + TEXT("\"");
WritePrivateProfileString(p.lang.c_str(), TEXT("doc_start"), ws.c_str(), iniPath);
ws = TEXT("\"") + toWideString(ps->doc_line) + TEXT("\"");
WritePrivateProfileString(p.lang.c_str(), TEXT("doc_line_"), ws.c_str(), iniPath);
ws = TEXT("\"") + toWideString(ps->doc_end) + TEXT("\"");
WritePrivateProfileString(p.lang.c_str(), TEXT("doc_end__"), ws.c_str(), iniPath);
ws = TEXT("\"") + toWideString(ps->command_prefix) + TEXT("\"");
WritePrivateProfileString(p.lang.c_str(), TEXT("command_prefix"), ws.c_str(), iniPath);
// Encode \r\n as literal "\r\n" in the ini file
ws = TEXT("\"") + toWideString(stringReplace(std::string(ps->file_format), "\r\n", "\\r\\n")) + TEXT("\"");
WritePrivateProfileString(p.lang.c_str(), TEXT("file_format"), ws.c_str(), iniPath);
// Encode \r\n as literal "\r\n" in the ini file
ws = TEXT("\"") + toWideString(stringReplace(std::string(ps->function_format), "\r\n", "\\r\\n")) + TEXT("\"");
WritePrivateProfileString(p.lang.c_str(), TEXT("function_format"), ws.c_str(), iniPath);
// Write out interal parser attributes
if(!p.external)
{
WritePrivateProfileString(p.lang.c_str(), TEXT("align"), BOOLTOSTR(ps->align), iniPath);
}
else // add it to the list of external settings
{
WritePrivateProfileString(TEXT("External"), p.language_name.c_str(), TEXT(""), iniPath);
}
}
}
void configLoad()
{
wchar_t iniPath[MAX_PATH];
wchar_t tbuffer[512]; // "relatively" large
wchar_t tbuffer2[512];
wchar_t *current;
ParserSettings ps;
getIniFilePath(iniPath, MAX_PATH);
// [DoxyIt]
GetPrivateProfileString(NPP_PLUGIN_NAME, TEXT("active_commenting"), TEXT("true"), tbuffer, MAX_PATH, iniPath);
do_active_commenting = (lstrcmp(tbuffer, TEXT("true")) == 0);
// NPPM_SETMENUITEMCHECK does not seem to work unless the
// menu item is actually clicked, so lets do it manually
if(do_active_commenting) CheckMenuItem(GetMenu(nppData._nppHandle), funcItem[3]._cmdID, MF_CHECKED);
// Don't need these for now
//version = GetPrivateProfileInt(NPP_PLUGIN_NAME, TEXT("version"), 0, iniPath);
//version_stage = GetPrivateProfileString(NPP_PLUGIN_NAME, TEXT("version_stage"), TEXT(""), tbuffer, MAX_PATH, iniPath);
for(auto &p : parsers)
{
// NOTE: We cant use the default value because GetPrivateProfileString strips the whitespace,
// also, wrapping it in quotes doesn't seem to work either. So...use "!!!" as the default text
// and if we find that the value wasn't found and we have "!!!" then use the default value in the
// parser, else, use what we pulled from the file.
GetPrivateProfileString(p.lang.c_str(), TEXT("doc_start"), TEXT("!!!"), tbuffer, 512, iniPath);
if(lstrcmp(tbuffer, TEXT("!!!")) != 0) p.ps.doc_start = toString(tbuffer);
GetPrivateProfileString(p.lang.c_str(), TEXT("doc_line_"), TEXT("!!!"), tbuffer, 512, iniPath);
if(lstrcmp(tbuffer, TEXT("!!!")) != 0) p.ps.doc_line = toString(tbuffer);
GetPrivateProfileString(p.lang.c_str(), TEXT("doc_end__"), TEXT("!!!"), tbuffer, 512, iniPath);
if(lstrcmp(tbuffer, TEXT("!!!")) != 0) p.ps.doc_end = toString(tbuffer);
GetPrivateProfileString(p.lang.c_str(), TEXT("command_prefix"), TEXT("!!!"), tbuffer, 512, iniPath);
if(lstrcmp(tbuffer, TEXT("!!!")) != 0) p.ps.command_prefix = toString(tbuffer);
GetPrivateProfileString(p.lang.c_str(), TEXT("function_format"), TEXT("!!!"), tbuffer, 512, iniPath);
if(lstrcmp(tbuffer, TEXT("!!!")) != 0) p.ps.function_format = stringReplace(toString(tbuffer), "\\r\\n", "\r\n");
GetPrivateProfileString(p.lang.c_str(), TEXT("file_format"), TEXT("!!!"), tbuffer, 512, iniPath);
if(lstrcmp(tbuffer, TEXT("!!!")) != 0) p.ps.file_format = stringReplace(toString(tbuffer), "\\r\\n", "\r\n");
GetPrivateProfileString(p.lang.c_str(), TEXT("align"), BOOLTOSTR(p.ps.align), tbuffer, 512, iniPath);
p.ps.align = (lstrcmp(tbuffer, TEXT("true")) == 0);
}
GetPrivateProfileSection(TEXT("External"), tbuffer, 512, iniPath);
current = tbuffer;
while(current[0] != NULL)
{
wchar_t *equals = wcschr(current, TEXT('='));
// Temporarily remove the '=' that was found
*equals = NULL;
GetPrivateProfileString(current, TEXT("doc_start"), TEXT("/**"), tbuffer2, 512, iniPath);
ps.doc_start = toString(tbuffer2);
GetPrivateProfileString(current, TEXT("doc_line_"), TEXT(" * "), tbuffer2, 512, iniPath);
ps.doc_line = toString(tbuffer2);
GetPrivateProfileString(current, TEXT("doc_end__"), TEXT(" */"), tbuffer2, 512, iniPath);
ps.doc_end = toString(tbuffer2);
GetPrivateProfileString(current, TEXT("command_prefix"), TEXT("\\"), tbuffer2, 512, iniPath);
ps.command_prefix = toString(tbuffer2);
GetPrivateProfileString(current, TEXT("function_format"), TEXT("!!!"), tbuffer2, 512, iniPath);
if(lstrcmp(tbuffer2, TEXT("!!!")) != 0) ps.function_format = stringReplace(toString(tbuffer2), "\\r\\n", "\r\n");
else ps.function_format = default_internal_function_format;
GetPrivateProfileString(current, TEXT("file_format"), TEXT("!!!"), tbuffer2, 512, iniPath);
if(lstrcmp(tbuffer2, TEXT("!!!")) != 0) ps.file_format = stringReplace(toString(tbuffer2), "\\r\\n", "\r\n");
else ps.file_format = default_file_format;
addNewParser(toString(current), &ps);
// add back in the equals so we can correctly calculate the length
*equals = TEXT('=');
current = ¤t[lstrlen(current) + 1];
}
}
void pluginInit(HANDLE hModule)
{
_hModule = hModule;
}
void pluginCleanUp()
{
}
void setNppInfo(NppData notepadPlusData)
{
nppData = notepadPlusData;
sd.init((HINSTANCE) _hModule, nppData);
}
// --- Menu call backs ---
void doxyItFunction()
{
std::string doc_block;
int startLine, endLine;
char *indent = NULL;
if(!updateScintilla()) return;
doc_block = Parse();
// Don't issue any warning messages, let Parse() handle that for us since it knows
// about the error. Just return if it is a zero length string
if(doc_block.length() == 0)
return;
// Keep track of where we started
startLine = SendNpp(NPPM_GETCURRENTLINE);
// Get the whitespace of the next line so we can insert it in front of
// all the lines of the document block that is going to be inserted
indent = getLineIndentStr(startLine + 1);
SendScintilla(SCI_BEGINUNDOACTION);
SendScintilla(SCI_REPLACESEL, SCI_UNUSED, (LPARAM) doc_block.c_str());
endLine = SendNpp(NPPM_GETCURRENTLINE); // get the end of the document block
if(indent) insertBeforeLines(indent, startLine, endLine + 1);
SendScintilla(SCI_ENDUNDOACTION);
if(indent) delete[] indent;
}
void doxyItFile()
{
std::string doc_block;
const ParserSettings *ps;
if(!updateScintilla()) return;
ps = getCurrentParserSettings();
if(!ps)
{
MessageBox(NULL, TEXT("Unrecognized language type."), NPP_PLUGIN_NAME, MB_OK|MB_ICONERROR);
return;
}
doc_block = FormatFileBlock(ps);
SendScintilla(SCI_REPLACESEL, SCI_UNUSED, (LPARAM) doc_block.c_str());
}
void activeCommenting()
{
do_active_commenting = !do_active_commenting;
SendNpp(NPPM_SETMENUITEMCHECK, funcItem[3]._cmdID, (LPARAM) do_active_commenting);
}
void showSettings()
{
if(!updateScintilla()) return;
sd.doDialog();
}
void showAbout()
{
CreateDialog((HINSTANCE) _hModule, MAKEINTRESOURCE(IDD_ABOUTDLG), nppData._nppHandle, abtDlgProc);
}
// --- Notification callbacks ---
void doxyItNewLine()
{
std::string indentation;
const ParserSettings *ps;
const char *eol;
char *previousLine, *found = NULL;
int curLine;
if(!updateScintilla()) return;
ps = getCurrentParserSettings();
if(!ps) return;
eol = getEolStr();
curLine = SendNpp(NPPM_GETCURRENTLINE);
previousLine = getLine(curLine - 1);
// NOTE: we cannot use getLineIndentStr() because doc_start or doc_line may start with whitespace
// which we don't want counted towards the indentation string.
if(found = strstr(previousLine, ps->doc_line.c_str()))
{
indentation.append(previousLine, found - previousLine);
// doc_line should have only whitespace in front of it
if(isWhiteSpace(indentation))
{
SendScintilla(SCI_BEGINUNDOACTION);
SendScintilla(SCI_DELLINELEFT); // Clear any automatic indentation
SendScintilla(SCI_REPLACESEL, SCI_UNUSED, (LPARAM) indentation.c_str());
SendScintilla(SCI_REPLACESEL, SCI_UNUSED, (LPARAM) ps->doc_line.c_str());
SendScintilla(SCI_ENDUNDOACTION);
SendScintilla(SCI_CHOOSECARETX);
}
}
// If doc_start is relatively long we do not want the user typing the entire line, just the first 3 should suffice.
// Also, if doc_end is found, this means a doc block was closed. This allows e.g. /** inline comments */
else if((found = strstr(previousLine, ps->doc_start.substr(0, 3).c_str())) &&
strstr(previousLine, ps->doc_end.c_str()) == 0)
{
indentation.append(previousLine, found - previousLine);
if(isWhiteSpace(indentation))
{
int pos;
unsigned int i = 0;
// Count the characters in common so we can add the rest
while(i < ps->doc_start.length() && found[i] == ps->doc_start.at(i)) ++i;
SendScintilla(SCI_BEGINUNDOACTION);
SendScintilla(SCI_DELLINELEFT); // Clear any automatic indentation
SendScintilla(SCI_DELETEBACK); // Clear the newline
SendScintilla(SCI_REPLACESEL, SCI_UNUSED, (LPARAM) &ps->doc_start.c_str()[i]); // Fill the rest of doc_start
SendScintilla(SCI_REPLACESEL, SCI_UNUSED, (LPARAM) eol);
SendScintilla(SCI_REPLACESEL, SCI_UNUSED, (LPARAM) indentation.c_str());
SendScintilla(SCI_REPLACESEL, SCI_UNUSED, (LPARAM) ps->doc_line.c_str());
pos = SendScintilla(SCI_GETCURRENTPOS); // Save this position so we can restore it
SendScintilla(SCI_LINEEND); // Skip any text the user carried to next line
SendScintilla(SCI_REPLACESEL, SCI_UNUSED, (LPARAM) eol);
SendScintilla(SCI_REPLACESEL, SCI_UNUSED, (LPARAM) indentation.c_str());
SendScintilla(SCI_REPLACESEL, SCI_UNUSED, (LPARAM) ps->doc_end.c_str());
SendScintilla(SCI_ENDUNDOACTION);
SendScintilla(SCI_CHOOSECARETX);
// Restore the position
SendScintilla(SCI_GOTOPOS, pos);
}
}
delete[] previousLine;
}
void handleNotification(SCNotification *notifyCode)
{
static bool do_newline = false;
NotifyHeader nh = notifyCode->nmhdr;
int ch = notifyCode->ch;
switch(nh.code)
{
case SCN_UPDATEUI: // Now is when we can check to see if we do the commenting
if(do_newline)
{
do_newline = false;
if(!updateScintilla()) return;
doxyItNewLine();
}
break;
case SCN_CHARADDED:
// Set a flag so that all line endings can trigger the commenting
if((ch == '\r' || ch == '\n') && do_active_commenting) do_newline = true;
break;
case NPPN_READY:
InitializeParsers();
configLoad();
getCurrentParser(true);
break;
case NPPN_SHUTDOWN:
configSave();
CleanUpParsers();
break;
case NPPN_BUFFERACTIVATED:
case NPPN_LANGCHANGED:
// Don't actually need the parser here, but this forces it to updates the current reference
getCurrentParser(true);
break;
}
return;
}