使用IDEA進行Lua代碼調試、自動提示、代碼跳轉、智能重命名


試了幾個Lua IDE后,Lua Studio、Lua Glider、VS+babelua插件、Sublime都不是特別滿意。直到發現了國人自創的另一個神奇工具:基於IDEA的EmmyLua插件。該插件功能非常完整:斷點調試、自動提示、代碼跳轉、智能重命名等,可以極大地提高Lua編程的速度。界面也比較舒服。

安裝步驟

  1. 下載、安裝並破解IntelliJ IDEA(我的版本是目前最新的2017.3.4)
  2. 安裝EmmyLua插件 
    建議直接在IDEA工具內搜索插件安裝(當然也可以下載插件到硬盤安裝)。 
    (1)在啟動界面點擊Configure-Plugins: 
    這里寫圖片描述 
    (2)搜索EmmyLua,然后點Search in repositories: 
    這里寫圖片描述
    (3)點擊右邊的Install綠色按鈕。安裝完需要重啟IDEA。
  3. 配置SDK,默認是Path路徑需要有一個Lua解釋器(lua.exe)。你可以在創建項目時指定其他目錄。
  4. 配置其他事項。 
    (1)將*.txt識別成lua文件: 
    這里寫圖片描述
    (2)忽略代碼提示大小寫差別: 
    這里寫圖片描述

創建項目

  1. 創建普通Lua項目(不依賴其他程序,如游戲引擎): 
    New-Project,然后next,填項目名、路徑,點擊finish。 
    在項目視圖的src文件夾New一個Lua文件,可以自己print()一下,Run一下,看看有沒有輸出,有的話,說明SDK配置正確。 
    這里寫圖片描述 
    這里寫圖片描述

  2. 創建Unity引擎Lua項目 
    New-Modules from existing sources(注意不要選錯,這里創建的是Modules,不是Project,否則等下導入不了api自動提示的library)。【這里有一個IDE Bug:第一次創建Modules,會在文件夾里生成一個.iml文件。但是如果文件夾里本來就有.iml文件,以后再點Modules from existing sources就會無法生成Modules,也就無法導入library。這個Bug我折騰了一晚上才發現的!必須刪掉.iml文件,才可以重新創建Modules】 
    然后選擇Unity文件夾的Lua訪問根目錄,我選的是Resources文件夾,因為可以從Resources作為根目錄搜索lua文件。 
    然后我們測試一下斷點調試功能。打開其中一個Lua文件,設置斷點: 
    這里寫圖片描述
    然后Run-Attach To Local Process: 
    這里寫圖片描述 
    選擇Unity進程,觸發斷點,說明能斷點調試: 
    這里寫圖片描述

Unity API代碼提示

現在Unity API代碼提示是沒有的,因為我們還沒導入API描述的library。這個library根據你選擇的Lua中間件不同而不同,所以建議是自己導出。我的Lua中間件是SLua。這里以SLua為例。 
1.打開SLua官方自帶的Unity項目,在Slua-Editor下面,新建一個SLuaApiExporter.cs腳本: 
這里寫圖片描述 
2.輸入如下代碼:

