工作

Zed 支援使用其整合終端機產生(和重新執行)命令以輸出結果的方法。這些命令可以讀取 Zed 狀態的有限子集(例如目前正在編輯的檔案路徑或選取的文字)。

[
  {
    "label": "Example task",
    "command": "for i in {1..5}; do echo \"Hello $i/5\"; sleep 1; done",
    //"args": [],
    // Env overrides for the command, will be appended to the terminal's environment from the settings.
    "env": { "foo": "bar" },
    // Current working directory to spawn the command into, defaults to current project root.
    //"cwd": "/path/to/working/directory",
    // Whether to use a new terminal tab or reuse the existing one to spawn the process, defaults to `false`.
    "use_new_terminal": false,
    // Whether to allow multiple instances of the same task to be run, or rather wait for the existing ones to finish, defaults to `false`.
    "allow_concurrent_runs": false,
    // What to do with the terminal pane and tab, after the command was started:
    // * `always` — always show the terminal pane, add and focus the corresponding task's tab in it (default)
    // * `never` — avoid changing current terminal pane focus, but still add/reuse the task's tab there
    "reveal": "always"
  }
]

有兩個動作驅動使用工作的流程:task: spawntask: reruntask: spawn 會開啟一個包含目前檔案中所有可用工作的模式視窗。task: rerun 會重新執行最近產生的工作。您也可以從工作模式視窗重新執行工作。

工作範本

可以定義工作

  • 在全域 tasks.json 檔案中;此類工作在您處理的所有 Zed 專案中都可用。此檔案通常位於 ~/.config/zed/tasks.json。您可以使用 zed: open tasks 動作來編輯它們。
  • 在特定工作樹(本地)的 .zed/tasks.json 檔案中;此類工作僅在處理包含該工作樹的專案時可用。您可以使用 zed: open local tasks 來編輯特定工作樹的工作。
  • 透過一次性工作動態產生。這些工作是特定於專案的,並且不會跨區段持續存在。
  • 透過語言擴充功能。

變數

Zed 工作就像您的 shell 一樣運作;這也表示您可以透過類似 sh 的 $VAR_NAME 語法來參考環境變數。為了方便起見,我們設定了幾個額外的環境變數。這些變數允許您從目前的編輯器中提取資訊,並在您的工作中使用它們。以下變數可用:

  • ZED_COLUMN:目前的欄位位置
  • ZED_ROW:目前的列數
  • ZED_FILE:目前開啟檔案的絕對路徑(例如 /Users/my-user/path/to/project/src/main.rs
  • ZED_FILENAME:目前開啟檔案的檔名(例如 main.rs
  • ZED_DIRNAME:目前開啟檔案的絕對路徑,不含檔名(例如 /Users/my-user/path/to/project/src
  • ZED_RELATIVE_FILE:目前開啟檔案的路徑,相對於 ZED_WORKTREE_ROOT(例如 src/main.rs
  • ZED_STEM:目前開啟檔案的主檔名(不含副檔名)(例如 main
  • ZED_SYMBOL:目前選取的符號;應該與符號階層連結中顯示的最後一個符號相符(例如 mod tests > fn test_task_contexts
  • ZED_SELECTED_TEXT:目前選取的文字
  • ZED_WORKTREE_ROOT:目前工作樹根目錄的絕對路徑。(例如 /Users/my-user/path/to/project
  • ZED_CUSTOM_RUST_PACKAGE:(Rust 特定)$ZED_FILE 原始程式檔的父套件名稱。

若要在工作中使用變數,請在變數前面加上一個錢字號 ($)

{
  "label": "echo current file's path",
  "command": "echo $ZED_FILE"
}

您也可以使用詳細語法,在指定的變數不可用時指定預設值:${ZED_FILE:default_value}

這些環境變數也可以在工作的 cwdargslabel 欄位中使用。

一次性工作

透過 task: spawn 開啟的相同工作模式視窗支援任意類似 bash 的命令執行:在模式視窗文字欄位中輸入命令,然後使用 opt-enter 產生該命令。

工作模式視窗會在目前的 Zed 工作階段中保留這些命令的清單,如果這些命令是最後產生的命令,task: rerun 也會重新執行這些命令。

您也可以在模式視窗中調整目前選取的工作(opt-e 是預設的按鍵綁定)。這樣做會將其命令放入提示中,然後可以編輯並產生為一次性工作。

暫時性工作

您可以在透過模式視窗產生工作時使用 cmd 修飾鍵;以這種方式產生的工作不會增加其使用次數(因此,它們不會使用 task: rerun 重新產生,並且它們在工作模式視窗中的排名也不會很高)。暫時性工作的預期用途是在持續使用 task: rerun 的情況下保持流程順暢。

工作的自訂按鍵綁定

您可以透過 task::Spawn 的額外參數為您的工作定義自己的按鍵綁定。如果您想將上述的 echo current file's path 工作綁定到 alt-g,您可以在您的keymap.json 檔案中新增以下程式碼片段

{
  "context": "Workspace",
  "bindings": {
    "alt-g": ["task::Spawn", { "task_name": "echo current file's path" }]
  }
}

將可執行標籤綁定到工作範本

Zed 支援透過工作區本機和全域 tasks.json 檔案,以以下優先順序階層覆寫內嵌可執行指示器的預設動作

  1. 工作區 tasks.json
  2. 全域 tasks.json
  3. 語言提供的標籤綁定(預設)。

若要標記工作,請將可執行標籤名稱新增到工作範本上的 tags 欄位

{
  "label": "echo current file's path",
  "command": "echo $ZED_FILE",
  "tags": ["rust-test"]
}

這樣一來,您就可以變更在可執行指示器中顯示的工作。