aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/libeditor/src/lib.rs26
-rw-r--r--crates/libeditor/tests/test.rs43
-rw-r--r--crates/server/src/conv.rs8
-rw-r--r--crates/server/src/main_loop/handlers.rs21
-rw-r--r--crates/server/src/main_loop/mod.rs4
-rw-r--r--crates/server/src/req.rs17
6 files changed, 106 insertions, 13 deletions
diff --git a/crates/libeditor/src/lib.rs b/crates/libeditor/src/lib.rs
index 9e44f5d92..28da457d1 100644
--- a/crates/libeditor/src/lib.rs
+++ b/crates/libeditor/src/lib.rs
@@ -12,8 +12,8 @@ mod code_actions;
12use libsyntax2::{ 12use libsyntax2::{
13 ast::{self, NameOwner}, 13 ast::{self, NameOwner},
14 AstNode, 14 AstNode,
15 algo::walk, 15 algo::{walk, find_leaf_at_offset},
16 SyntaxKind::*, 16 SyntaxKind::{self, *},
17}; 17};
18pub use libsyntax2::{File, TextRange, TextUnit}; 18pub use libsyntax2::{File, TextRange, TextUnit};
19pub use self::{ 19pub use self::{
@@ -52,6 +52,28 @@ pub fn parse(text: &str) -> ast::File {
52 ast::File::parse(text) 52 ast::File::parse(text)
53} 53}
54 54
55pub fn matching_brace(file: &ast::File, offset: TextUnit) -> Option<TextUnit> {
56 const BRACES: &[SyntaxKind] = &[
57 L_CURLY, R_CURLY,
58 L_BRACK, R_BRACK,
59 L_PAREN, R_PAREN,
60 L_ANGLE, R_ANGLE,
61 ];
62 let syntax = file.syntax();
63 let syntax = syntax.as_ref();
64 let (brace_node, brace_idx) = find_leaf_at_offset(syntax, offset)
65 .filter_map(|node| {
66 let idx = BRACES.iter().position(|&brace| brace == node.kind())?;
67 Some((node, idx))
68 })
69 .next()?;
70 let parent = brace_node.parent()?;
71 let matching_kind = BRACES[brace_idx ^ 1];
72 let matching_node = parent.children()
73 .find(|node| node.kind() == matching_kind)?;
74 Some(matching_node.range().start())
75}
76
55pub fn highlight(file: &ast::File) -> Vec<HighlightedRange> { 77pub fn highlight(file: &ast::File) -> Vec<HighlightedRange> {
56 let syntax = file.syntax(); 78 let syntax = file.syntax();
57 let mut res = Vec::new(); 79 let mut res = Vec::new();
diff --git a/crates/libeditor/tests/test.rs b/crates/libeditor/tests/test.rs
index 7063425ce..d5df9d0cc 100644
--- a/crates/libeditor/tests/test.rs
+++ b/crates/libeditor/tests/test.rs
@@ -9,7 +9,7 @@ use itertools::Itertools;
9use libeditor::{ 9use libeditor::{
10 File, TextUnit, TextRange, ActionResult, CursorPosition, 10 File, TextUnit, TextRange, ActionResult, CursorPosition,
11 highlight, runnables, extend_selection, file_structure, 11 highlight, runnables, extend_selection, file_structure,
12 flip_comma, add_derive, 12 flip_comma, add_derive, matching_brace,
13}; 13};
14 14
15#[test] 15#[test]
@@ -119,6 +119,25 @@ fn test_add_derive() {
119 ) 119 )
120} 120}
121 121
122#[test]
123fn test_matching_brace() {
124 fn do_check(before: &str, after: &str) {
125 let (pos, before) = extract_cursor(before);
126 let file = file(&before);
127 let new_pos = match matching_brace(&file, pos) {
128 None => pos,
129 Some(pos) => pos,
130 };
131 let actual = add_cursor(&before, new_pos);
132 assert_eq_text!(after, &actual);
133 }
134
135 do_check(
136 "struct Foo { a: i32, }<|>",
137 "struct Foo <|>{ a: i32, }",
138 );
139}
140
122fn file(text: &str) -> File { 141fn file(text: &str) -> File {
123 File::parse(text) 142 File::parse(text)
124} 143}
@@ -138,16 +157,12 @@ fn check_action<F: Fn(&File, TextUnit) -> Option<ActionResult>>(
138 let file = file(&before); 157 let file = file(&before);
139 let result = f(&file, before_cursor_pos).expect("code action is not applicable"); 158 let result = f(&file, before_cursor_pos).expect("code action is not applicable");
140 let actual = result.edit.apply(&before); 159 let actual = result.edit.apply(&before);
141 let actual_cursor_pos: u32 = match result.cursor_position { 160 let actual_cursor_pos = match result.cursor_position {
142 CursorPosition::Same => result.edit.apply_to_offset(before_cursor_pos).unwrap(), 161 CursorPosition::Same => result.edit.apply_to_offset(before_cursor_pos).unwrap(),
143 CursorPosition::Offset(off) => off, 162 CursorPosition::Offset(off) => off,
144 }.into(); 163 };
145 let actual_cursor_pos = actual_cursor_pos as usize; 164 let actual = add_cursor(&actual, actual_cursor_pos);
146 let mut actual_with_cursor = String::new(); 165 assert_eq_text!(after, &actual);
147 actual_with_cursor.push_str(&actual[..actual_cursor_pos]);
148 actual_with_cursor.push_str("<|>");
149 actual_with_cursor.push_str(&actual[actual_cursor_pos..]);
150 assert_eq_text!(after, &actual_with_cursor);
151} 166}
152 167
153fn extract_cursor(text: &str) -> (TextUnit, String) { 168fn extract_cursor(text: &str) -> (TextUnit, String) {
@@ -162,3 +177,13 @@ fn extract_cursor(text: &str) -> (TextUnit, String) {
162 let cursor_pos = TextUnit::from(cursor_pos as u32); 177 let cursor_pos = TextUnit::from(cursor_pos as u32);
163 (cursor_pos, new_text) 178 (cursor_pos, new_text)
164} 179}
180
181fn add_cursor(text: &str, offset: TextUnit) -> String {
182 let offset: u32 = offset.into();
183 let offset: usize = offset as usize;
184 let mut res = String::new();
185 res.push_str(&text[..offset]);
186 res.push_str("<|>");
187 res.push_str(&text[offset..]);
188 res
189}
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 {
117 } 117 }
118} 118}
119 119
120impl<T: ConvWith> ConvWith for Option<T> {
121 type Ctx = <T as ConvWith>::Ctx;
122 type Output = Option<<T as ConvWith>::Output>;
123 fn conv_with(self, ctx: &Self::Ctx) -> Self::Output {
124 self.map(|x| ConvWith::conv_with(x, ctx))
125 }
126}
127
120impl<'a> TryConvWith for &'a Url { 128impl<'a> TryConvWith for &'a Url {
121 type Ctx = PathMap; 129 type Ctx = PathMap;
122 type Output = FileId; 130 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;
3use languageserver_types::{ 3use languageserver_types::{
4 Diagnostic, DiagnosticSeverity, Url, DocumentSymbol, 4 Diagnostic, DiagnosticSeverity, Url, DocumentSymbol,
5 Command, TextDocumentIdentifier, WorkspaceEdit, 5 Command, TextDocumentIdentifier, WorkspaceEdit,
6 SymbolInformation, 6 SymbolInformation, Position,
7}; 7};
8use libanalysis::{World, Query}; 8use libanalysis::{World, Query};
9use libeditor; 9use libeditor;
@@ -42,6 +42,25 @@ pub fn handle_extend_selection(
42 Ok(req::ExtendSelectionResult { selections }) 42 Ok(req::ExtendSelectionResult { selections })
43} 43}
44 44
45pub fn handle_find_matching_brace(
46 world: World,
47 path_map: PathMap,
48 params: req::FindMatchingBraceParams,
49) -> Result<Vec<Position>> {
50 let file_id = params.text_document.try_conv_with(&path_map)?;
51 let file = world.file_syntax(file_id)?;
52 let line_index = world.file_line_index(file_id)?;
53 let res = params.offsets
54 .into_iter()
55 .map_conv_with(&line_index)
56 .map(|offset| {
57 libeditor::matching_brace(&file, offset).unwrap_or(offset)
58 })
59 .map_conv_with(&line_index)
60 .collect();
61 Ok(res)
62}
63
45pub fn handle_document_symbol( 64pub fn handle_document_symbol(
46 world: World, 65 world: World,
47 path_map: PathMap, 66 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 {
26 handle_execute_command, 26 handle_execute_command,
27 handle_workspace_symbol, 27 handle_workspace_symbol,
28 handle_goto_definition, 28 handle_goto_definition,
29 handle_find_matching_brace,
29 }, 30 },
30}; 31};
31 32
@@ -148,6 +149,9 @@ fn on_request(
148 handle_request_on_threadpool::<req::ExtendSelection>( 149 handle_request_on_threadpool::<req::ExtendSelection>(
149 &mut req, pool, path_map, world, sender, handle_extend_selection, 150 &mut req, pool, path_map, world, sender, handle_extend_selection,
150 )?; 151 )?;
152 handle_request_on_threadpool::<req::FindMatchingBrace>(
153 &mut req, pool, path_map, world, sender, handle_find_matching_brace,
154 )?;
151 handle_request_on_threadpool::<req::DocumentSymbolRequest>( 155 handle_request_on_threadpool::<req::DocumentSymbolRequest>(
152 &mut req, pool, path_map, world, sender, handle_document_symbol, 156 &mut req, pool, path_map, world, sender, handle_document_symbol,
153 )?; 157 )?;
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 @@
1use serde::{ser::Serialize, de::DeserializeOwned}; 1use serde::{ser::Serialize, de::DeserializeOwned};
2use languageserver_types::{TextDocumentIdentifier, Range, Url}; 2use languageserver_types::{TextDocumentIdentifier, Range, Url, Position};
3use url_serde; 3use url_serde;
4 4
5pub use languageserver_types::{ 5pub use languageserver_types::{
@@ -65,6 +65,21 @@ pub struct ExtendSelectionResult {
65 pub selections: Vec<Range>, 65 pub selections: Vec<Range>,
66} 66}
67 67
68pub enum FindMatchingBrace {}
69
70impl Request for FindMatchingBrace {
71 type Params = FindMatchingBraceParams;
72 type Result = Vec<Position>;
73 const METHOD: &'static str = "m/findMatchingBrace";
74}
75
76#[derive(Deserialize, Debug)]
77#[serde(rename_all = "camelCase")]
78pub struct FindMatchingBraceParams {
79 pub text_document: TextDocumentIdentifier,
80 pub offsets: Vec<Position>,
81}
82
68pub enum PublishDecorations {} 83pub enum PublishDecorations {}
69 84
70impl Notification for PublishDecorations { 85impl Notification for PublishDecorations {