lsp_core/feature/
rename.rs

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
use bevy_ecs::{
    prelude::*,
    schedule::{IntoSystemConfigs, ScheduleLabel},
};
use lsp_types::TextEdit;
use tracing::instrument;

use crate::prelude::*;
pub use crate::util::token::get_current_token;

/// [`Component`] indicating that the current document is currently handling a PrepareRename request.
#[derive(Component, Debug)]
pub struct PrepareRenameRequest {
    pub range: lsp_types::Range,
    pub placeholder: String,
}

/// [`ScheduleLabel`] related to the PrepareRename schedule
#[derive(ScheduleLabel, Clone, Eq, PartialEq, Debug, Hash)]
pub struct PrepareRename;

/// [`Component`] indicating that the current document is currently handling a Rename request,
/// collecting [TextEdits](`lsp_types::TextEdit`).
#[derive(Component, Debug)]
pub struct RenameEdits(pub Vec<(lsp_types::Url, lsp_types::TextEdit)>, pub String);

/// [`ScheduleLabel`] related to the Rename schedule
#[derive(ScheduleLabel, Clone, Eq, PartialEq, Debug, Hash)]
pub struct Rename;

pub fn setup_schedules(world: &mut World) {
    let mut prepare_rename_schedule = Schedule::new(PrepareRename);
    prepare_rename_schedule
        .add_systems((get_current_token, prepare_rename.after(get_current_token)));
    world.add_schedule(prepare_rename_schedule);

    let mut rename_schedule = Schedule::new(Rename);
    rename_schedule.add_systems((get_current_token, rename.after(get_current_token)));
    world.add_schedule(rename_schedule);
}

#[instrument(skip(query, commands,))]
pub fn prepare_rename(query: Query<(Entity, Option<&TokenComponent>)>, mut commands: Commands) {
    for (e, m_token) in &query {
        commands.entity(e).remove::<(PrepareRenameRequest,)>();
        if let Some(token) = m_token {
            let renameable = match token.token.value() {
                Token::Variable(_) => true,
                Token::IRIRef(_) => true,
                Token::PNameLN(_, _) => true,
                Token::BlankNodeLabel(_) => true,
                _ => false,
            };

            if renameable {
                commands.entity(e).insert(PrepareRenameRequest {
                    range: token.range.clone(),
                    placeholder: token.text.clone(),
                });
                continue;
            }
        }
        tracing::info!("Didn't find a good token");
    }
}

#[instrument(skip(query,))]
pub fn rename(mut query: Query<(&TokenComponent, &Tokens, &RopeC, &Label, &mut RenameEdits)>) {
    for (token, tokens, rope, label, mut edits) in &mut query {
        tracing::info!("Token {:?}", token);
        let new_text = edits.1.clone();
        for t in tokens.0.iter().filter(|x| x.value() == token.token.value()) {
            tracing::info!("Changing {:?}", t);
            if let Some(range) = range_to_range(t.span(), &rope.0) {
                edits.0.push((
                    label.0.clone(),
                    TextEdit {
                        range,
                        new_text: new_text.clone(),
                    },
                ))
            }
        }
        // commands.entity(e).insert(PrepareRenameRequest {
        //     range: token.range.clone(),
        //     placeholder: token.text.clone(),
        // });
    }
}