Use LSP range formatting when document formatting is not available
This commit is contained in:
parent
dc5a09b3f7
commit
46da80d726
3 changed files with 43 additions and 9 deletions
|
@ -1314,15 +1314,41 @@ impl Project {
|
||||||
}
|
}
|
||||||
|
|
||||||
for (buffer, buffer_abs_path, lang_server) in local_buffers {
|
for (buffer, buffer_abs_path, lang_server) in local_buffers {
|
||||||
let lsp_edits = lang_server
|
let capabilities = if let Some(capabilities) = lang_server.capabilities().await {
|
||||||
.request::<lsp::request::Formatting>(lsp::DocumentFormattingParams {
|
capabilities
|
||||||
text_document: lsp::TextDocumentIdentifier::new(
|
} else {
|
||||||
lsp::Url::from_file_path(&buffer_abs_path).unwrap(),
|
continue;
|
||||||
),
|
};
|
||||||
options: Default::default(),
|
|
||||||
work_done_progress_params: Default::default(),
|
let text_document = lsp::TextDocumentIdentifier::new(
|
||||||
})
|
lsp::Url::from_file_path(&buffer_abs_path).unwrap(),
|
||||||
.await?;
|
);
|
||||||
|
let lsp_edits = if capabilities.document_formatting_provider.is_some() {
|
||||||
|
lang_server
|
||||||
|
.request::<lsp::request::Formatting>(lsp::DocumentFormattingParams {
|
||||||
|
text_document,
|
||||||
|
options: Default::default(),
|
||||||
|
work_done_progress_params: Default::default(),
|
||||||
|
})
|
||||||
|
.await?
|
||||||
|
} else if capabilities.document_range_formatting_provider.is_some() {
|
||||||
|
let buffer_start = lsp::Position::new(0, 0);
|
||||||
|
let buffer_end = buffer
|
||||||
|
.read_with(&cx, |buffer, _| buffer.max_point_utf16())
|
||||||
|
.to_lsp_position();
|
||||||
|
lang_server
|
||||||
|
.request::<lsp::request::RangeFormatting>(
|
||||||
|
lsp::DocumentRangeFormattingParams {
|
||||||
|
text_document,
|
||||||
|
range: lsp::Range::new(buffer_start, buffer_end),
|
||||||
|
options: Default::default(),
|
||||||
|
work_done_progress_params: Default::default(),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await?
|
||||||
|
} else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
if let Some(lsp_edits) = lsp_edits {
|
if let Some(lsp_edits) = lsp_edits {
|
||||||
let edits = buffer
|
let edits = buffer
|
||||||
|
|
|
@ -126,6 +126,10 @@ impl Rope {
|
||||||
self.chunks.extent(&())
|
self.chunks.extent(&())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn max_point_utf16(&self) -> PointUtf16 {
|
||||||
|
self.chunks.extent(&())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn cursor(&self, offset: usize) -> Cursor {
|
pub fn cursor(&self, offset: usize) -> Cursor {
|
||||||
Cursor::new(self, offset)
|
Cursor::new(self, offset)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1526,6 +1526,10 @@ impl BufferSnapshot {
|
||||||
self.visible_text.max_point()
|
self.visible_text.max_point()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn max_point_utf16(&self) -> PointUtf16 {
|
||||||
|
self.visible_text.max_point_utf16()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn point_to_offset(&self, point: Point) -> usize {
|
pub fn point_to_offset(&self, point: Point) -> usize {
|
||||||
self.visible_text.point_to_offset(point)
|
self.visible_text.point_to_offset(point)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue