2021-06-19 13:26:52 +02:00
|
|
|
use crate::{
|
2021-06-25 05:58:15 +02:00
|
|
|
clipboard::{get_clipboard_provider, ClipboardProvider},
|
2021-11-15 17:46:39 +01:00
|
|
|
document::SCRATCH_BUFFER_NAME,
|
2021-06-25 05:58:15 +02:00
|
|
|
graphics::{CursorKind, Rect},
|
2021-12-12 13:16:48 +01:00
|
|
|
input::KeyEvent,
|
2021-06-19 13:26:52 +02:00
|
|
|
theme::{self, Theme},
|
2021-10-23 13:06:40 +02:00
|
|
|
tree::{self, 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::{
|
2021-11-04 05:43:45 +01:00
|
|
|
collections::BTreeMap,
|
2021-11-10 02:53:14 +01:00
|
|
|
io::stdin,
|
2021-11-25 03:07:23 +01:00
|
|
|
num::NonZeroUsize,
|
2021-08-12 09:00:42 +02:00
|
|
|
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-12-13 16:52:15 +01:00
|
|
|
use anyhow::{bail, Error};
|
2020-10-16 05:29:22 +02:00
|
|
|
|
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;
|
helix-term/commands: implement buffer-close (bc, bclose) (#1035)
* helix-view/view: impl method to remove document from jumps
* helix-view/editor: impl close_document
* helix-view/editor: remove close_buffer argument from `close`
According to archseer, this was never implemented or used properly. Now
that we have a proper "buffer close" function, we can get rid of this.
* helix-term/commands: implement buffer-close (bc, bclose)
This behaves the same as Kakoune's `delete-buffer` / `db` command:
* With 3 files opened by the user with `:o ab`, `:o cd`, and `:o ef`:
* `buffer-close` once closes `ef` and switches to `cd`
* `buffer-close` again closes `cd` and switches to `ab`
* `buffer-close` again closes `ab` and switches to a scratch buffer
* With 3 files opened from the command line with `hx -- ab cd ef`:
* `buffer-close` once closes `ab` and switches to `cd`
* `buffer-close` again closes `cd` and switches to `ef`
* `buffer-close` again closes `ef` and switches to a scratch buffer
* With 1 file opened (`ab`):
* `buffer-close` once closes `ab` and switches to a scratch buffer
* `buffer-close` again closes the scratch buffer and switches to a new
scratch buffer
* helix-term/commands: implement buffer-close! (bclose!, bc!)
Namely, if you have a document open in multiple splits, all the splits
will be closed at the same time, leaving only splits without that
document focused (or a scratch buffer if they were all focused on that
buffer).
* helix-view/tree: reset focus if Tree is empty
2021-11-15 16:30:45 +01:00
|
|
|
use helix_core::{Position, Selection};
|
2021-05-07 07:19:58 +02:00
|
|
|
|
2021-12-26 02:04:33 +01:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-08-08 07:07:14 +02:00
|
|
|
|
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-12-26 02:04:33 +01:00
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
2021-11-20 15:23:36 +01:00
|
|
|
#[serde(rename_all = "kebab-case", default, deny_unknown_fields)]
|
|
|
|
pub struct FilePickerConfig {
|
|
|
|
/// IgnoreOptions
|
|
|
|
/// Enables ignoring hidden files.
|
|
|
|
/// Whether to hide hidden files in file picker and global search results. Defaults to true.
|
|
|
|
pub hidden: bool,
|
|
|
|
/// Enables reading ignore files from parent directories. Defaults to true.
|
|
|
|
pub parents: bool,
|
|
|
|
/// Enables reading `.ignore` files.
|
|
|
|
/// Whether to hide files listed in .ignore in file picker and global search results. Defaults to true.
|
|
|
|
pub ignore: bool,
|
|
|
|
/// Enables reading `.gitignore` files.
|
|
|
|
/// Whether to hide files listed in .gitignore in file picker and global search results. Defaults to true.
|
|
|
|
pub git_ignore: bool,
|
|
|
|
/// Enables reading global .gitignore, whose path is specified in git's config: `core.excludefile` option.
|
|
|
|
/// Whether to hide files listed in global .gitignore in file picker and global search results. Defaults to true.
|
|
|
|
pub git_global: bool,
|
|
|
|
/// Enables reading `.git/info/exclude` files.
|
|
|
|
/// Whether to hide files listed in .git/info/exclude in file picker and global search results. Defaults to true.
|
|
|
|
pub git_exclude: bool,
|
|
|
|
/// WalkBuilder options
|
|
|
|
/// Maximum Depth to recurse directories in file picker and global search. Defaults to `None`.
|
|
|
|
pub max_depth: Option<usize>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for FilePickerConfig {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
hidden: true,
|
|
|
|
parents: true,
|
|
|
|
ignore: true,
|
|
|
|
git_ignore: true,
|
|
|
|
git_global: true,
|
|
|
|
git_exclude: true,
|
|
|
|
max_depth: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-26 02:04:33 +01:00
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
2021-11-06 16:57:14 +01:00
|
|
|
#[serde(rename_all = "kebab-case", default, deny_unknown_fields)]
|
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-10-16 15:57:41 +02:00
|
|
|
/// Middle click paste support. Defaults to true.
|
2021-08-12 04:53:48 +02:00
|
|
|
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-16 15:57:41 +02:00
|
|
|
/// Automatic auto-completion, automatically pop up without user trigger. Defaults to true.
|
|
|
|
pub auto_completion: 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-10-18 08:14:50 +02:00
|
|
|
pub completion_trigger_len: u8,
|
2021-11-05 03:25:08 +01:00
|
|
|
/// Whether to display infoboxes. Defaults to true.
|
|
|
|
pub auto_info: bool,
|
2021-11-20 15:23:36 +01:00
|
|
|
pub file_picker: FilePickerConfig,
|
2021-11-14 13:26:48 +01:00
|
|
|
/// Set to `true` to override automatic detection of terminal truecolor support in the event of a false negative. Defaults to `false`.
|
|
|
|
pub true_color: bool,
|
2021-08-08 07:07:14 +02:00
|
|
|
}
|
|
|
|
|
2021-12-26 02:04:33 +01:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
2021-08-16 04:11:53 +02:00
|
|
|
#[serde(rename_all = "kebab-case")]
|
|
|
|
pub enum LineNumber {
|
|
|
|
/// Show absolute line number
|
|
|
|
Absolute,
|
|
|
|
|
|
|
|
/// Show relative line number to the primary cursor
|
|
|
|
Relative,
|
|
|
|
}
|
|
|
|
|
2021-12-26 02:04:33 +01:00
|
|
|
impl std::str::FromStr for LineNumber {
|
|
|
|
type Err = anyhow::Error;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
match s.to_lowercase().as_str() {
|
|
|
|
"absolute" | "abs" => Ok(Self::Absolute),
|
|
|
|
"relative" | "rel" => Ok(Self::Relative),
|
|
|
|
_ => anyhow::bail!("Line number can only be `absolute` or `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-16 15:57:41 +02:00
|
|
|
auto_completion: true,
|
2021-10-10 05:32:06 +02:00
|
|
|
idle_timeout: Duration::from_millis(400),
|
2021-10-18 08:14:50 +02:00
|
|
|
completion_trigger_len: 2,
|
2021-11-05 03:25:08 +01:00
|
|
|
auto_info: true,
|
2021-11-20 15:23:36 +01:00
|
|
|
file_picker: FilePickerConfig::default(),
|
2021-11-14 13:26:48 +01:00
|
|
|
true_color: false,
|
2021-08-08 07:07:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-24 15:47:10 +02:00
|
|
|
pub struct Motion(pub Box<dyn Fn(&mut Editor)>);
|
|
|
|
impl Motion {
|
|
|
|
pub fn run(&self, e: &mut Editor) {
|
|
|
|
(self.0)(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl std::fmt::Debug for Motion {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
f.write_str("motion")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-11-25 03:07:23 +01:00
|
|
|
pub next_document_id: DocumentId,
|
2021-11-04 05:43:45 +01:00
|
|
|
pub documents: BTreeMap<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-12-12 13:16:48 +01:00
|
|
|
pub macro_recording: Option<(char, Vec<KeyEvent>)>,
|
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>>,
|
2021-10-24 15:47:10 +02:00
|
|
|
pub last_motion: Option<Motion>,
|
2021-11-15 05:06:12 +01:00
|
|
|
|
|
|
|
pub exit_code: i32,
|
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-11-26 10:26:22 +01:00
|
|
|
theme_loader: Arc<theme::Loader>,
|
|
|
|
syn_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-11-25 03:07:23 +01:00
|
|
|
next_document_id: DocumentId::default(),
|
2021-11-04 05:43:45 +01:00
|
|
|
documents: BTreeMap::new(),
|
2021-02-09 08:39:17 +01:00
|
|
|
count: None,
|
2021-09-08 07:52:09 +02:00
|
|
|
selected_register: None,
|
2021-12-12 13:16:48 +01:00
|
|
|
macro_recording: None,
|
2021-11-26 10:26:22 +01:00
|
|
|
theme: theme_loader.default(),
|
2021-01-21 08:55:46 +01:00
|
|
|
language_servers,
|
2021-11-26 10:26:22 +01:00
|
|
|
syn_loader,
|
|
|
|
theme_loader,
|
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-10-10 15:39:47 +02:00
|
|
|
idle_timer: Box::pin(sleep(config.idle_timeout)),
|
2021-10-24 15:47:10 +02:00
|
|
|
last_motion: None,
|
2021-08-08 07:07:14 +02:00
|
|
|
config,
|
2021-11-15 05:06:12 +01:00
|
|
|
exit_code: 0,
|
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()
|
2021-10-10 15:39:47 +02:00
|
|
|
.reset(Instant::now() + self.config.idle_timeout);
|
2021-08-22 08:28:45 +02:00
|
|
|
}
|
|
|
|
|
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) {
|
2021-11-06 16:57:14 +01:00
|
|
|
// `ui.selection` is the only scope required to be able to render a theme.
|
|
|
|
if theme.find_scope_index("ui.selection").is_none() {
|
|
|
|
self.set_error("Invalid theme: `ui.selection` required".to_owned());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-19 13:26:52 +02:00
|
|
|
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-11-28 02:19:54 +01:00
|
|
|
/// Refreshes the language server for a given document
|
|
|
|
pub fn refresh_language_server(&mut self, doc_id: DocumentId) -> Option<()> {
|
|
|
|
let doc = self.documents.get_mut(&doc_id)?;
|
|
|
|
doc.detect_language(Some(&self.theme), &self.syn_loader);
|
|
|
|
Self::launch_language_server(&mut self.language_servers, doc)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Launch a language server for a given document
|
|
|
|
fn launch_language_server(ls: &mut helix_lsp::Registry, doc: &mut Document) -> Option<()> {
|
|
|
|
// try to find a language server based on the language name
|
|
|
|
let language_server = doc.language.as_ref().and_then(|language| {
|
|
|
|
ls.get(language)
|
|
|
|
.map_err(|e| {
|
|
|
|
log::error!(
|
|
|
|
"Failed to initialize the LSP for `{}` {{ {} }}",
|
|
|
|
language.scope(),
|
|
|
|
e
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.ok()
|
|
|
|
});
|
|
|
|
if let Some(language_server) = language_server {
|
|
|
|
// only spawn a new lang server if the servers aren't the same
|
|
|
|
if Some(language_server.id()) != doc.language_server().map(|server| server.id()) {
|
|
|
|
if let Some(language_server) = doc.language_server() {
|
|
|
|
tokio::spawn(language_server.text_document_did_close(doc.identifier()));
|
|
|
|
}
|
|
|
|
let language_id = doc
|
|
|
|
.language()
|
|
|
|
.and_then(|s| s.split('.').last()) // source.rust
|
|
|
|
.map(ToOwned::to_owned)
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
|
|
|
// TODO: this now races with on_init code if the init happens too quickly
|
|
|
|
tokio::spawn(language_server.text_document_did_open(
|
|
|
|
doc.url().unwrap(),
|
|
|
|
doc.version(),
|
|
|
|
doc.text(),
|
|
|
|
language_id,
|
|
|
|
));
|
|
|
|
|
|
|
|
doc.set_language_server(Some(language_server));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(())
|
|
|
|
}
|
|
|
|
|
2021-03-23 09:47:40 +01:00
|
|
|
fn _refresh(&mut self) {
|
|
|
|
for (view, _) in self.tree.views_mut() {
|
2021-11-04 05:43:45 +01:00
|
|
|
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
|
|
|
|
helix-term/commands: implement buffer-close (bc, bclose) (#1035)
* helix-view/view: impl method to remove document from jumps
* helix-view/editor: impl close_document
* helix-view/editor: remove close_buffer argument from `close`
According to archseer, this was never implemented or used properly. Now
that we have a proper "buffer close" function, we can get rid of this.
* helix-term/commands: implement buffer-close (bc, bclose)
This behaves the same as Kakoune's `delete-buffer` / `db` command:
* With 3 files opened by the user with `:o ab`, `:o cd`, and `:o ef`:
* `buffer-close` once closes `ef` and switches to `cd`
* `buffer-close` again closes `cd` and switches to `ab`
* `buffer-close` again closes `ab` and switches to a scratch buffer
* With 3 files opened from the command line with `hx -- ab cd ef`:
* `buffer-close` once closes `ab` and switches to `cd`
* `buffer-close` again closes `cd` and switches to `ef`
* `buffer-close` again closes `ef` and switches to a scratch buffer
* With 1 file opened (`ab`):
* `buffer-close` once closes `ab` and switches to a scratch buffer
* `buffer-close` again closes the scratch buffer and switches to a new
scratch buffer
* helix-term/commands: implement buffer-close! (bclose!, bc!)
Namely, if you have a document open in multiple splits, all the splits
will be closed at the same time, leaving only splits without that
document focused (or a scratch buffer if they were all focused on that
buffer).
* helix-view/tree: reset focus if Tree is empty
2021-11-15 16:30:45 +01:00
|
|
|
fn replace_document_in_view(&mut self, current_view: ViewId, doc_id: DocumentId) {
|
|
|
|
let view = self.tree.get_mut(current_view);
|
|
|
|
view.doc = doc_id;
|
|
|
|
view.offset = Position::default();
|
|
|
|
|
|
|
|
let doc = self.documents.get_mut(&doc_id).unwrap();
|
|
|
|
|
|
|
|
// initialize selection for view
|
|
|
|
doc.selections
|
|
|
|
.entry(view.id)
|
|
|
|
.or_insert_with(|| Selection::point(0));
|
|
|
|
// TODO: reuse align_view
|
|
|
|
let pos = doc
|
|
|
|
.selection(view.id)
|
|
|
|
.primary()
|
|
|
|
.cursor(doc.text().slice(..));
|
|
|
|
let line = doc.text().char_to_line(pos);
|
|
|
|
view.offset.row = line.saturating_sub(view.inner_area().height as usize / 2);
|
|
|
|
}
|
|
|
|
|
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-06-12 14:21:06 +02:00
|
|
|
|
2021-11-04 05:43:45 +01:00
|
|
|
if !self.documents.contains_key(&id) {
|
2021-06-12 14:21:06 +02:00
|
|
|
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-10-31 02:42:49 +02:00
|
|
|
let (view, doc) = current_ref!(self);
|
|
|
|
// If the current view is an empty scratch buffer and is not displayed in any other views, delete it.
|
|
|
|
// Boolean value is determined before the call to `view_mut` because the operation requires a borrow
|
|
|
|
// of `self.tree`, which is mutably borrowed when `view_mut` is called.
|
|
|
|
let remove_empty_scratch = !doc.is_modified()
|
|
|
|
// If the buffer has no path and is not modified, it is an empty scratch buffer.
|
|
|
|
&& doc.path().is_none()
|
2021-11-04 09:57:04 +01:00
|
|
|
// If the buffer we are changing to is not this buffer
|
|
|
|
&& id != doc.id
|
2021-10-31 02:42:49 +02:00
|
|
|
// Ensure the buffer is not displayed in any other splits.
|
|
|
|
&& !self
|
|
|
|
.tree
|
|
|
|
.traverse()
|
|
|
|
.any(|(_, v)| v.doc == doc.id && v.id != view.id);
|
2021-12-02 05:46:57 +01:00
|
|
|
|
|
|
|
let (view, doc) = current!(self);
|
2021-10-31 02:42:49 +02:00
|
|
|
if remove_empty_scratch {
|
|
|
|
// Copy `doc.id` into a variable before calling `self.documents.remove`, which requires a mutable
|
|
|
|
// borrow, invalidating direct access to `doc.id`.
|
|
|
|
let id = doc.id;
|
2021-11-04 05:43:45 +01:00
|
|
|
self.documents.remove(&id);
|
2021-10-31 02:42:49 +02:00
|
|
|
} else {
|
|
|
|
let jump = (view.doc, doc.selection(view.id).clone());
|
|
|
|
view.jumps.push(jump);
|
2021-12-02 05:46:57 +01:00
|
|
|
// Set last accessed doc if it is a different document
|
|
|
|
if doc.id != id {
|
|
|
|
view.last_accessed_doc = Some(view.doc);
|
|
|
|
// Set last modified doc if modified and last modified doc is different
|
|
|
|
if std::mem::take(&mut doc.modified_since_accessed)
|
|
|
|
&& view.last_modified_docs[0] != Some(id)
|
|
|
|
{
|
|
|
|
view.last_modified_docs = [Some(view.doc), view.last_modified_docs[0]];
|
|
|
|
}
|
|
|
|
}
|
2021-10-31 02:42:49 +02:00
|
|
|
}
|
2021-05-17 09:36:13 +02:00
|
|
|
|
helix-term/commands: implement buffer-close (bc, bclose) (#1035)
* helix-view/view: impl method to remove document from jumps
* helix-view/editor: impl close_document
* helix-view/editor: remove close_buffer argument from `close`
According to archseer, this was never implemented or used properly. Now
that we have a proper "buffer close" function, we can get rid of this.
* helix-term/commands: implement buffer-close (bc, bclose)
This behaves the same as Kakoune's `delete-buffer` / `db` command:
* With 3 files opened by the user with `:o ab`, `:o cd`, and `:o ef`:
* `buffer-close` once closes `ef` and switches to `cd`
* `buffer-close` again closes `cd` and switches to `ab`
* `buffer-close` again closes `ab` and switches to a scratch buffer
* With 3 files opened from the command line with `hx -- ab cd ef`:
* `buffer-close` once closes `ab` and switches to `cd`
* `buffer-close` again closes `cd` and switches to `ef`
* `buffer-close` again closes `ef` and switches to a scratch buffer
* With 1 file opened (`ab`):
* `buffer-close` once closes `ab` and switches to a scratch buffer
* `buffer-close` again closes the scratch buffer and switches to a new
scratch buffer
* helix-term/commands: implement buffer-close! (bclose!, bc!)
Namely, if you have a document open in multiple splits, all the splits
will be closed at the same time, leaving only splits without that
document focused (or a scratch buffer if they were all focused on that
buffer).
* helix-view/tree: reset focus if Tree is empty
2021-11-15 16:30:45 +01:00
|
|
|
let view_id = view.id;
|
|
|
|
self.replace_document_in_view(view_id, id);
|
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 => {
|
2021-11-08 16:17:54 +01:00
|
|
|
let view_id = view!(self).id;
|
2021-11-26 10:26:22 +01:00
|
|
|
let doc = self.documents.get_mut(&id).unwrap();
|
|
|
|
if doc.selections().is_empty() {
|
|
|
|
doc.selections.insert(view_id, Selection::point(0));
|
2021-11-08 16:17:54 +01:00
|
|
|
}
|
2021-07-14 21:29:39 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-11-26 10:26:22 +01:00
|
|
|
Action::HorizontalSplit | Action::VerticalSplit => {
|
2021-05-07 07:40:11 +02:00
|
|
|
let view = View::new(id);
|
2021-11-26 10:26:22 +01:00
|
|
|
let view_id = self.tree.split(
|
|
|
|
view,
|
|
|
|
match action {
|
|
|
|
Action::HorizontalSplit => Layout::Horizontal,
|
|
|
|
Action::VerticalSplit => Layout::Vertical,
|
|
|
|
_ => unreachable!(),
|
|
|
|
},
|
|
|
|
);
|
2021-03-31 10:17:01 +02:00
|
|
|
// initialize selection for view
|
2021-11-04 05:43:45 +01:00
|
|
|
let doc = self.documents.get_mut(&id).unwrap();
|
2021-03-31 10:17:01 +02:00
|
|
|
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-11-25 03:07:23 +01:00
|
|
|
/// Generate an id for a new document and register it.
|
|
|
|
fn new_document(&mut self, mut doc: Document) -> DocumentId {
|
|
|
|
let id = self.next_document_id;
|
|
|
|
// Safety: adding 1 from 1 is fine, probably impossible to reach usize max
|
|
|
|
self.next_document_id =
|
|
|
|
DocumentId(unsafe { NonZeroUsize::new_unchecked(self.next_document_id.0.get() + 1) });
|
|
|
|
doc.id = id;
|
|
|
|
self.documents.insert(id, doc);
|
helix-term/commands: implement buffer-close (bc, bclose) (#1035)
* helix-view/view: impl method to remove document from jumps
* helix-view/editor: impl close_document
* helix-view/editor: remove close_buffer argument from `close`
According to archseer, this was never implemented or used properly. Now
that we have a proper "buffer close" function, we can get rid of this.
* helix-term/commands: implement buffer-close (bc, bclose)
This behaves the same as Kakoune's `delete-buffer` / `db` command:
* With 3 files opened by the user with `:o ab`, `:o cd`, and `:o ef`:
* `buffer-close` once closes `ef` and switches to `cd`
* `buffer-close` again closes `cd` and switches to `ab`
* `buffer-close` again closes `ab` and switches to a scratch buffer
* With 3 files opened from the command line with `hx -- ab cd ef`:
* `buffer-close` once closes `ab` and switches to `cd`
* `buffer-close` again closes `cd` and switches to `ef`
* `buffer-close` again closes `ef` and switches to a scratch buffer
* With 1 file opened (`ab`):
* `buffer-close` once closes `ab` and switches to a scratch buffer
* `buffer-close` again closes the scratch buffer and switches to a new
scratch buffer
* helix-term/commands: implement buffer-close! (bclose!, bc!)
Namely, if you have a document open in multiple splits, all the splits
will be closed at the same time, leaving only splits without that
document focused (or a scratch buffer if they were all focused on that
buffer).
* helix-view/tree: reset focus if Tree is empty
2021-11-15 16:30:45 +01:00
|
|
|
id
|
|
|
|
}
|
|
|
|
|
2021-11-25 03:07:23 +01:00
|
|
|
fn new_file_from_document(&mut self, action: Action, doc: Document) -> DocumentId {
|
|
|
|
let id = self.new_document(doc);
|
2021-05-07 07:40:11 +02:00
|
|
|
self.switch(id, action);
|
|
|
|
id
|
2021-04-06 12:02:22 +02:00
|
|
|
}
|
|
|
|
|
2021-11-10 02:53:14 +01:00
|
|
|
pub fn new_file(&mut self, action: Action) -> DocumentId {
|
|
|
|
self.new_file_from_document(action, Document::default())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_file_from_stdin(&mut self, action: Action) -> Result<DocumentId, Error> {
|
|
|
|
let (rope, encoding) = crate::document::from_reader(&mut stdin(), None)?;
|
|
|
|
Ok(self.new_file_from_document(action, Document::from(rope, Some(encoding))))
|
|
|
|
}
|
|
|
|
|
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-11-26 10:26:22 +01:00
|
|
|
let id = self.document_by_path(&path).map(|doc| doc.id);
|
2021-04-06 12:02:22 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2021-11-28 02:19:54 +01:00
|
|
|
let _ = Self::launch_language_server(&mut self.language_servers, &mut doc);
|
2021-04-06 12:02:22 +02:00
|
|
|
|
2021-11-25 03:07:23 +01:00
|
|
|
self.new_document(doc)
|
2021-04-06 12:02:22 +02:00
|
|
|
};
|
|
|
|
|
2021-05-07 07:40:11 +02:00
|
|
|
self.switch(id, action);
|
|
|
|
Ok(id)
|
2021-04-06 12:02:22 +02:00
|
|
|
}
|
|
|
|
|
helix-term/commands: implement buffer-close (bc, bclose) (#1035)
* helix-view/view: impl method to remove document from jumps
* helix-view/editor: impl close_document
* helix-view/editor: remove close_buffer argument from `close`
According to archseer, this was never implemented or used properly. Now
that we have a proper "buffer close" function, we can get rid of this.
* helix-term/commands: implement buffer-close (bc, bclose)
This behaves the same as Kakoune's `delete-buffer` / `db` command:
* With 3 files opened by the user with `:o ab`, `:o cd`, and `:o ef`:
* `buffer-close` once closes `ef` and switches to `cd`
* `buffer-close` again closes `cd` and switches to `ab`
* `buffer-close` again closes `ab` and switches to a scratch buffer
* With 3 files opened from the command line with `hx -- ab cd ef`:
* `buffer-close` once closes `ab` and switches to `cd`
* `buffer-close` again closes `cd` and switches to `ef`
* `buffer-close` again closes `ef` and switches to a scratch buffer
* With 1 file opened (`ab`):
* `buffer-close` once closes `ab` and switches to a scratch buffer
* `buffer-close` again closes the scratch buffer and switches to a new
scratch buffer
* helix-term/commands: implement buffer-close! (bclose!, bc!)
Namely, if you have a document open in multiple splits, all the splits
will be closed at the same time, leaving only splits without that
document focused (or a scratch buffer if they were all focused on that
buffer).
* helix-view/tree: reset focus if Tree is empty
2021-11-15 16:30:45 +01:00
|
|
|
pub fn close(&mut self, id: ViewId) {
|
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
|
2021-11-04 05:43:45 +01:00
|
|
|
self.documents
|
|
|
|
.get_mut(&view.doc)
|
|
|
|
.unwrap()
|
|
|
|
.selections
|
|
|
|
.remove(&id);
|
2021-03-16 08:20:43 +01:00
|
|
|
|
helix-term/commands: implement buffer-close (bc, bclose) (#1035)
* helix-view/view: impl method to remove document from jumps
* helix-view/editor: impl close_document
* helix-view/editor: remove close_buffer argument from `close`
According to archseer, this was never implemented or used properly. Now
that we have a proper "buffer close" function, we can get rid of this.
* helix-term/commands: implement buffer-close (bc, bclose)
This behaves the same as Kakoune's `delete-buffer` / `db` command:
* With 3 files opened by the user with `:o ab`, `:o cd`, and `:o ef`:
* `buffer-close` once closes `ef` and switches to `cd`
* `buffer-close` again closes `cd` and switches to `ab`
* `buffer-close` again closes `ab` and switches to a scratch buffer
* With 3 files opened from the command line with `hx -- ab cd ef`:
* `buffer-close` once closes `ab` and switches to `cd`
* `buffer-close` again closes `cd` and switches to `ef`
* `buffer-close` again closes `ef` and switches to a scratch buffer
* With 1 file opened (`ab`):
* `buffer-close` once closes `ab` and switches to a scratch buffer
* `buffer-close` again closes the scratch buffer and switches to a new
scratch buffer
* helix-term/commands: implement buffer-close! (bclose!, bc!)
Namely, if you have a document open in multiple splits, all the splits
will be closed at the same time, leaving only splits without that
document focused (or a scratch buffer if they were all focused on that
buffer).
* helix-view/tree: reset focus if Tree is empty
2021-11-15 16:30:45 +01:00
|
|
|
self.tree.remove(id);
|
|
|
|
self._refresh();
|
|
|
|
}
|
2021-03-16 08:20:43 +01:00
|
|
|
|
helix-term/commands: implement buffer-close (bc, bclose) (#1035)
* helix-view/view: impl method to remove document from jumps
* helix-view/editor: impl close_document
* helix-view/editor: remove close_buffer argument from `close`
According to archseer, this was never implemented or used properly. Now
that we have a proper "buffer close" function, we can get rid of this.
* helix-term/commands: implement buffer-close (bc, bclose)
This behaves the same as Kakoune's `delete-buffer` / `db` command:
* With 3 files opened by the user with `:o ab`, `:o cd`, and `:o ef`:
* `buffer-close` once closes `ef` and switches to `cd`
* `buffer-close` again closes `cd` and switches to `ab`
* `buffer-close` again closes `ab` and switches to a scratch buffer
* With 3 files opened from the command line with `hx -- ab cd ef`:
* `buffer-close` once closes `ab` and switches to `cd`
* `buffer-close` again closes `cd` and switches to `ef`
* `buffer-close` again closes `ef` and switches to a scratch buffer
* With 1 file opened (`ab`):
* `buffer-close` once closes `ab` and switches to a scratch buffer
* `buffer-close` again closes the scratch buffer and switches to a new
scratch buffer
* helix-term/commands: implement buffer-close! (bclose!, bc!)
Namely, if you have a document open in multiple splits, all the splits
will be closed at the same time, leaving only splits without that
document focused (or a scratch buffer if they were all focused on that
buffer).
* helix-view/tree: reset focus if Tree is empty
2021-11-15 16:30:45 +01:00
|
|
|
pub fn close_document(&mut self, doc_id: DocumentId, force: bool) -> anyhow::Result<()> {
|
|
|
|
let doc = match self.documents.get(&doc_id) {
|
|
|
|
Some(doc) => doc,
|
2021-11-26 10:26:22 +01:00
|
|
|
None => bail!("document does not exist"),
|
helix-term/commands: implement buffer-close (bc, bclose) (#1035)
* helix-view/view: impl method to remove document from jumps
* helix-view/editor: impl close_document
* helix-view/editor: remove close_buffer argument from `close`
According to archseer, this was never implemented or used properly. Now
that we have a proper "buffer close" function, we can get rid of this.
* helix-term/commands: implement buffer-close (bc, bclose)
This behaves the same as Kakoune's `delete-buffer` / `db` command:
* With 3 files opened by the user with `:o ab`, `:o cd`, and `:o ef`:
* `buffer-close` once closes `ef` and switches to `cd`
* `buffer-close` again closes `cd` and switches to `ab`
* `buffer-close` again closes `ab` and switches to a scratch buffer
* With 3 files opened from the command line with `hx -- ab cd ef`:
* `buffer-close` once closes `ab` and switches to `cd`
* `buffer-close` again closes `cd` and switches to `ef`
* `buffer-close` again closes `ef` and switches to a scratch buffer
* With 1 file opened (`ab`):
* `buffer-close` once closes `ab` and switches to a scratch buffer
* `buffer-close` again closes the scratch buffer and switches to a new
scratch buffer
* helix-term/commands: implement buffer-close! (bclose!, bc!)
Namely, if you have a document open in multiple splits, all the splits
will be closed at the same time, leaving only splits without that
document focused (or a scratch buffer if they were all focused on that
buffer).
* helix-view/tree: reset focus if Tree is empty
2021-11-15 16:30:45 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if !force && doc.is_modified() {
|
2021-11-26 10:26:22 +01:00
|
|
|
bail!(
|
helix-term/commands: implement buffer-close (bc, bclose) (#1035)
* helix-view/view: impl method to remove document from jumps
* helix-view/editor: impl close_document
* helix-view/editor: remove close_buffer argument from `close`
According to archseer, this was never implemented or used properly. Now
that we have a proper "buffer close" function, we can get rid of this.
* helix-term/commands: implement buffer-close (bc, bclose)
This behaves the same as Kakoune's `delete-buffer` / `db` command:
* With 3 files opened by the user with `:o ab`, `:o cd`, and `:o ef`:
* `buffer-close` once closes `ef` and switches to `cd`
* `buffer-close` again closes `cd` and switches to `ab`
* `buffer-close` again closes `ab` and switches to a scratch buffer
* With 3 files opened from the command line with `hx -- ab cd ef`:
* `buffer-close` once closes `ab` and switches to `cd`
* `buffer-close` again closes `cd` and switches to `ef`
* `buffer-close` again closes `ef` and switches to a scratch buffer
* With 1 file opened (`ab`):
* `buffer-close` once closes `ab` and switches to a scratch buffer
* `buffer-close` again closes the scratch buffer and switches to a new
scratch buffer
* helix-term/commands: implement buffer-close! (bclose!, bc!)
Namely, if you have a document open in multiple splits, all the splits
will be closed at the same time, leaving only splits without that
document focused (or a scratch buffer if they were all focused on that
buffer).
* helix-view/tree: reset focus if Tree is empty
2021-11-15 16:30:45 +01:00
|
|
|
"buffer {:?} is modified",
|
|
|
|
doc.relative_path()
|
|
|
|
.map(|path| path.to_string_lossy().to_string())
|
2021-11-15 17:46:39 +01:00
|
|
|
.unwrap_or_else(|| SCRATCH_BUFFER_NAME.into())
|
helix-term/commands: implement buffer-close (bc, bclose) (#1035)
* helix-view/view: impl method to remove document from jumps
* helix-view/editor: impl close_document
* helix-view/editor: remove close_buffer argument from `close`
According to archseer, this was never implemented or used properly. Now
that we have a proper "buffer close" function, we can get rid of this.
* helix-term/commands: implement buffer-close (bc, bclose)
This behaves the same as Kakoune's `delete-buffer` / `db` command:
* With 3 files opened by the user with `:o ab`, `:o cd`, and `:o ef`:
* `buffer-close` once closes `ef` and switches to `cd`
* `buffer-close` again closes `cd` and switches to `ab`
* `buffer-close` again closes `ab` and switches to a scratch buffer
* With 3 files opened from the command line with `hx -- ab cd ef`:
* `buffer-close` once closes `ab` and switches to `cd`
* `buffer-close` again closes `cd` and switches to `ef`
* `buffer-close` again closes `ef` and switches to a scratch buffer
* With 1 file opened (`ab`):
* `buffer-close` once closes `ab` and switches to a scratch buffer
* `buffer-close` again closes the scratch buffer and switches to a new
scratch buffer
* helix-term/commands: implement buffer-close! (bclose!, bc!)
Namely, if you have a document open in multiple splits, all the splits
will be closed at the same time, leaving only splits without that
document focused (or a scratch buffer if they were all focused on that
buffer).
* helix-view/tree: reset focus if Tree is empty
2021-11-15 16:30:45 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(language_server) = doc.language_server() {
|
|
|
|
tokio::spawn(language_server.text_document_did_close(doc.identifier()));
|
|
|
|
}
|
|
|
|
|
|
|
|
let views_to_close = self
|
|
|
|
.tree
|
|
|
|
.views()
|
|
|
|
.filter_map(|(view, _focus)| {
|
|
|
|
if view.doc == doc_id {
|
|
|
|
Some(view.id)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
for view_id in views_to_close {
|
|
|
|
self.close(view_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.documents.remove(&doc_id);
|
|
|
|
|
|
|
|
// If the document we removed was visible in all views, we will have no more views. We don't
|
|
|
|
// want to close the editor just for a simple buffer close, so we need to create a new view
|
|
|
|
// containing either an existing document, or a brand new document.
|
2021-11-26 10:26:22 +01:00
|
|
|
if self.tree.views().next().is_none() {
|
helix-term/commands: implement buffer-close (bc, bclose) (#1035)
* helix-view/view: impl method to remove document from jumps
* helix-view/editor: impl close_document
* helix-view/editor: remove close_buffer argument from `close`
According to archseer, this was never implemented or used properly. Now
that we have a proper "buffer close" function, we can get rid of this.
* helix-term/commands: implement buffer-close (bc, bclose)
This behaves the same as Kakoune's `delete-buffer` / `db` command:
* With 3 files opened by the user with `:o ab`, `:o cd`, and `:o ef`:
* `buffer-close` once closes `ef` and switches to `cd`
* `buffer-close` again closes `cd` and switches to `ab`
* `buffer-close` again closes `ab` and switches to a scratch buffer
* With 3 files opened from the command line with `hx -- ab cd ef`:
* `buffer-close` once closes `ab` and switches to `cd`
* `buffer-close` again closes `cd` and switches to `ef`
* `buffer-close` again closes `ef` and switches to a scratch buffer
* With 1 file opened (`ab`):
* `buffer-close` once closes `ab` and switches to a scratch buffer
* `buffer-close` again closes the scratch buffer and switches to a new
scratch buffer
* helix-term/commands: implement buffer-close! (bclose!, bc!)
Namely, if you have a document open in multiple splits, all the splits
will be closed at the same time, leaving only splits without that
document focused (or a scratch buffer if they were all focused on that
buffer).
* helix-view/tree: reset focus if Tree is empty
2021-11-15 16:30:45 +01:00
|
|
|
let doc_id = self
|
|
|
|
.documents
|
|
|
|
.iter()
|
|
|
|
.map(|(&doc_id, _)| doc_id)
|
|
|
|
.next()
|
|
|
|
.unwrap_or_else(|| self.new_document(Document::default()));
|
|
|
|
let view = View::new(doc_id);
|
|
|
|
let view_id = self.tree.insert(view);
|
|
|
|
let doc = self.documents.get_mut(&doc_id).unwrap();
|
|
|
|
doc.selections.insert(view_id, Selection::point(0));
|
2021-03-16 08:20:43 +01:00
|
|
|
}
|
2021-03-16 10:27:57 +01:00
|
|
|
|
2021-03-23 09:47:40 +01:00
|
|
|
self._refresh();
|
helix-term/commands: implement buffer-close (bc, bclose) (#1035)
* helix-view/view: impl method to remove document from jumps
* helix-view/editor: impl close_document
* helix-view/editor: remove close_buffer argument from `close`
According to archseer, this was never implemented or used properly. Now
that we have a proper "buffer close" function, we can get rid of this.
* helix-term/commands: implement buffer-close (bc, bclose)
This behaves the same as Kakoune's `delete-buffer` / `db` command:
* With 3 files opened by the user with `:o ab`, `:o cd`, and `:o ef`:
* `buffer-close` once closes `ef` and switches to `cd`
* `buffer-close` again closes `cd` and switches to `ab`
* `buffer-close` again closes `ab` and switches to a scratch buffer
* With 3 files opened from the command line with `hx -- ab cd ef`:
* `buffer-close` once closes `ab` and switches to `cd`
* `buffer-close` again closes `cd` and switches to `ef`
* `buffer-close` again closes `ef` and switches to a scratch buffer
* With 1 file opened (`ab`):
* `buffer-close` once closes `ab` and switches to a scratch buffer
* `buffer-close` again closes the scratch buffer and switches to a new
scratch buffer
* helix-term/commands: implement buffer-close! (bclose!, bc!)
Namely, if you have a document open in multiple splits, all the splits
will be closed at the same time, leaving only splits without that
document focused (or a scratch buffer if they were all focused on that
buffer).
* helix-view/tree: reset focus if Tree is empty
2021-11-15 16:30:45 +01:00
|
|
|
|
|
|
|
Ok(())
|
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-10-23 13:06:40 +02:00
|
|
|
pub fn focus_right(&mut self) {
|
|
|
|
self.tree.focus_direction(tree::Direction::Right);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn focus_left(&mut self) {
|
|
|
|
self.tree.focus_direction(tree::Direction::Left);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn focus_up(&mut self) {
|
|
|
|
self.tree.focus_direction(tree::Direction::Up);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn focus_down(&mut self) {
|
|
|
|
self.tree.focus_direction(tree::Direction::Down);
|
|
|
|
}
|
|
|
|
|
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);
|
2021-11-04 05:43:45 +01:00
|
|
|
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> {
|
2021-11-04 05:43:45 +01:00
|
|
|
self.documents.get(&id)
|
2021-03-23 09:47:40 +01:00
|
|
|
}
|
|
|
|
|
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> {
|
2021-11-04 05:43:45 +01:00
|
|
|
self.documents.get_mut(&id)
|
2021-06-23 20:35:39 +02:00
|
|
|
}
|
|
|
|
|
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-11-26 10:26:22 +01:00
|
|
|
let (view, doc) = current_ref!(self);
|
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
|
|
|
}
|