Composer.Editor (composer v0.2.0)

Copy Markdown View Source

Opens a file in the user's editor, for mix composer.recipe.edit and mix composer.step.edit.

The command is the first of these environment variables that is set:

  • COMPOSER_EDITOR
  • VISUAL
  • EDITOR

__FILE__ and __LINE__ in the command are replaced with the file and the line to open at, as in ELIXIR_EDITOR; a command without __FILE__ gets the (shell-quoted) file appended. __FILE__ is substituted as is, so it can sit inside quotes in the command:

# Neovim in the current terminal
export COMPOSER_EDITOR="nvim +__LINE__ __FILE__"

# Neovim in a new Ghostty window (Ghostty drops `+N` arguments,
# so the line goes in as `-c N`)
export COMPOSER_EDITOR="ghostty -e nvim -c __LINE__ __FILE__"

The command runs through sh -c and inherits the terminal (stdin, stdout and stderr), so a terminal editor takes it over until it exits; the task waits for the command to finish.

Summary

Functions

Builds the shell command that opens file at line, from env (a map of environment variables). Returns {:error, :no_editor} when none of vars/0 is set.

Opens file at line in the editor from the environment and waits for it to exit. Raises a Mix.Error when no editor is set or the editor exits with a non-zero status.

Runs command attached to the terminal and waits for it to exit. Returns {:error, {:exit, status}} on a non-zero exit status.

The environment variables read, in order of precedence.

Warns when file lies under the project's deps directory: edits there are lost when the dependency is updated.

Functions

command(file, line, env \\ System.get_env())

@spec command(Path.t(), pos_integer(), %{required(String.t()) => String.t()}) ::
  {:ok, String.t()} | {:error, :no_editor}

Builds the shell command that opens file at line, from env (a map of environment variables). Returns {:error, :no_editor} when none of vars/0 is set.

open!(file, line)

@spec open!(Path.t(), pos_integer()) :: :ok

Opens file at line in the editor from the environment and waits for it to exit. Raises a Mix.Error when no editor is set or the editor exits with a non-zero status.

run(command)

@spec run(String.t()) :: :ok | {:error, {:exit, non_neg_integer()}}

Runs command attached to the terminal and waits for it to exit. Returns {:error, {:exit, status}} on a non-zero exit status.

vars()

@spec vars() :: [String.t()]

The environment variables read, in order of precedence.

warn_if_dep(file)

@spec warn_if_dep(Path.t()) :: :ok

Warns when file lies under the project's deps directory: edits there are lost when the dependency is updated.