Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ expression: redactor(content)

```astro
---

---
<div></div>
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,8 @@ file.astro:2:1 lint/suspicious/noDebugger FIXABLE ━━━━━━━━━
i Unsafe fix: Remove debugger statement
1 │ -
2 │ - debugger;
1 │ +
3 2 │
1 │ debugger;
│ ---------
Copy link
Copy Markdown
Member Author

@Conaclos Conaclos May 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the bug regarding the code fixes that I mentioned in the PR description.

```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ debugger;

```block
---

---
<div></div>
```
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,14 @@ file.astro:2:1 assist/source/organizeImports FIXABLE ━━━━━━━━

i Safe fix: Organize imports and exports (Biome)

1 1 │
2 │ - import·{·getLocale·}·from·"astro:i18n";
3 │ - import·{·Code·}·from·"astro:components";
2 │ + import·{·Code·}·from·"astro:components";
3 │ + import·{·getLocale·}·from·"astro:i18n";
4 4 │ import "style.css";
5 │ - import·{·B,·A·}·from·"./util.js";
5 │ + import·{·A,·B·}·from·"./util.js";
6 6 │
1 │ - import·{·getLocale·}·from·"astro:i18n";
2 │ - import·{·Code·}·from·"astro:components";
1 │ + import·{·Code·}·from·"astro:components";
2 │ + import·{·getLocale·}·from·"astro:i18n";
3 3 │ import "style.css";
4 │ - import·{·B,·A·}·from·"./util.js";
4 │ + import·{·A,·B·}·from·"./util.js";
5 5 │


```
Expand Down
29 changes: 18 additions & 11 deletions crates/biome_service/src/file_handlers/astro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ use biome_js_parser::{JsParserOptions, parse_js_with_cache};
use biome_js_syntax::{JsFileSource, TextRange, TextSize};
use biome_parser::AnyParse;
use biome_rowan::NodeCache;
use regex::{Matches, Regex, RegexBuilder};
use core::ops::Range;
use regex::{Regex, RegexBuilder};
use std::sync::LazyLock;

use super::SearchCapabilities;

#[derive(Debug, Default, PartialEq, Eq)]
pub struct AstroFileHandler;

pub static ASTRO_FENCE: LazyLock<Regex> = LazyLock::new(|| {
static ASTRO_FENCE: LazyLock<Regex> = LazyLock::new(|| {
RegexBuilder::new(r#"^---\s*$"#)
.multi_line(true)
.build()
Expand All @@ -34,30 +35,36 @@ impl AstroFileHandler {
pub fn input(text: &str) -> &str {
let mut matches = Self::matches(text);
match (matches.next(), matches.next()) {
(Some(start), Some(end)) => &text[start.end()..end.start()],
(Some(start), Some(end)) => &text[start.end..end.start],
_ => "",
}
}

/// Returns the start byte offset of the Astro fence
/// Returns the start byte offset after the Astro fence
pub fn start(input: &str) -> Option<u32> {
ASTRO_FENCE.find_iter(input).next().map(|m| m.end() as u32)
Self::matches(input).next().map(|m| m.end as u32)
}

fn matches(input: &str) -> Matches<'_, '_> {
ASTRO_FENCE.find_iter(input)
fn matches(input: &str) -> impl Iterator<Item = Range<usize>> {
ASTRO_FENCE.find_iter(input).map(|m| {
if input[m.end()..].starts_with('\n') {
m.start()..(m.end() + 1)
} else {
m.start()..m.end()
}
})
}

/// It takes the original content of an Astro file, and new output of an Astro file. The output is only the content contained inside the
/// Astro fences. The function replaces `output` inside those fences.
pub fn output(input: &str, output: &str) -> String {
let mut matches = Self::matches(input);
if let (Some(start), Some(end)) = (matches.next(), matches.next()) {
if let (Some(prefix), Some(suffix)) = (matches.next(), matches.next()) {
format!(
"{}{}{}",
&input[..start.end() + 1],
output.trim_start(),
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This trim_start can remove too many spaces.

&input[end.start()..]
&input[..prefix.end],
output,
&input[suffix.start..],
)
} else {
input.to_string()
Expand Down
Loading