From 76e170c3d0d0784c0e612c5849798c65a2034f29 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 25 May 2020 14:12:53 +0200 Subject: Less rust-analyzer specific onEnter --- crates/ra_ide/src/lib.rs | 3 +- crates/ra_ide/src/typing/on_enter.rs | 11 ++---- crates/rust-analyzer/src/caps.rs | 1 + crates/rust-analyzer/src/lsp_ext.rs | 4 +-- crates/rust-analyzer/src/main_loop/handlers.rs | 14 +++++--- crates/rust-analyzer/src/to_proto.rs | 12 +++++++ crates/rust-analyzer/tests/heavy_tests/main.rs | 46 ++++++++------------------ 7 files changed, 43 insertions(+), 48 deletions(-) (limited to 'crates') diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs index 5ac002d82..d983cd910 100644 --- a/crates/ra_ide/src/lib.rs +++ b/crates/ra_ide/src/lib.rs @@ -309,7 +309,8 @@ impl Analysis { /// Returns an edit which should be applied when opening a new line, fixing /// up minor stuff like continuing the comment. - pub fn on_enter(&self, position: FilePosition) -> Cancelable> { + /// The edit will be a snippet (with `$0`). + pub fn on_enter(&self, position: FilePosition) -> Cancelable> { self.with_db(|db| typing::on_enter(&db, position)) } diff --git a/crates/ra_ide/src/typing/on_enter.rs b/crates/ra_ide/src/typing/on_enter.rs index e7d64b4f6..a40d8af9c 100644 --- a/crates/ra_ide/src/typing/on_enter.rs +++ b/crates/ra_ide/src/typing/on_enter.rs @@ -11,9 +11,7 @@ use ra_syntax::{ }; use ra_text_edit::TextEdit; -use crate::{SourceChange, SourceFileEdit}; - -pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option { +pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option { let parse = db.parse(position.file_id); let file = parse.tree(); let comment = file @@ -41,9 +39,7 @@ pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option bool { @@ -90,9 +86,8 @@ mod tests { let (analysis, file_id) = single_file(&before); let result = analysis.on_enter(FilePosition { offset, file_id }).unwrap()?; - assert_eq!(result.source_file_edits.len(), 1); let mut actual = before.to_string(); - result.source_file_edits[0].edit.apply(&mut actual); + result.apply(&mut actual); Some(actual) } diff --git a/crates/rust-analyzer/src/caps.rs b/crates/rust-analyzer/src/caps.rs index 780fc9317..d55cbb15f 100644 --- a/crates/rust-analyzer/src/caps.rs +++ b/crates/rust-analyzer/src/caps.rs @@ -85,6 +85,7 @@ pub fn server_capabilities(client_caps: &ClientCapabilities) -> ServerCapabiliti experimental: Some(json!({ "joinLines": true, "ssr": true, + "onEnter": true, })), } } diff --git a/crates/rust-analyzer/src/lsp_ext.rs b/crates/rust-analyzer/src/lsp_ext.rs index 52e4fcbec..1cce1baa4 100644 --- a/crates/rust-analyzer/src/lsp_ext.rs +++ b/crates/rust-analyzer/src/lsp_ext.rs @@ -102,8 +102,8 @@ pub enum OnEnter {} impl Request for OnEnter { type Params = lsp_types::TextDocumentPositionParams; - type Result = Option; - const METHOD: &'static str = "rust-analyzer/onEnter"; + type Result = Option>; + const METHOD: &'static str = "experimental/onEnter"; } pub enum Runnables {} diff --git a/crates/rust-analyzer/src/main_loop/handlers.rs b/crates/rust-analyzer/src/main_loop/handlers.rs index d73107968..a13a0e1f5 100644 --- a/crates/rust-analyzer/src/main_loop/handlers.rs +++ b/crates/rust-analyzer/src/main_loop/handlers.rs @@ -174,13 +174,17 @@ pub fn handle_join_lines( pub fn handle_on_enter( world: WorldSnapshot, params: lsp_types::TextDocumentPositionParams, -) -> Result> { +) -> Result>> { let _p = profile("handle_on_enter"); let position = from_proto::file_position(&world, params)?; - match world.analysis().on_enter(position)? { - None => Ok(None), - Some(source_change) => to_proto::snippet_workspace_edit(&world, source_change).map(Some), - } + let edit = match world.analysis().on_enter(position)? { + None => return Ok(None), + Some(it) => it, + }; + let line_index = world.analysis().file_line_index(position.file_id)?; + let line_endings = world.file_line_endings(position.file_id); + let edit = to_proto::snippet_text_edit_vec(&line_index, line_endings, true, edit); + Ok(Some(edit)) } // Don't forget to add new trigger characters to `ServerCapabilities` in `caps.rs`. diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index 81a347247..39d58f1e0 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs @@ -135,6 +135,18 @@ pub(crate) fn text_edit_vec( text_edit.into_iter().map(|indel| self::text_edit(line_index, line_endings, indel)).collect() } +pub(crate) fn snippet_text_edit_vec( + line_index: &LineIndex, + line_endings: LineEndings, + is_snippet: bool, + text_edit: TextEdit, +) -> Vec { + text_edit + .into_iter() + .map(|indel| self::snippet_text_edit(line_index, line_endings, is_snippet, indel)) + .collect() +} + pub(crate) fn completion_item( line_index: &LineIndex, line_endings: LineEndings, diff --git a/crates/rust-analyzer/tests/heavy_tests/main.rs b/crates/rust-analyzer/tests/heavy_tests/main.rs index 738a9a8e3..b1bfc968a 100644 --- a/crates/rust-analyzer/tests/heavy_tests/main.rs +++ b/crates/rust-analyzer/tests/heavy_tests/main.rs @@ -473,23 +473,14 @@ fn main() {{}} text_document: server.doc_id("src/m0.rs"), position: Position { line: 0, character: 5 }, }, - json!({ - "documentChanges": [ - { - "edits": [ - { - "insertTextFormat": 2, - "newText": "\n/// $0", - "range": { - "end": { "character": 5, "line": 0 }, - "start": { "character": 5, "line": 0 } - } - } - ], - "textDocument": { "uri": "file:///[..]src/m0.rs", "version": null } + json!([{ + "insertTextFormat": 2, + "newText": "\n/// $0", + "range": { + "end": { "character": 5, "line": 0 }, + "start": { "character": 5, "line": 0 } } - ] - }), + }]), ); let elapsed = start.elapsed(); assert!(elapsed.as_millis() < 2000, "typing enter took {:?}", elapsed); @@ -519,23 +510,14 @@ version = \"0.0.0\" text_document: server.doc_id("src/main.rs"), position: Position { line: 0, character: 8 }, }, - json!({ - "documentChanges": [ - { - "edits": [ - { - "insertTextFormat": 2, - "newText": "\r\n/// $0", - "range": { - "end": { "line": 0, "character": 8 }, - "start": { "line": 0, "character": 8 } - } - } - ], - "textDocument": { "uri": "file:///[..]src/main.rs", "version": null } + json!([{ + "insertTextFormat": 2, + "newText": "\r\n/// $0", + "range": { + "end": { "line": 0, "character": 8 }, + "start": { "line": 0, "character": 8 } } - ] - }), + }]), ); } -- cgit v1.2.3