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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
use crate::FullTextDocument;
use lsp_types::{
notification::{
DidChangeTextDocument, DidCloseTextDocument, DidOpenTextDocument, Notification,
},
DidChangeTextDocumentParams, DidCloseTextDocumentParams, DidOpenTextDocumentParams, PositionEncodingKind,
Range, Uri,
};
use serde_json::Value;
use std::collections::BTreeMap;
pub struct TextDocuments {
documents: BTreeMap<Uri, FullTextDocument>,
default_encoding: PositionEncodingKind,
}
impl Default for TextDocuments {
fn default() -> Self {
Self::with_encoding(PositionEncodingKind::UTF16)
}
}
impl TextDocuments {
/// Create a text documents
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// use lsp_textdocument::TextDocuments;
///
/// let text_documents = TextDocuments::new();
/// ```
pub fn new() -> Self {
Self::with_encoding(PositionEncodingKind::UTF16)
}
/// Create a TextDocuments instance with a specific position encoding
///
/// This method allows you to specify the position encoding used for character positions
/// in text documents. The encoding determines how character offsets are calculated and is
/// important for proper LSP communication between client and server.
///
/// # Arguments
///
/// * `default_encoding` - The position encoding to use. Can be UTF-8, UTF-16, or UTF-32.
///
/// # Position Encodings
///
/// - **UTF-16**: The default encoding for backward compatibility with LSP 3.16 and earlier.
/// Each UTF-16 code unit counts as one position unit.
/// - **UTF-8**: Each byte counts as one position unit. More efficient for ASCII-heavy text.
/// - **UTF-32**: Each Unicode code point counts as one position unit.
///
/// The encoding should match what was negotiated with the LSP client during initialization.
///
/// # Examples
///
/// Basic usage with UTF-16 (default):
///
/// ```
/// use lsp_textdocument::TextDocuments;
/// use lsp_types::PositionEncodingKind;
///
/// let text_documents = TextDocuments::with_encoding(PositionEncodingKind::UTF16);
/// ```
///
/// Using UTF-8 encoding for better performance with ASCII text:
///
/// ```
/// use lsp_textdocument::TextDocuments;
/// use lsp_types::PositionEncodingKind;
///
/// let text_documents = TextDocuments::with_encoding(PositionEncodingKind::UTF8);
/// ```
///
/// Using UTF-32 encoding where each Unicode code point is one unit:
///
/// ```
/// use lsp_textdocument::TextDocuments;
/// use lsp_types::PositionEncodingKind;
///
/// let text_documents = TextDocuments::with_encoding(PositionEncodingKind::UTF32);
/// ```
pub fn with_encoding(default_encoding: PositionEncodingKind) -> Self {
Self {
documents: BTreeMap::new(),
default_encoding,
}
}
#[allow(clippy::mutable_key_type)]
// `Uri` (url::Url) implements interior mutability APIs, but we never mutate keys after
// insertion, and map operations rely on its stable ordering. Suppress the lint here.
pub fn documents(&self) -> &BTreeMap<Uri, FullTextDocument> {
&self.documents
}
/// Returns the default position encoding used for newly created documents.
///
/// This is useful for checking which encoding was configured or negotiated
/// (for example, during server initialization) when this `TextDocuments`
/// instance was created.
pub fn default_encoding(&self) -> PositionEncodingKind {
self.default_encoding.clone()
}
/// Get specify document by giving Uri
///
/// # Examples:
///
/// Basic usage:
/// ```
/// use lsp_textdocument::TextDocuments;
/// use lsp_types::Uri;
///
/// let text_documents = TextDocuments::new();
/// let uri:Uri = "file://example.txt".parse().unwrap();
/// text_documents.get_document(&uri);
/// ```
pub fn get_document(&self, uri: &Uri) -> Option<&FullTextDocument> {
self.documents.get(uri)
}
/// Get specify document content by giving Range
///
/// # Examples
///
/// Basic usage:
/// ```no_run
/// use lsp_textdocument::TextDocuments;
/// use lsp_types::{Uri, Range, Position};
///
/// let uri: Uri = "file://example.txt".parse().unwrap();
/// let text_documents = TextDocuments::new();
///
/// // get document all content
/// let content = text_documents.get_document_content(&uri, None);
/// assert_eq!(content, Some("hello rust!"));
///
/// // get document specify content by range
/// let (start, end) = (Position::new(0, 1), Position::new(0, 9));
/// let range = Range::new(start, end);
/// let sub_content = text_documents.get_document_content(&uri, Some(range));
/// assert_eq!(sub_content, Some("ello rus"));
/// ```
pub fn get_document_content(&self, uri: &Uri, range: Option<Range>) -> Option<&str> {
self.documents
.get(uri)
.map(|document| document.get_content(range))
}
/// Get specify document's language by giving Uri
///
/// # Examples
///
/// Basic usage:
/// ```no_run
/// use lsp_textdocument::TextDocuments;
/// use lsp_types::Uri;
///
/// let text_documents = TextDocuments::new();
/// let uri:Uri = "file://example.js".parse().unwrap();
/// let language = text_documents.get_document_language(&uri);
/// assert_eq!(language, Some("javascript"));
/// ```
pub fn get_document_language(&self, uri: &Uri) -> Option<&str> {
self.documents
.get(uri)
.map(|document| document.language_id())
}
/// Listening the notification from client, you just need to pass `method` and `params`
///
/// # Examples:
///
/// Basic usage:
/// ```no_run
/// use lsp_textdocument::TextDocuments;
///
/// let method = "textDocument/didOpen";
/// let params = serde_json::to_value("message produced by client").unwrap();
///
/// let mut text_documents = TextDocuments::new();
/// let accept: bool = text_documents.listen(method, ¶ms);
/// ```
pub fn listen(&mut self, method: &str, params: &Value) -> bool {
match method {
DidOpenTextDocument::METHOD => {
let params: DidOpenTextDocumentParams = serde_json::from_value(params.clone())
.expect("Expect receive DidOpenTextDocumentParams");
let text_document = params.text_document;
let document = FullTextDocument::new_with_encoding(
text_document.language_id,
text_document.version,
text_document.text,
// use default encoding negotiated at server init
self.default_encoding.clone(),
);
self.documents.insert(text_document.uri, document);
true
}
DidChangeTextDocument::METHOD => {
let params: DidChangeTextDocumentParams = serde_json::from_value(params.clone())
.expect("Expect receive DidChangeTextDocumentParams");
if let Some(document) = self.documents.get_mut(¶ms.text_document.uri) {
let changes = ¶ms.content_changes;
let version = params.text_document.version;
document.update(changes, version);
};
true
}
DidCloseTextDocument::METHOD => {
let params: DidCloseTextDocumentParams = serde_json::from_value(params.clone())
.expect("Expect receive DidCloseTextDocumentParams");
self.documents.remove(¶ms.text_document.uri);
true
}
_ => {
// ignore other request
false
}
}
}
}