Skip to content

Show changes relative to VCS in diagnostics #467

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

Closed
wants to merge 3 commits into from
Closed
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
71 changes: 71 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ members = [
"helix-tui",
"helix-syntax",
"helix-lsp",
"helix-vcs",
]

# Build helix-syntax in release mode to make the code path faster in development.
Expand Down
1 change: 1 addition & 0 deletions helix-term/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ path = "src/main.rs"
helix-core = { version = "0.3", path = "../helix-core" }
helix-view = { version = "0.3", path = "../helix-view" }
helix-lsp = { version = "0.3", path = "../helix-lsp" }
helix-vcs = { version = "0.3", path = "../helix-vcs"}

anyhow = "1"
once_cell = "1.8"
Expand Down
23 changes: 23 additions & 0 deletions helix-term/src/ui/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use helix_core::{
syntax::{self, HighlightEvent},
LineEnding, Position, Range,
};
use helix_vcs::LineChange;
use helix_view::{
document::Mode,
graphics::{CursorKind, Modifier, Rect, Style},
Expand Down Expand Up @@ -327,8 +328,30 @@ impl EditorView {
let info: Style = theme.get("info");
let hint: Style = theme.get("hint");

let line_added: Style = theme.get("diff.added");
let line_removed_above: Style = theme.get("diff.removed.above");
let line_removed_below: Style = theme.get("diff.removed.below");
let line_modified: Style = theme.get("diff.modified");

for (i, line) in (view.first_line..last_line).enumerate() {
use helix_core::diagnostic::Severity;
if let Some(line_changes) = doc.vcs_line_changes() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To address #405, maybe here we could abstract it to simply be doc.line_changes() . It doesn't need to be done in this PR however.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, though I agree that it is probably outside of the scope of this PR.

if let Some(line_change) = line_changes.get(&(line + 1)) {
surface.set_stringn(
viewport.x - OFFSET,
viewport.y + i as u16,
// TODO: set this in a theme
line_change.as_str(),
1,
match line_change {
LineChange::Added => line_added,
LineChange::RemovedAbove => line_removed_above,
LineChange::RemovedBelow => line_removed_below,
LineChange::Modified => line_modified,
},
);
}
}
if let Some(diagnostic) = doc.diagnostics().iter().find(|d| d.line == line) {
surface.set_stringn(
viewport.x - OFFSET,
Expand Down
10 changes: 10 additions & 0 deletions helix-vcs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "helix-vcs"
version = "0.3.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
git2 = { version = "0.13", default-features = false }
helix-core = { version = "0.3", path = "../helix-core" }
102 changes: 102 additions & 0 deletions helix-vcs/src/git.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};

use git2::{DiffOptions, IntoCString, Repository};

use crate::{LineChange, LineChanges};

pub struct Git {
relative_path: PathBuf,
repo: Repository,
pub line_changes: Option<LineChanges>,
}
impl Git {
pub fn from_path(filename: &Path) -> Option<Self> {
if let Ok(repo) = Repository::discover(&filename) {
let repo_path_absolute = fs::canonicalize(repo.workdir()?).ok()?;

let relative_path = fs::canonicalize(&filename)
.ok()?
.strip_prefix(&repo_path_absolute)
.ok()?
.to_path_buf();
Some(Git {
repo,
relative_path,
line_changes: None,
})
} else {
None
}
}

/// Taken from https://github.com/sharkdp/bat/blob/master/src/diff.rs
pub fn diff(&mut self) {
let mut diff_options = DiffOptions::new();
let pathspec = if let Ok(p) = self.relative_path.clone().into_c_string() {
p
} else {
return;
};
diff_options.pathspec(pathspec);
diff_options.context_lines(0);

let diff = if let Ok(d) = self
.repo
.diff_index_to_workdir(None, Some(&mut diff_options))
{
d
} else {
return;
};

let mut line_changes: LineChanges = HashMap::new();

let mark_section =
|line_changes: &mut LineChanges, start: u32, end: i32, change: LineChange| {
for line in start..=end as u32 {
line_changes.insert(line as usize, change);
}
};

let _ = diff.foreach(
&mut |_, _| true,
None,
Some(&mut |delta, hunk| {
let path = delta.new_file().path().unwrap_or_else(|| Path::new(""));

if self.relative_path != path {
return false;
}

let old_lines = hunk.old_lines();
let new_start = hunk.new_start();
let new_lines = hunk.new_lines();
let new_end = (new_start + new_lines) as i32 - 1;

if old_lines == 0 && new_lines > 0 {
mark_section(&mut line_changes, new_start, new_end, LineChange::Added);
} else if new_lines == 0 && old_lines > 0 {
if new_start == 0 {
mark_section(&mut line_changes, 1, 1, LineChange::RemovedAbove);
} else {
mark_section(
&mut line_changes,
new_start,
new_start as i32,
LineChange::RemovedBelow,
);
}
} else {
mark_section(&mut line_changes, new_start, new_end, LineChange::Modified);
}

true
}),
None,
);

self.line_changes = Some(line_changes);
}
}
45 changes: 45 additions & 0 deletions helix-vcs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use std::collections::HashMap;
use std::path::Path;

mod git;
use git::Git;

#[derive(Copy, Clone, Debug)]
pub enum LineChange {
Added,
RemovedAbove,
RemovedBelow,
Modified,
}
impl LineChange {
pub fn as_str(&self) -> &'static str {
match self {
LineChange::Added => &"▌",
LineChange::RemovedAbove => &"▘",
LineChange::RemovedBelow => &"▖",
LineChange::Modified => &"▐",
}
}
}

pub type LineChanges = HashMap<usize, LineChange>;

//#[derive(Clone)]
pub enum VCS {
Git(Git),
}
impl VCS {
pub fn from_path(filename: &Path) -> Option<Self> {
Some(VCS::Git(Git::from_path(filename)?))
}
pub fn get_line_changes(&self) -> Option<&LineChanges> {
match self {
VCS::Git(git) => git.line_changes.as_ref(),
}
}
pub fn diff(&mut self) {
match self {
VCS::Git(git) => git.diff(),
}
}
}
1 change: 1 addition & 0 deletions helix-view/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ bitflags = "1.0"
anyhow = "1"
helix-core = { version = "0.3", path = "../helix-core" }
helix-lsp = { version = "0.3", path = "../helix-lsp"}
helix-vcs = { version = "0.3", path = "../helix-vcs"}
crossterm = { version = "0.20", optional = true }

# Conversion traits
Expand Down
Loading