Skip to content

Commit

Permalink
Merge pull request #2 from H0R15H0/feature/user-can-select-what-to-ge…
Browse files Browse the repository at this point in the history
…nerate

[Feature] select what to generate
  • Loading branch information
H0R15H0 authored Jul 30, 2023
2 parents 664c916 + db285e3 commit b80df0d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 24 deletions.
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const SELECT_BTN_GETTER = 'Getter';
export const SELECT_BTN_SETTER = 'Setter';
85 changes: 61 additions & 24 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as vscode from 'vscode';
import {GoParser} from "./golang-parser/golang";
import * as types from "./golang-parser/types";
import {dedent} from 'ts-dedent';
import {SELECT_BTN_GETTER, SELECT_BTN_SETTER} from './constants';

// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed
Expand All @@ -18,14 +19,14 @@ export function activate(context: vscode.ExtensionContext) {
// 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('go-getter-setter.generate', () => {
const parser = new GoParser();
// The code you place here will be executed every time your command is executed
// Display a message box to the user
const editor = vscode.window.activeTextEditor;
if (editor === undefined) {
return;
}
const selection = editor.selection;
const parser = new GoParser();
const text = editor.document.getText(selection);

let struct:types.Struct;
Expand All @@ -35,31 +36,17 @@ export function activate(context: vscode.ExtensionContext) {
vscode.window.showErrorMessage(e.message);
return;
}
editor.edit((editBuilder)=> {
let currentLocation = new vscode.Position(selection.end.line, 0);
if (!text.endsWith('\n')) {
currentLocation = new vscode.Position(selection.end.line + 1, 0);
}
insert(editBuilder, currentLocation, '');

// Getter
for (const [i, field] of struct.fields.entries()) {
insert(editBuilder, currentLocation, dedent(`
func (${struct.name[0].toLowerCase()} *${struct.name}) ${field.name[0].toUpperCase() + field.name.slice(1)}() ${field.type} {
return ${struct.name[0].toLowerCase()}.${field.name}
}
`), i === struct.fields.length - 1 ? 1 : 2);
}
insert(editBuilder, currentLocation, '');

// Setter
for (const [i, field] of struct.fields.entries()) {
insert(editBuilder, currentLocation, dedent(`
func (${struct.name[0].toLowerCase()} *${struct.name}) Set${field.name[0].toUpperCase() + field.name.slice(1)}(${field.name} ${field.type}) {
${struct.name[0].toLowerCase()}.${field.name} = ${field.name}
selectGenerateTargets(struct).then(targets => {
editor.edit((editBuilder)=> {
let currentLocation = new vscode.Position(selection.end.line, 0);
if (!text.endsWith('\n')) {
currentLocation = new vscode.Position(selection.end.line + 1, 0);
}
`), i === struct.fields.length - 1 ? 1 : 2);
}
insert(editBuilder, currentLocation, '');

generate(editBuilder, currentLocation, struct.name, targets);
});
});
});

Expand All @@ -71,4 +58,54 @@ export function deactivate() {}

function insert(editBuilder: vscode.TextEditorEdit, location: vscode.Position, text: string, brCount: number = 1): void{
editBuilder.insert(location, text + '\n'.repeat(brCount));
}

type GenerateTargets = {
methods: string[],
fields: GenerateField[],
};

type GenerateField = {
name: string,
type: string,
};

async function selectGenerateTargets(struct: types.Struct): Promise<GenerateTargets> {
const fieldName2Field = Object.fromEntries(struct.fields.map(f => [f.name, f]));

const methods = await vscode.window.showQuickPick([SELECT_BTN_GETTER, SELECT_BTN_SETTER], { canPickMany: true, placeHolder: "select what you want to generate" })
const fields = await vscode.window.showQuickPick<vscode.QuickPickItem>(struct.fields.map(f => ({label: f.name, description: f.type})), { canPickMany: true, placeHolder: "select what you want to generate" });
if (methods === undefined || fields === undefined) {
return <GenerateTargets>{};
};
const generateFields = fields.map((field) => (<GenerateField>{name: field.label, type: fieldName2Field[field.label].type}));
return <GenerateTargets>{methods: methods, fields: generateFields};
}

function generate(editBuilder: vscode.TextEditorEdit, currentLocation: vscode.Position, structName: string, targets: GenerateTargets) {
for (const item of targets.methods) {
// Getter
if (item === SELECT_BTN_GETTER) {
for (const [i, field] of targets.fields.entries()) {
insert(editBuilder, currentLocation, dedent(`
func (${structName[0].toLowerCase()} *${structName}) ${field.name[0].toUpperCase() + field.name.slice(1)}() ${field.type} {
return ${structName[0].toLowerCase()}.${field.name}
}
`), i === targets.fields.length - 1 ? 1 : 2);
}
if (targets.methods.length === 2) {
insert(editBuilder, currentLocation, '');
}
}
// Setter
if (item === SELECT_BTN_SETTER) {
for (const [i, field] of targets.fields.entries()) {
insert(editBuilder, currentLocation, dedent(`
func (${structName[0].toLowerCase()} *${structName}) Set${field.name[0].toUpperCase() + field.name.slice(1)}(${field.name} ${field.type}) {
${structName[0].toLowerCase()}.${field.name} = ${field.name}
}
`), i === targets.fields.length - 1 ? 1 : 2);
}
}
}
}

0 comments on commit b80df0d

Please sign in to comment.