Skip to content

Commit

Permalink
config: Rename [terminal] to [editor] and pass it into Editor
Browse files Browse the repository at this point in the history
  • Loading branch information
archseer committed Aug 8, 2021
1 parent f0eb6ed commit a2ccfff
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 42 deletions.
9 changes: 7 additions & 2 deletions helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ impl Application {
let syn_loader_conf = toml::from_slice(lang_conf).expect("Could not parse languages.toml");
let syn_loader = std::sync::Arc::new(syntax::Loader::new(syn_loader_conf));

let mut editor = Editor::new(size, theme_loader.clone(), syn_loader.clone());
let mut editor = Editor::new(
size,
theme_loader.clone(),
syn_loader.clone(),
config.editor.clone(),
);

let editor_view = Box::new(ui::EditorView::new(std::mem::take(&mut config.keys)));
compositor.push(editor_view);
Expand Down Expand Up @@ -489,7 +494,7 @@ impl Application {
terminal::enable_raw_mode()?;
let mut stdout = stdout();
execute!(stdout, terminal::EnterAlternateScreen)?;
if self.config.terminal.mouse {
if self.config.editor.mouse {
execute!(stdout, EnableMouseCapture)?;
}
Ok(())
Expand Down
27 changes: 15 additions & 12 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@ use helix_core::{
};

use helix_view::{
document::Mode,
editor::Action,
input::KeyEvent,
keyboard::KeyCode,
view::{View, PADDING},
Document, DocumentId, Editor, ViewId,
document::Mode, editor::Action, input::KeyEvent, keyboard::KeyCode, view::View, Document,
DocumentId, Editor, ViewId,
};

use anyhow::{anyhow, bail, Context as _};
Expand Down Expand Up @@ -452,7 +448,11 @@ fn goto_first_nonwhitespace(cx: &mut Context) {
fn goto_window(cx: &mut Context, align: Align) {
let (view, doc) = current!(cx.editor);

let scrolloff = PADDING.min(view.area.height as usize / 2); // TODO: user pref
let scrolloff = cx
.editor
.config
.scrolloff
.min(view.area.height as usize / 2); // TODO: user pref

let last_line = view.last_line(doc);

Expand Down Expand Up @@ -890,7 +890,11 @@ fn scroll(cx: &mut Context, offset: usize, direction: Direction) {
return;
}

let scrolloff = PADDING.min(view.area.height as usize / 2); // TODO: user pref
let scrolloff = cx
.editor
.config
.scrolloff
.min(view.area.height as usize / 2); // TODO: user pref

view.first_line = match direction {
Forward => view.first_line + offset,
Expand Down Expand Up @@ -3927,10 +3931,9 @@ fn align_view_middle(cx: &mut Context) {
.cursor(doc.text().slice(..));
let pos = coords_at_pos(doc.text().slice(..), pos);

const OFFSET: usize = 7; // gutters
view.first_col = pos
.col
.saturating_sub(((view.area.width as usize).saturating_sub(OFFSET)) / 2);
view.first_col = pos.col.saturating_sub(
((view.area.width as usize).saturating_sub(crate::ui::editor::GUTTER_OFFSET as usize)) / 2,
);
}

fn scroll_up(cx: &mut Context) {
Expand Down
14 changes: 1 addition & 13 deletions helix-term/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct Config {
#[serde(default)]
pub keys: Keymaps,
#[serde(default)]
pub terminal: TerminalConfig,
pub editor: helix_view::editor::Config,
}

#[derive(Debug, Default, Clone, PartialEq, Deserialize)]
Expand All @@ -19,18 +19,6 @@ pub struct LspConfig {
pub display_messages: bool,
}

#[derive(Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct TerminalConfig {
pub mouse: bool,
}

impl Default for TerminalConfig {
fn default() -> Self {
Self { mouse: true }
}
}

#[test]
fn parsing_keymaps_config_file() {
use crate::keymap;
Expand Down
14 changes: 7 additions & 7 deletions helix-term/src/ui/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub struct EditorView {
pub autoinfo: Option<Info>,
}

const OFFSET: u16 = 7; // 1 diagnostic + 5 linenr + 1 gutter
pub const GUTTER_OFFSET: u16 = 7; // 1 diagnostic + 5 linenr + 1 gutter

impl Default for EditorView {
fn default() -> Self {
Expand Down Expand Up @@ -72,9 +72,9 @@ impl EditorView {
loader: &syntax::Loader,
) {
let area = Rect::new(
view.area.x + OFFSET,
view.area.x + GUTTER_OFFSET,
view.area.y,
view.area.width - OFFSET,
view.area.width - GUTTER_OFFSET,
view.area.height.saturating_sub(1),
); // - 1 for statusline

Expand Down Expand Up @@ -339,7 +339,7 @@ impl EditorView {
use helix_core::diagnostic::Severity;
if let Some(diagnostic) = doc.diagnostics().iter().find(|d| d.line == line) {
surface.set_stringn(
viewport.x - OFFSET,
viewport.x - GUTTER_OFFSET,
viewport.y + i as u16,
"●",
1,
Expand All @@ -360,7 +360,7 @@ impl EditorView {
format!("{:>5}", line + 1)
};
surface.set_stringn(
viewport.x + 1 - OFFSET,
viewport.x + 1 - GUTTER_OFFSET,
viewport.y + i as u16,
line_number_text,
5,
Expand Down Expand Up @@ -401,7 +401,7 @@ impl EditorView {
format!("{:>5}", line_number + 1)
};
surface.set_stringn(
viewport.x + 1 - OFFSET,
viewport.x + 1 - GUTTER_OFFSET,
viewport.y + head.row as u16,
line_number_text,
5,
Expand Down Expand Up @@ -778,7 +778,7 @@ impl Component for EditorView {
}

let (view, doc) = current!(cx.editor);
view.ensure_cursor_in_view(doc);
view.ensure_cursor_in_view(doc, cx.editor.config.scrolloff);

// mode transitions
match (mode, doc.mode()) {
Expand Down
4 changes: 2 additions & 2 deletions helix-term/src/ui/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod completion;
mod editor;
pub(crate) mod editor;
mod info;
mod markdown;
mod menu;
Expand Down Expand Up @@ -63,7 +63,7 @@ pub fn regex_prompt(

fun(view, doc, registers, regex);

view.ensure_cursor_in_view(doc);
view.ensure_cursor_in_view(doc, cx.editor.config.scrolloff);
}
Err(_err) => (), // TODO: mark command line as error
}
Expand Down
28 changes: 26 additions & 2 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,26 @@ pub use helix_core::register::Registers;
use helix_core::syntax;
use helix_core::Position;

use serde::Deserialize;

#[derive(Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct Config {

This comment has been minimized.

Copy link
@sudormrfbin

sudormrfbin Aug 8, 2021

Member

This will actually error out if there's only one item from the struct in the config (when there's scrolloff only or mouse only); #[serde(rename_all = "kebab-case", default)] should solve the problem.

/// Padding to keep between the edge of the screen and the cursor when scrolling. Defaults to 5.
pub scrolloff: usize,
/// Mouse support. Defaults to true.
pub mouse: bool,
}

impl Default for Config {
fn default() -> Self {
Self {
scrolloff: 5,

This comment has been minimized.

Copy link
@sudormrfbin

sudormrfbin Aug 8, 2021

Member

I think scroll_offset is a bit more descriptive at least in the config file itself 👀

mouse: true,
}
}
}

#[derive(Debug)]
pub struct Editor {
pub tree: Tree,
Expand All @@ -33,6 +53,8 @@ pub struct Editor {
pub theme_loader: Arc<theme::Loader>,

pub status_msg: Option<(String, Severity)>,

pub config: Config,
}

#[derive(Debug, Copy, Clone)]
Expand All @@ -48,6 +70,7 @@ impl Editor {
mut area: Rect,
themes: Arc<theme::Loader>,
config_loader: Arc<syntax::Loader>,
config: Config,
) -> Self {
let language_servers = helix_lsp::Registry::new();

Expand All @@ -66,6 +89,7 @@ impl Editor {
registers: Registers::default(),
clipboard_provider: get_clipboard_provider(),
status_msg: None,
config,
}
}

Expand Down Expand Up @@ -108,7 +132,7 @@ impl Editor {
fn _refresh(&mut self) {
for (view, _) in self.tree.views_mut() {
let doc = &self.documents[view.doc];
view.ensure_cursor_in_view(doc)
view.ensure_cursor_in_view(doc, self.config.scrolloff)
}
}

Expand Down Expand Up @@ -267,7 +291,7 @@ impl Editor {
pub fn ensure_cursor_in_view(&mut self, id: ViewId) {
let view = self.tree.get_mut(id);
let doc = &self.documents[view.doc];
view.ensure_cursor_in_view(doc)
view.ensure_cursor_in_view(doc, self.config.scrolloff)
}

pub fn document(&self, id: DocumentId) -> Option<&Document> {
Expand Down
6 changes: 2 additions & 4 deletions helix-view/src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ use helix_core::{
Position, RopeSlice, Selection,
};

pub const PADDING: usize = 5;

type Jump = (DocumentId, Selection);

#[derive(Debug)]
Expand Down Expand Up @@ -84,7 +82,7 @@ impl View {
}
}

pub fn ensure_cursor_in_view(&mut self, doc: &Document) {
pub fn ensure_cursor_in_view(&mut self, doc: &Document, scrolloff: usize) {
let cursor = doc
.selection(self.id)
.primary()
Expand All @@ -95,7 +93,7 @@ impl View {
let height = self.area.height.saturating_sub(1); // - 1 for statusline
let last_line = (self.first_line + height as usize).saturating_sub(1);

let scrolloff = PADDING.min(self.area.height as usize / 2); // TODO: user pref
let scrolloff = scrolloff.min(self.area.height as usize / 2);

// TODO: not ideal
const OFFSET: usize = 7; // 1 diagnostic + 5 linenr + 1 gutter
Expand Down

0 comments on commit a2ccfff

Please sign in to comment.