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

17
Cargo.lock generated
View File

@@ -1826,16 +1826,6 @@ dependencies = [
"winapi",
]
[[package]]
name = "ropey"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbd22239fafefc42138ca5da064f3c17726a80d2379d817a3521240e78dd0064"
dependencies = [
"smallvec",
"str_indices",
]
[[package]]
name = "ruff"
version = "0.0.229"
@@ -1868,7 +1858,6 @@ dependencies = [
"once_cell",
"path-absolutize",
"regex",
"ropey",
"ruff_macros",
"rustc-hash",
"rustpython-ast",
@@ -2256,12 +2245,6 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]]
name = "str_indices"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f026164926842ec52deb1938fae44f83dfdb82d0a5b0270c5bd5935ab74d6dd"
[[package]]
name = "string_cache"
version = "0.8.4"

View File

@@ -46,7 +46,6 @@ num-traits = "0.2.15"
once_cell = { version = "1.16.0" }
path-absolutize = { version = "3.0.14", features = ["once_cell_cache", "use_unix_paths_on_wasm"] }
regex = { version = "1.6.0" }
ropey = { version = "1.5.0", features = ["cr_lines", "simd"], default-features = false }
ruff_macros = { version = "0.0.229", path = "ruff_macros" }
rustc-hash = { version = "1.1.0" }
rustpython-ast = { features = ["unparse"], git = "https://github.com/RustPython/RustPython.git", rev = "62aa942bf506ea3d41ed0503b947b84141fdaa3c" }
@@ -98,7 +97,3 @@ opt-level = 3
# https://github.com/bytecodealliance/wasm-tools/blob/b5c3d98e40590512a3b12470ef358d5c7b983b15/crates/wasmparser/src/limits.rs#L29
[profile.dev.package.rustpython-parser]
opt-level = 1
[[bench]]
name = "source_code_locator"
harness = false

View File

@@ -1,18 +0,0 @@
use std::fs;
use std::path::Path;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use ropey::Rope;
fn criterion_benchmark(c: &mut Criterion) {
let contents = fs::read_to_string(Path::new("resources/test/fixtures/D.py")).unwrap();
c.bench_function("rope", |b| {
b.iter(|| {
let rope = Rope::from_str(black_box(&contents));
rope.line_to_char(black_box(4));
});
});
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);

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)]