Skip to content

Commit

Permalink
Make text-width optional in editor config
Browse files Browse the repository at this point in the history
When it was only used for `:reflow` it made sense to have a default
value set to `80`, but now that soft-wrapping uses this setting, keeping
a default set to `80` would make soft-wrapping behave more aggressively.
  • Loading branch information
divarvel committed Feb 10, 2023
1 parent 6aad3ff commit b247d52
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
10 changes: 7 additions & 3 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1739,20 +1739,24 @@ fn reflow(
return Ok(());
}

const DEFAULT_MAX_LEN: usize = 79;

let scrolloff = cx.editor.config().scrolloff;
let cfg_text_width: usize = cx.editor.config().text_width;
let cfg_text_width = cx.editor.config().text_width;
let (view, doc) = current!(cx.editor);

// Find the text_width by checking the following sources in order:
// - The passed argument in `args`
// - The configured text-width for this language in languages.toml
// - The configured text-width in the config.toml
// - The configured text-width in config.toml
// - The const default we set above
let text_width: usize = args
.get(0)
.map(|num| num.parse::<usize>())
.transpose()?
.or_else(|| doc.language_config().and_then(|config| config.text_width))
.unwrap_or(cfg_text_width);
.or(cfg_text_width)
.unwrap_or(DEFAULT_MAX_LEN);

let rope = doc.text();

Expand Down
8 changes: 6 additions & 2 deletions helix-view/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1231,15 +1231,19 @@ impl Document {
}

pub fn text_format(&self, mut viewport_width: u16, theme: Option<&Theme>) -> TextFormat {
if let Some(text_width) = self.language_config().and_then(|config| config.text_width) {
let config = self.config.load();
let text_width = self
.language_config()
.and_then(|config| config.text_width)
.or_else(|| config.text_width);
if let Some(text_width) = text_width {
// We increase max_line_len by 1 because softwrap considers the newline character
// as part of the line length while the "typical" expectation is that this is not the case.
// In particular other commands like :reflow do not count the line terminator.
// This is technically inconsistent for the last line as that line never has a line terminator
// but having the last visual line exceed the width by 1 seems like a rare edge case.
viewport_width = viewport_width.min(text_width as u16 + 1)
}
let config = self.config.load();
let soft_wrap = &config.soft_wrap;
let tab_width = self.tab_width() as u16;
TextFormat {
Expand Down
4 changes: 2 additions & 2 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ pub struct Config {
/// Automatic save on focus lost. Defaults to false.
pub auto_save: bool,
/// Set a global text_width
pub text_width: usize,
pub text_width: Option<usize>,
/// Time in milliseconds since last keypress before idle timers trigger.
/// Used for autocompletion, set to 0 for instant. Defaults to 400ms.
#[serde(
Expand Down Expand Up @@ -766,7 +766,7 @@ impl Default for Config {
indent_guides: IndentGuidesConfig::default(),
color_modes: false,
soft_wrap: SoftWrap::default(),
text_width: 80,
text_width: None,
}
}
}
Expand Down

0 comments on commit b247d52

Please sign in to comment.