Skip to content
Joshua Meng edited this page May 30, 2016 · 6 revisions

All plugins are luaexec scripts which would be executed when Lunar need to update apis and goto-definition.

This is the most important functionality of Lunar, developers can write certian plugin scripts to make the auto-completion & goto-definition meet their needs. But it is a little difficult because if you want more intelligent auto-completion & goto-definition, you have to write more compilcate scripts.

Traverse all the scripts under the project source directory, parse the content line by line and add functions matched by lua pattern or regex is the simplest way to write a procedure-oriented script plugin. But for object-oriented script like python, this is not so easy, you have to put the inherit things into considering.

How to write auto-completion plugin

Auto-completion list would always be there even if you do not write a plugin, because the invariant apis and word-tips would be loaded.

Except invariant apis and word-tips, there is another important part 'supplement api' in auto-completion, supplement api can help you get better programming experience with Lunar. Other than invariant apis defined in apis/... which loaded once for a script, supplement api are dynamic add / remove when project source changed. Every time user save file, parse_supplement_api script execute and supplement apis update.

Steps:

  1. Make a parse_supplement_api_xxx.lua file.

  2. Add a function.

    function parseSupplementApi(filename, cursor_line, project_src_dir)
    
  3. Implement it:

    • filename is current script file path.
    • cursor_line is the cursor line of current document.
    • project_src_dir is current script source dir which defined in extension.lua.
    • Implement source-parsing procedure. Lunar has a build-in luaexec engine to compile plugin script, you can use standard lua libraries and base/file/regex in luaextend. You still can use sendLog(message) as a debug tool.
    • Let parseSupplementApi return a table which contains current parsed supplement apis.
  4. Configure parse_supplement_api_xxx.lua in extension.lua.

How to write goto-definition plugin

Goto-definition is easier to implement than auto-completion plugin. You only need to parse all the source files and make a table to record all the functions with their file path and line number. Then you can check which function would be the one user want to goto, just return them.

Steps:

  1. Make a goto_xxx.lua file.

  2. Add a function.

    function gotoDefinition(text, line, filename, project_src_dir)
        
    
  3. Implement it:

    • text is the symbol user want to goto definition.
    • line is selected symbol line.
    • filename is current file path.
    • project_src_dir is current script source dir which defined in extension.lua
    • Implement goto-definition procedure. Lunar has a build-in luaexec engine to compile plugin script, you can use standard lua libraries and base/file/regex in luaextend. You still can use sendLog(message) as a debug tool.
    • Let gotoDefinition return a table which contains all destinitions user may want to goto. Destinition format is "{relative_path}\n{line_number}\n{line_text}".
    • Configure goto_xxx.lua in extension.lua.
Clone this wiki locally