|
| 1 | +# Auto Import |
| 2 | + |
| 3 | +Nowadays, most language plugins have auto import functions. Due to the inconsistent modularization of Lua in this respect, there are large differences in personal understanding and use of modules, resulting in almost no auto import plugins. |
| 4 | + |
| 5 | +This project provides a commonly used auto import function that is almost a standard way of importing modules. |
| 6 | + |
| 7 | +## lua.module.json configuration file |
| 8 | + |
| 9 | +The auto import function no longer uses the .editorconfig file as a configuration file. Use lua.module.json as the configuration file. In which directory the file is located, all lua files in that directory will use lua.module.json in that directory as the module configuration. And the starting directory of the extension is this directory. |
| 10 | + |
| 11 | +If there are multiple lua.module.json in the workspace, the principle of proximity is adopted, which means that the module configuration of a file is based on the lua.module.json closest to it. |
| 12 | + |
| 13 | +### lua.module.json basic form |
| 14 | +```json |
| 15 | +{ |
| 16 | + "name": "root", |
| 17 | + "import": [ |
| 18 | + "root" |
| 19 | + ] |
| 20 | +} |
| 21 | +``` |
| 22 | + |
| 23 | +This basic form defines the module name and the imported module name, that is to say, after the module (lua.modudle.json is called the module) defines its own name, the name can be imported through the import field for all lua files that the module can affect , You can only see the lua module path introduced by the module name introduced in the import field. |
| 24 | + |
| 25 | +In other words for the following directory structure |
| 26 | +```plaintext |
| 27 | +scirpt |
| 28 | +|-lua.module.json |
| 29 | +|-aaa.lua |
| 30 | +|-lib |
| 31 | +| |-lua.module.json |
| 32 | +| |-bbb.lua |
| 33 | +``` |
| 34 | + |
| 35 | +If the contents of lua.module.json are respectively |
| 36 | +```json |
| 37 | +// script/lua.module.json |
| 38 | +{ |
| 39 | + "name": "root", |
| 40 | + "import": [ |
| 41 | + "root", "lib" |
| 42 | + ] |
| 43 | +} |
| 44 | +// script/lib/lua.module.json |
| 45 | +{ |
| 46 | + "name": "lib", |
| 47 | + "import": [ |
| 48 | + "lib" |
| 49 | + ] |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +Then the bbb file in the lib directory cannot see the aaa.lua file during auto import because it did not introduce the root module |
| 54 | +The script/aaa.lua file can see the bbb.lua file because it introduced the lib module name |
| 55 | + |
| 56 | +### Configuration "separator" |
| 57 | + |
| 58 | +This configuration item determines the path separator symbol during auto import. Generally speaking, Lua recommends'.' as the module path separator symbol, but many items are not sufficiently standardized to use'/' as the separator. |
| 59 | + |
| 60 | +The default value of this configuration item is'.' |
| 61 | + |
| 62 | +### Configuration "import.function" |
| 63 | + |
| 64 | +This configuration item indicates the import function used in auto import. Generally speaking, the Lua standard uses the require function, but many projects have also created a function with slightly different usage and semantics to import modules such as import |
| 65 | + |
| 66 | +The default value of this configuration item is "require" |
| 67 | + |
| 68 | +### Configuration item special.module |
| 69 | + |
| 70 | +The configuration item is an array, the basic form is |
| 71 | +```json |
| 72 | +{ |
| 73 | + "special.module": [ |
| 74 | + {"name": "socket_core", "module": "socket.core"} |
| 75 | + ] |
| 76 | +} |
| 77 | +``` |
| 78 | +This configuration item is to set the auto import matching name of some existing modules. When the above settings are set, the socket.core module will be automatically imported when socket_core is entered. |
0 commit comments