rust/crates/ra_ide/src/typing/on_enter.rs

206 lines
4.3 KiB
Rust
Raw Normal View History

2020-03-11 12:46:36 +01:00
//! Handles the `Enter` key press. At the momently, this only continues
//! comments, but should handle indent some time in the future as well.
use ra_db::{FilePosition, SourceDatabase};
use ra_ide_db::RootDatabase;
use ra_syntax::{
ast::{self, AstToken},
AstNode, SmolStr, SourceFile,
SyntaxKind::*,
2020-04-24 23:40:41 +02:00
SyntaxToken, TextSize, TokenAtOffset,
2020-03-11 12:46:36 +01:00
};
use ra_text_edit::TextEdit;
2020-05-25 14:12:53 +02:00
pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option<TextEdit> {
2020-03-11 12:46:36 +01:00
let parse = db.parse(position.file_id);
let file = parse.tree();
let comment = file
.syntax()
.token_at_offset(position.offset)
.left_biased()
.and_then(ast::Comment::cast)?;
if comment.kind().shape.is_block() {
return None;
}
let prefix = comment.prefix();
let comment_range = comment.syntax().text_range();
2020-04-24 23:40:41 +02:00
if position.offset < comment_range.start() + TextSize::of(prefix) {
2020-03-11 12:46:36 +01:00
return None;
}
// Continuing single-line non-doc comments (like this one :) ) is annoying
if prefix == "//" && comment_range.end() == position.offset && !followed_by_comment(&comment) {
2020-03-11 12:46:36 +01:00
return None;
}
let indent = node_indent(&file, comment.syntax())?;
let inserted = format!("\n{}{} $0", indent, prefix);
2020-03-11 12:46:36 +01:00
let edit = TextEdit::insert(position.offset, inserted);
2020-05-25 14:12:53 +02:00
Some(edit)
2020-03-11 12:46:36 +01:00
}
fn followed_by_comment(comment: &ast::Comment) -> bool {
let ws = match comment.syntax().next_token().and_then(ast::Whitespace::cast) {
Some(it) => it,
None => return false,
};
if ws.spans_multiple_lines() {
return false;
}
ws.syntax().next_token().and_then(ast::Comment::cast).is_some()
}
2020-03-11 12:46:36 +01:00
fn node_indent(file: &SourceFile, token: &SyntaxToken) -> Option<SmolStr> {
let ws = match file.syntax().token_at_offset(token.text_range().start()) {
TokenAtOffset::Between(l, r) => {
assert!(r == *token);
l
}
TokenAtOffset::Single(n) => {
assert!(n == *token);
return Some("".into());
}
TokenAtOffset::None => unreachable!(),
};
if ws.kind() != WHITESPACE {
return None;
}
let text = ws.text();
let pos = text.rfind('\n').map(|it| it + 1).unwrap_or(0);
Some(text[pos..].into())
}
#[cfg(test)]
mod tests {
2020-06-24 11:05:47 +02:00
use test_utils::assert_eq_text;
2020-03-11 12:46:36 +01:00
2020-06-24 11:05:47 +02:00
use crate::mock_analysis::single_file_with_position;
2020-06-24 11:29:43 +02:00
use stdx::trim_indent;
2020-03-11 12:46:36 +01:00
fn apply_on_enter(before: &str) -> Option<String> {
2020-06-24 11:05:47 +02:00
let (analysis, position) = single_file_with_position(&before);
let result = analysis.on_enter(position).unwrap()?;
2020-03-11 12:46:36 +01:00
2020-06-24 11:05:47 +02:00
let mut actual = analysis.file_text(position.file_id).unwrap().to_string();
2020-05-25 14:12:53 +02:00
result.apply(&mut actual);
2020-03-11 12:46:36 +01:00
Some(actual)
}
fn do_check(ra_fixture_before: &str, ra_fixture_after: &str) {
2020-06-24 11:29:43 +02:00
let ra_fixture_after = &trim_indent(ra_fixture_after);
2020-03-11 12:46:36 +01:00
let actual = apply_on_enter(ra_fixture_before).unwrap();
assert_eq_text!(ra_fixture_after, &actual);
}
fn do_check_noop(ra_fixture_text: &str) {
assert!(apply_on_enter(ra_fixture_text).is_none())
}
#[test]
2020-03-11 12:49:17 +01:00
fn continues_doc_comment() {
2020-03-11 12:46:36 +01:00
do_check(
r"
/// Some docs<|>
fn foo() {
}
",
r"
/// Some docs
/// $0
2020-03-11 12:46:36 +01:00
fn foo() {
}
",
);
2020-03-11 12:49:17 +01:00
2020-03-11 12:46:36 +01:00
do_check(
r"
impl S {
/// Some<|> docs.
fn foo() {}
}
",
r"
impl S {
/// Some
/// $0 docs.
2020-03-11 12:46:36 +01:00
fn foo() {}
}
",
);
2020-03-11 12:49:17 +01:00
2020-03-11 12:46:36 +01:00
do_check(
r"
2020-03-11 12:49:17 +01:00
///<|> Some docs
fn foo() {
2020-03-11 12:46:36 +01:00
}
",
r"
2020-03-11 12:49:17 +01:00
///
/// $0 Some docs
2020-03-11 12:49:17 +01:00
fn foo() {
2020-03-11 12:46:36 +01:00
}
",
);
2020-03-11 12:49:17 +01:00
}
#[test]
fn does_not_continue_before_doc_comment() {
do_check_noop(r"<|>//! docz");
}
#[test]
fn continues_code_comment_in_the_middle_of_line() {
2020-03-11 12:46:36 +01:00
do_check(
r"
2020-03-11 12:49:17 +01:00
fn main() {
// Fix<|> me
let x = 1 + 1;
2020-03-11 12:46:36 +01:00
}
",
r"
2020-03-11 12:49:17 +01:00
fn main() {
// Fix
// $0 me
2020-03-11 12:49:17 +01:00
let x = 1 + 1;
2020-03-11 12:46:36 +01:00
}
",
);
2020-03-11 12:49:17 +01:00
}
#[test]
fn continues_code_comment_in_the_middle_several_lines() {
do_check(
r"
fn main() {
// Fix<|>
// me
let x = 1 + 1;
}
",
r"
fn main() {
// Fix
// $0
// me
let x = 1 + 1;
}
",
);
}
2020-03-11 12:49:17 +01:00
#[test]
fn does_not_continue_end_of_code_comment() {
2020-03-11 12:46:36 +01:00
do_check_noop(
r"
fn main() {
// Fix me<|>
let x = 1 + 1;
}
",
);
}
}