diff options
author | Aleksey Kladov <[email protected]> | 2020-05-24 15:18:46 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-05-24 15:53:18 +0100 |
commit | 934227361623b258d833be20e464e1509cb432ad (patch) | |
tree | cfb43f218d80f8e29045cf75e6e31ccfd6ca3782 | |
parent | 7e862626cc892662c3c95ba393483ac89c07e31a (diff) |
Document matchingBrace LSP request
-rw-r--r-- | crates/rust-analyzer/src/lsp_ext.rs | 12 | ||||
-rw-r--r-- | crates/rust-analyzer/src/main_loop.rs | 4 | ||||
-rw-r--r-- | crates/rust-analyzer/src/main_loop/handlers.rs | 8 | ||||
-rw-r--r-- | docs/dev/lsp-extensions.md | 47 | ||||
-rw-r--r-- | editors/code/src/commands/matching_brace.ts | 4 | ||||
-rw-r--r-- | editors/code/src/rust-analyzer-api.ts | 7 |
6 files changed, 63 insertions, 19 deletions
diff --git a/crates/rust-analyzer/src/lsp_ext.rs b/crates/rust-analyzer/src/lsp_ext.rs index c25d90a50..b8b9e65ed 100644 --- a/crates/rust-analyzer/src/lsp_ext.rs +++ b/crates/rust-analyzer/src/lsp_ext.rs | |||
@@ -60,19 +60,19 @@ pub struct ExpandMacroParams { | |||
60 | pub position: Option<Position>, | 60 | pub position: Option<Position>, |
61 | } | 61 | } |
62 | 62 | ||
63 | pub enum FindMatchingBrace {} | 63 | pub enum MatchingBrace {} |
64 | 64 | ||
65 | impl Request for FindMatchingBrace { | 65 | impl Request for MatchingBrace { |
66 | type Params = FindMatchingBraceParams; | 66 | type Params = MatchingBraceParams; |
67 | type Result = Vec<Position>; | 67 | type Result = Vec<Position>; |
68 | const METHOD: &'static str = "rust-analyzer/findMatchingBrace"; | 68 | const METHOD: &'static str = "experimental/matchingBrace"; |
69 | } | 69 | } |
70 | 70 | ||
71 | #[derive(Deserialize, Serialize, Debug)] | 71 | #[derive(Deserialize, Serialize, Debug)] |
72 | #[serde(rename_all = "camelCase")] | 72 | #[serde(rename_all = "camelCase")] |
73 | pub struct FindMatchingBraceParams { | 73 | pub struct MatchingBraceParams { |
74 | pub text_document: TextDocumentIdentifier, | 74 | pub text_document: TextDocumentIdentifier, |
75 | pub offsets: Vec<Position>, | 75 | pub positions: Vec<Position>, |
76 | } | 76 | } |
77 | 77 | ||
78 | pub enum ParentModule {} | 78 | pub enum ParentModule {} |
diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs index 87795fffb..e28a32c26 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs | |||
@@ -509,8 +509,8 @@ fn on_request( | |||
509 | .on_sync::<lsp_types::request::SelectionRangeRequest>(|s, p| { | 509 | .on_sync::<lsp_types::request::SelectionRangeRequest>(|s, p| { |
510 | handlers::handle_selection_range(s.snapshot(), p) | 510 | handlers::handle_selection_range(s.snapshot(), p) |
511 | })? | 511 | })? |
512 | .on_sync::<lsp_ext::FindMatchingBrace>(|s, p| { | 512 | .on_sync::<lsp_ext::MatchingBrace>(|s, p| { |
513 | handlers::handle_find_matching_brace(s.snapshot(), p) | 513 | handlers::handle_matching_brace(s.snapshot(), p) |
514 | })? | 514 | })? |
515 | .on::<lsp_ext::AnalyzerStatus>(handlers::handle_analyzer_status)? | 515 | .on::<lsp_ext::AnalyzerStatus>(handlers::handle_analyzer_status)? |
516 | .on::<lsp_ext::SyntaxTree>(handlers::handle_syntax_tree)? | 516 | .on::<lsp_ext::SyntaxTree>(handlers::handle_syntax_tree)? |
diff --git a/crates/rust-analyzer/src/main_loop/handlers.rs b/crates/rust-analyzer/src/main_loop/handlers.rs index 2aaff3ea4..d73107968 100644 --- a/crates/rust-analyzer/src/main_loop/handlers.rs +++ b/crates/rust-analyzer/src/main_loop/handlers.rs | |||
@@ -126,15 +126,15 @@ pub fn handle_selection_range( | |||
126 | Ok(Some(res?)) | 126 | Ok(Some(res?)) |
127 | } | 127 | } |
128 | 128 | ||
129 | pub fn handle_find_matching_brace( | 129 | pub fn handle_matching_brace( |
130 | world: WorldSnapshot, | 130 | world: WorldSnapshot, |
131 | params: lsp_ext::FindMatchingBraceParams, | 131 | params: lsp_ext::MatchingBraceParams, |
132 | ) -> Result<Vec<Position>> { | 132 | ) -> Result<Vec<Position>> { |
133 | let _p = profile("handle_find_matching_brace"); | 133 | let _p = profile("handle_matching_brace"); |
134 | let file_id = from_proto::file_id(&world, ¶ms.text_document.uri)?; | 134 | let file_id = from_proto::file_id(&world, ¶ms.text_document.uri)?; |
135 | let line_index = world.analysis().file_line_index(file_id)?; | 135 | let line_index = world.analysis().file_line_index(file_id)?; |
136 | let res = params | 136 | let res = params |
137 | .offsets | 137 | .positions |
138 | .into_iter() | 138 | .into_iter() |
139 | .map(|position| { | 139 | .map(|position| { |
140 | let offset = from_proto::offset(&line_index, position); | 140 | let offset = from_proto::offset(&line_index, position); |
diff --git a/docs/dev/lsp-extensions.md b/docs/dev/lsp-extensions.md index 1cc51410b..9fa1c5fc2 100644 --- a/docs/dev/lsp-extensions.md +++ b/docs/dev/lsp-extensions.md | |||
@@ -95,7 +95,7 @@ Invoking code action at this position will yield two code actions for importing | |||
95 | 95 | ||
96 | This request is send from client to server to handle "Join Lines" editor action. | 96 | This request is send from client to server to handle "Join Lines" editor action. |
97 | 97 | ||
98 | **Method:** `experimental/JoinLines` | 98 | **Method:** `experimental/joinLines` |
99 | 99 | ||
100 | **Request:** | 100 | **Request:** |
101 | 101 | ||
@@ -172,3 +172,48 @@ SSR with query `foo($a:expr, $b:expr) ==>> ($a).foo($b)` will transform, eg `foo | |||
172 | 172 | ||
173 | * Probably needs search without replace mode | 173 | * Probably needs search without replace mode |
174 | * Needs a way to limit the scope to certain files. | 174 | * Needs a way to limit the scope to certain files. |
175 | |||
176 | ## Matching Brace | ||
177 | |||
178 | **Issue:** https://github.com/microsoft/language-server-protocol/issues/999 | ||
179 | |||
180 | **Server Capability:** `{ "matchingBrace": boolean }` | ||
181 | |||
182 | This request is send from client to server to handle "Matching Brace" editor action. | ||
183 | |||
184 | **Method:** `experimental/matchingBrace` | ||
185 | |||
186 | **Request:** | ||
187 | |||
188 | ```typescript | ||
189 | interface MatchingBraceParams { | ||
190 | textDocument: TextDocumentIdentifier, | ||
191 | /// Position for each cursor | ||
192 | positions: Position[], | ||
193 | } | ||
194 | ``` | ||
195 | |||
196 | **Response:** | ||
197 | |||
198 | ```typescript | ||
199 | Position[] | ||
200 | ``` | ||
201 | |||
202 | ### Example | ||
203 | |||
204 | ```rust | ||
205 | fn main() { | ||
206 | let x: Vec<()>/*cursor here*/ = vec![] | ||
207 | } | ||
208 | ``` | ||
209 | |||
210 | `experimental/matchingBrace` yields the position of `<`. | ||
211 | In many cases, matching braces can be handled by the editor. | ||
212 | However, some cases (like disambiguating between generics and comparison operations) need a real parser. | ||
213 | Moreover, it would be cool if editors didn't need to implement even basic language parsing | ||
214 | |||
215 | ### Unresolved Question | ||
216 | |||
217 | * Should we return a a nested brace structure, to allow paredit-like actions of jump *out* of the current brace pair? | ||
218 | This is how `SelectionRange` request works. | ||
219 | * Alternatively, should we perhaps flag certain `SelectionRange`s as being brace pairs? | ||
diff --git a/editors/code/src/commands/matching_brace.ts b/editors/code/src/commands/matching_brace.ts index a60776e2d..9c418b887 100644 --- a/editors/code/src/commands/matching_brace.ts +++ b/editors/code/src/commands/matching_brace.ts | |||
@@ -9,9 +9,9 @@ export function matchingBrace(ctx: Ctx): Cmd { | |||
9 | const client = ctx.client; | 9 | const client = ctx.client; |
10 | if (!editor || !client) return; | 10 | if (!editor || !client) return; |
11 | 11 | ||
12 | const response = await client.sendRequest(ra.findMatchingBrace, { | 12 | const response = await client.sendRequest(ra.matchingBrace, { |
13 | textDocument: { uri: editor.document.uri.toString() }, | 13 | textDocument: { uri: editor.document.uri.toString() }, |
14 | offsets: editor.selections.map(s => | 14 | positions: editor.selections.map(s => |
15 | client.code2ProtocolConverter.asPosition(s.active), | 15 | client.code2ProtocolConverter.asPosition(s.active), |
16 | ), | 16 | ), |
17 | }); | 17 | }); |
diff --git a/editors/code/src/rust-analyzer-api.ts b/editors/code/src/rust-analyzer-api.ts index 73f36432f..900c5cd5b 100644 --- a/editors/code/src/rust-analyzer-api.ts +++ b/editors/code/src/rust-analyzer-api.ts | |||
@@ -40,12 +40,11 @@ export interface ExpandedMacro { | |||
40 | export const expandMacro = request<ExpandMacroParams, Option<ExpandedMacro>>("expandMacro"); | 40 | export const expandMacro = request<ExpandMacroParams, Option<ExpandedMacro>>("expandMacro"); |
41 | 41 | ||
42 | 42 | ||
43 | export interface FindMatchingBraceParams { | 43 | export interface MatchingBraceParams { |
44 | textDocument: lc.TextDocumentIdentifier; | 44 | textDocument: lc.TextDocumentIdentifier; |
45 | offsets: Vec<lc.Position>; | 45 | positions: lc.Position[]; |
46 | } | 46 | } |
47 | export const findMatchingBrace = request<FindMatchingBraceParams, Vec<lc.Position>>("findMatchingBrace"); | 47 | export const matchingBrace = new lc.RequestType<MatchingBraceParams, lc.Position[], unknown>('experimental/matchingBrace'); |
48 | |||
49 | 48 | ||
50 | export interface PublishDecorationsParams { | 49 | export interface PublishDecorationsParams { |
51 | uri: string; | 50 | uri: string; |