You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

195 lines
6.0 KiB

try {
console.log("start")
const vscode = require("vscode");
function goplsfix(path) {
// axios.post("http://127.0.0.1:12677/gopls/fix.json", {
// path: path,
// }).then(res => {
// }).catch(error => {
// console.log("fix:", error);
// })
// vscode.commands.executeCommand('editor.action.quickFix', vscode.Uri.file(path), new vscode.Range((new vscode.Position(0, 0)), (new vscode.Position(100, 0))));
vscode.window.createTerminal("go:fix", "cmd.exe", ["/c", `gopls fix -a -w ${path}`])
}
const url = vscode.workspace.getConfiguration().get('go-vscode-config.url');
const fs = require("fs")
const path = require('path');
let start = vscode.window.activeTextEditor.selection.start.line
let fileName = vscode.window.activeTextEditor.document.fileName
console.log(start)
console.log(fileName)
let fileDir = path.dirname(fileName)
let fileBaseName = path.basename(fileName)
fs.readFile(fileName, 'utf8', (err, data) => {
if (err) throw err;
var context_arr = data.split("\n")
let a = context_arr[start].trim(" ")
console.log(a)
for (; a.indexOf(" ") > 0;) {
a = a.replace(" ", " ")
}
let b = a.split(" ")
if (b[0] != "type" || b[2] != "struct" || b[3] != "{") {
vscode.window.showErrorMessage("找不到结构")
return
}
let struct = b[1]
var stack = ['{']
var endLine = 0
var endChar = 0;
(function () {
for (var i = start + 1; i < context_arr.length; i++) {
for (let k = 0; k < context_arr[i].length; k++) {
const element = context_arr[i][k];
console.log(element)
if (element == '{') {
stack.push('{')
console.log(stack)
}
if (element == '}') {
stack.pop()
console.log(stack)
}
if (stack.length == 0) {
endLine = i
endChar = k + 1
return
}
}
}
})()
if (endLine < start) {
vscode.window.showErrorMessage("结构异常")
return
}
vscode.window.showInputBox({
placeHolder: "请输入接口前缀,例如输入admin则填充为/admin/结构名/get|save..."
}).then((prefix) => {
for (; prefix.indexOf(" ") > 0;) {
prefix = prefix.replace(" ", " ")
}
let rearr = prefix.toLocaleLowerCase().split(" ");
let underscore_naming = rearr.join("_");
let lowercase_first_letter = "";
let capitalize_the_first_letter = "";
for (var i = 0; i < rearr.length; i++) {
let tmp = rearr[i].split("");
tmp[0] = tmp[0].toLocaleUpperCase();
capitalize_the_first_letter += tmp.join("");
if (i == 0) {
lowercase_first_letter += rearr[i];
} else {
lowercase_first_letter += tmp.join("");;
}
}
prefix = prefix.replace(/ /g, "/")
let endPosition = new vscode.Position(endLine, endChar)
axios({
method: 'get',
url: 'http://git.ouxuan.net/3136352472/vscode-helper-js/raw/branch/master/hasaki-gen-crud/template.go',
responseType: 'text'
}).then(res => {
console.log("123")
let text = "" + res['data']
text = text.replace(/Temp3A60/g, struct)
let structHeadLower = struct[0].toLocaleLowerCase() + struct.slice(1)
console.log("structHeadLower", structHeadLower, struct[0].toLocaleLowerCase())
text = text.replace(/TempLowHead3A60/g, structHeadLower)
text = text.replace(/Prefix3A60/g, prefix)
text = text.replace(/PrefixHigHead3A60/g, capitalize_the_first_letter)
console.log(text)
console.log(endPosition)
let tmp = text.split("// ----------------------------temp------------------------------------")
if (data.indexOf(`${struct}) wherePrimaryKey`) < 0) {
vscode.window.activeTextEditor.edit(function (textEditorEdit) {
textEditorEdit.insert(
endPosition,
tmp[1],
)
})
}
let package_src = ""
for (let i = 0; i < context_arr.length; i++) {
const element = context_arr[i].trim(" ");
if (element.indexOf("package") == 0) {
package_src = element
break
}
}
let crudFileName = path.join(fileDir, `auto_generated_crud_${struct}_${fileBaseName}`)
let httpFileName = path.join(fileDir, `auto_generated_http_crud_${underscore_naming}_${struct}_${fileBaseName}`)
let mark = `//${package_src} 该文件由插件自动生成,无特殊情况请勿改动`;
fs.writeFile(crudFileName, `${mark}\n\n${package_src}\n\n${tmp[2]}`, function () {
goplsfix(crudFileName);
})
fs.writeFile(httpFileName, `${mark}\n\n${package_src}\n\n${tmp[3]}`, function () {
goplsfix(httpFileName);
})
goplsfix(fileName);
// vscode.window.showErrorMessage(res)
}).catch(error => {
vscode.window.showErrorMessage(`err:${error.message}`)
})
});
})
} catch (error) {
console.log(error)
}