Skip to content

Commit

Permalink
Changed file picker
Browse files Browse the repository at this point in the history
  • Loading branch information
xJonathanLEI committed Feb 12, 2023
1 parent a4751db commit 10122c3
Show file tree
Hide file tree
Showing 8 changed files with 558 additions and 6 deletions.
69 changes: 69 additions & 0 deletions Cargo.lock

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

41 changes: 41 additions & 0 deletions helix-term/src/commands.rs
Expand Up @@ -284,6 +284,7 @@ impl MappableCommand {
buffer_picker, "Open buffer picker",
jumplist_picker, "Open jumplist picker",
symbol_picker, "Open symbol picker",
changed_file_picker, "Open changed file picker",
select_references_to_symbol_under_cursor, "Select symbol references",
workspace_symbol_picker, "Open workspace symbol picker",
diagnostics_picker, "Open diagnostic picker",
Expand Down Expand Up @@ -2579,6 +2580,46 @@ fn jumplist_picker(cx: &mut Context) {
cx.push_layer(Box::new(overlayed(picker)));
}

fn changed_file_picker(cx: &mut Context) {
let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("./"));

let entries = match cx.editor.diff_providers.get_changed_files(&cwd) {
Ok(entries) => entries,
Err(err) => {
cx.editor.set_error(format!("{err}"));
return;
}
};

let added = cx.editor.theme.get("diff.plus");
let deleted = cx.editor.theme.get("diff.minus");
let modified = cx.editor.theme.get("diff.delta");

let picker = FilePicker::new(
entries,
ui::menu::FileChangeData {
cwd,
style_untracked: added,
style_modified: modified,
style_deleted: deleted,
style_renamed: modified,
},
|cx, meta, action| {
let path_to_open = meta.path();
if let Err(e) = cx.editor.open(path_to_open, action) {
let err = if let Some(err) = e.source() {
format!("{}", err)
} else {
format!("unable to open \"{}\"", path_to_open.display())
};
cx.editor.set_error(err);
}
},
|_editor, meta| Some((meta.path().to_path_buf().into(), None)),
);
cx.push_layer(Box::new(overlayed(picker)));
}

impl ui::menu::Item for MappableCommand {
type Data = ReverseKeymap;

Expand Down
3 changes: 2 additions & 1 deletion helix-term/src/keymap/default.rs
Expand Up @@ -219,9 +219,10 @@ pub fn default() -> HashMap<Mode, Keymap> {
"S" => workspace_symbol_picker,
"d" => diagnostics_picker,
"D" => workspace_diagnostics_picker,
"g" => changed_file_picker,
"a" => code_action,
"'" => last_picker,
"g" => { "Debug (experimental)" sticky=true
"G" => { "Debug (experimental)" sticky=true
"l" => dap_launch,
"b" => dap_toggle_breakpoint,
"c" => dap_continue,
Expand Down
34 changes: 33 additions & 1 deletion helix-term/src/ui/menu.rs
Expand Up @@ -11,7 +11,8 @@ pub use tui::widgets::{Cell, Row};
use fuzzy_matcher::skim::SkimMatcherV2 as Matcher;
use fuzzy_matcher::FuzzyMatcher;

use helix_view::{graphics::Rect, Editor};
use helix_vcs::FileChange;
use helix_view::{graphics::Rect, theme::Style, Editor};
use tui::layout::Constraint;

pub trait Item {
Expand Down Expand Up @@ -45,6 +46,37 @@ impl Item for PathBuf {

pub type MenuCallback<T> = Box<dyn Fn(&mut Editor, Option<&T>, MenuEvent)>;

pub struct FileChangeData {
pub cwd: PathBuf,
pub style_untracked: Style,
pub style_modified: Style,
pub style_deleted: Style,
pub style_renamed: Style,
}

impl Item for FileChange {
type Data = FileChangeData;

fn format(&self, data: &Self::Data) -> Row {
let (sign, style) = match self {
Self::Untracked { .. } => ("[+]", data.style_untracked),
Self::Modified { .. } => ("[~]", data.style_modified),
Self::Deleted { .. } => ("[-]", data.style_deleted),
Self::Renamed { .. } => ("[>]", data.style_modified),
};
let path = self.path();

Row::new(vec![
sign.to_owned(),
path.strip_prefix(&data.cwd)
.unwrap_or(path)
.to_string_lossy()
.to_string(),
])
.style(style)
}
}

pub struct Menu<T: Item> {
options: Vec<T>,
editor_data: T::Data,
Expand Down
4 changes: 4 additions & 0 deletions helix-vcs/Cargo.toml
Expand Up @@ -13,11 +13,15 @@ homepage = "https://helix-editor.com"
[dependencies]
helix-core = { version = "0.6", path = "../helix-core" }

anyhow = "1.0"
tokio = { version = "1", features = ["rt", "rt-multi-thread", "time", "sync", "parking_lot", "macros"] }
parking_lot = "0.12"

content_inspector = "0.2.4"
git-repository = { version = "0.32", default-features = false , optional = true }
ignore = "0.4"
imara-diff = "0.1.5"
sha1 = "0.10.0"

log = "0.4"

Expand Down
28 changes: 28 additions & 0 deletions helix-vcs/src/diff.rs
@@ -1,4 +1,5 @@
use std::ops::Range;
use std::path::{Path, PathBuf};
use std::sync::Arc;

use helix_core::Rope;
Expand Down Expand Up @@ -277,3 +278,30 @@ impl FileHunks<'_> {
}
}
}

pub enum FileChange {
Untracked {
path: PathBuf,
},
Modified {
path: PathBuf,
},
Deleted {
path: PathBuf,
},
Renamed {
from_path: PathBuf,
to_path: PathBuf,
},
}

impl FileChange {
pub fn path(&self) -> &Path {
match self {
Self::Untracked { path } => path,
Self::Modified { path } => path,
Self::Deleted { path } => path,
Self::Renamed { to_path, .. } => to_path,
}
}
}

0 comments on commit 10122c3

Please sign in to comment.