mirror of
https://github.com/astral-sh/ruff
synced 2026-01-22 05:51:03 -05:00
## Summary This PR removes the cyclic dev dependency some of the crates had with the parser crate. The cyclic dependencies are: * `ruff_python_ast` has a **dev dependency** on `ruff_python_parser` and `ruff_python_parser` directly depends on `ruff_python_ast` * `ruff_python_trivia` has a **dev dependency** on `ruff_python_parser` and `ruff_python_parser` has an indirect dependency on `ruff_python_trivia` (`ruff_python_parser` - `ruff_python_ast` - `ruff_python_trivia`) Specifically, this PR does the following: * Introduce two new crates * `ruff_python_ast_integration_tests` and move the tests from the `ruff_python_ast` crate which uses the parser in this crate * `ruff_python_trivia_integration_tests` and move the tests from the `ruff_python_trivia` crate which uses the parser in this crate ### Motivation The main motivation for this PR is to help development. Before this PR, `rust-analyzer` wouldn't provide any intellisense in the `ruff_python_parser` crate regarding the symbols in `ruff_python_ast` crate. ``` [ERROR][2024-05-03 13:47:06] .../vim/lsp/rpc.lua:770 "rpc" "/Users/dhruv/.cargo/bin/rust-analyzer" "stderr" "[ERROR project_model::workspace] cyclic deps: ruff_python_parser(Idx::<CrateData>(50)) -> ruff_python_ast(Idx::<CrateData>(37)), alternative path: ruff_python_ast(Idx::<CrateData>(37)) -> ruff_python_parser(Idx::<CrateData>(50))\n" ``` ## Test Plan Check the logs of `rust-analyzer` to not see any signs of cyclic dependency.
82 lines
2.8 KiB
Rust
82 lines
2.8 KiB
Rust
use ruff_source_file::Locator;
|
|
use ruff_text_size::{TextRange, TextSize};
|
|
|
|
/// Extract the leading indentation from a line.
|
|
pub fn indentation_at_offset<'a>(offset: TextSize, locator: &'a Locator) -> Option<&'a str> {
|
|
let line_start = locator.line_start(offset);
|
|
let indentation = locator.slice(TextRange::new(line_start, offset));
|
|
|
|
if indentation.chars().all(is_python_whitespace) {
|
|
Some(indentation)
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
|
|
/// Return `true` if the node starting the given [`TextSize`] has leading content.
|
|
pub fn has_leading_content(offset: TextSize, locator: &Locator) -> bool {
|
|
let line_start = locator.line_start(offset);
|
|
let leading = locator.slice(TextRange::new(line_start, offset));
|
|
leading.chars().any(|char| !is_python_whitespace(char))
|
|
}
|
|
|
|
/// Return `true` if the node ending at the given [`TextSize`] has trailing content.
|
|
pub fn has_trailing_content(offset: TextSize, locator: &Locator) -> bool {
|
|
let line_end = locator.line_end(offset);
|
|
let trailing = locator.slice(TextRange::new(offset, line_end));
|
|
|
|
for char in trailing.chars() {
|
|
if char == '#' {
|
|
return false;
|
|
}
|
|
if !is_python_whitespace(char) {
|
|
return true;
|
|
}
|
|
}
|
|
false
|
|
}
|
|
|
|
/// Returns `true` for [whitespace](https://docs.python.org/3/reference/lexical_analysis.html#whitespace-between-tokens)
|
|
/// characters.
|
|
pub const fn is_python_whitespace(c: char) -> bool {
|
|
matches!(
|
|
c,
|
|
// Space, tab, or form-feed
|
|
' ' | '\t' | '\x0C'
|
|
)
|
|
}
|
|
|
|
/// Extract the leading indentation from a line.
|
|
pub fn leading_indentation(line: &str) -> &str {
|
|
line.find(|char: char| !is_python_whitespace(char))
|
|
.map_or(line, |index| &line[..index])
|
|
}
|
|
|
|
pub trait PythonWhitespace {
|
|
/// Like `str::trim()`, but only removes whitespace characters that Python considers
|
|
/// to be [whitespace](https://docs.python.org/3/reference/lexical_analysis.html#whitespace-between-tokens).
|
|
fn trim_whitespace(&self) -> &Self;
|
|
|
|
/// Like `str::trim_start()`, but only removes whitespace characters that Python considers
|
|
/// to be [whitespace](https://docs.python.org/3/reference/lexical_analysis.html#whitespace-between-tokens).
|
|
fn trim_whitespace_start(&self) -> &Self;
|
|
|
|
/// Like `str::trim_end()`, but only removes whitespace characters that Python considers
|
|
/// to be [whitespace](https://docs.python.org/3/reference/lexical_analysis.html#whitespace-between-tokens).
|
|
fn trim_whitespace_end(&self) -> &Self;
|
|
}
|
|
|
|
impl PythonWhitespace for str {
|
|
fn trim_whitespace(&self) -> &Self {
|
|
self.trim_matches(is_python_whitespace)
|
|
}
|
|
|
|
fn trim_whitespace_start(&self) -> &Self {
|
|
self.trim_start_matches(is_python_whitespace)
|
|
}
|
|
|
|
fn trim_whitespace_end(&self) -> &Self {
|
|
self.trim_end_matches(is_python_whitespace)
|
|
}
|
|
}
|