2021-03-24 06:03:20 +01:00
|
|
|
use crate::{theme::Theme, tree::Tree, Document, DocumentId, View, ViewId};
|
2021-03-23 09:47:40 +01:00
|
|
|
use tui::layout::Rect;
|
2020-10-16 05:29:22 +02:00
|
|
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2021-03-24 06:03:20 +01:00
|
|
|
use slotmap::SlotMap;
|
2021-02-19 09:46:43 +01:00
|
|
|
|
2020-10-16 05:29:22 +02:00
|
|
|
use anyhow::Error;
|
|
|
|
|
2021-05-07 07:19:58 +02:00
|
|
|
pub use helix_core::diagnostic::Severity;
|
|
|
|
|
2020-10-16 05:29:22 +02:00
|
|
|
pub struct Editor {
|
2021-02-03 11:36:54 +01:00
|
|
|
pub tree: Tree,
|
2021-03-23 09:47:40 +01:00
|
|
|
pub documents: SlotMap<DocumentId, Document>,
|
2021-02-09 08:39:17 +01:00
|
|
|
pub count: Option<usize>,
|
2021-02-24 09:29:28 +01:00
|
|
|
pub theme: Theme,
|
2021-01-21 08:55:46 +01:00
|
|
|
pub language_servers: helix_lsp::Registry,
|
2021-05-07 07:19:58 +02:00
|
|
|
|
|
|
|
pub status_msg: Option<(String, Severity)>,
|
2020-10-16 05:29:22 +02:00
|
|
|
}
|
|
|
|
|
2021-03-31 16:42:16 +02:00
|
|
|
#[derive(Copy, Clone)]
|
2021-03-24 06:28:26 +01:00
|
|
|
pub enum Action {
|
|
|
|
Replace,
|
|
|
|
HorizontalSplit,
|
|
|
|
VerticalSplit,
|
|
|
|
}
|
|
|
|
|
2020-10-16 05:29:22 +02:00
|
|
|
impl Editor {
|
2021-05-06 06:56:34 +02:00
|
|
|
pub fn new(mut area: tui::layout::Rect) -> Self {
|
2021-04-07 16:56:20 +02:00
|
|
|
use helix_core::config_dir;
|
|
|
|
let config = std::fs::read(config_dir().join("theme.toml"));
|
|
|
|
// load $HOME/.config/helix/theme.toml, fallback to default config
|
|
|
|
let toml = config
|
|
|
|
.as_deref()
|
|
|
|
.unwrap_or(include_bytes!("../../theme.toml"));
|
2021-05-06 10:20:00 +02:00
|
|
|
let theme: Theme = toml::from_slice(toml).expect("failed to parse theme.toml");
|
2021-03-25 08:42:14 +01:00
|
|
|
|
2021-01-21 08:55:46 +01:00
|
|
|
let language_servers = helix_lsp::Registry::new();
|
2020-10-19 10:18:03 +02:00
|
|
|
|
2021-02-09 07:59:42 +01:00
|
|
|
// HAXX: offset the render area height by 1 to account for prompt/commandline
|
|
|
|
area.height -= 1;
|
|
|
|
|
2020-10-16 07:37:12 +02:00
|
|
|
Self {
|
2021-02-03 11:36:54 +01:00
|
|
|
tree: Tree::new(area),
|
2021-03-16 10:27:57 +01:00
|
|
|
documents: SlotMap::with_key(),
|
2021-02-09 08:39:17 +01:00
|
|
|
count: None,
|
2020-10-19 10:18:03 +02:00
|
|
|
theme,
|
2021-01-21 08:55:46 +01:00
|
|
|
language_servers,
|
2021-05-07 07:19:58 +02:00
|
|
|
status_msg: None,
|
2020-10-16 07:37:12 +02:00
|
|
|
}
|
2020-10-16 05:29:22 +02:00
|
|
|
}
|
|
|
|
|
2021-05-07 07:19:58 +02:00
|
|
|
pub fn set_status(&mut self, status: String) {
|
|
|
|
self.status_msg = Some((status, Severity::Info));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_error(&mut self, error: String) {
|
|
|
|
self.status_msg = Some((error, Severity::Error));
|
|
|
|
}
|
|
|
|
|
2021-03-23 09:47:40 +01:00
|
|
|
fn _refresh(&mut self) {
|
|
|
|
for (view, _) in self.tree.views_mut() {
|
|
|
|
let doc = &self.documents[view.doc];
|
|
|
|
view.ensure_cursor_in_view(doc)
|
|
|
|
}
|
|
|
|
}
|
2021-03-16 10:27:57 +01:00
|
|
|
|
2021-05-07 07:40:11 +02:00
|
|
|
pub fn switch(&mut self, id: DocumentId, action: Action) {
|
2021-03-24 06:28:26 +01:00
|
|
|
use crate::tree::Layout;
|
2021-03-31 10:17:01 +02:00
|
|
|
use helix_core::Selection;
|
2021-03-24 06:28:26 +01:00
|
|
|
match action {
|
|
|
|
Action::Replace => {
|
2021-03-24 10:01:26 +01:00
|
|
|
let view = self.view();
|
2021-03-31 10:17:01 +02:00
|
|
|
let jump = (
|
|
|
|
view.doc,
|
|
|
|
self.documents[view.doc].selection(view.id).clone(),
|
|
|
|
);
|
2021-03-24 10:01:26 +01:00
|
|
|
|
2021-03-24 08:56:29 +01:00
|
|
|
let view = self.view_mut();
|
2021-03-24 10:01:26 +01:00
|
|
|
view.jumps.push(jump);
|
2021-03-24 08:56:29 +01:00
|
|
|
view.doc = id;
|
|
|
|
view.first_line = 0;
|
2021-03-31 10:17:01 +02:00
|
|
|
let view_id = view.id;
|
|
|
|
|
|
|
|
// initialize selection for view
|
|
|
|
let doc = &mut self.documents[id];
|
|
|
|
doc.selections.insert(view_id, Selection::point(0));
|
|
|
|
|
2021-05-07 07:40:11 +02:00
|
|
|
return;
|
2021-03-24 06:28:26 +01:00
|
|
|
}
|
|
|
|
Action::HorizontalSplit => {
|
2021-05-07 07:40:11 +02:00
|
|
|
let view = View::new(id);
|
2021-03-31 10:17:01 +02:00
|
|
|
let view_id = self.tree.split(view, Layout::Horizontal);
|
|
|
|
// initialize selection for view
|
|
|
|
let doc = &mut self.documents[id];
|
|
|
|
doc.selections.insert(view_id, Selection::point(0));
|
2021-03-24 06:28:26 +01:00
|
|
|
}
|
|
|
|
Action::VerticalSplit => {
|
2021-05-07 07:40:11 +02:00
|
|
|
let view = View::new(id);
|
2021-03-31 10:17:01 +02:00
|
|
|
let view_id = self.tree.split(view, Layout::Vertical);
|
|
|
|
// initialize selection for view
|
|
|
|
let doc = &mut self.documents[id];
|
|
|
|
doc.selections.insert(view_id, Selection::point(0));
|
2021-03-24 06:28:26 +01:00
|
|
|
}
|
2021-01-21 08:55:46 +01:00
|
|
|
}
|
|
|
|
|
2021-03-23 09:47:40 +01:00
|
|
|
self._refresh();
|
2020-10-16 05:29:22 +02:00
|
|
|
}
|
2020-10-19 09:20:59 +02:00
|
|
|
|
2021-05-07 07:40:11 +02:00
|
|
|
pub fn new_file(&mut self, action: Action) -> DocumentId {
|
2021-04-06 12:02:22 +02:00
|
|
|
use helix_core::Rope;
|
|
|
|
let doc = Document::new(Rope::from("\n"));
|
|
|
|
let id = self.documents.insert(doc);
|
|
|
|
self.documents[id].id = id;
|
2021-05-07 07:40:11 +02:00
|
|
|
self.switch(id, action);
|
|
|
|
id
|
2021-04-06 12:02:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn open(&mut self, path: PathBuf, action: Action) -> Result<DocumentId, Error> {
|
2021-05-07 07:30:23 +02:00
|
|
|
let path = std::fs::canonicalize(path)?;
|
|
|
|
|
2021-04-06 12:02:22 +02:00
|
|
|
let id = self
|
|
|
|
.documents()
|
|
|
|
.find(|doc| doc.path() == Some(&path))
|
|
|
|
.map(|doc| doc.id);
|
|
|
|
|
|
|
|
let id = if let Some(id) = id {
|
|
|
|
id
|
|
|
|
} else {
|
|
|
|
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()
|
2021-05-06 06:56:34 +02:00
|
|
|
.and_then(|language| self.language_servers.get(language));
|
2021-04-06 12:02:22 +02:00
|
|
|
|
|
|
|
if let Some(language_server) = language_server {
|
|
|
|
doc.set_language_server(Some(language_server.clone()));
|
|
|
|
|
|
|
|
let language_id = doc
|
|
|
|
.language()
|
|
|
|
.and_then(|s| s.split('.').last()) // source.rust
|
|
|
|
.map(ToOwned::to_owned)
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
2021-05-06 08:08:59 +02:00
|
|
|
tokio::spawn(language_server.text_document_did_open(
|
2021-04-06 12:02:22 +02:00
|
|
|
doc.url().unwrap(),
|
|
|
|
doc.version(),
|
|
|
|
doc.text(),
|
|
|
|
language_id,
|
2021-05-06 08:08:59 +02:00
|
|
|
));
|
2021-04-06 12:02:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let id = self.documents.insert(doc);
|
|
|
|
self.documents[id].id = id;
|
|
|
|
id
|
|
|
|
};
|
|
|
|
|
2021-05-07 07:40:11 +02:00
|
|
|
self.switch(id, action);
|
|
|
|
Ok(id)
|
2021-04-06 12:02:22 +02:00
|
|
|
}
|
|
|
|
|
2021-05-07 07:52:47 +02:00
|
|
|
pub fn close(&mut self, id: ViewId, close_buffer: bool) {
|
2021-03-16 08:20:43 +01:00
|
|
|
let view = self.tree.get(self.tree.focus);
|
2021-05-07 07:52:47 +02:00
|
|
|
// remove selection
|
|
|
|
self.documents[view.doc].selections.remove(&id);
|
2021-03-16 08:20:43 +01:00
|
|
|
|
2021-05-07 07:52:47 +02:00
|
|
|
if close_buffer {
|
|
|
|
// get around borrowck issues
|
|
|
|
let language_servers = &mut self.language_servers;
|
|
|
|
let doc = &self.documents[view.doc];
|
2021-03-16 08:20:43 +01:00
|
|
|
|
2021-05-07 07:52:47 +02:00
|
|
|
let language_server = doc
|
|
|
|
.language
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|language| language_servers.get(language));
|
|
|
|
if let Some(language_server) = language_server {
|
|
|
|
tokio::spawn(language_server.text_document_did_close(doc.identifier()));
|
|
|
|
}
|
|
|
|
self.documents.remove(view.doc);
|
2021-03-16 08:20:43 +01:00
|
|
|
}
|
2021-03-16 10:27:57 +01:00
|
|
|
|
|
|
|
self.tree.remove(id);
|
2021-03-23 09:47:40 +01:00
|
|
|
self._refresh();
|
2021-03-16 10:27:57 +01:00
|
|
|
}
|
|
|
|
|
2021-03-23 09:47:40 +01:00
|
|
|
pub fn resize(&mut self, area: Rect) {
|
|
|
|
self.tree.resize(area);
|
|
|
|
self._refresh();
|
2021-03-16 10:27:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn focus_next(&mut self) {
|
|
|
|
self.tree.focus_next();
|
2021-02-19 09:46:43 +01:00
|
|
|
}
|
|
|
|
|
2021-03-18 06:48:42 +01:00
|
|
|
pub fn should_close(&self) -> bool {
|
2021-02-19 09:46:43 +01:00
|
|
|
self.tree.is_empty()
|
|
|
|
}
|
|
|
|
|
2021-04-01 03:39:46 +02:00
|
|
|
pub fn current(&mut self) -> (&mut View, &mut Document) {
|
|
|
|
let view = self.tree.get_mut(self.tree.focus);
|
|
|
|
let doc = &mut self.documents[view.doc];
|
|
|
|
(view, doc)
|
|
|
|
}
|
|
|
|
|
2021-01-21 08:55:46 +01:00
|
|
|
pub fn view(&self) -> &View {
|
2021-02-04 08:49:55 +01:00
|
|
|
self.tree.get(self.tree.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-04 08:49:55 +01:00
|
|
|
self.tree.get_mut(self.tree.focus)
|
2020-10-19 09:20:59 +02:00
|
|
|
}
|
2021-03-02 07:44:09 +01:00
|
|
|
|
2021-03-24 06:03:20 +01:00
|
|
|
pub fn ensure_cursor_in_view(&mut self, id: ViewId) {
|
2021-03-23 09:47:40 +01:00
|
|
|
let view = self.tree.get_mut(id);
|
|
|
|
let doc = &self.documents[view.doc];
|
|
|
|
view.ensure_cursor_in_view(doc)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn document(&self, id: DocumentId) -> Option<&Document> {
|
|
|
|
self.documents.get(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn documents(&self) -> impl Iterator<Item = &Document> {
|
|
|
|
self.documents.iter().map(|(_id, doc)| doc)
|
|
|
|
}
|
|
|
|
|
2021-03-29 08:22:43 +02:00
|
|
|
// pub fn current_document(&self) -> Document {
|
|
|
|
// let id = self.view().doc;
|
|
|
|
// let doc = &mut editor.documents[id];
|
|
|
|
// }
|
|
|
|
|
2021-03-02 07:44:09 +01:00
|
|
|
pub fn cursor_position(&self) -> Option<helix_core::Position> {
|
|
|
|
const OFFSET: u16 = 7; // 1 diagnostic + 5 linenr + 1 gutter
|
|
|
|
let view = self.view();
|
2021-03-23 09:47:40 +01:00
|
|
|
let doc = &self.documents[view.doc];
|
2021-03-31 10:17:01 +02:00
|
|
|
let cursor = doc.selection(view.id).cursor();
|
2021-03-23 09:47:40 +01:00
|
|
|
if let Some(mut pos) = view.screen_coords_at_pos(doc, doc.text().slice(..), cursor) {
|
2021-03-02 07:44:09 +01:00
|
|
|
pos.col += view.area.x as usize + OFFSET as usize;
|
|
|
|
pos.row += view.area.y as usize;
|
|
|
|
return Some(pos);
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
2020-10-16 05:29:22 +02:00
|
|
|
}
|