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-18 18:46:54 +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-18 18:46:54 +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-10-15 17:44:23 -04:00
|
|
|
SyntaxNodeRef, TextRange, TextUnit,
|
2018-08-11 12:28:59 +03:00
|
|
|
};
|
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-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-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-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",
|
|
|
|
|
_ => continue,
|
|
|
|
|
};
|
|
|
|
|
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-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) {
|
|
|
|
|
if use_tree_list.use_trees().count() <= 1 {
|
|
|
|
|
diagnostics.push(Diagnostic {
|
|
|
|
|
range: use_tree_list.syntax().range(),
|
|
|
|
|
msg: format!("Unnecessary braces in use statement"),
|
2018-12-24 22:48:46 +08:00
|
|
|
severity: Severity::WeakWarning,
|
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-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 {
|
|
|
|
|
use super::*;
|
2018-12-06 21:16:37 +03:00
|
|
|
use crate::test_utils::{add_cursor, assert_eq_dbg, extract_offset, assert_eq_text};
|
2018-08-28 14:47:12 +03:00
|
|
|
|
|
|
|
|
#[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" },
|
|
|
|
|
HighlightedRange { range: [29; 36), tag: "text" },
|
|
|
|
|
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() {
|
|
|
|
|
let file = SourceFileNode::parse(
|
|
|
|
|
r#"
|
|
|
|
|
use a;
|
|
|
|
|
use {b};
|
|
|
|
|
use a::{c};
|
|
|
|
|
use a::{c, d::e};
|
|
|
|
|
use a::{c, d::{e}};
|
|
|
|
|
fn main() {}
|
|
|
|
|
"#,
|
|
|
|
|
);
|
|
|
|
|
let diagnostics = check_unnecessary_braces_in_use_statement(&file);
|
|
|
|
|
assert_eq_dbg(
|
2018-12-24 22:48:46 +08:00
|
|
|
r#"[Diagnostic { range: [12; 15), msg: "Unnecessary braces in use statement", severity: WeakWarning },
|
|
|
|
|
Diagnostic { range: [24; 27), msg: "Unnecessary braces in use statement", severity: WeakWarning },
|
|
|
|
|
Diagnostic { range: [61; 64), msg: "Unnecessary braces in use statement", severity: WeakWarning }]"#,
|
2018-12-23 23:50:11 +08:00
|
|
|
&diagnostics,
|
|
|
|
|
)
|
|
|
|
|
}
|
2018-08-28 14:47:12 +03:00
|
|
|
}
|