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
This commit is contained in:
Dhruv Manilawala 2024-09-12 00:35:26 +05:30 committed by GitHub
parent eded78a39b
commit b72d49be16
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 57 additions and 4 deletions

View file

@ -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};

View file

@ -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 {
@ -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
}
@ -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>,

View file

@ -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());
}

View file

@ -77,6 +77,7 @@ pub(crate) fn check(
file_resolver_settings,
Some(linter_settings),
None,
query.text_document_language_id(),
) {
return DiagnosticsMap::default();
}

View file

@ -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
@ -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,
@ -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
}
}

View file

@ -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);

View file

@ -90,6 +90,7 @@ fn format_text_document(
file_resolver_settings,
None,
Some(formatter_settings),
text_document.language_id(),
) {
return Ok(None);
}

View file

@ -54,6 +54,7 @@ fn format_text_document_range(
file_resolver_settings,
None,
Some(formatter_settings),
text_document.language_id(),
) {
return Ok(None);
}

View file

@ -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,
@ -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
}
}
}