Skip to content

Commit

Permalink
Use Line::end to get end byte of lines
Browse files Browse the repository at this point in the history
Hopefully fixes issue on Windows
  • Loading branch information
ZedThree committed Feb 3, 2025
1 parent e854b37 commit 09a66b9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
9 changes: 4 additions & 5 deletions fortitude/src/rules/style/line_length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use crate::TextRule;
use lazy_regex::regex_is_match;
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_source_file::OneIndexed;
use ruff_source_file::SourceFile;
use ruff_source_file::UniversalNewlines;
use ruff_text_size::{TextLen, TextRange, TextSize};

/// ## What does it do?
Expand Down Expand Up @@ -48,14 +48,14 @@ impl TextRule for LineTooLong {
let source = source_file.to_source_code();
let max_length = settings.line_length;
let mut violations = Vec::new();
for (idx, line) in source.text().lines().enumerate() {
for line in source.text().universal_newlines() {
// Note: Can't use string.len(), as that gives byte length, not char length
let actual_length = line.chars().count();
if actual_length > max_length {
// Are we ending on a string or comment? If so, we'll allow it through, as it may
// contain something like a long URL that cannot be reasonably split across multiple
// lines.
if regex_is_match!(r#"(["']\w*&?$)|(!.*$)|(^\w*&)"#, line) {
if regex_is_match!(r#"(["']\w*&?$)|(!.*$)|(^\w*&)"#, line.as_str()) {
continue;
}
// Get the byte range from the first character that oversteps the limit
Expand All @@ -66,8 +66,7 @@ impl TextRule for LineTooLong {
.take(actual_length - max_length)
.map(TextLen::text_len)
.sum();
let line_end = source.line_end_exclusive(OneIndexed::from_zero_indexed(idx));
let range = TextRange::new(line_end - extra_bytes, line_end);
let range = TextRange::new(line.end() - extra_bytes, line.end());
violations.push(Diagnostic::new(
Self {
max_length,
Expand Down
7 changes: 3 additions & 4 deletions fortitude/src/rules/style/whitespace.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// Defines rules that enforce widely accepted whitespace rules.
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, violation};
use ruff_source_file::{OneIndexed, SourceFile};
use ruff_source_file::{SourceFile, UniversalNewlines};
use ruff_text_size::{TextLen, TextRange, TextSize};
use tree_sitter::Node;

Expand Down Expand Up @@ -33,16 +33,15 @@ impl TextRule for TrailingWhitespace {
fn check(_settings: &Settings, source_file: &SourceFile) -> Vec<Diagnostic> {
let source = source_file.to_source_code();
let mut violations = Vec::new();
for (idx, line) in source.text().lines().enumerate() {
for line in source.text().universal_newlines() {
let whitespace_bytes: TextSize = line
.chars()
.rev()
.take_while(|c| c.is_whitespace())
.map(TextLen::text_len)
.sum();
if whitespace_bytes > 0.into() {
let line_end_byte = source.line_end_exclusive(OneIndexed::from_zero_indexed(idx));
let range = TextRange::new(line_end_byte - whitespace_bytes, line_end_byte);
let range = TextRange::new(line.end() - whitespace_bytes, line.end());
let edit = Edit::range_deletion(range);
violations.push(Diagnostic::new(Self {}, range).with_fix(Fix::safe_edit(edit)));
}
Expand Down

0 comments on commit 09a66b9

Please sign in to comment.