A simple CPU rendered GUI IDE experience.
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
use std::iter::repeat;
use std::path::Path;
use std::process::Stdio;
use std::sync::Arc;

use Default::default;
use Into::into;
use anyhow::{anyhow, bail};
use dsb::Cell;
use dsb::cell::Style;
use lsp_types::*;
use rust_analyzer::lsp::ext::*;

use crate::FG;
use crate::edi::{Editor, lsp_m};
use crate::lsp::PathURI;
use crate::menu::{back, charc, filter, next, score};
use crate::text::{RopeExt, SortTedits, TextArea, col, color_};

macro_rules! commands {
    ($(#[doc = $d: literal] $t:tt $identifier: ident: $c:literal),+ $(,)?) => {
        #[derive(Copy, Clone, PartialEq, Eq)]
        pub enum Cmd {
            $(#[doc = $d] $identifier),+
        }
        impl Cmd {
            pub const ALL: [Cmd; { [$($c),+].len() }] = [$(Self::$identifier,)+];
            pub fn name(self) -> &'static str {
            match self {
                $(Self::$identifier => $c,)+
            }
            }
            pub fn desc(self) -> &'static str {
            match self {
                $(Self::$identifier => $d,)+
            }
            }
            pub fn needs_lsp(self) -> bool {
                match self {
                    $(Self::$identifier => stringify!($t) == "@",)+
                }
            }
        }
    };
}
commands!(
    /// move item at cursor down
    @ RAMoveID: "move-item-down",
    /// move item at cursor up
    @ RAMoveIU: "move-item-up",
    /// restart rust analyzer
    @ RARestart: "ra-restart",
    /// go to parent module
    @ RAParent: "parent",
    /// join lines under cursors.
    @ RAJoinLines: "join-lines",
    /// gets list of runnables
    @ RARunnables: "runnables",
    /// Open docs for type at cursor
    @ RADocs: "open-docs",
    /// Rebuilds rust-analyzer proc macros
    @ RARebuildProcMacros: "rebuild-proc-macros",
    /// Cancels current running rust-analyzer check process
    @ RACancelFlycheck: "cancel-flycheck",
    /// Opens Cargo.toml file for this workspace
    @ RAOpenCargoToml: "open-cargo-toml"
);

#[derive(Debug, Default)]
pub struct Commands {
    pub tedit: TextArea,
    pub selection: usize,
    pub vo: usize,
}

const N: usize = 30;
impl Commands {
    fn f(&self) -> String {
        self.tedit.rope.to_string()
    }
    pub fn next(mut self) -> Self {
        let n = filter_c(&self.f()).count();
        // coz its bottom up
        back::<N>(n, &mut self.selection, &mut self.vo);
        self
    }

    pub fn sel(&self) -> Cmd {
        let f = self.f();
        score_c(filter_c(&f), &f)[self.selection].1
    }

    pub fn back(mut self) -> Self {
        let n = filter_c(&self.f()).count();
        next::<N>(n, &mut self.selection, &mut self.vo);
        self
    }
    pub fn cells(&self, c: usize, ws: &Path) -> Vec<Cell> {
        let f = self.f();
        let mut out = vec![];
        let v = score_c(filter_c(&f), &f);
        let vlen = v.len();
        let i = v.into_iter().zip(0..vlen).skip(self.vo).take(N).rev();

        i.for_each(|((_, x, indices), i)| {
            r(x, ws, c, i == self.selection, &indices, &mut out)
        });

        out
    }
}
fn score_c(
    x: impl Iterator<Item = Cmd>,
    filter: &'_ str,
) -> Vec<(u32, Cmd, Vec<u32>)> {
    score(x, filter)
}

fn filter_c(f: &'_ str) -> impl Iterator<Item = Cmd> {
    filter(Cmd::ALL.into_iter(), f)
}
impl crate::menu::Key<'static> for Cmd {
    fn key(&self) -> impl Into<std::borrow::Cow<'static, str>> {
        self.name()
    }
}
#[implicit_fn::implicit_fn]
fn r(
    x: Cmd,
    _workspace: &Path,
    c: usize,
    selected: bool,
    indices: &[u32],
    to: &mut Vec<Cell>,
) {
    let bg = if selected { col!("#262d3b") } else { col!("#1c212b") };

    let ds: Style = Style::new(FG, bg);
    let d: Cell = Cell { letter: None, style: ds };
    let mut b = vec![d; c];
    let (bgt, col, ty) = (col!("#FFFFFF"), col!("#ACACAC"), "");
    b.iter_mut().zip(ty.chars()).for_each(|(x, c)| {
        *x = (Style::new(col, bgt) | Style::BOLD).basic(c)
    });
    let i = &mut b[..];
    let qualifier = x.desc().chars();
    let _left = i.len() as i32
        - (charc(&x.name()) as i32 + qualifier.clone().count() as i32)
        - 3;

    i.iter_mut()
        .zip(
            x.name()
                .chars()
                .chain([' '])
                .map(|x| ds.basic(x))
                .zip(0..)
                .chain(
                    qualifier
                        .map(|x| {
                            Style {
                                bg,
                                fg: color_("#858685"),
                                ..default()
                            }
                            .basic(x)
                        })
                        .zip(repeat(u32::MAX)),
                ),
        )
        .for_each(|(a, (b, i))| {
            *a = b;
            if indices.contains(&i) {
                a.style |= (Style::BOLD, color_("#ffcc66"));
            }
        });
    to.extend(b);
}

