266 lines
9.5 KiB
TypeScript
266 lines
9.5 KiB
TypeScript
import * as vscode from 'vscode';
|
|
|
|
function codeGeneratorQuickPickLogic(): undefined {
|
|
const textEditor = vscode.window.activeTextEditor;
|
|
if (!textEditor) {
|
|
return undefined;
|
|
}
|
|
let thenable = vscode.window.showQuickPick(
|
|
[
|
|
{
|
|
label: "Class name",
|
|
detail: "Segments Minus One (Object || Array)",
|
|
output: "%ClassName%"
|
|
}, {
|
|
label: "Class name (Camel-Cased)",
|
|
detail: "Segments Minus One (Object || Array)",
|
|
output: "%ClassNameCamelCased%"
|
|
}, {
|
|
label: "Class name (Camel-Cased and Plural)",
|
|
detail: "Segments Minus One (Object || Array)",
|
|
output: "%ClassNameCamelCasedPlural%"
|
|
}, {
|
|
label: "Class name (Plural)",
|
|
detail: "Segments Minus One (Object || Array)",
|
|
output: "%ClassNamePlural%"
|
|
}, {
|
|
label: "Key Without Brackets Segment at Level",
|
|
detail: "",
|
|
output: "%KeyWithoutBracketsSegmentAtLevel%"
|
|
}, {
|
|
label: "Name",
|
|
detail: "Segments Last only name (no path)",
|
|
output: "%Name%"
|
|
}, {
|
|
label: "Namespace",
|
|
detail: "Namespace",
|
|
output: "%namespace%"
|
|
}, {
|
|
label: "Name (Camel-Cased)",
|
|
detail: "Segments Last only name (no path)",
|
|
output: "%NameCamelCased%"
|
|
}, {
|
|
label: "Name (Camel-Cased and Plural)",
|
|
detail: "Segments Last only name (no path)",
|
|
output: "%NameCamelCasedPlural%"
|
|
}, {
|
|
label: "Name Detail",
|
|
detail: "",
|
|
output: "%NameDetail%"
|
|
}, {
|
|
label: "Name Detail (Camel-Cased)",
|
|
detail: "",
|
|
output: "%NameDetailCamelCased%"
|
|
}, {
|
|
label: "Name Detail (Humanized)",
|
|
detail: "",
|
|
output: "%NameDetailHumanized%"
|
|
}, {
|
|
label: "Name Detail (Humanized) *Collection",
|
|
detail: "",
|
|
output: "%NameDetailHumanizedCollection%"
|
|
}, {
|
|
label: "Name (Humanized)",
|
|
detail: "Segments Last only name (no path)",
|
|
output: "%NameHumanized%"
|
|
}, {
|
|
label: "Name (Null Segments)",
|
|
detail: "",
|
|
output: "%NameNullSegments%"
|
|
}, {
|
|
label: "Name (Null Segments) *Collection",
|
|
detail: "",
|
|
output: "%NameNullSegmentsCollection%"
|
|
}, {
|
|
label: "Name (Plural)",
|
|
detail: "Segments Last only name (no path)",
|
|
output: "%NamePlural%"
|
|
}, {
|
|
label: "Name Segments",
|
|
detail: "",
|
|
output: "%NameSegments%"
|
|
}, {
|
|
label: "Name Segments *Collection",
|
|
detail: "",
|
|
output: "%NameSegmentsCollection%"
|
|
}, {
|
|
label: "Path without Brackets (Singularized)",
|
|
detail: "",
|
|
output: "%PathWithoutBracketsSingularized%"
|
|
}, {
|
|
label: "Suggested Type",
|
|
detail: "Type based on values in *.json file",
|
|
output: "%SuggestedType%"
|
|
}, {
|
|
label: "Suggested Type Id",
|
|
detail: "",
|
|
output: "%SuggestedTypeId%"
|
|
}, {
|
|
label: "Suggested Type Id (Camel-Cased)",
|
|
detail: "",
|
|
output: "%SuggestedTypeIdCamelCased%"
|
|
}, {
|
|
label: "Type",
|
|
detail: "Segments Minus One (!Object && !Array)",
|
|
output: "%Type%"
|
|
}, {
|
|
label: "Type (Camel-Cased)",
|
|
detail: "Segments Minus One (!Object && !Array)",
|
|
output: "%TypeCamelCased%"
|
|
}
|
|
]
|
|
).then(item => {
|
|
if (!item) {
|
|
return;
|
|
}
|
|
const selection = textEditor.selection;
|
|
return textEditor.edit(editBuilder => {
|
|
var range;
|
|
if (selection.isEmpty) {
|
|
editBuilder.insert(selection.start, item.output)
|
|
}
|
|
else {
|
|
range = new vscode.Range(selection.start.line, selection.start.character, selection.end.line, selection.end.character);
|
|
editBuilder.replace(range, item.output);
|
|
}
|
|
});
|
|
});
|
|
return undefined;
|
|
}
|
|
|
|
function insertDateTimeLogic(): undefined {
|
|
const textEditor = vscode.window.activeTextEditor;
|
|
if (!textEditor) {
|
|
return undefined;
|
|
}
|
|
const selection = textEditor.selection;
|
|
textEditor.edit(editBuilder => {
|
|
var range;
|
|
const now: Date = new Date();
|
|
const time: number = now.getTime();
|
|
const year: number = now.getFullYear();
|
|
const start: Date = new Date(year, 0, 0);
|
|
const oneDay: number = 1000 * 60 * 60 * 24;
|
|
const timezoneOffset: number = now.getTimezoneOffset();
|
|
const diff: number = (time - start.getTime()) + ((start.getTimezoneOffset() - timezoneOffset) * 60 * 1000);
|
|
const seconds: number = time.valueOf() + timezoneOffset;
|
|
const day: number = Math.floor(diff / oneDay);
|
|
const epoch: number = seconds * 10000;
|
|
const ticks: number = epoch + 621355968000000000;
|
|
let season: string = year + "-";
|
|
if (day < 78) {
|
|
season += "0.Winter";
|
|
} else if (day < 124) {
|
|
season += "1.Spring";
|
|
} else if (day < 171) {
|
|
season += "2.Spring";
|
|
} else if (day < 217) {
|
|
season += "3.Summer";
|
|
} else if (day < 264) {
|
|
season += "4.Summer";
|
|
} else if (day < 309) {
|
|
season += "5.Fall";
|
|
} else if (day < 354) {
|
|
season += "6.Fall";
|
|
} else {
|
|
season += "7.Winter";
|
|
}
|
|
const dateText = seconds + " = " + ticks + " = " + season + " = " + now.toString();
|
|
if (selection.isEmpty) {
|
|
editBuilder.insert(selection.start, dateText)
|
|
}
|
|
else {
|
|
range = new vscode.Range(selection.start.line, selection.start.character, selection.end.line, selection.end.character);
|
|
editBuilder.replace(range, dateText);
|
|
}
|
|
});
|
|
return undefined;
|
|
}
|
|
|
|
function insertAllExtensionsLogic(): undefined {
|
|
const textEditor = vscode.window.activeTextEditor;
|
|
if (!textEditor) {
|
|
return undefined;
|
|
}
|
|
const selection = textEditor.selection;
|
|
textEditor.edit(editBuilder => {
|
|
var range;
|
|
let extensions = vscode.extensions.all;
|
|
let text = extensions.map(extension => extension.id).join("\n");
|
|
if (selection.isEmpty) {
|
|
editBuilder.insert(selection.start, text)
|
|
}
|
|
else {
|
|
range = new vscode.Range(selection.start.line, selection.start.character, selection.end.line, selection.end.character);
|
|
editBuilder.replace(range, text);
|
|
}
|
|
});
|
|
return undefined;
|
|
}
|
|
|
|
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 transformToProperCaseLogic(): undefined {
|
|
const textEditor = vscode.window.activeTextEditor;
|
|
if (!textEditor) {
|
|
return undefined;
|
|
}
|
|
const selection = textEditor.selection;
|
|
textEditor.edit(editBuilder => {
|
|
var range;
|
|
if (selection.isEmpty) {
|
|
return undefined;
|
|
}
|
|
else {
|
|
range = new vscode.Range(selection.start.line, selection.start.character, selection.end.line, selection.end.character);
|
|
const highlighted = textEditor.document.getText(range);
|
|
let camelCased = camelCase(highlighted);
|
|
let properCased = camelCased.substring(0, 1).toUpperCase() + camelCased.substring(1, camelCased.length);
|
|
editBuilder.replace(range, properCased);
|
|
}
|
|
});
|
|
return undefined;
|
|
}
|
|
|
|
export function paramCase(str: string) {
|
|
return str
|
|
.replace(
|
|
/([A-Z]+(.))/g,
|
|
(_, separator, letter, offset) => (offset ? "-" + separator : separator).toLowerCase()
|
|
)
|
|
.split(/[\s!?.,@:;|\\/"'`£$%\^&*{}[\]()<>~#+\-=_¬]+/g)
|
|
.join('-')
|
|
.replace(/(^-|-$)/g, '');
|
|
};
|
|
|
|
function transformToParamCaseLogic(): undefined {
|
|
const textEditor = vscode.window.activeTextEditor;
|
|
if (!textEditor) {
|
|
return undefined;
|
|
}
|
|
const selection = textEditor.selection;
|
|
textEditor.edit(editBuilder => {
|
|
var range;
|
|
if (selection.isEmpty) {
|
|
return undefined;
|
|
}
|
|
else {
|
|
range = new vscode.Range(selection.start.line, selection.start.character, selection.end.line, selection.end.character);
|
|
const highlighted = textEditor.document.getText(range);
|
|
let paramCased = paramCase(highlighted);
|
|
editBuilder.replace(range, paramCased);
|
|
}
|
|
});
|
|
return undefined;
|
|
}
|
|
|
|
export const codeGeneratorQuickPick = () => codeGeneratorQuickPickLogic();
|
|
export const insertDateTime = () => insertDateTimeLogic();
|
|
export const insertAllExtensions = () => insertAllExtensionsLogic();
|
|
export const transformToProperCase = () => transformToProperCaseLogic();
|
|
export const transformToParamCase = () => transformToParamCaseLogic(); |