forked from editorconfig/editorconfig-notepad-plus-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPluginDefinition.cpp
More file actions
498 lines (420 loc) · 15.8 KB
/
PluginDefinition.cpp
File metadata and controls
498 lines (420 loc) · 15.8 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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
//this file is part of EditorConfig plugin for Notepad++
//
//Copyright (C)2003 Don HO <[email protected]>
//Copyright (C)2025 Sven Strickroth <[email protected]>
//Copyright (C)2011-2016 EditorConfig Team <https://editorconfig.org>
//
//This program 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.hpp"
#include "menuCmdID.hpp"
#include "DlgAbout.hpp"
//
// The plugin data that Notepad++ needs
//
FuncItem funcItem[nbFunc];
//
// The data of Notepad++ that you can use in your plugin commands
//
NppData nppData;
//
// Handle to the Notepad++ instance
//
HINSTANCE hInst;
//
// Initialize your plugin data here
// It will be called while plugin loading
void pluginInit(HANDLE hModule)
{
hInst = (HINSTANCE) hModule;
}
//
// Here you can do the clean up, save the parameters (if any) for the next session
//
void pluginCleanUp()
{
}
//
// Parse the EditorConfig file
//
static bool parseConfig(editorconfig_handle eh)
{
WCHAR wcFileName[MAX_PATH];
// get the file name
if (::SendMessage(nppData._nppHandle, NPPM_GETFULLCURRENTPATH, sizeof(wcFileName), reinterpret_cast<LPARAM>(wcFileName)) == FALSE)
return false;
const size_t bytes_needed = wcstombs(nullptr, wcFileName, 0);
if (bytes_needed == static_cast<size_t>(-1))
return false;
std::unique_ptr<char[]> fileName = std::unique_ptr<char[]>(new (std::nothrow) char[bytes_needed + 1]);
if (!fileName)
return false;
if (wcstombs(fileName.get(), wcFileName, bytes_needed) == static_cast<size_t>(-1))
return false;
// start parsing
int err_num;
if ((err_num = editorconfig_parse(fileName.get(), eh)) != 0 &&
/* Ignore full path error, whose error code is
* EDITORCONFIG_PARSE_NOT_FULL_PATH */
err_num != EDITORCONFIG_PARSE_NOT_FULL_PATH) {
std::tstringstream err_msg;
err_msg << TEXT("EditorConfig Error: ") << err_num;
::MessageBox(NULL, err_msg.str().c_str(),
TEXT("EditorConfig"), MB_OK);
editorconfig_handle_destroy(eh);
return false;
}
return true;
}
//
// Get the current scintilla
//
static HWND getCurrentScintilla()
{
int which = -1;
SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM) &which);
if (which == -1)
return NULL;
return((which == 0) ? nppData._scintillaMainHandle : nppData._scintillaSecondHandle);
}
//
// Set the syntax highlighting to "INI" for .editorconfig files
//
void setSyntaxFromFilename()
{
// Retrieve the filename
WCHAR szFilename[_MAX_PATH];
SendMessage(nppData._nppHandle, NPPM_GETFILENAME, _MAX_PATH, (LPARAM) szFilename);
if (wcscmp(szFilename, L".editorconfig") == 0)
SendMessage(nppData._nppHandle, NPPM_SETCURRENTLANGTYPE, 0, L_INI);
}
void loadConfig()
{
int name_value_count;
editorconfig_handle eh;
// Parse the EditorConfig file
eh = editorconfig_handle_init();
if (!eh || !parseConfig(eh))
return;
// Get the current scintilla
HWND curScintilla = getCurrentScintilla();
if (curScintilla == NULL)
return;
struct
{
const char* indent_style;
#define INDENT_SIZE_TAB (-1000) // indent_size = -1000 means indent_size = tab
int indent_size;
int tab_width;
const char* end_of_line;
} ecConf; // obtained EditorConfig settings will be here
memset(&ecConf, 0, sizeof(ecConf));
// apply the settings
name_value_count = editorconfig_handle_get_name_value_count(eh);
for (int i = 0; i < name_value_count; ++i) {
const char* name;
const char* value;
editorconfig_handle_get_name_value(eh, i, &name, &value);
if (!strcmp(name, "indent_style"))
ecConf.indent_style = value;
else if (!strcmp(name, "tab_width"))
ecConf.tab_width = atoi(value);
else if (!strcmp(name, "indent_size")) {
int value_i = atoi(value);
if (!strcmp(value, "tab"))
ecConf.indent_size = INDENT_SIZE_TAB;
else if (value_i > 0)
ecConf.indent_size = value_i;
}
else if (!strcmp(name, "end_of_line"))
ecConf.end_of_line = value;
}
if (ecConf.indent_style) {
if (!strcmp(ecConf.indent_style, "tab"))
::SendMessage(curScintilla, SCI_SETUSETABS, (WPARAM)true, 0);
else if (!strcmp(ecConf.indent_style, "space"))
::SendMessage(curScintilla, SCI_SETUSETABS, (WPARAM)false, 0);
}
if (ecConf.indent_size > 0) {
::SendMessage(curScintilla, SCI_SETINDENT,
(WPARAM)ecConf.indent_size, 0);
// We set the tab width here, so that this could be overrided then
// if ecConf.tab_wdith > 0
::SendMessage(curScintilla, SCI_SETTABWIDTH,
(WPARAM)ecConf.indent_size, 0);
}
if (ecConf.tab_width > 0)
::SendMessage(curScintilla, SCI_SETTABWIDTH,
(WPARAM)ecConf.tab_width, 0);
if (ecConf.indent_size == INDENT_SIZE_TAB)
// set indent_size to tab_width here
::SendMessage(curScintilla, SCI_SETINDENT,
(WPARAM)::SendMessage(curScintilla, SCI_GETTABWIDTH, 0, 0), 0);
// set eol
if (ecConf.end_of_line) {
if (!strcmp(ecConf.end_of_line, "lf"))
::SendMessage(curScintilla, SCI_SETEOLMODE,
(WPARAM)SC_EOL_LF, 0);
else if (!strcmp(ecConf.end_of_line, "cr"))
::SendMessage(curScintilla, SCI_SETEOLMODE,
(WPARAM)SC_EOL_CR, 0);
else if (!strcmp(ecConf.end_of_line, "crlf"))
::SendMessage(curScintilla, SCI_SETEOLMODE,
(WPARAM)SC_EOL_CRLF, 0);
}
editorconfig_handle_destroy(eh);
}
//
// Is the char c a newline character?
//
static bool isNewline(char c)
{
return c == '\n' || c == '\r';
}
//
// insert the final newline or remove any remaining newlines
//
void insertFinalNewline(bool insert)
{
HWND curScintilla = getCurrentScintilla();
if (curScintilla == NULL)
return;
// Get the last line number
intptr_t lastLine = ::SendMessage(curScintilla, SCI_GETLINECOUNT, 0, 0) - 1;
// Get the length of that last line
intptr_t length = ::SendMessage(curScintilla, SCI_LINELENGTH, lastLine, 0);
// Do we need to insert a final newline?
if (insert)
{
// If there is an empty last line, we're done
if (length == 0)
return;
// What type of EOL must be inserted?
char eolBuf[10];
eolBuf[0] = 0;
int eolMode = static_cast<int>(::SendMessage(curScintilla, SCI_GETEOLMODE, 0, 0));
switch (eolMode)
{
case SC_EOL_CRLF:
strcpy(eolBuf, "\r\n");
break;
case SC_EOL_CR:
strcpy(eolBuf, "\r");
break;
case SC_EOL_LF:
strcpy(eolBuf, "\n");
break;
}
// Add the EOL at the end of the document
length = strlen(eolBuf);
if (length != 0)
SendMessage(curScintilla, SCI_APPENDTEXT, length, (LPARAM) eolBuf);
return;
}
// Is there an non-empty last line, we're done
if (length != 0)
return;
// Find the last non-newline character
length = SendMessage(curScintilla, SCI_GETTEXTLENGTH, 0, 0);
intptr_t pos = length;
while (pos > 0 && isNewline((char) SendMessage(curScintilla, SCI_GETCHARAT, pos - 1, 0)))
pos--;
// Remove the final newline(s)
SendMessage(curScintilla, SCI_DELETERANGE, pos, length - pos);
}
static void selectBufferId(HWND hWnd, uptr_t bufferId)
{
const int32_t docPosInfo = static_cast<int32_t>(::SendMessage(hWnd, NPPM_GETPOSFROMBUFFERID, bufferId, 0));
SendMessage(hWnd, NPPM_ACTIVATEDOC, docPosInfo >> 30, docPosInfo & 0x3FFFFFFF);
}
void onBeforeSave(HWND hWnd, uptr_t idFrom)
{
const uptr_t keepBufferID = ::SendMessage(hWnd, NPPM_GETCURRENTBUFFERID, 0, 0);
selectBufferId(hWnd, idFrom);
editorconfig_handle eh = editorconfig_handle_init();
if (!eh || !parseConfig(eh))
{
selectBufferId(hWnd, keepBufferID);
return;
}
enum {
NPPEC_BOOLVAL_UNSPECIFIED = -1,
NPPEC_BOOLVAL_FALSE = 0,
NPPEC_BOOLVAL_TRUE = 1
};
int trim_trailing_whitespace = NPPEC_BOOLVAL_UNSPECIFIED;
int insert_final_newline = NPPEC_BOOLVAL_UNSPECIFIED;
int end_of_line = 0;
int charset = 0;
int name_value_count = editorconfig_handle_get_name_value_count(eh);
for (int i = 0; i < name_value_count; ++i) {
const char* name;
const char* value;
editorconfig_handle_get_name_value(eh, i, &name, &value);
if (trim_trailing_whitespace == NPPEC_BOOLVAL_UNSPECIFIED
&& strcmp(name, "trim_trailing_whitespace") == 0) {
if (strcmp(value, "true") == 0) {
trim_trailing_whitespace = NPPEC_BOOLVAL_TRUE;
}
continue;
}
if (insert_final_newline == NPPEC_BOOLVAL_UNSPECIFIED
&& strcmp(name, "insert_final_newline") == 0) {
if (strcmp(value, "true") == 0) {
insert_final_newline = NPPEC_BOOLVAL_TRUE;
} else if (strcmp(value, "false") == 0) {
insert_final_newline = NPPEC_BOOLVAL_FALSE;
}
continue;
}
// Need to convert the end of lines?
if (strcmp(name, "end_of_line") == 0) {
if (strcmp(value, "lf") == 0)
end_of_line = IDM_FORMAT_TOUNIX;
else if (strcmp(value, "crlf") == 0)
end_of_line = IDM_FORMAT_TODOS;
else if (strcmp(value, "cr") == 0)
end_of_line = IDM_FORMAT_TOMAC;
continue;
}
// Need to convert the charset of the document?
if (strcmp(name, "charset") == 0)
{
if (strcmp(value, "latin1") == 0)
charset = IDM_FORMAT_CONV2_ANSI;
else if (strcmp(value, "utf-8") == 0)
charset = IDM_FORMAT_CONV2_AS_UTF_8;
else if (strcmp(value, "utf-8-bom") == 0)
charset = IDM_FORMAT_CONV2_UTF_8;
else if (strcmp(value, "utf-16be") == 0)
charset = IDM_FORMAT_CONV2_UTF_16BE;
else if (strcmp(value, "utf-16le") == 0)
charset = IDM_FORMAT_CONV2_UTF_16LE;
continue;
}
}
// Save the folding behavior and set it to 0 to keep folds from opening
// when modifying the file.
HWND curScintilla = getCurrentScintilla();
int automatic_fold = static_cast<int>(::SendMessage(curScintilla, SCI_GETAUTOMATICFOLD, 0, 0));
SendMessage(curScintilla, SCI_SETAUTOMATICFOLD, 0, 0);
// Make sure there is no active selection
const int curPos = static_cast<int>(SendMessage(curScintilla, SCI_GETCURRENTPOS, 0, 0));
SendMessage(curScintilla, SCI_SETEMPTYSELECTION, curPos, 0);
// Trailing whitespace needs to be trimmed before 'insert_final_newline' is
// applied.
if (trim_trailing_whitespace == NPPEC_BOOLVAL_TRUE) {
SendMessage(hWnd, NPPM_MENUCOMMAND, 0, IDM_EDIT_TRIMTRAILING);
}
if (insert_final_newline != NPPEC_BOOLVAL_UNSPECIFIED) {
insertFinalNewline(insert_final_newline == NPPEC_BOOLVAL_TRUE);
}
if (end_of_line != 0) {
SendMessage(hWnd, NPPM_MENUCOMMAND, 0, end_of_line);
}
if (charset != 0) {
SendMessage(hWnd, NPPM_MENUCOMMAND, 0, charset);
}
// Restore the folding behavior
SendMessage(curScintilla, SCI_SETAUTOMATICFOLD, automatic_fold, 0);
editorconfig_handle_destroy(eh);
selectBufferId(hWnd, keepBufferID);
}
void onReloadEditorConfig()
{
loadConfig();
}
void showEditorConfigSettings()
{
int name_value_count;
editorconfig_handle eh;
// Parse the EditorConfig file
eh = editorconfig_handle_init();
if (!eh || !parseConfig(eh))
return;
std::tstringstream settings;
settings << "Current EditorConfig settings:" << std::endl << std::endl;
// get the settings
name_value_count = editorconfig_handle_get_name_value_count(eh);
for (int i = 0; i < name_value_count; ++i) {
const char* name;
const char* value;
editorconfig_handle_get_name_value(eh, i, &name, &value);
settings << name << " = " << value << std::endl;
}
MessageBox(NULL, settings.str().c_str(), TEXT("EditorConfig"), MB_OK);
}
//
// Initialization of your plugin commands
// You should fill your plugins commands here
void commandMenuInit()
{
//--------------------------------------------//
//-- STEP 3. CUSTOMIZE YOUR PLUGIN COMMANDS --//
//--------------------------------------------//
// with function :
// setCommand(int index, // zero based number to indicate the order of command
// TCHAR *commandName, // the command name that you want to see in plugin menu
// PFUNCPLUGINCMD functionPointer, // the symbol of function (function pointer) associated with this command. The body should be defined below. See Step 4.
// ShortcutKey *shortcut, // optional. Define a shortcut to trigger this command
// bool check0nInit // optional. Make this menu item be checked visually
// );
setCommand(0, TEXT("Reload EditorConfig for this file"),
onReloadEditorConfig, NULL, false);
setCommand(1, TEXT("Show EditorConfig settings for this file"),
showEditorConfigSettings, NULL, false);
// Separator
setCommand(2, TEXT(""), NULL, NULL, false);
setCommand(3, TEXT("About..."), showAboutDlg, NULL, false);
}
//
// Here you can do the clean up (especially for the shortcut)
//
void commandMenuCleanUp()
{
// Don't forget to deallocate your shortcut here
}
//
// This function help you to initialize your plugin commands
//
bool setCommand(size_t index, TCHAR *cmdName, PFUNCPLUGINCMD pFunc, ShortcutKey *sk, bool check0nInit)
{
if (index >= nbFunc)
return false;
lstrcpy(funcItem[index]._itemName, cmdName);
funcItem[index]._pFunc = pFunc;
funcItem[index]._init2Check = check0nInit;
funcItem[index]._pShKey = sk;
return true;
}
//
// Center the window, relative the NPP-window
//
void centerWindow(HWND hDlg)
{
RECT rc;
GetClientRect(nppData._nppHandle, &rc);
POINT center;
int w = rc.right - rc.left;
int h = rc.bottom - rc.top;
center.x = rc.left + (w / 2);
center.y = rc.top + (h / 2);
ClientToScreen(nppData._nppHandle, ¢er);
RECT dlgRect;
GetClientRect(hDlg, &dlgRect);
int x = center.x - ((dlgRect.right - dlgRect.left) / 2);
int y = center.y - ((dlgRect.bottom - dlgRect.top) / 2);
SetWindowPos(hDlg, HWND_TOP, x, y, -1, -1, SWP_NOSIZE | SWP_SHOWWINDOW);
}