use std::collections::{BTreeSet, HashMap, VecDeque};
use std::num::NonZeroUsize;
use crate::app::{Action, AppEffect, Event, GitEffect, SearchState};
use crate::domain::{
ChangedFile, CommitMessage, CommitPage, CommitSummary, DiffDocument, DiffTarget,
DocumentSymbol, FileDocument, FileRevision, NavigationTarget, ObjectId, RepoPath,
RepositoryRoot, SearchHit, SemanticNavigationKind, SourcePosition, TreeEntry, WorktreeChange,
};
const MAX_DIFF_CACHE_ENTRIES: usize = 16;
const MAX_DIFF_CACHE_BYTES: usize = 16 * 1024 * 1024;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum VerticalEdge {
Top,
Bottom,
}
impl VerticalEdge {
pub(crate) const fn is_bottom(self) -> bool {
matches!(self, Self::Bottom)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum HorizontalDirection {
Left,
Right,
}
impl HorizontalDirection {
pub(crate) const fn is_right(self) -> bool {
matches!(self, Self::Right)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum CursorColumnPolicy {
Preserve,
Reset,
}
impl CursorColumnPolicy {
pub(crate) const fn resets_column(self) -> bool {
matches!(self, Self::Reset)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum AppView {
Changes,
History,
CommitDetails,
Graph,
GraphDetails,
FileHistory,
Code,
}
impl AppView {
pub(crate) const fn uses_commit_history(self) -> bool {
matches!(
self,
Self::History | Self::CommitDetails | Self::Graph | Self::GraphDetails
)
}
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum LspAvailability {
#[default]
Disabled,
Enabled,
}
impl LspAvailability {
pub(crate) const fn is_enabled(self) -> bool {
matches!(self, Self::Enabled)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum FocusedPane {
Primary,
Secondary,
Diff,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum HistoryPanel {
ChangedFiles,
Tree,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum HistoryPreview {
Message,
Diff,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Overlay {
None,
Help,
CommitMessage,
Diff,
RepositorySearch,
FileContent,
CodeContent,
SemanticTargets,
LspHover,
FullFile,
SymbolContext,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum RepositorySearchKind {
Files,
Content,
}
impl RepositorySearchKind {
#[must_use]
pub fn label(self) -> &'static str {
match self {
Self::Files => "files",
Self::Content => "content",
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum CommitLoadMode {
Replace,
Append,
}
impl CommitLoadMode {
pub(crate) const fn is_append(self) -> bool {
matches!(self, Self::Append)
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct RequestId(u64);
impl RequestId {
#[must_use]
pub(crate) const fn value(self) -> u64 {
self.0
}
const FIRST: Self = Self(1);
fn advance(&mut self) {
self.0 = self.0.saturating_add(1);
}
}
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
pub struct DocumentRevision(u64);
impl DocumentRevision {
pub(crate) fn advance(&mut self) {
self.0 = self.0.saturating_add(1);
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ErrorNotice(String);
impl ErrorNotice {
#[must_use]
pub fn new(message: impl Into<String>) -> Self {
Self(message.into())
}
#[must_use]
pub fn message(&self) -> &str {
&self.0
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum LoadState<T> {
Idle,
Loading {
request_id: RequestId,
},
Ready(T),
Failed(ErrorNotice),
}
impl<T> LoadState<T> {
#[must_use]
pub fn loading_request(&self) -> Option<RequestId> {
match self {
Self::Loading { request_id } => Some(*request_id),
Self::Idle | Self::Ready(_) | Self::Failed(_) => None,
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct Selection {
index: Option<usize>,
pub(crate) viewport_top: usize,
}
impl Selection {
pub(crate) fn new() -> Self {
Self {
index: None,
viewport_top: 0,
}
}
pub(crate) fn index(&self) -> Option<usize> {
self.index
}
pub(crate) fn reset(&mut self, len: usize) {
self.index = (len > 0).then_some(0);
self.viewport_top = 0;
}
pub(crate) fn reset_to(&mut self, len: usize, preferred: Option<usize>) {
if len == 0 {
self.viewport_top = 0;
}
self.index = if len == 0 {
None
} else {
Some(preferred.unwrap_or(0).min(len - 1))
};
}
pub(crate) fn clamp(&mut self, len: usize) {
if len == 0 {
self.viewport_top = 0;
}
self.index = match (self.index, len) {
(_, 0) => None,
(Some(index), _) => Some(index.min(len - 1)),
(None, _) => Some(0),
};
}
pub(crate) fn move_by(&mut self, delta: isize, len: usize) -> bool {
if delta < 0 {
self.move_up(delta.unsigned_abs(), len)
} else {
self.move_down(delta.unsigned_abs(), len)
}
}
pub(crate) fn move_up(&mut self, count: usize, len: usize) -> bool {
let previous = self.index;
self.reset_to(len, Some(self.index.unwrap_or(0).saturating_sub(count)));
previous != self.index
}
pub(crate) fn move_down(&mut self, count: usize, len: usize) -> bool {
let previous = self.index;
self.reset_to(len, Some(self.index.unwrap_or(0).saturating_add(count)));
previous != self.index
}
pub(crate) fn top(&mut self, len: usize) -> bool {
let previous = self.index;
self.index = (len > 0).then_some(0);
previous != self.index
}
pub(crate) fn bottom(&mut self, len: usize) -> bool {
let previous = self.index;
self.index = len.checked_sub(1);
previous != self.index
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct VisibleTreeEntry {
entry: TreeEntry,
path: RepoPath,
depth: usize,
expansion: ExpansionState,
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
enum ExpansionState {
#[default]
Collapsed,
Expanded,
}
impl VisibleTreeEntry {
#[must_use]
pub(crate) fn new(entry: TreeEntry, path: RepoPath, depth: usize) -> Self {
Self {
entry,
path,
depth,
expansion: ExpansionState::Collapsed,
}
}
#[must_use]
pub fn entry(&self) -> &TreeEntry {
&self.entry
}
#[must_use]
pub fn path(&self) -> &RepoPath {
&self.path
}
#[must_use]
pub fn depth(&self) -> usize {
self.depth
}
#[must_use]
pub fn expanded(&self) -> bool {
self.expansion == ExpansionState::Expanded
}
pub(crate) fn expand(&mut self) {
self.expansion = ExpansionState::Expanded;
}
pub(crate) fn collapse(&mut self) {
self.expansion = ExpansionState::Collapsed;
}
}
#[derive(Clone, Debug)]
pub(crate) struct DiffViewState {
pub(crate) target: Option<DiffTarget>,
pub(crate) content: LoadState<DiffDocument>,
pub(crate) vertical: usize,
pub(crate) byte_column: usize,
pub(crate) desired_display_column: Option<usize>,
pub(crate) viewport_vertical: usize,
pub(crate) horizontal: usize,
}
impl DiffViewState {
pub(crate) fn new() -> Self {
Self {
target: None,
content: LoadState::Idle,
vertical: 0,
byte_column: 0,
desired_display_column: None,
viewport_vertical: 0,
horizontal: 0,
}
}
}
#[derive(Clone, Debug)]
pub(crate) struct MessageState {
pub(crate) commit: Option<ObjectId>,
pub(crate) content: LoadState<CommitMessage>,
pub(crate) scroll: usize,
pub(crate) byte_column: usize,
pub(crate) desired_display_column: Option<usize>,
pub(crate) viewport_vertical: usize,
pub(crate) horizontal: usize,
}
#[derive(Clone, Debug)]
pub(crate) struct HistoryPageState {
pub(crate) continuation: HistoryContinuation,
pub(crate) loading_more: Option<RequestId>,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum HistoryContinuation {
Exhausted,
Available,
}
impl HistoryContinuation {
pub(crate) const fn has_more(self) -> bool {
matches!(self, Self::Available)
}
}
#[derive(Clone, Debug)]
pub(crate) struct TreeState {
pub(crate) commit: Option<ObjectId>,
pub(crate) visible: LoadState<Vec<VisibleTreeEntry>>,
pub(crate) selection: Selection,
pub(crate) children: HashMap<ObjectId, Vec<TreeEntry>>,
pub(crate) pending: Option<RequestId>,
}
#[derive(Debug)]
pub(crate) struct RepositorySearchState {
pub(crate) kind: RepositorySearchKind,
pub(crate) prompt: Option<String>,
pub(crate) query: String,
pub(crate) results: LoadState<Vec<SearchHit>>,
pub(crate) selection: Selection,
pub(crate) return_view: AppView,
}
impl RepositorySearchState {
fn new(view: AppView) -> Self {
Self {
kind: RepositorySearchKind::Files,
prompt: None,
query: String::new(),
results: LoadState::Idle,
selection: Selection::new(),
return_view: view,
}
}
}
#[derive(Debug)]
pub(crate) struct FileViewState {
pub(crate) path: Option<RepoPath>,
pub(crate) commits: LoadState<Vec<CommitSummary>>,
pub(crate) selection: Selection,
pub(crate) content: LoadState<FileDocument>,
pub(crate) mode: FileViewMode,
pub(crate) vertical: usize,
pub(crate) byte_column: usize,
pub(crate) desired_display_column: Option<usize>,
pub(crate) viewport_vertical: usize,
pub(crate) horizontal: usize,
pub(crate) return_view: AppView,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum FileViewMode {
CurrentContent,
HistoryDiff,
}
impl FileViewMode {
pub(crate) const fn shows_history_diff(self) -> bool {
matches!(self, Self::HistoryDiff)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum CodeEntryKind {
Directory,
File,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct VisibleCodeEntry {
path: RepoPath,
name: RepoPath,
depth: usize,
kind: CodeEntryKind,
expansion: ExpansionState,
}
impl VisibleCodeEntry {
pub(crate) fn new(path: RepoPath, name: RepoPath, depth: usize, kind: CodeEntryKind) -> Self {
Self {
path,
name,
depth,
kind,
expansion: ExpansionState::Collapsed,
}
}
pub(crate) fn path(&self) -> &RepoPath {
&self.path
}
pub(crate) fn name(&self) -> &RepoPath {
&self.name
}
pub(crate) fn depth(&self) -> usize {
self.depth
}
pub(crate) fn kind(&self) -> CodeEntryKind {
self.kind
}
pub(crate) fn expanded(&self) -> bool {
self.expansion == ExpansionState::Expanded
}
pub(crate) fn expand(&mut self) {
self.expansion = ExpansionState::Expanded;
}
pub(crate) fn collapse(&mut self) {
self.expansion = ExpansionState::Collapsed;
}
}
#[derive(Debug)]
pub(crate) struct CodeViewState {
pub(crate) files: Vec<RepoPath>,
pub(crate) visible: LoadState<Vec<VisibleCodeEntry>>,
pub(crate) selection: Selection,
pub(crate) path: Option<RepoPath>,
pub(crate) content: LoadState<FileDocument>,
pub(crate) cursor: SourcePosition,
pub(crate) desired_display_column: Option<usize>,
pub(crate) viewport_vertical: usize,
pub(crate) viewport_horizontal: usize,
pub(crate) pending_reveal: Option<RepoPath>,
pub(crate) document_revision: DocumentRevision,
}
impl CodeViewState {
fn new() -> Self {
Self {
files: Vec::new(),
visible: LoadState::Idle,
selection: Selection::new(),
path: None,
content: LoadState::Idle,
cursor: SourcePosition::new(0, 0),
desired_display_column: None,
viewport_vertical: 0,
viewport_horizontal: 0,
pending_reveal: None,
document_revision: DocumentRevision::default(),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct NavigationOrigin {
pub(crate) path: RepoPath,
pub(crate) cursor: SourcePosition,
pub(crate) first_non_blank_column: usize,
pub(crate) viewport_vertical: usize,
pub(crate) viewport_horizontal: usize,
}
#[derive(Debug)]
pub(crate) struct SemanticNavigationState {
pub(crate) targets: LoadState<Vec<NavigationTarget>>,
pub(crate) selection: Selection,
pub(crate) source_path: Option<RepoPath>,
pub(crate) source_position: SourcePosition,
pub(crate) source_revision: DocumentRevision,
pub(crate) kind: Option<SemanticNavigationKind>,
pub(crate) status: Option<String>,
pub(crate) back_stack: VecDeque<NavigationOrigin>,
pub(crate) forward_stack: VecDeque<NavigationOrigin>,
}
#[derive(Debug)]
pub(crate) struct LspHoverState {
pub(crate) content: LoadState<Option<String>>,
pub(crate) source_path: Option<RepoPath>,
pub(crate) source_position: SourcePosition,
pub(crate) source_revision: DocumentRevision,
pub(crate) scroll: usize,
pub(crate) status: Option<String>,
pub(crate) return_overlay: Overlay,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum FullFileMode {
Changes,
New,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum SourceDiffContext {
None,
Pending,
Ready,
}
impl SourceDiffContext {
pub(crate) const fn has_diff(self) -> bool {
matches!(self, Self::Pending | Self::Ready)
}
pub(crate) const fn is_pending(self) -> bool {
matches!(self, Self::Pending)
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct FullFileDeletion {
pub(crate) anchor: u32,
pub(crate) old_line: Option<u32>,
pub(crate) content: String,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct SourceFileIdentity {
pub(crate) revision: FileRevision,
pub(crate) path: RepoPath,
}
#[derive(Debug)]
pub(crate) struct FullFileState {
pub(crate) identity: Option<SourceFileIdentity>,
pub(crate) content: LoadState<FileDocument>,
pub(crate) mode: FullFileMode,
pub(crate) cursor: SourcePosition,
pub(crate) desired_display_column: Option<usize>,
pub(crate) viewport_vertical: usize,
pub(crate) viewport_horizontal: usize,
pub(crate) changed_lines: BTreeSet<u32>,
pub(crate) deleted_lines: Vec<FullFileDeletion>,
pub(crate) diff_context: SourceDiffContext,
pub(crate) return_overlay: Overlay,
pub(crate) document_revision: DocumentRevision,
}
impl FullFileState {
fn new() -> Self {
Self {
identity: None,
content: LoadState::Idle,
mode: FullFileMode::New,
cursor: SourcePosition::new(0, 0),
desired_display_column: None,
viewport_vertical: 0,
viewport_horizontal: 0,
changed_lines: BTreeSet::new(),
deleted_lines: Vec::new(),
diff_context: SourceDiffContext::None,
return_overlay: Overlay::None,
document_revision: DocumentRevision::default(),
}
}
}
#[derive(Debug)]
pub(crate) struct SymbolContextState {
pub(crate) symbols: LoadState<Vec<DocumentSymbol>>,
pub(crate) selection: Selection,
pub(crate) return_overlay: Overlay,
pub(crate) full_file_return_overlay: Overlay,
pub(crate) source_path: Option<RepoPath>,
pub(crate) source_revision: DocumentRevision,
pub(crate) status: Option<String>,
pub(crate) pending_source_request: Option<RequestId>,
}
impl SymbolContextState {
fn new() -> Self {
Self {
symbols: LoadState::Idle,
selection: Selection::new(),
return_overlay: Overlay::None,
full_file_return_overlay: Overlay::None,
source_path: None,
source_revision: DocumentRevision::default(),
status: None,
pending_source_request: None,
}
}
}
impl LspHoverState {
fn new() -> Self {
Self {
content: LoadState::Idle,
source_path: None,
source_position: SourcePosition::new(0, 0),
source_revision: DocumentRevision::default(),
scroll: 0,
status: None,
return_overlay: Overlay::None,
}
}
}
impl SemanticNavigationState {
fn new() -> Self {
Self {
targets: LoadState::Idle,
selection: Selection::new(),
source_path: None,
source_position: SourcePosition::new(0, 0),
source_revision: DocumentRevision::default(),
kind: None,
status: None,
back_stack: VecDeque::new(),
forward_stack: VecDeque::new(),
}
}
}
impl FileViewState {
fn new(view: AppView) -> Self {
Self {
path: None,
commits: LoadState::Idle,
selection: Selection::new(),
content: LoadState::Idle,
mode: FileViewMode::CurrentContent,
vertical: 0,
byte_column: 0,
desired_display_column: None,
viewport_vertical: 0,
horizontal: 0,
return_view: view,
}
}
}
#[derive(Debug)]
struct DiffCache {
entries: VecDeque<(DiffTarget, DiffDocument, usize)>,
bytes: usize,
}
impl DiffCache {
fn new() -> Self {
Self {
entries: VecDeque::new(),
bytes: 0,
}
}
fn get(&mut self, target: &DiffTarget) -> Option<DiffDocument> {
let index = self.entries.iter().position(|(key, _, _)| key == target)?;
let entry = self.entries.remove(index)?;
let result = entry.1.clone();
self.entries.push_front(entry);
Some(result)
}
fn insert(&mut self, target: DiffTarget, document: DiffDocument) {
let size = document.approximate_bytes();
if size > MAX_DIFF_CACHE_BYTES {
return;
}
if let Some(index) = self.entries.iter().position(|(key, _, _)| key == &target)
&& let Some((_, _, old_size)) = self.entries.remove(index)
{
self.bytes = self.bytes.saturating_sub(old_size);
}
self.bytes = self.bytes.saturating_add(size);
self.entries.push_front((target, document, size));
while self.entries.len() > MAX_DIFF_CACHE_ENTRIES || self.bytes > MAX_DIFF_CACHE_BYTES {
if let Some((_, _, removed)) = self.entries.pop_back() {
self.bytes = self.bytes.saturating_sub(removed);
} else {
break;
}
}
}
fn clear(&mut self) {
self.entries.clear();
self.bytes = 0;
}
}
#[derive(Debug)]
pub struct AppState {
pub(crate) root: RepositoryRoot,
pub(crate) view: AppView,
pub(crate) focus: FocusedPane,
pub(crate) history_panel: HistoryPanel,
pub(crate) history_preview: HistoryPreview,
pub(crate) overlay: Overlay,
pub(crate) should_quit: bool,
pub(crate) wrap_lines: bool,
pub(crate) branch_picker: Option<crate::app::branches::BranchPicker>,
pub(crate) changes: LoadState<Vec<WorktreeChange>>,
pub(crate) change_selection: Selection,
pub(crate) commits: LoadState<Vec<CommitSummary>>,
pub(crate) commit_selection: Selection,
pub(crate) history_page: HistoryPageState,
pub(crate) files: LoadState<Vec<ChangedFile>>,
pub(crate) file_selection: Selection,
pub(crate) diff: DiffViewState,
pub(crate) message: MessageState,
pub(crate) tree: TreeState,
pub(crate) search: SearchState,
pub(crate) repository_search: RepositorySearchState,
pub(crate) file_view: FileViewState,
pub(crate) code_view: CodeViewState,
pub(crate) semantic_navigation: SemanticNavigationState,
pub(crate) vim_marks: HashMap<char, NavigationOrigin>,
pub(crate) lsp_hover: LspHoverState,
pub(crate) full_file: FullFileState,
pub(crate) symbol_context: SymbolContextState,
pub(crate) lsp_availability: LspAvailability,
pub(crate) notice: Option<ErrorNotice>,
pub(crate) preferred_change: Option<RepoPath>,
pub(crate) preferred_commit: Option<ObjectId>,
pub(crate) terminal_width: u16,
pub(crate) terminal_height: u16,
pub(crate) scrolloff: usize,
next_request: RequestId,
diff_cache: DiffCache,
}
impl AppState {
pub(crate) fn history_message_focused(&self) -> bool {
self.view == AppView::History
&& self.focus == FocusedPane::Diff
&& self.history_preview == HistoryPreview::Message
}
#[must_use]
pub fn new(root: RepositoryRoot, view: AppView) -> Self {
Self {
root,
view,
focus: FocusedPane::Primary,
history_panel: HistoryPanel::ChangedFiles,
history_preview: HistoryPreview::Message,
overlay: Overlay::None,
should_quit: false,
wrap_lines: false,
branch_picker: None,
changes: LoadState::Idle,
change_selection: Selection::new(),
commits: LoadState::Idle,
commit_selection: Selection::new(),
history_page: HistoryPageState {
continuation: HistoryContinuation::Exhausted,
loading_more: None,
},
files: LoadState::Idle,
file_selection: Selection::new(),
diff: DiffViewState::new(),
message: MessageState {
commit: None,
content: LoadState::Idle,
scroll: 0,
byte_column: 0,
desired_display_column: None,
viewport_vertical: 0,
horizontal: 0,
},
tree: TreeState {
commit: None,
visible: LoadState::Idle,
selection: Selection::new(),
children: HashMap::new(),
pending: None,
},
search: SearchState::new(),
repository_search: RepositorySearchState::new(view),
file_view: FileViewState::new(view),
code_view: CodeViewState::new(),
semantic_navigation: SemanticNavigationState::new(),
vim_marks: HashMap::new(),
lsp_hover: LspHoverState::new(),
full_file: FullFileState::new(),
symbol_context: SymbolContextState::new(),
lsp_availability: LspAvailability::Disabled,
notice: None,
preferred_change: None,
preferred_commit: None,
terminal_width: 80,
terminal_height: 24,
scrolloff: crate::config::DEFAULT_SCROLLOFF,
next_request: RequestId::FIRST,
diff_cache: DiffCache::new(),
}
}
pub fn set_terminal_size(&mut self, width: u16, height: u16) {
self.terminal_width = width;
self.terminal_height = height;
crate::app::update::sync_viewport(self);
}
pub fn set_scrolloff(&mut self, rows: usize) {
self.scrolloff = rows;
crate::app::update::sync_viewport(self);
}
pub fn set_lsp_availability(&mut self, availability: LspAvailability) {
self.lsp_availability = availability;
}
pub fn start(&mut self) -> Vec<GitEffect> {
match self.view {
AppView::Changes => self.request_changes(),
AppView::History | AppView::CommitDetails | AppView::Graph | AppView::GraphDetails => {
self.request_commits(crate::app::CommitLoadMode::Replace)
}
AppView::FileHistory => Vec::new(),
AppView::Code => crate::app::code_view::request_tree(self),
}
}
pub fn start_effects(&mut self) -> Vec<AppEffect> {
self.start().into_iter().map(AppEffect::from).collect()
}
pub fn handle_action(&mut self, action: Action) -> Vec<GitEffect> {
let sync = self.action_updates_viewport(action);
let effects = crate::app::update::apply_action(self, action);
if sync {
crate::app::update::sync_viewport(self);
}
effects
}
pub fn handle_app_action(&mut self, action: Action) -> Vec<AppEffect> {
let sync = self.action_updates_viewport(action);
let effects = self.dispatch_app_action(action);
if sync {
crate::app::update::sync_viewport(self);
}
effects
}
fn action_updates_viewport(&self, action: Action) -> bool {
!matches!(
action,
Action::StartSearch(_)
| Action::InsertSearch(_)
| Action::DeleteSearch
| Action::CancelSearch
| Action::Tick
) && !(action == Action::DismissSearchOrClose
&& (self.is_search_input_active() || self.has_active_search_highlights()))
}
fn dispatch_app_action(&mut self, action: Action) -> Vec<AppEffect> {
if action == Action::OpenBranches || self.branch_picker.is_some() {
return self
.handle_action(action)
.into_iter()
.map(AppEffect::from)
.collect();
}
if action == Action::Quit {
self.should_quit = true;
return Vec::new();
}
if let Some(effects) = crate::app::semantic_navigation::apply_action(self, action) {
effects
} else if let Some(effects) = crate::app::source_view::apply_action(self, action) {
effects
} else {
self.handle_action(action)
.into_iter()
.map(AppEffect::from)
.collect()
}
}
pub fn handle_event(&mut self, event: Event) -> Vec<GitEffect> {
let effects = crate::app::update::apply_event(self, event);
crate::app::update::sync_viewport(self);
effects
}
pub fn handle_app_event(&mut self, event: Event) -> Vec<AppEffect> {
let effects = self.dispatch_app_event(event);
crate::app::update::sync_viewport(self);
effects
}
fn dispatch_app_event(&mut self, event: Event) -> Vec<AppEffect> {
match event {
Event::SemanticNavigationCompleted { .. }
| Event::LspHoverCompleted { .. }
| Event::DocumentSymbolsCompleted { .. }
| Event::LspStatus { .. } => crate::app::semantic_navigation::apply_event(self, event),
Event::FullFileLoaded { .. } => {
crate::app::source_view::apply_event(self, event).unwrap_or_default()
}
event => self
.handle_event(event)
.into_iter()
.map(AppEffect::from)
.collect(),
}
}
#[must_use]
pub fn should_quit(&self) -> bool {
self.should_quit
}
#[must_use]
pub fn view(&self) -> AppView {
self.view
}
#[must_use]
pub fn focus(&self) -> FocusedPane {
self.focus
}
#[must_use]
pub fn overlay(&self) -> Overlay {
self.overlay
}
#[must_use]
pub fn is_search_input_active(&self) -> bool {
if self.branch_picker.is_some() {
return false;
}
self.search.is_input_active()
|| (self.overlay == Overlay::RepositorySearch
&& self.repository_search.prompt.is_some())
}
pub(crate) fn has_active_search_highlights(&self) -> bool {
crate::app::update::has_active_search_highlights(self)
}
pub(crate) fn request_id(&mut self) -> RequestId {
let id = self.next_request;
self.next_request.advance();
id
}
pub(crate) fn reload_after_branch_switch(&mut self) -> Vec<GitEffect> {
let file_path = self.file_view.path.clone();
let return_view = self.file_view.return_view;
let mut fresh = Self::new(self.root.clone(), self.view);
fresh.next_request = self.next_request;
fresh.terminal_width = self.terminal_width;
fresh.scrolloff = self.scrolloff;
fresh.wrap_lines = self.wrap_lines;
fresh.terminal_height = self.terminal_height;
fresh.lsp_availability = self.lsp_availability;
fresh.should_quit = self.should_quit;
*self = fresh;
if self.view == AppView::FileHistory {
self.file_view.return_view = return_view;
file_path
.map(|path| crate::app::repository_search::load_file_view(self, path))
.unwrap_or_default()
} else {
self.start()
}
}
pub(crate) fn request_changes(&mut self) -> Vec<GitEffect> {
let request_id = self.request_id();
self.changes = LoadState::Loading { request_id };
self.notice = None;
vec![GitEffect::LoadChanges { request_id }]
}
pub(crate) fn request_commits(&mut self, mode: CommitLoadMode) -> Vec<GitEffect> {
const PAGE_SIZE: NonZeroUsize = NonZeroUsize::new(200).unwrap();
let request_id = self.request_id();
let skip = if mode.is_append() {
match &self.commits {
LoadState::Ready(commits) => commits.len(),
_ => 0,
}
} else {
self.commits = LoadState::Loading { request_id };
self.history_page.loading_more = None;
0
};
if mode.is_append() {
self.history_page.loading_more = Some(request_id);
}
self.notice = None;
vec![GitEffect::LoadCommits {
request_id,
page: CommitPage::new(skip, PAGE_SIZE),
mode,
}]
}
pub(crate) fn request_diff(&mut self, target: DiffTarget) -> Vec<GitEffect> {
self.diff.vertical = 0;
self.diff.byte_column = 0;
self.diff.desired_display_column = None;
self.diff.viewport_vertical = 0;
self.diff.horizontal = 0;
self.search.clear();
if let Some(cached) = self.diff_cache.get(&target) {
self.diff.target = Some(target);
self.diff.content = LoadState::Ready(cached);
return Vec::new();
}
let request_id = self.request_id();
self.diff.target = Some(target.clone());
self.diff.content = LoadState::Loading { request_id };
vec![GitEffect::LoadDiff { request_id, target }]
}
pub(crate) fn cache_diff(&mut self, target: DiffTarget, document: DiffDocument) {
self.diff_cache.insert(target, document);
}
pub(crate) fn clear_cache(&mut self) {
self.diff_cache.clear();
}
}