Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,37 @@ Using **lazy.nvim**:
- Execute a command in the running container. If `cmd` is omitted, you’ll be prompted. Common patterns: build, test, codegen.
- `:DevcontainerDown`
- Stop and remove the container (see `remove_existing_container` if you want to always start clean).
- `:DevcontainerSelectConfig`
- On the host, pick which `devcontainer.json` subsequent plugin commands should use. It does not switch the container of an existing Neovim process.
- `:DevContainerToggle`
Comment thread
coderabbitai[bot] marked this conversation as resolved.
- Toggle the last devcontainer terminal window.

---

## Multiple dev container configs

A project may define more than one config, for example:

```text
.devcontainer/devcontainer.json
.devcontainer/gpu/devcontainer.json
.devcontainer/cpu/devcontainer.json
```

The first command you run on the host (`:DevcontainerUp`, `:DevcontainerExec`, …) asks you to pick one, listing each config's `name` alongside its path. That choice is reused for the rest of that host Neovim session; use `:DevcontainerSelectConfig` to change the config used by subsequent commands, or set `reuse_fixed_path = false` to be asked every time.

Selecting a config inside a devcontainer cannot move the current Neovim process to another container. To switch, return to the host Neovim, run `:DevcontainerSelectConfig`, then `:DevcontainerUp` and `:DevcontainerConnect`.

To skip the prompt altogether, pin a config (path relative to the workspace folder, or absolute):

```lua
require("devcontainer-cli").setup({
fixed_devcontainer_json_path = ".devcontainer/gpu/devcontainer.json",
})
```

---

## Configuration

Call `require("devcontainer-cli").setup({ ... })` with any of the following options (defaults shown):
Expand All @@ -121,6 +147,13 @@ Call `require("devcontainer-cli").setup({ ... })` with any of the following opti
-- Search upwards and use the nearest `.devcontainer/` folder
toplevel = true,

-- Pin a specific devcontainer.json (relative to the workspace folder, or
-- absolute). When nil, you are asked to pick one if the project has several
fixed_devcontainer_json_path = nil,

-- Reuse the config you picked instead of asking again on every command
reuse_fixed_path = true,

-- Start from scratch each `DevcontainerUp` (slower but clean)
remove_existing_container = true,

Expand Down
15 changes: 15 additions & 0 deletions doc/devcontainer-cli.nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,19 @@ DevcontainerConnect *DevcontainerConnect*

DevcontainerDown *DevcontainerDown*
Stops and removes the devcontainer associated with the current project.

DevcontainerSelectConfig *DevcontainerSelectConfig*
On the host, choose which devcontainer.json subsequent DevcontainerUp,
DevcontainerExec, DevcontainerConnect and DevcontainerDown commands should
use. This does not change the container in which nvim is currently running.
To switch containers, return to the host nvim, select another config, run
DevcontainerUp and then DevcontainerConnect.

When a project contains several configs (`.devcontainer/devcontainer.json`,
`.devcontainer.json` and/or `.devcontainer/<name>/devcontainer.json`) the
other commands prompt for one the first time they are used and remember that
choice in the current host nvim session.

Set `fixed_devcontainer_json_path` to pin a config and skip the prompt, or
`reuse_fixed_path = false` to be asked on every command.
================================================================================
1 change: 1 addition & 0 deletions doc/tags
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
DevcontainerConnect devcontainer-cli.nvim.txt /*DevcontainerConnect*
DevcontainerDown devcontainer-cli.nvim.txt /*DevcontainerDown*
DevcontainerExec devcontainer-cli.nvim.txt /*DevcontainerExec*
DevcontainerSelectConfig devcontainer-cli.nvim.txt /*DevcontainerSelectConfig*
DevcontainerToggle devcontainer-cli.nvim.txt /*DevcontainerToggle*
DevcontainerUp devcontainer-cli.nvim.txt /*DevcontainerUp*
devcontainer-cli.nvim devcontainer-cli.nvim.txt /*devcontainer-cli.nvim*
Expand Down
7 changes: 7 additions & 0 deletions lua/devcontainer-cli/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ local default_config = {
interactive = false,
-- use the .devcontainer directory closest to root in the directory tree
toplevel = true,
-- path of the devcontainer.json to use, either absolute or relative to the
-- workspace folder, e.g. ".devcontainer/gpu/devcontainer.json". When unset
-- and the workspace holds more than one, you are asked to pick one
fixed_devcontainer_json_path = nil,
-- reuse the devcontainer.json you picked for a workspace for the rest of the
-- nvim session instead of asking again on every command
reuse_fixed_path = true,
-- Folder where the nvim-devcontainer-cli is installed
nvim_plugin_folder = file_path:gsub("init.lua", "") .. "../../../",
-- Remove existing container each time DevcontainerUp is executed
Expand Down
21 changes: 14 additions & 7 deletions lua/devcontainer-cli/devcontainer_cli.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
-- SOFTWARE.
local utils = require("devcontainer-cli.devcontainer_utils")
local terminal = require("devcontainer-cli.terminal")
local log = require("devcontainer-cli.log")

local M = {}

Expand Down Expand Up @@ -61,15 +60,23 @@ function M.up()
utils.bringup()
end

-- choose which devcontainer config to use when the project has several
function M.select_config()
utils.select_config()
end

-- Thanks to the autocommand executed after leaving the UI, after closing the
-- neovim window the devcontainer will be automatically open in a new terminal
function M.connect()
if not utils.create_connect_cmd() then
log.error("Failed to create autocommand")
return
end

vim.cmd("wqa")
utils.create_connect_cmd(
function()
vim.schedule(
function()
vim.cmd("wqa")
end
)
end
)
end

-- kill the current running docker container associated with the current project
Expand Down
Loading