Unnamed repository; edit this file 'description' to name the repository.
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
use crossterm::event::Event;
use helix_core::Position;
use helix_view::{
    graphics::{CursorKind, Rect},
    Editor,
};
use tui::buffer::Buffer;

use crate::compositor::{Component, Context, EventResult};

/// Contains a component placed in the center of the parent component
pub struct Overlay<T> {
    /// Child component
    pub content: T,
    /// Function to compute the size and position of the child component
    pub calc_child_size: Box<dyn Fn(Rect) -> Rect>,
}

/// Surrounds the component with a margin of 5% on each side, and an additional 2 rows at the bottom
pub fn overlayed<T>(content: T) -> Overlay<T> {
    Overlay {
        content,
        calc_child_size: Box::new(|rect: Rect| clip_rect_relative(rect.clip_bottom(2), 90, 90)),
    }
}

fn clip_rect_relative(rect: Rect, percent_horizontal: u8, percent_vertical: u8) -> Rect {
    fn mul_and_cast(size: u16, factor: u8) -> u16 {
        ((size as u32) * (factor as u32) / 100).try_into().unwrap()
    }

    let inner_w = mul_and_cast(rect.width, percent_horizontal);
    let inner_h = mul_and_cast(rect.height, percent_vertical);

    let offset_x = rect.width.saturating_sub(inner_w) / 2;
    let offset_y = rect.height.saturating_sub(inner_h) / 2;

    Rect {
        x: rect.x + offset_x,
        y: rect.y + offset_y,
        width: inner_w,
        height: inner_h,
    }
}

impl<T: Component + 'static> Component for Overlay<T> {
    fn render(&mut self, area: Rect, frame: &mut Buffer, ctx: &mut Context) {
        let dimensions = (self.calc_child_size)(area);
        self.content.render(dimensions, frame, ctx)
    }

    fn required_size(&mut self, (width, height): (u16, u16)) -> Option<(u16, u16)> {
        let area = Rect {
            x: 0,
            y: 0,
            width,
            height,
        };
        let dimensions = (self.calc_child_size)(area);
        let viewport = (dimensions.width, dimensiouse ide_db::{
    assists::{AssistId, AssistKind},
    base_db::AnchoredPathBuf,
};
use syntax::{ast, AstNode, ToSmolStr};

use crate::{
    assist_context::{AssistContext, Assists},
    utils::trimmed_text_range,
};

// Assist: move_from_mod_rs
//
// Moves xxx/mod.rs to xxx.rs.
//
// ```
// //- /main.rs
// mod a;
// //- /a/mod.rs
// $0fn t() {}$0
// ```
// ->
// ```
// fn t() {}
// ```
pub(crate) fn move_from_mod_rs(acc: &mut Assists, ctx: &
ile_id(), dst); }, ) } #[cfg(test)] mod tests { use crate::tests::{check_assist, check_assist_not_applicable}; use super::*; #[test] fn trivial() { check_assist( move_from_mod_rs, r#" //- /main.rs mod a; //- /a/mod.rs $0fn t() {} $0"#, r#" //- /a.rs fn t() {} "#, ); } #[test] fn must_select_all_file() { cov_mark::check!(not_all_selected); check_assist_not_applicable( move_from_mod_rs, r#" //- /main.rs mod a; //- /a/mod.rs fn t() {}$0 "#, ); cov_mark::check!(not_all_selected); check_assist_not_applicable( move_from_mod_rs, r#" //- /main.rs mod a; //- /a/mod.rs $0fn$0 t() {} "#, ); } #[test] fn cannot_move_not_mod_rs() { cov_mark::check!(not_mod_rs); check_assist_not_applicable( move_from_mod_rs, r#"//- /main.rs mod a; //- /a.rs $0fn t() {}$0 "#, ); } #[test] fn cannot_downgrade_main_and_lib_rs() { check_assist_not_applicable( move_from_mod_rs, r#"//- /main.rs $0fn t() {}$0 "#, ); check_assist_not_applicable( move_from_mod_rs, r#"//- /lib.rs $0fn t() {}$0 "#, ); } }