2020-10-19 10:18:03 +02:00
|
|
|
use crate::theme::Theme;
|
2021-02-03 11:36:54 +01:00
|
|
|
use crate::tree::Tree;
|
2020-10-22 07:35:07 +02:00
|
|
|
use crate::{Document, View};
|
2021-02-03 11:36:54 +01:00
|
|
|
use slotmap::DefaultKey as Key;
|
2020-10-16 05:29:22 +02:00
|
|
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
use anyhow::Error;
|
|
|
|
|
|
|
|
pub struct Editor {
|
2021-02-03 11:36:54 +01:00
|
|
|
pub tree: Tree,
|
|
|
|
// pub documents: Vec<Document>,
|
|
|
|
pub focus: Key,
|
2020-10-16 07:37:12 +02:00
|
|
|
pub should_close: bool,
|
2020-10-19 10:18:03 +02:00
|
|
|
pub theme: Theme, // TODO: share one instance
|
2021-01-21 08:55:46 +01:00
|
|
|
pub language_servers: helix_lsp::Registry,
|
2020-10-16 05:29:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Editor {
|
2021-02-03 11:36:54 +01:00
|
|
|
pub fn new(area: tui::layout::Rect) -> Self {
|
2020-10-19 10:18:03 +02:00
|
|
|
let theme = Theme::default();
|
2021-01-21 08:55:46 +01:00
|
|
|
let language_servers = helix_lsp::Registry::new();
|
2020-10-19 10:18:03 +02:00
|
|
|
|
2020-10-16 07:37:12 +02:00
|
|
|
Self {
|
2021-02-03 11:36:54 +01:00
|
|
|
tree: Tree::new(area),
|
|
|
|
focus: Key::default(),
|
2020-10-16 07:37:12 +02:00
|
|
|
should_close: false,
|
2020-10-19 10:18:03 +02:00
|
|
|
theme,
|
2021-01-21 08:55:46 +01:00
|
|
|
language_servers,
|
2020-10-16 07:37:12 +02:00
|
|
|
}
|
2020-10-16 05:29:22 +02:00
|
|
|
}
|
|
|
|
|
2021-02-03 11:36:54 +01:00
|
|
|
pub fn open(&mut self, path: PathBuf, executor: &smol::Executor) -> Result<(), Error> {
|
2021-01-21 08:55:46 +01:00
|
|
|
let mut doc = Document::load(path, self.theme.scopes())?;
|
|
|
|
|
|
|
|
// try to find a language server based on the language name
|
|
|
|
let language_server = doc
|
|
|
|
.language
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|language| self.language_servers.get(&language, &executor));
|
|
|
|
|
|
|
|
if let Some(language_server) = language_server {
|
|
|
|
// TODO: do this everywhere
|
|
|
|
doc.set_language_server(Some(language_server.clone()));
|
|
|
|
|
|
|
|
smol::block_on(language_server.text_document_did_open(
|
|
|
|
doc.url().unwrap(),
|
|
|
|
doc.version,
|
|
|
|
doc.text(),
|
|
|
|
))
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
2021-02-03 11:36:54 +01:00
|
|
|
let view = View::new(doc)?;
|
|
|
|
let pos = self.tree.insert(view);
|
2020-10-19 09:20:59 +02:00
|
|
|
self.focus = pos;
|
2020-10-16 05:29:22 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
2020-10-19 09:20:59 +02:00
|
|
|
|
2021-01-21 08:55:46 +01:00
|
|
|
pub fn view(&self) -> &View {
|
2021-02-03 11:36:54 +01:00
|
|
|
self.tree.get(self.focus)
|
2020-10-19 09:20:59 +02:00
|
|
|
}
|
|
|
|
|
2021-01-21 08:55:46 +01:00
|
|
|
pub fn view_mut(&mut self) -> &mut View {
|
2021-02-03 11:36:54 +01:00
|
|
|
self.tree.get_mut(self.focus)
|
2020-10-19 09:20:59 +02:00
|
|
|
}
|
2020-10-16 05:29:22 +02:00
|
|
|
}
|