aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/typing
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-05-21 13:26:44 +0100
committerAleksey Kladov <[email protected]>2020-05-21 14:08:03 +0100
commit4b495da368162a5b373d078be4ff51e55bffdf69 (patch)
treec35bf74905b9f03343fc10d834926e3457bc181c /crates/ra_ide/src/typing
parenta4e6963a2313971fe7bbec97d03bc67266ef68a9 (diff)
Transition OnEnter to WorkspaceSnippetEdit
This also changes our handiling of snippet edits on the client side. `editor.insertSnippet` unfortunately forces indentation, which we really don't want to have to deal with. So, let's just implement our manual hacky way of dealing with a simple subset of snippets we actually use in rust-analyzer
Diffstat (limited to 'crates/ra_ide/src/typing')
-rw-r--r--crates/ra_ide/src/typing/on_enter.rs29
1 files changed, 13 insertions, 16 deletions
diff --git a/crates/ra_ide/src/typing/on_enter.rs b/crates/ra_ide/src/typing/on_enter.rs
index 78a40cc94..85be14ad3 100644
--- a/crates/ra_ide/src/typing/on_enter.rs
+++ b/crates/ra_ide/src/typing/on_enter.rs
@@ -38,17 +38,15 @@ pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option<Sour
38 } 38 }
39 39
40 let indent = node_indent(&file, comment.syntax())?; 40 let indent = node_indent(&file, comment.syntax())?;
41 let inserted = format!("\n{}{} ", indent, prefix); 41 let inserted = format!("\n{}{} $0", indent, prefix);
42 let cursor_position = position.offset + TextSize::of(&inserted);
43 let edit = TextEdit::insert(position.offset, inserted); 42 let edit = TextEdit::insert(position.offset, inserted);
44 43
45 Some( 44 let mut res = SourceChange::source_file_edit(
46 SourceChange::source_file_edit( 45 "On enter",
47 "On enter", 46 SourceFileEdit { edit, file_id: position.file_id },
48 SourceFileEdit { edit, file_id: position.file_id }, 47 );
49 ) 48 res.is_snippet = true;
50 .with_cursor(FilePosition { offset: cursor_position, file_id: position.file_id }), 49 Some(res)
51 )
52} 50}
53 51
54fn followed_by_comment(comment: &ast::Comment) -> bool { 52fn followed_by_comment(comment: &ast::Comment) -> bool {
@@ -84,7 +82,7 @@ fn node_indent(file: &SourceFile, token: &SyntaxToken) -> Option<SmolStr> {
84 82
85#[cfg(test)] 83#[cfg(test)]
86mod tests { 84mod tests {
87 use test_utils::{add_cursor, assert_eq_text, extract_offset}; 85 use test_utils::{assert_eq_text, extract_offset};
88 86
89 use crate::mock_analysis::single_file; 87 use crate::mock_analysis::single_file;
90 88
@@ -98,7 +96,6 @@ mod tests {
98 assert_eq!(result.source_file_edits.len(), 1); 96 assert_eq!(result.source_file_edits.len(), 1);
99 let mut actual = before.to_string(); 97 let mut actual = before.to_string();
100 result.source_file_edits[0].edit.apply(&mut actual); 98 result.source_file_edits[0].edit.apply(&mut actual);
101 let actual = add_cursor(&actual, result.cursor_position.unwrap().offset);
102 Some(actual) 99 Some(actual)
103 } 100 }
104 101
@@ -121,7 +118,7 @@ fn foo() {
121", 118",
122 r" 119 r"
123/// Some docs 120/// Some docs
124/// <|> 121/// $0
125fn foo() { 122fn foo() {
126} 123}
127", 124",
@@ -137,7 +134,7 @@ impl S {
137 r" 134 r"
138impl S { 135impl S {
139 /// Some 136 /// Some
140 /// <|> docs. 137 /// $0 docs.
141 fn foo() {} 138 fn foo() {}
142} 139}
143", 140",
@@ -151,7 +148,7 @@ fn foo() {
151", 148",
152 r" 149 r"
153/// 150///
154/// <|> Some docs 151/// $0 Some docs
155fn foo() { 152fn foo() {
156} 153}
157", 154",
@@ -175,7 +172,7 @@ fn main() {
175 r" 172 r"
176fn main() { 173fn main() {
177 // Fix 174 // Fix
178 // <|> me 175 // $0 me
179 let x = 1 + 1; 176 let x = 1 + 1;
180} 177}
181", 178",
@@ -195,7 +192,7 @@ fn main() {
195 r" 192 r"
196fn main() { 193fn main() {
197 // Fix 194 // Fix
198 // <|> 195 // $0
199 // me 196 // me
200 let x = 1 + 1; 197 let x = 1 + 1;
201} 198}