2018-09-16 04:54:24 -05:00
|
|
|
use ra_syntax::{
|
2019-01-10 08:50:49 -06:00
|
|
|
algo::{find_node_at_offset, find_leaf_at_offset, LeafAtOffset},
|
2018-08-25 05:42:40 -05:00
|
|
|
ast,
|
2019-01-10 08:50:49 -06:00
|
|
|
AstNode, Direction, SourceFile, SyntaxKind::*,
|
|
|
|
SyntaxNode, TextUnit,
|
2018-08-23 12:55:23 -05:00
|
|
|
};
|
|
|
|
|
2019-01-08 11:44:31 -06:00
|
|
|
use crate::{LocalEdit, TextEditBuilder};
|
2018-08-23 12:55:23 -05:00
|
|
|
|
2019-01-07 07:53:24 -06:00
|
|
|
pub fn on_enter(file: &SourceFile, offset: TextUnit) -> Option<LocalEdit> {
|
2018-10-15 16:44:23 -05:00
|
|
|
let comment = find_leaf_at_offset(file.syntax(), offset)
|
|
|
|
.left_biased()
|
2018-10-17 18:25:37 -05:00
|
|
|
.and_then(ast::Comment::cast)?;
|
2018-10-11 09:25:35 -05:00
|
|
|
|
|
|
|
if let ast::CommentFlavor::Multiline = comment.flavor() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let prefix = comment.prefix();
|
|
|
|
if offset < comment.syntax().range().start() + TextUnit::of_str(prefix) + TextUnit::from(1) {
|
2018-10-09 08:00:20 -05:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2018-10-11 09:25:35 -05:00
|
|
|
let indent = node_indent(file, comment.syntax())?;
|
|
|
|
let inserted = format!("\n{}{} ", indent, prefix);
|
2018-10-09 08:00:20 -05:00
|
|
|
let cursor_position = offset + TextUnit::of_str(&inserted);
|
2019-01-03 09:59:17 -06:00
|
|
|
let mut edit = TextEditBuilder::default();
|
2018-10-09 08:00:20 -05:00
|
|
|
edit.insert(offset, inserted);
|
|
|
|
Some(LocalEdit {
|
2018-12-25 10:45:13 -06:00
|
|
|
label: "on enter".to_string(),
|
2018-10-09 08:00:20 -05:00
|
|
|
edit: edit.finish(),
|
|
|
|
cursor_position: Some(cursor_position),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-01-07 07:53:24 -06:00
|
|
|
fn node_indent<'a>(file: &'a SourceFile, node: &SyntaxNode) -> Option<&'a str> {
|
2018-10-09 08:00:20 -05:00
|
|
|
let ws = match find_leaf_at_offset(file.syntax(), node.range().start()) {
|
|
|
|
LeafAtOffset::Between(l, r) => {
|
|
|
|
assert!(r == node);
|
|
|
|
l
|
|
|
|
}
|
|
|
|
LeafAtOffset::Single(n) => {
|
|
|
|
assert!(n == node);
|
2018-10-15 16:44:23 -05:00
|
|
|
return Some("");
|
2018-10-09 08:00:20 -05:00
|
|
|
}
|
|
|
|
LeafAtOffset::None => unreachable!(),
|
|
|
|
};
|
|
|
|
if ws.kind() != WHITESPACE {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let text = ws.leaf_text().unwrap();
|
|
|
|
let pos = text.as_str().rfind('\n').map(|it| it + 1).unwrap_or(0);
|
|
|
|
Some(&text[pos..])
|
|
|
|
}
|
|
|
|
|
2019-01-07 07:53:24 -06:00
|
|
|
pub fn on_eq_typed(file: &SourceFile, offset: TextUnit) -> Option<LocalEdit> {
|
|
|
|
let let_stmt: &ast::LetStmt = find_node_at_offset(file.syntax(), 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-08 12:50:04 -06:00
|
|
|
if expr_range.contains(offset) && offset != expr_range.start() {
|
2018-08-28 03:12:42 -05:00
|
|
|
return None;
|
|
|
|
}
|
2018-10-15 16:44:23 -05:00
|
|
|
if file
|
|
|
|
.syntax()
|
|
|
|
.text()
|
|
|
|
.slice(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());
|
2018-08-29 10:03:14 -05:00
|
|
|
Some(LocalEdit {
|
2018-12-25 10:45:13 -06:00
|
|
|
label: "add semicolon".to_string(),
|
2018-08-28 03:12:42 -05:00
|
|
|
edit: edit.finish(),
|
|
|
|
cursor_position: None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-01-07 07:53:24 -06:00
|
|
|
pub fn on_dot_typed(file: &SourceFile, offset: TextUnit) -> Option<LocalEdit> {
|
2019-01-05 17:58:03 -06:00
|
|
|
let before_dot_offset = offset - TextUnit::of_char('.');
|
|
|
|
|
2019-01-06 23:16:04 -06:00
|
|
|
let whitespace = find_leaf_at_offset(file.syntax(), before_dot_offset).left_biased()?;
|
2019-01-06 14:59:14 -06:00
|
|
|
|
|
|
|
// find whitespace just left of the dot
|
2019-01-06 23:16:04 -06:00
|
|
|
ast::Whitespace::cast(whitespace)?;
|
2019-01-06 14:59:14 -06:00
|
|
|
|
|
|
|
// make sure there is a method call
|
2019-01-06 23:16:04 -06:00
|
|
|
let method_call = whitespace
|
2019-01-06 14:59:14 -06:00
|
|
|
.siblings(Direction::Prev)
|
|
|
|
// first is whitespace
|
|
|
|
.skip(1)
|
|
|
|
.next()?;
|
|
|
|
|
2019-01-07 07:53:24 -06:00
|
|
|
ast::MethodCallExpr::cast(method_call)?;
|
2019-01-06 14:59:14 -06:00
|
|
|
|
|
|
|
// find how much the _method call is indented
|
2019-01-06 23:16:04 -06:00
|
|
|
let method_chain_indent = method_call
|
|
|
|
.parent()?
|
2019-01-06 14:59:14 -06:00
|
|
|
.siblings(Direction::Prev)
|
|
|
|
.skip(1)
|
|
|
|
.next()?
|
|
|
|
.leaf_text()
|
|
|
|
.map(|x| last_line_indent_in_whitespace(x))?;
|
2019-01-05 17:58:03 -06:00
|
|
|
|
2019-01-06 23:16:04 -06:00
|
|
|
let current_indent = TextUnit::of_str(last_line_indent_in_whitespace(whitespace.leaf_text()?));
|
2019-01-05 17:58:03 -06:00
|
|
|
// TODO: indent is always 4 spaces now. A better heuristic could look on the previous line(s)
|
|
|
|
|
2019-01-06 14:59:14 -06:00
|
|
|
let target_indent = TextUnit::of_str(method_chain_indent) + TextUnit::from_usize(4);
|
|
|
|
|
|
|
|
let diff = target_indent - current_indent;
|
|
|
|
|
|
|
|
let indent = "".repeat(diff.to_usize());
|
|
|
|
|
|
|
|
let cursor_position = offset + diff;
|
2019-01-05 17:58:03 -06:00
|
|
|
let mut edit = TextEditBuilder::default();
|
|
|
|
edit.insert(before_dot_offset, indent);
|
|
|
|
Some(LocalEdit {
|
|
|
|
label: "indent dot".to_string(),
|
|
|
|
edit: edit.finish(),
|
|
|
|
cursor_position: Some(cursor_position),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-01-06 14:59:14 -06:00
|
|
|
/// Finds the last line in the whitespace
|
|
|
|
fn last_line_indent_in_whitespace(ws: &str) -> &str {
|
|
|
|
ws.split('\n').last().unwrap_or("")
|
|
|
|
}
|
|
|
|
|
2018-08-28 06:47:12 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-01-10 08:50:49 -06:00
|
|
|
use crate::test_utils::{add_cursor, assert_eq_text, extract_offset};
|
2018-10-11 10:11:00 -05:00
|
|
|
|
2019-01-10 08:50:49 -06:00
|
|
|
use super::*;
|
2018-08-28 06:47:12 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_on_eq_typed() {
|
|
|
|
fn do_check(before: &str, after: &str) {
|
|
|
|
let (offset, before) = extract_offset(before);
|
2019-01-07 07:53:24 -06:00
|
|
|
let file = SourceFile::parse(&before);
|
2018-08-28 06:47:12 -05:00
|
|
|
let result = on_eq_typed(&file, offset).unwrap();
|
|
|
|
let actual = result.edit.apply(&before);
|
|
|
|
assert_eq_text!(after, &actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
// do_check(r"
|
|
|
|
// fn foo() {
|
|
|
|
// let foo =<|>
|
|
|
|
// }
|
|
|
|
// ", r"
|
|
|
|
// fn foo() {
|
|
|
|
// let foo =;
|
|
|
|
// }
|
|
|
|
// ");
|
2018-10-15 16:44:23 -05:00
|
|
|
do_check(
|
|
|
|
r"
|
2018-08-28 06:47:12 -05:00
|
|
|
fn foo() {
|
|
|
|
let foo =<|> 1 + 1
|
|
|
|
}
|
2018-10-15 16:44:23 -05:00
|
|
|
",
|
|
|
|
r"
|
2018-08-28 06:47:12 -05:00
|
|
|
fn foo() {
|
|
|
|
let foo = 1 + 1;
|
|
|
|
}
|
2018-10-15 16:44:23 -05:00
|
|
|
",
|
|
|
|
);
|
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;
|
|
|
|
// }
|
|
|
|
// ");
|
|
|
|
}
|
2018-10-09 08:00:20 -05:00
|
|
|
|
2019-01-05 17:58:03 -06:00
|
|
|
#[test]
|
|
|
|
fn test_on_dot_typed() {
|
|
|
|
fn do_check(before: &str, after: &str) {
|
|
|
|
let (offset, before) = extract_offset(before);
|
2019-01-07 07:53:24 -06:00
|
|
|
let file = SourceFile::parse(&before);
|
2019-01-06 14:59:14 -06:00
|
|
|
if let Some(result) = on_eq_typed(&file, offset) {
|
|
|
|
let actual = result.edit.apply(&before);
|
|
|
|
assert_eq_text!(after, &actual);
|
|
|
|
};
|
2019-01-05 17:58:03 -06:00
|
|
|
}
|
2019-01-06 05:24:33 -06:00
|
|
|
// indent if continuing chain call
|
2019-01-05 17:58:03 -06:00
|
|
|
do_check(
|
|
|
|
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-06 05:24:33 -06:00
|
|
|
",
|
|
|
|
);
|
|
|
|
|
|
|
|
// do not indent if already indented
|
|
|
|
do_check(
|
|
|
|
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)
|
|
|
|
.
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
|
|
|
|
// indent if the previous line is already indented
|
|
|
|
do_check(
|
|
|
|
r"
|
|
|
|
pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
|
|
|
|
self.child_impl(db, name)
|
|
|
|
.first()
|
|
|
|
.<|>
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
|
|
|
|
self.child_impl(db, name)
|
|
|
|
.first()
|
|
|
|
.
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
|
|
|
|
// don't indent if indent matches previous line
|
|
|
|
do_check(
|
|
|
|
r"
|
|
|
|
pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
|
|
|
|
self.child_impl(db, name)
|
|
|
|
.first()
|
|
|
|
.<|>
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
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
|
|
|
",
|
|
|
|
);
|
|
|
|
|
|
|
|
// don't indent if there is no method call on previous line
|
|
|
|
do_check(
|
|
|
|
r"
|
|
|
|
pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
|
|
|
|
.<|>
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
|
|
|
|
.
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
|
|
|
|
// indent to match previous expr
|
|
|
|
do_check(
|
|
|
|
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-05 17:58:03 -06:00
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-10-09 08:00:20 -05:00
|
|
|
#[test]
|
|
|
|
fn test_on_enter() {
|
|
|
|
fn apply_on_enter(before: &str) -> Option<String> {
|
|
|
|
let (offset, before) = extract_offset(before);
|
2019-01-07 07:53:24 -06:00
|
|
|
let file = SourceFile::parse(&before);
|
2018-10-09 08:00:20 -05:00
|
|
|
let result = on_enter(&file, offset)?;
|
|
|
|
let actual = result.edit.apply(&before);
|
|
|
|
let actual = add_cursor(&actual, result.cursor_position.unwrap());
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2018-10-15 16:44:23 -05:00
|
|
|
do_check(
|
|
|
|
r"
|
2018-10-09 08:00:20 -05:00
|
|
|
/// Some docs<|>
|
|
|
|
fn foo() {
|
|
|
|
}
|
2018-10-15 16:44:23 -05:00
|
|
|
",
|
|
|
|
r"
|
2018-10-09 08:00:20 -05:00
|
|
|
/// Some docs
|
|
|
|
/// <|>
|
|
|
|
fn foo() {
|
|
|
|
}
|
2018-10-15 16:44:23 -05:00
|
|
|
",
|
|
|
|
);
|
|
|
|
do_check(
|
|
|
|
r"
|
2018-10-09 08:00:20 -05:00
|
|
|
impl S {
|
|
|
|
/// Some<|> docs.
|
|
|
|
fn foo() {}
|
|
|
|
}
|
2018-10-15 16:44:23 -05:00
|
|
|
",
|
|
|
|
r"
|
2018-10-09 08:00:20 -05:00
|
|
|
impl S {
|
|
|
|
/// Some
|
|
|
|
/// <|> docs.
|
|
|
|
fn foo() {}
|
|
|
|
}
|
2018-10-15 16:44:23 -05:00
|
|
|
",
|
|
|
|
);
|
2018-10-09 08:00:20 -05:00
|
|
|
do_check_noop(r"<|>//! docz");
|
|
|
|
}
|
2018-08-28 06:47:12 -05:00
|
|
|
}
|