generated from tweag/project
-
Notifications
You must be signed in to change notification settings - Fork 33
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
Add support for @upper_case @lower_case captures #838
Open
ctdunc
wants to merge
1
commit into
tweag:main
Choose a base branch
from
ctdunc:feature/capitalization
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+117
−7
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,5 +1,6 @@ | ||
use std::{ | ||
borrow::Cow, | ||
cmp::max, | ||
collections::{HashMap, HashSet}, | ||
mem, | ||
ops::Deref, | ||
|
@@ -8,7 +9,8 @@ use std::{ | |
use topiary_tree_sitter_facade::Node; | ||
|
||
use crate::{ | ||
tree_sitter::NodeExt, Atom, FormatterError, FormatterResult, ScopeCondition, ScopeInformation, | ||
tree_sitter::NodeExt, Atom, FormatterError, FormatterResult, HowCapitalize, ScopeCondition, | ||
ScopeInformation, | ||
}; | ||
|
||
/// A struct that holds sets of node IDs that have line breaks before or after them. | ||
|
@@ -278,6 +280,14 @@ impl AtomCollection { | |
self.prepend(Atom::DeleteBegin, node, predicates); | ||
self.append(Atom::DeleteEnd, node, predicates); | ||
} | ||
"upper_case" => { | ||
self.prepend(Atom::CaseBegin(HowCapitalize::UpperCase), node, predicates); | ||
self.append(Atom::CaseEnd, node, predicates); | ||
} | ||
"lower_case" => { | ||
self.prepend(Atom::CaseBegin(HowCapitalize::LowerCase), node, predicates); | ||
self.append(Atom::CaseEnd, node, predicates); | ||
} | ||
// Scope manipulation | ||
"prepend_begin_scope" => { | ||
self.prepend( | ||
|
@@ -538,6 +548,7 @@ impl AtomCollection { | |
original_position: node.start_position().into(), | ||
single_line_no_indent: false, | ||
multi_line_indent_all: false, | ||
how_capitalize: HowCapitalize::Pass, | ||
}); | ||
// Mark all sub-nodes as having this node as a "leaf parent" | ||
self.mark_leaf_parent(node, node.id()); | ||
|
@@ -591,6 +602,7 @@ impl AtomCollection { | |
self.append.entry(target_node.id()).or_default().push(atom); | ||
} | ||
|
||
// fn capitalize(&mut self, | ||
/// Expands a softline atom to a hardline, space or empty atom depending on | ||
/// if we are in a multiline context or not. | ||
/// | ||
|
@@ -883,13 +895,71 @@ impl AtomCollection { | |
} | ||
} | ||
|
||
fn post_process_capitalization(&mut self) { | ||
let mut case_context: Vec<HowCapitalize> = Vec::new(); | ||
for atom in &mut self.atoms { | ||
match atom { | ||
Atom::CaseBegin(case) => { | ||
match case { | ||
HowCapitalize::UpperCase => { | ||
case_context.push(HowCapitalize::UpperCase); | ||
*atom = Atom::Empty; | ||
/* | ||
if lower_case_level > 0 { | ||
panic!("Cannot use @lower_case inside of a node captured by @upper_case!"); | ||
}*/ | ||
} | ||
HowCapitalize::LowerCase => { | ||
case_context.push(HowCapitalize::LowerCase); | ||
*atom = Atom::Empty; | ||
/* | ||
if upper_case_level > 0 { | ||
panic!("Cannot use @upper_case inside of a node captured by @lower_case!"); | ||
}*/ | ||
} | ||
_ => {} | ||
} | ||
} | ||
Atom::CaseEnd => { | ||
case_context.pop(); | ||
*atom = Atom::Empty; | ||
} | ||
_ => match atom { | ||
Atom::Leaf { | ||
content, | ||
id, | ||
original_position, | ||
single_line_no_indent, | ||
multi_line_indent_all, | ||
.. | ||
} => { | ||
// TODO don't be stupid with derefs | ||
*atom = Atom::Leaf { | ||
content: (*content).to_string(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the derefs here feel bad. I'm sure there's a rustier way to do this. |
||
id: *id, | ||
original_position: *original_position, | ||
single_line_no_indent: *single_line_no_indent, | ||
multi_line_indent_all: *multi_line_indent_all, | ||
how_capitalize: case_context | ||
.last() | ||
.unwrap_or(&HowCapitalize::Pass) | ||
.clone(), | ||
} | ||
} | ||
_ => {} | ||
}, | ||
} | ||
} | ||
} | ||
|
||
/// This function merges the spaces, new lines and blank lines. | ||
/// If there are several tokens of different kind one after the other, | ||
/// the blank line is kept over the new line which itself is kept over the space. | ||
/// Furthermore, this function put the indentation delimiters before any space/line atom. | ||
pub fn post_process(&mut self) { | ||
self.post_process_scopes(); | ||
self.post_process_deletes(); | ||
self.post_process_capitalization(); | ||
self.post_process_inner(); | ||
|
||
// We have taken care of spaces following an antispace. Now fix the | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is probably not ideal, but I wasn't sure what the preferred way of handling such errors is. There may also be times where users want to capitalize all but a subset of a node, which this would need to be changed to support.