TODO: 完善剩下来的内容

This commit is contained in:
huangsimin 2019-10-08 18:22:24 +08:00
parent 037fe62588
commit d5f413c9e9
3 changed files with 165 additions and 121 deletions

View File

@ -11,9 +11,7 @@
], ],
"activationEvents": [ "activationEvents": [
"onLanguage:go", "onLanguage:go",
"onCommand:go-quickly-generator.helloWorld", "onCommand:Go-Quickly-Generator.Go-Gen-GetSet"
"onCommand:go-quickly-generator.go-gen-set",
"onCommand:go-quickly-generator.allGetterAndSetter"
], ],
"main": "./out/extension.js", "main": "./out/extension.js",
"contributes": { "contributes": {
@ -30,21 +28,20 @@
], ],
"commands": [ "commands": [
{ {
"command": "go-quickly-generator.helloWorld", "command": "Go-Quickly-Generator.Go-Gen-GetSet",
"title": "Hello World", "title": "Generator Get Set",
"category": "go-quickly-generator"
},
{
"command": "go-quickly-generator.go-gen-set",
"title": "gen get set",
"category": "go-quickly-generator"
},
{
"command": "go-quickly-generator.allGetterAndSetter",
"title": "allGetterAndSetter",
"category": "go-quickly-generator" "category": "go-quickly-generator"
} }
] ],
"menus": {
"editor/context": [
{
"when": "editorTextFocus && resourceLangId == go",
"command": "Go-Quickly-Generator.Go-Gen-GetSet",
"group": "GoQuickly@1"
}
]
}
}, },
"scripts": { "scripts": {
"vscode:prepublish": "npm run compile", "vscode:prepublish": "npm run compile",
@ -64,8 +61,5 @@
"tslint": "^5.12.1", "tslint": "^5.12.1",
"vscode-test": "^1.2.0" "vscode-test": "^1.2.0"
}, },
"dependencies": { "dependencies": {}
}
} }

View File

