2018-08-12 18:50:16 +03:00
|
|
|
mod code_actions;
|
2018-10-15 17:44:23 -04:00
|
|
|
mod extend_selection;
|
|
|
|
|
mod folding_ranges;
|
|
|
|
|
mod line_index;
|
2018-12-25 21:26:36 +01:00
|
|
|
mod line_index_utils;
|
2018-10-15 17:44:23 -04:00
|
|
|
mod symbols;
|
2018-08-28 14:47:12 +03:00
|
|
|
#[cfg(test)]
|
|
|
|
|
mod test_utils;
|
2018-10-15 17:44:23 -04:00
|
|
|
mod typing;
|
2018-08-01 10:40:07 +03:00
|
|
|
|
2018-10-15 17:44:23 -04:00
|
|
|
pub use self::{
|
2018-12-16 18:17:33 +09:00
|
|
|
code_actions::{add_derive, add_impl, flip_comma, introduce_variable, make_pub_crate, LocalEdit},
|
2018-10-15 17:44:23 -04:00
|
|
|
extend_selection::extend_selection,
|
|
|
|
|
folding_ranges::{folding_ranges, Fold, FoldKind},
|
|
|
|
|
line_index::{LineCol, LineIndex},
|
2018-12-23 15:49:14 +01:00
|
|
|
line_index_utils::translate_offset_with_edit,
|
2018-10-15 17:44:23 -04:00
|
|
|
symbols::{file_structure, file_symbols, FileSymbol, StructureNode},
|
|
|
|
|
typing::{join_lines, on_enter, on_eq_typed},
|
|
|
|
|
};
|
2018-12-11 19:07:17 +01:00
|
|
|
use ra_text_edit::{TextEdit, TextEditBuilder};
|
2018-09-16 12:54:24 +03:00
|
|
|
use ra_syntax::{
|
2018-10-02 18:02:57 +03:00
|
|
|
algo::find_leaf_at_offset,
|
2018-10-15 17:44:23 -04:00
|
|
|
ast::{self, AstNode, NameOwner},
|
2018-11-07 18:32:33 +03:00
|
|
|
SourceFileNode,
|
2018-11-05 18:38:34 +01:00
|
|
|
Location,
|
2018-08-16 00:23:22 +03:00
|
|
|
SyntaxKind::{self, *},
|
2018-12-28 17:39:12 +03:00
|
|
|
SyntaxNodeRef, TextRange, TextUnit, Direction,
|
2018-08-11 12:28:59 +03:00
|
|
|
};
|
2018-12-27 13:35:08 +03:00
|
|
|
use itertools::Itertools;
|
2018-12-28 17:39:12 +03:00
|
|
|
use rustc_hash::FxHashSet;
|
2018-08-01 10:40:07 +03:00
|
|
|
|
2018-08-09 12:47:34 +03:00
|
|
|
#[derive(Debug)]
|
2018-08-01 10:40:07 +03:00
|
|
|
pub struct HighlightedRange {
|
|
|
|
|
pub range: TextRange,
|
|
|
|
|
pub tag: &'static str,
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-23 23:50:11 +08:00
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
|
pub enum Severity {
|
|
|
|
|
Error,
|
2018-12-24 22:48:46 +08:00
|
|
|
WeakWarning,
|
2018-12-23 23:50:11 +08:00
|
|
|
}
|
|
|
|
|
|
2018-08-09 21:27:44 +03:00
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct Diagnostic {
|
|
|
|
|
pub range: TextRange,
|
|
|
|
|
pub msg: String,
|
2018-12-24 22:48:46 +08:00
|
|
|
pub severity: Severity,
|
2018-12-26 00:45:13 +08:00
|
|
|
pub fix: Option<LocalEdit>,
|
2018-08-09 21:27:44 +03:00
|
|
|
}
|
|
|
|
|
|
2018-08-09 16:03:21 +03:00
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct Runnable {
|
|
|
|
|
pub range: TextRange,
|
|
|
|
|
pub kind: RunnableKind,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub enum RunnableKind {
|
|
|
|
|
Test { name: String },
|
|
|
|
|
Bin,
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-07 18:32:33 +03:00
|
|
|
pub fn matching_brace(file: &SourceFileNode, offset: TextUnit) -> Option<TextUnit> {
|
2018-08-16 00:23:22 +03:00
|
|
|
const BRACES: &[SyntaxKind] = &[
|
2018-10-15 17:44:23 -04:00
|
|
|
L_CURLY, R_CURLY, L_BRACK, R_BRACK, L_PAREN, R_PAREN, L_ANGLE, R_ANGLE,
|
2018-08-16 00:23:22 +03:00
|
|
|
];
|
2018-08-17 22:00:13 +03:00
|
|
|
let (brace_node, brace_idx) = find_leaf_at_offset(file.syntax(), offset)
|
2018-08-16 00:23:22 +03:00
|
|
|
.filter_map(|node| {
|
|
|
|
|
let idx = BRACES.iter().position(|&brace| brace == node.kind())?;
|
|
|
|
|
Some((node, idx))
|
|
|
|
|
})
|
|
|
|
|
.next()?;
|
|
|
|
|
let parent = brace_node.parent()?;
|
|
|
|
|
let matching_kind = BRACES[brace_idx ^ 1];
|
2018-10-15 17:44:23 -04:00
|
|
|
let matching_node = parent
|
|
|
|
|
.children()
|
2018-08-16 00:23:22 +03:00
|
|
|
.find(|node| node.kind() == matching_kind)?;
|
|
|
|
|
Some(matching_node.range().start())
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-07 18:32:33 +03:00
|
|
|
pub fn highlight(file: &SourceFileNode) -> Vec<HighlightedRange> {
|
2018-12-28 17:39:12 +03:00
|
|
|
// Visited nodes to handle highlighting priorities
|
|
|
|
|
let mut highlighted = FxHashSet::default();
|
2018-08-10 15:07:43 +03:00
|
|
|
let mut res = Vec::new();
|
2018-10-02 18:02:57 +03:00
|
|
|
for node in file.syntax().descendants() {
|
2018-12-28 17:39:12 +03:00
|
|
|
if highlighted.contains(&node) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2018-08-10 15:07:43 +03:00
|
|
|
let tag = match node.kind() {
|
2018-10-31 17:38:18 -04:00
|
|
|
COMMENT => "comment",
|
2018-08-10 15:07:43 +03:00
|
|
|
STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => "string",
|
|
|
|
|
ATTR => "attribute",
|
|
|
|
|
NAME_REF => "text",
|
|
|
|
|
NAME => "function",
|
|
|
|
|
INT_NUMBER | FLOAT_NUMBER | CHAR | BYTE => "literal",
|
|
|
|
|
LIFETIME => "parameter",
|
|
|
|
|
k if k.is_keyword() => "keyword",
|
2018-12-28 17:39:12 +03:00
|
|
|
_ => {
|
|
|
|
|
if let Some(macro_call) = ast::MacroCall::cast(node) {
|
|
|
|
|
if let Some(path) = macro_call.path() {
|
|
|
|
|
if let Some(segment) = path.segment() {
|
|
|
|
|
if let Some(name_ref) = segment.name_ref() {
|
|
|
|
|
highlighted.insert(name_ref.syntax());
|
|
|
|
|
let range_start = name_ref.syntax().range().start();
|
|
|
|
|
let mut range_end = name_ref.syntax().range().end();
|
|
|
|
|
for sibling in path.syntax().siblings(Direction::Next) {
|
|
|
|
|
match sibling.kind() {
|
|
|
|
|
EXCL | IDENT => range_end = sibling.range().end(),
|
|
|
|
|
_ => (),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
res.push(HighlightedRange {
|
|
|
|
|
range: TextRange::from_to(range_start, range_end),
|
|
|
|
|
tag: "macro",
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2018-08-10 15:07:43 +03:00
|
|
|
};
|
|
|
|
|
res.push(HighlightedRange {
|
|
|
|
|
range: node.range(),
|
|
|
|
|
tag,
|
|
|
|
|
})
|
2018-08-01 10:40:07 +03:00
|
|
|
}
|
2018-08-10 15:07:43 +03:00
|
|
|
res
|
|
|
|
|
}
|
2018-08-01 10:40:07 +03:00
|
|
|
|
2018-11-07 18:32:33 +03:00
|
|
|
pub fn diagnostics(file: &SourceFileNode) -> Vec<Diagnostic> {
|
2018-11-05 18:38:34 +01:00
|
|
|
fn location_to_range(location: Location) -> TextRange {
|
|
|
|
|
match location {
|
|
|
|
|
Location::Offset(offset) => TextRange::offset_len(offset, 1.into()),
|
|
|
|
|
Location::Range(range) => range,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-24 00:39:33 +08:00
|
|
|
let mut errors: Vec<Diagnostic> = file
|
|
|
|
|
.errors()
|
2018-10-15 17:44:23 -04:00
|
|
|
.into_iter()
|
|
|
|
|
.map(|err| Diagnostic {
|
2018-11-05 18:38:34 +01:00
|
|
|
range: location_to_range(err.location()),
|
|
|
|
|
msg: format!("Syntax Error: {}", err),
|
2018-12-24 22:48:46 +08:00
|
|
|
severity: Severity::Error,
|
2018-12-26 00:45:13 +08:00
|
|
|
fix: None,
|
2018-10-15 17:44:23 -04:00
|
|
|
})
|
2018-12-23 23:50:11 +08:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
let warnings = check_unnecessary_braces_in_use_statement(file);
|
|
|
|
|
|
|
|
|
|
errors.extend(warnings);
|
|
|
|
|
errors
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn check_unnecessary_braces_in_use_statement(file: &SourceFileNode) -> Vec<Diagnostic> {
|
|
|
|
|
let mut diagnostics = Vec::new();
|
|
|
|
|
for node in file.syntax().descendants() {
|
|
|
|
|
if let Some(use_tree_list) = ast::UseTreeList::cast(node) {
|
2018-12-27 13:35:08 +03:00
|
|
|
if let Some((single_use_tree,)) = use_tree_list.use_trees().collect_tuple() {
|
2018-12-26 00:45:13 +08:00
|
|
|
let range = use_tree_list.syntax().range();
|
2018-12-27 00:51:27 +08:00
|
|
|
let edit = text_edit_for_remove_unnecessary_braces_with_self_in_use_statement(
|
|
|
|
|
single_use_tree,
|
|
|
|
|
)
|
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
|
let to_replace = single_use_tree.syntax().text().to_string();
|
|
|
|
|
let mut edit_builder = TextEditBuilder::new();
|
|
|
|
|
edit_builder.delete(range);
|
|
|
|
|
edit_builder.insert(range.start(), to_replace);
|
|
|
|
|
edit_builder.finish()
|
|
|
|
|
});
|
2018-12-26 00:45:13 +08:00
|
|
|
|
2018-12-23 23:50:11 +08:00
|
|
|
diagnostics.push(Diagnostic {
|
2018-12-26 00:45:13 +08:00
|
|
|
range: range,
|
2018-12-23 23:50:11 +08:00
|
|
|
msg: format!("Unnecessary braces in use statement"),
|
2018-12-24 22:48:46 +08:00
|
|
|
severity: Severity::WeakWarning,
|
2018-12-26 00:45:13 +08:00
|
|
|
fix: Some(LocalEdit {
|
|
|
|
|
label: "Remove unnecessary braces".to_string(),
|
2018-12-27 00:51:27 +08:00
|
|
|
edit: edit,
|
2018-12-26 00:45:13 +08:00
|
|
|
cursor_position: None,
|
|
|
|
|
}),
|
2018-12-23 23:50:11 +08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
diagnostics
|
2018-08-10 15:07:43 +03:00
|
|
|
}
|
2018-08-09 21:27:44 +03:00
|
|
|
|
2018-12-27 00:51:27 +08:00
|
|
|
fn text_edit_for_remove_unnecessary_braces_with_self_in_use_statement(
|
|
|
|
|
single_use_tree: ast::UseTree,
|
|
|
|
|
) -> Option<TextEdit> {
|
|
|
|
|
let use_tree_list_node = single_use_tree.syntax().parent()?;
|
|
|
|
|
if single_use_tree
|
|
|
|
|
.path()?
|
|
|
|
|
.segment()?
|
|
|
|
|
.syntax()
|
|
|
|
|
.first_child()?
|
|
|
|
|
.kind()
|
|
|
|
|
== SyntaxKind::SELF_KW
|
|
|
|
|
{
|
|
|
|
|
let start = use_tree_list_node.prev_sibling()?.range().start();
|
|
|
|
|
let end = use_tree_list_node.range().end();
|
|
|
|
|
let range = TextRange::from_to(start, end);
|
|
|
|
|
let mut edit_builder = TextEditBuilder::new();
|
|
|
|
|
edit_builder.delete(range);
|
|
|
|
|
return Some(edit_builder.finish());
|
|
|
|
|
}
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-07 18:32:33 +03:00
|
|
|
pub fn syntax_tree(file: &SourceFileNode) -> String {
|
2018-09-16 12:54:24 +03:00
|
|
|
::ra_syntax::utils::dump_tree(file.syntax())
|
2018-08-10 15:07:43 +03:00
|
|
|
}
|
2018-08-05 19:06:14 +03:00
|
|
|
|
2018-11-07 18:32:33 +03:00
|
|
|
pub fn runnables(file: &SourceFileNode) -> Vec<Runnable> {
|
2018-10-15 17:44:23 -04:00
|
|
|
file.syntax()
|
|
|
|
|
.descendants()
|
2018-08-28 19:23:55 +03:00
|
|
|
.filter_map(ast::FnDef::cast)
|
2018-08-10 15:07:43 +03:00
|
|
|
.filter_map(|f| {
|
|
|
|
|
let name = f.name()?.text();
|
|
|
|
|
let kind = if name == "main" {
|
|
|
|
|
RunnableKind::Bin
|
|
|
|
|
} else if f.has_atom_attr("test") {
|
|
|
|
|
RunnableKind::Test {
|
2018-10-15 17:44:23 -04:00
|
|
|
name: name.to_string(),
|
2018-08-10 15:07:43 +03:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return None;
|
|
|
|
|
};
|
|
|
|
|
Some(Runnable {
|
|
|
|
|
range: f.syntax().range(),
|
|
|
|
|
kind,
|
2018-08-09 16:03:21 +03:00
|
|
|
})
|
2018-08-10 15:07:43 +03:00
|
|
|
})
|
|
|
|
|
.collect()
|
2018-08-05 19:06:14 +03:00
|
|
|
}
|
2018-08-26 09:12:18 +03:00
|
|
|
|
|
|
|
|
pub fn find_node_at_offset<'a, N: AstNode<'a>>(
|
|
|
|
|
syntax: SyntaxNodeRef<'a>,
|
|
|
|
|
offset: TextUnit,
|
|
|
|
|
) -> Option<N> {
|
2018-11-21 18:34:20 +03:00
|
|
|
find_leaf_at_offset(syntax, offset).find_map(|leaf| leaf.ancestors().find_map(N::cast))
|
2018-08-26 09:12:18 +03:00
|
|
|
}
|
2018-08-28 14:47:12 +03:00
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
2018-12-27 00:51:27 +08:00
|
|
|
use crate::test_utils::{add_cursor, assert_eq_dbg, assert_eq_text, extract_offset};
|
|
|
|
|
|
2018-08-28 14:47:12 +03:00
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_highlighting() {
|
2018-11-07 18:32:33 +03:00
|
|
|
let file = SourceFileNode::parse(
|
2018-10-15 17:44:23 -04:00
|
|
|
r#"
|
2018-08-28 14:47:12 +03:00
|
|
|
// comment
|
|
|
|
|
fn main() {}
|
|
|
|
|
println!("Hello, {}!", 92);
|
2018-10-15 17:44:23 -04:00
|
|
|
"#,
|
|
|
|
|
);
|
2018-08-28 14:47:12 +03:00
|
|
|
let hls = highlight(&file);
|
|
|
|
|
assert_eq_dbg(
|
|
|
|
|
r#"[HighlightedRange { range: [1; 11), tag: "comment" },
|
|
|
|
|
HighlightedRange { range: [12; 14), tag: "keyword" },
|
|
|
|
|
HighlightedRange { range: [15; 19), tag: "function" },
|
2018-12-28 17:39:12 +03:00
|
|
|
HighlightedRange { range: [29; 37), tag: "macro" },
|
2018-08-28 14:47:12 +03:00
|
|
|
HighlightedRange { range: [38; 50), tag: "string" },
|
|
|
|
|
HighlightedRange { range: [52; 54), tag: "literal" }]"#,
|
|
|
|
|
&hls,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_runnables() {
|
2018-11-07 18:32:33 +03:00
|
|
|
let file = SourceFileNode::parse(
|
2018-10-15 17:44:23 -04:00
|
|
|
r#"
|
2018-08-28 14:47:12 +03:00
|
|
|
fn main() {}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_foo() {}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
#[ignore]
|
|
|
|
|
fn test_foo() {}
|
2018-10-15 17:44:23 -04:00
|
|
|
"#,
|
|
|
|
|
);
|
2018-08-28 14:47:12 +03:00
|
|
|
let runnables = runnables(&file);
|
|
|
|
|
assert_eq_dbg(
|
|
|
|
|
r#"[Runnable { range: [1; 13), kind: Bin },
|
|
|
|
|
Runnable { range: [15; 39), kind: Test { name: "test_foo" } },
|
|
|
|
|
Runnable { range: [41; 75), kind: Test { name: "test_foo" } }]"#,
|
|
|
|
|
&runnables,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_matching_brace() {
|
|
|
|
|
fn do_check(before: &str, after: &str) {
|
|
|
|
|
let (pos, before) = extract_offset(before);
|
2018-11-07 18:32:33 +03:00
|
|
|
let file = SourceFileNode::parse(&before);
|
2018-08-28 14:47:12 +03:00
|
|
|
let new_pos = match matching_brace(&file, pos) {
|
|
|
|
|
None => pos,
|
|
|
|
|
Some(pos) => pos,
|
|
|
|
|
};
|
|
|
|
|
let actual = add_cursor(&before, new_pos);
|
|
|
|
|
assert_eq_text!(after, &actual);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-15 17:44:23 -04:00
|
|
|
do_check("struct Foo { a: i32, }<|>", "struct Foo <|>{ a: i32, }");
|
2018-08-28 14:47:12 +03:00
|
|
|
}
|
2018-12-23 23:50:11 +08:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_check_unnecessary_braces_in_use_statement() {
|
2018-12-27 13:35:08 +03:00
|
|
|
fn check_not_applicable(code: &str) {
|
|
|
|
|
let file = SourceFileNode::parse(code);
|
|
|
|
|
let diagnostics = check_unnecessary_braces_in_use_statement(&file);
|
|
|
|
|
assert!(diagnostics.is_empty());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn check_apply(before: &str, after: &str) {
|
|
|
|
|
let file = SourceFileNode::parse(before);
|
|
|
|
|
let diagnostic = check_unnecessary_braces_in_use_statement(&file)
|
|
|
|
|
.pop()
|
|
|
|
|
.unwrap_or_else(|| panic!("no diagnostics for:\n{}\n", before));
|
|
|
|
|
let fix = diagnostic.fix.unwrap();
|
|
|
|
|
let actual = fix.edit.apply(&before);
|
|
|
|
|
assert_eq_text!(after, &actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
check_not_applicable(
|
|
|
|
|
"
|
|
|
|
|
use a;
|
|
|
|
|
use a::{c, d::e};
|
|
|
|
|
",
|
2018-12-23 23:50:11 +08:00
|
|
|
);
|
2018-12-27 13:35:08 +03:00
|
|
|
check_apply("use {b};", "use b;");
|
|
|
|
|
check_apply("use a::{c};", "use a::c;");
|
|
|
|
|
check_apply("use a::{self};", "use a;");
|
|
|
|
|
check_apply("use a::{c, d::{e}};", "use a::{c, d::e};");
|
2018-12-23 23:50:11 +08:00
|
|
|
}
|
2018-08-28 14:47:12 +03:00
|
|
|
}
|