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

ruff server respects per-file-ignores configuration #11224

Merged
merged 1 commit into from
May 2, 2024
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
19 changes: 16 additions & 3 deletions crates/ruff_server/src/fix.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand All @@ -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<Vec<lsp_types::TextEdit>> {
let source = document.contents();

let document_path = document_url
.to_file_path()
.expect("document URL should be a valid file path");
snowsignal marked this conversation as resolved.
Show resolved Hide resolved

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
Expand All @@ -35,8 +48,8 @@ pub(crate) fn fix_all(
result: LinterResult { error, .. },
..
} = ruff_linter::linter::lint_fix(
Path::new("<filename>"),
None,
&document_path,
package,
flags::Noqa::Enabled,
UnsafeFixes::Disabled,
linter_settings,
Expand Down
19 changes: 15 additions & 4 deletions crates/ruff_server/src/lint.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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<lsp_types::Diagnostic> {
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
Expand All @@ -71,8 +82,8 @@ pub(crate) fn check(
data: (diagnostics, _imports),
..
} = check_path(
Path::new("<filename>"),
None,
&document_path,
package,
&locator,
&stylist,
&indexer,
Expand Down
1 change: 1 addition & 0 deletions crates/ruff_server/src/server/api/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub(super) fn generate_diagnostics(snapshot: &DocumentSnapshot) -> Vec<lsp_types
if snapshot.client_settings().lint() {
crate::lint::check(
snapshot.document(),
snapshot.url(),
snapshot.settings().linter(),
snapshot.encoding(),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,18 @@ pub(super) fn resolve_edit_for_fix_all(
tracker.set_edits_for_document(
url.clone(),
version,
fix_all_edit(document, linter_settings, encoding)?,
fix_all_edit(document, url, linter_settings, encoding)?,
)?;
Ok(tracker.into_workspace_edit())
}

pub(super) fn fix_all_edit(
document: &crate::edit::Document,
document_url: &types::Url,
linter_settings: &LinterSettings,
encoding: PositionEncoding,
) -> crate::Result<Vec<types::TextEdit>> {
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(
Expand All @@ -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<Vec<types::TextEdit>> {
Expand All @@ -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)
}
2 changes: 2 additions & 0 deletions crates/ruff_server/src/server/api/requests/execute_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)
Expand All @@ -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(),
)
Expand Down
Loading