Skip to main content

reovim_module_git_signs/
lib.rs

1#![cfg_attr(coverage_nightly, allow(unused_features))]
2#![cfg_attr(coverage_nightly, feature(coverage_attribute))]
3//! Git signs module for reovim.
4//!
5//! Provides gutter annotations for git diff hunks: additions, changes,
6//! and deletions. Also provides hunk navigation, stage, reset, preview,
7//! and diff commands (#668).
8
9use std::sync::Arc;
10
11use {
12    reovim_driver_annotation::{AnnotationSourceKey, AnnotationSourceRegistry},
13    reovim_driver_command::CommandHandlerStore,
14    reovim_driver_input::KeybindingStore,
15    reovim_kernel::api::v1::{
16        KeybindingRegistration, Module, ModuleContext, ModuleError, ModuleId, ProbeResult, Version,
17    },
18};
19
20pub mod commands;
21mod hunk;
22pub mod ids;
23mod keybinding;
24pub mod navigation;
25mod source;
26
27pub use {hunk::SignKind, source::GitSignsSource};
28
29/// Git signs module.
30///
31/// Registers a `GitSignsSource` into the `AnnotationSourceRegistry`
32/// so git diff hunks appear as gutter signs. Also registers hunk
33/// navigation and operation commands (#668).
34pub struct GitSignsModule;
35
36impl GitSignsModule {
37    /// Create a new instance.
38    #[must_use]
39    pub const fn new() -> Self {
40        Self
41    }
42}
43
44#[cfg_attr(coverage_nightly, coverage(off))]
45impl Default for GitSignsModule {
46    fn default() -> Self {
47        Self::new()
48    }
49}
50
51impl Module for GitSignsModule {
52    fn id(&self) -> ModuleId {
53        ids::MODULE
54    }
55
56    fn name(&self) -> &'static str {
57        "Git Signs"
58    }
59
60    fn version(&self) -> Version {
61        Version::new(0, 2, 0)
62    }
63
64    #[cfg_attr(coverage_nightly, coverage(off))]
65    fn init(&mut self, ctx: &ModuleContext) -> ProbeResult {
66        // Register annotation source for gutter signs
67        let source_registry = ctx.services.get_or_create::<AnnotationSourceRegistry>();
68        source_registry.register(
69            AnnotationSourceKey::new("git-signs"),
70            Arc::new(GitSignsSource::new(ctx.services.clone())),
71        );
72
73        // Register command handlers (#668)
74        let command_store = ctx.services.get_or_create::<CommandHandlerStore>();
75        for handler in commands::command_handlers() {
76            command_store.add(handler);
77        }
78
79        // Register keybindings (#668)
80        let keybinding_store = ctx.services.get_or_create::<KeybindingStore>();
81        keybinding_store.add_all(self.keybindings());
82
83        ProbeResult::Success
84    }
85
86    fn exit(&mut self) -> Result<(), ModuleError> {
87        Ok(())
88    }
89
90    #[cfg_attr(coverage_nightly, coverage(off))]
91    fn keybindings(&self) -> Vec<KeybindingRegistration> {
92        keybinding::all()
93    }
94}
95
96#[cfg(feature = "dynamic")]
97reovim_module_macros::declare_module!(GitSignsModule);
98
99#[cfg(test)]
100mod tests;