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

[red-knot] Integrate formatter #11215

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

[red-knot] Integrate formatter #11215

wants to merge 2 commits into from

Conversation

MichaReiser
Copy link
Member

Summary

This is a first hacky approach to integrate the formatter into red-knot.

There are plenty of TODOs. The main questions is where and when we should write the formatted content and how we avoid that formatting requires two-passes for the cache to be "hot".

Test Plan

I ran red knot and it formated my files (all lints gone, whoops)

@MichaReiser MichaReiser added the internal An internal refactor or improvement label Apr 30, 2024
@MichaReiser MichaReiser changed the base branch from main to files-snapshot April 30, 2024 15:00
@MichaReiser MichaReiser changed the base branch from files-snapshot to main April 30, 2024 15:15
@MichaReiser MichaReiser marked this pull request as draft April 30, 2024 15:15
Copy link
Contributor

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

Comment on lines +162 to +164
if !program.is_cancelled() {
let _ = program.format();
}
Copy link
Member Author

Choose a reason for hiding this comment

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

That's a bit hacked in. We can also decide to remove it for now. But it does show that formatting after linting is possible (but requires a &mut Program)

@MichaReiser MichaReiser marked this pull request as ready for review April 30, 2024 16:06
@MichaReiser MichaReiser requested a review from carljm May 1, 2024 07:11

/// Checks if the file is correctly formatted. It creates a diagnostic for formatting issues.
#[tracing::instrument(level = "trace", skip(db))]
pub(crate) fn check_formatted<Db>(db: &Db, file_id: FileId) -> Result<Diagnostics, FormatError>
Copy link
Contributor

Choose a reason for hiding this comment

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

for consistency?

Suggested change
pub(crate) fn check_formatted<Db>(db: &Db, file_id: FileId) -> Result<Diagnostics, FormatError>
pub(crate) fn check_file_formatted<Db>(db: &Db, file_id: FileId) -> Result<Diagnostics, FormatError>

Ok(FormattedFile::Formatted(content)) => {
let path = self.file_path(file);

// TODO: This is problematic because it immediately re-triggers the file watcher.
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems like mostly this is a good thing? The file content has changed, the rest of the system should react accordingly, as it normally would. I don't think we should special-case re-formatting in the rest of our invalidation -- it's a file change like any other.

The only place where this seems undesirable is that you would like to track that a file that we just formatted is still formatted; we don't have to reformat it. So I would scope any special-casing to this specific point only, contained within the formatter logic. Maybe we could keep a cache mapping the hash of known-well-formatted contents for a file, so when we get updated sources for a file from the file watcher, we can check if the hash of the new contents matches an entry in this cache, and if so, directly mark it as already well-formatted?

}

fn check_file_formatted(&self, file_id: FileId) -> Result<Diagnostics, FormatError> {
check_formatted(self, file_id)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
check_formatted(self, file_id)
check_file_formatted(self, file_id)

@MichaReiser
Copy link
Member Author

MichaReiser commented May 10, 2024

I still plan to land this PR eventually but aren't prioritising it right now. Deciding on a query system and exploring persistent caching is of higher importance right now and the prototype itself was enough for me to learn a lot about the challenge when it comes to integrate the formatter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
internal An internal refactor or improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants