-
Notifications
You must be signed in to change notification settings - Fork 16
/
extension.ts
165 lines (138 loc) · 5.75 KB
/
extension.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import * as util from 'util';
import * as vscode from 'vscode';
import * as base64 from './lib/base64';
import * as core from './lib/core';
import * as crockford32 from './lib/crockford32';
import * as htmlentities from './lib/htmlentities';
import * as jsonarray from './lib/jsonarray';
import * as md5 from './lib/md5';
import * as unicode from './lib/unicode';
import * as urlEncode from './lib/urlencode';
import * as xmlentities from './lib/xmlentities';
class Context {
public failedChanges: Change[] = [];
}
class Change {
private textEditor: vscode.TextEditor;
private transformer: core.Transformer;
private originalOffset: number;
private updatedSelectionStartOffset: number = -1;
private inputOutputLengthDelta: number = -1;
public originalSelection: vscode.Selection;
public updatedSelection: vscode.Selection;
public updatedOffset: number = -1;
public input: string = "";
public output: string = ""
constructor(textEditor: vscode.TextEditor, originalSelection: vscode.Selection, transformer: core.Transformer, originalOffset: number) {
this.textEditor = textEditor;
this.originalSelection = originalSelection;
this.updatedSelection = this.originalSelection;
this.transformer = transformer;
this.originalOffset = originalOffset;
}
public transformText(context: Context, edit: vscode.TextEditorEdit) {
let originalSelectionStartOffset = this.textEditor.document.offsetAt(this.originalSelection.start);
//let originalSelectionEndOffset = this.textEditor.document.offsetAt(this.originalSelection.end);
//let originalSelectionLength = originalSelectionEndOffset - originalSelectionStartOffset;
this.updatedSelectionStartOffset = originalSelectionStartOffset + this.originalOffset;
let range = new vscode.Range(this.originalSelection.start, this.originalSelection.end);
this.input = this.textEditor.document.getText(range);
if (this.transformer.check(this.input) == true) {
this.output = this.transformer.transform(this.input);
} else {
this.output = this.input;
context.failedChanges.push(this);
}
edit.replace(range, this.output);
this.inputOutputLengthDelta = this.output.length - this.input.length;
this.updatedOffset = this.originalOffset + this.inputOutputLengthDelta;
}
public updateSelection() {
if (this.updatedSelectionStartOffset != undefined && this.output != undefined)
{
let updatedSelectionStart = this.textEditor.document.positionAt(this.updatedSelectionStartOffset);
let updatedSelectionEnd = this.textEditor.document.positionAt(this.updatedSelectionStartOffset + this.output.length);
// Build and store the new selection.
this.updatedSelection = new vscode.Selection(updatedSelectionStart, updatedSelectionEnd);
}
}
}
function processSelections(textEditor: vscode.TextEditor, edit: vscode.TextEditorEdit, transformer: core.Transformer) {
// let document = textEditor.document;
let changes: Change[] = [];
let updatedSelections: vscode.Selection[] = [];
let context = new Context();
textEditor.edit((editBuilder) => {
for (let selectionIndex = 0; selectionIndex < textEditor.selections.length; selectionIndex++) {
let selection = textEditor.selections[selectionIndex];
let offset = 0;
if (selectionIndex != 0) {
let previousChange = changes[selectionIndex - 1];
offset = previousChange.updatedOffset;
}
let change = new Change(
textEditor,
selection,
transformer,
offset
);
changes[selectionIndex] = change;
change.transformText(context, editBuilder);
}
}).then(() => {
for (let changeIndex = 0; changeIndex < changes.length; changeIndex++) {
let change = changes[changeIndex];
change.updateSelection();
updatedSelections.push(change.updatedSelection);
}
textEditor.selections = updatedSelections;
}).then(() => {
if (context.failedChanges.length != 0) {
let message = util.format(
'%s selections could not be processed.',
context.failedChanges.length
);
vscode.window.showWarningMessage(message);
}
});
}
function selectAndApplyTransformation(textEditor: vscode.TextEditor, edit: vscode.TextEditorEdit) {
let transformers: core.Transformer[] = [
new base64.StringToBase64Transformer(),
new base64.Base64ToStringTransformer(),
new jsonarray.StringToJsonArrayTransformer(),
new jsonarray.Base64ToJsonArrayTransformer(),
new md5.StringToMD5Base64Transformer(),
new md5.StringToMD5HexTransformer(),
new htmlentities.StringToHtmlEntitiesTransformer(),
new htmlentities.StringToHtmlDecimalEntitiesTransformer(),
new htmlentities.HtmlEntitiesToStringTransformer(),
new xmlentities.StringToXmlEntitiesTransformer(),
new xmlentities.XmlEntitiesToStringTransformer(),
new crockford32.IntegerToCrockfordBase32Transformer(),
new crockford32.CrockfordBase32ToIntegerTransformer(),
new unicode.StringToUnicodeTransformer(),
new unicode.UnicodeToStringTransformer(),
new urlEncode.StringToEncodedUrlTransformer(),
new urlEncode.EncodedUrlToStringTransformer()
];
vscode.window.showQuickPick(transformers).then((transformer) => {
if (transformer != undefined)
{
processSelections(textEditor, edit, transformer);
}
});
}
function registerConvertSelectionCommands(context: vscode.ExtensionContext) {
let subtleConvertSelectionDisposable = vscode.commands.registerTextEditorCommand('extension.subtleConvertSelection', (textEditor, edit) => {
selectAndApplyTransformation(textEditor, edit);
});
let convertSelectionDisposable = vscode.commands.registerTextEditorCommand('extension.convertSelection', (textEditor, edit) => {
selectAndApplyTransformation(textEditor, edit);
});
context.subscriptions.push(subtleConvertSelectionDisposable);
context.subscriptions.push(convertSelectionDisposable);
}
export function activate(context: vscode.ExtensionContext) {
registerConvertSelectionCommands(context);
}