Add per-file-target-version option (#16257)

## Summary

This PR is another step in preparing to detect syntax errors in the
parser. It introduces the new `per-file-target-version` top-level
configuration option, which holds a mapping of compiled glob patterns to
Python versions. I intend to use the
`LinterSettings::resolve_target_version` method here to pass to the
parser:


f50849aeef/crates/ruff_linter/src/linter.rs (L491-L493)

## Test Plan

I added two new CLI tests to show that the `per-file-target-version` is
respected in both the formatter and the linter.
This commit is contained in:
Brent Westbrook 2025-02-24 08:47:13 -05:00 committed by GitHub
parent 42a5f5ef6a
commit e7a6c19e3a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
78 changed files with 820 additions and 274 deletions

View file

@ -85,9 +85,10 @@ fn format_text_document(
let settings = query.settings();
// If the document is excluded, return early.
if let Some(file_path) = query.file_path() {
let file_path = query.file_path();
if let Some(file_path) = &file_path {
if is_document_excluded_for_formatting(
&file_path,
file_path,
&settings.file_resolver,
&settings.formatter,
text_document.language_id(),
@ -97,8 +98,13 @@ fn format_text_document(
}
let source = text_document.contents();
let formatted = crate::format::format(text_document, query.source_type(), &settings.formatter)
.with_failure_code(lsp_server::ErrorCode::InternalError)?;
let formatted = crate::format::format(
text_document,
query.source_type(),
&settings.formatter,
file_path.as_deref(),
)
.with_failure_code(lsp_server::ErrorCode::InternalError)?;
let Some(mut formatted) = formatted else {
return Ok(None);
};

View file

@ -49,9 +49,10 @@ fn format_text_document_range(
let settings = query.settings();
// If the document is excluded, return early.
if let Some(file_path) = query.file_path() {
let file_path = query.file_path();
if let Some(file_path) = &file_path {
if is_document_excluded_for_formatting(
&file_path,
file_path,
&settings.file_resolver,
&settings.formatter,
text_document.language_id(),
@ -68,6 +69,7 @@ fn format_text_document_range(
query.source_type(),
&settings.formatter,
range,
file_path.as_deref(),
)
.with_failure_code(lsp_server::ErrorCode::InternalError)?;