-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathToolsEditor.cpp
157 lines (125 loc) · 6.16 KB
/
ToolsEditor.cpp
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
#include "stdAfx.h"
#include "XMLTools.h"
#include "Scintilla.h"
#include "nppHelpers.h"
void closeXMLTag() {
dbgln("closeXMLTag()");
char buf[512];
int currentEdit;
::SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)¤tEdit);
HWND hCurrentEditView = getCurrentHScintilla(currentEdit);
Sci_PositionCR currentPos = Sci_PositionCR(::SendMessage(hCurrentEditView, SCI_GETCURRENTPOS, 0, 0));
Sci_PositionCR beginPos = currentPos - (sizeof(buf) - 1);
Sci_PositionCR startPos = (beginPos > 0) ? beginPos : 0;
Sci_PositionCR size = currentPos - startPos;
//int insertStringSize = 2;
#define MAX_TAGNAME_LENGTH 516
char insertString[MAX_TAGNAME_LENGTH] = "</";
if (size >= 3) {
struct TextRange tr = { {startPos, currentPos}, buf };
::SendMessage(hCurrentEditView, SCI_GETTEXTRANGE, 0, (LPARAM)&tr);
if (buf[size - 2] != '/' && buf[size - 2] != '-' && buf[size - 2] != '?' && buf[size - 2] != ']' && buf[size - 2] != ')' && buf[size - 2] != '%') {
const char* pBegin = &buf[0];
const char* pCur = &buf[size - 2];
int insertStringSize = 2;
// search the beginning of tag
// TODO: optimize by not looping on every char !
for (; pCur > pBegin && *pCur != '<' && *pCur != '>';) {
--pCur;
}
if (*pCur == '<') {
++pCur;
if (*pCur == '/' || *pCur == '!' || *pCur == '?' || *pCur == '[' || *pCur == '%') return;
// search attributes of
while (*pCur != '>' && *pCur != ' ' && *pCur != '\n' && *pCur != '\r') {
//while (IsCharAlphaNumeric(*pCur) || strchr(":_-.", *pCur) != NULL) {
if (insertStringSize == MAX_TAGNAME_LENGTH - 1) return;
insertString[insertStringSize++] = *pCur;
++pCur;
}
}
if (insertStringSize == MAX_TAGNAME_LENGTH - 1) return;
insertString[insertStringSize++] = '>';
insertString[insertStringSize] = '\0';
if (insertStringSize > 3) {
::SendMessage(hCurrentEditView, SCI_BEGINUNDOACTION, 0, 0);
::SendMessage(hCurrentEditView, SCI_REPLACESEL, 0, (LPARAM)insertString);
::SendMessage(hCurrentEditView, SCI_SETSEL, currentPos, currentPos);
::SendMessage(hCurrentEditView, SCI_ENDUNDOACTION, 0, 0);
}
}
}
#undef MAX_TAGNAME_LENGTH
}
///////////////////////////////////////////////////////////////////////////////
void tagAutoIndent() {
dbgln("tagAutoIndent()");
// On n'indente que si l'on est dans un noeud (au niveau de l'attribut ou
// au niveau du contenu. Donc on recherche le dernier < ou >. S'il s'agit
// d'un >, on regarde qu'il n'y ait pas de / avant (sinon on se retrouve
// au même niveau et il n'y a pas d'indentation à faire)
// Si le dernier symbole que l'on trouve est un <, alors on indente.
char buf[512];
int currentEdit;
::SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)¤tEdit);
HWND hCurrentEditView = getCurrentHScintilla(currentEdit);
Sci_PositionCR currentPos = Sci_PositionCR(::SendMessage(hCurrentEditView, SCI_GETCURRENTPOS, 0, 0));
Sci_PositionCR beginPos = currentPos - (sizeof(buf) - 1);
Sci_PositionCR startPos = (beginPos > 0) ? beginPos : 0;
Sci_PositionCR size = currentPos - startPos;
struct TextRange tr = { {startPos, currentPos}, buf };
::SendMessage(hCurrentEditView, SCI_GETTEXTRANGE, 0, (LPARAM)&tr);
int tabwidth = (int) ::SendMessage(hCurrentEditView, SCI_GETTABWIDTH, 0, 0);
bool usetabs = (bool) ::SendMessage(hCurrentEditView, SCI_GETUSETABS, 0, 0);
if (tabwidth <= 0) tabwidth = 4;
bool ignoreIndentation = false;
if (size >= 1) {
const char* pBegin = &buf[0];
const char* pCur = &buf[size - 1];
for (; pCur > pBegin && *pCur != '>';) --pCur;
if (pCur > pBegin) {
if (*(pCur - 1) == '/') ignoreIndentation = true; // si on a "/>", on abandonne l'indentation
// maintenant, on recherche le <
while (pCur > pBegin && *pCur != '<') --pCur;
if (*pCur == '<' && *(pCur + 1) == '/') ignoreIndentation = true; // si on a "</", on abandonne aussi
int insertStringSize = 0;
char insertString[516] = { '\0' };
--pCur;
// on récupère l'indentation actuelle
while (pCur > pBegin && *pCur != '\n' && *pCur != '\r') {
if (*pCur == '\t') insertString[insertStringSize++] = '\t';
else insertString[insertStringSize++] = ' ';
--pCur;
}
// et on ajoute une indentation
if (!ignoreIndentation) {
if (usetabs) insertString[insertStringSize++] = '\t';
else {
for (int i = 0; i < tabwidth; ++i) insertString[insertStringSize++] = ' ';
}
}
currentPos += insertStringSize;
// on a trouvé le <, il reste à insérer une indentation après le curseur
::SendMessage(hCurrentEditView, SCI_REPLACESEL, 0, (LPARAM)insertString);
::SendMessage(hCurrentEditView, SCI_SETSEL, currentPos, currentPos);
}
}
}
LangType setAutoXMLType(bool force = FALSE) {
dbgln("setAutoXMLType()");
LangType docType;
::SendMessage(nppData._nppHandle, NPPM_GETCURRENTLANGTYPE, 0, (LPARAM)&docType);
if (force || (config.doAutoXMLType && docType != LangType::L_XML)) {
int currentEdit;
::SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)¤tEdit);
HWND hCurrentEditView = getCurrentHScintilla(currentEdit);
// on récupère les 6 premiers caractères du fichier
char head[8] = { '\0' };
::SendMessage(hCurrentEditView, SCI_GETTEXT, 7, reinterpret_cast<LPARAM>(&head));
if (strlen(head) >= 6 && !strcmp(head, "<?xml ")) {
::SendMessage(nppData._nppHandle, NPPM_SETCURRENTLANGTYPE, 0, (LPARAM)LangType::L_XML);
docType = LangType::L_XML;
}
}
return docType;
}