Emmylua annotation function

Time:2022-1-25

preface

Online configurationEmmyLuaThere are many methods, which will not be repeated here (so the premise is that you have installed and configured emmylua)

This article is only forEmmylua plug-inwithinAnnotation functionCode demonstration of usage. This article is written because most emmylua configuration tutorials on the Internet do not explain this part, and the annotation function of emmylua is almost indispensable during actual Lua development

Purpose of annotation

When we write c# scripts, the relevant plug-ins of IDE can prompt various methods or members and descriptions:

However, in Lua, even if emmylua is installed and no annotation is written, there will be no prompt (the gray prompt only indicates that the parameter has just been written, and it is completely unknown whether it is a member variable or method):

Therefore, emmylua annotation function is to solve this problem:Simulation implementation code prompt

usage

Class declaration

Basic format:[email protected] MY_TYPE[:PARENT_TYPE] [@comment]

[email protected] person
Person = {};

[email protected] gamer: person player
Gamer = {};

Additional properties of class

Even if the class does not hold a certain attribute, it can also appear in the prompt by adding annotations (PS: in fact, emmylua’s prompt for implementing unityapi is also based on this)

Basic format:[email protected] [public|protected|private] field_name FIELD_TYPE[|OTHER_TYPE] [@comment]

[email protected] person
[email protected] public name string name
[email protected] private m_ Age number
Person = {
    Name = "",
};

Type of variable

Basic format:[email protected] MY_TYPE[|OTHER_TYPE] [@comment]

Dimension array:[email protected] MY_TYPE[]

Label Dictionary:[email protected] table

PS: according to the above official usage, the comment description should be placed at the end, but I tried the next line, which can be placed at the top

---My ID
[email protected] number
myId = 1;

---Example of Gamer
[email protected] Gamer
gamerA = Gamer:Create();

---Player array
[email protected] Gamer[]
gamersArr = {};

---Player's Dictionary
[email protected] table
gamersTable = {};

function

Label the type of function definition parameter:[email protected] param_name MY_TYPE[|other_type] [@comment]

Return value type of annotation function:[email protected] MY_TYPE[|OTHER_TYPE] [@comment]

Indefinite parameters of dimension function:[email protected] TYPE

---Get lines
[email protected] param ISCN Boolean Chinese
[email protected] ID number line dictionary ID
[email protected] string lines
function GetLines(isCN, id)
    local str = "";    -- do something
    return str;
end

---Cumulative summation
[email protected] number is the accumulated number
[email protected] number and
function AddNum(...)
    local total = 0;
    for _, v in pairs{...} do
        total = total + v;
    end
    return total;
end

Alias of type

For the annotation of variable types, the above method is usually used[email protected]Just enough (common types arestring|number|boolean|table|...Or through[email protected]Declared user-defined types), but if complex types (such as closure functions) are encountered, alias annotations can be used,Register some complex types that are not easy to enter as a new alias

Basic format:[email protected] NEW_NAME TYPE

---Function to generate log function of print target level
[email protected] loglevel number log level
[email protected] logprinter print log function
function GenLogPrinter(logLevel)

    [email protected] LogPrinter fun(logMsg : string) : void
    return function(logMsg)
        if(logLevel == 1) then
            print("Log : " .. logMsg);
        elseif(logLevel == 2) then
            print("Warning : " .. logMsg);
        end
    end
end

[email protected] LogPrinter
local LP = GenLogPrinter(1);
[email protected] LogPrinter
local LP_Warning = GenLogPrinter(2);

LP("a normal log.");  	         -- Log : a normal log.
LP_Warning("a warning log.");  	 -- Warning : a warning log.

Embedded language

It is used to mark that a piece of text is in some code format (JSON, XML, Java, etc.), so that it can be highlighted

Basic format:[email protected] LANGUAGE_ID

[email protected] JSON
local jsonText = [[{
    "name":"Joker",
    "age": 18
}]]

[email protected] XML
local XMLText = [[
    
        
        
    
]]

remarks

Under idea, use Alt + enter shortcut key (or click a small light bulb) for the target to automatically complete the annotation:

Specific examples

existingPersonBase class,GamerClass inherits fromPerson, in main Two classes can be created and used in Lua (just stick to the local one):

Main.lua:

require("Person");
require("Gamer");

[email protected] Person
local pa = Person:Create("joker", 18);
pa:ShowInfo();
pa:ReName("Joker");
pa:ShowInfo()

[email protected] Gamer
local ga = Gamer:Create("fox", 19, nil, nil);
ga:ShowInfo();
ga:ReName("Fox");
ga:ReGamerInfo("123", "456");
ga:ShowInfo();

Person.lua:

[email protected] person type
[email protected] public name string name
[email protected] private m_ Age number
Person = {
    Name = "",
    m_Age = 0,
};

Person.__index = Person;

---Create
[email protected] name string
[email protected] age number
function Person:Create(name, age)
    [email protected] Person
    local t = {};

    setmetatable(t, Person);

    t:ReName(name);
    t:ReAge(age);

    return t;
end

---ReName
[email protected] newName string
[email protected]
function Person:ReName(newName)
    self.Name = newName;
end

---ReAge
[email protected] newAge number
[email protected]
function Person:ReAge(newAge)
    self.m_Age = newAge;
end

---ShowInfo
[email protected]
function Person:ShowInfo()
    print("Name = " .. self.Name .. ", Age = " .. self.m_Age);
end

Gamer.lua:

require("Person")

[email protected] gamer: person player
[email protected] private SW string SW code
[email protected] private steamid string steam link
Gamer = {
    SW = "",
    SteamId = "",
};

Gamer.__index = Gamer;
setmetatable(Gamer, Person);

function Gamer:Create(name, age, sw, steamId)
    [email protected] Gamer
    local t = {};
    t = Person:Create(name, age);

    setmetatable(t, Gamer);

    t:ReGamerInfo(sw, steamId);

    return t;
end

---ReGamerInfo
[email protected] sw string
[email protected] steamId string
[email protected]
function Gamer:ReGamerInfo(sw, steamId)
    self.SW = sw or "0";
    self.SteamId = steamId or "0";
end

---ShowInfo
[email protected]
function Gamer:ShowInfo()
    print("Name = " .. self.Name .. ", Age = " .. self.m_Age .. ", SW = " .. self.SW .. ", SteamId = " .. self.SteamId);
end

PS: YesImplement object-oriented features in Lua — simulation class, inheritance and polymorphism horseBig guy, this code is for practice

Reference articles

Recommended Today

IOS memory partition

#Stack area It is a continuous data structure expanded from high address to low address, which is used to store the values of local variables and function parameters #Heap area Programmers need to manage and release the data structure domain expanded by low address like high address and discontinuous memory area #Global area Global variables […]