Make `text-width` optional in editor config

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.
pull/5893/head
Clément Delafargue 2 years ago
parent 6aad3fffbe
commit b247d526d6
No known key found for this signature in database

@ -1739,20 +1739,24 @@ fn reflow(
return Ok(()); return Ok(());
} }
const DEFAULT_MAX_LEN: usize = 79;
let scrolloff = cx.editor.config().scrolloff; 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); let (view, doc) = current!(cx.editor);
// Find the text_width by checking the following sources in order: // Find the text_width by checking the following sources in order:
// - The passed argument in `args` // - The passed argument in `args`
// - The configured text-width for this language in languages.toml // - 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 let text_width: usize = args
.get(0) .get(0)
.map(|num| num.parse::<usize>()) .map(|num| num.parse::<usize>())
.transpose()? .transpose()?
.or_else(|| doc.language_config().and_then(|config| config.text_width)) .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(); let rope = doc.text();

@ -1231,7 +1231,12 @@ impl Document {
} }
pub fn text_format(&self, mut viewport_width: u16, theme: Option<&Theme>) -> TextFormat { 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 // 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. // 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. // In particular other commands like :reflow do not count the line terminator.
@ -1239,7 +1244,6 @@ impl Document {
// but having the last visual line exceed the width by 1 seems like a rare edge case. // 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) viewport_width = viewport_width.min(text_width as u16 + 1)
} }
let config = self.config.load();
let soft_wrap = &config.soft_wrap; let soft_wrap = &config.soft_wrap;
let tab_width = self.tab_width() as u16; let tab_width = self.tab_width() as u16;
TextFormat { TextFormat {

@ -242,7 +242,7 @@ pub struct Config {
/// Automatic save on focus lost. Defaults to false. /// Automatic save on focus lost. Defaults to false.
pub auto_save: bool, pub auto_save: bool,
/// Set a global text_width /// Set a global text_width
pub text_width: usize, pub text_width: Option<usize>,
/// Time in milliseconds since last keypress before idle timers trigger. /// Time in milliseconds since last keypress before idle timers trigger.
/// Used for autocompletion, set to 0 for instant. Defaults to 400ms. /// Used for autocompletion, set to 0 for instant. Defaults to 400ms.
#[serde( #[serde(
@ -766,7 +766,7 @@ impl Default for Config {
indent_guides: IndentGuidesConfig::default(), indent_guides: IndentGuidesConfig::default(),
color_modes: false, color_modes: false,
soft_wrap: SoftWrap::default(), soft_wrap: SoftWrap::default(),
text_width: 80, text_width: None,
} }
} }
} }

Loading…
Cancel
Save