182 lines
6.5 KiB
TypeScript
182 lines
6.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 date = new Date();
|
|
const selection = textEditor.selection;
|
|
textEditor.edit(editBuilder => {
|
|
var range;
|
|
if (selection.isEmpty) {
|
|
editBuilder.insert(selection.start, date.toString())
|
|
}
|
|
else {
|
|
range = new vscode.Range(selection.start.line, selection.start.character, selection.end.line, selection.end.character);
|
|
editBuilder.replace(range, date.toString());
|
|
}
|
|
});
|
|
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 transformToPopperCaseLogic(): 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 const codeGeneratorQuickPick = () => codeGeneratorQuickPickLogic();
|
|
export const insertDateTime = () => insertDateTimeLogic();
|
|
export const transformToPopperCase = () => transformToPopperCaseLogic(); |