rust/crates/ra_ide_api/src/typing.rs

420 lines
12 KiB
Rust
Raw Normal View History

2018-09-16 04:54:24 -05:00
use ra_syntax::{
2019-01-11 05:57:19 -06:00
AstNode, SourceFile, SyntaxKind::*,
2019-03-30 05:25:53 -05:00
TextUnit, TextRange, SyntaxToken,
algo::{find_node_at_offset, find_token_at_offset, TokenAtOffset},
2019-04-02 02:23:18 -05:00
ast::{self, AstToken},
2018-08-23 12:55:23 -05:00
};
use ra_fmt::leading_indent;
2019-03-23 10:55:47 -05:00
use ra_text_edit::{TextEdit, TextEditBuilder};
use ra_db::{FilePosition, SourceDatabase};
use crate::{db::RootDatabase, SourceChange, SourceFileEdit};
2018-08-23 12:55:23 -05:00
2019-03-23 10:55:47 -05:00
pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option<SourceChange> {
2019-05-28 10:46:11 -05:00
let file = db.parse(position.file_id).tree;
2019-03-30 05:25:53 -05:00
let comment = find_token_at_offset(file.syntax(), position.offset)
2019-03-23 10:55:47 -05:00
.left_biased()
.and_then(ast::Comment::cast)?;
2018-10-11 09:25:35 -05:00
2019-04-02 04:18:52 -05:00
if comment.kind().shape.is_block() {
2018-10-11 09:25:35 -05:00
return None;
}
let prefix = comment.prefix();
2019-03-23 10:55:47 -05:00
if position.offset
< comment.syntax().range().start() + TextUnit::of_str(prefix) + TextUnit::from(1)
{
return None;
}
2019-03-23 10:55:47 -05:00
let indent = node_indent(&file, comment.syntax())?;
2018-10-11 09:25:35 -05:00
let inserted = format!("\n{}{} ", indent, prefix);
2019-03-23 10:55:47 -05:00
let cursor_position = position.offset + TextUnit::of_str(&inserted);
2019-01-03 09:59:17 -06:00
let mut edit = TextEditBuilder::default();
2019-03-23 10:55:47 -05:00
edit.insert(position.offset, inserted);
Some(
SourceChange::source_file_edit(
"on enter",
SourceFileEdit { edit: edit.finish(), file_id: position.file_id },
)
.with_cursor(FilePosition { offset: cursor_position, file_id: position.file_id }),
)
}
2019-03-30 05:25:53 -05:00
fn node_indent<'a>(file: &'a SourceFile, token: SyntaxToken) -> Option<&'a str> {
let ws = match find_token_at_offset(file.syntax(), token.range().start()) {
TokenAtOffset::Between(l, r) => {
assert!(r == token);
l
}
2019-03-30 05:25:53 -05:00
TokenAtOffset::Single(n) => {
assert!(n == token);
return Some("");
}
2019-03-30 05:25:53 -05:00
TokenAtOffset::None => unreachable!(),
};
if ws.kind() != WHITESPACE {
return None;
}
2019-03-30 05:25:53 -05:00
let text = ws.text();
let pos = text.rfind('\n').map(|it| it + 1).unwrap_or(0);
Some(&text[pos..])
}
2019-03-23 10:55:47 -05:00
pub fn on_eq_typed(file: &SourceFile, eq_offset: TextUnit) -> Option<TextEdit> {
2019-01-11 05:57:19 -06:00
assert_eq!(file.syntax().text().char_at(eq_offset), Some('='));
let let_stmt: &ast::LetStmt = find_node_at_offset(file.syntax(), eq_offset)?;
2018-08-28 03:12:42 -05:00
if let_stmt.has_semi() {
return None;
}
if let Some(expr) = let_stmt.initializer() {
let expr_range = expr.syntax().range();
2019-01-11 05:57:19 -06:00
if expr_range.contains(eq_offset) && eq_offset != expr_range.start() {
2018-08-28 03:12:42 -05:00
return None;
}
2019-02-08 05:49:43 -06:00
if file.syntax().text().slice(eq_offset..expr_range.start()).contains('\n') {
2018-08-28 13:45:59 -05:00
return None;
}
2018-08-28 03:17:08 -05:00
} else {
return None;
2018-08-28 03:12:42 -05:00
}
let offset = let_stmt.syntax().range().end();
2019-01-03 09:59:17 -06:00
let mut edit = TextEditBuilder::default();
2018-08-28 03:12:42 -05:00
edit.insert(offset, ";".to_string());
2019-03-23 10:55:47 -05:00
Some(edit.finish())
2018-08-28 03:12:42 -05:00
}
2019-03-23 10:55:47 -05:00
pub(crate) fn on_dot_typed(db: &RootDatabase, position: FilePosition) -> Option<SourceChange> {
2019-05-28 10:46:11 -05:00
let file = db.parse(position.file_id).tree;
2019-03-23 10:55:47 -05:00
assert_eq!(file.syntax().text().char_at(position.offset), Some('.'));
2019-01-05 17:58:03 -06:00
2019-03-30 05:25:53 -05:00
let whitespace = find_token_at_offset(file.syntax(), position.offset)
2019-01-11 05:57:19 -06:00
.left_biased()
.and_then(ast::Whitespace::cast)?;
2019-01-06 14:59:14 -06:00
2019-01-11 05:57:19 -06:00
let current_indent = {
let text = whitespace.text();
let newline = text.rfind('\n')?;
&text[newline + 1..]
};
let current_indent_len = TextUnit::of_str(current_indent);
2019-01-06 14:59:14 -06:00
2019-01-11 05:57:19 -06:00
// Make sure dot is a part of call chain
2019-03-30 05:25:53 -05:00
let field_expr = ast::FieldExpr::cast(whitespace.syntax().parent())?;
2019-01-11 05:57:19 -06:00
let prev_indent = leading_indent(field_expr.syntax())?;
let target_indent = format!(" {}", prev_indent);
let target_indent_len = TextUnit::of_str(&target_indent);
if current_indent_len == target_indent_len {
return None;
}
2019-01-05 17:58:03 -06:00
let mut edit = TextEditBuilder::default();
2019-01-11 05:57:19 -06:00
edit.replace(
2019-03-23 10:55:47 -05:00
TextRange::from_to(position.offset - current_indent_len, position.offset),
2019-01-11 05:57:19 -06:00
target_indent.into(),
);
let res = SourceChange::source_file_edit_from("reindent dot", position.file_id, edit.finish())
.with_cursor(FilePosition {
2019-03-23 10:55:47 -05:00
offset: position.offset + target_indent_len - current_indent_len
+ TextUnit::of_char('.'),
file_id: position.file_id,
});
2019-01-11 05:57:19 -06:00
Some(res)
2019-01-06 14:59:14 -06:00
}
2018-08-28 06:47:12 -05:00
#[cfg(test)]
mod tests {
2019-03-21 13:51:42 -05:00
use test_utils::{add_cursor, assert_eq_text, extract_offset};
2018-10-11 10:11:00 -05:00
2019-03-23 10:55:47 -05:00
use crate::mock_analysis::single_file;
2019-01-10 08:50:49 -06:00
use super::*;
2018-08-28 06:47:12 -05:00
#[test]
fn test_on_eq_typed() {
2019-01-11 06:48:06 -06:00
fn type_eq(before: &str, after: &str) {
2018-08-28 06:47:12 -05:00
let (offset, before) = extract_offset(before);
2019-01-11 06:48:06 -06:00
let mut edit = TextEditBuilder::default();
edit.insert(offset, "=".to_string());
let before = edit.finish().apply(&before);
2019-05-28 10:46:11 -05:00
let file = SourceFile::parse(&before).tree;
2019-01-11 06:48:06 -06:00
if let Some(result) = on_eq_typed(&file, offset) {
2019-03-23 10:55:47 -05:00
let actual = result.apply(&before);
2019-01-11 06:48:06 -06:00
assert_eq_text!(after, &actual);
} else {
assert_eq_text!(&before, after)
};
2018-08-28 06:47:12 -05:00
}
// do_check(r"
// fn foo() {
// let foo =<|>
// }
// ", r"
// fn foo() {
// let foo =;
// }
// ");
2019-01-11 06:48:06 -06:00
type_eq(
r"
2018-08-28 06:47:12 -05:00
fn foo() {
2019-01-11 06:48:06 -06:00
let foo <|> 1 + 1
2018-08-28 06:47:12 -05:00
}
",
r"
2018-08-28 06:47:12 -05:00
fn foo() {
let foo = 1 + 1;
}
",
);
2018-08-28 06:47:12 -05:00
// do_check(r"
// fn foo() {
// let foo =<|>
// let bar = 1;
// }
// ", r"
// fn foo() {
// let foo =;
// let bar = 1;
// }
// ");
}
2019-01-11 06:48:06 -06:00
fn type_dot(before: &str, after: &str) {
let (offset, before) = extract_offset(before);
let mut edit = TextEditBuilder::default();
edit.insert(offset, ".".to_string());
let before = edit.finish().apply(&before);
2019-03-23 10:55:47 -05:00
let (analysis, file_id) = single_file(&before);
if let Some(result) = analysis.on_dot_typed(FilePosition { offset, file_id }) {
assert_eq!(result.source_file_edits.len(), 1);
let actual = result.source_file_edits[0].edit.apply(&before);
2019-01-11 06:48:06 -06:00
assert_eq_text!(after, &actual);
} else {
assert_eq_text!(&before, after)
};
}
2019-01-05 17:58:03 -06:00
#[test]
2019-01-11 06:48:06 -06:00
fn indents_new_chain_call() {
type_dot(
2019-01-05 17:58:03 -06:00
r"
2019-01-11 05:57:19 -06:00
pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
self.child_impl(db, name)
2019-01-11 06:48:06 -06:00
<|>
2019-01-11 05:57:19 -06:00
}
",
2019-01-05 17:58:03 -06:00
r"
2019-01-11 05:57:19 -06:00
pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
self.child_impl(db, name)
.
}
",
2019-01-06 05:24:33 -06:00
);
2019-01-11 06:48:06 -06:00
type_dot(
2019-01-06 05:24:33 -06:00
r"
2019-01-11 05:57:19 -06:00
pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
self.child_impl(db, name)
2019-01-11 06:48:06 -06:00
<|>
2019-01-11 05:57:19 -06:00
}
",
2019-01-06 05:24:33 -06:00
r"
2019-01-11 05:57:19 -06:00
pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
self.child_impl(db, name)
.
}
",
2019-01-11 06:48:06 -06:00
)
}
2019-01-06 05:24:33 -06:00
2019-01-13 09:21:23 -06:00
#[test]
fn indents_new_chain_call_with_semi() {
type_dot(
r"
pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
self.child_impl(db, name)
<|>;
}
",
r"
pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
self.child_impl(db, name)
.;
}
",
);
type_dot(
r"
pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
self.child_impl(db, name)
<|>;
}
",
r"
pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
self.child_impl(db, name)
.;
}
",
)
}
2019-01-11 06:48:06 -06:00
#[test]
fn indents_continued_chain_call() {
type_dot(
2019-01-06 05:24:33 -06:00
r"
2019-01-11 05:57:19 -06:00
pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
self.child_impl(db, name)
.first()
2019-01-11 06:48:06 -06:00
<|>
2019-01-11 05:57:19 -06:00
}
",
2019-01-06 05:24:33 -06:00
r"
2019-01-11 05:57:19 -06:00
pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
self.child_impl(db, name)
.first()
.
}
",
2019-01-06 05:24:33 -06:00
);
2019-01-11 06:48:06 -06:00
type_dot(
2019-01-06 05:24:33 -06:00
r"
2019-01-11 05:57:19 -06:00
pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
self.child_impl(db, name)
.first()
2019-01-11 06:48:06 -06:00
<|>
2019-01-11 05:57:19 -06:00
}
",
2019-01-06 05:24:33 -06:00
r"
2019-01-11 05:57:19 -06:00
pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
self.child_impl(db, name)
.first()
.
}
",
2019-01-06 14:59:14 -06:00
);
2019-01-11 06:48:06 -06:00
}
2019-01-06 14:59:14 -06:00
2019-01-25 02:23:15 -06:00
#[test]
fn indents_middle_of_chain_call() {
type_dot(
r"
fn source_impl() {
let var = enum_defvariant_list().unwrap()
<|>
.nth(92)
.unwrap();
}
",
r"
fn source_impl() {
let var = enum_defvariant_list().unwrap()
.
.nth(92)
.unwrap();
}
",
);
type_dot(
r"
fn source_impl() {
let var = enum_defvariant_list().unwrap()
<|>
.nth(92)
.unwrap();
}
",
r"
fn source_impl() {
let var = enum_defvariant_list().unwrap()
.
.nth(92)
.unwrap();
}
",
);
}
2019-01-11 06:48:06 -06:00
#[test]
fn dont_indent_freestanding_dot() {
type_dot(
2019-01-06 14:59:14 -06:00
r"
2019-01-11 05:57:19 -06:00
pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
2019-01-11 06:48:06 -06:00
<|>
2019-01-11 05:57:19 -06:00
}
",
2019-01-06 14:59:14 -06:00
r"
2019-01-11 05:57:19 -06:00
pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
.
}
",
2019-01-06 14:59:14 -06:00
);
2019-01-11 06:48:06 -06:00
type_dot(
2019-01-06 14:59:14 -06:00
r"
2019-01-11 05:57:19 -06:00
pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
2019-01-11 06:48:06 -06:00
<|>
}
2019-01-11 05:57:19 -06:00
",
2019-01-06 14:59:14 -06:00
r"
2019-01-11 05:57:19 -06:00
pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
2019-01-11 06:48:06 -06:00
.
2019-01-11 05:57:19 -06:00
}
",
2019-01-05 17:58:03 -06:00
);
}
#[test]
fn test_on_enter() {
fn apply_on_enter(before: &str) -> Option<String> {
let (offset, before) = extract_offset(before);
2019-03-23 10:55:47 -05:00
let (analysis, file_id) = single_file(&before);
let result = analysis.on_enter(FilePosition { offset, file_id })?;
assert_eq!(result.source_file_edits.len(), 1);
let actual = result.source_file_edits[0].edit.apply(&before);
let actual = add_cursor(&actual, result.cursor_position.unwrap().offset);
Some(actual)
}
fn do_check(before: &str, after: &str) {
let actual = apply_on_enter(before).unwrap();
assert_eq_text!(after, &actual);
}
fn do_check_noop(text: &str) {
assert!(apply_on_enter(text).is_none())
}
do_check(
r"
/// Some docs<|>
fn foo() {
}
",
r"
/// Some docs
/// <|>
fn foo() {
}
",
);
do_check(
r"
impl S {
/// Some<|> docs.
fn foo() {}
}
",
r"
impl S {
/// Some
/// <|> docs.
fn foo() {}
}
",
);
do_check_noop(r"<|>//! docz");
}
2018-08-28 06:47:12 -05:00
}