Skip to content

Commit

Permalink
Add support for extensionless Python files for server (#13326)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvmanila committed Sep 11, 2024
1 parent eded78a commit b72d49b
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 4 deletions.
2 changes: 1 addition & 1 deletion crates/ruff_server/src/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use lsp_types::{PositionEncodingKind, Url};
pub use notebook::NotebookDocument;
pub(crate) use range::{NotebookRange, RangeExt, ToRangeExt};
pub(crate) use replacement::Replacement;
pub(crate) use text_document::DocumentVersion;
pub use text_document::TextDocument;
pub(crate) use text_document::{DocumentVersion, LanguageId};

use crate::{fix::Fixes, session::ResolvedClientCapabilities};

Expand Down
28 changes: 28 additions & 0 deletions crates/ruff_server/src/edit/text_document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ pub struct TextDocument {
/// The latest version of the document, set by the LSP client. The server will panic in
/// debug mode if we attempt to update the document with an 'older' version.
version: DocumentVersion,
/// The language ID of the document as provided by the client.
language_id: Option<LanguageId>,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum LanguageId {
Python,
Other,
}

impl From<&str> for LanguageId {
fn from(language_id: &str) -> Self {
match language_id {
"python" => Self::Python,
_ => Self::Other,
}
}
}

impl TextDocument {
Expand All @@ -29,9 +46,16 @@ impl TextDocument {
contents,
index,
version,
language_id: None,
}
}

#[must_use]
pub fn with_language_id(mut self, language_id: &str) -> Self {
self.language_id = Some(LanguageId::from(language_id));
self
}

pub fn into_contents(self) -> String {
self.contents
}
Expand All @@ -48,6 +72,10 @@ impl TextDocument {
self.version
}

pub fn language_id(&self) -> Option<LanguageId> {
self.language_id
}

pub fn apply_changes(
&mut self,
changes: Vec<lsp_types::TextDocumentContentChangeEvent>,
Expand Down
1 change: 1 addition & 0 deletions crates/ruff_server/src/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub(crate) fn fix_all(
file_resolver_settings,
Some(linter_settings),
None,
query.text_document_language_id(),
) {
return Ok(Fixes::default());
}
Expand Down
1 change: 1 addition & 0 deletions crates/ruff_server/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub(crate) fn check(
file_resolver_settings,
Some(linter_settings),
None,
query.text_document_language_id(),
) {
return DiagnosticsMap::default();
}
Expand Down
11 changes: 10 additions & 1 deletion crates/ruff_server/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use ruff_linter::settings::LinterSettings;
use ruff_workspace::resolver::{match_any_exclusion, match_any_inclusion};
use ruff_workspace::{FileResolverSettings, FormatterSettings};

use crate::edit::LanguageId;

/// Return `true` if the document at the given [`Path`] should be excluded.
///
/// The tool-specific settings should be provided if the request for the document is specific to
Expand All @@ -19,6 +21,7 @@ pub(crate) fn is_document_excluded(
resolver_settings: &FileResolverSettings,
linter_settings: Option<&LinterSettings>,
formatter_settings: Option<&FormatterSettings>,
language_id: Option<LanguageId>,
) -> bool {
if let Some(exclusion) = match_any_exclusion(
path,
Expand All @@ -38,8 +41,14 @@ pub(crate) fn is_document_excluded(
) {
tracing::debug!("Included path via `{}`: {}", inclusion, path.display());
false
} else if let Some(LanguageId::Python) = language_id {
tracing::debug!("Included path via Python language ID: {}", path.display());
false
} else {
// Path is excluded by not being in the inclusion set.
tracing::debug!(
"Ignored path as it's not in the inclusion set: {}",
path.display()
);
true
}
}
7 changes: 5 additions & 2 deletions crates/ruff_server/src/server/api/notifications/did_open.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ impl super::SyncNotificationHandler for DidOpen {
types::DidOpenTextDocumentParams {
text_document:
types::TextDocumentItem {
uri, text, version, ..
uri,
text,
version,
language_id,
},
}: types::DidOpenTextDocumentParams,
) -> Result<()> {
let document = TextDocument::new(text, version);
let document = TextDocument::new(text, version).with_language_id(&language_id);

session.open_text_document(uri.clone(), document);

Expand Down
1 change: 1 addition & 0 deletions crates/ruff_server/src/server/api/requests/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ fn format_text_document(
file_resolver_settings,
None,
Some(formatter_settings),
text_document.language_id(),
) {
return Ok(None);
}
Expand Down
1 change: 1 addition & 0 deletions crates/ruff_server/src/server/api/requests/format_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ fn format_text_document_range(
file_resolver_settings,
None,
Some(formatter_settings),
text_document.language_id(),
) {
return Ok(None);
}
Expand Down
9 changes: 9 additions & 0 deletions crates/ruff_server/src/session/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use rustc_hash::FxHashMap;

pub(crate) use ruff_settings::RuffSettings;

use crate::edit::LanguageId;
use crate::{
edit::{DocumentKey, DocumentVersion, NotebookDocument},
PositionEncoding, TextDocument,
Expand Down Expand Up @@ -603,4 +604,12 @@ impl DocumentQuery {
.and_then(|cell_uri| notebook.cell_document_by_uri(cell_uri)),
}
}

pub(crate) fn text_document_language_id(&self) -> Option<LanguageId> {
if let DocumentQuery::Text { document, .. } = self {
document.language_id()
} else {
None
}
}
}

0 comments on commit b72d49b

Please sign in to comment.