From 7a53ca4d8c0f789204c902ad587ab594fcdf01d9 Mon Sep 17 00:00:00 2001 From: huangsimin Date: Sat, 12 Oct 2019 18:29:34 +0800 Subject: [PATCH] =?UTF-8?q?TODO:=20=E5=AE=8C=E5=96=84=20Get=20Set=20?= =?UTF-8?q?=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/codeAction.ts | 19 ++++++++++++ src/extension.ts | 73 +++++++++++++++++++++++++++++++++----------- src/testfile/main.go | 1 + 3 files changed, 76 insertions(+), 17 deletions(-) create mode 100644 src/codeAction.ts diff --git a/src/codeAction.ts b/src/codeAction.ts new file mode 100644 index 0000000..54a9786 --- /dev/null +++ b/src/codeAction.ts @@ -0,0 +1,19 @@ +import * as vscode from 'vscode'; + +export class Provider implements vscode.CodeActionProvider { + provideCodeActions(document: vscode.TextDocument, range: vscode.Range | vscode.Selection, context: vscode.CodeActionContext, token: vscode.CancellationToken): vscode.ProviderResult<(vscode.Command | vscode.CodeAction)[]> { + // throw new Error("Method not implemented."); + let actions: vscode.CodeAction[] = []; + + let action = new vscode.CodeAction(`Get Set Generator`, vscode.CodeActionKind.Source); + + action.command = { + title: "Get Set Generator", + command: "Go-Quickly-Generator.Go-Gen-GetSet" + } as vscode.Command; + actions.push(action); + action.isPreferred = true; + + return actions; + } +} \ No newline at end of file diff --git a/src/extension.ts b/src/extension.ts index e821738..18dfaa6 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -4,6 +4,7 @@ Object.defineProperty(exports, "__esModule", { value: true }); // Import the module and reference it with the alias vscode in your code below import * as vscode from 'vscode'; +import { Provider } from './codeAction'; class StructInfo { ShorthandName: string; @@ -65,6 +66,9 @@ class Field { } } +let typeMap = new Map() ; +let typeCharMap = new Map() ; + // this method is called when your extension is activated // your extension is activated the very first time the command is executed function activate(context: vscode.ExtensionContext) { @@ -72,20 +76,46 @@ function activate(context: vscode.ExtensionContext) { // This line of code will only be executed once when your extension is activated console.log('Congratulations, your extension "go-quickly-generator" is now active!'); + + typeMap.set("Getter", GeneratorType.Getter); + typeMap.set("Setter", GeneratorType.Setter); + typeCharMap.set(GeneratorType.Getter, "G"); + typeCharMap.set(GeneratorType.Setter, "S"); + typeCharMap.set(GeneratorType.Getter | GeneratorType.Setter, "GS"); + + context.subscriptions.push(vscode.commands.registerCommand('Go-Quickly-Generator.Go-Gen-GetSet', () => { // The code you place here will be executed every time your command is executed // Display a message box to the user let sinfo = GetStruct(); if(sinfo) { - GeneratorSetGet(sinfo); + + vscode.window.showQuickPick(["Getter", "Setter"], {canPickMany: true, placeHolder: "select generator type getter or setter"}).then( items => { + console.log(items); + let myitems = items as any as string[]; + let t = GeneratorType.Unknown; + myitems.forEach((value)=>{ + let sel = typeMap.get(value); + if(sel) { + t = t | sel; + } + }); + if(sinfo) { + GeneratorSetGet(sinfo, t); + } + }); + // GeneratorSetGet(sinfo, GeneratorType.Getter | GeneratorType.Setter); } else { vscode.window.showErrorMessage("there is no struct(go) to focus. you can move point to struct(go)"); } })); + // context.subscriptions.push(vscode.languages.registerCodeActionsProvider( + // "go", new Provider(), { providedCodeActionKinds: [vscode.CodeActionKind.Source] } + // )); - context.subscriptions.push(vscode.commands.registerCommand('go-quickly-generator.allGetterAndSetter', function () { + context.subscriptions.push(vscode.commands.registerCommand('Go-Quickly-Generator.Getter', function () { let editor = vscode.window.activeTextEditor; if (editor !== undefined) { const currentPos = editor.selection.active; @@ -111,21 +141,27 @@ function getAbbreviation(name: string): string | undefined { return undefined; } -function GeneratorSetGet(sinfo: StructInfo) { +enum GeneratorType { + Unknown = 0 , + Setter = 1 << 0, + Getter = 1 << 1, +} + +function GeneratorSetGet(sinfo: StructInfo, stype: GeneratorType) { console.log(sinfo); let editor = vscode.window.activeTextEditor; if (editor !== undefined) { - let regexFunction = `func {0,}\\(.+${sinfo.Name} {0,}\\) {0,}[GS]et([a-zA-Z_]+) {0,}\\(`; + let gtypechar = typeCharMap.get(stype) as string; + let regexFunction = `^func {0,}\\(.+${sinfo.Name} {0,}\\) {0,}[${gtypechar}]et([a-zA-Z_]+) {0,}\\(`; // console.log(regexFunction); let existsStructFunctions: Set = new Set(); for (let n = 0; n < editor.document.lineCount; n++) { let line = editor.document.lineAt(n); let matches = line.text.match(regexFunction); if (matches !== null) { - // console.log(matches[0], matches[1]); existsStructFunctions.add(matches[1]); } } @@ -168,19 +204,22 @@ function GeneratorSetGet(sinfo: StructInfo) { let funcitonName = field.Parent.replace( new RegExp("\\.", "g"), "") + keyName ; // Set - let prefix = "Set"; - let setFunction = prefix + funcitonName ; - let params = `(${field.Name} ${field.Type})`; - let comment = `// ${setFunction} ${prefix} ${field.Name} ${field.Type}\n`; - let ss = new vscode.SnippetString(`\n${comment}${structString} ${setFunction}${params} {\n\t${sname}${field.Parent}${field.Name} = ${field.Name}\n}\n`); - editor.insertSnippet(ss, new vscode.Position(this.info.Range[1] + 1, 0)); + if(stype & GeneratorType.Setter) { + let prefix = "Set"; + let setFunction = prefix + funcitonName ; + let params = `(${field.Name} ${field.Type})`; + let comment = `// ${setFunction} ${prefix} ${field.Name} ${field.Type}\n`; + let ss = new vscode.SnippetString(`\n${comment}${structString} ${setFunction}${params} {\n\t${sname}${field.Parent}${field.Name} = ${field.Name}\n}\n`); + editor.insertSnippet(ss, new vscode.Position(this.info.Range[1] + 1, 0)); + } - prefix = "Get"; - let getFunction = prefix + funcitonName ; - comment = `// ${getFunction} ${prefix} return ${field.Name} ${field.Type}\n`; - ss = new vscode.SnippetString(`\n${comment}${structString} ${getFunction}() ${field.Type} {\n\treturn ${sname}${field.Parent}${field.Name}\n}\n`); - editor.insertSnippet(ss, new vscode.Position(this.info.Range[1] + 1, 0)); - + if(stype & GeneratorType.Getter) { + let prefix = "Get"; + let getFunction = prefix + funcitonName ; + let comment = `// ${getFunction} ${prefix} return ${field.Name} ${field.Type}\n`; + let ss = new vscode.SnippetString(`\n${comment}${structString} ${getFunction}() ${field.Type} {\n\treturn ${sname}${field.Parent}${field.Name}\n}\n`); + editor.insertSnippet(ss, new vscode.Position(this.info.Range[1] + 1, 0)); + } } } }); diff --git a/src/testfile/main.go b/src/testfile/main.go index 817f27a..d3e358d 100644 --- a/src/testfile/main.go +++ b/src/testfile/main.go @@ -61,6 +61,7 @@ func (es *ExStruct) SetChildStructBoy(boy int) { // GetChildStructGirl Get return girl string func (es *ExStruct) GetChildStructGirl() string { return es.ChildStruct.girl + } // SetChildStructGirl Set girl string