Skip to content

Commit 52da9e9

Browse files
committed
Move on enter to a separate module
1 parent c48dcf7 commit 52da9e9

File tree

2 files changed

+177
-154
lines changed

2 files changed

+177
-154
lines changed

crates/ra_ide/src/typing.rs

Lines changed: 6 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -13,77 +13,21 @@
1313
//! Language server executes such typing assists synchronously. That is, they
1414
//! block user's typing and should be pretty fast for this reason!
1515
16+
mod on_enter;
17+
1618
use ra_db::{FilePosition, SourceDatabase};
1719
use ra_fmt::leading_indent;
1820
use ra_ide_db::RootDatabase;
1921
use ra_syntax::{
2022
algo::find_node_at_offset,
2123
ast::{self, AstToken},
22-
AstNode, SmolStr, SourceFile,
23-
SyntaxKind::*,
24-
SyntaxToken, TextRange, TextUnit, TokenAtOffset,
24+
AstNode, SourceFile, TextRange, TextUnit,
2525
};
2626
use ra_text_edit::TextEdit;
2727

28-
use crate::{source_change::SingleFileChange, SourceChange, SourceFileEdit};
29-
30-
pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option<SourceChange> {
31-
let parse = db.parse(position.file_id);
32-
let file = parse.tree();
33-
let comment = file
34-
.syntax()
35-
.token_at_offset(position.offset)
36-
.left_biased()
37-
.and_then(ast::Comment::cast)?;
38-
39-
if comment.kind().shape.is_block() {
40-
return None;
41-
}
42-
43-
let prefix = comment.prefix();
44-
let comment_range = comment.syntax().text_range();
45-
if position.offset < comment_range.start() + TextUnit::of_str(prefix) {
46-
return None;
47-
}
48-
49-
// Continuing non-doc line comments (like this one :) ) is annoying
50-
if prefix == "//" && comment_range.end() == position.offset {
51-
return None;
52-
}
53-
54-
let indent = node_indent(&file, comment.syntax())?;
55-
let inserted = format!("\n{}{} ", indent, prefix);
56-
let cursor_position = position.offset + TextUnit::of_str(&inserted);
57-
let edit = TextEdit::insert(position.offset, inserted);
28+
use crate::{source_change::SingleFileChange, SourceChange};
5829

59-
Some(
60-
SourceChange::source_file_edit(
61-
"on enter",
62-
SourceFileEdit { edit, file_id: position.file_id },
63-
)
64-
.with_cursor(FilePosition { offset: cursor_position, file_id: position.file_id }),
65-
)
66-
}
67-
68-
fn node_indent(file: &SourceFile, token: &SyntaxToken) -> Option<SmolStr> {
69-
let ws = match file.syntax().token_at_offset(token.text_range().start()) {
70-
TokenAtOffset::Between(l, r) => {
71-
assert!(r == *token);
72-
l
73-
}
74-
TokenAtOffset::Single(n) => {
75-
assert!(n == *token);
76-
return Some("".into());
77-
}
78-
TokenAtOffset::None => unreachable!(),
79-
};
80-
if ws.kind() != WHITESPACE {
81-
return None;
82-
}
83-
let text = ws.text();
84-
let pos = text.rfind('\n').map(|it| it + 1).unwrap_or(0);
85-
Some(text[pos..].into())
86-
}
30+
pub(crate) use on_enter::on_enter;
8731

8832
pub(crate) const TRIGGER_CHARS: &str = ".=>";
8933

@@ -196,102 +140,10 @@ fn on_arrow_typed(file: &SourceFile, offset: TextUnit) -> Option<SingleFileChang
196140

