aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-05-21 23:28:49 +0100
committerAleksey Kladov <[email protected]>2020-05-21 23:28:49 +0100
commit5ef4ebff2017d7bdfa03f0eccb9960a86c9b94ca (patch)
tree6458549d833ba628fd86d050cb5dd1238c590fc3
parent59732df8d40dfadc6dcf5951265416576399712a (diff)
Use WorkspaceEdit for ssr
-rw-r--r--crates/rust-analyzer/src/caps.rs1
-rw-r--r--crates/rust-analyzer/src/lsp_ext.rs4
-rw-r--r--crates/rust-analyzer/src/main_loop/handlers.rs4
-rw-r--r--docs/dev/lsp-extensions.md35
-rw-r--r--editors/code/src/commands/ssr.ts6
-rw-r--r--editors/code/src/rust-analyzer-api.ts2
6 files changed, 43 insertions, 9 deletions
diff --git a/crates/rust-analyzer/src/caps.rs b/crates/rust-analyzer/src/caps.rs
index 4c417c270..13af75469 100644
--- a/crates/rust-analyzer/src/caps.rs
+++ b/crates/rust-analyzer/src/caps.rs
@@ -94,6 +94,7 @@ pub fn server_capabilities() -> ServerCapabilities {
94 ), 94 ),
95 experimental: Some(json!({ 95 experimental: Some(json!({
96 "joinLines": true, 96 "joinLines": true,
97 "ssr": true,
97 })), 98 })),
98 } 99 }
99} 100}
diff --git a/crates/rust-analyzer/src/lsp_ext.rs b/crates/rust-analyzer/src/lsp_ext.rs
index 1bb1b02ab..0fd60caf4 100644
--- a/crates/rust-analyzer/src/lsp_ext.rs
+++ b/crates/rust-analyzer/src/lsp_ext.rs
@@ -173,8 +173,8 @@ pub enum Ssr {}
173 173
174impl Request for Ssr { 174impl Request for Ssr {
175 type Params = SsrParams; 175 type Params = SsrParams;
176 type Result = SourceChange; 176 type Result = lsp_types::WorkspaceEdit;
177 const METHOD: &'static str = "rust-analyzer/ssr"; 177 const METHOD: &'static str = "experimental/ssr";
178} 178}
179 179
180#[derive(Debug, Deserialize, Serialize)] 180#[derive(Debug, Deserialize, Serialize)]
diff --git a/crates/rust-analyzer/src/main_loop/handlers.rs b/crates/rust-analyzer/src/main_loop/handlers.rs
index 121964718..25e660bd5 100644
--- a/crates/rust-analyzer/src/main_loop/handlers.rs
+++ b/crates/rust-analyzer/src/main_loop/handlers.rs
@@ -986,11 +986,11 @@ pub fn handle_document_highlight(
986pub fn handle_ssr( 986pub fn handle_ssr(
987 world: WorldSnapshot, 987 world: WorldSnapshot,
988 params: lsp_ext::SsrParams, 988 params: lsp_ext::SsrParams,
989) -> Result<lsp_ext::SourceChange> { 989) -> Result<lsp_types::WorkspaceEdit> {
990 let _p = profile("handle_ssr"); 990 let _p = profile("handle_ssr");
991 let source_change = 991 let source_change =
992 world.analysis().structural_search_replace(&params.query, params.parse_only)??; 992 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)
994} 994}
995 995
996pub fn publish_diagnostics(world: &WorldSnapshot, file_id: FileId) -> Result<DiagnosticTask> { 996pub fn publish_diagnostics(world: &WorldSnapshot, file_id: FileId) -> Result<DiagnosticTask> {
diff --git a/docs/dev/lsp-extensions.md b/docs/dev/lsp-extensions.md
index 0e3a0af1c..7c45aef4c 100644
--- a/docs/dev/lsp-extensions.md
+++ b/docs/dev/lsp-extensions.md
@@ -84,3 +84,38 @@ fn main() {
84* What is the position of the cursor after `joinLines`? 84* What is the position of the cursor after `joinLines`?
85 Currently this is left to editor's discretion, but it might be useful to specify on the server via snippets. 85 Currently this is left to editor's discretion, but it might be useful to specify on the server via snippets.
86 However, it then becomes unclear how it works with multi cursor. 86 However, it then becomes unclear how it works with multi cursor.
87
88## Structural Search Replace (SSR)
89
90**Server Capability:** `{ "ssr": boolean }`
91
92This 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
99interface 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
111WorkspaceEdit
112```
113
114### Example
115
116SSR 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.
diff --git a/editors/code/src/commands/ssr.ts b/editors/code/src/commands/ssr.ts
index 4ef8cdf04..5d40a64d2 100644
--- a/editors/code/src/commands/ssr.ts
+++ b/editors/code/src/commands/ssr.ts
@@ -2,7 +2,6 @@ import * as vscode from 'vscode';
2import * as ra from "../rust-analyzer-api"; 2import * as ra from "../rust-analyzer-api";
3 3
4import { Ctx, Cmd } from '../ctx'; 4import { Ctx, Cmd } from '../ctx';
5import { applySourceChange } from '../source_change';
6 5
7export function ssr(ctx: Ctx): Cmd { 6export function ssr(ctx: Ctx): Cmd {
8 return async () => { 7 return async () => {
@@ -22,11 +21,10 @@ export function ssr(ctx: Ctx): Cmd {
22 } 21 }
23 }; 22 };
24 const request = await vscode.window.showInputBox(options); 23 const request = await vscode.window.showInputBox(options);
25
26 if (!request) return; 24 if (!request) return;
27 25
28 const change = await client.sendRequest(ra.ssr, { query: request, parseOnly: false }); 26 const edit = await client.sendRequest(ra.ssr, { query: request, parseOnly: false });
29 27
30 await applySourceChange(ctx, change); 28 await vscode.workspace.applyEdit(client.protocol2CodeConverter.asWorkspaceEdit(edit));
31 }; 29 };
32} 30}
diff --git a/editors/code/src/rust-analyzer-api.ts b/editors/code/src/rust-analyzer-api.ts
index 8ed56c173..73f36432f 100644
--- a/editors/code/src/rust-analyzer-api.ts
+++ b/editors/code/src/rust-analyzer-api.ts
@@ -112,7 +112,7 @@ export interface SsrParams {
112 query: string; 112 query: string;
113 parseOnly: boolean; 113 parseOnly: boolean;
114} 114}
115export const ssr = request<SsrParams, SourceChange>("ssr"); 115export const ssr = new lc.RequestType<SsrParams, lc.WorkspaceEdit, unknown>('experimental/ssr');
116 116
117 117
118export const publishDecorations = notification<PublishDecorationsParams>("publishDecorations"); 118export const publishDecorations = notification<PublishDecorationsParams>("publishDecorations");