Skip to content

Commit

Permalink
fix: handle multi-line strings
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Jan 25, 2024
1 parent f80e359 commit 8940cdd
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 14 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ jobs:
if: matrix.config.kind == 'test_release'
run: cargo test --release

# integration test
- name: Wasm integration test - Setup
if: matrix.config.kind == 'test_release'
run: |
echo '{ "plugins": ["./target/wasm32-unknown-unknown/release/dprint_plugin_toml.wasm"] }' >> dprint.test.json
- name: Wasm integration test - Run
if: matrix.config.kind == 'test_release'
uses: dprint/[email protected]
with:
config-path: dprint.test.json
- name: Wasm integration test - Cleanup
if: matrix.config.kind == 'test_release'
run: rm dprint.test.json

- name: Get tag version
if: matrix.config.kind == 'test_release' && startsWith(github.ref, 'refs/tags/')
id: get_tag_version
Expand Down
2 changes: 1 addition & 1 deletion src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ fn sort_children(children: Vec<NodeWithLeadingTrivia>, cmp: &impl Fn(&SyntaxNode
child_groups.into_iter().flatten().collect()
}

fn sort_group(child_group: &mut Vec<NodeWithLeadingTrivia>, cmp: &impl Fn(&SyntaxNode, &SyntaxNode) -> Ordering) {
fn sort_group(child_group: &mut [NodeWithLeadingTrivia], cmp: &impl Fn(&SyntaxNode, &SyntaxNode) -> Ordering) {
// remove the first item's trivia to get the group trivia
let group_trivia: Vec<_> = child_group.first_mut().unwrap().trivia.drain(..).collect();
let previous_first_item_index = child_group.first().unwrap().node.index();
Expand Down
6 changes: 2 additions & 4 deletions src/configuration/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use super::*;
/// .line_width(80)
/// .build();
/// ```
#[derive(Default)]
pub struct ConfigurationBuilder {
pub(super) config: ConfigKeyMap,
global_config: Option<GlobalConfiguration>,
Expand All @@ -24,10 +25,7 @@ pub struct ConfigurationBuilder {
impl ConfigurationBuilder {
/// Constructs a new configuration builder.
pub fn new() -> ConfigurationBuilder {
ConfigurationBuilder {
config: ConfigKeyMap::new(),
global_config: None,
}
ConfigurationBuilder::default()
}

/// Gets the final configuration that can be used to format a file.
Expand Down
1 change: 1 addition & 0 deletions src/configuration/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod builder;
#[allow(clippy::module_inception)]
mod configuration;
mod resolve_config;

Expand Down
2 changes: 1 addition & 1 deletion src/format_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn parse_and_process_node(file_path: &Path, text: &str, config: &Configuration)
fn parse_taplo(text: &str) -> Result<SyntaxNode> {
let parse_result = taplo::parser::parse(text);

if let Some(err) = parse_result.errors.get(0) {
if let Some(err) = parse_result.errors.first() {
bail!(
"{}",
dprint_core::formatting::utils::string_utils::format_diagnostic(Some((err.range.start().into(), err.range.end().into())), &err.message, text,)
Expand Down
21 changes: 14 additions & 7 deletions src/generation/generate.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::needless_lifetimes)]

use dprint_core::formatting::conditions::*;
use dprint_core::formatting::ir_helpers::SingleLineOptions;
use dprint_core::formatting::*;
Expand Down Expand Up @@ -60,14 +62,20 @@ fn gen_node_with_inner<'a>(node: SyntaxElement, context: &mut Context<'a>, inner
},
NodeOrToken::Token(token) => match token.kind() {
SyntaxKind::COMMENT => Ok(gen_comment(token, context)),
_ => Ok(ir_helpers::gen_from_string(token.text().trim().into())),
SyntaxKind::MULTI_LINE_STRING | SyntaxKind::MULTI_LINE_STRING_LITERAL => {
let mut items = PrintItems::new();
items.push_str("");
items.extend(ir_helpers::gen_from_raw_string(token.text().trim()));
Ok(items)
}
_ => Ok(ir_helpers::gen_from_string(token.text().trim())),
},
};

items.extend(inner_parse(
match result {
Ok(items) => items,
Err(()) => ir_helpers::gen_from_raw_string_trim_line_ends(node.text().trim().into()),
Err(()) => ir_helpers::gen_from_raw_string_trim_line_ends(node.text().trim()),
},
context,
));
Expand Down Expand Up @@ -182,7 +190,7 @@ fn gen_inline_table<'a>(node: SyntaxNode, context: &mut Context<'a>) -> PrintIte
// the comment seems to be stored as the last child of an inline table, so check for it here
if let Some(NodeOrToken::Token(token)) = node.children_with_tokens().last() {
if token.kind() == SyntaxKind::COMMENT {
items.extend(gen_comment(token.into(), context));
items.extend(gen_comment(token, context));
}
}

Expand Down Expand Up @@ -348,7 +356,7 @@ fn gen_comma_separated_value<'a>(value: SyntaxElement, generated_comma: PrintIte
let generated_comma = generated_comma.into_rc_path();
items.extend(gen_node_with_inner(value, context, move |mut items, _| {
// this Rc clone is necessary because we can't move the captured generated_comma out of this closure
items.push_optional_path(generated_comma.clone());
items.push_optional_path(generated_comma);
items
}));

Expand Down Expand Up @@ -386,7 +394,7 @@ fn gen_comment<'a>(comment: SyntaxToken, context: &mut Context<'a>) -> PrintItem
let mut text = "#".repeat(info.leading_hashes_count);
if !after_hash_text.is_empty() {
if !info.has_leading_whitespace {
text.push_str(" ");
text.push(' ');
}
text.push_str(after_hash_text);
}
Expand All @@ -405,10 +413,9 @@ struct CommentTextInfo {
}

fn get_comment_text_info(text: &str) -> CommentTextInfo {
let mut chars = text.chars();
let mut leading_hashes_count = 0;
let mut has_leading_whitespace = false;
while let Some(c) = chars.next() {
for c in text.chars() {
match c {
'#' => leading_hashes_count += 1,
' ' | '\t' => {
Expand Down
2 changes: 1 addition & 1 deletion src/rowan_extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl SyntaxElementExtensions for SyntaxElement {

fn start_including_leading_comments(&self) -> usize {
let result = self.get_comments_on_previous_lines();
if let Some(comment) = result.get(0) {
if let Some(comment) = result.first() {
comment.text_range().start().into()
} else {
self.text_range().start().into()
Expand Down
29 changes: 29 additions & 0 deletions tests/specs/Issues/Issue0010.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
== should format multi-line strings (only) ==
strings = [
"""
This is
a
multi-line
string
""",
'''This is
a multi-line literal string


''',
]

[expect]
strings = [
"""
This is
a
multi-line
string
""",
'''This is
a multi-line literal string


''',
]

0 comments on commit 8940cdd

Please sign in to comment.