This commit is contained in:
2020-01-19 16:18:01 -07:00
commit 4d4ed174e9
27 changed files with 1694 additions and 0 deletions

View File

@ -0,0 +1,44 @@
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';
import * as helper from './helper';
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {
// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
console.log('Congratulations, your extension "type-script-helper" is now active!');
// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
// let disposable = vscode.commands.registerCommand('extension.helloWorld', () => {
// // The code you place here will be executed every time your command is executed
// // Display a message box to the user
// vscode.window.showInformationMessage('Hello World!');
// });
// context.subscriptions.push(disposable);
const commands = [
vscode.commands.registerCommand('helper.addCSharpComment', helper.addCSharpComment),
vscode.commands.registerCommand('helper.addVBComment', helper.addVBComment),
vscode.commands.registerCommand('helper.convertToRegularExpression', helper.convertToRegularExpression),
vscode.commands.registerCommand('helper.expandSql', helper.expandSql),
vscode.commands.registerCommand('helper.prettySql', helper.prettySql),
vscode.commands.registerCommand('helper.removeComment', helper.removeComment),
vscode.commands.registerCommand('helper.sortNormal', helper.sortNormal),
vscode.commands.registerCommand('helper.unwrapSql', helper.unwrapSql),
vscode.commands.registerCommand('helper.wrapSqlCSharp', helper.wrapSqlCSharp),
vscode.commands.registerCommand('helper.wrapSqlVB', helper.wrapSqlVB)
];
commands.forEach(command => context.subscriptions.push(command));
}
// this method is called when your extension is deactivated
export function deactivate() {}

View File

