diff options
author | ivan770 <[email protected]> | 2021-03-16 12:37:00 +0000 |
---|---|---|
committer | ivan770 <[email protected]> | 2021-03-18 09:22:27 +0000 |
commit | 7d604584954660d255ad0929d3be8ce03f879d0c (patch) | |
tree | 613fdfdfd7eeb170082800533fb8b669dc35d25b /crates/rust-analyzer | |
parent | d704750ba982153d92ccff90cf236121641b9da3 (diff) |
Item up and down movers
Diffstat (limited to 'crates/rust-analyzer')
-rw-r--r-- | crates/rust-analyzer/src/handlers.rs | 19 | ||||
-rw-r--r-- | crates/rust-analyzer/src/lsp_ext.rs | 22 | ||||
-rw-r--r-- | crates/rust-analyzer/src/main_loop.rs | 1 | ||||
-rw-r--r-- | crates/rust-analyzer/src/to_proto.rs | 12 |
4 files changed, 54 insertions, 0 deletions
diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index b6f484e51..8daf27867 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs | |||
@@ -1424,6 +1424,25 @@ pub(crate) fn handle_open_cargo_toml( | |||
1424 | Ok(Some(res)) | 1424 | Ok(Some(res)) |
1425 | } | 1425 | } |
1426 | 1426 | ||
1427 | pub(crate) fn handle_move_item( | ||
1428 | snap: GlobalStateSnapshot, | ||
1429 | params: lsp_ext::MoveItemParams, | ||
1430 | ) -> Result<Option<lsp_types::TextDocumentEdit>> { | ||
1431 | let _p = profile::span("handle_move_item"); | ||
1432 | let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?; | ||
1433 | let range = from_proto::file_range(&snap, params.text_document, params.range)?; | ||
1434 | |||
1435 | let direction = match params.direction { | ||
1436 | lsp_ext::MoveItemDirection::Up => ide::Direction::Up, | ||
1437 | lsp_ext::MoveItemDirection::Down => ide::Direction::Down, | ||
1438 | }; | ||
1439 | |||
1440 | match snap.analysis.move_item(range, direction)? { | ||
1441 | Some(text_edit) => Ok(Some(to_proto::text_document_edit(&snap, file_id, text_edit)?)), | ||
1442 | None => Ok(None), | ||
1443 | } | ||
1444 | } | ||
1445 | |||
1427 | fn to_command_link(command: lsp_types::Command, tooltip: String) -> lsp_ext::CommandLink { | 1446 | fn to_command_link(command: lsp_types::Command, tooltip: String) -> lsp_ext::CommandLink { |
1428 | lsp_ext::CommandLink { tooltip: Some(tooltip), command } | 1447 | lsp_ext::CommandLink { tooltip: Some(tooltip), command } |
1429 | } | 1448 | } |
diff --git a/crates/rust-analyzer/src/lsp_ext.rs b/crates/rust-analyzer/src/lsp_ext.rs index efcdcd1d9..0e1fec209 100644 --- a/crates/rust-analyzer/src/lsp_ext.rs +++ b/crates/rust-analyzer/src/lsp_ext.rs | |||
@@ -402,3 +402,25 @@ pub(crate) enum CodeLensResolveData { | |||
402 | pub fn supports_utf8(caps: &lsp_types::ClientCapabilities) -> bool { | 402 | pub fn supports_utf8(caps: &lsp_types::ClientCapabilities) -> bool { |
403 | caps.offset_encoding.as_deref().unwrap_or_default().iter().any(|it| it == "utf-8") | 403 | caps.offset_encoding.as_deref().unwrap_or_default().iter().any(|it| it == "utf-8") |
404 | } | 404 | } |
405 | |||
406 | pub enum MoveItem {} | ||
407 | |||
408 | impl Request for MoveItem { | ||
409 | type Params = MoveItemParams; | ||
410 | type Result = Option<lsp_types::TextDocumentEdit>; | ||
411 | const METHOD: &'static str = "experimental/moveItem"; | ||
412 | } | ||
413 | |||
414 | #[derive(Serialize, Deserialize, Debug)] | ||
415 | #[serde(rename_all = "camelCase")] | ||
416 | pub struct MoveItemParams { | ||
417 | pub direction: MoveItemDirection, | ||
418 | pub text_document: TextDocumentIdentifier, | ||
419 | pub range: Range, | ||
420 | } | ||
421 | |||
422 | #[derive(Serialize, Deserialize, Debug)] | ||
423 | pub enum MoveItemDirection { | ||
424 | Up, | ||
425 | Down, | ||
426 | } | ||
diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs index 984790d35..022a20851 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs | |||
@@ -507,6 +507,7 @@ impl GlobalState { | |||
507 | .on::<lsp_ext::HoverRequest>(handlers::handle_hover) | 507 | .on::<lsp_ext::HoverRequest>(handlers::handle_hover) |
508 | .on::<lsp_ext::ExternalDocs>(handlers::handle_open_docs) | 508 | .on::<lsp_ext::ExternalDocs>(handlers::handle_open_docs) |
509 | .on::<lsp_ext::OpenCargoToml>(handlers::handle_open_cargo_toml) | 509 | .on::<lsp_ext::OpenCargoToml>(handlers::handle_open_cargo_toml) |
510 | .on::<lsp_ext::MoveItem>(handlers::handle_move_item) | ||
510 | .on::<lsp_types::request::OnTypeFormatting>(handlers::handle_on_type_formatting) | 511 | .on::<lsp_types::request::OnTypeFormatting>(handlers::handle_on_type_formatting) |
511 | .on::<lsp_types::request::DocumentSymbolRequest>(handlers::handle_document_symbol) | 512 | .on::<lsp_types::request::DocumentSymbolRequest>(handlers::handle_document_symbol) |
512 | .on::<lsp_types::request::WorkspaceSymbol>(handlers::handle_workspace_symbol) | 513 | .on::<lsp_types::request::WorkspaceSymbol>(handlers::handle_workspace_symbol) |
diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index 70501618e..3171708d5 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs | |||
@@ -658,6 +658,18 @@ pub(crate) fn goto_definition_response( | |||
658 | } | 658 | } |
659 | } | 659 | } |
660 | 660 | ||
661 | pub(crate) fn text_document_edit( | ||
662 | snap: &GlobalStateSnapshot, | ||
663 | file_id: FileId, | ||
664 | edit: TextEdit, | ||
665 | ) -> Result<lsp_types::TextDocumentEdit> { | ||
666 | let text_document = optional_versioned_text_document_identifier(snap, file_id); | ||
667 | let line_index = snap.file_line_index(file_id)?; | ||
668 | let edits = | ||
669 | edit.into_iter().map(|it| lsp_types::OneOf::Left(text_edit(&line_index, it))).collect(); | ||
670 | Ok(lsp_types::TextDocumentEdit { text_document, edits }) | ||
671 | } | ||
672 | |||
661 | pub(crate) fn snippet_text_document_edit( | 673 | pub(crate) fn snippet_text_document_edit( |
662 | snap: &GlobalStateSnapshot, | 674 | snap: &GlobalStateSnapshot, |
663 | is_snippet: bool, | 675 | is_snippet: bool, |