-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(format): respect max line width (#242)
- Loading branch information
Showing
14 changed files
with
891 additions
and
1,066 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,73 @@ | ||
//! Indentation within formatting configuration. | ||
use std::num::NonZeroUsize; | ||
use crate::SPACE; | ||
use crate::TAB; | ||
|
||
/// The default number of spaces to represent one indentation level. | ||
const DEFAULT_SPACE_INDENT: usize = 4; | ||
/// The default indentation. | ||
pub const DEFAULT_INDENT: Indent = Indent::Spaces(unsafe { NonZeroUsize::new_unchecked(4) }); | ||
pub const DEFAULT_INDENT: Indent = Indent::Spaces(DEFAULT_SPACE_INDENT); | ||
/// The maximum number of spaces to represent one indentation level. | ||
pub const MAX_SPACE_INDENT: usize = 16; | ||
|
||
/// An indentation level. | ||
#[derive(Clone, Copy, Debug)] | ||
pub enum Indent { | ||
/// Tabs. | ||
Tabs(NonZeroUsize), | ||
|
||
Tabs, | ||
/// Spaces. | ||
Spaces(NonZeroUsize), | ||
Spaces(usize), | ||
} | ||
|
||
impl Default for Indent { | ||
fn default() -> Self { | ||
DEFAULT_INDENT | ||
} | ||
} | ||
|
||
impl Indent { | ||
/// Attempts to create a new indentation level configuration. | ||
pub fn try_new(tab: bool, num_spaces: Option<usize>) -> Result<Self, String> { | ||
match (tab, num_spaces) { | ||
(true, None) => Ok(Indent::Tabs), | ||
(true, Some(_)) => { | ||
Err("Indentation with tabs cannot have a number of spaces".to_string()) | ||
} | ||
(false, Some(n)) => { | ||
if n > MAX_SPACE_INDENT { | ||
Err(format!( | ||
"Indentation with spaces cannot have more than {} characters", | ||
MAX_SPACE_INDENT | ||
)) | ||
} else { | ||
Ok(Indent::Spaces(n)) | ||
} | ||
} | ||
(false, None) => Ok(Indent::Spaces(DEFAULT_SPACE_INDENT)), | ||
} | ||
} | ||
|
||
/// Gets the number of characters to indent. | ||
pub fn num(&self) -> usize { | ||
match self { | ||
Indent::Tabs => 1, | ||
Indent::Spaces(n) => *n, | ||
} | ||
} | ||
|
||
/// Gets the character used for indentation. | ||
pub fn character(&self) -> &str { | ||
match self { | ||
Indent::Tabs => TAB, | ||
Indent::Spaces(_) => SPACE, | ||
} | ||
} | ||
|
||
/// Gets the string representation of the indentation. | ||
pub fn string(&self) -> String { | ||
match self { | ||
Indent::Tabs => self.character().to_string(), | ||
Indent::Spaces(n) => self.character().repeat(*n), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
//! Configuration for max line length formatting. | ||
/// The default maximum line length. | ||
pub const DEFAULT_MAX_LINE_LENGTH: usize = 90; | ||
/// The minimum maximum line length. | ||
pub const MIN_MAX_LINE_LENGTH: usize = 60; | ||
/// The maximum maximum line length. | ||
pub const MAX_MAX_LINE_LENGTH: usize = 240; | ||
|
||
/// The maximum line length. | ||
#[derive(Clone, Copy, Debug, Eq, PartialEq)] | ||
pub struct MaxLineLength(Option<usize>); | ||
|
||
impl MaxLineLength { | ||
/// Attempts to create a new `MaxLineLength` with the provided value. | ||
/// | ||
/// A value of `0` indicates no maximum. | ||
pub fn try_new(value: usize) -> Result<Self, String> { | ||
let val = match value { | ||
0 => Self(None), | ||
MIN_MAX_LINE_LENGTH..=MAX_MAX_LINE_LENGTH => Self(Some(value)), | ||
_ => { | ||
return Err(format!( | ||
"The maximum line length must be between {} and {} or 0", | ||
MIN_MAX_LINE_LENGTH, MAX_MAX_LINE_LENGTH | ||
)); | ||
} | ||
}; | ||
Ok(val) | ||
} | ||
|
||
/// Gets the maximum line length. A value of `None` indicates no maximum. | ||
pub fn get(&self) -> Option<usize> { | ||
self.0 | ||
} | ||
} | ||
|
||
impl Default for MaxLineLength { | ||
fn default() -> Self { | ||
Self(Some(DEFAULT_MAX_LINE_LENGTH)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.