panel_ex.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. 面板扩展
  3. 功能: 代码输入提示
  4. */
  5. 'use strict';
  6. var path = require('path');
  7. var fs = require('fs');
  8. module.exports = {
  9. /** @type import('../../panel/vs-panel/vs-panel-base') */
  10. parent : null,
  11. //
  12. ready(parent){
  13. // index.js 对象
  14. this.parent = parent;
  15. let EditorCompletion = require('./editor-completion');
  16. this.completor = new EditorCompletion.default(this.parent);
  17. },
  18. // 编辑器启动完成
  19. onLoad(parent){
  20. this.initCompletionEvent()
  21. this.loadCompleter()
  22. },
  23. initCompletionEvent(){
  24. this.completor.onLoad(this.parent.vs_editor)
  25. this.parent.monaco.languages.registerCompletionItemProvider('typescript',this.completor);
  26. this.parent.monaco.languages.registerCompletionItemProvider('plaintext',this.completor);
  27. },
  28. // 配置代码输入提示
  29. loadCompleter(){
  30. // 载入自定义提示
  31. let text = fs.readFileSync(Editor.url('packages://simple-code/template/hint_text.txt', 'utf8')).toString()
  32. // 提示列表格式 : arrWord = ["cat", "this.node.runAction( cc.MoveTo(0,cc.p(0,0) ))",...]
  33. let arrWord = text.split(" ");
  34. this.completor.addCustomCompleters(arrWord)
  35. // 动态添加提示
  36. // this.completor.addCustomCompleters(["cc.Sequence([new cc.delayTime(1),new cc.MoveTo(0.1,pos),new cc.CallFunc(()=>{})])"])
  37. // // 提示缩写, 只需输入 "delay"就有提示
  38. // this.completor.addCustomCompleter("delay","cc.Sequence([new cc.delayTime(1)],new cc.CallFunc(()=>{})])","延时动作回调")
  39. // this.completor.addCustomCompleter("this.ui.","this.ui.","命令模式下该ui保存场景所有节点")
  40. // this.completor.addCustomCompleter("this.node.","this.node.","命令模式下该node保存当前鼠标选择的节点")
  41. this.completor.addCustomCompleter("forEach in","forEach((value,key)=>{${1: }})","遍历数组",27,true)
  42. this.completor.addCustomCompleter("for","for (let ${1:i} = 0; ${1:i} < ${2:arr}.length; i++) {\n let ${3:vaule} = ${2:arr}[${1:i}];\n}","for(let i=0; i<array.length;i++)",27,true)
  43. this.completor.addCustomCompleter("for loop","for (let ${1:i} = 0; ${1:i} < ${2:arr}.length; i++) {\n let ${3:vaule} = ${2:arr}[${1:i}];\n}","for(let i=0; i<array.length;i++)",27,true)
  44. this.completor.addCustomCompleter("for re loop","for (let ${1:i} = ${2:arr}.length-1; ${1:i} >= 0; i--) {\n let ${3:vaule} = ${2:arr}[${1:i}];\n}","for(let i=array.length -1; i>=0;i--)",27,true)
  45. this.completor.addCustomCompleter("for in","for (let ${1:k} in ${2:object}) {\n let ${3:vaule} = ${2:object}[${1:k}];\n}","for (let k in object)",27,true)
  46. this.completor.addCustomCompleter("while","while (${1:true}) {\n ${2: }\n}","while (true)",27,true)
  47. this.completor.addCustomCompleter("if","if (${1:true}) {\n ${2: }\n}","if (true)",27,true)
  48. this.completor.addCustomCompleter("else if","else if(${1:true}) {\n ${2: }\n}","else if(true){}",27,true)
  49. this.completor.addCustomCompleter("switch (key)","switch (${1:key}) {\n case value:\n \n break;\n\n default:\n break;\n}",'',27,true)
  50. this.completor.addCustomCompleter("try catch","try {\n ${2: } \n} catch (${1:error}) {\n ${3: }\n}",'try catch(err)',27,true)
  51. this.completor.addCustomCompleter("import from; ",'import ${1:model} from"${2:model}"','',27,true)
  52. },
  53. onSaveFile(fileInfo){
  54. // 保存后刷新下
  55. if(!this.parent.is_save_wait_up){
  56. this.completor.upAllSymSuggests()
  57. }
  58. },
  59. onAssetsCreatedEvent(files){
  60. this.completor.upAllSymSuggests()
  61. },
  62. // 刷新场景内节点信息
  63. onCurrSceneChildrenInfo(currSceneChildrenInfo)
  64. {
  65. if(!this.parent) return;
  66. // 写入提示
  67. currSceneChildrenInfo.forEach((info)=>
  68. {
  69. // 动态添加当前场景所有节点的name输入提示
  70. // 名字,名字,节点路径深度描述,类型图标,是否覆盖
  71. this.completor.addCustomCompleter(info.name,info.name,info.path,this.parent.monaco.languages.CompletionItemKind.Unit,true)
  72. })
  73. },
  74. onLoadAssetAndCompleter(filePath, extname, isUrlType,isScript)
  75. {
  76. // if(isScript){
  77. // // 插入模块名字提示
  78. // let file_name = filePath.substr(filePath.lastIndexOf('/') + 1)
  79. // let word = file_name.substr(0,file_name.lastIndexOf('.'))
  80. // this.completor.addCustomCompleter(word,word,file_name,this.parent.monaco.languages.CompletionItemKind.Reference,true);
  81. // }
  82. // else
  83. if(isUrlType){
  84. // 插入模块文件名提示
  85. let word = filePath.substr(12,filePath.lastIndexOf('.')-12)
  86. let matchInfo = filePath.match(/resources\/(.+)/)
  87. if(matchInfo){
  88. word = matchInfo[1];
  89. if(word.lastIndexOf('.plist') == -1){
  90. this.completor.addCustomCompleter(word,word,'',this.parent.monaco.languages.CompletionItemKind.Folder,true);
  91. }
  92. }
  93. }
  94. },
  95. // 面板销毁
  96. onDestroy(){
  97. },
  98. /************* 事件 *************/
  99. messages:{
  100. // 快捷键打开当前选中文件/节点进入编辑
  101. 'custom-cmd' (event,info) {
  102. },
  103. 'scene:saved'(){
  104. // Editor.log("事件 save")
  105. }
  106. },
  107. };