Visual Studio 代码 + Lua

TyK*_*Ket 5 scripting intellisense lua visual-studio-code

据我所知,目前用于 Lua 的 Visual Studio Code 仅支持语法着色,我们可以有格式和一些带有扩展的片段。我需要知道的是是否存在或计划使用某种智能感知。

Ian*_*Ian 2

用于自定义语法突出显示和代码完成。
在 VSCode 中,安装扩展:Lua by sumneko
我不知道如何正确使用 emmyLua,所以我的示例并不完美,但它很容易设置,只需最少的努力。我的用例是 NLua 集成在 C# 中。只需要通过一些代码完成来编辑文件。

创建文件:Demo.lua

-- set the class a dummy name, since creator is the same name
-- eg. class name Point, creator name also Point will work
--     but will result in messy suggestions
---@class cPoint
---@field X number
---@field Y number

-- creator
---@type fun( x:number, y:number ) : cPoint
Point = {};

---@class Shapes
---@field Origin cPoint
local Shapes = nil;

---@type fun( x:number, y:number )
function Shapes:Move( x, y ) end

---@class cCircle : Shapes
---@field Radius number
local cCircle = {};           -- define to be able to ...

---@type fun( angle:number )
function cCircle:Roll( angle ) end  --    ... add methods

---@type fun( x:number, y:number, r:number ) : cCircle
Circle = {};

---@class cRectangle:Shapes
---@field Width number
---@field Height number
local cRectangle = {};

---@type fun( origin:cPoint, w:number, h:number ) : cRectangle
Rectangle = nil;

-- no method overload, so just force it
---@type fun( x:number, y:number, w:number, h:number ) : cRectangle
Rectangle = nil;
Run Code Online (Sandbox Code Playgroud)

创建另一个文件:test.lua

c = Circle( 10, 10, 10 );
c.Origin.X = 10;
c.Move( 10, 10 );
c.Roll( 10 );

r = Rectangle( Point( 0, 0 ), 10, 10 );
r = Rectangle( 10, 10, 10, 10 );

-- DETECTED ERRORS
c.origin.X = 10;
s = Shapes();
r.Roll( 10 );

-- NOT DETECTED
r = Rectangle( "hello" );
c = Circle(10,10);
c = Circle();
c.Roll();
Rectangle()
Run Code Online (Sandbox Code Playgroud)

结果