首页 > 代码库 > HBuilder js 自定义代码块

HBuilder js 自定义代码块

  1 =begin   2 本文档是HBuilder预置的js代码块的文件。注意不要把其他语言的设置放到js里来。  3 如果用户修改此文档,HBuilder升级后会覆盖用户的修改,建议进入菜单 工具→扩展代码块 扩展相应的代码块。  4 若修改本文档,需要重启才能生效。修改过程中注意备份。  5 脚本开源地址 https://github.com/dcloudio/HBuilderRubyBundle  6 1、编辑代码块  7 如果要新增一个代码块,复制如下一段代码到空白行,然后设定参数。  8 snippet "document.getElementById" do |s|            #‘document.getElementById‘是代码块的显示名字;  9     s.trigger = "dgi"                               #‘dgi‘ 是激活字符,比如输入dgi均会在代码提示时显示该代码块; 10     s.expansion = "document.getElementById(\"$1\")" #expansion是设定该代码块的输出字符,其中$0、$1是光标的停留和切换位置。 11                                                     #如果输出涉及到换行和tab,也需严格在这里使用换行和tab。 12                                                     #输出双引号在前面加\来转义,输出$使用$$转义 13     s.needApplyReContentAssist = true               #输入代码块后立即在$1位置激活代码提示助手 14 end 15 2、编辑按键命令 16 如果要新增一个按键操作命令,复制如下一段代码到空白行,然后设定参数。 17   command t(:multicomment) do |cmd| #:首先起个名字,multicomment是名字 18     cmd.key_binding = ‘M1+M2+/‘ #这里绑定触发按键,这里是Ctrl+Shift+/ 19     cmd.input = :selection #输入内容是选中区域的内容 20     cmd.invoke do |context| 21       selection = ENV[‘TM_SELECTED_TEXT‘] || ‘‘ 22       # 如果选区长度大于0,则输出如下字符。回车符就使用真实回车。如下输出即在选中内容前后加上/* */的注释 23       if selection.length > 0 24         "/* 25 ${1:#{selection}} 26 */" 27       end 28     end 29   end 30 =end 31  32 require ‘ruble‘ 33  34 with_defaults :scope => "source.js" do   #=====JS代码块编辑=============================== 35    36   snippet t(:object_method) do |s| 37     s.trigger = ":f" 38     s.expansion = "${1:method_name}: function(${2:attribute}){ 39     $0 40 }${3:,}" 41   end 42    43   snippet "function" do |s| 44     s.trigger = "funn" 45     s.expansion = "function ${1:function_name} ($2) { 46     $3 47 }" 48   end 49    50   snippet t(:function_anonymous) do |s| 51     s.trigger = "funan" 52     s.expansion = "function ($1) { 53     $2 54 }" 55   end 56    57   snippet t(:function_closures) do |s| 58     s.trigger = "funcl" 59     s.expansion = "(function ($1) { 60     $2 61 })($3)" 62   end 63    64   snippet t(:prototype) do |s| 65     s.trigger = "proto" 66     s.expansion = "${1:class_name}.prototype.${2:method_name} = function(${3:first_argument}) { 67     ${0:// body...} 68 }; 69 " 70   end 71    72   snippet t(:if) do |s| 73     s.trigger = "iff" 74     s.expansion = "if ($1) { 75     $2 76 }" 77   end 78    79   snippet t(:if_else) do |s| 80     s.trigger = "ife" 81     s.expansion = "if ($1) { 82      83 } else{ 84      85 }" 86   end 87  88  89   snippet t(:if_compare) do |s| 90     s.trigger = "ifc" 91     s.expansion = "if ($1 == ${2:true}) { 92      93 } else{ 94      95 }" 96   end 97    98   snippet t(:for) do |s| 99     s.trigger = "forr"100     s.expansion = "for ($1) {101     $2102 }"103   end104 105   snippet t(:fori) do |s|106     s.trigger = "fori"107     s.expansion = "for (var i = 0; i < ${1:Things}.length; i++) {108     ${1:Things}[i]109 }"110   end111   112   snippet t(:with) do |s|113     s.trigger = "withh"114     s.expansion = "with ($1){115     $2116 }"117   end118   snippet t(:typeof) do |s|119     s.trigger = "typeoff"120     s.expansion = ‘typeof($1)=="${2:undefined/boolean/function/number/object/string}"‘121   end  122 123   snippet t(:switch_case) do |s|124     s.trigger = "switchcase"125     s.expansion = "switch (${1}){126     case ${2:value}:127         break;128     default:129         break;130 }"131   end132   133   snippet "while" do |s|134     s.trigger = "whilee"135     s.expansion = "while (${1:condition}){136     137 }"138   end139 140   snippet "var i=0;" do |s|141   s.trigger = "vari"142   s.expansion = "var ${1:i}=${2:0};"143   end144   145   snippet "var s=\"\";" do |s|146   s.trigger = "vars"147   s.expansion = "var ${1:s}=\"$2\";"148   end149   150   snippet "var a=[];" do |s|151   s.trigger = "vara"152   s.expansion = "var ${1:a}=[$2];"153   end154   155   snippet "var l=a.length;" do |s|156   s.trigger = "varl"157   s.expansion = "var ${1:l}=${2:a}.length;"158   end159 160   snippet "var c = canvas" do |s|161     s.trigger = "varc"162     s.expansion = "var ${2:c} = document.getElementById(\"$1\").getContext(\"2d\");"163     s.needApplyReContentAssist = true164   end165 166   snippet "var xhr" do |s|167     s.trigger = "varxhr"168     s.expansion = "var ${1:xhr} = new XMLHttpRequest();169 xhr.open(\"${2:GET/POST/PUT}\",\"$3\",${4:true/false});"170   end171   172   snippet "return true;" do |s|173   s.trigger = "returntrue"174   s.expansion = "return true;"175   end176 177   snippet "return false;" do |s|178   s.trigger = "returnfalse"179   s.expansion = "return false;"180   end181   182   snippet "console.log();" do |s|183   s.trigger = "consolelog"184   s.expansion = "console.log(\"$1\");"185   end186   187   snippet "try{}catch(e)" do |s|188   s.trigger = "trycatch"189   s.expansion = "try{190     $1191 }catch(e){192     //TODO handle the exception193 }"194   end195   196   snippet "$ (document.getElementById)" do |s|197     s.trigger = "$$$"198     s.expansion = "document.getElementById(\"$1\")"199     s.needApplyReContentAssist = true200   end201 202   snippet ‘$("")‘ do |s|203     s.trigger = "dl"204     s.expansion = "$$(\"$1\")"205     s.needApplyReContentAssist = true206   end207   snippet ‘$("#")‘ do |s|208     s.trigger = "dlid"209     s.expansion = "$$(\"\#$1\")"210     s.needApplyReContentAssist = true211   end212   snippet ‘$(".")‘ do |s|213     s.trigger = "dlclass"214     s.expansion = "$$(\".$1\")"215     s.needApplyReContentAssist = true216   end217   218   snippet "document.getElementById" do |s|219     s.trigger = "dgi"220     s.expansion = "document.getElementById(\"$1\")"221     s.needApplyReContentAssist = true222   end223 224   snippet "document.querySelectorAll" do |s|225     s.trigger = "dqs"226     s.expansion = "document.querySelectorAll(\"$1\")"227     s.needApplyReContentAssist = true228   end229   230   snippet "document.write" do |s|231     s.trigger = "dw"232     s.expansion = "document.write(\"$1\")"233   end234 235   snippet "navigator.userAgent;" do |s|236   s.trigger = "nuser"237   s.expansion = "navigator.userAgent"238   end239   240   snippet t(:object_value) do |s|241     s.trigger = ":,"242     s.expansion = "${1:value_name}:${0:value},"243   end244   245   snippet t(:object_key) do |s|246     s.trigger = ":"247     s.expansion = ‘${1:key}: ${2:"${3:value}"}${4:, }‘248   end249   250   snippet t(:setTimeout) do |s|251     s.trigger = "timeout"252     s.expansion = "setTimeout(function() {$0}, ${1:10});"253   end254   255   snippet t(:object_method_string) do |s|256     s.trigger = ‘:f‘257     s.expansion = "‘${1:${2:#thing}:${3:click}}‘: function(element){258     $0259 }${4:,}"260   end261   262   snippet "@alias" do |s|263     s.trigger = "@alias"264     s.locationType="JS_DOC"265     s.expansion = "@alias ${1:alias_name}"266   end267   268   snippet "@author" do |s|269     s.trigger = "@author"270     s.locationType="JS_DOC"271     s.expansion = "@author ${1:author}"272   end273   274   snippet "@classDescription" do |s|275     s.trigger = "@classDescription"276     s.locationType="JS_DOC"277     s.expansion = "@classDescription {${1:namespace}} ${2:description}"278   end279   280   snippet "@constructor" do |s|281     s.trigger = "@constructor"282     s.locationType="JS_DOC"283     s.expansion = "@constructor ${1:description}"284   end285   286   snippet "@example" do |s|287     s.trigger = "@example"288     s.locationType="JS_DOC"289     s.expansion = "@example ${1:example_code}"290   end291   292   snippet "@exception" do |s|293     s.trigger = "@exception"294     s.locationType="JS_DOC"295     s.expansion = "@exception {${1:exception_type}} ${2:description}"296   end297   298   snippet "@extends" do |s|299     s.trigger = "@extends"300     s.locationType="JS_DOC"301     s.expansion = "@extends {${1:parent_type}} ${2:description}"302   end303 304   snippet "@function" do |s|305     s.trigger = "@function"306     s.locationType="JS_DOC"307     s.expansion = "@function ${1}"308   end309     310   snippet "@internal" do |s|311     s.trigger = "@internal"312     s.locationType="JS_DOC"313     s.expansion = "@internal ${1}"314   end315   316   snippet "@param" do |s|317     s.trigger = "@param"318     s.locationType="JS_DOC"319     s.expansion = "@param {${1:boolean/function/number/object/string/AttrString/ClassString/ColorString/CSSString/CSSURIString/EventString/FontString/HTMLString/IDString/ImageURIString/JSURIString/MultimediaString/SelectorString/TagString/URIString}} ${2:param_name}"320   end321   322   snippet "@private" do |s|323     s.trigger = "@private"324     s.locationType="JS_DOC"325     s.expansion = "@private ${1}"326   end327 328   snippet "@property" do |s|329     s.trigger = "@property"330     s.locationType="JS_DOC"331     s.expansion = "@property ${1}"332   end333     334   snippet "@return" do |s|335     s.trigger = "@return"336     s.locationType="JS_DOC"337     s.expansion = "@return {${1:return_type}} ${2}"338   end339   340   snippet "@see" do |s|341     s.trigger = "@see"342     s.locationType="JS_DOC"343     s.expansion = "@see ${2:link}"344   end345   346   snippet "@type" do |s|347     s.trigger = "@type"348     s.locationType="JS_DOC"349     s.expansion = "@type {${1:boolean/function/number/object/string/AttrString/ClassString/ColorString/CSSString/CSSURIString/EventString/FontString/HTMLString/IDString/ImageURIString/JSURIString/MultimediaString/SelectorString/TagString/URIString}} ${2:param_name}"350   end351     352   snippet "@userAgent" do |s|353     s.trigger = "@userAgent"354     s.locationType="JS_DOC"355     s.expansion = "@userAgent ${1:browser1}:${2:version1},${3:browser2}:${4:version2}"356   end357   358 end359 360 361 with_defaults :scope => "source.js", :input => :none, :output => :insert_as_snippet do |bundle|362 =begin363   command t(:multicomment) do |cmd| #:首先起个名字,multicomment是名字364     cmd.key_binding = ‘M1+M2+/‘ #这里绑定触发按键,这里是Ctrl+Shift+/365     cmd.input = :selection #输入内容是选中区域的内容366     cmd.invoke do |context|367       selection = ENV[‘TM_SELECTED_TEXT‘] || ‘‘368       # 如果选区长度大于0,则输出如下字符。回车符就使用真实回车。如下输出即在选中内容前后加上/* */的注释369       if selection.length > 0370         "/*371 ${1:#{selection}}372 */"373       end374     end375   end376 =end377 end

 

HBuilder js 自定义代码块