Make better use of stdx::always

This commit is contained in:
Jonas Schievink 2021-04-07 16:44:25 +02:00
parent 3f599ae4ed
commit 39d59fb06f

View File

@ -58,9 +58,13 @@ pub(crate) fn on_char_typed(
position: FilePosition, position: FilePosition,
char_typed: char, char_typed: char,
) -> Option<SourceChange> { ) -> Option<SourceChange> {
assert!(TRIGGER_CHARS.contains(char_typed)); if !stdx::always!(TRIGGER_CHARS.contains(char_typed)) {
return None;
}
let file = &db.parse(position.file_id); let file = &db.parse(position.file_id);
assert_eq!(file.tree().syntax().text().char_at(position.offset), Some(char_typed)); if !stdx::always!(file.tree().syntax().text().char_at(position.offset) == Some(char_typed)) {
return None;
}
let edit = on_char_typed_inner(file, position.offset, char_typed)?; let edit = on_char_typed_inner(file, position.offset, char_typed)?;
Some(SourceChange::from_text_edit(position.file_id, edit)) Some(SourceChange::from_text_edit(position.file_id, edit))
} }
@ -70,7 +74,9 @@ fn on_char_typed_inner(
offset: TextSize, offset: TextSize,
char_typed: char, char_typed: char,
) -> Option<TextEdit> { ) -> Option<TextEdit> {
assert!(TRIGGER_CHARS.contains(char_typed)); if !stdx::always!(TRIGGER_CHARS.contains(char_typed)) {
return None;
}
match char_typed { match char_typed {
'.' => on_dot_typed(&file.tree(), offset), '.' => on_dot_typed(&file.tree(), offset),
'=' => on_eq_typed(&file.tree(), offset), '=' => on_eq_typed(&file.tree(), offset),
@ -83,7 +89,9 @@ fn on_char_typed_inner(
/// Inserts a closing `}` when the user types an opening `{`, wrapping an existing expression in a /// Inserts a closing `}` when the user types an opening `{`, wrapping an existing expression in a
/// block. /// block.
fn on_opening_brace_typed(file: &Parse<SourceFile>, offset: TextSize) -> Option<TextEdit> { fn on_opening_brace_typed(file: &Parse<SourceFile>, offset: TextSize) -> Option<TextEdit> {
stdx::always!(file.tree().syntax().text().char_at(offset) == Some('{')); if !stdx::always!(file.tree().syntax().text().char_at(offset) == Some('{')) {
return None;
}
let brace_token = file.tree().syntax().token_at_offset(offset).right_biased()?; let brace_token = file.tree().syntax().token_at_offset(offset).right_biased()?;
@ -120,7 +128,9 @@ fn on_opening_brace_typed(file: &Parse<SourceFile>, offset: TextSize) -> Option<
/// this works when adding `let =`. /// this works when adding `let =`.
// FIXME: use a snippet completion instead of this hack here. // FIXME: use a snippet completion instead of this hack here.
fn on_eq_typed(file: &SourceFile, offset: TextSize) -> Option<TextEdit> { fn on_eq_typed(file: &SourceFile, offset: TextSize) -> Option<TextEdit> {
stdx::always!(file.syntax().text().char_at(offset) == Some('=')); if !stdx::always!(file.syntax().text().char_at(offset) == Some('=')) {
return None;
}
let let_stmt: ast::LetStmt = find_node_at_offset(file.syntax(), offset)?; let let_stmt: ast::LetStmt = find_node_at_offset(file.syntax(), offset)?;
if let_stmt.semicolon_token().is_some() { if let_stmt.semicolon_token().is_some() {
return None; return None;
@ -142,7 +152,9 @@ fn on_eq_typed(file: &SourceFile, offset: TextSize) -> Option<TextEdit> {
/// Returns an edit which should be applied when a dot ('.') is typed on a blank line, indenting the line appropriately. /// Returns an edit which should be applied when a dot ('.') is typed on a blank line, indenting the line appropriately.
fn on_dot_typed(file: &SourceFile, offset: TextSize) -> Option<TextEdit> { fn on_dot_typed(file: &SourceFile, offset: TextSize) -> Option<TextEdit> {
stdx::always!(file.syntax().text().char_at(offset) == Some('.')); if !stdx::always!(file.syntax().text().char_at(offset) == Some('.')) {
return None;
}
let whitespace = let whitespace =
file.syntax().token_at_offset(offset).left_biased().and_then(ast::Whitespace::cast)?; file.syntax().token_at_offset(offset).left_biased().and_then(ast::Whitespace::cast)?;
@ -171,7 +183,9 @@ fn on_dot_typed(file: &SourceFile, offset: TextSize) -> Option<TextEdit> {
/// Adds a space after an arrow when `fn foo() { ... }` is turned into `fn foo() -> { ... }` /// Adds a space after an arrow when `fn foo() { ... }` is turned into `fn foo() -> { ... }`
fn on_arrow_typed(file: &SourceFile, offset: TextSize) -> Option<TextEdit> { fn on_arrow_typed(file: &SourceFile, offset: TextSize) -> Option<TextEdit> {
let file_text = file.syntax().text(); let file_text = file.syntax().text();
stdx::always!(file_text.char_at(offset) == Some('>')); if !stdx::always!(file_text.char_at(offset) == Some('>')) {
return None;
}
let after_arrow = offset + TextSize::of('>'); let after_arrow = offset + TextSize::of('>');
if file_text.char_at(after_arrow) != Some('{') { if file_text.char_at(after_arrow) != Some('{') {
return None; return None;