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

Implement exclude and default-excludes TOML options #213

Merged
merged 17 commits into from
Feb 10, 2025
Merged

Conversation

DavisVaughan
Copy link
Collaborator

Closes #128

This PR adds support for including and excluding files from the formatter using .gitignore style patterns. There are 2 new options you can set in air.toml:

  • exclude, i.e. exclude = ["renv/", "my-generated-file.R"]
  • default-excludes, a boolean, defaults to true

Air now comes pre-loaded with a set of default files and folders to exclude, I will call them out inline below. If you'd like to format these files, you can set default-excludes = false, but I imagine this will be extremely rare.

The option name

I know we talked about naming this option ignore to match .gitignore, but both cargo and ruff have both include and exclude as TOML options which has a very nice symmetry in the naming scheme. Internally we have switched to also using an include option that mirrors exclude. In the future I think it is possible we will want to expose include, it would be very easy to do so, particularly if we ever support Rmd or Qmd documents natively.

gitignore syntax

Using .gitignore as the syntax rather than "standard" unix globs is quite nice. Mostly it gets us the ability to specify "file.R" as an equivalent to "**/file.R", and folder/ as an equivalent to **/folder/, which is likely a high proportion of real use cases. This is all implemented using the ignore crate, which I've learned cargo itself also uses for the exact same purposes as us.

CLI

In the CLI, our parallel file discovery walker has gained the ability to include or exclude files based on these glob patterns. This ends up working really well, and is the exact same use case that ripgrep has, so everything wires up quite nicely. Since we walk down the directory tree, if we see that foo/renv/ is a directory to exclude, we never even walk into that directory, we just skip it entirely.

LSP

The LSP is different from the CLI because we aren't discovering files, we are provided a full file path and are asked if we should format that particular file or not. This means that for foo/renv/activate.R, we need to check foo/, foo/renv/ and foo/renv/activate.R to see if any of those paths are excluded by a glob. Luckily ignore has a method for this, but it is worth keeping in mind how different this is than the CLI method.

@DavisVaughan DavisVaughan marked this pull request as draft February 8, 2025 19:43
…nts()` method

`matched_path_or_any_parents()` assumes you have provided a `root`, and it enforces this assumption by requiring that, after root stripping, `path` cannot contain root-like components, including a leading `/` on Unix and leading `C:/` on Windows. This is a reasonable restriction for ignore, but we've asserted that all of our patterns are root-agnostic by requiring `**/`, so we have to have a custom implementation that throws this check away.
@DavisVaughan DavisVaughan marked this pull request as ready for review February 10, 2025 19:58
@DavisVaughan DavisVaughan merged commit 092bc58 into main Feb 10, 2025
4 checks passed
@DavisVaughan DavisVaughan deleted the feature/exclude branch February 10, 2025 20:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a default set of files / folders to ignore
2 participants