Skip to content

feat: add workspace_directory and current_working_directory variable expansions #13068

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions book/src/command-line.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ The following variables are supported:
| `cursor_column` | The column number of the primary cursor in the currently focused document, starting at 1. This is counted as the number of grapheme clusters from the start of the line rather than bytes or codepoints. |
| `buffer_name` | The relative path of the currently focused document. `[scratch]` is expanded instead for scratch buffers. |
| `line_ending` | A string containing the line ending of the currently focused document. For example on Unix systems this is usually a line-feed character (`\n`) but on Windows systems this may be a carriage-return plus a line-feed (`\r\n`). The line ending kind of the currently focused document can be inspected with the `:line-ending` command. |
| `current_working_directory` | Current working directory |
| `workspace_directory` | Nearest ancestor directory of the current working directory that contains `.git`, `.svn`, `jj` or `.helix` |

Aside from editor variables, the following expansions may be used:

Expand Down
21 changes: 21 additions & 0 deletions helix-view/src/expansion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ pub enum Variable {
BufferName,
/// A string containing the line-ending of the currently focused document.
LineEnding,
/// Curreng working directory
CurrentWorkingDirectory,
/// Nearest ancestor directory of the current working directory that contains `.git`, `.svn`, `jj` or `.helix`
WorkspaceDirectory,
}

impl Variable {
Expand All @@ -41,6 +45,8 @@ impl Variable {
Self::CursorColumn,
Self::BufferName,
Self::LineEnding,
Self::CurrentWorkingDirectory,
Self::WorkspaceDirectory,
];

pub const fn as_str(&self) -> &'static str {
Expand All @@ -49,6 +55,8 @@ impl Variable {
Self::CursorColumn => "cursor_column",
Self::BufferName => "buffer_name",
Self::LineEnding => "line_ending",
Self::CurrentWorkingDirectory => "current_working_directory",
Self::WorkspaceDirectory => "workspace_directory",
}
}

Expand All @@ -58,6 +66,8 @@ impl Variable {
"cursor_column" => Some(Self::CursorColumn),
"buffer_name" => Some(Self::BufferName),
"line_ending" => Some(Self::LineEnding),
"workspace_directory" => Some(Self::WorkspaceDirectory),
"current_working_directory" => Some(Self::CurrentWorkingDirectory),
_ => None,
}
}
Expand Down Expand Up @@ -215,5 +225,16 @@ fn expand_variable(editor: &Editor, variable: Variable) -> Result<Cow<'static, s
}
}
Variable::LineEnding => Ok(Cow::Borrowed(doc.line_ending.as_str())),
Variable::CurrentWorkingDirectory => Ok(std::borrow::Cow::Owned(
helix_stdx::env::current_working_dir()
.to_string_lossy()
.to_string(),
)),
Variable::WorkspaceDirectory => Ok(std::borrow::Cow::Owned(
helix_loader::find_workspace()
.0
.to_string_lossy()
.to_string(),
)),
}
}