Skip to content

Commit fbb8b88

Browse files
bors[bot]matklad
andauthored
Merge #4593
4593: Document some rust-analyzer specific protocol extensions r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents d959c91 + 5276bff commit fbb8b88

File tree

6 files changed

+131
-27
lines changed

6 files changed

+131
-27
lines changed

crates/rust-analyzer/src/lsp_ext.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,6 @@ pub struct SyntaxTreeParams {
3838
pub range: Option<Range>,
3939
}
4040

41-
#[derive(Deserialize, Serialize, Debug)]
42-
#[serde(rename_all = "camelCase")]
43-
pub struct ExpandedMacro {
44-
pub name: String,
45-
pub expansion: String,
46-
}
47-
4841
pub enum ExpandMacro {}
4942

5043
impl Request for ExpandMacro {
@@ -60,19 +53,26 @@ pub struct ExpandMacroParams {
6053
pub position: Option<Position>,
6154
}
6255

63-
pub enum FindMatchingBrace {}
56+
#[derive(Deserialize, Serialize, Debug)]
57+
#[serde(rename_all = "camelCase")]
58+
pub struct ExpandedMacro {
59+
pub name: String,
60+
pub expansion: String,
61+
}
62+
63+
pub enum MatchingBrace {}
6464

65-
impl Request for FindMatchingBrace {
66-
type Params = FindMatchingBraceParams;
65+
impl Request for MatchingBrace {
66+
type Params = MatchingBraceParams;
6767
type Result = Vec<Position>;
68-
const METHOD: &'static str = "rust-analyzer/findMatchingBrace";
68+
const METHOD: &'static str = "experimental/matchingBrace";
6969
}
7070

7171
#[derive(Deserialize, Serialize, Debug)]
7272
#[serde(rename_all = "camelCase")]
73-
pub struct FindMatchingBraceParams {
73+
pub struct MatchingBraceParams {
7474
pub text_document: TextDocumentIdentifier,
75-
pub offsets: Vec<Position>,
75+
pub positions: Vec<Position>,
7676
}
7777

7878
pub enum ParentModule {}

crates/rust-analyzer/src/main_loop.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -509,9 +509,7 @@ fn on_request(
509509
.on_sync::<lsp_types::request::SelectionRangeRequest>(|s, p| {
510510
handlers::handle_selection_range(s.snapshot(), p)
511511
})?
512-
.on_sync::<lsp_ext::FindMatchingBrace>(|s, p| {
513-
handlers::handle_find_matching_brace(s.snapshot(), p)
514-
})?
512+
.on_sync::<lsp_ext::MatchingBrace>(|s, p| handlers::handle_matching_brace(s.snapshot(), p))?
515513
.on::<lsp_ext::AnalyzerStatus>(handlers::handle_analyzer_status)?
516514
.on::<lsp_ext::SyntaxTree>(handlers::handle_syntax_tree)?
517515
.on::<lsp_ext::ExpandMacro>(handlers::handle_expand_macro)?

crates/rust-analyzer/src/main_loop/handlers.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,15 @@ pub fn handle_selection_range(
126126
Ok(Some(res?))
127127
}
128128

129-
pub fn handle_find_matching_brace(
129+
pub fn handle_matching_brace(
130130
world: WorldSnapshot,
131-
params: lsp_ext::FindMatchingBraceParams,
131+
params: lsp_ext::MatchingBraceParams,
132132
) -> Result<Vec<Position>> {
133-
let _p = profile("handle_find_matching_brace");
133+
let _p = profile("handle_matching_brace");
134134
let file_id = from_proto::file_id(&world, &params.text_document.uri)?;
135135
let line_index = world.analysis().file_line_index(file_id)?;
136136
let res = params
137-
.offsets
137+
.positions
138138
.into_iter()
139139
.map(|position| {
140140
let offset = from_proto::offset(&line_index, position);

docs/dev/lsp-extensions.md

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ Invoking code action at this position will yield two code actions for importing
9595

9696
This request is send from client to server to handle "Join Lines" editor action.
9797

98-
**Method:** `experimental/JoinLines`
98+
**Method:** `experimental/joinLines`
9999

100100
**Request:**
101101

@@ -172,3 +172,110 @@ SSR with query `foo($a:expr, $b:expr) ==>> ($a).foo($b)` will transform, eg `foo
172172

173173
* Probably needs search without replace mode
174174
* 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?
220+
221+
## Analyzer Status
222+
223+
**Method:** `rust-analyzer/analyzerStatus`
224+
225+
**Request:** `null`
226+
227+
**Response:** `string`
228+
229+
Returns internal status message, mostly for debugging purposes.
230+
231+
## Collect Garbage
232+
233+
**Method:** `rust-analyzer/collectGarbage`
234+
235+
**Request:** `null`
236+
237+
**Response:** `null`
238+
239+
Frees some caches. For internal use, and is mostly broken at the moment.
240+
241+
## Syntax Tree
242+
243+
**Method:** `rust-analyzer/syntaxTree`
244+
245+
**Request:**
246+
247+
```typescript
248+
interface SyntaxTeeParams {
249+
textDocument: TextDocumentIdentifier,
250+
range?: Range,
251+
}
252+
```
253+
254+
**Response:** `string`
255+
256+
Returns textual representation of a parse tree for the file/selected region.
257+
Primarily for debugging, but very useful for all people working on rust-analyzer itself.
258+
259+
## Expand Macro
260+
261+
**Method:** `rust-analyzer/expandMacro`
262+
263+
**Request:**
264+
265+
```typescript
266+
interface ExpandMacroParams {
267+
textDocument: TextDocumentIdentifier,
268+
position?: Position,
269+
}
270+
```
271+
272+
**Response:**
273+
274+
```typescript
275+
interface ExpandedMacro {
276+
name: string,
277+
expansion: string,
278+
}
279+
```
280+
281+
Expands macro call at a given position.

editors/code/src/commands/matching_brace.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ export function matchingBrace(ctx: Ctx): Cmd {
99
const client = ctx.client;
1010
if (!editor || !client) return;
1111

12-
const response = await client.sendRequest(ra.findMatchingBrace, {
12+
const response = await client.sendRequest(ra.matchingBrace, {
1313
textDocument: { uri: editor.document.uri.toString() },
14-
offsets: editor.selections.map(s =>
14+
positions: editor.selections.map(s =>
1515
client.code2ProtocolConverter.asPosition(s.active),
1616
),
1717
});

editors/code/src/rust-analyzer-api.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,11 @@ export interface ExpandedMacro {
4040
export const expandMacro = request<ExpandMacroParams, Option<ExpandedMacro>>("expandMacro");
4141

4242

43-
export interface FindMatchingBraceParams {
43+
export interface MatchingBraceParams {
4444
textDocument: lc.TextDocumentIdentifier;
45-
offsets: Vec<lc.Position>;
45+
positions: lc.Position[];
4646
}
47-
export const findMatchingBrace = request<FindMatchingBraceParams, Vec<lc.Position>>("findMatchingBrace");
48-
47+
export const matchingBrace = new lc.RequestType<MatchingBraceParams, lc.Position[], unknown>('experimental/matchingBrace');
4948

5049
export interface PublishDecorationsParams {
5150
uri: string;

0 commit comments

Comments
 (0)