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.
 
 

119 lines
3.4 KiB

try {
console.log("start")
const vscode = require("vscode")
const fs = require("fs")
let start = vscode.window.activeTextEditor.selection.start.line
let fileName = vscode.window.activeTextEditor.document.fileName
console.log(start)
console.log(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(" ", " ")
}
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']
let tmp = text.split("// ----------------------------temp------------------------------------")
text = tmp[1]
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)
console.log(text)
console.log(endPosition)
vscode.window.activeTextEditor.edit(function (textEditorEdit) {
textEditorEdit.insert(
endPosition,
text,
)
})
// vscode.window.showErrorMessage(res)
}).catch(error => {
vscode.window.showErrorMessage(`err:${error.message}`)
})
});
})
} catch (error) {
console.log(error)
}