Skip to content

Commit

Permalink
git commit support
Browse files Browse the repository at this point in the history
  • Loading branch information
dzhou121 committed Jan 27, 2022
1 parent ff3d14a commit 82ad6b3
Show file tree
Hide file tree
Showing 12 changed files with 362 additions and 56 deletions.
2 changes: 1 addition & 1 deletion core/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ impl BufferNew {
let event_sink = self.event_sink.clone();
let tab_id = self.tab_id;
let version = version.to_string();
rayon::spawn_fifo(move || {
rayon::spawn(move || {
let mut highlight_config = highlight_config.lock();
let mut highlighter = highlighter.lock();
let highlights = rope_styles(
Expand Down
9 changes: 7 additions & 2 deletions core/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use anyhow::Result;
use druid::{Point, Rect, Selector, Size, WidgetId};
use indexmap::IndexMap;
use lapce_proxy::{
dispatch::FileNodeItem, plugin::PluginDescription, terminal::TermId,
dispatch::{FileDiff, FileNodeItem},
plugin::PluginDescription,
terminal::TermId,
};
use lsp_types::{
CodeActionResponse, CompletionItem, CompletionResponse, Location, Position,
Expand Down Expand Up @@ -181,6 +183,9 @@ pub enum LapceWorkbenchCommand {

#[strum(serialize = "focus_terminal")]
FocusTerminal,

#[strum(serialize = "source_control_commit")]
SourceControlCommit,
}

#[derive(Display, EnumString, EnumIter, Clone, PartialEq, Debug, EnumMessage)]
Expand Down Expand Up @@ -501,7 +506,7 @@ pub enum LapceUICommand {
UpdateLineChanges(BufferId),
PublishDiagnostics(PublishDiagnosticsParams),
WorkDoneProgress(ProgressParams),
UpdateDiffFiles(Vec<PathBuf>),
UpdateFileDiffs(Vec<FileDiff>),
ReloadBuffer(BufferId, u64, String),
EnsureVisible((Rect, (f64, f64), Option<EnsureVisiblePosition>)),
EnsureRectVisible(Rect),
Expand Down
43 changes: 42 additions & 1 deletion core/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use druid::{
};
use im::{self, hashmap};
use itertools::Itertools;
use lapce_proxy::{plugin::PluginDescription, terminal::TermId};
use lapce_proxy::{dispatch::FileDiff, plugin::PluginDescription, terminal::TermId};
use lsp_types::{
CodeActionOrCommand, CodeActionResponse, CompletionItem, CompletionResponse,
CompletionTextEdit, Diagnostic, DiagnosticSeverity, GotoDefinitionResponse,
Expand Down Expand Up @@ -1007,6 +1007,47 @@ impl LapceTabData {
}
}
}
LapceWorkbenchCommand::SourceControlCommit => {
let diffs: Vec<FileDiff> = self
.source_control
.file_diffs
.iter()
.filter_map(
|(diff, checked)| {
if *checked {
Some(diff.clone())
} else {
None
}
},
)
.collect();
if diffs.len() == 0 {
return;
}
let buffer = self
.main_split
.local_buffers
.get_mut(&LocalBufferKind::SourceControl)
.unwrap();
let message = buffer.rope.to_string();
let message = message.trim();
if message == "" {
return;
}
self.proxy.git_commit(message, diffs);
Arc::make_mut(buffer).load_content("");
let editor = self
.main_split
.editors
.get_mut(&self.source_control.editor_view_id)
.unwrap();
Arc::make_mut(editor).cursor = if self.config.lapce.modal {
Cursor::new(CursorMode::Normal(0), None)
} else {
Cursor::new(CursorMode::Insert(Selection::caret(0)), None)
};
}
}
}

Expand Down
7 changes: 4 additions & 3 deletions core/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -984,14 +984,15 @@ impl LapceEditorBufferData {

fn next_diff(&mut self, ctx: &mut EventCtx, env: &Env) {
if let BufferContent::File(buffer_path) = &self.buffer.content {
if self.source_control.diff_files.len() == 0 {
if self.source_control.file_diffs.len() == 0 {
return;
}
let mut diff_files: Vec<(PathBuf, Vec<Position>)> = self
.source_control
.diff_files
.file_diffs
.iter()
.map(|(path, _)| {
.map(|(diff, _)| {
let path = diff.path();
let mut positions = Vec::new();
if let Some(buffer) = self.main_split.open_files.get(path) {
if let Some(changes) = buffer.history_changes.get("head") {
Expand Down
19 changes: 17 additions & 2 deletions core/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crossbeam_utils::sync::WaitGroup;
use druid::{ExtEventSink, WidgetId};
use druid::{Target, WindowId};
use flate2::read::GzDecoder;
use lapce_proxy::dispatch::FileDiff;
use lapce_proxy::dispatch::{FileNodeItem, NewBufferResponse};
use lapce_proxy::plugin::PluginDescription;
use lapce_proxy::terminal::TermId;
Expand Down Expand Up @@ -269,6 +270,16 @@ impl LapceProxy {
)
}

pub fn git_commit(&self, message: &str, diffs: Vec<FileDiff>) {
self.peer.lock().as_ref().unwrap().send_rpc_notification(
"git_commit",
&json!({
"message": message,
"diffs": diffs,
}),
)
}

pub fn install_plugin(&self, plugin: &PluginDescription) {
self.peer
.lock()
Expand Down Expand Up @@ -532,6 +543,9 @@ pub enum Notification {
DiffFiles {
files: Vec<PathBuf>,
},
FileDiffs {
diffs: Vec<FileDiff>,
},
UpdateTerminal {
term_id: TermId,
content: String,
Expand Down Expand Up @@ -607,10 +621,11 @@ impl Handler for ProxyHandlerNew {
);
}
Notification::ListDir { items } => {}
Notification::DiffFiles { files } => {
Notification::DiffFiles { files } => {}
Notification::FileDiffs { diffs } => {
self.event_sink.submit_command(
LAPCE_UI_COMMAND,
LapceUICommand::UpdateDiffFiles(files),
LapceUICommand::UpdateFileDiffs(diffs),
Target::Widget(self.tab_id),
);
}
Expand Down
96 changes: 74 additions & 22 deletions core/src/source_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use druid::{
Rect, RenderContext, Size, Target, TextLayout, UpdateCtx, Widget, WidgetExt,
WidgetId, WidgetPod, WindowId,
};
use lapce_proxy::dispatch::FileDiff;

use crate::{
command::{
Expand All @@ -29,7 +30,7 @@ use crate::{
scroll::LapceScrollNew,
split::{LapceSplitNew, SplitDirection, SplitMoveDirection},
state::Mode,
svg::file_svg_new,
svg::{file_svg_new, get_svg},
theme::OldLapceTheme,
};

Expand All @@ -45,7 +46,7 @@ pub struct SourceControlData {
pub file_list_id: WidgetId,
pub file_list_index: usize,
pub editor_view_id: WidgetId,
pub diff_files: Vec<(PathBuf, bool)>,
pub file_diffs: Vec<(FileDiff, bool)>,
}

impl SourceControlData {
Expand All @@ -60,7 +61,7 @@ impl SourceControlData {
file_list_index: 0,
split_id: WidgetId::next(),
split_direction: SplitDirection::Horizontal,
diff_files: Vec::new(),
file_diffs: Vec::new(),
}
}

Expand Down Expand Up @@ -141,31 +142,31 @@ impl KeyPressFocus for SourceControlData {
LapceCommand::Up | LapceCommand::ListPrevious => {
self.file_list_index = Movement::Up.update_index(
self.file_list_index,
self.diff_files.len(),
self.file_diffs.len(),
1,
true,
);
}
LapceCommand::Down | LapceCommand::ListNext => {
self.file_list_index = Movement::Down.update_index(
self.file_list_index,
self.diff_files.len(),
self.file_diffs.len(),
1,
true,
);
}
LapceCommand::ListExpand => {
if self.diff_files.len() > 0 {
self.diff_files[self.file_list_index].1 =
!self.diff_files[self.file_list_index].1;
if self.file_diffs.len() > 0 {
self.file_diffs[self.file_list_index].1 =
!self.file_diffs[self.file_list_index].1;
}
}
LapceCommand::ListSelect => {
if self.diff_files.len() > 0 {
if self.file_diffs.len() > 0 {
ctx.submit_command(Command::new(
LAPCE_UI_COMMAND,
LapceUICommand::OpenFileDiff(
self.diff_files[self.file_list_index].0.clone(),
self.file_diffs[self.file_list_index].0.path().clone(),
"head".to_string(),
),
Target::Auto,
Expand Down Expand Up @@ -224,15 +225,15 @@ impl Widget<LapceTabData> for SourceControlFileList {
let y = mouse_event.pos.y;
if y > 0.0 {
let line = (y / line_height).floor() as usize;
if line < data.source_control.diff_files.len()
if line < data.source_control.file_diffs.len()
&& mouse_event.pos.x < line_height
{
if let Some(mouse_down) = self.mouse_down {
if mouse_down == line {
let source_control =
Arc::make_mut(&mut data.source_control);
source_control.diff_files[line].1 =
!source_control.diff_files[line].1;
source_control.file_diffs[line].1 =
!source_control.file_diffs[line].1;
}
}
}
Expand All @@ -247,15 +248,15 @@ impl Widget<LapceTabData> for SourceControlFileList {
let y = mouse_event.pos.y;
if y > 0.0 {
let line = (y / line_height).floor() as usize;
if line < source_control.diff_files.len() {
if line < source_control.file_diffs.len() {
source_control.file_list_index = line;
if mouse_event.pos.x < line_height {
self.mouse_down = Some(line);
} else {
ctx.submit_command(Command::new(
LAPCE_UI_COMMAND,
LapceUICommand::OpenFileDiff(
source_control.diff_files[line].0.clone(),
source_control.file_diffs[line].0.path().clone(),
"head".to_string(),
),
Target::Widget(data.id),
Expand Down Expand Up @@ -316,6 +317,11 @@ impl Widget<LapceTabData> for SourceControlFileList {
data: &LapceTabData,
env: &Env,
) {
if data.source_control.file_diffs.len()
!= old_data.source_control.file_diffs.len()
{
ctx.request_layout();
}
}

fn layout(
Expand All @@ -326,16 +332,18 @@ impl Widget<LapceTabData> for SourceControlFileList {
env: &Env,
) -> Size {
let line_height = data.config.editor.line_height as f64;
let height = line_height * data.source_control.diff_files.len() as f64;
let height = line_height * data.source_control.file_diffs.len() as f64;
Size::new(bc.max().width, height)
}

fn paint(&mut self, ctx: &mut PaintCtx, data: &LapceTabData, env: &Env) {
let self_size = ctx.size();

let line_height = data.config.editor.line_height as f64;

let files = &data.source_control.diff_files;
let diffs = &data.source_control.file_diffs;

if ctx.is_focused() && files.len() > 0 {
if ctx.is_focused() && diffs.len() > 0 {
let rect = Size::new(ctx.size().width, line_height)
.to_rect()
.with_origin(Point::new(
Expand All @@ -352,11 +360,12 @@ impl Widget<LapceTabData> for SourceControlFileList {
let start_line = (rect.y0 / line_height).floor() as usize;
let end_line = (rect.y1 / line_height).ceil() as usize;
for line in start_line..end_line {
if line >= files.len() {
if line >= diffs.len() {
break;
}
let y = line_height * line as f64;
let (mut path, checked) = files[line].clone();
let (diff, checked) = diffs[line].clone();
let mut path = diff.path().clone();
if let Some(workspace) = data.workspace.as_ref() {
path = path
.strip_prefix(&workspace.path)
Expand Down Expand Up @@ -407,7 +416,13 @@ impl Widget<LapceTabData> for SourceControlFileList {
)
.build()
.unwrap();
ctx.draw_text(&text_layout, Point::new(line_height * 2.0, y + 4.0));
ctx.draw_text(
&text_layout,
Point::new(
line_height * 2.0,
y + (line_height - text_layout.size().height) / 2.0,
),
);
let folder = path
.parent()
.and_then(|s| s.to_str())
Expand All @@ -429,9 +444,46 @@ impl Widget<LapceTabData> for SourceControlFileList {
.unwrap();
ctx.draw_text(
&text_layout,
Point::new(line_height * 2.0 + x + 5.0, y + 4.0),
Point::new(
line_height * 2.0 + x + 5.0,
y + (line_height - text_layout.size().height) / 2.0,
),
);
}

let (svg, color) = match diff {
FileDiff::Modified(_) => (
"diff-modified.svg",
data.config
.get_color_unchecked(LapceTheme::SOURCE_CONTROL_MODIFIED),
),
FileDiff::Added(_) => (
"diff-added.svg",
data.config
.get_color_unchecked(LapceTheme::SOURCE_CONTROL_ADDED),
),
FileDiff::Deleted(_) => (
"diff-removed.svg",
data.config
.get_color_unchecked(LapceTheme::SOURCE_CONTROL_REMOVED),
),
FileDiff::Renamed(_, _) => (
"diff-renamed.svg",
data.config
.get_color_unchecked(LapceTheme::SOURCE_CONTROL_MODIFIED),
),
};
let svg = get_svg(svg).unwrap();

let svg_size = 15.0;
let rect =
Size::new(svg_size, svg_size)
.to_rect()
.with_origin(Point::new(
self_size.width - svg_size - 10.0,
line as f64 * line_height + (line_height - svg_size) / 2.0,
));
ctx.draw_svg(&svg, rect, Some(&color.clone().with_alpha(0.9)));
}
}
}
Loading

0 comments on commit 82ad6b3

Please sign in to comment.