-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
240 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,37 @@ | ||
pub(crate) use biome_lsp_converters::from_proto::offset; | ||
pub(crate) use biome_lsp_converters::from_proto::text_range; | ||
|
||
use tower_lsp::lsp_types; | ||
|
||
use crate::documents::Document; | ||
|
||
pub fn apply_text_edits( | ||
doc: &Document, | ||
mut edits: Vec<lsp_types::TextEdit>, | ||
) -> anyhow::Result<String> { | ||
let mut text = doc.contents.clone(); | ||
|
||
// Apply edits from bottom to top to avoid inserted newlines to invalidate | ||
// positions in earlier parts of the doc (they are sent in reading order | ||
// accorder to the LSP protocol) | ||
edits.reverse(); | ||
|
||
for edit in edits { | ||
let start: usize = offset( | ||
&doc.line_index.index, | ||
edit.range.start, | ||
doc.line_index.encoding, | ||
)? | ||
.into(); | ||
let end: usize = offset( | ||
&doc.line_index.index, | ||
edit.range.end, | ||
doc.line_index.encoding, | ||
)? | ||
.into(); | ||
|
||
text.replace_range(start..end, &edit.new_text); | ||
} | ||
|
||
Ok(text) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// --- source | ||
// authors = ["rust-analyzer team"] | ||
// license = "MIT OR Apache-2.0" | ||
// origin = "https://github.com/rust-lang/rust-analyzer/blob/8d5e91c9/crates/rust-analyzer/src/handlers/request.rs#L2483" | ||
// --- | ||
|
||
use biome_text_size::{TextRange, TextSize}; | ||
|
||
use super::text_edit::TextEdit; | ||
|
||
pub(crate) fn diff(left: &str, right: &str) -> TextEdit { | ||
use dissimilar::Chunk; | ||
|
||
let chunks = dissimilar::diff(left, right); | ||
|
||
let mut builder = TextEdit::builder(); | ||
let mut pos = TextSize::default(); | ||
|
||
let mut chunks = chunks.into_iter().peekable(); | ||
while let Some(chunk) = chunks.next() { | ||
if let (Chunk::Delete(deleted), Some(&Chunk::Insert(inserted))) = (chunk, chunks.peek()) { | ||
chunks.next().unwrap(); | ||
let deleted_len = TextSize::of(deleted); | ||
builder.replace(TextRange::at(pos, deleted_len), inserted.into()); | ||
pos += deleted_len; | ||
continue; | ||
} | ||
|
||
match chunk { | ||
Chunk::Equal(text) => { | ||
pos += TextSize::of(text); | ||
} | ||
Chunk::Delete(deleted) => { | ||
let deleted_len = TextSize::of(deleted); | ||
builder.delete(TextRange::at(pos, deleted_len)); | ||
pos += deleted_len; | ||
} | ||
Chunk::Insert(inserted) => { | ||
builder.insert(pos, inserted.into()); | ||
} | ||
} | ||
} | ||
builder.finish() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod diff; | ||
pub mod line_index; | ||
pub mod text_edit; | ||
pub mod to_proto; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
crates/lsp/src/snapshots/lsp__handlers_format__tests__format.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
source: crates/lsp/src/handlers_format.rs | ||
expression: formatted | ||
--- | ||
1 | ||
2 + 2 | ||
3 + 3 + 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use lsp_test::lsp_client::TestClient; | ||
use tower_lsp::lsp_types; | ||
|
||
use crate::{documents::Document, from_proto}; | ||
|
||
pub(crate) trait TestClientExt { | ||
async fn open_document(&mut self, doc: &Document) -> lsp_types::TextDocumentItem; | ||
async fn format_document(&mut self, doc: &Document) -> String; | ||
async fn format_document_edits(&mut self, doc: &Document) -> Option<Vec<lsp_types::TextEdit>>; | ||
} | ||
|
||
impl TestClientExt for TestClient { | ||
async fn open_document(&mut self, doc: &Document) -> lsp_types::TextDocumentItem { | ||
let path = format!("test://{}", uuid::Uuid::new_v4()); | ||
let uri = url::Url::parse(&path).unwrap(); | ||
|
||
let text_document = lsp_types::TextDocumentItem { | ||
uri, | ||
language_id: String::from("r"), | ||
version: 0, | ||
text: doc.contents.clone(), | ||
}; | ||
|
||
let params = lsp_types::DidOpenTextDocumentParams { | ||
text_document: text_document.clone(), | ||
}; | ||
self.did_open_text_document(params).await; | ||
|
||
text_document | ||
} | ||
|
||
async fn format_document(&mut self, doc: &Document) -> String { | ||
let edits = self.format_document_edits(doc).await.unwrap(); | ||
from_proto::apply_text_edits(doc, edits).unwrap() | ||
} | ||
|
||
async fn format_document_edits(&mut self, doc: &Document) -> Option<Vec<lsp_types::TextEdit>> { | ||
let lsp_doc = self.open_document(&doc).await; | ||
|
||
let options = lsp_types::FormattingOptions { | ||
tab_size: 4, | ||
insert_spaces: false, | ||
..Default::default() | ||
}; | ||
|
||
self.formatting(lsp_types::DocumentFormattingParams { | ||
text_document: lsp_types::TextDocumentIdentifier { | ||
uri: lsp_doc.uri.clone(), | ||
}, | ||
options, | ||
work_done_progress_params: Default::default(), | ||
}) | ||
.await; | ||
|
||
let response = self.recv_response().await; | ||
|
||
let value: Option<Vec<lsp_types::TextEdit>> = | ||
serde_json::from_value(response.result().unwrap().clone()).unwrap(); | ||
|
||
self.close_document(lsp_doc.uri).await; | ||
|
||
value | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters