panel_ex.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. 面板扩展
  3. 功能: 生成 gettrs 和 setters
  4. 文档: https://github.com/WilsonGodoi/generate-getters-setters
  5. */
  6. 'use strict';
  7. const fe = Editor.require('packages://simple-code/tools/tools.js');
  8. module.exports = {
  9. /** @type import('../../panel/vs-panel/vs-panel-base') */
  10. parent : null,
  11. // 面板初始化
  12. onLoad(parent) {
  13. // index.js 对象
  14. this.parent = parent;
  15. // monaco 右击菜单
  16. this.parent.vs_editor.addAction({
  17. id: 'gettrs-settrs', // 菜单项 id
  18. label: fe.translate('gettrs-settrs'), // 菜单项名称
  19. // keybindings: [this.monaco.KeyMod.CtrlCmd | this.monaco.KeyCode.KEY_J], // 绑定快捷键
  20. // A precondition for this action.
  21. precondition: 'editorHasSelection', // 选取状态才显示
  22. contextMenuGroupId: '9_cutcopypaste', // 所属菜单的分组
  23. contextMenuOrder: 9,
  24. run: this.handel.bind(this),
  25. })
  26. },
  27. handel() {
  28. // 获得编辑器
  29. const editor = this.parent.vs_editor;
  30. const model = editor.getModel();
  31. let languageInfo = model.getLanguageIdentifier();
  32. if(!languageInfo || languageInfo.language != 'typescript'){
  33. Editor.info(fe.translate('gettrs-setters-language'));
  34. return;
  35. }
  36. let code = ``;
  37. let reverse = false;
  38. // 获得选取的内容
  39. let selections = editor.getSelections();
  40. for (let selection of selections) {
  41. reverse = selection.getDirection();
  42. }
  43. // 选取位置反转
  44. if (reverse) {
  45. selections = selections.reverse();
  46. }
  47. // 合并选取的文本
  48. for (let selection of selections) {
  49. code += model.getValueInRange(selection);
  50. code += `\n`;
  51. }
  52. // 少于一定值则不执行
  53. if (code.length < 1) {
  54. Editor.info('No selected properties.');
  55. return;
  56. }
  57. try {
  58. // 创建代码
  59. var getterAndSetter = this.createGetterAndSetter(code);
  60. if(getterAndSetter == ''){
  61. Editor.info('Something went wrong! Try that the properties are in this format: "private name: string;"');
  62. return;
  63. }
  64. // 插入代码到后面
  65. // editor.edit(e => e.insert(selections[selections.length - 1].end, getterAndSetter));
  66. let select = selections[selections.length - 1];
  67. select = new Editor.monaco.Selection(select.endLineNumber,select.endColumn+1,select.endLineNumber,select.endColumn+1)
  68. let edits = [{
  69. range:select,
  70. text:'\n'+getterAndSetter,
  71. forceMoveMarkers:true,
  72. }]
  73. model.pushStackElement();
  74. model.pushEditOperations([select], edits, () => []);
  75. model.pushStackElement();
  76. }
  77. catch (error) {
  78. Editor.info('Something went wrong! Try that the properties are in this format: "private name: string;"');
  79. }
  80. },
  81. toCamelCase(str) {
  82. return str.replace(/\w+/g, w => w[0].toUpperCase() + w.slice(1));
  83. },
  84. createGetterAndSetter(textProperties) {
  85. // 用换行分割每个变量
  86. let rows = textProperties.split('\n').map(x => x.replace(';', ''));
  87. // 成员变量缓存
  88. let properties = [];
  89. for (let row of rows) {
  90. let s_i = row.indexOf('=');
  91. if(s_i != -1) row = row.substr(0,s_i);
  92. if (row.trim() !== "") { // 删除空行
  93. properties.push(row); // 记录
  94. }
  95. }
  96. let generatedCode = '';
  97. // 遍历成员
  98. for (let p in properties) {
  99. // 去掉空格或换行的前缀
  100. while (properties[p].startsWith(" ")) { properties[p] = properties[p].substr(1); }
  101. while (properties[p].startsWith("\t")) { properties[p] = properties[p].substr(1); }
  102. let words = [];
  103. if(properties[p].indexOf(':') == -1) continue;
  104. // 空格分割 private id:number
  105. let rows = properties[p].split(" ").map(x => x.replace('\r\n', ''));
  106. for (let row of rows) {
  107. if (row.trim() !== '') {
  108. words.push(row.replace('\r', '')); // 成员缓存分割信息
  109. }
  110. }
  111. let type, attribute, Attribute = "";
  112. let create = false;
  113. if(words[0] != 'public' && words[0] != 'private' && words[0] != 'protected'){
  114. // if words === ["name:", "string"];
  115. words.unshift('public');
  116. }
  117. // 解析成员组成
  118. // if words === ["private", "name:", "string"];
  119. if (words.length === 3) {
  120. let attributeArray = words[1].split(":");
  121. type = words[2];
  122. attribute = attributeArray[0];
  123. Attribute = this.toCamelCase(attribute);
  124. create = type;
  125. // if words === ["private", "name:string"];
  126. } else if (words.length === 2) {
  127. let array = words[1].split(":");
  128. type = array[1];
  129. attribute = array[0];
  130. Attribute = this.toCamelCase(attribute);
  131. create = type;
  132. // if words === ["private", "name", ":", "string"];
  133. } else if (words.length === 4) {
  134. let array = [];
  135. for (let word of words) {
  136. if (word !== ':') {
  137. array.push(word);
  138. }
  139. }
  140. type = array[2].trim();
  141. attribute = array[1];
  142. Attribute = this.toCamelCase(attribute);
  143. create = type;
  144. } else {
  145. Editor.info('Something went wrong! Try that the properties are in this format: "private name: string;"')
  146. generatedCode = ``;
  147. break;
  148. }
  149. if (create) {
  150. let code = `
  151. public ${(type === "Boolean" || type === "boolean" ? "is" : "get")}${Attribute}(): ${type} {
  152. return this.${attribute};
  153. }
  154. public set${Attribute}(${attribute}: ${type}): void {
  155. this.${attribute} = ${attribute};
  156. }
  157. `;
  158. generatedCode += code;
  159. }
  160. }
  161. return generatedCode;
  162. },
  163. /************* 事件 *************/
  164. messages: {
  165. },
  166. };