on master help sign in
config: Rename [terminal] to [editor] and pass it into Editor
commita2ccfffda1171bde5118c1a1120af49dc87aecca authorBlaž Hrastnik <blaz@mxxn.io> dateSun 8 Aug 2021, 14:07 (5 years ago) parentf0eb6ed
7 files changed+60−42
helix-term/src/application.rs+7−2 helix-term/src/commands.rs+15−12 helix-term/src/config.rs+1−13 helix-term/src/ui/editor.rs+7−7 helix-term/src/ui/mod.rs+2−2 helix-view/src/editor.rs+26−2 helix-view/src/view.rs+2−4
helix-term/src/application.rs+7−2
@@ -80,7 +80,12 @@ impl Application {
8080 let syn_loader_conf = toml::from_slice(lang_conf).expect("Could not parse languages.toml");
8181 let syn_loader = std::sync::Arc::new(syntax::Loader::new(syn_loader_conf));
8282
83− let mut editor = Editor::new(size, theme_loader.clone(), syn_loader.clone());
83+ let mut editor = Editor::new(
84+ size,
85+ theme_loader.clone(),
86+ syn_loader.clone(),
87+ config.editor.clone(),
88+ );
8489
8590 let editor_view = Box::new(ui::EditorView::new(std::mem::take(&mut config.keys)));
8691 compositor.push(editor_view);
@@ -489,7 +494,7 @@ impl Application {
489494 terminal::enable_raw_mode()?;
490495 let mut stdout = stdout();
491496 execute!(stdout, terminal::EnterAlternateScreen)?;
492− if self.config.terminal.mouse {
497+ if self.config.editor.mouse {
493498 execute!(stdout, EnableMouseCapture)?;
494499 }
495500 Ok(())
helix-term/src/commands.rs+15−12
@@ -12,12 +12,8 @@ use helix_core::{
1212};
1313
1414use helix_view::{
15− document::Mode,
16− editor::Action,
17− input::KeyEvent,
18− keyboard::KeyCode,
19− view::{View, PADDING},
20− Document, DocumentId, Editor, ViewId,
15+ document::Mode, editor::Action, input::KeyEvent, keyboard::KeyCode, view::View, Document,
16+ DocumentId, Editor, ViewId,
2117};
2218
2319use anyhow::{anyhow, bail, Context as _};
@@ -452,7 +448,11 @@ fn goto_first_nonwhitespace(cx: &mut Context) {
452448fn goto_window(cx: &mut Context, align: Align) {
453449 let (view, doc) = current!(cx.editor);
454450
455− let scrolloff = PADDING.min(view.area.height as usize / 2); // TODO: user pref
451+ let scrolloff = cx
452+ .editor
453+ .config
454+ .scrolloff
455+ .min(view.area.height as usize / 2); // TODO: user pref
456456
457457 let last_line = view.last_line(doc);
458458
@@ -890,7 +890,11 @@ fn scroll(cx: &mut Context, offset: usize, direction: Direction) {
890890 return;
891891 }
892892
893− let scrolloff = PADDING.min(view.area.height as usize / 2); // TODO: user pref
893+ let scrolloff = cx
894+ .editor
895+ .config
896+ .scrolloff
897+ .min(view.area.height as usize / 2); // TODO: user pref
894898
895899 view.first_line = match direction {
896900 Forward => view.first_line + offset,
@@ -3927,10 +3931,9 @@ fn align_view_middle(cx: &mut Context) {
39273931 .cursor(doc.text().slice(..));
39283932 let pos = coords_at_pos(doc.text().slice(..), pos);
39293933
3930− const OFFSET: usize = 7; // gutters
3931− view.first_col = pos
3932− .col
3933− .saturating_sub(((view.area.width as usize).saturating_sub(OFFSET)) / 2);
3934+ view.first_col = pos.col.saturating_sub(
3935+ ((view.area.width as usize).saturating_sub(crate::ui::editor::GUTTER_OFFSET as usize)) / 2,
3936+ );
39343937}
39353938
39363939fn scroll_up(cx: &mut Context) {
helix-term/src/config.rs+1−13
@@ -10,7 +10,7 @@ pub struct Config {
1010 #[serde(default)]
1111 pub keys: Keymaps,
1212 #[serde(default)]
13− pub terminal: TerminalConfig,
13+ pub editor: helix_view::editor::Config,
1414}
1515
1616#[derive(Debug, Default, Clone, PartialEq, Deserialize)]
@@ -19,18 +19,6 @@ pub struct LspConfig {
1919 pub display_messages: bool,
2020}
2121
22−#[derive(Debug, Clone, PartialEq, Deserialize)]
23−#[serde(rename_all = "kebab-case")]
24−pub struct TerminalConfig {
25− pub mouse: bool,
26−}
27−
28−impl Default for TerminalConfig {
29− fn default() -> Self {
30− Self { mouse: true }
31− }
32−}
33−
3422#[test]
3523fn parsing_keymaps_config_file() {
3624 use crate::keymap;
helix-term/src/ui/editor.rs+7−7
@@ -36,7 +36,7 @@ pub struct EditorView {
3636 pub autoinfo: Option<Info>,
3737}
3838
39−const OFFSET: u16 = 7; // 1 diagnostic + 5 linenr + 1 gutter
39+pub const GUTTER_OFFSET: u16 = 7; // 1 diagnostic + 5 linenr + 1 gutter
4040
4141impl Default for EditorView {
4242 fn default() -> Self {
@@ -72,9 +72,9 @@ impl EditorView {
7272 loader: &syntax::Loader,
7373 ) {
7474 let area = Rect::new(
75− view.area.x + OFFSET,
75+ view.area.x + GUTTER_OFFSET,
7676 view.area.y,
77− view.area.width - OFFSET,
77+ view.area.width - GUTTER_OFFSET,
7878 view.area.height.saturating_sub(1),
7979 ); // - 1 for statusline
8080
@@ -339,7 +339,7 @@ impl EditorView {
339339 use helix_core::diagnostic::Severity;
340340 if let Some(diagnostic) = doc.diagnostics().iter().find(|d| d.line == line) {
341341 surface.set_stringn(
342− viewport.x - OFFSET,
342+ viewport.x - GUTTER_OFFSET,
343343 viewport.y + i as u16,
344344 "●",
345345 1,
@@ -360,7 +360,7 @@ impl EditorView {
360360 format!("{:>5}", line + 1)
361361 };
362362 surface.set_stringn(
363− viewport.x + 1 - OFFSET,
363+ viewport.x + 1 - GUTTER_OFFSET,
364364 viewport.y + i as u16,
365365 line_number_text,
366366 5,
@@ -401,7 +401,7 @@ impl EditorView {
401401 format!("{:>5}", line_number + 1)
402402 };
403403 surface.set_stringn(
404− viewport.x + 1 - OFFSET,
404+ viewport.x + 1 - GUTTER_OFFSET,
405405 viewport.y + head.row as u16,
406406 line_number_text,
407407 5,
@@ -778,7 +778,7 @@ impl Component for EditorView {
778778 }
779779
780780 let (view, doc) = current!(cx.editor);
781− view.ensure_cursor_in_view(doc);
781+ view.ensure_cursor_in_view(doc, cx.editor.config.scrolloff);
782782
783783 // mode transitions
784784 match (mode, doc.mode()) {
helix-term/src/ui/mod.rs+2−2
@@ -1,5 +1,5 @@
11mod completion;
2−mod editor;
2+pub(crate) mod editor;
33mod info;
44mod markdown;
55mod menu;
@@ -63,7 +63,7 @@ pub fn regex_prompt(
6363
6464 fun(view, doc, registers, regex);
6565
66− view.ensure_cursor_in_view(doc);
66+ view.ensure_cursor_in_view(doc, cx.editor.config.scrolloff);
6767 }
6868 Err(_err) => (), // TODO: mark command line as error
6969 }
helix-view/src/editor.rs+26−2
@@ -18,6 +18,26 @@ pub use helix_core::register::Registers;
1818use helix_core::syntax;
1919use helix_core::Position;
2020
21+use serde::Deserialize;
22+
23+#[derive(Debug, Clone, PartialEq, Deserialize)]
24+#[serde(rename_all = "kebab-case")]
25+pub struct Config {
26+ /// Padding to keep between the edge of the screen and the cursor when scrolling. Defaults to 5.
27+ pub scrolloff: usize,
28+ /// Mouse support. Defaults to true.
29+ pub mouse: bool,
30+}
31+
32+impl Default for Config {
33+ fn default() -> Self {
34+ Self {
35+ scrolloff: 5,
36+ mouse: true,
37+ }
38+ }
39+}
40+
2141#[derive(Debug)]
2242pub struct Editor {
2343 pub tree: Tree,
@@ -33,6 +53,8 @@ pub struct Editor {
3353 pub theme_loader: Arc<theme::Loader>,
3454
3555 pub status_msg: Option<(String, Severity)>,
56+
57+ pub config: Config,
3658}
3759
3860#[derive(Debug, Copy, Clone)]
@@ -48,6 +70,7 @@ impl Editor {
4870 mut area: Rect,
4971 themes: Arc<theme::Loader>,
5072 config_loader: Arc<syntax::Loader>,
73+ config: Config,
5174 ) -> Self {
5275 let language_servers = helix_lsp::Registry::new();
5376
@@ -66,6 +89,7 @@ impl Editor {
6689 registers: Registers::default(),
6790 clipboard_provider: get_clipboard_provider(),
6891 status_msg: None,
92+ config,
6993 }
7094 }
7195
@@ -108,7 +132,7 @@ impl Editor {
108132 fn _refresh(&mut self) {
109133 for (view, _) in self.tree.views_mut() {
110134 let doc = &self.documents[view.doc];
111− view.ensure_cursor_in_view(doc)
135+ view.ensure_cursor_in_view(doc, self.config.scrolloff)
112136 }
113137 }
114138
@@ -267,7 +291,7 @@ impl Editor {
267291 pub fn ensure_cursor_in_view(&mut self, id: ViewId) {
268292 let view = self.tree.get_mut(id);
269293 let doc = &self.documents[view.doc];
270− view.ensure_cursor_in_view(doc)
294+ view.ensure_cursor_in_view(doc, self.config.scrolloff)
271295 }
272296
273297 pub fn document(&self, id: DocumentId) -> Option<&Document> {
helix-view/src/view.rs+2−4
@@ -8,8 +8,6 @@ use helix_core::{
88 Position, RopeSlice, Selection,
99};
1010
11−pub const PADDING: usize = 5;
12−
1311type Jump = (DocumentId, Selection);
1412
1513#[derive(Debug)]
@@ -84,7 +82,7 @@ impl View {
8482 }
8583 }
8684
87− pub fn ensure_cursor_in_view(&mut self, doc: &Document) {
85+ pub fn ensure_cursor_in_view(&mut self, doc: &Document, scrolloff: usize) {
8886 let cursor = doc
8987 .selection(self.id)
9088 .primary()
@@ -95,7 +93,7 @@ impl View {
9593 let height = self.area.height.saturating_sub(1); // - 1 for statusline
9694 let last_line = (self.first_line + height as usize).saturating_sub(1);
9795
98− let scrolloff = PADDING.min(self.area.height as usize / 2); // TODO: user pref
96+ let scrolloff = scrolloff.min(self.area.height as usize / 2);
9997
10098 // TODO: not ideal
10199 const OFFSET: usize = 7; // 1 diagnostic + 5 linenr + 1 gutter