@ -4,9 +4,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
// Import the module and reference it with the alias vscode in your code below // Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode'; import * as vscode from 'vscode';
import { resolve } from 'url';
import { rejects } from 'assert';
import { prototype } from 'events';
class StructInfo { class StructInfo {
ShorthandName: string; ShorthandName: string;
@ -30,7 +27,7 @@ class StructInfo {
this.Range = range; this.Range = range;
this.Fields = new Map<string, Field>(); this.Fields = new Map<string, Field>();
fields.forEach((value)=>{ fields.forEach((value) => {
this.Fields.set(value.Key, value); this.Fields.set(value.Key, value);
}); });
} }
@ -58,7 +55,9 @@ class Field {
this.Type = type; this.Type = type;
this.Name = name; this.Name = name;
this.Range = range; this.Range = range;
this.Key = (this.Parent.substr(1) + this.Name).replace(".", "");
this.Key = (this.Parent.substr(1) + this.Name[0].toUpperCase() + this.Name.substr(1)).replace(new RegExp("\\.", "g"), "");
// TODO: 大小写Map的问题
} }
toString(): string { toString(): string {
@ -72,94 +71,15 @@ function activate(context: vscode.ExtensionContext) {
// Use the console to output diagnostic information (console.log) and errors (console.error) // 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 // 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!'); console.log('Congratulations, your extension "go-quickly-generator" is now active!');
context.subscriptions.push(vscode.commands.registerCommand('go-quickly-generator.go-gen-set', () => {
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 // The code you place here will be executed every time your command is executed
// Display a message box to the user // Display a message box to the user
let sinfo = GetStruct(); let sinfo = GetStruct();
if (sinfo) { if(sinfo) {
console.log(sinfo); GeneratorSetGet(sinfo);
} else {
let editor = vscode.window.activeTextEditor; vscode.window.showErrorMessage("there is no struct(go) to focus. you can move point to struct(go)");
if (editor !== undefined) {
let regexFunction = `func {0,}\\(.+${sinfo.Name} {0,}\\) {0,}[GS]et([a-zA-Z_]+) {0,}\\(`;
// console.log(regexFunction);
let existsStructFunctions: Set<string> = new Set<string>();
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]);
}
}
const options = <vscode.QuickPickOptions>{ canPickMany: true, placeHolder: "select the fields that would be generator get set" };
var items: vscode.QuickPickItem[] = [];
var obj = {
info: sinfo,
exists: existsStructFunctions,
items: function() {
this.info.Fields.forEach( (value, key) => {
if(this.exists.has(key)) {
vscode.window.showInformationMessage("Get" + key + "or Set" + key + " is Exists");
} else {
items.push( <vscode.QuickPickItem> {
label: value.toString(),
detail: this.info.Name,
description: key,
});
}
});
},
pick: function() {
this.items();
vscode.window.showQuickPick(items, options).then( (item) => {
if(item) {
console.log("123", item, this.info.Name);
}
});
}
};
obj.pick();
}
// const pickThen = function (input: string | undefined) {
// if (typeof (input) !== "string") {
// var selections: string[] = input as any;
// console.log(selections); // TODO: search exists function
// selections.forEach((selection) => {
// let infos = selection.match(`(\\d+)\\) ([^\\.]+)([^ ]+) (.+)`);
// if (infos) {
// // console.log("infos", selection, infos);
// console.log("arguments", arguments, prototype);
// }
// });
// }
// };
// vscode.window.showQuickPick(sinfo.getFieldsString(), options).then( (input) => {
// if(typeof(input) !== "string") {
// var selections: string[] = input as any;
// console.log(selections); // TODO: search exists function
// selections.forEach( (selection)=>{
// let infos = selection.match(`(\\d+)\\) ([^\\.]+)([^ ]+) (.+)`);
// if(infos) {
// console.log("infos",selection, infos);
// }
// });
// }
// });
} }
})); }));
@ -177,6 +97,101 @@ function activate(context: vscode.ExtensionContext) {
})); }));
} }
function getAbbreviation(name: string): string | undefined {
if (name.length) {
let shortName = name[0].toLowerCase();
let m = name.substr(1).match("[A-Z]");
if (m) {
m.forEach((v) => {
shortName += v.toLowerCase();
});
return shortName;
}
}
return undefined;
}
function GeneratorSetGet(sinfo: StructInfo) {
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,}\\(`;
// console.log(regexFunction);
let existsStructFunctions: Set<string> = new Set<string>();
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]);
}
}
const options = <vscode.QuickPickOptions>{ canPickMany: true, placeHolder: "select the fields that would be generator get set" };
var items: vscode.QuickPickItem[] = [];
var obj = {
info: sinfo,
exists: existsStructFunctions,
items: function () {
this.info.Fields.forEach((value, key) => {
if (this.exists.has(key)) {
vscode.window.showInformationMessage("Get" + key + " or Set" + key + " is Exists");
} else {
items.push(<vscode.QuickPickItem>{
label: value.toString(),
detail: this.info.Name,
description: key,
});
}
});
},
pick: function () {
this.items();
vscode.window.showQuickPick(items, options).then((item) => {
if (item) {
let fields = item as any as vscode.QuickPickItem[];
let sname = getAbbreviation(this.info.Name) as string;
let structString = `func (${sname} *${this.info.Name})`;
fields.forEach((qitem) => {
let field = this.info.Fields.get(qitem.description as string);
if (field) {
let editor = vscode.window.activeTextEditor;
if (editor) {
let keyName = field.Name[0].toUpperCase() + field.Name.substr(1);
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));
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));
}
}
});
}
});
}
};
obj.pick();
}
}
function GetStruct(): StructInfo | undefined { function GetStruct(): StructInfo | undefined {
let editor = vscode.window.activeTextEditor; let editor = vscode.window.activeTextEditor;
@ -251,7 +266,7 @@ function getStructField(editor: vscode.TextEditor, parent: string, startline: nu
function getSingleStructRelationship(source: string, parent: string): Field | undefined { function getSingleStructRelationship(source: string, parent: string): Field | undefined {
let smatch = source.match("([^ \t]+)[^s]+struct {0,}\\{(.+)\\}"); let smatch = source.match("([^ \t]+)[^s]+struct {0,}\\{(.+)\\}");
if (smatch !== null) { if (smatch !== null) {
console.log(smatch[0], smatch[1], smatch[2]); // console.log(smatch[0], smatch[1], smatch[2]);
return getSingleStructRelationship(smatch[2], parent + "." + smatch[1]); return getSingleStructRelationship(smatch[2], parent + "." + smatch[1]);
} else { } else {
smatch = source.match("([^ \t]+)[ \t]+(.+)"); smatch = source.match("([^ \t]+)[ \t]+(.+)");

View File

@ -38,26 +38,61 @@ func DoFunc(a func(
// ExStruct 升级 // ExStruct 升级
type ExStruct struct { type ExStruct struct {
ChildStruct struct { ChildStruct struct {
A int A int
B interface{} B interface{}
girl string
boy int
} }
C string C string
D func(a int, b string) D func(a int, b string)
child string
}
// GetChildStructBoy Get return boy int
func (es *ExStruct) GetChildStructBoy() int {
return es.ChildStruct.boy
}
// SetChildStructBoy Set boy int
func (es *ExStruct) SetChildStructBoy(boy int) {
es.ChildStruct.boy = boy
}
// GetChildStructGirl Get return girl string
func (es *ExStruct) GetChildStructGirl() string {
return es.ChildStruct.girl
}
// SetChildStructGirl Set girl string
func (es *ExStruct) SetChildStructGirl(girl string) {
es.ChildStruct.girl = girl
}
// GetChildStructGirl
// GetChildStructB Get return B interface{}
func (es *ExStruct) GetChildStructB() interface{} {
return es.ChildStruct.B
}
// SetChildStructB Set B interface{}
func (es *ExStruct) SetChildStructB(B interface{}) {
es.ChildStruct.B = B
} }
// SetChildStructA set // SetChildStructA set
func (e *ExStruct) SetChildStructA(a int) { func (es *ExStruct) SetChildStructA(a int) {
e.ChildStruct.A = a es.ChildStruct.A = a
} }
// GetChildStructA get // GetChildStructA get
func (e *ExStruct) GetChildStructA(a int) { func (es *ExStruct) GetChildStructA() int {
e.ChildStruct.A = a return es.ChildStruct.A
} }
// GetC get // GetC get
func (e *ExStruct) GetC(a int) { func (es *ExStruct) GetC(a int) {
e.ChildStruct.A = a es.ChildStruct.A = a
} }
func main() { func main() {