Skip to content

Commit 803af1c

Browse files
authored
Merge pull request #75 from ffes/fix-indentation
Implement "Fix indentation"
2 parents 4740cf3 + 072890d commit 803af1c

File tree

3 files changed

+47
-18
lines changed

3 files changed

+47
-18
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ The EditorConfig Notepad++ plugin supports the following EditorConfig
105105
* charset
106106
* root (only used by EditorConfig core)
107107

108+
## Fix indentation
109+
110+
In v0.5.0 a new menu item was added to the plugin named `Fix indentation`.
111+
This is very simple way to fix the indentation style (tabs or spaces) at the **beginning** of the line.
112+
113+
It basically is the same as when you use `Ctrl+A` followed by `Tab` (to indent everything one level) and directly follow by `Shift-Tab` (to unindent everything back). It works most of the time, but only for the tabs/spaces at the start of a line, not at the end (like with comments at the end of the line are aligned).
114+
108115
## Bugs and Feature Requests
109116

110117
Feel free to submit bugs, feature requests, and other issues to the

src/PluginDefinition.cpp

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,35 @@ void insertFinalNewline(bool insert)
273273
SendMessage(curScintilla, SCI_DELETERANGE, pos, length - pos);
274274
}
275275

276+
//
277+
// Fix the indentation of the document
278+
//
279+
void fixIndentation()
280+
{
281+
const HWND curScintilla = getCurrentScintilla();
282+
if (curScintilla == NULL)
283+
return;
284+
285+
// Make sure the undo works as expected
286+
SendMessage(curScintilla, SCI_BEGINUNDOACTION, 0, 0);
287+
288+
// Get the number of lines in the document
289+
const int linecount = static_cast<int>(SendMessage(curScintilla, SCI_GETLINECOUNT, 0, 0)) - 1;
290+
291+
// Go through the lines
292+
for (int line = 1; line <= linecount; line++)
293+
{
294+
// Get the indentation level of the current line
295+
const int level = static_cast<int>(SendMessage(curScintilla, SCI_GETLINEINDENTATION, line, 0));
296+
297+
// Increase and decrease the indention level to reset the indentation style
298+
SendMessage(curScintilla, SCI_SETLINEINDENTATION, line, level + 1);
299+
SendMessage(curScintilla, SCI_SETLINEINDENTATION, line, level);
300+
}
301+
302+
SendMessage(curScintilla, SCI_ENDUNDOACTION, 0, 0);
303+
}
304+
276305
static void selectBufferId(HWND hWnd, uptr_t bufferId)
277306
{
278307
const int32_t docPosInfo = static_cast<int32_t>(::SendMessage(hWnd, NPPM_GETPOSFROMBUFFERID, bufferId, 0));
@@ -392,11 +421,6 @@ void onBeforeSave(HWND hWnd, uptr_t idFrom)
392421
selectBufferId(hWnd, keepBufferID);
393422
}
394423

395-
void onReloadEditorConfig()
396-
{
397-
loadConfig();
398-
}
399-
400424
void showEditorConfigSettings()
401425
{
402426
int name_value_count;
@@ -427,6 +451,7 @@ void showEditorConfigSettings()
427451
//
428452
// Initialization of your plugin commands
429453
// You should fill your plugins commands here
454+
//
430455
void commandMenuInit()
431456
{
432457
//--------------------------------------------//
@@ -439,15 +464,13 @@ void commandMenuInit()
439464
// ShortcutKey *shortcut, // optional. Define a shortcut to trigger this command
440465
// bool check0nInit // optional. Make this menu item be checked visually
441466
// );
442-
setCommand(0, TEXT("Reload EditorConfig for this file"),
443-
onReloadEditorConfig, NULL, false);
444-
setCommand(1, TEXT("Show EditorConfig settings for this file"),
445-
showEditorConfigSettings, NULL, false);
446-
447-
// Separator
448-
setCommand(2, TEXT(""), NULL, NULL, false);
449-
450-
setCommand(3, TEXT("About..."), showAboutDlg, NULL, false);
467+
int id = 0;
468+
setCommand(id++, TEXT("Reload EditorConfig for this file"), loadConfig, NULL, false);
469+
setCommand(id++, TEXT("Show EditorConfig settings for this file"), showEditorConfigSettings, NULL, false);
470+
setCommand(id++, TEXT(""), NULL, NULL, false); // Separator
471+
setCommand(id++, TEXT("Fix indentation"), fixIndentation, NULL, false);
472+
setCommand(id++, TEXT(""), NULL, NULL, false); // Separator
473+
setCommand(id++, TEXT("About..."), showAboutDlg, NULL, false);
451474
}
452475

453476
//

src/PluginDefinition.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
//along with this program; if not, write to the Free Software
1818
//Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
1919
//
20-
20+
2121
#pragma once
2222

2323
#include <string>
@@ -26,7 +26,7 @@
2626
#include "editorconfig/editorconfig.h"
2727

2828
//
29-
// All difinitions of plugin interface
29+
// All definitions of plugin interface
3030
//
3131
#include "PluginInterface.hpp"
3232

@@ -55,8 +55,7 @@ const TCHAR NPP_PLUGIN_NAME[] = TEXT("EditorConfig");
5555
//
5656
// Here define the number of your plugin commands
5757
//
58-
const int nbFunc = 4;
59-
58+
const int nbFunc = 6;
6059

6160
struct NppData;
6261
extern HINSTANCE hInst;

0 commit comments

Comments
 (0)