First with local

This commit is contained in:
2022-01-11 17:15:26 -07:00
commit ad0108f248
34 changed files with 3621 additions and 0 deletions

View File

@ -0,0 +1,55 @@
// 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 replaceLinesHelper from './replaceLinesHelper';
import * as readOnlyLinesHelper from './readOnlyLinesHelper';
// 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('readOnlyLinesHelper.searchGoogle', readOnlyLinesHelper.searchGoogle),
vscode.commands.registerCommand('replaceLinesHelper.addCSharpComment', replaceLinesHelper.addCSharpComment),
vscode.commands.registerCommand('replaceLinesHelper.addVBComment', replaceLinesHelper.addVBComment),
vscode.commands.registerCommand('replaceLinesHelper.convertToRegularExpression', replaceLinesHelper.convertToRegularExpression),
vscode.commands.registerCommand('replaceLinesHelper.cutEachLine', replaceLinesHelper.cutEachLine),
vscode.commands.registerCommand('replaceLinesHelper.expandSql', replaceLinesHelper.expandSql),
vscode.commands.registerCommand('replaceLinesHelper.listToListFamily', replaceLinesHelper.listToListFamily),
vscode.commands.registerCommand('replaceLinesHelper.listToListWrappedComma', replaceLinesHelper.listToListWrappedComma),
vscode.commands.registerCommand('replaceLinesHelper.prettySql', replaceLinesHelper.prettySql),
vscode.commands.registerCommand('replaceLinesHelper.quickFixCamelCaseProperties', replaceLinesHelper.quickFixCamelCaseProperties),
vscode.commands.registerCommand('replaceLinesHelper.quickFixCS0108', replaceLinesHelper.quickFixCS0108),
vscode.commands.registerCommand('replaceLinesHelper.quickFixInstanceFieldToCalisthenics', replaceLinesHelper.quickFixInstanceFieldToCalisthenics),
vscode.commands.registerCommand('replaceLinesHelper.quickFixProperCaseProperties', replaceLinesHelper.quickFixProperCaseProperties),
vscode.commands.registerCommand('replaceLinesHelper.quickFixPublic', replaceLinesHelper.quickFixPublic),
vscode.commands.registerCommand('replaceLinesHelper.removeComment', replaceLinesHelper.removeComment),
vscode.commands.registerCommand('replaceLinesHelper.sortLength', replaceLinesHelper.sortLength),
vscode.commands.registerCommand('replaceLinesHelper.sortNormal', replaceLinesHelper.sortNormal),
vscode.commands.registerCommand('replaceLinesHelper.unwrapSql', replaceLinesHelper.unwrapSql),
vscode.commands.registerCommand('replaceLinesHelper.wrapSqlCSharp', replaceLinesHelper.wrapSqlCSharp),
vscode.commands.registerCommand('replaceLinesHelper.wrapSqlVB', replaceLinesHelper.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,51 @@
import * as vscode from 'vscode';
enum LinesAction {
searchGoogle
}
function searchGoogleLogic(text: string, lines: string[]): void {
if (text.length > 0) {
vscode.env.openExternal(
vscode.Uri.parse(`https://www.google.com/search?q=${text.trim()}`)
)
}
else if (lines.length > 0) {
vscode.env.openExternal(
vscode.Uri.parse(`https://www.google.com/search?q=${lines[0].trim()}`)
)
}
}
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 linesFunction(linesAction: LinesAction): Thenable<boolean> | undefined {
const textEditor = vscode.window.activeTextEditor;
if (!textEditor) {
return undefined;
}
let text = '';
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 range = new vscode.Range(selection.start, selection.end)
text = textEditor.document.getText(range);
}
let lines: string[] = getLines(textEditor, startLine, endLine);
switch (linesAction) {
case LinesAction.searchGoogle: { searchGoogleLogic(text, lines); break; }
default: { throw new Error(); }
}
return undefined;
}
export const searchGoogle = () => linesFunction(LinesAction.searchGoogle);

View File

