lua中实现switch以及sublime下自动补全switch语法
2017-05-15 11:00
435 查看
原文地址:http://blog.csdn.net/a82239946/article/details/42490405
刚开始使用lua的人肯定会不满lua居然没有switch这个语法。
但是熟悉lua的强大特性之后,你会发现其实switch是完全没有必要提供的^.^,因为lua有强大的table和function
例子:
模版如下:
local switch = {
[case1] = function()
--case 1
end,
[case2] = function()
--case 2
end,
}
local fSwitch = switch[key] --switch func
if fSwitch then --key exists
local result = fSwitch() --do func
else --key not found
end
为sublime增加switch自动补全
方法:
1、菜单-Preferences-Tools-New Snippet ,sublime自动新建一个文件
2、把代码全部复制进去,然后保存,文件名填 switch.sublime-snippet
方法二:
1、新建文本文档,把复制代码进去,然后保存,文件名写switch.sublime-snippet
2、sublime菜单-Preferences-Browse Packages打开文件夹
3、把刚才保存的switch.sublime-snippet文件放入User目录里面
这样就可以在sublime里面使用了
效果如下
刚开始使用lua的人肯定会不满lua居然没有switch这个语法。
但是熟悉lua的强大特性之后,你会发现其实switch是完全没有必要提供的^.^,因为lua有强大的table和function
例子:
local key = 1 local switch = { [1] = function() print("switch:"..1) end, [2] = function() print("switch:"..2) end, ["test"] = function() print("switch:test") end, } local fSwitch = switch[key] --switch func if fSwitch then --key exists local result = fSwitch() --do func else --key not found end
local key = 1 local switch = { [1] = function() print("switch:"..1) end, [2] = function() print("switch:"..2) end, ["test"] = function() print("switch:test") end, } local fSwitch = switch[key] --switch func if fSwitch then --key exists local result = fSwitch() --do func else --key not found end
模版如下:
local switch = {
[case1] = function()
--case 1
end,
[case2] = function()
--case 2
end,
}
local fSwitch = switch[key] --switch func
if fSwitch then --key exists
local result = fSwitch() --do func
else --key not found
end
local switch = { [case1] = function() --case 1 end, [case2] = function() --case 2 end, } local fSwitch = switch[key] --switch func if fSwitch then --key exists local result = fSwitch() --do func else --key not found end
为sublime增加switch自动补全
方法:
1、菜单-Preferences-Tools-New Snippet ,sublime自动新建一个文件
2、把代码全部复制进去,然后保存,文件名填 switch.sublime-snippet
<snippet> <content><![CDATA[local switch = { [${1:case1}] = function() ${2:--case 1} end, [${3:case2}] = function() ${4:--case 2} end, } local fSwitch = switch[${5:key}] --switch func if fSwitch then --key exists local result = fSwitch() --do func else --key not found end ]]></content> <tabTrigger>switch</tabTrigger> <scope>source.lua</scope> <description>switch-case</description> </snippet>
<snippet> <content><![CDATA[local switch = { [${1:case1}] = function() ${2:--case 1} end, [${3:case2}] = function() ${4:--case 2} end, } local fSwitch = switch[${5:key}] --switch func if fSwitch then --key exists local result = fSwitch() --do func else --key not found end ]]></content> <tabTrigger>switch</tabTrigger> <scope>source.lua</scope> <description>switch-case</description> </snippet>
方法二:
1、新建文本文档,把复制代码进去,然后保存,文件名写switch.sublime-snippet
2、sublime菜单-Preferences-Browse Packages打开文件夹
3、把刚才保存的switch.sublime-snippet文件放入User目录里面
这样就可以在sublime里面使用了
效果如下
相关文章推荐
- lua中实现switch以及sublime下自动补全switch语法
- lua中实现switch以及sublime下自动补全switch语法
- SciTE使用lua脚本实现配对符号自动补全功能
- Mac环境下Sublime 3 配置 Anaconda 实现python自动补全
- Linux 快速配置 VIM 实现语法高亮 自动补全 自动缩进以及文件管理等功能
- python、ipython的安装以及命令行自动补全功能实现
- ajax(DWR框架)实现简单的内容自动补全
- 用javascript实现始终保持打开同一个子窗口以及关闭父窗口同时自动关闭所有子窗口
- Web报表中用switchCase实现不同条件自动计算不同的表达式
- Windows下为GTK库添加tags并在VIM中实现代码自动补全和跳转
- 关于字符和数字类型的索引,Oracle如何实现内部自动转换以及索引使用的验证测试
- Reporting Service Tips 101(#4) - 使用RS实现报表的自动生成以及在程序中调用RS
- Vim 中实现括号自动补全
- 在 Vim 中实现括号自动补全
- vim 编辑 java javascript 自动补全{([ 以及输入分号时在行尾加分号
- JQuery实现文本框自动补全(修改版)
- Jquery实现自动补全
- 如何实现输入字母就可以查出数据库中以该字母匹配的中文实现自动补全功能
- xkx mushclient 全自动walk lua实现