@ -0,0 +1,246 @@
import * as vscode from 'vscode';
import { stringify } from 'querystring';
type ArrayTransformer = (lines: string[]) => string[];
type SortingAlgorithm = (a: string, b: string) => number;
enum LinesAction {
addCSharpComment,
addVBComment,
convertToRegularExpression,
expandSql,
prettySql,
removeComment,
sortNormal,
unwrapSql,
wrapSqlCSharp,
wrapSqlVB
}
function removeBlanks(lines: string[]): void {
for (let i = 0; i < lines.length; ++i) {
if (lines[i].trim() === '') {
lines.splice(i, 1);
i--;
}
}
}
function getLines(textEditor: vscode.TextEditor, startLine: number, endLine: number) {
let results: string[] = [];
for (let i = startLine; i <= endLine; i++) {
results.push(textEditor.document.lineAt(i).text);
}
return results;
}
function addCSharpCommentLogic(lines: string[]): void {
for (let i = 0; i < lines.length; ++i) {
lines[i] = '//' + lines[i].trim();
}
}
function addVBCommentLogic(lines: string[]): void {
for (let i = 0; i < lines.length; ++i) {
lines[i] = "'" + lines[i].trim();
}
}
function convertToRegularExpressionLogic(lines: string[]): string[] {
let results: string[];
let result: string = "";
let explicitLines = 50;
for (let i = 0; i < lines.length - 1 && i < explicitLines; ++i) {
result = result + lines[i].trim().
split('\\').join('\\\\').
split('(').join('\\(').
split(')').join('\\)').
split('[').join('\\[').
split(']').join('\\]').
split('.').join('\\.').
split('*').join('\\*').
split('+').join('\\+').
split('?').join('\\?').
split('|').join('\\|').
split('$').join('\\$').
split('^').join('\\^') +
'~';
}
for (let i = explicitLines; i < lines.length - 1; ++i) {
result = result + '.*~';
}
result = result.
substring(0, result.length - 1).
split('~').join('\\r\\n\?\\s\*');
results = [result];
return results;
}
function expandSqlLogic(lines: string[]): void {
for (let i = 0; i < lines.length; ++i) {
lines[i] = ' ' + lines[i];
lines[i] = lines[i].
split(' select ').join(' SELECT\r\n').
split(' from ').join('\r\n FROM ').
split(' where ').join('\r\n WHERE ').
split(' and ').join('\r\n and ').
split(' join ').join('\r\n join ').
split(' left join ').join('\r\n left join ').
split(' on ').join('\r\n ON ').
split(' group by ').join('\r\n GROUP BY ').
split(' order by ').join('\r\n ORDER BY ').
split(' SELECT ').join(' SELECT\r\n').
split(' FROM ').join('\r\n FROM ').
split(' WHERE ').join('\r\n WHERE ').
split(' AND ').join('\r\n AND ').
split(' JOIN ').join('\r\n JOIN ').
split(' LEFT JOIN ').join('\r\n LEFT JOIN ').
split(' ON ').join('\r\n ON ').
split(' GROUP BY ').join('\r\n GROUP BY ').
split(' ORDER BY ').join('\r\n ORDER BY ').
split('\r\n\r\n').join('\r\n').
trim();
}
removeBlanks(lines);
prettySqlLogic(lines);
}
function prettySqlLogic(lines: string[]): void {
for (let i = 0; i < lines.length; ++i) {
lines[i] = ' ' + lines[i];
lines[i] = lines[i].
split(' select ').join(' SELECT ').
split(' distinct ').join(' DISTINCT ').
split(' as ').join(' AS ').
split(' from ').join(' FROM ').
split(' where ').join(' WHERE ').
split(' and ').join(' AND ').
split(' join ').join(' JOIN ').
split(' left join ').join(' LEFT JOIN ').
split(' on ').join(' ON ').
split(' group by ').join(' GROUP BY ').
split(' order by ').join(' ORDER BY ').
split('\r\n\r\n').join('\r\n').
trim();
}
removeBlanks(lines);
}
function makeSorter(algorithm?: SortingAlgorithm): ArrayTransformer {
return function (lines: string[]): string[] {
return lines.sort(algorithm);
};
}
function removeCommentLogic(lines: string[]): void {
for (let i = 0; i < lines.length; ++i) {
lines[i] = lines[i].trim();
if(lines[i].length > 1 && lines[i][0] === '/' && lines[i][1] === '/') {
lines[i] = lines[i].substr(2);
}
if(lines[i].length > 0 && lines[i][0] === "'") {
lines[i] = lines[i].substr(1);
}
}
}
function sortNormalLogic(lines: string[]): void {
var transformers: ArrayTransformer[] = [];
transformers.push(makeSorter());
removeBlanks(lines);
lines = transformers.reduce((currentLines, transform) => transform(currentLines), lines);
}
function unwrapSqlLogic(lines: string[]): void {
for (let i = 0; i < lines.length; ++i) {
lines[i] = "//" + lines[i].
split('sql = "').join('').
split('sql = sql & "').join('').
split('sql.Append("').join('').
split('.Append("').join('').
split('Append("').join('').
split('");').join('').
split('").').join('').
split('")').join('').
split('" & ').join('$').
split(' & "').join('$').
split('"').join('').
trim();
}
}
function wrapSqlCSharpLogic(lines: string[]): void {
for (let i = 0; i < lines.length; ++i) {
lines[i] = lines[i].trim();
if(lines[i].length > 1 && lines[i][0] === '/' && lines[i][1] === '/') {
lines[i] = lines[i].substr(2);
}
lines[i] = 'Append(" ' + lines[i] + ' ").';
}
if(lines.length > 0) {
lines[0] = 'sql.' + lines[0];
lines[lines.length - 1] = lines[lines.length - 1].split(' ").').join(' ");');
}
}
function wrapSqlVBLogic(lines: string[]): void {
for (let i = 0; i < lines.length; ++i) {
lines[i] = lines[i].trim();
if(lines[i].length > 0 && lines[i][0] === "'") {
lines[i] = lines[i].substr(1);
}
lines[i] = 'Append(" ' + lines[i] + ' ").';
}
if(lines.length > 0) {
lines[0] = 'sql.' + lines[0];
}
}
function returnLines(textEditor: vscode.TextEditor, startLine: number, endLine: number, lines: string[]) {
return textEditor.edit(editBuilder => {
const range = new vscode.Range(startLine, 0, endLine, textEditor.document.lineAt(endLine).text.length);
editBuilder.replace(range, lines.join('\n'));
});
}
function linesFunction(linesAction: LinesAction): Thenable<boolean> | undefined {
const textEditor = vscode.window.activeTextEditor;
if (!textEditor) {
return undefined;
}
var startLine = 0;
var endLine = textEditor.document.lineCount - 1;
const selection = textEditor.selection;
if (!selection.isEmpty) {
startLine = selection.start.line;
endLine = selection.end.line;
}
let lines: string[] = getLines(textEditor, startLine, endLine);
switch (linesAction) {
case LinesAction.addCSharpComment: { addCSharpCommentLogic(lines); break; }
case LinesAction.addVBComment: { addVBCommentLogic(lines); break; }
case LinesAction.convertToRegularExpression: { lines = convertToRegularExpressionLogic(lines); break; }
case LinesAction.expandSql: { expandSqlLogic(lines); break; }
case LinesAction.prettySql: { prettySqlLogic(lines); break; }
case LinesAction.removeComment: { removeCommentLogic(lines); break; }
case LinesAction.sortNormal: { sortNormalLogic(lines); break; }
case LinesAction.unwrapSql: { unwrapSqlLogic(lines); break; }
case LinesAction.wrapSqlCSharp: { wrapSqlCSharpLogic(lines); break; }
case LinesAction.wrapSqlVB: { wrapSqlVBLogic(lines); break; }
default: { throw new Error(); }
}
return returnLines(textEditor, startLine, endLine, lines);
}
export const addCSharpComment = () => linesFunction(LinesAction.addCSharpComment);
export const addVBComment = () => linesFunction(LinesAction.addVBComment);
export const convertToRegularExpression = () => linesFunction(LinesAction.convertToRegularExpression);
export const expandSql = () => linesFunction(LinesAction.expandSql);
export const prettySql = () => linesFunction(LinesAction.prettySql);
export const removeComment = () => linesFunction(LinesAction.removeComment);
export const sortNormal = () => linesFunction(LinesAction.sortNormal);
export const unwrapSql = () => linesFunction(LinesAction.unwrapSql);
export const wrapSqlCSharp = () => linesFunction(LinesAction.wrapSqlCSharp);
export const wrapSqlVB = () => linesFunction(LinesAction.wrapSqlVB);

