1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
//! # Application Customization
//!
//! ## Overview
//!
//! This module contains traits that library consumers can use for extending modalkit to better fit
//! their own needs.
//!
//! ## Example
//!
//! ```
//! use std::fmt;
//! use std::path::PathBuf;
//!
//! use modalkit::{
//! editing::{
//! application::{
//! ApplicationAction,
//! ApplicationError,
//! ApplicationWindowId,
//! ApplicationContentId,
//! ApplicationInfo,
//! ApplicationStore,
//! },
//! context::EditContext,
//! },
//! keybindings::SequenceStatus,
//! prelude::*,
//! };
//!
//! // Unique identifier for a review.
//! #[derive(Clone, Debug, Eq, Hash, PartialEq)]
//! struct ReviewId(usize);
//!
//! // Unique identifier for a user.
//! #[derive(Clone, Debug, Eq, Hash, PartialEq)]
//! struct UserId(usize);
//!
//! #[derive(Clone, Debug, Eq, PartialEq)]
//! enum CodeReviewAction {
//! // Approve a review for merging.
//! Approve(ReviewId),
//!
//! // Leave a comment on a line in a file in a review.
//! Comment(ReviewId, PathBuf, usize, String),
//!
//! // Show more lines around the hunk.
//! ExpandHunk(Count),
//!
//! // Merge changes after review and approval.
//! Merge(ReviewId),
//! }
//!
//! impl ApplicationAction for CodeReviewAction {
//! fn is_edit_sequence(&self, _: &EditContext) -> SequenceStatus {
//! match self {
//! CodeReviewAction::Approve(..) => SequenceStatus::Break,
//! CodeReviewAction::Comment(..) => SequenceStatus::Break,
//! CodeReviewAction::ExpandHunk(..) => SequenceStatus::Atom,
//! CodeReviewAction::Merge(..) => SequenceStatus::Break,
//! }
//! }
//!
//! fn is_last_action(&self, _: &EditContext) -> SequenceStatus {
//! match self {
//! CodeReviewAction::Approve(..) => SequenceStatus::Atom,
//! CodeReviewAction::Comment(..) => SequenceStatus::Atom,
//! CodeReviewAction::ExpandHunk(..) => SequenceStatus::Atom,
//! CodeReviewAction::Merge(..) => SequenceStatus::Atom,
//! }
//! }
//!
//! fn is_last_selection(&self, _: &EditContext) -> SequenceStatus {
//! match self {
//! CodeReviewAction::Approve(..) => SequenceStatus::Ignore,
//! CodeReviewAction::Comment(..) => SequenceStatus::Ignore,
//! CodeReviewAction::ExpandHunk(..) => SequenceStatus::Ignore,
//! CodeReviewAction::Merge(..) => SequenceStatus::Ignore,
//! }
//! }
//!
//! fn is_switchable(&self, _: &EditContext) -> bool {
//! match self {
//! CodeReviewAction::Approve(..) => false,
//! CodeReviewAction::Comment(..) => false,
//! CodeReviewAction::ExpandHunk(..) => false,
//! CodeReviewAction::Merge(..) => false,
//! }
//! }
//! }
//!
//! struct CodeReviewStore {
//! user: UserId,
//! }
//!
//! impl ApplicationStore for CodeReviewStore {}
//!
//! #[derive(Clone, Debug, Eq, Hash, PartialEq)]
//! enum CodeReviewWindowId {
//! // A window that shows a code review.
//! Review(ReviewId),
//!
//! // A window that shows a user's open reviews.
//! User(UserId),
//! }
//!
//! impl ApplicationWindowId for CodeReviewWindowId {}
//!
//! #[derive(Clone, Debug, Eq, Hash, PartialEq)]
//! enum CodeReviewContentId {
//! // Different buffer used by the command bar.
//! Command(CommandType),
//!
//! // Buffer for a comment left on a line.
//! Review(ReviewId, usize),
//! }
//!
//! impl ApplicationContentId for CodeReviewContentId {}
//!
//! #[derive(Debug)]
//! enum CodeReviewError {
//! NoReview(ReviewId),
//! }
//!
//! impl fmt::Display for CodeReviewError {
//! fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
//! match self {
//! CodeReviewError::NoReview(id) => write!(f, "No review with ID {:?}", id),
//! }
//! }
//! }
//!
//! impl ApplicationError for CodeReviewError {}
//!
//! #[derive(Clone, Debug, Eq, PartialEq)]
//! enum CodeReviewInfo {}
//!
//! impl ApplicationInfo for CodeReviewInfo {
//! type Error = CodeReviewError;
//! type Action = CodeReviewAction;
//! type Store = CodeReviewStore;
//! type WindowId = CodeReviewWindowId;
//! type ContentId = CodeReviewContentId;
//!
//! fn content_of_command(ct: CommandType) -> CodeReviewContentId {
//! CodeReviewContentId::Command(ct)
//! }
//! }
//! ```
use std::fmt::{Debug, Display};
use std::hash::Hash;
use crate::{
editing::{context::EditContext, cursor::Cursor, rope::EditRope, store::Store},
keybindings::SequenceStatus,
prelude::CommandType,
};
/// Trait for objects that describe application-specific actions.
///
/// Implementors of this trait can be used with [Action::Application]. This can then be used to
/// create additional keybindings and commands on top of the defaults provided within
/// [modalkit::env](crate::env).
///
/// [Action::Application]: super::actions::Action::Application
pub trait ApplicationAction: Clone + Debug + Eq + PartialEq + Send {
/// Allows controlling how application-specific actions are included in
/// [RepeatType::EditSequence](crate::prelude::RepeatType::EditSequence).
fn is_edit_sequence(&self, ctx: &EditContext) -> SequenceStatus;
/// Allows controlling how application-specific actions are included in
/// [RepeatType::LastAction](crate::prelude::RepeatType::LastAction).
fn is_last_action(&self, ctx: &EditContext) -> SequenceStatus;
/// Allows controlling how application-specific actions are included in
/// [RepeatType::LastSelection](crate::prelude::RepeatType::LastSelection).
fn is_last_selection(&self, ctx: &EditContext) -> SequenceStatus;
/// Allows controlling whether an application-specific action can cause
/// a buffer switch on an
/// [EditError::WrongBuffer](crate::actions::EditError::WrongBuffer).
fn is_switchable(&self, ctx: &EditContext) -> bool;
}
impl ApplicationAction for () {
fn is_edit_sequence(&self, _: &EditContext) -> SequenceStatus {
SequenceStatus::Break
}
fn is_last_action(&self, _: &EditContext) -> SequenceStatus {
SequenceStatus::Ignore
}
fn is_last_selection(&self, _: &EditContext) -> SequenceStatus {
SequenceStatus::Ignore
}
fn is_switchable(&self, _: &EditContext) -> bool {
false
}
}
/// Trait for application-specific errors.
pub trait ApplicationError: Debug + Display {}
impl ApplicationError for String {}
/// Trait for objects that hold application-specific information.
///
/// Implementors of this trait can be embedded in [Store].
pub trait ApplicationStore {}
impl ApplicationStore for () {}
/// Trait for window identifiers in an application.
pub trait ApplicationWindowId: Clone + Debug + Eq + Hash + Send {}
impl ApplicationWindowId for () {}
impl ApplicationWindowId for usize {}
impl ApplicationWindowId for Option<usize> {}
impl ApplicationWindowId for String {}
/// Trait for identifiers of specific content within a window in an application.
pub trait ApplicationContentId: Clone + Debug + Eq + Hash + Send {}
impl ApplicationContentId for () {}
impl ApplicationContentId for usize {}
impl ApplicationContentId for Option<usize> {}
impl ApplicationContentId for String {}
/// Trait for objects that describe application-specific behaviour and types.
#[allow(unused)]
pub trait ApplicationInfo: Clone + Debug + Eq + PartialEq {
/// An application-specific error type.
type Error: ApplicationError;
/// The type for application-specific actions.
type Action: ApplicationAction;
/// The type for application-specific storage.
type Store: ApplicationStore;
/// The type for application-specific windows.
type WindowId: ApplicationWindowId;
/// The type for application-specific content within a window.
type ContentId: ApplicationContentId;
/// Given a [Cursor] position in an [EditRope], and its content identifier, generate a list of
/// completion candidates.
///
/// By default, this returns an empty list, which causes completion inside buffers to fall back
/// to word completion.
fn complete(
text: &EditRope,
cursor: &mut Cursor,
content: &Self::ContentId,
store: &mut Store<Self>,
) -> Vec<String> {
vec![]
}
/// Get the [ApplicationContentId] used to show a given command type.
fn content_of_command(cmdtype: CommandType) -> Self::ContentId;
}
/// A default implementor of [ApplicationInfo] for consumers that don't require any customization.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum EmptyInfo {}
impl ApplicationInfo for EmptyInfo {
type Error = String;
type Action = ();
type Store = ();
type WindowId = String;
type ContentId = String;
fn complete(
_: &EditRope,
_: &mut Cursor,
_: &Self::ContentId,
_: &mut Store<Self>,
) -> Vec<String> {
vec![]
}
fn content_of_command(cmdtype: CommandType) -> String {
match cmdtype {
CommandType::Search => "*search*".into(),
CommandType::Command => "*command*".into(),
}
}
}