From c631b585a7358d1569a051f2529ecaae222e95cd Mon Sep 17 00:00:00 2001
From: Aleksey Kladov <aleksey.kladov@gmail.com>
Date: Thu, 16 Aug 2018 00:23:22 +0300
Subject: matching brace

---
 crates/server/src/conv.rs               |  8 ++++++++
 crates/server/src/main_loop/handlers.rs | 21 ++++++++++++++++++++-
 crates/server/src/main_loop/mod.rs      |  4 ++++
 crates/server/src/req.rs                | 17 ++++++++++++++++-
 4 files changed, 48 insertions(+), 2 deletions(-)

(limited to 'crates/server/src')

diff --git a/crates/server/src/conv.rs b/crates/server/src/conv.rs
index bbe512ece..b3709ccaf 100644
--- a/crates/server/src/conv.rs
+++ b/crates/server/src/conv.rs
@@ -117,6 +117,14 @@ impl ConvWith for AtomEdit {
     }
 }
 
+impl<T: ConvWith> ConvWith for Option<T> {
+    type Ctx = <T as ConvWith>::Ctx;
+    type Output = Option<<T as ConvWith>::Output>;
+    fn conv_with(self, ctx: &Self::Ctx) -> Self::Output {
+        self.map(|x| ConvWith::conv_with(x, ctx))
+    }
+}
+
 impl<'a> TryConvWith for &'a Url {
     type Ctx = PathMap;
     type Output = FileId;
diff --git a/crates/server/src/main_loop/handlers.rs b/crates/server/src/main_loop/handlers.rs
index 078abfbfa..d7b78b4fa 100644
--- a/crates/server/src/main_loop/handlers.rs
+++ b/crates/server/src/main_loop/handlers.rs
@@ -3,7 +3,7 @@ use std::collections::HashMap;
 use languageserver_types::{
     Diagnostic, DiagnosticSeverity, Url, DocumentSymbol,
     Command, TextDocumentIdentifier, WorkspaceEdit,
-    SymbolInformation,
+    SymbolInformation, Position,
 };
 use libanalysis::{World, Query};
 use libeditor;
@@ -42,6 +42,25 @@ pub fn handle_extend_selection(
     Ok(req::ExtendSelectionResult { selections })
 }
 
+pub fn handle_find_matching_brace(
+    world: World,
+    path_map: PathMap,
+    params: req::FindMatchingBraceParams,
+) -> Result<Vec<Position>> {
+    let file_id = params.text_document.try_conv_with(&path_map)?;
+    let file = world.file_syntax(file_id)?;
+    let line_index = world.file_line_index(file_id)?;
+    let res = params.offsets
+        .into_iter()
+        .map_conv_with(&line_index)
+        .map(|offset| {
+            libeditor::matching_brace(&file, offset).unwrap_or(offset)
+        })
+        .map_conv_with(&line_index)
+        .collect();
+    Ok(res)
+}
+
 pub fn handle_document_symbol(
     world: World,
     path_map: PathMap,
diff --git a/crates/server/src/main_loop/mod.rs b/crates/server/src/main_loop/mod.rs
index 2a31297be..4d5dfb437 100644
--- a/crates/server/src/main_loop/mod.rs
+++ b/crates/server/src/main_loop/mod.rs
@@ -26,6 +26,7 @@ use {
         handle_execute_command,
         handle_workspace_symbol,
         handle_goto_definition,
+        handle_find_matching_brace,
     },
 };
 
@@ -148,6 +149,9 @@ fn on_request(
     handle_request_on_threadpool::<req::ExtendSelection>(
         &mut req, pool, path_map, world, sender, handle_extend_selection,
     )?;
+    handle_request_on_threadpool::<req::FindMatchingBrace>(
+        &mut req, pool, path_map, world, sender, handle_find_matching_brace,
+    )?;
     handle_request_on_threadpool::<req::DocumentSymbolRequest>(
         &mut req, pool, path_map, world, sender, handle_document_symbol,
     )?;
diff --git a/crates/server/src/req.rs b/crates/server/src/req.rs
index 17ef10e43..c3efc7489 100644
--- a/crates/server/src/req.rs
+++ b/crates/server/src/req.rs
@@ -1,5 +1,5 @@
 use serde::{ser::Serialize, de::DeserializeOwned};
-use languageserver_types::{TextDocumentIdentifier, Range, Url};
+use languageserver_types::{TextDocumentIdentifier, Range, Url, Position};
 use url_serde;
 
 pub use languageserver_types::{
@@ -65,6 +65,21 @@ pub struct ExtendSelectionResult {
     pub selections: Vec<Range>,
 }
 
+pub enum FindMatchingBrace {}
+
+impl Request for FindMatchingBrace {
+    type Params = FindMatchingBraceParams;
+    type Result = Vec<Position>;
+    const METHOD: &'static str = "m/findMatchingBrace";
+}
+
+#[derive(Deserialize, Debug)]
+#[serde(rename_all = "camelCase")]
+pub struct FindMatchingBraceParams {
+    pub text_document: TextDocumentIdentifier,
+    pub offsets: Vec<Position>,
+}
+
 pub enum PublishDecorations {}
 
 impl Notification for PublishDecorations {
-- 
cgit v1.2.3