Remove remaining ropey usages (#2076)

This commit is contained in:
Charlie Marsh
2023-01-21 18:24:10 -05:00
committed by GitHub
parent 4dcf284a04
commit 6bfa1804de
6 changed files with 18 additions and 64 deletions

View File

@@ -1,8 +1,6 @@
use std::borrow::Cow;
use std::collections::BTreeSet;
use itertools::Itertools;
use ropey::RopeBuilder;
use rustpython_ast::Location;
use crate::ast::types::Range;
@@ -14,10 +12,7 @@ pub mod fixer;
pub mod helpers;
/// Auto-fix errors in a file, and write the fixed source code to disk.
pub fn fix_file<'a>(
diagnostics: &'a [Diagnostic],
locator: &'a Locator<'a>,
) -> Option<(Cow<'a, str>, usize)> {
pub fn fix_file(diagnostics: &[Diagnostic], locator: &Locator) -> Option<(String, usize)> {
if diagnostics.iter().all(|check| check.fix.is_none()) {
return None;
}
@@ -32,8 +27,8 @@ pub fn fix_file<'a>(
fn apply_fixes<'a>(
fixes: impl Iterator<Item = &'a Fix>,
locator: &'a Locator<'a>,
) -> (Cow<'a, str>, usize) {
let mut output = RopeBuilder::new();
) -> (String, usize) {
let mut output = String::new();
let mut last_pos: Location = Location::new(1, 0);
let mut applied: BTreeSet<&Fix> = BTreeSet::default();
let mut num_fixed: usize = 0;
@@ -54,10 +49,10 @@ fn apply_fixes<'a>(
// Add all contents from `last_pos` to `fix.location`.
let slice = locator.slice_source_code_range(&Range::new(last_pos, fix.location));
output.append(slice);
output.push_str(slice);
// Add the patch itself.
output.append(&fix.content);
output.push_str(&fix.content);
// Track that the fix was applied.
last_pos = fix.end_location;
@@ -67,9 +62,9 @@ fn apply_fixes<'a>(
// Add the remaining content.
let slice = locator.slice_source_code_at(last_pos);
output.append(slice);
output.push_str(slice);
(Cow::from(output.finish()), num_fixed)
(output, num_fixed)
}
#[cfg(test)]

View File

@@ -63,9 +63,9 @@ pub struct Source {
impl Source {
pub fn from_diagnostic(diagnostic: &Diagnostic, locator: &Locator) -> Self {
let location = Location::new(diagnostic.location.row(), 0);
// Diagnostics can already extend one-past-the-end per Ropey's semantics. If
// they do, though, then they'll end at the start of a line. We need to
// avoid extending by yet another line past-the-end.
// Diagnostics can already extend one-past-the-end. If they do, though, then
// they'll end at the start of a line. We need to avoid extending by yet another
// line past-the-end.
let end_location = if diagnostic.end_location.column() == 0 {
diagnostic.end_location
} else {

View File

@@ -8,7 +8,6 @@ use comments::Comment;
use helpers::trailing_comma;
use itertools::Either::{Left, Right};
use itertools::Itertools;
use ropey::RopeBuilder;
use rustc_hash::FxHashMap;
use rustpython_ast::{Stmt, StmtKind};
use settings::RelatveImportsOrder;
@@ -593,7 +592,7 @@ pub fn format_imports(
extra_standard_library,
);
let mut output = RopeBuilder::new();
let mut output = String::new();
// Generate replacement source code.
let mut is_first_block = true;
@@ -630,14 +629,14 @@ pub fn format_imports(
if is_first_block {
is_first_block = false;
} else if !no_lines_before.contains(&import_type) {
output.append(stylist.line_ending());
output.push_str(stylist.line_ending());
}
let mut is_first_statement = true;
for import in imports {
match import {
Import((alias, comments)) => {
output.append(&format::format_import(
output.push_str(&format::format_import(
&alias,
&comments,
is_first_statement,
@@ -645,7 +644,7 @@ pub fn format_imports(
));
}
ImportFrom((import_from, comments, trailing_comma, aliases)) => {
output.append(&format::format_import_from(
output.push_str(&format::format_import_from(
&import_from,
&comments,
&aliases,
@@ -663,14 +662,14 @@ pub fn format_imports(
match trailer {
None => {}
Some(Trailer::Sibling) => {
output.append(stylist.line_ending());
output.push_str(stylist.line_ending());
}
Some(Trailer::FunctionDef | Trailer::ClassDef) => {
output.append(stylist.line_ending());
output.append(stylist.line_ending());
output.push_str(stylist.line_ending());
output.push_str(stylist.line_ending());
}
}
output.finish().to_string()
output
}
#[cfg(test)]