Skip to content

Commit dac87ff

Browse files
authored
Split get_document_or_error() from get_document() (#313)
1 parent 7b3c81b commit dac87ff

File tree

4 files changed

+22
-16
lines changed

4 files changed

+22
-16
lines changed

crates/lsp/src/handlers_ext.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub(crate) enum ViewFileKind {
2323
}
2424

2525
pub(crate) fn view_file(params: ViewFileParams, state: &WorldState) -> anyhow::Result<String> {
26-
let doc = state.get_document(&params.text_document.uri)?;
26+
let doc = state.get_document_or_error(&params.text_document.uri)?;
2727

2828
match params.kind {
2929
ViewFileKind::TreeSitter => {

crates/lsp/src/handlers_format.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub(crate) fn document_formatting(
2323
state: &WorldState,
2424
) -> anyhow::Result<Option<Vec<lsp_types::TextEdit>>> {
2525
let uri = &params.text_document.uri;
26-
let doc = state.get_document(uri)?;
26+
let doc = state.get_document_or_error(uri)?;
2727

2828
let workspace_settings = lsp_state.workspace_document_settings(uri);
2929

@@ -68,7 +68,7 @@ pub(crate) fn document_range_formatting(
6868
state: &WorldState,
6969
) -> anyhow::Result<Option<Vec<lsp_types::TextEdit>>> {
7070
let uri = &params.text_document.uri;
71-
let doc = state.get_document(uri)?;
71+
let doc = state.get_document_or_error(uri)?;
7272

7373
let workspace_settings = lsp_state.workspace_document_settings(uri);
7474

crates/lsp/src/handlers_state.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ pub(crate) fn did_change(
167167
state: &mut WorldState,
168168
) -> anyhow::Result<()> {
169169
let uri = &params.text_document.uri;
170-
let doc = state.get_document_mut(uri)?;
170+
let doc = state.get_document_mut_or_error(uri)?;
171171
doc.on_did_change(params);
172172

173173
Ok(())
@@ -249,7 +249,7 @@ pub(crate) fn did_change_formatting_options(
249249
opts: &FormattingOptions,
250250
state: &mut WorldState,
251251
) {
252-
let Ok(doc) = state.get_document_mut(uri) else {
252+
let Some(doc) = state.get_document_mut(uri) else {
253253
return;
254254
};
255255

@@ -400,7 +400,7 @@ fn update_documents_config(
400400
let config: DocumentSettings = config.into();
401401

402402
// Finally, update the document's config
403-
state.get_document_mut(&uri)?.settings = config;
403+
state.get_document_mut_or_error(&uri)?.settings = config;
404404
}
405405

406406
Ok(())

crates/lsp/src/state.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,25 @@ pub(crate) struct WorldState {
4141
}
4242

4343
impl WorldState {
44-
pub(crate) fn get_document(&self, uri: &Url) -> anyhow::Result<&Document> {
45-
if let Some(doc) = self.documents.get(uri) {
46-
Ok(doc)
47-
} else {
48-
Err(anyhow!("Can't find document for URI {uri}"))
44+
pub(crate) fn get_document(&self, uri: &Url) -> Option<&Document> {
45+
self.documents.get(uri)
46+
}
47+
48+
pub(crate) fn get_document_mut(&mut self, uri: &Url) -> Option<&mut Document> {
49+
self.documents.get_mut(uri)
50+
}
51+
52+
pub(crate) fn get_document_or_error(&self, uri: &Url) -> anyhow::Result<&Document> {
53+
match self.get_document(uri) {
54+
Some(doc) => Ok(doc),
55+
None => Err(anyhow!("Can't find document for URI {uri}")),
4956
}
5057
}
5158

52-
pub(crate) fn get_document_mut(&mut self, uri: &Url) -> anyhow::Result<&mut Document> {
53-
if let Some(doc) = self.documents.get_mut(uri) {
54-
Ok(doc)
55-
} else {
56-
Err(anyhow!("Can't find document for URI {uri}"))
59+
pub(crate) fn get_document_mut_or_error(&mut self, uri: &Url) -> anyhow::Result<&mut Document> {
60+
match self.get_document_mut(uri) {
61+
Some(doc) => Ok(doc),
62+
None => Err(anyhow!("Can't find document for URI {uri}")),
5763
}
5864
}
5965

0 commit comments

Comments
 (0)