2021-06-19 13:26:52 +02:00
|
|
|
use crate::{
|
2021-06-25 05:58:15 +02:00
|
|
|
clipboard::{get_clipboard_provider, ClipboardProvider},
|
|
|
|
graphics::{CursorKind, Rect},
|
2021-06-19 13:26:52 +02:00
|
|
|
theme::{self, Theme},
|
|
|
|
tree::Tree,
|
2021-09-08 07:52:09 +02:00
|
|
|
Document, DocumentId, View, ViewId,
|
2021-06-19 13:26:52 +02:00
|
|
|
};
|
2020-10-16 05:29:22 +02:00
|
|
|
|
2021-06-19 05:14:40 +02:00
|
|
|
use futures_util::future;
|
2021-08-12 09:00:42 +02:00
|
|
|
use std::{
|
|
|
|
path::{Path, PathBuf},
|
2021-08-22 08:28:45 +02:00
|
|
|
pin::Pin,
|
2021-08-12 09:00:42 +02:00
|
|
|
sync::Arc,
|
|
|
|
};
|
2020-10-16 05:29:22 +02:00
|
|
|
|
2021-08-22 08:28:45 +02:00
|
|
|
use tokio::time::{sleep, Duration, Instant, Sleep};
|
|
|
|
|
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;
|
2021-06-15 05:26:05 +02:00
|
|
|
pub use helix_core::register::Registers;
|
2021-06-25 05:58:15 +02:00
|
|
|
use helix_core::syntax;
|
2021-06-23 08:03:34 +02:00
|
|
|
use helix_core::Position;
|
2021-05-07 07:19:58 +02:00
|
|
|
|
2021-08-08 07:07:14 +02:00
|
|
|
use serde::Deserialize;
|
|
|
|
|
2021-10-10 05:32:06 +02:00
|
|
|
fn deserialize_duration_millis<'de, D>(deserializer: D) -> Result<Duration, D::Error>
|
|
|
|
where
|
|
|
|
D: serde::Deserializer<'de>,
|
|
|
|
{
|
|
|
|
let millis = u64::deserialize(deserializer)?;
|
|
|
|
Ok(Duration::from_millis(millis))
|
|
|
|
}
|
|
|
|
|
2021-08-08 07:07:14 +02:00
|
|
|
#[derive(Debug, Clone, PartialEq, Deserialize)]
|
2021-08-10 07:35:20 +02:00
|
|
|
#[serde(rename_all = "kebab-case", default)]
|
2021-08-08 07:07:14 +02:00
|
|
|
pub struct Config {
|
|
|
|
/// Padding to keep between the edge of the screen and the cursor when scrolling. Defaults to 5.
|
|
|
|
pub scrolloff: usize,
|
2021-08-10 07:35:20 +02:00
|
|
|
/// Number of lines to scroll at once. Defaults to 3
|
|
|
|
pub scroll_lines: isize,
|
2021-08-08 07:07:14 +02:00
|
|
|
/// Mouse support. Defaults to true.
|
|
|
|
pub mouse: bool,
|
2021-08-31 11:13:16 +02:00
|
|
|
/// Shell to use for shell commands. Defaults to ["cmd", "/C"] on Windows and ["sh", "-c"] otherwise.
|
|
|
|
pub shell: Vec<String>,
|
2021-08-16 04:11:53 +02:00
|
|
|
/// Line number mode.
|
|
|
|
pub line_number: LineNumber,
|
2021-08-12 04:53:48 +02:00
|
|
|
/// Middle click paste support. Defaults to true
|
|
|
|
pub middle_click_paste: bool,
|
2021-09-20 06:45:07 +02:00
|
|
|
/// Smart case: Case insensitive searching unless pattern contains upper case characters. Defaults to true.
|
|
|
|
pub smart_case: bool,
|
2021-09-24 03:28:44 +02:00
|
|
|
/// Automatic insertion of pairs to parentheses, brackets, etc. Defaults to true.
|
|
|
|
pub auto_pairs: bool,
|
2021-10-10 05:32:06 +02:00
|
|
|
/// Time in milliseconds since last keypress before idle timers trigger. Used for autocompletion, set to 0 for instant. Defaults to 400ms.
|
|
|
|
#[serde(skip_serializing, deserialize_with = "deserialize_duration_millis")]
|
|
|
|
pub idle_timeout: Duration,
|
2021-08-08 07:07:14 +02:00
|
|
|
}
|
|
|
|
|
2021-08-16 04:11:53 +02:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
|
|
|
|
#[serde(rename_all = "kebab-case")]
|
|
|
|
pub enum LineNumber {
|
|
|
|
/// Show absolute line number
|
|
|
|
Absolute,
|
|
|
|
|
|
|
|
/// Show relative line number to the primary cursor
|
|
|
|
Relative,
|
|
|
|
}
|
|
|
|
|
2021-08-08 07:07:14 +02:00
|
|
|
impl Default for Config {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
scrolloff: 5,
|
2021-08-10 07:35:20 +02:00
|
|
|
scroll_lines: 3,
|
2021-08-08 07:07:14 +02:00
|
|
|
mouse: true,
|
2021-08-31 11:13:16 +02:00
|
|
|
shell: if cfg!(windows) {
|
|
|
|
vec!["cmd".to_owned(), "/C".to_owned()]
|
|
|
|
} else {
|
|
|
|
vec!["sh".to_owned(), "-c".to_owned()]
|
|
|
|
},
|
2021-08-16 04:11:53 +02:00
|
|
|
line_number: LineNumber::Absolute,
|
2021-08-12 04:53:48 +02:00
|
|
|
middle_click_paste: true,
|
2021-09-20 06:45:07 +02:00
|
|
|
smart_case: true,
|
2021-09-24 03:28:44 +02:00
|
|
|
auto_pairs: true,
|
2021-10-10 05:32:06 +02:00
|
|
|
idle_timeout: Duration::from_millis(400),
|
2021-08-08 07:07:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-07 16:34:19 +02:00
|
|
|
#[derive(Debug)]
|
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-06-08 05:24:27 +02:00
|
|
|
pub count: Option<std::num::NonZeroUsize>,
|
2021-09-08 07:52:09 +02:00
|
|
|
pub selected_register: Option<char>,
|
2021-06-15 05:26:05 +02:00
|
|
|
pub registers: Registers,
|
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-06-14 23:37:17 +02:00
|
|
|
pub clipboard_provider: Box<dyn ClipboardProvider>,
|
2021-05-07 07:19:58 +02:00
|
|
|
|
2021-06-19 13:26:52 +02:00
|
|
|
pub syn_loader: Arc<syntax::Loader>,
|
|
|
|
pub theme_loader: Arc<theme::Loader>,
|
2021-05-07 07:19:58 +02:00
|
|
|
|
|
|
|
pub status_msg: Option<(String, Severity)>,
|
2021-08-08 07:07:14 +02:00
|
|
|
|
|
|
|
pub config: Config,
|
2021-08-22 08:28:45 +02:00
|
|
|
|
|
|
|
pub idle_timer: Pin<Box<Sleep>>,
|
2020-10-16 05:29:22 +02:00
|
|
|
}
|
|
|
|
|
2021-06-07 16:34:19 +02:00
|
|
|
#[derive(Debug, Copy, Clone)]
|
2021-03-24 06:28:26 +01:00
|
|
|
pub enum Action {
|
2021-07-14 21:29:39 +02:00
|
|
|
Load,
|
2021-03-24 06:28:26 +01:00
|
|
|
Replace,
|
|
|
|
HorizontalSplit,
|
|
|
|
VerticalSplit,
|
|
|
|
}
|
|
|
|
|
2020-10-16 05:29:22 +02:00
|
|
|
impl Editor {
|
2021-06-19 13:26:52 +02:00
|
|
|
pub fn new(
|
2021-06-25 05:58:15 +02:00
|
|
|
mut area: Rect,
|
2021-06-19 13:26:52 +02:00
|
|
|
themes: Arc<theme::Loader>,
|
|
|
|
config_loader: Arc<syntax::Loader>,
|
2021-08-08 07:07:14 +02:00
|
|
|
config: Config,
|
2021-06-19 13:26:52 +02:00
|
|
|
) -> Self {
|
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,
|
2021-09-08 07:52:09 +02:00
|
|
|
selected_register: None,
|
2021-06-19 13:26:52 +02:00
|
|
|
theme: themes.default(),
|
2021-01-21 08:55:46 +01:00
|
|
|
language_servers,
|
2021-06-19 13:26:52 +02:00
|
|
|
syn_loader: config_loader,
|
|
|
|
theme_loader: themes,
|
2021-06-15 05:26:05 +02:00
|
|
|
registers: Registers::default(),
|
2021-06-14 23:37:17 +02:00
|
|
|
clipboard_provider: get_clipboard_provider(),
|
2021-05-07 07:19:58 +02:00
|
|
|
status_msg: None,
|
2021-08-22 08:28:45 +02:00
|
|
|
idle_timer: Box::pin(sleep(Duration::from_millis(500))),
|
2021-08-08 07:07:14 +02:00
|
|
|
config,
|
2020-10-16 07:37:12 +02:00
|
|
|
}
|
2020-10-16 05:29:22 +02:00
|
|
|
}
|
|
|
|
|
2021-08-22 08:28:45 +02:00
|
|
|
pub fn clear_idle_timer(&mut self) {
|
|
|
|
// equivalent to internal Instant::far_future() (30 years)
|
|
|
|
self.idle_timer
|
|
|
|
.as_mut()
|
|
|
|
.reset(Instant::now() + Duration::from_secs(86400 * 365 * 30));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn reset_idle_timer(&mut self) {
|
|
|
|
self.idle_timer
|
|
|
|
.as_mut()
|
|
|
|
.reset(Instant::now() + Duration::from_millis(500));
|
|
|
|
}
|
|
|
|
|
2021-06-11 05:42:16 +02:00
|
|
|
pub fn clear_status(&mut self) {
|
|
|
|
self.status_msg = None;
|
|
|
|
}
|
|
|
|
|
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-06-19 13:26:52 +02:00
|
|
|
pub fn set_theme(&mut self, theme: Theme) {
|
|
|
|
let scopes = theme.scopes();
|
|
|
|
for config in self
|
|
|
|
.syn_loader
|
|
|
|
.language_configs_iter()
|
|
|
|
.filter(|cfg| cfg.is_highlight_initialized())
|
|
|
|
{
|
2021-06-19 13:52:28 +02:00
|
|
|
config.reconfigure(scopes);
|
2021-06-19 13:26:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
self.theme = theme;
|
|
|
|
self._refresh();
|
|
|
|
}
|
|
|
|
|
2021-06-26 19:50:44 +02:00
|
|
|
pub fn set_theme_from_name(&mut self, theme: &str) -> anyhow::Result<()> {
|
2021-07-11 09:35:57 +02:00
|
|
|
use anyhow::Context;
|
|
|
|
let theme = self
|
|
|
|
.theme_loader
|
|
|
|
.load(theme.as_ref())
|
|
|
|
.with_context(|| format!("failed setting theme `{}`", theme))?;
|
2021-06-19 13:26:52 +02:00
|
|
|
self.set_theme(theme);
|
2021-06-26 19:50:44 +02:00
|
|
|
Ok(())
|
2021-06-19 13:26:52 +02:00
|
|
|
}
|
|
|
|
|
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];
|
2021-08-08 07:07:14 +02:00
|
|
|
view.ensure_cursor_in_view(doc, self.config.scrolloff)
|
2021-03-23 09:47:40 +01:00
|
|
|
}
|
|
|
|
}
|
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-06-12 14:21:06 +02:00
|
|
|
|
|
|
|
if !self.documents.contains_key(id) {
|
|
|
|
log::error!("cannot switch to document that does not exist (anymore)");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-24 06:28:26 +01:00
|
|
|
match action {
|
|
|
|
Action::Replace => {
|
2021-06-18 00:09:10 +02:00
|
|
|
let view = view!(self);
|
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-06-18 00:09:10 +02:00
|
|
|
let view = view_mut!(self);
|
2021-03-24 10:01:26 +01:00
|
|
|
view.jumps.push(jump);
|
2021-06-12 14:21:06 +02:00
|
|
|
view.last_accessed_doc = Some(view.doc);
|
2021-03-24 08:56:29 +01:00
|
|
|
view.doc = id;
|
2021-08-19 05:52:07 +02:00
|
|
|
view.offset = Position::default();
|
2021-05-17 09:36:13 +02:00
|
|
|
|
2021-06-18 00:09:10 +02:00
|
|
|
let (view, doc) = current!(self);
|
2021-03-31 10:17:01 +02:00
|
|
|
|
|
|
|
// initialize selection for view
|
2021-07-19 18:25:10 +02:00
|
|
|
doc.selections
|
2021-05-17 16:01:45 +02:00
|
|
|
.entry(view.id)
|
|
|
|
.or_insert_with(|| Selection::point(0));
|
2021-05-17 09:36:13 +02:00
|
|
|
// TODO: reuse align_view
|
2021-07-26 17:40:30 +02:00
|
|
|
let pos = doc
|
|
|
|
.selection(view.id)
|
|
|
|
.primary()
|
|
|
|
.cursor(doc.text().slice(..));
|
2021-05-17 09:36:13 +02:00
|
|
|
let line = doc.text().char_to_line(pos);
|
2021-08-19 06:19:15 +02:00
|
|
|
view.offset.row = line.saturating_sub(view.inner_area().height as usize / 2);
|
2021-03-31 10:17:01 +02:00
|
|
|
|
2021-05-07 07:40:11 +02:00
|
|
|
return;
|
2021-03-24 06:28:26 +01:00
|
|
|
}
|
2021-07-14 21:29:39 +02:00
|
|
|
Action::Load => {
|
|
|
|
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-06-23 08:03:34 +02:00
|
|
|
let doc = Document::default();
|
2021-04-06 12:02:22 +02:00
|
|
|
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-08-25 03:04:05 +02:00
|
|
|
let path = helix_core::path::get_canonicalized_path(&path)?;
|
2021-05-07 07:30:23 +02:00
|
|
|
|
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 {
|
2021-08-12 09:00:42 +02:00
|
|
|
let mut doc = Document::open(&path, None, Some(&self.theme), Some(&self.syn_loader))?;
|
2021-04-06 12:02:22 +02:00
|
|
|
|
|
|
|
// try to find a language server based on the language name
|
2021-09-15 07:58:06 +02:00
|
|
|
let language_server = doc.language.as_ref().and_then(|language| {
|
|
|
|
self.language_servers
|
|
|
|
.get(language)
|
|
|
|
.map_err(|e| {
|
|
|
|
log::error!("Failed to get LSP, {}, for `{}`", e, language.scope())
|
|
|
|
})
|
|
|
|
.ok()
|
|
|
|
});
|
2021-04-06 12:02:22 +02:00
|
|
|
|
|
|
|
if let Some(language_server) = language_server {
|
|
|
|
let language_id = doc
|
|
|
|
.language()
|
|
|
|
.and_then(|s| s.split('.').last()) // source.rust
|
|
|
|
.map(ToOwned::to_owned)
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
2021-09-02 06:55:08 +02:00
|
|
|
// TODO: this now races with on_init code if the init happens too quickly
|
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-09-02 06:55:08 +02:00
|
|
|
|
|
|
|
doc.set_language_server(Some(language_server));
|
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 doc = &self.documents[view.doc];
|
2021-03-16 08:20:43 +01:00
|
|
|
|
2021-09-01 07:38:47 +02:00
|
|
|
if let Some(language_server) = doc.language_server() {
|
2021-05-07 07:52:47 +02:00
|
|
|
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) {
|
2021-06-03 03:28:49 +02:00
|
|
|
if 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-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];
|
2021-08-08 07:07:14 +02:00
|
|
|
view.ensure_cursor_in_view(doc, self.config.scrolloff)
|
2021-03-23 09:47:40 +01:00
|
|
|
}
|
|
|
|
|
2021-09-02 05:52:32 +02:00
|
|
|
#[inline]
|
2021-03-23 09:47:40 +01:00
|
|
|
pub fn document(&self, id: DocumentId) -> Option<&Document> {
|
|
|
|
self.documents.get(id)
|
|
|
|
}
|
|
|
|
|
2021-09-02 05:52:32 +02:00
|
|
|
#[inline]
|
2021-06-23 20:35:39 +02:00
|
|
|
pub fn document_mut(&mut self, id: DocumentId) -> Option<&mut Document> {
|
|
|
|
self.documents.get_mut(id)
|
|
|
|
}
|
|
|
|
|
2021-09-02 05:52:32 +02:00
|
|
|
#[inline]
|
2021-03-23 09:47:40 +01:00
|
|
|
pub fn documents(&self) -> impl Iterator<Item = &Document> {
|
2021-09-02 05:52:32 +02:00
|
|
|
self.documents.values()
|
2021-03-23 09:47:40 +01:00
|
|
|
}
|
|
|
|
|
2021-09-02 05:52:32 +02:00
|
|
|
#[inline]
|
2021-06-19 13:26:52 +02:00
|
|
|
pub fn documents_mut(&mut self) -> impl Iterator<Item = &mut Document> {
|
2021-09-02 05:52:32 +02:00
|
|
|
self.documents.values_mut()
|
2021-06-19 13:26:52 +02:00
|
|
|
}
|
|
|
|
|
2021-08-12 09:00:42 +02:00
|
|
|
pub fn document_by_path<P: AsRef<Path>>(&self, path: P) -> Option<&Document> {
|
|
|
|
self.documents()
|
|
|
|
.find(|doc| doc.path().map(|p| p == path.as_ref()).unwrap_or(false))
|
|
|
|
}
|
|
|
|
|
2021-09-02 04:28:40 +02:00
|
|
|
pub fn document_by_path_mut<P: AsRef<Path>>(&mut self, path: P) -> Option<&mut Document> {
|
|
|
|
self.documents_mut()
|
|
|
|
.find(|doc| doc.path().map(|p| p == path.as_ref()).unwrap_or(false))
|
|
|
|
}
|
|
|
|
|
2021-06-15 07:03:56 +02:00
|
|
|
pub fn cursor(&self) -> (Option<Position>, CursorKind) {
|
2021-06-18 00:09:10 +02:00
|
|
|
let view = view!(self);
|
2021-03-23 09:47:40 +01:00
|
|
|
let doc = &self.documents[view.doc];
|
2021-07-26 17:40:30 +02:00
|
|
|
let cursor = doc
|
|
|
|
.selection(view.id)
|
|
|
|
.primary()
|
|
|
|
.cursor(doc.text().slice(..));
|
2021-03-23 09:47:40 +01:00
|
|
|
if let Some(mut pos) = view.screen_coords_at_pos(doc, doc.text().slice(..), cursor) {
|
2021-08-19 06:19:15 +02:00
|
|
|
let inner = view.inner_area();
|
|
|
|
pos.col += inner.x as usize;
|
|
|
|
pos.row += inner.y as usize;
|
2021-06-15 07:03:56 +02:00
|
|
|
(Some(pos), CursorKind::Hidden)
|
|
|
|
} else {
|
|
|
|
(None, CursorKind::Hidden)
|
2021-03-02 07:44:09 +01:00
|
|
|
}
|
|
|
|
}
|
2021-06-19 05:14:40 +02:00
|
|
|
|
|
|
|
/// Closes language servers with timeout. The default timeout is 500 ms, use
|
|
|
|
/// `timeout` parameter to override this.
|
|
|
|
pub async fn close_language_servers(
|
|
|
|
&self,
|
|
|
|
timeout: Option<u64>,
|
|
|
|
) -> Result<(), tokio::time::error::Elapsed> {
|
|
|
|
tokio::time::timeout(
|
|
|
|
Duration::from_millis(timeout.unwrap_or(500)),
|
|
|
|
future::join_all(
|
|
|
|
self.language_servers
|
|
|
|
.iter_clients()
|
|
|
|
.map(|client| client.force_shutdown()),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.map(|_| ())
|
|
|
|
}
|
2020-10-16 05:29:22 +02:00
|
|
|
}
|