diff --git a/crates/ruff_server/src/fix.rs b/crates/ruff_server/src/fix.rs index fa5607f972228..f94e8c138610b 100644 --- a/crates/ruff_server/src/fix.rs +++ b/crates/ruff_server/src/fix.rs @@ -1,11 +1,12 @@ use ruff_linter::{ linter::{FixerResult, LinterResult}, + packaging::detect_package_root, settings::{flags, types::UnsafeFixes, LinterSettings}, source_kind::SourceKind, }; use ruff_python_ast::PySourceType; use ruff_source_file::LineIndex; -use std::{borrow::Cow, path::Path}; +use std::borrow::Cow; use crate::{ edit::{Replacement, ToRangeExt}, @@ -14,11 +15,23 @@ use crate::{ pub(crate) fn fix_all( document: &crate::edit::Document, + document_url: &lsp_types::Url, linter_settings: &LinterSettings, encoding: PositionEncoding, ) -> crate::Result> { let source = document.contents(); + let document_path = document_url + .to_file_path() + .expect("document URL should be a valid file path"); + + let package = detect_package_root( + document_path + .parent() + .expect("a path to a document should have a parent path"), + &linter_settings.namespace_packages, + ); + let source_type = PySourceType::default(); // TODO(jane): Support Jupyter Notebooks @@ -35,8 +48,8 @@ pub(crate) fn fix_all( result: LinterResult { error, .. }, .. } = ruff_linter::linter::lint_fix( - Path::new(""), - None, + &document_path, + package, flags::Noqa::Enabled, UnsafeFixes::Disabled, linter_settings, diff --git a/crates/ruff_server/src/lint.rs b/crates/ruff_server/src/lint.rs index 355460913f1fb..02475ad0b7b0a 100644 --- a/crates/ruff_server/src/lint.rs +++ b/crates/ruff_server/src/lint.rs @@ -1,11 +1,10 @@ //! Access to the Ruff linting API for the LSP -use std::path::Path; - use ruff_diagnostics::{Applicability, Diagnostic, DiagnosticKind, Fix}; use ruff_linter::{ directives::{extract_directives, Flags}, linter::{check_path, LinterResult, TokenSource}, + packaging::detect_package_root, registry::AsRule, settings::{flags, LinterSettings}, source_kind::SourceKind, @@ -40,12 +39,24 @@ pub(crate) struct DiagnosticFix { pub(crate) fn check( document: &crate::edit::Document, + document_url: &lsp_types::Url, linter_settings: &LinterSettings, encoding: PositionEncoding, ) -> Vec { let contents = document.contents(); let index = document.index().clone(); + let document_path = document_url + .to_file_path() + .expect("document URL should be a valid file path"); + + let package = detect_package_root( + document_path + .parent() + .expect("a path to a document should have a parent path"), + &linter_settings.namespace_packages, + ); + let source_type = PySourceType::default(); // TODO(jane): Support Jupyter Notebooks @@ -71,8 +82,8 @@ pub(crate) fn check( data: (diagnostics, _imports), .. } = check_path( - Path::new(""), - None, + &document_path, + package, &locator, &stylist, &indexer, diff --git a/crates/ruff_server/src/server/api/diagnostics.rs b/crates/ruff_server/src/server/api/diagnostics.rs index ecd153aa5fd84..ba08cdf776969 100644 --- a/crates/ruff_server/src/server/api/diagnostics.rs +++ b/crates/ruff_server/src/server/api/diagnostics.rs @@ -6,6 +6,7 @@ pub(super) fn generate_diagnostics(snapshot: &DocumentSnapshot) -> Vec crate::Result> { - crate::fix::fix_all(document, linter_settings, encoding) + crate::fix::fix_all(document, document_url, linter_settings, encoding) } pub(super) fn resolve_edit_for_organize_imports( @@ -120,13 +121,14 @@ pub(super) fn resolve_edit_for_organize_imports( tracker.set_edits_for_document( url.clone(), version, - organize_imports_edit(document, linter_settings, encoding)?, + organize_imports_edit(document, url, linter_settings, encoding)?, )?; Ok(tracker.into_workspace_edit()) } pub(super) fn organize_imports_edit( document: &crate::edit::Document, + document_url: &types::Url, linter_settings: &LinterSettings, encoding: PositionEncoding, ) -> crate::Result> { @@ -138,5 +140,5 @@ pub(super) fn organize_imports_edit( .into_iter() .collect(); - crate::fix::fix_all(document, &linter_settings, encoding) + crate::fix::fix_all(document, document_url, &linter_settings, encoding) } diff --git a/crates/ruff_server/src/server/api/requests/execute_command.rs b/crates/ruff_server/src/server/api/requests/execute_command.rs index 4db24eb869fc3..6e2933a44a119 100644 --- a/crates/ruff_server/src/server/api/requests/execute_command.rs +++ b/crates/ruff_server/src/server/api/requests/execute_command.rs @@ -64,6 +64,7 @@ impl super::SyncRequestHandler for ExecuteCommand { Command::FixAll => { let edits = super::code_action_resolve::fix_all_edit( snapshot.document(), + snapshot.url(), snapshot.settings().linter(), snapshot.encoding(), ) @@ -83,6 +84,7 @@ impl super::SyncRequestHandler for ExecuteCommand { Command::OrganizeImports => { let edits = super::code_action_resolve::organize_imports_edit( snapshot.document(), + snapshot.url(), snapshot.settings().linter(), snapshot.encoding(), )