Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(grit): add auto-wrap + integrate with search command #3288

Merged
merged 7 commits into from
Jul 8, 2024

Conversation

arendjr
Copy link
Contributor

@arendjr arendjr commented Jun 25, 2024

Summary

This implements the first iteration of the biome search command:

small_steps

Changes in this PR:

  • Completed more Grit bindings
  • Implemented auto-wrap
  • Added SearchCapabilities to our file features system.

Note that diagnostics are still rough in some places, and plenty of features are still unsupported.

Test Plan

Tested manually.

@github-actions github-actions bot added A-CLI Area: CLI A-Project Area: project A-Core Area: core A-Parser Area: parser A-Formatter Area: formatter L-JavaScript Language: JavaScript and super languages L-CSS Language: CSS L-JSON Language: JSON and super languages labels Jun 25, 2024
@github-actions github-actions bot added the A-Diagnostic Area: diagnostocis label Jul 2, 2024
@arendjr arendjr marked this pull request as ready for review July 2, 2024 20:48
@arendjr arendjr requested a review from a team July 2, 2024 20:48
@arendjr arendjr changed the title Add auto-wrap with Step + integrate with search command chore(grit): add auto-wrap + integrate with search command Jul 2, 2024
Copy link

codspeed-hq bot commented Jul 2, 2024

CodSpeed Performance Report

Merging #3288 will degrade performances by 7.88%

Comparing arendjr:small-steps (c5cdc08) with arendjr:small-steps (dcfb43d)

Summary

⚡ 1 improvements
❌ 1 regressions
✅ 106 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark arendjr:small-steps arendjr:small-steps Change
dojo_11880045762646467684.js[cached] 8.6 ms 8 ms +6.83%
db_2930068967297060348.json[cached] 12.5 ms 13.6 ms -7.88%

Copy link
Member

@ematipico ematipico left a comment

Choose a reason for hiding this comment

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

Great! We are almost here :)

I believe there's some work to be done around diagnostics and the usage of anyhow. Not sure when they should be addressed, but really should at some point. Other than that, I just left few nitpicks.


for f in new_files {
let Some(file) = f.get_file() else {
bail!("Expected a list of files")
Copy link
Member

Choose a reason for hiding this comment

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

Internally, we don't use anyhow, so I wonder if you plan to change this later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd like to, but it would require updating the Grit libraries too. I agree it's less than ideal now.

Copy link
Member

Choose a reason for hiding this comment

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

I believe this is a fix that we should follow up upstream. anyhow isn't meant for libraries but for applications, so I suppose we can open an issue.

Comment on lines 178 to 198
let name: PathBuf = file
.name(&state.files)
.text(&state.files, &self.lang)
.unwrap()
.as_ref()
.into();
let body = file
.body(&state.files)
.text(&state.files, &self.lang)
.unwrap();
let owned_file =
new_file_owner(name.clone(), &body, &self.lang, logs)?.ok_or_else(|| {
anyhow!(
"failed to construct new file for file {}",
name.to_string_lossy()
)
})?;
self.files().push(owned_file);
let _ = state.files.push_new_file(self.files().last().unwrap());
}

Copy link
Member

Choose a reason for hiding this comment

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

Can you replace the unwrap with a safer alternative or provide a // SAFETY comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed in 240455b.

Comment on lines +92 to +96
match error.diagnostics.first() {
Some(diag) => {
fmt.write_str(": ")?;
diag.message(fmt)
}
Copy link
Member

Choose a reason for hiding this comment

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

This approach may not be correct. We are basically truncating diagnostics. I think we should revisit this part.

Also, diagnostics are marked as vector of SerializableDiagnostic, but they should not, theoretically. We want to use serialisable diagnostics only in particular cases; for example, we want to cross thread boundaries inside the workspace.

This means, the transformation must happen at the workspace level, like in this case:

.map(|diag| {
let diag = diag.with_file_path(params.path.as_path().display().to_string());
SerdeDiagnostic::new(diag)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed the part about SerializableDiagnostic in dcfb43d. I'll try to revisit the diagnostics further indeed, though I hope to push that to a follow-up PR. There's some even worse feedback issues still too 😅

crates/biome_grit_patterns/src/errors.rs Show resolved Hide resolved
@@ -341,6 +343,30 @@ pub enum SearchError {
)]
pub struct InvalidPattern;

#[derive(Debug, Serialize, Deserialize)]
pub struct QueryError(pub String);
Copy link
Member

Choose a reason for hiding this comment

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

nit: QueryDiagnostic

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed in 240455b.

fn verbose_advices(&self, visitor: &mut dyn Visit) -> std::io::Result<()> {
visitor.record_log(
LogCategory::Info,
&markup! { "For reference, please consult: https://docs.grit.io/language/syntax" },
Copy link
Member

Choose a reason for hiding this comment

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

You can use <Hyperlink href="">please consult the official grit syntax page.</Hyperlink> to create a clickable link.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed in 240455b.

Comment on lines 358 to 360
fn message(&self, fmt: &mut biome_console::fmt::Formatter<'_>) -> std::io::Result<()> {
fmt.write_markup(markup! { "Error executing the Grit query: "{{&self.0}} })
}
Copy link
Member

Choose a reason for hiding this comment

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

Since this diagnostic is inside the service, it will likely be used by other clients, too, e.g., LSP. Hence, we should implement description, too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed in 240455b.

Comment on lines +476 to +480
&BiomePath,
&DocumentFileSource,
AnyParse,
&GritQuery,
WorkspaceSettingsHandle,
Copy link
Member

Choose a reason for hiding this comment

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

nit: Considering the great number of arguments, we could evaluate a struct instead

@@ -584,6 +601,34 @@ pub(crate) fn parse_lang_from_script_opening_tag(script_opening_tag: &str) -> La
.map_or(Language::JavaScript, |lang| lang)
}

pub(crate) fn search_file(
Copy link
Member

Choose a reason for hiding this comment

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

nit: I would call it search, without the use of file. While it feels correct now, it won't be once this feature will be wired to additional clients.

It will be inline with the format capabilities. I suppose we will have search_range too in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed in 240455b.

@@ -851,3 +847,9 @@ impl Workspace for WorkspaceServer {
fn is_dir(path: &Path) -> bool {
path.is_dir() || (path.is_symlink() && fs::read_link(path).is_ok_and(|path| path.is_dir()))
}

fn make_search_pattern_id() -> String {
Copy link
Member

Choose a reason for hiding this comment

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

What's this for?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For searching, the workspace protocol is split into three functions: parse_pattern(), search_pattern(), and drop_pattern(). The reason for this is that we only need to parse once, and all workers can search using the pattern by referencing a handle instead of parsing again. This function generates the IDs for these "handles".

@arendjr arendjr merged commit a285781 into biomejs:main Jul 8, 2024
12 checks passed
@Conaclos Conaclos added the A-Changelog Area: changelog label Sep 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Changelog Area: changelog A-CLI Area: CLI A-Core Area: core A-Diagnostic Area: diagnostocis A-Formatter Area: formatter A-Parser Area: parser A-Project Area: project L-CSS Language: CSS L-JavaScript Language: JavaScript and super languages L-JSON Language: JSON and super languages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants