Skip to content
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

(rename) View.doc -> View.doc_id #6067

Closed
wants to merge 2 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
2 changes: 1 addition & 1 deletion helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ impl Application {
// reset view position in case softwrap was enabled/disabled
let scrolloff = self.editor.config().scrolloff;
for (view, _) in self.editor.tree.views_mut() {
let doc = &self.editor.documents[&view.doc];
let doc = &self.editor.documents[&view.doc_id];
view.ensure_cursor_in_view(doc, scrolloff)
}
}
Expand Down
16 changes: 8 additions & 8 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ fn goto_previous_buffer(cx: &mut Context) {
}

fn goto_buffer(editor: &mut Editor, direction: Direction) {
let current = view!(editor).doc;
let current = view!(editor).doc_id;

let id = match direction {
Direction::Forward => {
Expand Down Expand Up @@ -2432,7 +2432,7 @@ fn file_picker_in_current_directory(cx: &mut Context) {
}

fn buffer_picker(cx: &mut Context) {
let current = view!(cx.editor).doc;
let current = view!(cx.editor).doc_id;

struct BufferMeta {
id: DocumentId,
Expand Down Expand Up @@ -2547,7 +2547,7 @@ fn jumplist_picker(cx: &mut Context) {
path: doc.and_then(|d| d.path().cloned()),
selection,
text,
is_current: view.doc == doc_id,
is_current: view.doc_id == doc_id,
}
};

Expand Down Expand Up @@ -2640,7 +2640,7 @@ pub fn command_palette(cx: &mut Context) {
let config = ctx.editor.config();
let mode = ctx.editor.mode();
let view = view_mut!(ctx.editor, focus);
let doc = doc_mut!(ctx.editor, &view.doc);
let doc = doc_mut!(ctx.editor, &view.doc_id);

view.ensure_cursor_in_view(doc, config.scrolloff);

Expand Down Expand Up @@ -2899,7 +2899,7 @@ fn goto_last_modified_file(cx: &mut Context) {
.last_modified_docs
.into_iter()
.flatten()
.find(|&id| id != view.doc);
.find(|&id| id != view.doc_id);
if let Some(alt) = alternate_file {
cx.editor.switch(alt, Action::Replace);
} else {
Expand Down Expand Up @@ -4397,10 +4397,10 @@ fn jump_forward(cx: &mut Context) {
let count = cx.count();
let config = cx.editor.config();
let view = view_mut!(cx.editor);
let doc_id = view.doc;
let doc_id = view.doc_id;

if let Some((id, selection)) = view.jumps.forward(count) {
view.doc = *id;
view.doc_id = *id;
let selection = selection.clone();
let (view, doc) = current!(cx.editor); // refetch doc

Expand All @@ -4420,7 +4420,7 @@ fn jump_backward(cx: &mut Context) {
let doc_id = doc.id();

if let Some((id, selection)) = view.jumps.backward(view.id, doc, count) {
view.doc = *id;
view.doc_id = *id;
let selection = selection.clone();
let (view, doc) = current!(cx.editor); // refetch doc

Expand Down
8 changes: 4 additions & 4 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ fn buffer_close_by_ids_impl(
fn buffer_gather_paths_impl(editor: &mut Editor, args: &[Cow<str>]) -> Vec<DocumentId> {
// No arguments implies current document
if args.is_empty() {
let doc_id = view!(editor).doc;
let doc_id = view!(editor).doc_id;
return vec![doc_id];
}

Expand Down Expand Up @@ -1222,7 +1222,7 @@ fn reload_all(

for view_id in view_ids {
let view = view_mut!(cx.editor, view_id);
if view.doc.eq(&doc_id) {
if view.doc_id.eq(&doc_id) {
view.ensure_cursor_in_view(doc, scrolloff);
}
}
Expand Down Expand Up @@ -1396,7 +1396,7 @@ fn vsplit(
return Ok(());
}

let id = view!(cx.editor).doc;
let id = view!(cx.editor).doc_id;

if args.is_empty() {
cx.editor.switch(id, Action::VerticalSplit);
Expand All @@ -1419,7 +1419,7 @@ fn hsplit(
return Ok(());
}

let id = view!(cx.editor).doc;
let id = view!(cx.editor).doc_id;

if args.is_empty() {
cx.editor.switch(id, Action::HorizontalSplit);
Expand Down
12 changes: 6 additions & 6 deletions helix-term/src/ui/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ impl EditorView {
.unwrap_or_else(|| editor.theme.get("ui.statusline.inactive"));

let mut x = viewport.x;
let current_doc = view!(editor).doc;
let current_doc = view!(editor).doc_id;

for doc in editor.documents() {
let fname = doc
Expand Down Expand Up @@ -1013,7 +1013,7 @@ impl EditorView {

let pos_and_view = |editor: &Editor, row, column| {
editor.tree.views().find_map(|(view, _focus)| {
view.pos_at_screen_coords(&editor.documents[&view.doc], row, column, true)
view.pos_at_screen_coords(&editor.documents[&view.doc_id], row, column, true)
.map(|pos| (pos, view.id))
})
};
Expand All @@ -1030,7 +1030,7 @@ impl EditorView {
let editor = &mut cxt.editor;

if let Some((pos, view_id)) = pos_and_view(editor, row, column) {
let doc = doc_mut!(editor, &view!(editor, view_id).doc);
let doc = doc_mut!(editor, &view!(editor, view_id).doc_id);

if modifiers == KeyModifiers::ALT {
let selection = doc.selection(view_id).clone();
Expand Down Expand Up @@ -1165,7 +1165,7 @@ impl EditorView {
}

if let Some((pos, view_id)) = pos_and_view(editor, row, column) {
let doc = doc_mut!(editor, &view!(editor, view_id).doc);
let doc = doc_mut!(editor, &view!(editor, view_id).doc_id);
doc.set_selection(view_id, Selection::point(pos));
cxt.editor.focus(view_id);
commands::MappableCommand::paste_primary_clipboard_before.execute(cxt);
Expand Down Expand Up @@ -1302,7 +1302,7 @@ impl Component for EditorView {
let config = cx.editor.config();
let mode = cx.editor.mode();
let view = view_mut!(cx.editor, focus);
let doc = doc_mut!(cx.editor, &view.doc);
let doc = doc_mut!(cx.editor, &view.doc_id);

view.ensure_cursor_in_view(doc, config.scrolloff);

Expand Down Expand Up @@ -1357,7 +1357,7 @@ impl Component for EditorView {
}

for (view, is_focused) in cx.editor.tree.views() {
let doc = cx.editor.document(view.doc).unwrap();
let doc = cx.editor.document(view.doc_id).unwrap();
self.render_view(cx.editor, doc, view, area, surface, is_focused);
}

Expand Down
2 changes: 1 addition & 1 deletion helix-term/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub fn regex_prompt(
fun: impl Fn(&mut Editor, Regex, PromptEvent) + 'static,
) {
let (view, doc) = current!(cx.editor);
let doc_id = view.doc;
let doc_id = view.doc_id;
let snapshot = doc.selection(view.id).clone();
let offset_snapshot = view.offset;
let config = cx.editor.config();
Expand Down
56 changes: 28 additions & 28 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1147,7 +1147,7 @@ impl Editor {
fn _refresh(&mut self) {
let config = self.config();
for (view, _) in self.tree.views_mut() {
let doc = doc_mut!(self, &view.doc);
let doc = doc_mut!(self, &view.doc_id);
view.sync_changes(doc);
view.gutters = config.gutters.clone();
view.ensure_cursor_in_view(doc, config.scrolloff)
Expand All @@ -1156,7 +1156,7 @@ impl Editor {

fn replace_document_in_view(&mut self, current_view: ViewId, doc_id: DocumentId) {
let view = self.tree.get_mut(current_view);
view.doc = doc_id;
view.doc_id = doc_id;
view.offset = ViewPosition::default();

let doc = doc_mut!(self, &doc_id);
Expand All @@ -1166,10 +1166,10 @@ impl Editor {
align_view(doc, view, Align::Center);
}

pub fn switch(&mut self, id: DocumentId, action: Action) {
pub fn switch(&mut self, doc_id: DocumentId, action: Action) {
use crate::tree::Layout;

if !self.documents.contains_key(&id) {
if !self.documents.contains_key(&doc_id) {
log::error!("cannot switch to document that does not exist (anymore)");
return;
}
Expand All @@ -1186,12 +1186,12 @@ impl Editor {
// If the buffer has no path and is not modified, it is an empty scratch buffer.
&& doc.path().is_none()
// If the buffer we are changing to is not this buffer
&& id != doc.id
&& doc_id != doc.id
// Ensure the buffer is not displayed in any other splits.
&& !self
.tree
.traverse()
.any(|(_, v)| v.doc == doc.id && v.id != view.id);
.any(|(_, v)| v.doc_id == doc.id && v.id != view.id);

let (view, doc) = current!(self);
let view_id = view.id;
Expand All @@ -1210,27 +1210,28 @@ impl Editor {
view.remove_document(&id);
}
} else {
let jump = (view.doc, doc.selection(view.id).clone());
let jump = (view.doc_id, doc.selection(view.id).clone());
view.jumps.push(jump);
// Set last accessed doc if it is a different document
if doc.id != id {
view.add_to_history(view.doc);
if doc.id != doc_id {
view.add_to_history(view.doc_id);
// Set last modified doc if modified and last modified doc is different
if std::mem::take(&mut doc.modified_since_accessed)
&& view.last_modified_docs[0] != Some(view.doc)
&& view.last_modified_docs[0] != Some(view.doc_id)
{
view.last_modified_docs = [Some(view.doc), view.last_modified_docs[0]];
view.last_modified_docs =
[Some(view.doc_id), view.last_modified_docs[0]];
}
}
}

self.replace_document_in_view(view_id, id);
self.replace_document_in_view(view_id, doc_id);

return;
}
Action::Load => {
let view_id = view!(self).id;
let doc = doc_mut!(self, &id);
let doc = doc_mut!(self, &doc_id);
doc.ensure_view_init(view_id);
return;
}
Expand All @@ -1239,9 +1240,9 @@ impl Editor {
let view = self
.tree
.try_get(self.tree.focus)
.filter(|v| id == v.doc) // Different Document
.filter(|v| doc_id == v.doc_id) // Different Document
.cloned()
.unwrap_or_else(|| View::new(id, self.config().gutters.clone()));
.unwrap_or_else(|| View::new(doc_id, self.config().gutters.clone()));
let view_id = self.tree.split(
view,
match action {
Expand All @@ -1251,7 +1252,7 @@ impl Editor {
},
);
// initialize selection for view
let doc = doc_mut!(self, &id);
let doc = doc_mut!(self, &doc_id);
doc.ensure_view_init(view_id);
}
}
Expand All @@ -1261,26 +1262,25 @@ impl Editor {

/// Generate an id for a new document and register it.
fn new_document(&mut self, mut doc: Document) -> DocumentId {
let id = self.next_document_id;
let doc_id = self.next_document_id;
// Safety: adding 1 from 1 is fine, probably impossible to reach usize max
self.next_document_id =
DocumentId(unsafe { NonZeroUsize::new_unchecked(self.next_document_id.0.get() + 1) });
doc.id = id;
self.documents.insert(id, doc);
doc.id = doc_id;
self.documents.insert(doc_id, doc);

let (save_sender, save_receiver) = tokio::sync::mpsc::unbounded_channel();
self.saves.insert(id, save_sender);

self.saves.insert(doc_id, save_sender);
let stream = UnboundedReceiverStream::new(save_receiver).flatten();
self.save_queue.push(stream);

id
doc_id
}

fn new_file_from_document(&mut self, action: Action, doc: Document) -> DocumentId {
let id = self.new_document(doc);
self.switch(id, action);
id
let doc_id = self.new_document(doc);
self.switch(doc_id, action);
doc_id
}

pub fn new_file(&mut self, action: Action) -> DocumentId {
Expand Down Expand Up @@ -1361,7 +1361,7 @@ impl Editor {
.filter_map(|(view, _focus)| {
view.remove_document(&doc_id);

if view.doc == doc_id {
if view.doc_id == doc_id {
// something was previously open in the view, switch to previous doc
if let Some(prev_doc) = view.docs_access_history.pop() {
Some(Action::ReplaceDoc(view.id, prev_doc))
Expand Down Expand Up @@ -1452,7 +1452,7 @@ impl Editor {

// Update jumplist selections with new document changes.
for (view, _focused) in self.tree.views_mut() {
let doc = doc_mut!(self, &view.doc);
let doc = doc_mut!(self, &view.doc_id);
view.sync_changes(doc);
}
}
Expand Down Expand Up @@ -1488,7 +1488,7 @@ impl Editor {
pub fn ensure_cursor_in_view(&mut self, id: ViewId) {
let config = self.config();
let view = self.tree.get_mut(id);
let doc = &self.documents[&view.doc];
let doc = &self.documents[&view.doc_id];
view.ensure_cursor_in_view(doc, config.scrolloff)
}

Expand Down
29 changes: 14 additions & 15 deletions helix-view/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
macro_rules! current {
($editor:expr) => {{
let view = $crate::view_mut!($editor);
let id = view.doc;
let doc = $crate::doc_mut!($editor, &id);
let doc = $crate::doc_mut!($editor, &view.doc_id);
(view, doc)
}};
}
Expand All @@ -23,7 +22,7 @@ macro_rules! current {
macro_rules! current_ref {
($editor:expr) => {{
let view = $editor.tree.get($editor.tree.focus);
let doc = &$editor.documents[&view.doc];
let doc = &$editor.documents[&view.doc_id];
(view, doc)
}};
}
Expand All @@ -32,14 +31,24 @@ macro_rules! current_ref {
/// Returns `&mut Document`
#[macro_export]
macro_rules! doc_mut {
($editor:expr, $id:expr) => {{
$editor.documents.get_mut($id).unwrap()
($editor:expr, $doc_id:expr) => {{
$editor.documents.get_mut($doc_id).unwrap()
}};
($editor:expr) => {{
$crate::current!($editor).1
}};
}

#[macro_export]
macro_rules! doc {
($editor:expr, $doc_id:expr) => {{
&$editor.documents[$doc_id]
}};
($editor:expr) => {{
$crate::current_ref!($editor).1
}};
}

/// Get the current view mutably.
/// Returns `&mut View`
#[macro_export]
Expand All @@ -63,13 +72,3 @@ macro_rules! view {
$editor.tree.get($editor.tree.focus)
}};
}

#[macro_export]
macro_rules! doc {
($editor:expr, $id:expr) => {{
&$editor.documents[$id]
}};
($editor:expr) => {{
$crate::current_ref!($editor).1
}};
}
Loading