Skip to content

Commit b72d49b

Browse files
authored
Add support for extensionless Python files for server (#13326)
## Summary Closes: #12539 ## Test Plan https://github.com/user-attachments/assets/e49b2669-6f12-4684-9e45-a3321b19b659
1 parent eded78a commit b72d49b

File tree

9 files changed

+57
-4
lines changed

9 files changed

+57
-4
lines changed

crates/ruff_server/src/edit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use lsp_types::{PositionEncodingKind, Url};
1111
pub use notebook::NotebookDocument;
1212
pub(crate) use range::{NotebookRange, RangeExt, ToRangeExt};
1313
pub(crate) use replacement::Replacement;
14-
pub(crate) use text_document::DocumentVersion;
1514
pub use text_document::TextDocument;
15+
pub(crate) use text_document::{DocumentVersion, LanguageId};
1616

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

crates/ruff_server/src/edit/text_document.rs

+28
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,23 @@ pub struct TextDocument {
2020
/// The latest version of the document, set by the LSP client. The server will panic in
2121
/// debug mode if we attempt to update the document with an 'older' version.
2222
version: DocumentVersion,
23+
/// The language ID of the document as provided by the client.
24+
language_id: Option<LanguageId>,
25+
}
26+
27+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
28+
pub enum LanguageId {
29+
Python,
30+
Other,
31+
}
32+
33+
impl From<&str> for LanguageId {
34+
fn from(language_id: &str) -> Self {
35+
match language_id {
36+
"python" => Self::Python,
37+
_ => Self::Other,
38+
}
39+
}
2340
}
2441

2542
impl TextDocument {
@@ -29,9 +46,16 @@ impl TextDocument {
2946
contents,
3047
index,
3148
version,
49+
language_id: None,
3250
}
3351
}
3452

53+
#[must_use]
54+
pub fn with_language_id(mut self, language_id: &str) -> Self {
55+
self.language_id = Some(LanguageId::from(language_id));
56+
self
57+
}
58+
3559
pub fn into_contents(self) -> String {
3660
self.contents
3761
}
@@ -48,6 +72,10 @@ impl TextDocument {
4872
self.version
4973
}
5074

75+
pub fn language_id(&self) -> Option<LanguageId> {
76+
self.language_id
77+
}
78+
5179
pub fn apply_changes(
5280
&mut self,
5381
changes: Vec<lsp_types::TextDocumentContentChangeEvent>,

crates/ruff_server/src/fix.rs

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub(crate) fn fix_all(
3838
file_resolver_settings,
3939
Some(linter_settings),
4040
None,
41+
query.text_document_language_id(),
4142
) {
4243
return Ok(Fixes::default());
4344
}

crates/ruff_server/src/lint.rs

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ pub(crate) fn check(
7777
file_resolver_settings,
7878
Some(linter_settings),
7979
None,
80+
query.text_document_language_id(),
8081
) {
8182
return DiagnosticsMap::default();
8283
}

crates/ruff_server/src/resolve.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use ruff_linter::settings::LinterSettings;
44
use ruff_workspace::resolver::{match_any_exclusion, match_any_inclusion};
55
use ruff_workspace::{FileResolverSettings, FormatterSettings};
66

7+
use crate::edit::LanguageId;
8+
79
/// Return `true` if the document at the given [`Path`] should be excluded.
810
///
911
/// The tool-specific settings should be provided if the request for the document is specific to
@@ -19,6 +21,7 @@ pub(crate) fn is_document_excluded(
1921
resolver_settings: &FileResolverSettings,
2022
linter_settings: Option<&LinterSettings>,
2123
formatter_settings: Option<&FormatterSettings>,
24+
language_id: Option<LanguageId>,
2225
) -> bool {
2326
if let Some(exclusion) = match_any_exclusion(
2427
path,
@@ -38,8 +41,14 @@ pub(crate) fn is_document_excluded(
3841
) {
3942
tracing::debug!("Included path via `{}`: {}", inclusion, path.display());
4043
false
44+
} else if let Some(LanguageId::Python) = language_id {
45+
tracing::debug!("Included path via Python language ID: {}", path.display());
46+
false
4147
} else {
42-
// Path is excluded by not being in the inclusion set.
48+
tracing::debug!(
49+
"Ignored path as it's not in the inclusion set: {}",
50+
path.display()
51+
);
4352
true
4453
}
4554
}

crates/ruff_server/src/server/api/notifications/did_open.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@ impl super::SyncNotificationHandler for DidOpen {
2121
types::DidOpenTextDocumentParams {
2222
text_document:
2323
types::TextDocumentItem {
24-
uri, text, version, ..
24+
uri,
25+
text,
26+
version,
27+
language_id,
2528
},
2629
}: types::DidOpenTextDocumentParams,
2730
) -> Result<()> {
28-
let document = TextDocument::new(text, version);
31+
let document = TextDocument::new(text, version).with_language_id(&language_id);
2932

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

crates/ruff_server/src/server/api/requests/format.rs

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ fn format_text_document(
9090
file_resolver_settings,
9191
None,
9292
Some(formatter_settings),
93+
text_document.language_id(),
9394
) {
9495
return Ok(None);
9596
}

crates/ruff_server/src/server/api/requests/format_range.rs

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ fn format_text_document_range(
5454
file_resolver_settings,
5555
None,
5656
Some(formatter_settings),
57+
text_document.language_id(),
5758
) {
5859
return Ok(None);
5960
}

crates/ruff_server/src/session/index.rs

+9
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use rustc_hash::FxHashMap;
99

1010
pub(crate) use ruff_settings::RuffSettings;
1111

12+
use crate::edit::LanguageId;
1213
use crate::{
1314
edit::{DocumentKey, DocumentVersion, NotebookDocument},
1415
PositionEncoding, TextDocument,
@@ -603,4 +604,12 @@ impl DocumentQuery {
603604
.and_then(|cell_uri| notebook.cell_document_by_uri(cell_uri)),
604605
}
605606
}
607+
608+
pub(crate) fn text_document_language_id(&self) -> Option<LanguageId> {
609+
if let DocumentQuery::Text { document, .. } = self {
610+
document.language_id()
611+
} else {
612+
None
613+
}
614+
}
606615
}

0 commit comments

Comments
 (0)