Skip to content

Commit 90332ca

Browse files
bors[bot]matklad
andauthored
Merge #4560
4560: Use WorkspaceEdit for ssr r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 59732df + 5ef4ebf commit 90332ca

File tree

6 files changed

+43
-9
lines changed

6 files changed

+43
-9
lines changed

crates/rust-analyzer/src/caps.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ pub fn server_capabilities() -> ServerCapabilities {
9494
),
9595
experimental: Some(json!({
9696
"joinLines": true,
97+
"ssr": true,
9798
})),
9899
}
99100
}

crates/rust-analyzer/src/lsp_ext.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ pub enum Ssr {}
173173

174174
impl Request for Ssr {
175175
type Params = SsrParams;
176-
type Result = SourceChange;
177-
const METHOD: &'static str = "rust-analyzer/ssr";
176+
type Result = lsp_types::WorkspaceEdit;
177+
const METHOD: &'static str = "experimental/ssr";
178178
}
179179

180180
#[derive(Debug, Deserialize, Serialize)]

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -986,11 +986,11 @@ pub fn handle_document_highlight(
986986
pub fn handle_ssr(
987987
world: WorldSnapshot,
988988
params: lsp_ext::SsrParams,
989-
) -> Result<lsp_ext::SourceChange> {
989+
) -> Result<lsp_types::WorkspaceEdit> {
990990
let _p = profile("handle_ssr");
991991
let source_change =
992992
world.analysis().structural_search_replace(&params.query, params.parse_only)??;
993-
to_proto::source_change(&world, source_change)
993+
to_proto::workspace_edit(&world, source_change)
994994
}
995995

996996
pub fn publish_diagnostics(world: &WorldSnapshot, file_id: FileId) -> Result<DiagnosticTask> {

docs/dev/lsp-extensions.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,38 @@ fn main() {
8484
* What is the position of the cursor after `joinLines`?
8585
Currently this is left to editor's discretion, but it might be useful to specify on the server via snippets.
8686
However, it then becomes unclear how it works with multi cursor.
87+
88+
## Structural Search Replace (SSR)
89+
90+
**Server Capability:** `{ "ssr": boolean }`
91+
92+
This request is send from client to server to handle structural search replace -- automated syntax tree based transformation of the source.
93+
94+
**Method:** `experimental/ssr`
95+
96+
**Request:**
97+
98+
```typescript
99+
interface SsrParams {
100+
/// Search query.
101+
/// The specific syntax is specified outside of the protocol.
102+
query: string,
103+
/// If true, only check the syntax of the query and don't compute the actual edit.
104+
parseOnly: bool,
105+
}
106+
```
107+
108+
**Response:**
109+
110+
```typescript
111+
WorkspaceEdit
112+
```
113+
114+
### Example
115+
116+
SSR with query `foo($a:expr, $b:expr) ==>> ($a).foo($b)` will transform, eg `foo(y + 5, z)` into `(y + 5).foo(z)`.
117+
118+
### Unresolved Question
119+
120+
* Probably needs search without replace mode
121+
* Needs a way to limit the scope to certain files.

editors/code/src/commands/ssr.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as vscode from 'vscode';
22
import * as ra from "../rust-analyzer-api";
33

44
import { Ctx, Cmd } from '../ctx';
5-
import { applySourceChange } from '../source_change';
65

76
export function ssr(ctx: Ctx): Cmd {
87
return async () => {
@@ -22,11 +21,10 @@ export function ssr(ctx: Ctx): Cmd {
2221
}
2322
};
2423
const request = await vscode.window.showInputBox(options);
25-
2624
if (!request) return;
2725

28-
const change = await client.sendRequest(ra.ssr, { query: request, parseOnly: false });
26+
const edit = await client.sendRequest(ra.ssr, { query: request, parseOnly: false });
2927

30-
await applySourceChange(ctx, change);
28+
await vscode.workspace.applyEdit(client.protocol2CodeConverter.asWorkspaceEdit(edit));
3129
};
3230
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export interface SsrParams {
112112
query: string;
113113
parseOnly: boolean;
114114
}
115-
export const ssr = request<SsrParams, SourceChange>("ssr");
115+
export const ssr = new lc.RequestType<SsrParams, lc.WorkspaceEdit, unknown>('experimental/ssr');
116116

117117

118118
export const publishDecorations = notification<PublishDecorationsParams>("publishDecorations");

0 commit comments

Comments
 (0)