197141
#[cfg(test)]
198142
mod tests {
199-
use test_utils::{add_cursor, assert_eq_text, extract_offset};
200-
201-
use crate::mock_analysis::single_file;
143+
use test_utils::{assert_eq_text, extract_offset};
202144

203145
use super::*;
204146

205-
#[test]
206-
fn test_on_enter() {
207-
fn apply_on_enter(before: &str) -> Option<String> {
208-
let (offset, before) = extract_offset(before);
209-
let (analysis, file_id) = single_file(&before);
210-
let result = analysis.on_enter(FilePosition { offset, file_id }).unwrap()?;
211-
212-
assert_eq!(result.source_file_edits.len(), 1);
213-
let actual = result.source_file_edits[0].edit.apply(&before);
214-
let actual = add_cursor(&actual, result.cursor_position.unwrap().offset);
215-
Some(actual)
216-
}
217-
218-
fn do_check(before: &str, after: &str) {
219-
let actual = apply_on_enter(before).unwrap();
220-
assert_eq_text!(after, &actual);
221-
}
222-
223-
fn do_check_noop(text: &str) {
224-
assert!(apply_on_enter(text).is_none())
225-
}
226-
227-
do_check(
228-
r"
229-
/// Some docs<|>
230-
fn foo() {
231-
}
232-
",
233-
r"
234-
/// Some docs
235-
/// <|>
236-
fn foo() {
237-
}
238-
",
239-
);
240-
do_check(
241-
r"
242-
impl S {
243-
/// Some<|> docs.
244-
fn foo() {}
245-
}
246-
",
247-
r"
248-
impl S {
249-
/// Some
250-
/// <|> docs.
251-
fn foo() {}
252-
}
253-
",
254-
);
255-
do_check(
256-
r"
257-
fn main() {
258-
// Fix<|> me
259-
let x = 1 + 1;
260-
}
261-
",
262-
r"
263-
fn main() {
264-
// Fix
265-
// <|> me
266-
let x = 1 + 1;
267-
}
268-
",
269-
);
270-
do_check(
271-
r"
272-
///<|> Some docs
273-
fn foo() {
274-
}
275-
",
276-
r"
277-
///
278-
/// <|> Some docs
279-
fn foo() {
280-
}
281-
",
282-
);
283-
do_check_noop(
284-
r"
285-
fn main() {
286-
// Fix me<|>
287-
let x = 1 + 1;
288-
}
289-
",
290-
);
291-
292-
do_check_noop(r"<|>//! docz");
293-
}
294-
295147
fn do_type_char(char_typed: char, before: &str) -> Option<(String, SingleFileChange)> {
296148
let (offset, before) = extract_offset(before);
297149
let edit = TextEdit::insert(offset, char_typed.to_string());

crates/ra_ide/src/typing/on_enter.rs

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
//! Handles the `Enter` key press. At the momently, this only continues
2+
//! comments, but should handle indent some time in the future as well.
3+
4+
use ra_db::{FilePosition, SourceDatabase};
5+
use ra_ide_db::RootDatabase;
6+
use ra_syntax::{
7+
ast::{self, AstToken},
8+
AstNode, SmolStr, SourceFile,
9+
SyntaxKind::*,
10+
SyntaxToken, TextUnit, TokenAtOffset,
11+
};
12+
use ra_text_edit::TextEdit;
13+
14+
use crate::{SourceChange, SourceFileEdit};
15+
16+
pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option<SourceChange> {
17+
let parse = db.parse(position.file_id);
18+
let file = parse.tree();
19+
let comment = file
20+
.syntax()
21+
.token_at_offset(position.offset)
22+
.left_biased()
23+
.and_then(ast::Comment::cast)?;
24+
25+
if comment.kind().shape.is_block() {
26+
return None;
27+
}
28+
29+
let prefix = comment.prefix();
30+
let comment_range = comment.syntax().text_range();
31+
if position.offset < comment_range.start() + TextUnit::of_str(prefix) {
32+
return None;
33+
}
34+
35+
// Continuing non-doc line comments (like this one :) ) is annoying
36+
if prefix == "//" && comment_range.end() == position.offset {
37+
return None;
38+
}
39+
40+
let indent = node_indent(&file, comment.syntax())?;
41+
let inserted = format!("\n{}{} ", indent, prefix);
42+
let cursor_position = position.offset + TextUnit::of_str(&inserted);
43+
let edit = TextEdit::insert(position.offset, inserted);
44+
45+
Some(
46+
SourceChange::source_file_edit(
47+
"on enter",
48+
SourceFileEdit { edit, file_id: position.file_id },
49+
)
50+
.with_cursor(FilePosition { offset: cursor_position, file_id: position.file_id }),
51+
)
52+
}
53+
54+
fn node_indent(file: &SourceFile, token: &SyntaxToken) -> Option<SmolStr> {
55+
let ws = match file.syntax().token_at_offset(token.text_range().start()) {
56+
TokenAtOffset::Between(l, r) => {
57+
assert!(r == *token);
58+
l
59+
}
60+
TokenAtOffset::Single(n) => {
61+
assert!(n == *token);
62+
return Some("".into());
63+
}
64+
TokenAtOffset::None => unreachable!(),
65+
};
66+
if ws.kind() != WHITESPACE {
67+
return None;
68+
}
69+
let text = ws.text();
70+
let pos = text.rfind('\n').map(|it| it + 1).unwrap_or(0);
71+
Some(text[pos..].into())
72+
}
73+
74+
#[cfg(test)]
75+
mod tests {
76+
use test_utils::{add_cursor, assert_eq_text, extract_offset};
77+
78+
use crate::mock_analysis::single_file;
79+
80+
use super::*;
81+
82+
fn apply_on_enter(before: &str) -> Option<String> {
83+
let (offset, before) = extract_offset(before);
84+
let (analysis, file_id) = single_file(&before);
85+
let result = analysis.on_enter(FilePosition { offset, file_id }).unwrap()?;
86+
87+
assert_eq!(result.source_file_edits.len(), 1);
88+
let actual = result.source_file_edits[0].edit.apply(&before);
89+
let actual = add_cursor(&actual, result.cursor_position.unwrap().offset);
90+
Some(actual)
91+
}
92+
93+
fn do_check(ra_fixture_before: &str, ra_fixture_after: &str) {
94+
let actual = apply_on_enter(ra_fixture_before).unwrap();
95+
assert_eq_text!(ra_fixture_after, &actual);
96+
}
97+
98+
fn do_check_noop(ra_fixture_text: &str) {
99+
assert!(apply_on_enter(ra_fixture_text).is_none())
100+
}
101+
102+
#[test]
103+
fn test_on_enter() {
104+
do_check(
105+
r"
106+
/// Some docs<|>
107+
fn foo() {
108+
}
109+
",
110+
r"
111+
/// Some docs
112+
/// <|>
113+
fn foo() {
114+
}
115+
",
116+
);
117+
do_check(
118+
r"
119+
impl S {
120+
/// Some<|> docs.
121+
fn foo() {}
122+
}
123+
",
124+
r"
125+
impl S {
126+
/// Some
127+
/// <|> docs.
128+
fn foo() {}
129+
}
130+
",
131+
);
132+
do_check(
133+
r"
134+
fn main() {
135+
// Fix<|> me
136+
let x = 1 + 1;
137+
}
138+
",
139+
r"
140+
fn main() {
141+
// Fix
142+
// <|> me
143+
let x = 1 + 1;
144+
}
145+
",
146+
);
147+
do_check(
148+
r"
149+
///<|> Some docs
150+
fn foo() {
151+
}
152+
",
153+
r"
154+
///
155+
/// <|> Some docs
156+
fn foo() {
157+
}
158+
",
159+
);
160+
do_check_noop(
161+
r"
162+
fn main() {
163+
// Fix me<|>
164+
let x = 1 + 1;
165+
}
166+
",
167+
);
168+
169+
do_check_noop(r"<|>//! docz");
170+
}
171+
}

0 commit comments

Comments
 (0)