Skip to content

Commit

Permalink
(rename) View.doc -> View.doc_id
Browse files Browse the repository at this point in the history
  • Loading branch information
gibbz00 committed Feb 20, 2023
1 parent 44729fb commit a59dfcb
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 50 deletions.
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
23 changes: 12 additions & 11 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 Down Expand Up @@ -1191,7 +1191,7 @@ impl Editor {
&& !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,16 +1210,17 @@ 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);
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]];
}
}
}
Expand All @@ -1239,7 +1240,7 @@ impl Editor {
let view = self
.tree
.try_get(self.tree.focus)
.filter(|v| id == v.doc) // Different Document
.filter(|v| id == v.doc_id) // Different Document
.cloned()
.unwrap_or_else(|| View::new(id, self.config().gutters.clone()));
let view_id = self.tree.split(
Expand Down Expand Up @@ -1361,7 +1362,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 +1453,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 +1489,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
}};
}
2 changes: 1 addition & 1 deletion helix-view/src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ mod test {

fn doc_id(tree: &Tree, view_id: ViewId) -> Option<DocumentId> {
if let Content::View(view) = &tree.nodes[view_id].content {
Some(view.doc)
Some(view.doc_id)
} else {
None
}
Expand Down
6 changes: 3 additions & 3 deletions helix-view/src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ pub struct View {
pub id: ViewId,
pub offset: ViewPosition,
pub area: Rect,
pub doc: DocumentId,
pub doc_id: DocumentId,
pub jumps: JumpList,
// documents accessed from this view from the oldest one to last viewed one
pub docs_access_history: Vec<DocumentId>,
Expand All @@ -132,7 +132,7 @@ impl fmt::Debug for View {
f.debug_struct("View")
.field("id", &self.id)
.field("area", &self.area)
.field("doc", &self.doc)
.field("doc", &self.doc_id)
.finish()
}
}
Expand All @@ -141,7 +141,7 @@ impl View {
pub fn new(doc: DocumentId, gutters: GutterConfig) -> Self {
Self {
id: ViewId::default(),
doc,
doc_id: doc,
offset: ViewPosition {
anchor: 0,
horizontal_offset: 0,
Expand Down

0 comments on commit a59dfcb

Please sign in to comment.