2 clipboard::{get_clipboard_provider, ClipboardProvider},
3 graphics::{CursorKind, Rect},
6 Document, DocumentId, RegisterSelection, View, ViewId,
9use futures_util::future;
10use futures_util::stream::select_all::SelectAll;
11use tokio_stream::wrappers::UnboundedReceiverStream;
14 path::{Path, PathBuf},
23pub use helix_core::diagnostic::Severity;
24pub use helix_core::register::Registers;
25use helix_core::syntax;
26use helix_core::Position;
28use serde::Deserialize;
30#[derive(Debug, Clone, PartialEq, Deserialize)]
31#[serde(rename_all = "kebab-case", default)]
33 /// Padding to keep between the edge of the screen and the cursor when scrolling. Defaults to 5.
35 /// Number of lines to scroll at once. Defaults to 3
36 pub scroll_lines: isize,
37 /// Mouse support. Defaults to true.
40 pub line_number: LineNumber,
41 /// Middle click paste support. Defaults to true
42 pub middle_click_paste: bool,
45#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
46#[serde(rename_all = "kebab-case")]
48 /// Show absolute line number
51 /// Show relative line number to the primary cursor
55impl Default for Config {
56 fn default() -> Self {
61 line_number: LineNumber::Absolute,
62 middle_click_paste: true,
70 pub documents: SlotMap<DocumentId, Document>,
71 pub count: Option<std::num::NonZeroUsize>,
72 pub selected_register: RegisterSelection,
73 pub registers: Registers,
75 pub language_servers: helix_lsp::Registry,
77 pub debugger: Option<helix_dap::Client>,
78 pub debugger_events: SelectAll<UnboundedReceiverStream<helix_dap::Payload>>,
80 pub clipboard_provider: Box<dyn ClipboardProvider>,
82 pub syn_loader: Arc<syntax::Loader>,
83 pub theme_loader: Arc<theme::Loader>,
85 pub status_msg: Option<(String, Severity)>,
90#[derive(Debug, Copy, Clone)]
101 themes: Arc<theme::Loader>,
102 config_loader: Arc<syntax::Loader>,
105 let language_servers = helix_lsp::Registry::new();
107 // HAXX: offset the render area height by 1 to account for prompt/commandline
111 tree: Tree::new(area),
112 documents: SlotMap::with_key(),
114 selected_register: RegisterSelection::default(),
115 theme: themes.default(),
118 debugger_events: SelectAll::new(),
119 syn_loader: config_loader,
120 theme_loader: themes,
121 registers: Registers::default(),
122 clipboard_provider: get_clipboard_provider(),
128 pub fn clear_status(&mut self) {
129 self.status_msg = None;
132 pub fn set_status(&mut self, status: String) {
133 self.status_msg = Some((status, Severity::Info));
136 pub fn set_error(&mut self, error: String) {
137 self.status_msg = Some((error, Severity::Error));
140 pub fn set_theme(&mut self, theme: Theme) {
141 let scopes = theme.scopes();
144 .language_configs_iter()
145 .filter(|cfg| cfg.is_highlight_initialized())
147 config.reconfigure(scopes);
154 pub fn set_theme_from_name(&mut self, theme: &str) -> anyhow::Result<()> {
158 .load(theme.as_ref())
159 .with_context(|| format!("failed setting theme `{}`", theme))?;
160 self.set_theme(theme);
164 fn _refresh(&mut self) {
165 for (view, _) in self.tree.views_mut() {
166 let doc = &self.documents[view.doc];
167 view.ensure_cursor_in_view(doc, self.config.scrolloff)
171 pub fn switch(&mut self, id: DocumentId, action: Action) {
172 use crate::tree::Layout;
173 use helix_core::Selection;
175 if !self.documents.contains_key(id) {
176 log::error!("cannot switch to document that does not exist (anymore)");
182 let view = view!(self);
185 self.documents[view.doc].selection(view.id).clone(),
188 let view = view_mut!(self);
189 view.jumps.push(jump);
190 view.last_accessed_doc = Some(view.doc);
192 view.offset = Position::default();
194 let (view, doc) = current!(self);
196 // initialize selection for view
199 .or_insert_with(|| Selection::point(0));
200 // TODO: reuse align_view
204 .cursor(doc.text().slice(..));
205 let line = doc.text().char_to_line(pos);
206 view.offset.row = line.saturating_sub(view.inner_area().height as usize / 2);
213 Action::HorizontalSplit => {
214 let view = View::new(id);
215 let view_id = self.tree.split(view, Layout::Horizontal);
216 // initialize selection for view
217 let doc = &mut self.documents[id];
218 doc.selections.insert(view_id, Selection::point(0));
220 Action::VerticalSplit => {
221 let view = View::new(id);
222 let view_id = self.tree.split(view, Layout::Vertical);
223 // initialize selection for view
224 let doc = &mut self.documents[id];
225 doc.selections.insert(view_id, Selection::point(0));
232 pub fn new_file(&mut self, action: Action) -> DocumentId {
233 let doc = Document::default();
234 let id = self.documents.insert(doc);
235 self.documents[id].id = id;
236 self.switch(id, action);
240 pub fn open(&mut self, path: PathBuf, action: Action) -> Result<DocumentId, Error> {
241 let path = crate::document::canonicalize_path(&path)?;
245 .find(|doc| doc.path() == Some(&path))
248 let id = if let Some(id) = id {
251 let mut doc = Document::open(&path, None, Some(&self.theme), Some(&self.syn_loader))?;
253 // try to find a language server based on the language name
254 let language_server = doc
257 .and_then(|language| self.language_servers.get(language).ok());
259 if let Some(language_server) = language_server {
260 doc.set_language_server(Some(language_server.clone()));
262 let language_id = doc
264 .and_then(|s| s.split('.').last()) // source.rust
265 .map(ToOwned::to_owned)
266 .unwrap_or_default();
268 tokio::spawn(language_server.text_document_did_open(
276 let id = self.documents.insert(doc);
277 self.documents[id].id = id;
281 self.switch(id, action);
285 pub fn close(&mut self, id: ViewId, close_buffer: bool) {
286 let view = self.tree.get(self.tree.focus);
288 self.documents[view.doc].selections.remove(&id);
291 // get around borrowck issues
292 let language_servers = &mut self.language_servers;
293 let doc = &self.documents[view.doc];
295 let language_server = doc
298 .and_then(|language| language_servers.get(language).ok());
299 if let Some(language_server) = language_server {
300 tokio::spawn(language_server.text_document_did_close(doc.identifier()));
302 self.documents.remove(view.doc);
305 self.tree.remove(id);
309 pub fn resize(&mut self, area: Rect) {
310 if self.tree.resize(area) {
315 pub fn focus_next(&mut self) {
316 self.tree.focus_next();
319 pub fn should_close(&self) -> bool {
323 pub fn ensure_cursor_in_view(&mut self, id: ViewId) {
324 let view = self.tree.get_mut(id);
325 let doc = &self.documents[view.doc];
326 view.ensure_cursor_in_view(doc, self.config.scrolloff)
329 pub fn document(&self, id: DocumentId) -> Option<&Document> {
330 self.documents.get(id)
333 pub fn document_mut(&mut self, id: DocumentId) -> Option<&mut Document> {
334 self.documents.get_mut(id)
337 pub fn documents(&self) -> impl Iterator<Item = &Document> {
338 self.documents.iter().map(|(_id, doc)| doc)
341 pub fn documents_mut(&mut self) -> impl Iterator<Item = &mut Document> {
342 self.documents.iter_mut().map(|(_id, doc)| doc)
345 pub fn document_by_path<P: AsRef<Path>>(&self, path: P) -> Option<&Document> {
347 .find(|doc| doc.path().map(|p| p == path.as_ref()).unwrap_or(false))
350 // pub fn current_document(&self) -> Document {
351 // let id = self.view().doc;
352 // let doc = &mut editor.documents[id];
355 pub fn cursor(&self) -> (Option<Position>, CursorKind) {
356 let view = view!(self);
357 let doc = &self.documents[view.doc];
361 .cursor(doc.text().slice(..));
362 if let Some(mut pos) = view.screen_coords_at_pos(doc, doc.text().slice(..), cursor) {
363 let inner = view.inner_area();
364 pos.col += inner.x as usize;
365 pos.row += inner.y as usize;
366 (Some(pos), CursorKind::Hidden)
368 (None, CursorKind::Hidden)
372 /// Closes language servers with timeout. The default timeout is 500 ms, use
373 /// `timeout` parameter to override this.
374 pub async fn close_language_servers(
376 timeout: Option<u64>,
377 ) -> Result<(), tokio::time::error::Elapsed> {
378 tokio::time::timeout(
379 Duration::from_millis(timeout.unwrap_or(500)),
381 self.language_servers
383 .map(|client| client.force_shutdown()),
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list