View File

@ -0,0 +1,23 @@
import * as path from 'path';
import { runTests } from 'vscode-test';
async function main() {
try {
// The folder containing the Extension Manifest package.json
// Passed to `--extensionDevelopmentPath`
const extensionDevelopmentPath = path.resolve(__dirname, '../../');
// The path to test runner
// Passed to --extensionTestsPath
const extensionTestsPath = path.resolve(__dirname, './suite/index');
// Download VS Code, unzip it and run the integration test
await runTests({ extensionDevelopmentPath, extensionTestsPath });
} catch (err) {
console.error('Failed to run tests');
process.exit(1);
}
}
main();

View File

@ -0,0 +1,15 @@
import * as assert from 'assert';
// You can import and use all API from the 'vscode' module
// as well as import your extension to test it
import * as vscode from 'vscode';
// import * as myExtension from '../extension';
suite('Extension Test Suite', () => {
vscode.window.showInformationMessage('Start all tests.');
test('Sample test', () => {
assert.equal(-1, [1, 2, 3].indexOf(5));
assert.equal(-1, [1, 2, 3].indexOf(0));
});
});

View File

@ -0,0 +1,37 @@
import * as path from 'path';
import * as Mocha from 'mocha';
import * as glob from 'glob';
export function run(): Promise<void> {
// Create the mocha test
const mocha = new Mocha({
ui: 'tdd',
});
mocha.useColors(true);
const testsRoot = path.resolve(__dirname, '..');
return new Promise((c, e) => {
glob('**/**.test.js', { cwd: testsRoot }, (err, files) => {
if (err) {
return e(err);
}
// Add files to the test suite
files.forEach(f => mocha.addFile(path.resolve(testsRoot, f)));
try {
// Run the mocha test
mocha.run(failures => {
if (failures > 0) {
e(new Error(`${failures} tests failed.`));
} else {
c();
}
});
} catch (err) {
e(err);
}
});
});
}