Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Where to put Lua files -> Lua文件位置 (Line 59) #6

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
52 changes: 26 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,22 @@ A few tutorials have already been written to help people write plugins in Lua. S
- [nlua.nvim](https://github.com/tjdevries/nlua.nvim) - Lua Development for Neovim
- [BetterLua.vim](https://github.com/euclidianAce/BetterLua.vim) - Better Lua syntax highlighting in Vim/NeoVim

## Where to put Lua files
## Lua文件位置

### init.lua

Neovim supports loading an `init.lua` file for configuration instead of the usual `init.vim`.
Neovim 支援加載配置 `init.lua` 的文件,而非一般常用的 `init.vim` 文件。

Note: `init.lua` is of course _completely_ optional. Support for `init.vim` is not going away and is still a valid option for configuration. Do keep in mind that some features are not 100% exposed to Lua yet.
注意: `init.lua` 是 _可自由選擇的_ ,並非是強制的。Neovim 仍然支援加載配置 `init.vim` 的文件。 但請記住,Neovim 的一些功能尚未 100% 地開放給 Lua 使用

See also:
另請參見:
- [`:help config`](https://neovim.io/doc/user/starting.html#config)

### Modules
### 模組

Lua modules are found inside a `lua/` folder in your `'runtimepath'` (for most users, this will mean `~/.config/nvim/lua` on \*nix systems and `~/AppData/Local/nvim/lua` on Windows). You can `require()` files in this folder as Lua modules.
Lua 模組通常會出現在您的 `'runtimepath'` 中的 `lua/` 資料夾中(對於大多數的使用者而言,在 \*nix 作業系統上為 `~/.config/nvim/lua` ,而在 Windows 作業系統上則為 `~/AppData/Local/nvim/lua` )。因此,您可以 `require()` 此資料夾中的檔案作為 Lua 模組。

Let's take the following folder structure as an example:
我們以下面的資料夾架構為例:

```text
📂 ~/.config/nvim
Expand All @@ -88,32 +88,32 @@ Let's take the following folder structure as an example:
└── 🇻 init.vim
```

The following Lua code will load `myluamodule.lua`:
以下的 Lua 代碼將會加載 `myluamodule.lua`

```lua
require('myluamodule')
```

Notice the absence of a `.lua` extension.
注意沒有 `.lua` 的副檔名

Similarly, loading `other_modules/anothermodule.lua` is done like so:
同樣地,加載 `other_modules/anothermodule.lua` 的過程如下:

```lua
require('other_modules.anothermodule')
-- or
require('other_modules/anothermodule')
```

Path separators are denoted by either a dot `.` or a slash `/`.
路徑分隔符可使用點 `.` 或著斜線 `/` 表示,以分隔路徑。

A folder containing an `init.lua` file can be required directly, without having to specify the name of the file.
若是檔案夾中包含 `init.lua` 檔案,則不需要指定該檔案名稱,可以直接引用該檔案夾

```lua
require('other_modules') -- loads other_modules/init.lua
```

Requiring a nonexistent module or a module which contains syntax errors aborts the currently executing script.
`pcall()` may be used to prevent errors.
加載一個不存在或是有語法錯誤的模組會直接中止目前正在執行的程式。
`pcall()` 函式可以用來避免這類錯誤。

```lua
local ok, _ = pcall(require, 'module_with_error')
Expand All @@ -122,20 +122,20 @@ if not ok then
end
```

See also:
另請參見:
- [`:help lua-require`](https://neovim.io/doc/user/lua.html#lua-require)

#### Tips
#### 小提醒

Several Lua plugins might have identical filenames in their `lua/` folder. This could lead to namespace clashes.
許多 Lua 插件或外掛在它們的 `lua/` 檔案夾中可能會有相同的檔案名稱,這可能會導致命名空間衝突

If two different plugins have a `lua/main.lua` file, then doing `require('main')` is ambiguous: which file do we want to source?
若是兩個不同的插件皆有一個 `lua/main.lua` 檔案,那麼執行 `require('main')` 是不明確的 : 我們是要加載哪個檔案?

It might be a good idea to namespace your config or your plugin with a top-level folder, like so: `lua/plugin_name/main.lua`
我們最好將自己的配置檔或插件命名為頂級檔案夾,以如下的形式: `lua/plugin_name/main.lua`

### Runtime files
### 執行中檔案

Much like Vimscript files, Lua files can be loaded automatically from special folders in your `runtimepath`. Currently, the following folders are supported:
Vimscript 檔案類似, 位於 `runtimepath` 中一些特殊目錄中的 Lua 檔案是可以被 Neovim 自動加載的. 目前有以下這些特殊目錄可支援使用此功能。

- `colors/`
- `compiler/`
Expand All @@ -145,15 +145,15 @@ Much like Vimscript files, Lua files can be loaded automatically from special fo
- `plugin/`
- `syntax/`

Note: in a runtime directory, all `*.vim` files are sourced before `*.lua` files.
注意: 在同一個執行目錄中, 所有的 `*.vim` 檔案會優先於 `*.lua` 檔案。

See also:
另請參見:
- [`:help 'runtimepath'`](https://neovim.io/doc/user/options.html#'runtimepath')
- [`:help load-plugins`](https://neovim.io/doc/user/starting.html#load-plugins)

#### Tips
#### 小提醒

Since runtime files aren't based on the Lua module system, two plugins can have a `plugin/main.lua` file without it being an issue.
由於執行中的檔案並不是基於 Lua 模組系統,因此兩個不同的插件都可以擁有 `plugin/main.lua` 檔案,不會產生任何問題。

## Using Lua from Vimscript

Expand Down Expand Up @@ -440,7 +440,7 @@ Alternatively, you can use the `:lua` command to pretty-print a Lua expression b

Additionally, you may find that built-in Lua functions are sometimes lacking compared to what you would find in other languages (for example `os.clock()` only returns a value in seconds, not milliseconds). Be sure to look at the Neovim stdlib (and `vim.fn`, more on that later), it probably has what you're looking for.

## Using Vimscript from Lua
## 在 Lua 中使用 Vimscript

### vim.api.nvim_eval()

Expand Down
Loading