-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathXFinder.xcsm
409 lines (365 loc) · 13.9 KB
/
XFinder.xcsm
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
//xlang Source, Name:XFinder.xcsm
//Date: Mon Sep 13:36:15 2018
class XFinder{
public static class FindItem{
public String file;
public int position;
public int line, row;
public int type;
public int findlen;
public String findres;
public int method; //0:poslocation 1:linerowlocation
public FindItem(String _file, int p, int l, String s){
file = _file;
position = p;
findlen = l;
findres = s;
method = 0;
}
public FindItem(String _file, int _l, int _r, int l, String s){
file = _file;
findlen = l;
line = _l;
row = _r;
findres = s;
method = 1;
}
};
public static class FindResult : Vector<FindItem>{
public void offset(int ofst){
for (int i = 0 , c = size(); i < c ; i++){
FindItem fi = operator[](i);
fi.position += ofst;
}
}
};
public static String readFileContent(@NotNilptr String file){
FileInputStream fis = nilptr;
try{
fis = new FileInputStream(file);
byte [] data = fis.read();
try{
return new String(data);
}catch(Exception e){
}
}catch(Exception e){
}finally{
if (fis != nilptr){
fis.close();
}
}
return nilptr;
}
public static bool writeFileContent(@NotNilptr String file,@NotNilptr String content){
FileOutputStream fis = nilptr;
try{
fis = new FileOutputStream(file);
byte [] data = content.getBytes();
fis.write(data);
return true;
}catch(Exception e){
}finally{
if (fis != nilptr){
fis.close();
}
}
return false;
}
public static String getFileContent(@NotNilptr String file){
String content = XSourceEditor.getEditingContent(file);
if (content == nilptr){
content = readFileContent(file);
}
return content;
}
public static const int
SCOPE_CURRENT = 0,
SCOPE_ALLOPENED = 1,
SCOPE_PROJECT = 2,
SCOPE_SELECTED = 3,
SCOPE_OUTPUT = 4;
public static void find(@NotNilptr FindResult result, Project project, bool bcase, bool wholeWord, bool regex,String id, String specText, @NotNilptr String [] extsions, int scope,@NotNilptr String findTxt, Pattern pattern, bool forward, bool bOnce, bool bNext){
if (scope == SCOPE_PROJECT && project != nilptr){
if (bNext){
result.clear();
}
for (int i = 0, c = project.getSourceFileCount(); i < c; i++){
String path = project.getSourcePath(i);
if (path != nilptr){
if (findInFile(result, path, bcase, wholeWord, regex, extsions, findTxt, pattern, forward, bOnce) && bOnce){
return;
}
}
}
}else
if (scope == SCOPE_ALLOPENED){
if (bNext){
result.clear();
}
Map.Iterator<String, DocumentView> iter = DocumentView.editorMgr.iterator();
while (iter.hasNext()){
String file = iter.getKey();
if (file != nilptr){
if (findInFile(result, file, bcase, wholeWord, regex, extsions, findTxt, pattern, forward, bOnce) && bOnce){
return;
}
}
iter.next();
}
}else
if (scope == SCOPE_CURRENT){
DocumentView docview = (DocumentView)XWorkspace.workspace.currentSubWindow();
if (docview != nilptr){
String fileContent = "";
int ofst = 0;
if (bNext == false){
fileContent = docview.contentToString();
}else{
if (forward){
ofst = result[0].position;
if (ofst == 0){
ofst = docview.getContentLength();
}
fileContent = docview.getText(0, ofst);
ofst = 0;
}else{
ofst = result[0].position + result[0].findlen;
fileContent = docview.getText(ofst, docview.getContentLength());
}
result.clear();
}
if (fileContent != nilptr){
findInText(result, docview.getFilePath(), fileContent, bcase, wholeWord, regex, findTxt, pattern, forward, bOnce);
if (ofst != 0){
result.offset(ofst);
}
}
}else
if (bNext){
result.clear();
}
}else
if (scope == SCOPE_SELECTED){
result.clear();
findInText(result, id, specText, bcase, wholeWord, regex, findTxt, pattern, forward, bOnce);
}else
if (scope == SCOPE_OUTPUT){
if (bNext){
result.clear();
}
QScintilla sci = XWndOutput.outputWnd.getCurrentSci();
if (sci != nilptr){
String fileContent = sci.getText();
if (fileContent != nilptr){
findInText(result, "输出", fileContent, bcase, wholeWord, regex, findTxt, pattern, forward, bOnce);
}
}
}
}
public static bool testExtsion(@NotNilptr String file , @NotNilptr String [] extsions){
for (int i = 0; i < extsions.length; i++){
if (file.lower().endsWith(extsions[i].lower())){
return true;
}
}
return false;
}
public static bool findInFile(@NotNilptr FindResult result,@NotNilptr String file, bool bcase, bool wholeWord, bool regex, String [] extsions,@NotNilptr String findTxt, Pattern pattern,bool forward, bool bOnce){
if (extsions != nilptr && testExtsion(file, extsions) == false){
return false;
}
String fileContent = getFileContent(file);
if (fileContent != nilptr){
return findInText(result, file, fileContent, bcase, wholeWord, regex, findTxt, pattern, forward, bOnce);
}
return false;
}
public static bool findInText(@NotNilptr FindResult result, String file, @NotNilptr String text, bool bcase, bool wholeWord, bool regex,@NotNilptr String findTxt, Pattern pattern, bool forward, bool bOnce){
if ((text == nilptr) || (text.length() == 0)){
return false;
}
if (regex ){
if (pattern != nilptr){
if (bOnce){
return findRegexOnce(result, file, text, pattern);
}else{
return findRegex(result, file, text, pattern);
}
}
}else
if (bcase){
if (wholeWord){
return findTextWholeWord(result, file, text, text, findTxt, forward, bOnce);
}else{
return findText(result, file, text, text, findTxt, forward, bOnce);
}
}else{
if (wholeWord){
return findTextWholeWord(result, file, text, text.lower(), findTxt.lower(), forward, bOnce);
}else{
return findText(result, file, text, text.lower(), findTxt.lower(), forward, bOnce);
}
}
return false;
}
/*public static String replaceInText(@NotNilptr FindResult result, String file, @NotNilptr String text, bool bcase, bool wholeWord, bool regex,@NotNilptr String findTxt,@NotNilptr String replaceTxt, Pattern pattern){
findInText(result, file, text, bcase, wholeWord, regex, findTxt, pattern, true);
if (result != nilptr){
if (pattern != nilptr){
replaceTxt = replaceTxt.escape();
}
for(int i = result.size() - 1; i >= 0; i--){
FindItem fi = result[i];
replaceOnFile(fi, replaceTxt);
}
}
return text;
}*/
public static bool findRegex(@NotNilptr FindResult result,String file,@NotNilptr String text,@NotNilptr Pattern findTxt){
Pattern.Result
rt = findTxt.matchAll(text,0, -1, Pattern.NOTEMPTY);
bool rst = false;
for (int i = 0, c = rt.length(); i < c; i++){
int sl = rt.get(i).start();
int el = rt.get(i).end();
result.add(new FindItem(file, sl, el - sl, text.substring(sl,el)));
rst = true;
}
return rst;
}
public static bool findRegexOnce(@NotNilptr FindResult result,String file,@NotNilptr String text,@NotNilptr Pattern findTxt){
bool rst = false;
Pattern.Result patres = new Pattern.Result();
if (0 <= findTxt.match(text, patres, 0, -1, Pattern.NOTEMPTY)){
for (int i = 0, c = patres.length(); i < c; i++){
int sl = patres.get(i).start();
int el = patres.get(i).end();
result.add(new FindItem(file, sl, el - sl, text.substring(sl,el)));
rst = true;
}
}
return rst;
}
public static String cutString(@NotNilptr String text, int sl, int el){
sl = Math.max(sl - 20, 0);
el = Math.min(text.length() - 1, el + 20);
String out = text.substring(sl, el + 1);
out = out.replace("\t", " ");
out = out.replace("\r", " ");
out = out.replace("\n", " ");
return "..." + out + "...";
}
public static bool isWordChar(char c){
if (c == '_' || (c >='a' && c <= 'z') || (c >='A' && c <= 'Z') || (c >='0' && c <= '9')){
return true;
}
return false;
}
public static bool findText(@NotNilptr FindResult result,String file,@NotNilptr String originalText ,@NotNilptr String text,@NotNilptr String findTxt, bool forward, bool bOnce){
bool rst = false;
if (forward == false){
int findpos = -1;
int findlen = findTxt.length();
while ((findpos = text.indexOf(findTxt, findpos + 1)) != -1){
result.add(new FindItem(file, findpos, findlen, cutString(originalText, findpos, findpos + findlen - 1)));
rst = true;
if (bOnce){
break;
}
}
}else{
int findlen = findTxt.length();
int findpos = text.length();
while ((findpos = text.lastIndexOf(findTxt, findpos - 1)) != -1){
result.add(new FindItem(file, findpos, findlen, cutString(originalText, findpos, findpos + findlen - 1)));
rst = true;
if (bOnce){
break;
}
}
}
return rst;
}
public static bool findTextWholeWord(@NotNilptr FindResult result,String file,@NotNilptr String originalText ,@NotNilptr String text,@NotNilptr String findTxt, bool forward, bool bOnce){
bool rst = false;
if (forward == false){
int findpos = -1;
int findlen = findTxt.length();
while ((findpos = text.indexOf(findTxt, findpos + 1)) != -1){
if (findpos > 0){
char s = text.charAt(findpos - 1);
if (isWordChar(s)){
continue;
}
}
if ((findpos + findlen) < text.length()){
char e = text.charAt(findpos + findlen);
if (isWordChar(e)){
continue;
}
}
result.add(new FindItem(file, findpos, findTxt.length(), cutString(originalText, findpos, findpos + findTxt.length() - 1)));
rst = true;
if (bOnce){
break;
}
}
}
else{
int findlen = findTxt.length();
int findpos = text.length();
while ((findpos = text.lastIndexOf(findTxt, findpos - 1)) != -1){
if (findpos > 0){
char s = text.charAt(findpos - 1);
if (isWordChar(s)){
continue;
}
}
if ((findpos + findlen) < text.length()){
char e = text.charAt(findpos + findlen);
if (isWordChar(e)){
continue;
}
}
result.add(new FindItem(file, findpos, findTxt.length(), cutString(originalText, findpos, findpos + findTxt.length() - 1)));
rst = true;
if (bOnce){
break;
}
}
}
return rst;
}
public static void replace(@NotNilptr FindResult result,@NotNilptr String replaceStr, bool bforward){
if (bforward){
for (int i = 0 ; i < result.size(); i++){
FindItem item = result.get(i);
if (item != nilptr){
replaceOnFile(item, replaceStr);
}
}
}else{
for (int i = result.size() - 1; i >= 0; i--){
FindItem item = result.get(i);
if (item != nilptr){
replaceOnFile(item, replaceStr);
}
}
}
}
public static void replaceOnFile(@NotNilptr FindItem item,@NotNilptr String replaceStr){
DocumentView wnd = DocumentView.findDocumentWindow(nilptr, item.file, false);
if (wnd != nilptr){
wnd.replaceText(item.position, item.findlen, replaceStr);
}else{
String content = readFileContent(item.file);
if (content != nilptr){
try{
content = content.replace(item.position, item.position + item.findlen, replaceStr);
writeFileContent(item.file, content);
}catch(Exception e){
}
}
}
}
};