forked from morbac/xmltools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ToolsComment.cpp
256 lines (221 loc) · 9.15 KB
/
ToolsComment.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
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
#include "StdAfx.h"
#include "XMLTools.h"
#include "nppHelpers.h"
#include "Report.h"
#include <string>
#include <stack>
int validateSelectionForComment(std::string str, std::string::size_type sellength) {
dbgln("validateSelectionForComment()");
// Validate the selection
std::stack<int> checkstack;
std::string::size_type curpos = 0;
int errflag = 0;
while (curpos <= sellength && !errflag && (curpos = str.find_first_of("<-*", curpos)) != std::string::npos) {
if (curpos > sellength) break;
if (!str.compare(curpos, 4, "<!--")) {
checkstack.push(0);
}
if (!str.compare(curpos, 3, "-->")) {
if (!checkstack.empty()) {
if (checkstack.top() != 0) errflag = checkstack.top();
else checkstack.pop();
}
else {
errflag = -3;
break;
}
}
if (!str.compare(curpos, 3, "<![")) {
std::string::size_type endvalpos = str.find("]**", curpos);
if (endvalpos != std::string::npos) checkstack.push(atoi(str.substr(curpos + 3, endvalpos).c_str()));
}
if (!str.compare(curpos, 3, "**[")) {
if (!checkstack.empty()) {
std::string::size_type endvalpos = str.find("]>", curpos);
if (endvalpos != std::string::npos && atoi(str.substr(curpos + 3, endvalpos).c_str()) != checkstack.top()) errflag = -2;
else checkstack.pop();
}
else {
errflag = -4;
break;
}
}
++curpos;
}
if (!checkstack.empty()) errflag = -1;
return errflag;
}
void commentSelection() {
dbgln("commentSelection()");
long currentEdit;
int isReadOnly;
::SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)¤tEdit);
HWND hCurrentEditView = getCurrentHScintilla(currentEdit);
isReadOnly = (int) ::SendMessage(hCurrentEditView, SCI_GETREADONLY, 0, 0);
if (isReadOnly) return;
size_t selstart = (size_t) ::SendMessage(hCurrentEditView, SCI_GETSELECTIONSTART, 0, 0);
size_t selend = (size_t) ::SendMessage(hCurrentEditView, SCI_GETSELECTIONEND, 0, 0);
size_t sellength = selend - selstart;
if (selend <= selstart) {
Report::_printf_err(L"Please select text to transform before you call the function.");
return;
}
char* data = new char[sellength + sizeof(char)];
if (!data) return; // allocation error, abort check
memset(data, '\0', sellength + sizeof(char));
::SendMessage(hCurrentEditView, SCI_GETSELTEXT, 0, reinterpret_cast<LPARAM>(data));
while (sellength >= 0 && (data[sellength] == '\r' || data[sellength] == '\n')) {
data[sellength--] = '\0';
}
std::string str(data);
delete[] data;
data = NULL;
int errflag = validateSelectionForComment(str, sellength);
if (errflag != 0) {
wchar_t msg[512];
swprintf(msg, 512, L"The current selection covers part of another comment.\nUncomment process may be not applicable.\n\nDo you want to continue ? Error code %d", errflag);
if (::MessageBox(nppData._nppHandle, msg, L"XML Tools plugin", MB_YESNO | MB_ICONASTERISK) == IDNO) {
str.clear();
return;
}
}
std::string::size_type curpos = sellength;
while (curpos != std::string::npos && (curpos = str.rfind("<!{", curpos)) != std::string::npos) {
if (curpos != std::string::npos) {
size_t endvalpos = str.find("}**", curpos);
int endval = atoi(str.substr(curpos + 3, endvalpos).c_str());
char tmpstr[64];
sprintf(tmpstr, "<!{%d}**", endval + 1);
str.replace(curpos, endvalpos - curpos + 3, tmpstr);
sellength += (strlen(tmpstr) - (endvalpos - curpos + 3));
}
--curpos;
}
curpos = sellength;
while (curpos != std::string::npos && (curpos = str.rfind("**{", curpos)) != std::string::npos) {
if (curpos != std::string::npos) {
size_t endvalpos = str.find("}>", curpos);
int endval = atoi(str.substr(curpos + 3, endvalpos).c_str());
char tmpstr[64];
sprintf(tmpstr, "**{%d}>", endval + 1);
str.replace(curpos, endvalpos - curpos + 2, tmpstr);
sellength += (strlen(tmpstr) - (endvalpos - curpos + 2));
}
--curpos;
}
curpos = sellength;
while (curpos != std::string::npos && (curpos = str.rfind("<!--", curpos)) != std::string::npos) {
if (curpos != std::string::npos) {
str.replace(curpos, 4, "<!{1}**");
sellength += 3;
}
--curpos;
}
curpos = sellength;
while (curpos != std::string::npos && (curpos = str.rfind("-->", curpos)) != std::string::npos) {
if (curpos != std::string::npos) {
str.replace(curpos, 3, "**{1}>");
sellength += 3;
}
--curpos;
}
str.insert(0, "<!--"); sellength += 4;
str.insert(sellength, "-->"); sellength += 3;
// Replace the selection with new string
::SendMessage(hCurrentEditView, SCI_REPLACESEL, 0, reinterpret_cast<LPARAM>(str.c_str()));
// Defines selection without scrolling
::SendMessage(hCurrentEditView, SCI_SETCURRENTPOS, selstart, 0);
::SendMessage(hCurrentEditView, SCI_SETANCHOR, selstart + sellength, 0);
str.clear();
}
///////////////////////////////////////////////////////////////////////////////
void uncommentSelection() {
dbgln("uncommentSelection()");
long currentEdit;
int isReadOnly;
::SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)¤tEdit);
HWND hCurrentEditView = getCurrentHScintilla(currentEdit);
isReadOnly = (int) ::SendMessage(hCurrentEditView, SCI_GETREADONLY, 0, 0);
if (isReadOnly) return;
size_t selstart = (size_t) ::SendMessage(hCurrentEditView, SCI_GETSELECTIONSTART, 0, 0);
size_t selend = (size_t) ::SendMessage(hCurrentEditView, SCI_GETSELECTIONEND, 0, 0);
size_t sellength = selend - selstart;
if (selend <= selstart) {
Report::_printf_err(L"Please select text to transform before you call the function.");
return;
}
char* data = new char[sellength + sizeof(char)];
if (!data) return; // allocation error, abort check
memset(data, '\0', sellength + sizeof(char));
::SendMessage(hCurrentEditView, SCI_GETSELTEXT, 0, reinterpret_cast<LPARAM>(data));
std::string str(data);
delete[] data;
data = NULL;
int errflag = validateSelectionForComment(str, sellength);
if (errflag != 0) {
wchar_t msg[512];
swprintf(msg, 512, L"Unable to uncomment the current selection.\nError code is %d.", errflag);
Report::_printf_err(msg);
str.clear();
return;
}
// Proceed to uncomment
std::string::size_type curpos = sellength;
while (curpos != std::string::npos && (curpos = str.rfind("-->", curpos)) != std::string::npos) {
if (curpos != std::string::npos) {
str.erase(curpos, 3);
sellength -= 3;
}
--curpos;
}
curpos = sellength;
while (curpos != std::string::npos && (curpos = str.rfind("<!--", curpos)) != std::string::npos) {
if (curpos != std::string::npos) {
str.erase(curpos, 4);
sellength -= 4;
}
--curpos;
}
curpos = sellength;
while (curpos != std::string::npos && (curpos = str.rfind("<!{", curpos)) != std::string::npos) {
if (curpos != std::string::npos) {
size_t endvalpos = str.find("}**", curpos);
int endval = atoi(str.substr(curpos + 3, endvalpos).c_str());
if (endval > 1) {
char tmpstr[64];
sprintf(tmpstr, "<!{%d}**", endval - 1);
str.replace(curpos, endvalpos - curpos + 3, tmpstr);
sellength += (strlen(tmpstr) - (endvalpos - curpos + 3));
}
else {
str.replace(curpos, endvalpos - curpos + 3, "<!--");
sellength += (4 - (endvalpos - curpos + 3));
}
}
--curpos;
}
curpos = sellength;
while (curpos != std::string::npos && (curpos = str.rfind("**{", curpos)) != std::string::npos) {
if (curpos != std::string::npos) {
size_t endvalpos = str.find("}>", curpos);
int endval = atoi(str.substr(curpos + 3, endvalpos).c_str());
if (endval > 1) {
char tmpstr[64];
sprintf(tmpstr, "**{%d}>", endval - 1);
str.replace(curpos, endvalpos - curpos + 2, tmpstr);
sellength += (strlen(tmpstr) - (endvalpos - curpos + 2));
}
else {
str.replace(curpos, endvalpos - curpos + 2, "-->");
sellength += (3 - (endvalpos - curpos + 2));
}
}
--curpos;
}
// Replace the selection with new string
::SendMessage(hCurrentEditView, SCI_REPLACESEL, 0, reinterpret_cast<LPARAM>(str.c_str()));
// Defines selection without scrolling
::SendMessage(hCurrentEditView, SCI_SETCURRENTPOS, selstart, 0);
::SendMessage(hCurrentEditView, SCI_SETANCHOR, selstart + sellength, 0);
str.clear();
}