-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.