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

Add a version-control statusline element #5682

Merged
merged 10 commits into from
Mar 10, 2023
Next Next commit
Implement a version control component for the statusline
  • Loading branch information
dgyurov committed Jan 25, 2023
commit 4a15ff466e7e10595797c9833a6975a0a27c237b
1 change: 1 addition & 0 deletions book/src/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ The following statusline elements can be configured:
| `position-percentage` | The cursor position as a percentage of the total number of lines |
| `separator` | The string defined in `editor.statusline.separator` (defaults to `"│"`) |
| `spacer` | Inserts a space between elements (multiple/contiguous spacers may be specified) |
| `version-control` | The current branch name or detached commit hash of the opened workspace |

### `[editor.lsp]` Section

Expand Down
18 changes: 17 additions & 1 deletion helix-term/src/ui/statusline.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use helix_core::{coords_at_pos, encoding, Position};
use helix_core::{coords_at_pos, encoding, find_root, Position};
use helix_lsp::lsp::DiagnosticSeverity;
use helix_view::{
document::{Mode, SCRATCH_BUFFER_NAME},
Expand Down Expand Up @@ -155,6 +155,7 @@ where
helix_view::editor::StatusLineElement::TotalLineNumbers => render_total_line_numbers,
helix_view::editor::StatusLineElement::Separator => render_separator,
helix_view::editor::StatusLineElement::Spacer => render_spacer,
helix_view::editor::StatusLineElement::VersionControl => render_version_control,
}
}

Expand Down Expand Up @@ -466,3 +467,18 @@ where
{
write(context, String::from(" "), None);
}

fn render_version_control<F>(context: &mut RenderContext, write: F)
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
let path = find_root(None, &[]);

let current_branch_name = context
.editor
.diff_providers
.get_current_head_name(&path)
.unwrap_or_default();

write(context, current_branch_name, None);
}
12 changes: 12 additions & 0 deletions helix-vcs/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@ impl DiffProvider for Git {
}
Some(data)
}

fn get_current_head_name(&self, file: &Path) -> Option<String> {
// TODO cache repository lookup
let repo = Git::open_repo(file, None)?.to_thread_local();
let head_ref = repo.head_ref().ok()?;
let head_commit = repo.head_commit().ok()?;

match head_ref {
Some(reference) => Some(reference.name().shorten().to_string()),
None => Some(head_commit.id.to_hex_with_len(8).to_string()),
}
}
}

/// Finds the object that contains the contents of a file at a specific commit.
Expand Down
11 changes: 11 additions & 0 deletions helix-vcs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub trait DiffProvider {
/// The data is returned as raw byte without any decoding or encoding performed
/// to ensure all file encodings are handled correctly.
fn get_diff_base(&self, file: &Path) -> Option<Vec<u8>>;
fn get_current_head_name(&self, file: &Path) -> Option<String>;
}

#[doc(hidden)]
Expand All @@ -26,6 +27,10 @@ impl DiffProvider for Dummy {
fn get_diff_base(&self, _file: &Path) -> Option<Vec<u8>> {
None
}

fn get_current_head_name(&self, _file: &Path) -> Option<String> {
None
}
}

pub struct DiffProviderRegistry {
Expand All @@ -38,6 +43,12 @@ impl DiffProviderRegistry {
.iter()
.find_map(|provider| provider.get_diff_base(file))
}

pub fn get_current_head_name(&self, file: &Path) -> Option<String> {
self.providers
.iter()
.find_map(|provider| provider.get_current_head_name(file))
}
}

impl Default for DiffProviderRegistry {
Expand Down
3 changes: 3 additions & 0 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,9 @@ pub enum StatusLineElement {

/// A single space
Spacer,

/// Current version control information
VersionControl,
}

// Cursor shape is read and used on every rendered frame and so needs
Expand Down