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.

118 lines
3.4 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. try {
  2. console.log("start")
  3. const vscode = require("vscode")
  4. const fs = require("fs")
  5. let start = vscode.window.activeTextEditor.selection.start.line
  6. let fileName = vscode.window.activeTextEditor.document.fileName
  7. console.log(start)
  8. console.log(fileName)
  9. fs.readFile(fileName, 'utf8', (err, data) => {
  10. if (err) throw err;
  11. var context_arr = data.split("\n")
  12. let a = context_arr[start].trim(" ")
  13. console.log(a)
  14. for (; a.indexOf(" ") > 0;) {
  15. a = a.replace(" ", " ")
  16. }
  17. let b = a.split(" ")
  18. if (b[0] != "type" || b[2] != "struct" || b[3] != "{") {
  19. vscode.window.showErrorMessage("找不到结构")
  20. return
  21. }
  22. let struct = b[1]
  23. var stack = ['{']
  24. var endLine = 0
  25. var endChar = 0;
  26. (function () {
  27. for (var i = start + 1; i < context_arr.length; i++) {
  28. for (let k = 0; k < context_arr[i].length; k++) {
  29. const element = context_arr[i][k];
  30. console.log(element)
  31. if (element == '{') {
  32. stack.push('{')
  33. console.log(stack)
  34. }
  35. if (element == '}') {
  36. stack.pop()
  37. console.log(stack)
  38. }
  39. if (stack.length == 0) {
  40. endLine = i
  41. endChar = k + 1
  42. return
  43. }
  44. }
  45. }
  46. })()
  47. if (endLine < start) {
  48. vscode.window.showErrorMessage("结构异常")
  49. return
  50. }
  51. vscode.window.showInputBox({
  52. placeHolder: "请输入接口前缀,例如输入admin则填充为/admin/结构名/get|save..."
  53. }).then((prefix) => {
  54. for (; prefix.indexOf(" ") > 0;) {
  55. prefix = prefix.replace(" ", " ")
  56. }
  57. prefix = prefix.replace(/ /g, "/")
  58. let endPosition = new vscode.Position(endLine, endChar)
  59. axios({
  60. method: 'get',
  61. url: 'http://git.ouxuan.net/3136352472/vscode-helper-js/raw/branch/master/hasaki-gen-crud/template.go',
  62. responseType: 'text'
  63. }).then(res => {
  64. console.log("123")
  65. let text = "" + res['data']
  66. let tmp = text.split("// ----------------------------temp------------------------------------")
  67. text = tmp[1]
  68. text = text.replace(/Temp3A60/g, struct)
  69. let structHeadLower = struct[0].toLocaleLowerCase() + struct.slice(1)
  70. console.log("structHeadLower", structHeadLower, struct[0].toLocaleLowerCase())
  71. text = text.replace(/TempLowHead3A60/g, structHeadLower)
  72. text = text.replace(/Prefix3A60/g, prefix)
  73. console.log(text)
  74. console.log(endPosition)
  75. vscode.window.activeTextEditor.edit(function (textEditorEdit) {
  76. textEditorEdit.insert(
  77. endPosition,
  78. text,
  79. )
  80. })
  81. // vscode.window.showErrorMessage(res)
  82. }).catch(error => {
  83. vscode.window.showErrorMessage(`err:${error.message}`)
  84. })
  85. });
  86. })
  87. } catch (error) {
  88. console.log(error)
  89. }