refactor open file

This commit is contained in:
Dongdong Zhou 2021-09-30 10:32:23 +01:00
parent 65ea177e8e
commit e3ff355c1e
5 changed files with 136 additions and 78 deletions

View File

@ -297,15 +297,20 @@ pub fn reset_revs(&mut self) {
pub fn load_content(&mut self, content: &str) {
self.reset_revs();
let delta = Delta::simple_edit(Interval::new(0, 0), Rope::from(content), 0);
let (new_rev, new_text, new_tombstones, new_deletes_from_union) =
self.mk_new_rev(0, delta.clone());
self.revs.push(new_rev);
self.rope = new_text.clone();
self.tombstones = new_tombstones;
self.deletes_from_union = new_deletes_from_union;
self.code_actions.clear();
if content != "" {
let delta =
Delta::simple_edit(Interval::new(0, 0), Rope::from(content), 0);
println!("{:?}", delta);
let (new_rev, new_text, new_tombstones, new_deletes_from_union) =
self.mk_new_rev(0, delta.clone());
self.revs.push(new_rev);
self.rope = new_text.clone();
self.tombstones = new_tombstones;
self.deletes_from_union = new_deletes_from_union;
}
self.code_actions.clear();
let (max_len, max_len_line) = self.get_max_line_len();
self.max_len = max_len;
self.max_len_line = max_len_line;
@ -1144,6 +1149,7 @@ fn apply_edit(
new_deletes_from_union: Subset,
) {
if !self.loaded {
println!("not loaded");
return;
}
self.rev += 1;
@ -1210,9 +1216,14 @@ pub fn edit_multiple(
self.this_edit_type = edit_type;
let undo_group = self.calculate_undo_group();
self.last_edit_type = self.this_edit_type;
println!("{} {:?} {:?}", undo_group, delta, self.rope);
let (new_rev, new_text, new_tombstones, new_deletes_from_union) =
self.mk_new_rev(undo_group, delta.clone());
println!("{} {:?} {:?}", undo_group, delta, new_text);
self.apply_edit(
proxy,
&delta,

View File

@ -313,8 +313,8 @@ pub enum LapceUICommand {
JumpToLine(EditorKind, usize),
JumpToLocation(EditorKind, EditorLocationNew),
GoToLocationNew(WidgetId, EditorLocationNew),
GotoReference(usize, EditorLocationNew),
GotoDefinition(usize, EditorLocationNew),
GotoReference(WidgetId, usize, EditorLocationNew),
GotoDefinition(WidgetId, usize, EditorLocationNew),
PaletteReferences(usize, Vec<Location>),
GotoLocation(Location),
}

View File

@ -811,31 +811,29 @@ pub fn edit(
pub fn jump_to_position(
&mut self,
ctx: &mut EventCtx,
kind: &EditorKind,
editor_view_id: WidgetId,
position: Position,
config: &Config,
) {
let editor = self.editor_kind_mut(kind);
let editor = self.editors.get(&editor_view_id).unwrap();
let location = EditorLocationNew {
path: editor.buffer.clone(),
position,
position: Some(position),
scroll_offset: None,
};
self.jump_to_location(ctx, kind, location, config);
self.jump_to_location(ctx, editor_view_id, location, config);
}
pub fn jump_to_location(
&mut self,
ctx: &mut EventCtx,
kind: &EditorKind,
editor_view_id: WidgetId,
location: EditorLocationNew,
config: &Config,
) {
let path = self.editor_kind(kind).buffer.clone();
let buffer = self.open_files.get(&path).unwrap().clone();
let editor = self.editor_kind_mut(kind);
let editor = Arc::make_mut(self.editors.get_mut(&editor_view_id).unwrap());
let buffer = self.open_files.get(&editor.buffer).unwrap().clone();
editor.save_jump_location(&buffer);
let editor_view_id = editor.view_id;
self.go_to_location(ctx, editor_view_id, location, config);
}
@ -862,7 +860,15 @@ pub fn go_to_location(
);
} else {
let buffer = self.open_files.get(&path).unwrap();
let offset = buffer.offset_of_position(&location.position);
let (offset, scroll_offset) = match &location.position {
Some(position) => (
buffer.offset_of_position(position),
location.scroll_offset.as_ref(),
),
None => (buffer.cursor_offset, Some(&buffer.scroll_offset)),
};
editor.buffer = path.clone();
editor.cursor = if config.lapce.modal {
Cursor::new(CursorMode::Normal(offset), None)
@ -870,7 +876,7 @@ pub fn go_to_location(
Cursor::new(CursorMode::Insert(Selection::caret(offset)), None)
};
if let Some(scroll_offset) = location.scroll_offset {
if let Some(scroll_offset) = scroll_offset {
ctx.submit_command(Command::new(
LAPCE_UI_COMMAND,
LapceUICommand::ForceScrollTo(scroll_offset.x, scroll_offset.y),
@ -899,51 +905,19 @@ pub fn go_to_location(
pub fn jump_to_line(
&mut self,
ctx: &mut EventCtx,
kind: &EditorKind,
editor_view_id: WidgetId,
line: usize,
config: &Config,
) {
let buffer = self.open_files.get(&self.editor_kind(kind).buffer).unwrap();
let editor = self.editors.get(&editor_view_id).unwrap();
let buffer = self.open_files.get(&editor.buffer).unwrap();
let offset = buffer.first_non_blank_character_on_line(if line > 0 {
line - 1
} else {
0
});
let position = buffer.offset_to_position(offset);
self.jump_to_position(ctx, kind, position, config);
}
pub fn open_file(
&mut self,
ctx: &mut EventCtx,
path: &PathBuf,
config: &Config,
) {
let (cursor_offset, scroll_offset) = if let Some(buffer) =
self.open_files.get(path)
{
(buffer.cursor_offset, buffer.scroll_offset)
} else {
let buffer =
Arc::new(BufferNew::new(path.clone(), self.update_sender.clone()));
self.open_files.insert(path.clone(), buffer.clone());
buffer.retrieve_file(self.proxy.clone(), ctx.get_external_handle());
(0, Vec2::new(0.0, 0.0))
};
let editor = self.active_editor_mut();
editor.buffer = path.clone();
editor.cursor = if config.lapce.modal {
Cursor::new(CursorMode::Normal(cursor_offset), None)
} else {
Cursor::new(CursorMode::Insert(Selection::caret(cursor_offset)), None)
};
ctx.submit_command(Command::new(
LAPCE_UI_COMMAND,
LapceUICommand::ForceScrollTo(scroll_offset.x, scroll_offset.y),
Target::Widget(editor.container_id),
));
self.jump_to_position(ctx, editor_view_id, position, config);
}
}
@ -1114,7 +1088,7 @@ pub fn add_snippet_placeholders(
pub fn save_jump_location(&mut self, buffer: &BufferNew) {
let location = EditorLocationNew {
path: buffer.path.clone(),
position: buffer.offset_to_position(self.cursor.offset()),
position: Some(buffer.offset_to_position(self.cursor.offset())),
scroll_offset: Some(self.scroll_offset.clone()),
};
self.locations.push(location);
@ -1521,7 +1495,7 @@ pub fn next_error(&mut self, ctx: &mut EventCtx, env: &Env) {
);
let location = EditorLocationNew {
path,
position,
position: Some(position),
scroll_offset: None,
};
ctx.submit_command(Command::new(
@ -1970,10 +1944,13 @@ fn update_completion(&mut self, ctx: &mut EventCtx) {
.buffer
.slice_to_cow(start_offset..end_offset)
.to_string();
let char = self
.buffer
.slice_to_cow(start_offset - 1..start_offset)
.to_string();
let char = if start_offset == 0 {
"".to_string()
} else {
self.buffer
.slice_to_cow(start_offset - 1..start_offset)
.to_string()
};
let completion = Arc::make_mut(&mut self.completion);
if input == "" && char != "." && char != ":" {
completion.cancel();
@ -2760,6 +2737,7 @@ fn run_command(
let buffer_id = self.buffer.id;
let position = self.buffer.offset_to_position(offset);
let proxy = self.proxy.clone();
let editor_view_id = self.editor.view_id;
self.proxy.get_definition(
offset,
buffer_id,
@ -2790,7 +2768,10 @@ fn run_command(
position,
Box::new(move |result| {
process_get_references(
offset, result, event_sink,
editor_view_id,
offset,
result,
event_sink,
);
}),
);
@ -2798,12 +2779,15 @@ fn run_command(
event_sink.submit_command(
LAPCE_UI_COMMAND,
LapceUICommand::GotoDefinition(
editor_view_id,
offset,
EditorLocationNew {
path: PathBuf::from(
location.uri.path(),
),
position: location.range.start,
position: Some(
location.range.start,
),
scroll_offset: None,
},
),
@ -3108,6 +3092,7 @@ fn next_in_file_errors_offset(
}
fn process_get_references(
editor_view_id: WidgetId,
offset: usize,
result: Result<Value, xi_rpc::Error>,
event_sink: ExtEventSink,
@ -3122,10 +3107,11 @@ fn process_get_references(
event_sink.submit_command(
LAPCE_UI_COMMAND,
LapceUICommand::GotoReference(
editor_view_id,
offset,
EditorLocationNew {
path: PathBuf::from(location.uri.path()),
position: location.range.start.clone(),
position: Some(location.range.start.clone()),
scroll_offset: None,
},
),

View File

@ -115,7 +115,7 @@ pub struct EditorState {
#[derive(Clone, Debug)]
pub struct EditorLocationNew {
pub path: PathBuf,
pub position: Position,
pub position: Option<Position>,
pub scroll_offset: Option<Vec2>,
}
@ -568,6 +568,16 @@ fn event(
if *data.main_split.active == self.view_id {
ctx.request_focus();
}
data.main_split.go_to_location(
ctx,
self.view_id,
EditorLocationNew {
path: data.editor.buffer.clone(),
position: None,
scroll_offset: None,
},
&data.config,
);
}
Event::KeyDown(key_event) => {
if data.key_down(ctx, key_event, env) {

View File

@ -172,7 +172,18 @@ fn event(
.write(true)
.open(&path);
}
data.main_split.open_file(ctx, &path, &data.config);
let editor_view_id = data.main_split.active.clone();
data.main_split.jump_to_location(
ctx,
*editor_view_id,
EditorLocationNew {
path: path.clone(),
position: None,
scroll_offset: None,
},
&data.config,
);
}
}
LapceCommand::OpenKeyboardShortcuts => {
@ -185,7 +196,18 @@ fn event(
.write(true)
.open(&path);
}
data.main_split.open_file(ctx, &path, &data.config);
let editor_view_id = data.main_split.active.clone();
data.main_split.jump_to_location(
ctx,
*editor_view_id,
EditorLocationNew {
path: path.clone(),
position: None,
scroll_offset: None,
},
&data.config,
);
}
}
_ => (),
@ -289,7 +311,17 @@ fn event(
ctx.set_handled();
}
LapceUICommand::OpenFile(path) => {
data.main_split.open_file(ctx, path, &data.config);
let editor_view_id = data.main_split.active.clone();
data.main_split.jump_to_location(
ctx,
*editor_view_id,
EditorLocationNew {
path: path.clone(),
position: None,
scroll_offset: None,
},
&data.config,
);
ctx.set_handled();
}
LapceUICommand::GoToLocationNew(editor_view_id, location) => {
@ -302,45 +334,64 @@ fn event(
ctx.set_handled();
}
LapceUICommand::JumpToPosition(kind, position) => {
let editor_view_id =
data.main_split.editor_kind(kind).view_id;
data.main_split.jump_to_position(
ctx,
kind,
editor_view_id,
*position,
&data.config,
);
ctx.set_handled();
}
LapceUICommand::JumpToLocation(kind, location) => {
let editor_view_id =
data.main_split.editor_kind(kind).view_id;
data.main_split.jump_to_location(
ctx,
kind,
editor_view_id,
location.clone(),
&data.config,
);
ctx.set_handled();
}
LapceUICommand::JumpToLine(kind, line) => {
data.main_split.jump_to_line(ctx, kind, *line, &data.config);
let editor_view_id =
data.main_split.editor_kind(kind).view_id;
data.main_split.jump_to_line(
ctx,
editor_view_id,
*line,
&data.config,
);
ctx.set_handled();
}
LapceUICommand::GotoDefinition(offset, location) => {
LapceUICommand::GotoDefinition(
editor_view_id,
offset,
location,
) => {
if *offset == data.main_split.active_editor().cursor.offset()
{
data.main_split.jump_to_location(
ctx,
&EditorKind::SplitActive,
*editor_view_id,
location.clone(),
&data.config,
);
}
ctx.set_handled();
}
LapceUICommand::GotoReference(offset, location) => {
LapceUICommand::GotoReference(
editor_view_id,
offset,
location,
) => {
if *offset == data.main_split.active_editor().cursor.offset()
{
data.main_split.jump_to_location(
ctx,
&EditorKind::SplitActive,
*editor_view_id,
location.clone(),
&data.config,
);
@ -365,7 +416,7 @@ fn event(
.iter()
.map(|l| EditorLocationNew {
path: PathBuf::from(l.uri.path()),
position: l.range.start.clone(),
position: Some(l.range.start.clone()),
scroll_offset: None,
})
.collect();