This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
extension.ts
152 lines (137 loc) · 5.48 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
'use strict';
import * as vscode from 'vscode';
import Window = vscode.window;
import QuickPickItem = vscode.QuickPickItem;
import QuickPickOptions = vscode.QuickPickOptions;
import Document = vscode.TextDocument;
import Position = vscode.Position;
import Range = vscode.Range;
import Selection = vscode.Selection;
import TextDocument = vscode.TextDocument;
import TextEditor = vscode.TextEditor;
//import InputBoxOptions = InputBoxOptions;
var figlet = require('figlet');
import us = require('underscore.string');
export function activate() {
console.log('Congratulations, your extension "TextTools" is now active!');
vscode.commands.registerCommand('extension.textFunctions', textFunctions);
}
// String Functions Helper//////////////////////////////
function toUpper(e: TextEditor, d: TextDocument, sel: Selection[]) {
e.edit(function (edit) {
// itterate through the selections and convert all text to Upper
for (var x = 0; x < sel.length; x++) {
let txt: string = d.getText(new Range(sel[x].start, sel[x].end));
edit.replace(sel[x], txt.toUpperCase());
}
});
}
function toLower(e: TextEditor, d: TextDocument, sel: Selection[]) {
e.edit(function (edit) {
// itterate through the selections and convert all text to Lower
for (var x = 0; x < sel.length; x++) {
let txt: string = d.getText(new Range(sel[x].start, sel[x].end));
edit.replace(sel[x], txt.toLowerCase());
}
});
}
// This function takes a callback function for the text formatting 'formatCB',
// if there are any args pass an array as 'argsCB'
function processSelection(e: TextEditor, d: TextDocument, sel: Selection[], formatCB, argsCB) {
var replaceRanges: Selection[] = [];
e.edit(function (edit) {
// itterate through the selections
for (var x = 0; x < sel.length; x++) {
let txt: string = d.getText(new Range(sel[x].start, sel[x].end));
if (argsCB.length > 0) {
// in the case of figlet the params are test to change and font so this is hard coded
// the idea of the array of parameters is to allow for a more general approach in the future
txt = formatCB.apply(this, [txt, argsCB[0]]);
} else {
txt = formatCB(txt);
}
//replace the txt in the current select and work out any range adjustments
edit.replace(sel[x], txt);
let startPos: Position = new Position(sel[x].start.line, sel[x].start.character);
let endPos: Position = new Position(sel[x].start.line + txt.split(/\r\n|\r|\n/).length - 1, sel[x].start.character + txt.length);
replaceRanges.push(new Selection(startPos, endPos));
}
});
e.selections = replaceRanges;
}
// Main menu /////////////////////////////////////
function textFunctions() {
if (!vscode.window.activeTextEditor) {
vscode.window.showInformationMessage('Open a file first to manipulate text selections');
return;
}
var opts: QuickPickOptions = { matchOnDescription: true, placeHolder: "What do you want to do to the selection(s)?" };
var items: QuickPickItem[] = [];
items.push({ label: "toUpper", description: "Convert [aBc] to [ABC]" });
items.push({ label: "toLower", description: "Convert [aBc] to [abc]" });
items.push({ label: "swapCase", description: "Convert [aBc] to [AbC]" });
items.push({ label: "Titleize", description: "Convert [hello MD tools] to [Hello MD Tools]" });
items.push({ label: "Camelize", description: "Convert [hello MD-tools] to [HelloMDTools]" });
items.push({ label: "Clean String", description: "Convert [hello......world] to [hello world]" });
items.push({ label: "Reverse", description: "Convert [hello world] to [world hello]" });
items.push({ label: "Escape HTML", description: "Convert [<div>hello] to [<div>hello]" });
items.push({ label: "UnEscape HTML", description: "Convert [<div>hello] to [<div>hello]" });
items.push({ label: "Slugify", description: "Convert [txt for an URL] to [txt-for-an-url]" });
items.push({ label: "ASCII Art", description: "Convert [hello] to ASCII Art" });
Window.showQuickPick(items).then((selection) => {
if (!selection) {
return;
}
let e = Window.activeTextEditor;
let d = e.document;
let sel = e.selections;
switch (selection.label) {
case "toUpper":
toUpper(e, d, sel);
break;
case "toLower":
toLower(e, d, sel);
break;
case "swapCase":
processSelection(e, d, sel, us.swapCase, []);
break;
case "Titleize":
processSelection(e, d, sel, us.titleize, []);
break;
case "Clean String":
processSelection(e, d, sel, us.clean, []);
break;
case "Reverse":
processSelection(e, d, sel, us.reverse, []);
break;
case "Escape HTML":
processSelection(e, d, sel, us.escapeHTML, []);
break;
case "UnEscape HTML":
processSelection(e, d, sel, us.unescapeHTML, []);
break;
case "Camelize":
processSelection(e, d, sel, us.camelize, []);
break;
case "Slugify":
processSelection(e, d, sel, us.slugify, []);
break;
case "ASCII Art":
// build a full list of the fonts for the drop down
items = [];
figlet.fontsSync().forEach(function (font) {
items.push({ label: font, description: "User the " + font + " font" });
}, this);
Window.showQuickPick(items).then(function (selection) {
if (!selection) {
return;
}
processSelection(e, d, sel, figlet.textSync, [selection.label]);
});
break;
default:
console.log("hum this should not have happend - no selection")
break;
}
});
}