-
Notifications
You must be signed in to change notification settings - Fork 0
/
php_CODE_INSERT.js
123 lines (116 loc) · 3.77 KB
/
php_CODE_INSERT.js
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
// JavaScript Document
function canAcceptCommand() {
return (dw.getDocumentDOM() != null);
}
function receiveArguments() {
var dom = dw.getDocumentDOM();
var aOffSets2 = new Array();
var aOffSets1 = new Array();
var iOffSet2 = 0;
var iOffSet1 = 0;
var sCode = "";
var iOffSet = "";
var iNbTab1 = 0;
var iNbTab2 = 0;
var sNbTab1 = "";
var sNbTab2 = "";
var sNbTab3 = "";
var aLignes = new Array();
var iLigne = 0;
var sRC = "\r";
var sTab = "\t";
// ckecks arguments
if (arguments.length != 1) {
return;
}
// computes identation
aOffSets2 = dom.source.getSelection();
iOffSet2 = aOffSets2[0];
// dom.source.startOfLine(false) gives an error if no alert() before ?!
aLignes = dom.source.getCurrentLines(iOffSet2);
iLigne = aLignes[0];
dom.source.setCurrentLine(iLigne);
aOffSets1 = dom.source.getSelection();
iOffSet1 = aOffSets1[0];
iOffSet = iOffSet2 - iOffSet1;
iNbTab1 = iOffSet;
for (i=0; i<iNbTab1 ; i++) {
sNbTab1 += sTab;
}
sNbTab2 = sNbTab1 + sTab;
sNbTab3 = sNbTab2 + sTab;
// sets string to insert
switch (arguments[0]) {
case "if" :
sCode = "if () {" + sRC + sNbTab2 + sRC + sNbTab1 + "}";
break;
case "else" :
sCode = "else {" + sRC + sNbTab2 + sRC + sNbTab1 + "}";
break;
case "elseif" :
sCode = "elseif () {" + sRC + sNbTab2 + sRC + sNbTab1 + "}";
break;
case "ifelse" :
sCode = "if () {" + sRC + sNbTab2 + sRC + sNbTab1 + "}" + sRC + sNbTab1 + "else {" + sRC + sNbTab2 + sRC + sNbTab1 + "}";
break;
case "ifelseifelse" :
sCode = "if () {" + sRC + sNbTab2 + sRC + sNbTab1 + "}" + sRC + sNbTab1 + "elseif () {" + sRC + sNbTab2 + sRC + sNbTab1 + "}" + sRC + sNbTab1 + "else () {" + sRC + sNbTab2 + sRC + sNbTab1 + "}";
break;
case "switch" :
sCode = "switch () {" + sRC + sNbTab2 + "case :" + sRC + sNbTab3 + sRC + sNbTab3 + "break;" + sRC + sNbTab2 + "case :" + sRC + sNbTab3 + sRC + sNbTab3 + "break;" + sRC + sNbTab2 + "case :" + sRC + sNbTab3 + sRC + sNbTab3 + "break;" + sRC + sNbTab2 + "default :" + sRC + sNbTab3 + sRC + sNbTab1 + "}";
break;
case "for" :
sCode = "for ($ = 0; $ < ; $ ++) {" + sRC + sNbTab2 + sRC + sNbTab1 + "}";
break;
case "foreach" :
sCode = "foreach ($ As $ => $) {" + sRC + sNbTab2 + sRC + sNbTab1 + "}";
break;
case "while" :
sCode = "while () {" + sRC + sNbTab2 + sRC + sNbTab1 + "}";
break;
case "do" :
sCode = "do {" + sRC + sNbTab2 + sRC + sNbTab1 + "} while ();";
break;
case "function" :
sCode = "function () {" + sRC + sNbTab2 + sRC + sNbTab2 + "return ;" + sRC + sNbTab1 + "}";
break;
}
// writes string
dom.source.insert(iOffSet2, sCode);
// moves cursor
switch (arguments[0]) {
case "if" :
dom.source.setSelection(iOffSet2 + 4, iOffSet2 + 4);
break;
case "else" :
dom.source.setSelection(iOffSet2 + iNbTab1 + 9, iOffSet2 + iNbTab1 + 9);
break;
case "elseif" :
dom.source.setSelection(iOffSet2 + 8, iOffSet2 + 8);
break;
case "ifelse" :
dom.source.setSelection(iOffSet2 + 4, iOffSet2 + 4);
break;
case "ifelseifelse" :
dom.source.setSelection(iOffSet2 + 4, iOffSet2 + 4);
break;
case "switch" :
dom.source.setSelection(iOffSet2 + 8, iOffSet2 + 8);
break;
case "for" :
dom.source.setSelection(iOffSet2 + 6, iOffSet2 + 6);
break;
case "foreach" :
dom.source.setSelection(iOffSet2 + 10, iOffSet2 + 10);
break;
case "while" :
dom.source.setSelection(iOffSet2 + 7, iOffSet2 + 7);
break;
case "do" :
dom.source.setSelection(iOffSet2 + iNbTab1 + 7, iOffSet2 + iNbTab1 + 7);
break;
case "function" :
dom.source.setSelection(iOffSet2 + 9, iOffSet2 + 9);
break;
}
}