using System; using System.Reflection; using System.Collections.Generic; using System.Linq; using System.Text; using UnityEditor; using SLua; using System.IO; using UnityEngine; namespace Slua { public static class EmmyLuaApiExporter { [MenuItem("SLua/導出EmmyLuaApi", false, 14)] static void Gen() { string path = "./EmmyApi/"; if (Directory.Exists(path)) { Directory.Delete(path, true); } Directory.CreateDirectory(path); //UnityEngine GenAssembly("UnityEngine", path); //GenAssembly("UnityEngine.UI", path); GenCustom(path); } public static void GenAssembly(string name, string path) { List<string> excludeList; List<string> includeList; CustomExport.OnGetNoUseList(out excludeList); CustomExport.OnGetUseList(out includeList); Type[] types = Assembly.Load(name).GetTypes(); foreach (Type t in types) { if (LuaCodeGen.filterType(t, excludeList, includeList)) { GenType(t, false, path); } } } public static void GenCustom(string path) { Type[] types = Assembly.Load("Assembly-CSharp-firstpass").GetTypes(); foreach (Type t in types) { if (t.IsDefined(typeof(CustomLuaClassAttribute), false)) { GenType(t, true, path); } } types = Assembly.Load("Assembly-CSharp").GetTypes(); foreach (Type t in types) { if (t.IsDefined(typeof(CustomLuaClassAttribute), false)) { GenType(t, true, path); } } } public static void GenType(Type t, bool custom, string path) { if (!CheckType(t, custom)) return; //TODO System.MulticastDelegate var sb = new StringBuilder(); if (!CheckType(t.BaseType, custom)) sb.AppendFormat("---@class {0}\n", t.Name); else sb.AppendFormat("---@class {0} : {1}\n", t.Name, t.BaseType.Name); GenTypeField(t, sb); sb.AppendFormat("local {0}={{ }}\n", t.Name); GenTypeMehod(t, sb); sb.AppendFormat("{0}.{1} = {2}", t.Namespace, t.Name, t.Name); File.WriteAllText(path + t.FullName + ".lua", sb.ToString(), Encoding.UTF8); } static bool CheckType(Type t, bool custom) { if (t == null) return false; if (t == typeof(System.Object)) return false; if (t.IsGenericTypeDefinition) return false; if (t.IsDefined(typeof(ObsoleteAttribute), false)) return false; if (t == typeof(YieldInstruction)) return false; if (t == typeof(Coroutine)) return false; if (t.IsNested) return false; if (custom && !t.IsDefined(typeof(CustomLuaClassAttribute), false)) return false; return true; } public static void GenTypeField(Type t, StringBuilder sb) { FieldInfo[] fields = t.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly); foreach(var field in fields) { if (field.IsDefined(typeof(DoNotToLuaAttribute), false)) continue; sb.AppendFormat("---@field public {0} {1}\n", field.Name, GetLuaType(field.FieldType)); } PropertyInfo[] properties = t.GetProperties(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly); foreach (var pro in properties) { if (pro.IsDefined(typeof(DoNotToLuaAttribute), false)) continue; sb.AppendFormat("---@field public {0} {1}\n", pro.Name, GetLuaType(pro.PropertyType)); } } public static void GenTypeMehod(Type t, StringBuilder sb) { MethodInfo[] methods = t.GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly); foreach (var method in methods) { if (method.IsGenericMethod) continue; if (method.IsDefined(typeof(DoNotToLuaAttribute), false)) continue; if(method.Name.StartsWith("get_") || method.Name.StartsWith("set_")) continue; sb.AppendLine("---@public"); var paramstr = new StringBuilder(); foreach (var param in method.GetParameters()) { sb.AppendFormat("---@param {0} {1}\n", param.Name, GetLuaType(param.ParameterType)); if (paramstr.Length != 0) { paramstr.Append(", "); } paramstr.Append(param.Name); } sb.AppendFormat("---@return {0}\n", method.ReturnType == null ? "void" : GetLuaType(method.ReturnType)); if( method.IsStatic) { sb.AppendFormat("function {0}.{1}({2}) end\n", t.Name, method.Name, paramstr); } else { sb.AppendFormat("function {0}:{1}({2}) end\n", t.Name, method.Name, paramstr); } } } static string GetLuaType(Type t) { if (t.IsEnum //|| t == typeof(ulong) //|| t == typeof(long) //|| t == typeof(int) //|| t == typeof(uint) //|| t == typeof(float) || t == typeof(double) //|| t == typeof(byte) //|| t == typeof(ushort) //|| t == typeof(short) ) return "number"; if (t == typeof(bool)) return "bool"; if (t == typeof(string)) return "string"; if (t == typeof(void)) return "void"; return t.Name; } } } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184

3.在Unity編輯器中點擊SLua-導出EmmyLuaApi 
這里寫圖片描述

4.看到Unity工程項目會多出一個EmmyApi文件夾: 
這里寫圖片描述

5.將其打包成zip文件(注意不能是rar、7z其它壓縮格式!) 
這里寫圖片描述

6.在IDEA中點擊File-Project Structure,Modules-選擇我們的Modules-Dependencies,+號-Library-Lua Zip Library,選擇我們剛才打包的zip文件。然后一直OK保存就行了。 
這里寫圖片描述

這里寫圖片描述

7.測試Unity API提示功能: 
這里寫圖片描述

成功!

其它功能

代碼跳轉: 
這里寫圖片描述

智能重命名: 
這里寫圖片描述 
這里寫圖片描述

后續

本教程就到這里結束了,但是該插件還有許多有用的功能,可以自行探索,也可以加入EmmyLua的官方QQ群:29850775。群里面有許多教程,本文所用的API導出代碼也是從群文件里拿出來改的。

github: https://github.com/tangzx/IntelliJ-EmmyLua 
oschina: http://git.oschina.net/tangzx/IntelliJ-Lua 
IDEA Plugins : https://plugins.jetbrains.com/plugin/9768-emmylua

最后感謝EmmyLua的作者們無私開源編寫了這個強大的插件。

 

參考鏈接:https://blog.csdn.net/sinat_24229853/article/details/79226608

                  https://blog.csdn.net/u010019717/article/details/77510066?ref=myread

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM