Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specifying multiple values for line_length_guide in config #280

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions documentation/pages/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,15 @@ See: the infamous tabs vs. spaces debate.

```yaml
line_length_guide: 80
# or
line_length_guide:
- 80
- 100
```

When set to a positive integer, this renders a background vertical line at the specified offset, to guide line length. When set to `false`, the guide is hidden.

It can also be set to an array, in which case a vertical line is rendered at each specified offset.

### Line Wrapping

Expand Down
6 changes: 3 additions & 3 deletions src/commands/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn save(app: &mut Application) -> Result {
.path
.clone(); // clone instead of borrow as we call another command later

if path.is_some() {
if let Some(path) = path {
// Save the buffer.
app.workspace
.current_buffer
Expand All @@ -32,7 +32,7 @@ pub fn save(app: &mut Application) -> Result {
.chain_err(|| BUFFER_SAVE_FAILED)?;

// Run the format command if one is defined.
if app.preferences.borrow().format_on_save(&path.unwrap()) {
if app.preferences.borrow().format_on_save(&path) {
format(app)?;

// Save the buffer again. We intentionally save twice because we
Expand Down Expand Up @@ -914,7 +914,7 @@ pub fn insert_tab(app: &mut Application) -> Result {
.ok_or(BUFFER_MISSING)?;
let tab_content = app.preferences.borrow().tab_content(buffer.path.as_ref());
let tab_content_width = tab_content.chars().count();
buffer.insert(tab_content.clone());
buffer.insert(tab_content);

// Move the cursor to the end of the inserted content.
for _ in 0..tab_content_width {
Expand Down
10 changes: 5 additions & 5 deletions src/commands/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fn copy_to_clipboard(app: &mut Application) -> Result {
let selected_range = Range::new(cursor_position, select_mode.anchor);

let data = buffer
.read(&selected_range.clone())
.read(&selected_range)
.ok_or("Couldn't read selected data from buffer")?;
app.clipboard.set_content(ClipboardContent::Inline(data))?;
}
Expand All @@ -72,7 +72,7 @@ fn copy_to_clipboard(app: &mut Application) -> Result {
util::inclusive_range(&LineRange::new(mode.anchor, buffer.cursor.line), buffer);

let data = buffer
.read(&selected_range.clone())
.read(&selected_range)
.ok_or("Couldn't read selected data from buffer")?;
app.clipboard.set_content(ClipboardContent::Block(data))?;
}
Expand All @@ -86,9 +86,9 @@ pub fn justify(app: &mut Application) -> Result {
let range = sel_to_range(app)?;
let buffer = app.workspace.current_buffer.as_mut().unwrap();

let limit = match app.preferences.borrow().line_length_guide() {
Some(n) => n,
None => bail!("Justification requires a line_length_guide."),
let limit = match app.preferences.borrow().line_length_guides()[..] {
[first, ..] => first,
[] => bail!("Justification requires a line_length_guide."),
};

buffer.start_operation_group();
Expand Down
2 changes: 1 addition & 1 deletion src/input/key_map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl KeyMap {
let default_keymap_data = YamlLoader::load_from_str(KeyMap::default_data())
.chain_err(|| "Couldn't parse default keymap")?
.into_iter()
.nth(0)
.next()
.ok_or("Couldn't locate a document in the default keymap")?;

KeyMap::from(default_keymap_data.as_hash().unwrap())
Expand Down
4 changes: 2 additions & 2 deletions src/models/application/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ impl Clipboard {
};

// Update the in-app clipboard if we've found newer content.
if new_content.is_some() {
self.content = new_content.unwrap();
if let Some(new_content) = new_content {
self.content = new_content;
}

&self.content
Expand Down
31 changes: 22 additions & 9 deletions src/models/application/preferences/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,24 +207,29 @@ impl Preferences {
})
}

pub fn line_length_guide(&self) -> Option<usize> {
pub fn line_length_guides(&self) -> Vec<usize> {
self.data
.as_ref()
.and_then(|data| match data[LINE_LENGTH_GUIDE_KEY] {
Yaml::Integer(line_length) => Some(line_length as usize),
.map(|data| match data[LINE_LENGTH_GUIDE_KEY] {
Yaml::Integer(line_length) => vec![line_length as usize],
Yaml::Boolean(line_length_guide) => {
let default = self.default[LINE_LENGTH_GUIDE_KEY]
.as_i64()
.expect("Couldn't find default line length guide setting!");

if line_length_guide {
Some(default as usize)
vec![default as usize]
} else {
None
vec![]
}
}
_ => None,
Yaml::Array(ref guides) => guides
.iter()
.filter_map(|value| value.as_i64().map(|v| v as usize))
.collect(),
_ => vec![],
})
.unwrap_or_default()
}

pub fn line_wrapping(&self) -> bool {
Expand Down Expand Up @@ -584,23 +589,31 @@ mod tests {
let data = YamlLoader::load_from_str("line_length_guide: 100").unwrap();
let preferences = Preferences::new(data.into_iter().nth(0));

assert_eq!(preferences.line_length_guide(), Some(100));
assert_eq!(preferences.line_length_guides(), vec![100]);
}

#[test]
fn preferences_returns_user_defined_multiple_line_length_guides() {
let data = YamlLoader::load_from_str("line_length_guide: [80, 100, 120]").unwrap();
let preferences = Preferences::new(data.into_iter().nth(0));

assert_eq!(preferences.line_length_guides(), vec![80, 100, 120]);
}

#[test]
fn preferences_returns_user_disabled_line_length_guide() {
let data = YamlLoader::load_from_str("line_length_guide: false").unwrap();
let preferences = Preferences::new(data.into_iter().nth(0));

assert_eq!(preferences.line_length_guide(), None);
assert_eq!(preferences.line_length_guides(), Vec::<usize>::new());
}

#[test]
fn preferences_returns_user_default_line_length_guide() {
let data = YamlLoader::load_from_str("line_length_guide: true").unwrap();
let preferences = Preferences::new(data.into_iter().nth(0));

assert_eq!(preferences.line_length_guide(), Some(80));
assert_eq!(preferences.line_length_guides(), vec![80]);
}

#[test]
Expand Down
10 changes: 6 additions & 4 deletions src/view/buffer/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ impl<'a, 'p> BufferRenderer<'a, 'p> {

fn print_rest_of_line(&mut self) {
let on_cursor_line = self.on_cursor_line();
let guide_offset = self.length_guide_offset();
let guide_offsets = self.length_guide_offsets();

for offset in self.screen_position.offset..self.terminal.width() {
let colors = if on_cursor_line || guide_offset.map(|go| go == offset).unwrap_or(false) {
let colors = if on_cursor_line || guide_offsets.contains(&offset) {
Colors::Focused
} else {
Colors::Default
Expand All @@ -106,10 +106,12 @@ impl<'a, 'p> BufferRenderer<'a, 'p> {
}
}

fn length_guide_offset(&self) -> Option<usize> {
fn length_guide_offsets(&self) -> Vec<usize> {
self.preferences
.line_length_guide()
.line_length_guides()
.into_iter()
.map(|offset| self.gutter_width + offset)
.collect()
}

fn advance_to_next_line(&mut self) {
Expand Down
Loading