impl Editor {
    pub fn handle_command(
        &mut self,
        z: Cmd,
        w: Arc<winit::window::Window>,
    ) -> anyhow::Result<()> {
        if !z.needs_lsp() {
            return Ok(());
        }
        let Some((l, o)) = lsp_m!(self + p) else {
            bail!("no lsp");
        };

        match z {
            Cmd::RAMoveIU | Cmd::RAMoveID => {
                let r = self
                    .text
                    .to_l_position(*self.text.cursor.first())
                    .unwrap();
                let mut x  = l.request_immediate::<rust_analyzer::lsp::ext::MoveItem>(&MoveItemParams {
                            direction: if let Cmd::RAMoveIU = z { MoveItemDirection::Up } else { MoveItemDirection::Down },
                            text_document: o.tid(),
                            range: Range { start : r, end : r},
                        })?;

                x.sort_tedits();
                for t in x {
                    self.text.apply_snippet_tedit(&t)?;
                }
            }
            Cmd::RARestart => {
                _ = l.request::<ReloadWorkspace>(&())?.0;
            }
            Cmd::RAParent => {
                let Some(GotoDefinitionResponse::Link([ref x])) =
                    l.request_immediate::<ParentModule>(
                        &TextDocumentPositionParams {
                            text_document: o.tid(),
                            position: self
                                .text
                                .to_l_position(*self.text.cursor.first())
                                .unwrap(),
                        },
                    )?
                else {
                    self.bar.last_action = "no such parent".into();
                    return Ok(());
                };
                self.open_loclink(x, w);
            }
            Cmd::RAJoinLines => {
                let teds =
                    l.request_immediate::<JoinLines>(&JoinLinesParams {
                        ranges: self
                            .text
                            .cursor
                            .iter()
                            .filter_map(|x| {
                                self.text.to_l_range(
                                    x.sel.map(into).unwrap_or(*x..*x),
                                )
                            })
                            .collect(),
                        text_document: o.tid(),
                    })?;
                self.text
                    .apply_tedits(&mut { teds })
                    .map_err(|_| anyhow!("couldnt apply edits"))?;
            }
            Cmd::RADocs => {
                let u = l.request_immediate::<ExternalDocs>(
                    &TextDocumentPositionParams {
                        position: self
                            .text
                            .to_l_position(*self.text.cursor.first())
                            .unwrap(),
                        text_document: o.tid(),
                    },
                )?;
                use rust_analyzer::lsp::ext::ExternalDocsResponse::*;
                std::process::Command::new("firefox-nightly")
                    .arg(
                        match &u {
                            WithLocal(ExternalDocsPair {
                                web: Some(x),
                                ..
                            }) if let Some("doc.rust-lang.org") =
                                x.domain()
                                && let Some(x) =
                                    x.path().strip_prefix("/nightly/")
                                && option_env!("USER") == Some("os") =>
                                format!("http://127.0.0.1:3242/{x}"), // i have a lighttpd server running
                            WithLocal(ExternalDocsPair {
                                local: Some(url),
                                ..
                            }) if let Ok(p) = url.to_file_path()
                                && p.exists() =>
                                url.to_string(),
                            WithLocal(ExternalDocsPair {
                                web: Some(url),
                                ..
                            })
                            | Simple(Some(url)) => url.to_string(),
                            _ => return Ok(()),
                        }
                        .to_string(),
                    )
                    .stdout(Stdio::null())
                    .spawn()
                    .unwrap();
            }
            Cmd::RARebuildProcMacros => {
                _ = l.request::<RebuildProcMacros>(&())?;
            }
            Cmd::RACancelFlycheck => l.notify::<CancelFlycheck>(&())?,
            Cmd::RAOpenCargoToml => {
                let Some(GotoDefinitionResponse::Scalar(x)) =
                    &l.request_immediate::<OpenCargoToml>(
                        &OpenCargoTomlParams { text_document: o.tid() },
                    )?
                else {
                    bail!("wtf?");
                };
                self.open_loc(x, w);
            }
            _ => unimplemented!(),
        }

        Ok(())
    }
}