Repository navigation
Expand file tree
/
Copy pathedit.rs
More file actions
193 lines (173 loc) · 6.69 KB
/
Copy pathedit.rs
File metadata and controls
193 lines (173 loc) · 6.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//! Types and utilities for working with text, modifying source files, and `Ruff <-> LSP` type conversion.
mod notebook;
mod range;
mod replacement;
mod text_document;
use std::collections::HashMap;
use lsp_types::{DocumentChange, PositionEncodingKind, TextDocumentIdentifier, Uri};
pub(crate) use notebook::NotebookDocument;
pub(crate) use range::{NotebookRange, RangeExt, ToRangeExt};
pub(crate) use replacement::Replacement;
pub use text_document::TextDocument;
pub(crate) use text_document::{DocumentVersion, LanguageId};
use crate::{fix::Fixes, session::ResolvedClientCapabilities};
/// A convenient enumeration for supported text encodings. Can be converted to [`lsp_types::PositionEncodingKind`].
// Please maintain the order from least to greatest priority for the derived `Ord` impl.
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum PositionEncoding {
/// UTF 16 is the encoding supported by all LSP clients.
#[default]
UTF16,
/// Second choice because UTF32 uses a fixed 4 byte encoding for each character (makes conversion relatively easy)
UTF32,
/// Ruff's preferred encoding
UTF8,
}
impl From<PositionEncoding> for ruff_source_file::PositionEncoding {
fn from(value: PositionEncoding) -> Self {
match value {
PositionEncoding::UTF8 => Self::Utf8,
PositionEncoding::UTF16 => Self::Utf16,
PositionEncoding::UTF32 => Self::Utf32,
}
}
}
/// A unique document ID, derived from a URI passed as part of an LSP request.
/// This document ID can point to either be a standalone Python file, a full notebook, or a cell within a notebook.
#[derive(Clone, Debug)]
pub(crate) enum DocumentKey {
Notebook(Uri),
NotebookCell(Uri),
Text(Uri),
}
impl DocumentKey {
/// Converts the key back into its original URI.
pub(crate) fn into_uri(self) -> Uri {
match self {
DocumentKey::NotebookCell(uri)
| DocumentKey::Notebook(uri)
| DocumentKey::Text(uri) => uri,
}
}
}
impl std::fmt::Display for DocumentKey {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::NotebookCell(uri) | Self::Notebook(uri) | Self::Text(uri) => uri.fmt(f),
}
}
}
/// Tracks multi-document edits to eventually merge into a `WorkspaceEdit`.
/// Compatible with clients that don't support `workspace.workspaceEdit.documentChanges`.
#[derive(Debug)]
pub(crate) enum WorkspaceEditTracker {
DocumentChanges(Vec<lsp_types::TextDocumentEdit>),
Changes(HashMap<Uri, Vec<lsp_types::TextEdit>>),
}
impl From<PositionEncoding> for lsp_types::PositionEncodingKind {
fn from(value: PositionEncoding) -> Self {
match value {
PositionEncoding::UTF8 => lsp_types::PositionEncodingKind::UTF8,
PositionEncoding::UTF16 => lsp_types::PositionEncodingKind::UTF16,
PositionEncoding::UTF32 => lsp_types::PositionEncodingKind::UTF32,
}
}
}
impl TryFrom<&lsp_types::PositionEncodingKind> for PositionEncoding {
type Error = ();
fn try_from(value: &PositionEncodingKind) -> Result<Self, Self::Error> {
Ok(if value == &PositionEncodingKind::UTF8 {
PositionEncoding::UTF8
} else if value == &PositionEncodingKind::UTF16 {
PositionEncoding::UTF16
} else if value == &PositionEncodingKind::UTF32 {
PositionEncoding::UTF32
} else {
return Err(());
})
}
}
impl WorkspaceEditTracker {
pub(crate) fn new(client_capabilities: &ResolvedClientCapabilities) -> Self {
if client_capabilities.document_changes {
Self::DocumentChanges(Vec::default())
} else {
Self::Changes(HashMap::default())
}
}
/// Sets a series of [`Fixes`] for a text or notebook document.
pub(crate) fn set_fixes_for_document(
&mut self,
fixes: Fixes,
version: DocumentVersion,
) -> crate::Result<()> {
#[expect(
clippy::iter_over_hash_type,
reason = "edits for distinct document URIs are independent"
)]
for (uri, edits) in fixes {
self.set_edits_for_document(uri, version, edits)?;
}
Ok(())
}
/// Sets the edits made to a specific document. This should only be called
/// once for each document `uri`, and will fail if this is called for the same `uri`
/// multiple times.
pub(crate) fn set_edits_for_document(
&mut self,
uri: Uri,
_version: DocumentVersion,
edits: Vec<lsp_types::TextEdit>,
) -> crate::Result<()> {
match self {
Self::DocumentChanges(document_edits) => {
if document_edits
.iter()
.any(|document| document.text_document.text_document_identifier.uri == uri)
{
return Err(anyhow::anyhow!(
"Attempted to add edits for a document that was already edited"
));
}
document_edits.push(lsp_types::TextDocumentEdit {
text_document: lsp_types::OptionalVersionedTextDocumentIdentifier {
text_document_identifier: TextDocumentIdentifier { uri },
// TODO(jane): Re-enable versioned edits after investigating whether it could work with notebook cells
version: None,
},
edits: edits.into_iter().map(lsp_types::Edit::TextEdit).collect(),
});
Ok(())
}
Self::Changes(changes) => {
if changes.get(&uri).is_some() {
return Err(anyhow::anyhow!(
"Attempted to add edits for a document that was already edited"
));
}
changes.insert(uri, edits);
Ok(())
}
}
}
pub(crate) fn is_empty(&self) -> bool {
match self {
Self::DocumentChanges(document_edits) => document_edits.is_empty(),
Self::Changes(changes) => changes.is_empty(),
}
}
pub(crate) fn into_workspace_edit(self) -> lsp_types::WorkspaceEdit {
match self {
Self::DocumentChanges(document_edits) => lsp_types::WorkspaceEdit {
document_changes: Some(
document_edits
.into_iter()
.map(DocumentChange::TextDocumentEdit)
.collect(),
),
..Default::default()
},
Self::Changes(changes) => lsp_types::WorkspaceEdit::new(Some(changes), None, None),
}
}
}