Skip to content
Merged
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
25 changes: 13 additions & 12 deletions crates/ruff_server/src/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,22 @@ pub(crate) fn fix_all(
) -> crate::Result<Fixes> {
let source_kind = query.make_source_kind();
let settings = query.settings();
let document_path = query.file_path();
let document_path = query.virtual_file_path();

// If the document is excluded, return an empty list of fixes.
let package = if let Some(document_path) = document_path.as_ref() {
if is_document_excluded_for_linting(
document_path,
&settings.file_resolver,
linter_settings,
query.text_document_language_id(),
) {
return Ok(Fixes::default());
}
if is_document_excluded_for_linting(
&document_path,
&settings.file_resolver,
linter_settings,
query.text_document_language_id(),
) {
return Ok(Fixes::default());
}

let file_path = query.file_path();
let package = if let Some(file_path) = &file_path {
detect_package_root(
document_path
file_path
.parent()
.expect("a path to a document should have a parent path"),
&linter_settings.namespace_packages,
Expand All @@ -65,7 +66,7 @@ pub(crate) fn fix_all(
result,
..
} = ruff_linter::linter::lint_fix(
&query.virtual_file_path(),
&document_path,
package,
flags::Noqa::Enabled,
settings.unsafe_fixes,
Expand Down
16 changes: 8 additions & 8 deletions crates/ruff_server/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ pub(crate) fn format(
document: &TextDocument,
source_type: PySourceType,
formatter_settings: &FormatterSettings,
path: Option<&Path>,
path: &Path,
) -> crate::Result<Option<String>> {
let format_options =
formatter_settings.to_format_options(source_type, document.contents(), path);
formatter_settings.to_format_options(source_type, document.contents(), Some(path));
match format_module_source(document.contents(), format_options) {
Ok(formatted) => {
let formatted = formatted.into_code();
Expand All @@ -40,10 +40,10 @@ pub(crate) fn format_range(
source_type: PySourceType,
formatter_settings: &FormatterSettings,
range: TextRange,
path: Option<&Path>,
path: &Path,
) -> crate::Result<Option<PrintedRange>> {
let format_options =
formatter_settings.to_format_options(source_type, document.contents(), path);
formatter_settings.to_format_options(source_type, document.contents(), Some(path));

match ruff_python_formatter::format_range(document.contents(), range, format_options) {
Ok(formatted) => {
Expand Down Expand Up @@ -97,7 +97,7 @@ with open("a_really_long_foo") as foo, open("a_really_long_bar") as bar, open("a
per_file_target_version,
..Default::default()
},
Some(Path::new("test.py")),
Path::new("test.py"),
)
.expect("Expected no errors when formatting")
.expect("Expected formatting changes");
Expand All @@ -119,7 +119,7 @@ with open("a_really_long_foo") as foo, open("a_really_long_bar") as bar, open("a
unresolved_target_version: PythonVersion::PY38,
..Default::default()
},
Some(Path::new("test.py")),
Path::new("test.py"),
)
.expect("Expected no errors when formatting")
.expect("Expected formatting changes");
Expand Down Expand Up @@ -167,7 +167,7 @@ sys.exit(
..Default::default()
},
range,
Some(Path::new("test.py")),
Path::new("test.py"),
)
.expect("Expected no errors when formatting")
.expect("Expected formatting changes");
Expand All @@ -190,7 +190,7 @@ sys.exit(
..Default::default()
},
range,
Some(Path::new("test.py")),
Path::new("test.py"),
)
.expect("Expected no errors when formatting")
.expect("Expected formatting changes");
Expand Down
46 changes: 19 additions & 27 deletions crates/ruff_server/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,37 +69,29 @@ pub(crate) fn check(
) -> DiagnosticsMap {
let source_kind = query.make_source_kind();
let settings = query.settings();
let document_path = query.file_path();
let document_path = query.virtual_file_path();

// If the document is excluded, return an empty list of diagnostics.
let package = if let Some(document_path) = document_path.as_ref() {
if is_document_excluded_for_linting(
document_path,
&settings.file_resolver,
&settings.linter,
query.text_document_language_id(),
) {
return DiagnosticsMap::default();
}
if is_document_excluded_for_linting(
&document_path,
&settings.file_resolver,
&settings.linter,
query.text_document_language_id(),
) {
return DiagnosticsMap::default();
}

detect_package_root(
document_path
.parent()
.expect("a path to a document should have a parent path"),
&settings.linter.namespace_packages,
)
.map(PackageRoot::root)
} else {
None
};
let package = detect_package_root(
document_path
.parent()
.expect("a path to a document should have a parent path"),
&settings.linter.namespace_packages,
)
.map(PackageRoot::root);

let source_type = query.source_type();

let target_version = if let Some(path) = &document_path {
settings.linter.resolve_target_version(path)
} else {
settings.linter.unresolved_target_version
};
let target_version = settings.linter.resolve_target_version(&document_path);

let parse_options =
ParseOptions::from(source_type).with_target_version(target_version.parser_version());
Expand All @@ -123,7 +115,7 @@ pub(crate) fn check(

// Generate checks.
let diagnostics = check_path(
&query.virtual_file_path(),
&document_path,
package,
&locator,
&stylist,
Expand All @@ -138,7 +130,7 @@ pub(crate) fn check(
);

let noqa_edits = generate_noqa_edits(
&query.virtual_file_path(),
&document_path,
&diagnostics,
&locator,
indexer.comment_ranges(),
Expand Down
20 changes: 9 additions & 11 deletions crates/ruff_server/src/server/api/requests/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,26 +83,24 @@ fn format_text_document(
is_notebook: bool,
) -> Result<super::FormatResponse> {
let settings = query.settings();
let file_path = query.virtual_file_path();

// If the document is excluded, return early.
let file_path = query.file_path();
if let Some(file_path) = &file_path {
if is_document_excluded_for_formatting(
file_path,
&settings.file_resolver,
&settings.formatter,
text_document.language_id(),
) {
return Ok(None);
}
if is_document_excluded_for_formatting(
&file_path,
&settings.file_resolver,
&settings.formatter,
text_document.language_id(),
) {
return Ok(None);
}

let source = text_document.contents();
let formatted = crate::format::format(
text_document,
query.source_type(),
&settings.formatter,
file_path.as_deref(),
&file_path,
)
.with_failure_code(lsp_server::ErrorCode::InternalError)?;
let Some(mut formatted) = formatted else {
Expand Down
20 changes: 9 additions & 11 deletions crates/ruff_server/src/server/api/requests/format_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,16 @@ fn format_text_document_range(
encoding: PositionEncoding,
) -> Result<super::FormatResponse> {
let settings = query.settings();
let file_path = query.virtual_file_path();

// If the document is excluded, return early.
let file_path = query.file_path();
if let Some(file_path) = &file_path {
if is_document_excluded_for_formatting(
file_path,
&settings.file_resolver,
&settings.formatter,
text_document.language_id(),
) {
return Ok(None);
}
if is_document_excluded_for_formatting(
&file_path,
&settings.file_resolver,
&settings.formatter,
text_document.language_id(),
) {
return Ok(None);
}

let text = text_document.contents();
Expand All @@ -69,7 +67,7 @@ fn format_text_document_range(
query.source_type(),
&settings.formatter,
range,
file_path.as_deref(),
&file_path,
)
.with_failure_code(lsp_server::ErrorCode::InternalError)?;

Expand Down
Loading