-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathxslLexerRenameTag.ts
151 lines (132 loc) · 5.69 KB
/
xslLexerRenameTag.ts
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
/**
* Copyright (c) 2020 DeltaXML Ltd. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the MIT license
* which accompanies this distribution.
*
* Contributors:
* DeltaXML Ltd. - XPath/XSLT Lexer/Syntax Highlighter
*/
import { XslLexer, XMLCharState, GlobalInstructionType, XSLTokenLevelState, EntityPosition, GlobalInstructionData} from "./xslLexer";
import { BaseToken} from "./xpLexer";
import * as vscode from 'vscode';
export class XslLexerRenameTag extends XslLexer {
public renameTag(xsl: string, renamePos: vscode.Position): vscode.TextEdit|null {
this.globalInstructionData = [];
this.globalModeData = [];
let renameStartChar = renamePos.character;
let renameLine = renamePos.line;
let currentState: XMLCharState = XMLCharState.init;
let currentChar: string = '';
let tokenChars: string[] = [];
let xslLength = xsl.length - 1;
let storeToken = false;
let xmlElementStack: number = 0;
let lCharCount = -1;
let lineNumber = 0;
let lineNumberChar = -1;
let renameName = '';
let renameStackLength = -1;
let breakLoop = false;
let foundStartTag = false;
while (lCharCount < xslLength + 1) {
if (breakLoop) {
console.log('breaking loop');
}
lCharCount++;
lineNumberChar++;
let nextState: XMLCharState = XMLCharState.init;
let nextChar: string = xsl.charAt(lCharCount);
if (currentChar) {
let isCurrentCharNewLIne = currentChar === '\n';
if (isCurrentCharNewLIne) {
lineNumberChar = 0;
lineNumber++;
if (lineNumber > renameLine && renameName !== '') {
breakLoop = true;
}
}
nextState = this.calcNewState(
isCurrentCharNewLIne,
currentChar,
nextChar,
currentState,
);
if (nextState === currentState) {
if (isCurrentCharNewLIne) {
// do nothing
} else if (storeToken) {
tokenChars.push(currentChar);
}
} else {
switch (nextState) {
case XMLCharState.lSt:
storeToken = renameLine === lineNumber && renameName === '';
breakLoop = (renameName === '' && lineNumberChar >= renameStartChar);
break;
case XMLCharState.rSt:
if (storeToken) {
if (lineNumberChar < renameStartChar) {
renameName = tokenChars.join('');
}
}
xmlElementStack++;
storeToken = false;
tokenChars = [];
break;
case XMLCharState.rSelfCt:
break;
case XMLCharState.lCtName:
if (xmlElementStack > 0) {
xmlElementStack--;
}
if (renameName !== '' && xmlElementStack === renameStackLength) {
tokenChars.push(currentChar);
storeToken = true;
}
console.log('start of the close tag name')
break;
case XMLCharState.lEn:
if (storeToken) {
tokenChars.push(currentChar);
}
break;
case XMLCharState.lsElementNameWs:
case XMLCharState.rStNoAtt:
if (lineNumberChar >= renameStartChar) {
if (storeToken) {
renameName = tokenChars.join('');
renameStackLength = xmlElementStack;
}
} else if (renameName === '') {
breakLoop = true;
}
storeToken = false;
tokenChars = [];
if (nextState === XMLCharState.rStNoAtt) {
xmlElementStack++;
}
break;
case XMLCharState.rSelfCtNoAtt:
storeToken = false;
tokenChars = [];
break;
case XMLCharState.rCt:
if (xmlElementStack === renameStackLength) {
let closeTag = tokenChars.join('');
if (renameName === closeTag) {
console.log('found');
}
}
storeToken = false;
tokenChars = [];
break;
}
} // else ends
currentState = nextState;
}
currentChar = nextChar;
}
return null;
}
}