@ -0,0 +1,414 @@
import * as vscode from 'vscode';
type ArrayTransformer = (lines: string[]) => string[];
type SortingAlgorithm = (a: string, b: string) => number;
enum LinesAction {
addCSharpComment,
addVBComment,
convertToRegularExpression,
cutEachLine,
expandSql,
listToListFamily,
listToListWrappedComma,
prettySql,
quickFixCamelCaseProperties,
quickFixCS0108,
quickFixInstanceFieldToCalisthenics,
quickFixProperCaseProperties,
quickFixPublic,
pdsfToFixedWidth,
removeComment,
sortLength,
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('\\|').
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 listToListFamilyLogic(lines: string[]): void {
for (let i = 0; i < lines.length; ++i) {
lines[i] = "Kristy" + lines[i].trim() + "Mike" + lines[i].trim() + "Jason" + lines[i].trim() + "Mackenzie" + lines[i].trim() + "Logan" + lines[i].trim() + "Lilly" + lines[i].trim() + "Chelsea" + lines[i].trim() + "Piper" + lines[i].trim();
}
}
function listToListWrappedCommaLogic(lines: string[]): void {
for (let i = 0; i < lines.length; ++i) {
lines[i] = "'" + lines[i].trim() + "',";
}
}
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 camelCase(str: string) {
return str.replace(/(?:^\w|[A-Z]|\b\w|\s+)/g, function(match, index) {
if (+match === 0) return "";
return index === 0 ? match.toLowerCase() : match.toUpperCase();
});
}
function toCapitalizedWords(name: string) {
let words = name.match(/[A-Za-z][a-z]*|[0-9]+/g) || [];
return words.map(capitalize).join(" ");
}
function capitalize(word: string) {
return word.charAt(0).toUpperCase() + word.substring(1);
}
function quickFixCamelCasePropertiesLogic(lines: string[]): void {
for (let i = 0; i < lines.length; ++i) {
let segments = lines[i].trim().split(' ');
if(segments.length < 2 || segments.length > 3)
continue;
let leftPadding = lines[i].substring(0, lines[i].indexOf(segments[0]));
if (segments.length === 2) segments = ('protected ' + lines[i].trim()).split(' ');
let camelCased = camelCase(segments[2]);
lines[i] = leftPadding + segments[0] + ' ' + segments[1] + ' ' + camelCased;
}
removeBlanks(lines);
}
function quickFixCS0108Logic(lines: string[]): void {
for (let i = 0; i < lines.length; ++i) {
let segments = lines[i].trim().split(' ');
if(segments.length < 2 || segments.length > 3)
continue;
let leftPadding = lines[i].substring(0, lines[i].indexOf(segments[0]));
if (segments.length === 2) segments = ('protected ' + lines[i].trim()).split(' ');
let camelCased = camelCase(segments[2]);
let properCased = camelCased.substring(0, 1).toUpperCase() + camelCased.substring(1, camelCased.length - 1);
let capitalizedWords = toCapitalizedWords(properCased);
lines[i] = leftPadding + '[Display(Name = "' + capitalizedWords + '"), Required] public new ' + segments[1] + ' ' + properCased + ' { get; set; }';
}
removeBlanks(lines);
}
function quickFixInstanceFieldToCalisthenicsLogic(lines: string[]): void {
for (let i = 0; i < lines.length; ++i) {
let segments = lines[i].trim().split(' ');
if(segments.length !== 2)
continue;
let leftPadding = lines[i].substring(0, lines[i].indexOf(segments[0]));
let type = segments[0];
let name = segments[1].split(';')[0];
let singularName = name;
if(name[name.length - 1] === 's') {
if(name[name.length - 2] === 'e') {
if(name[name.length - 3] === 'i') {
singularName = name.substring(0, singularName.length - 3) + 'y';
}
else
singularName = name.substring(0, singularName.length - 2);
}
else
singularName = name.substring(0, singularName.length - 1);
}
segments = type.split('<');
if(segments.length === 2)
lines[i] = leftPadding + 'public ' + segments[0] + '<' + singularName + '> ' + name + ' { get; } //' + segments[1].split('>')[0];
else {
segments = type.split('[');
if(segments.length === 2)
lines[i] = leftPadding + 'public ' + singularName + '[] ' + name + ' { get; } //' + segments[0];
else
lines[i] = leftPadding + 'public ' + singularName + ' ' + name + ' { get; } //' + type;
}
}
removeBlanks(lines);
}
function quickFixProperCasePropertiesLogic(lines: string[]): void {
for (let i = 0; i < lines.length; ++i) {
let segments = lines[i].trim().split(' ');
if(segments.length < 2 || segments.length > 3)
continue;
let leftPadding = lines[i].substring(0, lines[i].indexOf(segments[0]));
if (segments.length === 2) segments = ('protected ' + lines[i].trim()).split(' ');
let camelCased = camelCase(segments[2]);
let properCased = camelCased.substring(0, 1).toUpperCase() + camelCased.substring(1, camelCased.length - 1);
lines[i] = leftPadding + segments[0] + ' ' + segments[1] + ' ' + properCased + ';';
}
removeBlanks(lines);
}
function quickFixPublicLogic(lines: string[]): void {
for (let i = 0; i < lines.length; ++i) {
let segments = lines[i].trim().split(' ');
if(segments.length < 2 || segments.length > 3)
continue;
let leftPadding = lines[i].substring(0, lines[i].indexOf(segments[0]));
if (segments.length === 2) segments = ('protected ' + lines[i].trim()).split(' ');
let camelCased = camelCase(segments[2]);
let properCased = camelCased.substring(0, 1).toUpperCase() + camelCased.substring(1, camelCased.length - 1);
lines[i] = leftPadding + 'public ' + segments[1] + ' ' + properCased + ' => ' + camelCased;
}
removeBlanks(lines);
}
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 makeSorter(algorithm?: SortingAlgorithm): ArrayTransformer {
return function (lines: string[]): string[] {
return lines.sort(algorithm);
};
}
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 sortLengthLogic(lines: string[]): void {
lines.sort(function (a, b) {
// ASC -> a.length - b.length
// DESC -> b.length - a.length
return a.length - b.length || // sort by length, if equal then
a.localeCompare(b); // sort by dictionary order
});
removeBlanks(lines);
}
function cutEachLineLogic(lines: string[]): void {
for (let i = 0; i < lines.length; ++i) {
if (lines[i].length > 0 && lines[i].indexOf("|||") > 0) {
lines[i] = lines[i].substr(0, lines[i].indexOf("|||"));
}
lines[i] = lines[i].trim();
}
removeBlanks(lines);
}
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.cutEachLine: { cutEachLineLogic(lines); break; }
case LinesAction.expandSql: { expandSqlLogic(lines); break; }
case LinesAction.listToListFamily: { listToListFamilyLogic(lines); break; }
case LinesAction.listToListWrappedComma: { listToListWrappedCommaLogic(lines); break; }
case LinesAction.prettySql: { prettySqlLogic(lines); break; }
case LinesAction.quickFixCamelCaseProperties: { quickFixCamelCasePropertiesLogic(lines); break; }
case LinesAction.quickFixCS0108: { quickFixCS0108Logic(lines); break; }
case LinesAction.quickFixInstanceFieldToCalisthenics: { quickFixInstanceFieldToCalisthenicsLogic(lines); break; }
case LinesAction.quickFixProperCaseProperties: { quickFixProperCasePropertiesLogic(lines); break; }
case LinesAction.quickFixPublic: { quickFixPublicLogic(lines); break; }
case LinesAction.removeComment: { removeCommentLogic(lines); break; }
case LinesAction.sortLength: { sortLengthLogic(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 cutEachLine = () => linesFunction(LinesAction.cutEachLine);
export const expandSql = () => linesFunction(LinesAction.expandSql);
export const listToListFamily = () => linesFunction(LinesAction.listToListFamily);
export const listToListWrappedComma = () => linesFunction(LinesAction.listToListWrappedComma);
export const prettySql = () => linesFunction(LinesAction.prettySql);
export const quickFixCamelCaseProperties = () => linesFunction(LinesAction.quickFixCamelCaseProperties);
export const quickFixCS0108 = () => linesFunction(LinesAction.quickFixCS0108);
export const quickFixInstanceFieldToCalisthenics = () => linesFunction(LinesAction.quickFixInstanceFieldToCalisthenics);
export const quickFixProperCaseProperties = () => linesFunction(LinesAction.quickFixProperCaseProperties);
export const quickFixPublic = () => linesFunction(LinesAction.quickFixPublic);
export const pdsfToFixedWidth = () => linesFunction(LinesAction.pdsfToFixedWidth);
export const removeComment = () => linesFunction(LinesAction.removeComment);
export const sortLength = () => linesFunction(LinesAction.sortLength);
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);
}
});
});
}