From ccd1b0800a5de5e046e6e9a4b6f49030c1ce3639 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 28 Nov 2019 12:50:26 +0300 Subject: Rename Source -> InFile --- crates/ra_ide/src/call_info.rs | 2 +- crates/ra_ide/src/completion/completion_context.rs | 4 ++-- crates/ra_ide/src/diagnostics.rs | 2 +- crates/ra_ide/src/display/navigation_target.rs | 4 ++-- crates/ra_ide/src/expand.rs | 8 ++++---- crates/ra_ide/src/expand_macro.rs | 8 ++++---- crates/ra_ide/src/goto_definition.rs | 8 ++++---- crates/ra_ide/src/hover.rs | 2 +- crates/ra_ide/src/impls.rs | 10 +++++----- crates/ra_ide/src/inlay_hints.rs | 2 +- crates/ra_ide/src/parent_module.rs | 4 ++-- crates/ra_ide/src/references.rs | 8 ++++---- crates/ra_ide/src/references/classify.rs | 6 +++--- crates/ra_ide/src/references/rename.rs | 2 +- crates/ra_ide/src/runnables.rs | 6 +++--- crates/ra_ide/src/syntax_highlighting.rs | 6 +++--- 16 files changed, 41 insertions(+), 41 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/call_info.rs b/crates/ra_ide/src/call_info.rs index d559dc4d0..b3c323d38 100644 --- a/crates/ra_ide/src/call_info.rs +++ b/crates/ra_ide/src/call_info.rs @@ -18,7 +18,7 @@ pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option CompletionContext<'a> { let src = hir::ModuleSource::from_position(db, position); let module = hir::Module::from_definition( db, - hir::Source { file_id: position.file_id.into(), value: src }, + hir::InFile { file_id: position.file_id.into(), value: src }, ); let token = original_parse.tree().syntax().token_at_offset(position.offset).left_biased()?; let analyzer = hir::SourceAnalyzer::new( db, - hir::Source::new(position.file_id.into(), &token.parent()), + hir::InFile::new(position.file_id.into(), &token.parent()), Some(position.offset), ); let mut ctx = CompletionContext { diff --git a/crates/ra_ide/src/diagnostics.rs b/crates/ra_ide/src/diagnostics.rs index cc1ccab4b..c50a70d99 100644 --- a/crates/ra_ide/src/diagnostics.rs +++ b/crates/ra_ide/src/diagnostics.rs @@ -96,7 +96,7 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec }); let source_file = db.parse(file_id).tree(); let src = - hir::Source { file_id: file_id.into(), value: hir::ModuleSource::SourceFile(source_file) }; + hir::InFile { file_id: file_id.into(), value: hir::ModuleSource::SourceFile(source_file) }; if let Some(m) = hir::Module::from_definition(db, src) { m.diagnostics(db, &mut sink); }; diff --git a/crates/ra_ide/src/display/navigation_target.rs b/crates/ra_ide/src/display/navigation_target.rs index 6ac60722b..61dca14ac 100644 --- a/crates/ra_ide/src/display/navigation_target.rs +++ b/crates/ra_ide/src/display/navigation_target.rs @@ -1,6 +1,6 @@ //! FIXME: write short doc here -use hir::{AssocItem, Either, FieldSource, HasSource, ModuleSource, Source}; +use hir::{AssocItem, Either, FieldSource, HasSource, InFile, ModuleSource}; use ra_db::{FileId, SourceDatabase}; use ra_syntax::{ ast::{self, DocCommentsOwner, NameOwner}, @@ -141,7 +141,7 @@ impl NavigationTarget { /// Allows `NavigationTarget` to be created from a `NameOwner` pub(crate) fn from_named( db: &RootDatabase, - node: Source<&dyn ast::NameOwner>, + node: InFile<&dyn ast::NameOwner>, docs: Option, description: Option, ) -> NavigationTarget { diff --git a/crates/ra_ide/src/expand.rs b/crates/ra_ide/src/expand.rs index 2f1abf509..216d5cfec 100644 --- a/crates/ra_ide/src/expand.rs +++ b/crates/ra_ide/src/expand.rs @@ -1,13 +1,13 @@ //! Utilities to work with files, produced by macros. use std::iter::successors; -use hir::Source; +use hir::InFile; use ra_db::FileId; use ra_syntax::{ast, AstNode, SyntaxNode, SyntaxToken}; use crate::{db::RootDatabase, FileRange}; -pub(crate) fn original_range(db: &RootDatabase, node: Source<&SyntaxNode>) -> FileRange { +pub(crate) fn original_range(db: &RootDatabase, node: InFile<&SyntaxNode>) -> FileRange { let expansion = match node.file_id.expansion_info(db) { None => { return FileRange { @@ -44,8 +44,8 @@ pub(crate) fn descend_into_macros( db: &RootDatabase, file_id: FileId, token: SyntaxToken, -) -> Source { - let src = Source::new(file_id.into(), token); +) -> InFile { + let src = InFile::new(file_id.into(), token); successors(Some(src), |token| { let macro_call = token.value.ancestors().find_map(ast::MacroCall::cast)?; diff --git a/crates/ra_ide/src/expand_macro.rs b/crates/ra_ide/src/expand_macro.rs index abc602244..862c03304 100644 --- a/crates/ra_ide/src/expand_macro.rs +++ b/crates/ra_ide/src/expand_macro.rs @@ -22,7 +22,7 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option< let name_ref = find_node_at_offset::(file.syntax(), position.offset)?; let mac = name_ref.syntax().ancestors().find_map(ast::MacroCall::cast)?; - let source = hir::Source::new(position.file_id.into(), mac.syntax()); + let source = hir::InFile::new(position.file_id.into(), mac.syntax()); let expanded = expand_macro_recur(db, source, source.with_value(&mac))?; // FIXME: @@ -34,8 +34,8 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option< fn expand_macro_recur( db: &RootDatabase, - source: hir::Source<&SyntaxNode>, - macro_call: hir::Source<&ast::MacroCall>, + source: hir::InFile<&SyntaxNode>, + macro_call: hir::InFile<&ast::MacroCall>, ) -> Option { let analyzer = hir::SourceAnalyzer::new(db, source, None); let expansion = analyzer.expand(db, macro_call)?; @@ -46,7 +46,7 @@ fn expand_macro_recur( let mut replaces = FxHashMap::default(); for child in children.into_iter() { - let node = hir::Source::new(macro_file_id, &child); + let node = hir::InFile::new(macro_file_id, &child); if let Some(new_node) = expand_macro_recur(db, source, node) { // Replace the whole node if it is root // `replace_descendants` will not replace the parent node diff --git a/crates/ra_ide/src/goto_definition.rs b/crates/ra_ide/src/goto_definition.rs index c10a6c844..76a741207 100644 --- a/crates/ra_ide/src/goto_definition.rs +++ b/crates/ra_ide/src/goto_definition.rs @@ -1,6 +1,6 @@ //! FIXME: write short doc here -use hir::{db::AstDatabase, Source}; +use hir::{db::AstDatabase, InFile}; use ra_syntax::{ ast::{self, DocCommentsOwner}, match_ast, AstNode, SyntaxNode, @@ -58,7 +58,7 @@ impl ReferenceResult { pub(crate) fn reference_definition( db: &RootDatabase, - name_ref: Source<&ast::NameRef>, + name_ref: InFile<&ast::NameRef>, ) -> ReferenceResult { use self::ReferenceResult::*; @@ -94,7 +94,7 @@ pub(crate) fn reference_definition( pub(crate) fn name_definition( db: &RootDatabase, - name: Source<&ast::Name>, + name: InFile<&ast::Name>, ) -> Option> { let parent = name.value.syntax().parent()?; @@ -115,7 +115,7 @@ pub(crate) fn name_definition( None } -fn named_target(db: &RootDatabase, node: Source<&SyntaxNode>) -> Option { +fn named_target(db: &RootDatabase, node: InFile<&SyntaxNode>) -> Option { match_ast! { match (node.value) { ast::StructDef(it) => { diff --git a/crates/ra_ide/src/hover.rs b/crates/ra_ide/src/hover.rs index 260a7b869..d8185c688 100644 --- a/crates/ra_ide/src/hover.rs +++ b/crates/ra_ide/src/hover.rs @@ -227,7 +227,7 @@ pub(crate) fn type_of(db: &RootDatabase, frange: FileRange) -> Option { .take_while(|it| it.text_range() == leaf_node.text_range()) .find(|it| ast::Expr::cast(it.clone()).is_some() || ast::Pat::cast(it.clone()).is_some())?; let analyzer = - hir::SourceAnalyzer::new(db, hir::Source::new(frange.file_id.into(), &node), None); + hir::SourceAnalyzer::new(db, hir::InFile::new(frange.file_id.into(), &node), None); let ty = if let Some(ty) = ast::Expr::cast(node.clone()).and_then(|e| analyzer.type_of(db, &e)) { ty diff --git a/crates/ra_ide/src/impls.rs b/crates/ra_ide/src/impls.rs index aa480e399..9b165ee2a 100644 --- a/crates/ra_ide/src/impls.rs +++ b/crates/ra_ide/src/impls.rs @@ -16,7 +16,7 @@ pub(crate) fn goto_implementation( let src = hir::ModuleSource::from_position(db, position); let module = hir::Module::from_definition( db, - hir::Source { file_id: position.file_id.into(), value: src }, + hir::InFile { file_id: position.file_id.into(), value: src }, )?; if let Some(nominal_def) = find_node_at_offset::(&syntax, position.offset) { @@ -42,15 +42,15 @@ fn impls_for_def( ) -> Option> { let ty = match node { ast::NominalDef::StructDef(def) => { - let src = hir::Source { file_id: position.file_id.into(), value: def.clone() }; + let src = hir::InFile { file_id: position.file_id.into(), value: def.clone() }; hir::Struct::from_source(db, src)?.ty(db) } ast::NominalDef::EnumDef(def) => { - let src = hir::Source { file_id: position.file_id.into(), value: def.clone() }; + let src = hir::InFile { file_id: position.file_id.into(), value: def.clone() }; hir::Enum::from_source(db, src)?.ty(db) } ast::NominalDef::UnionDef(def) => { - let src = hir::Source { file_id: position.file_id.into(), value: def.clone() }; + let src = hir::InFile { file_id: position.file_id.into(), value: def.clone() }; hir::Union::from_source(db, src)?.ty(db) } }; @@ -73,7 +73,7 @@ fn impls_for_trait( node: &ast::TraitDef, module: hir::Module, ) -> Option> { - let src = hir::Source { file_id: position.file_id.into(), value: node.clone() }; + let src = hir::InFile { file_id: position.file_id.into(), value: node.clone() }; let tr = hir::Trait::from_source(db, src)?; let krate = module.krate(); diff --git a/crates/ra_ide/src/inlay_hints.rs b/crates/ra_ide/src/inlay_hints.rs index 45149bf0c..59eced9d7 100644 --- a/crates/ra_ide/src/inlay_hints.rs +++ b/crates/ra_ide/src/inlay_hints.rs @@ -38,7 +38,7 @@ fn get_inlay_hints( node: &SyntaxNode, max_inlay_hint_length: Option, ) -> Option> { - let analyzer = SourceAnalyzer::new(db, hir::Source::new(file_id.into(), node), None); + let analyzer = SourceAnalyzer::new(db, hir::InFile::new(file_id.into(), node), None); match_ast! { match node { ast::LetStmt(it) => { diff --git a/crates/ra_ide/src/parent_module.rs b/crates/ra_ide/src/parent_module.rs index 6027e7d54..616d69fce 100644 --- a/crates/ra_ide/src/parent_module.rs +++ b/crates/ra_ide/src/parent_module.rs @@ -10,7 +10,7 @@ pub(crate) fn parent_module(db: &RootDatabase, position: FilePosition) -> Vec return Vec::new(), Some(it) => it, @@ -23,7 +23,7 @@ pub(crate) fn parent_module(db: &RootDatabase, position: FilePosition) -> Vec Vec { let src = hir::ModuleSource::from_file_id(db, file_id); let module = - match hir::Module::from_definition(db, hir::Source { file_id: file_id.into(), value: src }) + match hir::Module::from_definition(db, hir::InFile { file_id: file_id.into(), value: src }) { Some(it) => it, None => return Vec::new(), diff --git a/crates/ra_ide/src/references.rs b/crates/ra_ide/src/references.rs index 21a1ea69e..3e7bfd872 100644 --- a/crates/ra_ide/src/references.rs +++ b/crates/ra_ide/src/references.rs @@ -14,7 +14,7 @@ mod name_definition; mod rename; mod search_scope; -use hir::Source; +use hir::InFile; use once_cell::unsync::Lazy; use ra_db::{SourceDatabase, SourceDatabaseExt}; use ra_prof::profile; @@ -107,12 +107,12 @@ fn find_name<'a>( position: FilePosition, ) -> Option> { if let Some(name) = find_node_at_offset::(&syntax, position.offset) { - let def = classify_name(db, Source::new(position.file_id.into(), &name))?; + let def = classify_name(db, InFile::new(position.file_id.into(), &name))?; let range = name.syntax().text_range(); return Some(RangeInfo::new(range, (name.text().to_string(), def))); } let name_ref = find_node_at_offset::(&syntax, position.offset)?; - let def = classify_name_ref(db, Source::new(position.file_id.into(), &name_ref))?; + let def = classify_name_ref(db, InFile::new(position.file_id.into(), &name_ref))?; let range = name_ref.syntax().text_range(); Some(RangeInfo::new(range, (name_ref.text().to_string(), def))) } @@ -144,7 +144,7 @@ fn process_definition( continue; } } - if let Some(d) = classify_name_ref(db, Source::new(file_id.into(), &name_ref)) { + if let Some(d) = classify_name_ref(db, InFile::new(file_id.into(), &name_ref)) { if d == def { refs.push(FileRange { file_id, range }); } diff --git a/crates/ra_ide/src/references/classify.rs b/crates/ra_ide/src/references/classify.rs index 5cea805ec..b716d32e5 100644 --- a/crates/ra_ide/src/references/classify.rs +++ b/crates/ra_ide/src/references/classify.rs @@ -1,6 +1,6 @@ //! Functions that are used to classify an element from its definition or reference. -use hir::{FromSource, Module, ModuleSource, PathResolution, Source, SourceAnalyzer}; +use hir::{FromSource, InFile, Module, ModuleSource, PathResolution, SourceAnalyzer}; use ra_prof::profile; use ra_syntax::{ast, match_ast, AstNode}; use test_utils::tested_by; @@ -11,7 +11,7 @@ use super::{ }; use crate::db::RootDatabase; -pub(crate) fn classify_name(db: &RootDatabase, name: Source<&ast::Name>) -> Option { +pub(crate) fn classify_name(db: &RootDatabase, name: InFile<&ast::Name>) -> Option { let _p = profile("classify_name"); let parent = name.value.syntax().parent()?; @@ -117,7 +117,7 @@ pub(crate) fn classify_name(db: &RootDatabase, name: Source<&ast::Name>) -> Opti pub(crate) fn classify_name_ref( db: &RootDatabase, - name_ref: Source<&ast::NameRef>, + name_ref: InFile<&ast::NameRef>, ) -> Option { let _p = profile("classify_name_ref"); diff --git a/crates/ra_ide/src/references/rename.rs b/crates/ra_ide/src/references/rename.rs index d58496049..ea6b354c2 100644 --- a/crates/ra_ide/src/references/rename.rs +++ b/crates/ra_ide/src/references/rename.rs @@ -55,7 +55,7 @@ fn rename_mod( ) -> Option { let mut source_file_edits = Vec::new(); let mut file_system_edits = Vec::new(); - let module_src = hir::Source { file_id: position.file_id.into(), value: ast_module.clone() }; + let module_src = hir::InFile { file_id: position.file_id.into(), value: ast_module.clone() }; if let Some(module) = hir::Module::from_declaration(db, module_src) { let src = module.definition_source(db); let file_id = src.file_id.original_file(db); diff --git a/crates/ra_ide/src/runnables.rs b/crates/ra_ide/src/runnables.rs index 8039a5164..e213e1a06 100644 --- a/crates/ra_ide/src/runnables.rs +++ b/crates/ra_ide/src/runnables.rs @@ -1,6 +1,6 @@ //! FIXME: write short doc here -use hir::Source; +use hir::InFile; use itertools::Itertools; use ra_db::SourceDatabase; use ra_syntax::{ @@ -66,8 +66,8 @@ fn runnable_mod(db: &RootDatabase, file_id: FileId, module: ast::Module) -> Opti return None; } let range = module.syntax().text_range(); - let src = hir::ModuleSource::from_child_node(db, Source::new(file_id.into(), &module.syntax())); - let module = hir::Module::from_definition(db, Source::new(file_id.into(), src))?; + let src = hir::ModuleSource::from_child_node(db, InFile::new(file_id.into(), &module.syntax())); + let module = hir::Module::from_definition(db, InFile::new(file_id.into(), src))?; let path = module.path_to_root(db).into_iter().rev().filter_map(|it| it.name(db)).join("::"); Some(Runnable { range, kind: RunnableKind::TestMod { path } }) diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index 9a3e4c82f..e6a79541f 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -2,7 +2,7 @@ use rustc_hash::{FxHashMap, FxHashSet}; -use hir::{Name, Source}; +use hir::{InFile, Name}; use ra_db::SourceDatabase; use ra_prof::profile; use ra_syntax::{ast, AstNode, Direction, SyntaxElement, SyntaxKind, SyntaxKind::*, TextRange, T}; @@ -81,7 +81,7 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec Vec { let name = node.as_node().cloned().and_then(ast::Name::cast).unwrap(); let name_kind = - classify_name(db, Source::new(file_id.into(), &name)).map(|d| d.kind); + classify_name(db, InFile::new(file_id.into(), &name)).map(|d| d.kind); if let Some(Local(local)) = &name_kind { if let Some(name) = local.name(db) { -- cgit v1.2.3 From be9ba2b392aaada4b0ce72f61fd664fc3d4021b5 Mon Sep 17 00:00:00 2001 From: Wilco Kusee Date: Fri, 29 Nov 2019 15:52:12 +0100 Subject: Move identifier check to analysis --- crates/ra_ide/src/lib.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs index d1bff4a76..1f88791d7 100644 --- a/crates/ra_ide/src/lib.rs +++ b/crates/ra_ide/src/lib.rs @@ -56,7 +56,7 @@ use ra_db::{ salsa::{self, ParallelDatabase}, CheckCanceled, Env, FileLoader, SourceDatabase, }; -use ra_syntax::{SourceFile, TextRange, TextUnit}; +use ra_syntax::{tokenize, SourceFile, SyntaxKind, TextRange, TextUnit}; use crate::{db::LineIndexDatabase, display::ToNav, symbol_index::FileSymbol}; @@ -470,6 +470,13 @@ impl Analysis { position: FilePosition, new_name: &str, ) -> Cancelable>> { + let tokens = tokenize(new_name); + if tokens.len() != 1 + || (tokens[0].kind != SyntaxKind::IDENT && tokens[0].kind != SyntaxKind::UNDERSCORE) + { + return Ok(None); + } + self.with_db(|db| references::rename(db, position, new_name)) } -- cgit v1.2.3 From 645df2b5f5664b7730293d8f7441364799aacbf9 Mon Sep 17 00:00:00 2001 From: Wilco Kusee Date: Fri, 29 Nov 2019 16:03:39 +0100 Subject: Test rename for various identifiers --- crates/ra_ide/src/references/rename.rs | 43 ++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/references/rename.rs b/crates/ra_ide/src/references/rename.rs index ea6b354c2..c1771edf8 100644 --- a/crates/ra_ide/src/references/rename.rs +++ b/crates/ra_ide/src/references/rename.rs @@ -123,6 +123,49 @@ mod tests { mock_analysis::analysis_and_position, mock_analysis::single_file_with_position, FileId, }; + #[test] + fn test_rename_to_underscore() { + test_rename( + r#" + fn main() { + let i<|> = 1; + }"#, + "_", + r#" + fn main() { + let _ = 1; + }"#, + ); + } + + #[test] + fn test_rename_to_raw_identifier() { + test_rename( + r#" + fn main() { + let i<|> = 1; + }"#, + "r#fn", + r#" + fn main() { + let r#fn = 1; + }"#, + ); + } + + #[test] + fn test_rename_to_invalid_identifier() { + let (analysis, position) = single_file_with_position( + " + fn main() { + let i<|> = 1; + }", + ); + let new_name = "invalid!"; + let source_change = analysis.rename(position, new_name).unwrap(); + assert!(source_change.is_none()); + } + #[test] fn test_rename_for_local() { test_rename( -- cgit v1.2.3 From b3856568af3a21bfd13584e06fce852f84811fdb Mon Sep 17 00:00:00 2001 From: Wilco Kusee Date: Fri, 29 Nov 2019 16:06:20 +0100 Subject: Push identifier check to rename function --- crates/ra_ide/src/lib.rs | 9 +-------- crates/ra_ide/src/references/rename.rs | 9 ++++++++- 2 files changed, 9 insertions(+), 9 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs index 1f88791d7..d1bff4a76 100644 --- a/crates/ra_ide/src/lib.rs +++ b/crates/ra_ide/src/lib.rs @@ -56,7 +56,7 @@ use ra_db::{ salsa::{self, ParallelDatabase}, CheckCanceled, Env, FileLoader, SourceDatabase, }; -use ra_syntax::{tokenize, SourceFile, SyntaxKind, TextRange, TextUnit}; +use ra_syntax::{SourceFile, TextRange, TextUnit}; use crate::{db::LineIndexDatabase, display::ToNav, symbol_index::FileSymbol}; @@ -470,13 +470,6 @@ impl Analysis { position: FilePosition, new_name: &str, ) -> Cancelable>> { - let tokens = tokenize(new_name); - if tokens.len() != 1 - || (tokens[0].kind != SyntaxKind::IDENT && tokens[0].kind != SyntaxKind::UNDERSCORE) - { - return Ok(None); - } - self.with_db(|db| references::rename(db, position, new_name)) } diff --git a/crates/ra_ide/src/references/rename.rs b/crates/ra_ide/src/references/rename.rs index c1771edf8..b804d5f6d 100644 --- a/crates/ra_ide/src/references/rename.rs +++ b/crates/ra_ide/src/references/rename.rs @@ -2,7 +2,7 @@ use hir::ModuleSource; use ra_db::{RelativePath, RelativePathBuf, SourceDatabase, SourceDatabaseExt}; -use ra_syntax::{algo::find_node_at_offset, ast, AstNode, SyntaxNode}; +use ra_syntax::{algo::find_node_at_offset, ast, tokenize, AstNode, SyntaxKind, SyntaxNode}; use ra_text_edit::TextEdit; use crate::{ @@ -17,6 +17,13 @@ pub(crate) fn rename( position: FilePosition, new_name: &str, ) -> Option> { + let tokens = tokenize(new_name); + if tokens.len() != 1 + || (tokens[0].kind != SyntaxKind::IDENT && tokens[0].kind != SyntaxKind::UNDERSCORE) + { + return None; + } + let parse = db.parse(position.file_id); if let Some((ast_name, ast_module)) = find_name_and_module_at_offset(parse.tree().syntax(), position) -- cgit v1.2.3 From a5a07bde049c59059bc4a68b16a49a174d22cf65 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Mon, 2 Dec 2019 19:27:31 +0100 Subject: Add tests for checking the impl self type --- crates/ra_ide/src/completion/complete_dot.rs | 33 ++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/completion/complete_dot.rs b/crates/ra_ide/src/completion/complete_dot.rs index b6fe48627..a52eb0ee4 100644 --- a/crates/ra_ide/src/completion/complete_dot.rs +++ b/crates/ra_ide/src/completion/complete_dot.rs @@ -216,6 +216,39 @@ mod tests { ); } + #[test] + fn test_method_completion_only_fitting_impls() { + assert_debug_snapshot!( + do_ref_completion( + r" + struct A {} + impl A { + fn the_method(&self) {} + } + impl A { + fn the_other_method(&self) {} + } + fn foo(a: A) { + a.<|> + } + ", + ), + @r###" + [ + CompletionItem { + label: "the_method()", + source_range: [243; 243), + delete: [243; 243), + insert: "the_method()$0", + kind: Method, + lookup: "the_method", + detail: "fn the_method(&self)", + }, + ] + "### + ); + } + #[test] fn test_trait_method_completion() { assert_debug_snapshot!( -- cgit v1.2.3 From 009437f5d9949d2276aa26040e03af0ab328acf3 Mon Sep 17 00:00:00 2001 From: ice1000 Date: Tue, 3 Dec 2019 11:07:56 -0500 Subject: Replace `ra_hir_expand::either` with crate --- crates/ra_ide/Cargo.toml | 1 + crates/ra_ide/src/completion/complete_path.rs | 5 +++-- crates/ra_ide/src/display/navigation_target.rs | 7 ++++--- 3 files changed, 8 insertions(+), 5 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/Cargo.toml b/crates/ra_ide/Cargo.toml index e6383dd35..e3439ae31 100644 --- a/crates/ra_ide/Cargo.toml +++ b/crates/ra_ide/Cargo.toml @@ -11,6 +11,7 @@ doctest = false wasm = [] [dependencies] +either = "1.5" format-buf = "1.0.0" itertools = "0.8.0" join_to_string = "0.1.3" diff --git a/crates/ra_ide/src/completion/complete_path.rs b/crates/ra_ide/src/completion/complete_path.rs index 89e0009a1..28f94e0a7 100644 --- a/crates/ra_ide/src/completion/complete_path.rs +++ b/crates/ra_ide/src/completion/complete_path.rs @@ -1,6 +1,7 @@ //! FIXME: write short doc here -use hir::{Adt, Either, HasSource, PathResolution}; +use either::Either; +use hir::{Adt, HasSource, PathResolution}; use ra_syntax::AstNode; use test_utils::tested_by; @@ -27,7 +28,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { } if Some(module) == ctx.module { if let Some(import) = import { - if let Either::A(use_tree) = import.source(ctx.db).value { + if let Either::Left(use_tree) = import.source(ctx.db).value { if use_tree.syntax().text_range().contains_inclusive(ctx.offset) { // for `use self::foo<|>`, don't suggest `foo` as a completion tested_by!(dont_complete_current_use); diff --git a/crates/ra_ide/src/display/navigation_target.rs b/crates/ra_ide/src/display/navigation_target.rs index 61dca14ac..f920d3db6 100644 --- a/crates/ra_ide/src/display/navigation_target.rs +++ b/crates/ra_ide/src/display/navigation_target.rs @@ -1,6 +1,7 @@ //! FIXME: write short doc here -use hir::{AssocItem, Either, FieldSource, HasSource, InFile, ModuleSource}; +use either::Either; +use hir::{AssocItem, FieldSource, HasSource, InFile, ModuleSource}; use ra_db::{FileId, SourceDatabase}; use ra_syntax::{ ast::{self, DocCommentsOwner, NameOwner}, @@ -342,10 +343,10 @@ impl ToNav for hir::Local { fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { let src = self.source(db); let (full_range, focus_range) = match src.value { - Either::A(it) => { + Either::Left(it) => { (it.syntax().text_range(), it.name().map(|it| it.syntax().text_range())) } - Either::B(it) => (it.syntax().text_range(), Some(it.self_kw_token().text_range())), + Either::Right(it) => (it.syntax().text_range(), Some(it.self_kw_token().text_range())), }; let name = match self.name(db) { Some(it) => it.to_string().into(), -- cgit v1.2.3 From b437dca4bd100c0a7a498d5960d566a0ccd92432 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vincent=20Rouill=C3=A9?= Date: Wed, 4 Dec 2019 23:05:01 +0100 Subject: Run rustfmt with respect to Cargo.toml edition --- crates/ra_ide/src/lib.rs | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs index d1bff4a76..779a81b2c 100644 --- a/crates/ra_ide/src/lib.rs +++ b/crates/ra_ide/src/lib.rs @@ -422,6 +422,11 @@ impl Analysis { self.with_db(|db| parent_module::crate_for(db, file_id)) } + /// Returns the edition of the given crate. + pub fn crate_edition(&self, crate_id: CrateId) -> Cancelable { + self.with_db(|db| db.crate_graph().edition(crate_id)) + } + /// Returns the root file of the given crate. pub fn crate_root(&self, crate_id: CrateId) -> Cancelable { self.with_db(|db| db.crate_graph().crate_root(crate_id)) -- cgit v1.2.3 From 38853459e3d964cc7f635829cdc66f5faee33d85 Mon Sep 17 00:00:00 2001 From: ice1000 Date: Tue, 3 Dec 2019 15:24:02 -0500 Subject: Add `ModuleSource::Block` --- crates/ra_ide/src/display/navigation_target.rs | 43 +++++++++----------------- crates/ra_ide/src/references/rename.rs | 2 +- crates/ra_ide/src/references/search_scope.rs | 5 +++ 3 files changed, 21 insertions(+), 29 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/display/navigation_target.rs b/crates/ra_ide/src/display/navigation_target.rs index f920d3db6..b376fcdae 100644 --- a/crates/ra_ide/src/display/navigation_target.rs +++ b/crates/ra_ide/src/display/navigation_target.rs @@ -231,34 +231,21 @@ impl ToNav for hir::Module { fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { let src = self.definition_source(db); let name = self.name(db).map(|it| it.to_string().into()).unwrap_or_default(); - match &src.value { - ModuleSource::SourceFile(node) => { - let frange = original_range(db, src.with_value(node.syntax())); - - NavigationTarget::from_syntax( - frange.file_id, - name, - None, - frange.range, - node.syntax().kind(), - None, - None, - ) - } - ModuleSource::Module(node) => { - let frange = original_range(db, src.with_value(node.syntax())); - - NavigationTarget::from_syntax( - frange.file_id, - name, - None, - frange.range, - node.syntax().kind(), - node.doc_comment_text(), - node.short_label(), - ) - } - } + let syntax = match &src.value { + ModuleSource::SourceFile(node) => node.syntax(), + ModuleSource::Module(node) => node.syntax(), + ModuleSource::Block(node) => node.syntax(), + }; + let frange = original_range(db, src.with_value(syntax)); + NavigationTarget::from_syntax( + frange.file_id, + name, + None, + frange.range, + syntax.kind(), + None, + None, + ) } } diff --git a/crates/ra_ide/src/references/rename.rs b/crates/ra_ide/src/references/rename.rs index b804d5f6d..1abf20215 100644 --- a/crates/ra_ide/src/references/rename.rs +++ b/crates/ra_ide/src/references/rename.rs @@ -88,7 +88,7 @@ fn rename_mod( file_system_edits.push(move_file); } } - ModuleSource::Module(..) => {} + ModuleSource::Module(..) | ModuleSource::Block(..) => {} } } diff --git a/crates/ra_ide/src/references/search_scope.rs b/crates/ra_ide/src/references/search_scope.rs index f5c9589f4..92eb2015b 100644 --- a/crates/ra_ide/src/references/search_scope.rs +++ b/crates/ra_ide/src/references/search_scope.rs @@ -96,6 +96,10 @@ impl NameDefinition { let range = Some(m.syntax().text_range()); res.insert(file_id, range); } + ModuleSource::Block(b) => { + let range = Some(b.syntax().text_range()); + res.insert(file_id, range); + } ModuleSource::SourceFile(_) => { res.insert(file_id, None); res.extend(parent_module.children(db).map(|m| { @@ -137,6 +141,7 @@ impl NameDefinition { let mut res = FxHashMap::default(); let range = match module_src.value { ModuleSource::Module(m) => Some(m.syntax().text_range()), + ModuleSource::Block(b) => Some(b.syntax().text_range()), ModuleSource::SourceFile(_) => None, }; res.insert(file_id, range); -- cgit v1.2.3 From 088f50c0ab351d5ac072547a47c1ce7eeae029f3 Mon Sep 17 00:00:00 2001 From: ice1000 Date: Wed, 4 Dec 2019 13:35:24 -0500 Subject: No block at the moment --- crates/ra_ide/src/display/navigation_target.rs | 1 - crates/ra_ide/src/references/rename.rs | 2 +- crates/ra_ide/src/references/search_scope.rs | 5 ----- 3 files changed, 1 insertion(+), 7 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/display/navigation_target.rs b/crates/ra_ide/src/display/navigation_target.rs index b376fcdae..add11fbc3 100644 --- a/crates/ra_ide/src/display/navigation_target.rs +++ b/crates/ra_ide/src/display/navigation_target.rs @@ -234,7 +234,6 @@ impl ToNav for hir::Module { let syntax = match &src.value { ModuleSource::SourceFile(node) => node.syntax(), ModuleSource::Module(node) => node.syntax(), - ModuleSource::Block(node) => node.syntax(), }; let frange = original_range(db, src.with_value(syntax)); NavigationTarget::from_syntax( diff --git a/crates/ra_ide/src/references/rename.rs b/crates/ra_ide/src/references/rename.rs index 1abf20215..b804d5f6d 100644 --- a/crates/ra_ide/src/references/rename.rs +++ b/crates/ra_ide/src/references/rename.rs @@ -88,7 +88,7 @@ fn rename_mod( file_system_edits.push(move_file); } } - ModuleSource::Module(..) | ModuleSource::Block(..) => {} + ModuleSource::Module(..) => {} } } diff --git a/crates/ra_ide/src/references/search_scope.rs b/crates/ra_ide/src/references/search_scope.rs index 92eb2015b..f5c9589f4 100644 --- a/crates/ra_ide/src/references/search_scope.rs +++ b/crates/ra_ide/src/references/search_scope.rs @@ -96,10 +96,6 @@ impl NameDefinition { let range = Some(m.syntax().text_range()); res.insert(file_id, range); } - ModuleSource::Block(b) => { - let range = Some(b.syntax().text_range()); - res.insert(file_id, range); - } ModuleSource::SourceFile(_) => { res.insert(file_id, None); res.extend(parent_module.children(db).map(|m| { @@ -141,7 +137,6 @@ impl NameDefinition { let mut res = FxHashMap::default(); let range = match module_src.value { ModuleSource::Module(m) => Some(m.syntax().text_range()), - ModuleSource::Block(b) => Some(b.syntax().text_range()), ModuleSource::SourceFile(_) => None, }; res.insert(file_id, range); -- cgit v1.2.3 From 7702f690a9592605be71104ec9d0b732af940fcc Mon Sep 17 00:00:00 2001 From: ice1000 Date: Thu, 5 Dec 2019 08:28:31 -0500 Subject: One pub function less is good! --- crates/ra_ide/src/parent_module.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/parent_module.rs b/crates/ra_ide/src/parent_module.rs index 616d69fce..aef3fa3df 100644 --- a/crates/ra_ide/src/parent_module.rs +++ b/crates/ra_ide/src/parent_module.rs @@ -1,6 +1,6 @@ //! FIXME: write short doc here -use ra_db::{CrateId, FileId, FilePosition}; +use ra_db::{CrateId, FileId, FilePosition, SourceDatabase}; use crate::{db::RootDatabase, NavigationTarget}; @@ -21,7 +21,8 @@ pub(crate) fn parent_module(db: &RootDatabase, position: FilePosition) -> Vec Vec { - let src = hir::ModuleSource::from_file_id(db, file_id); + let source_file = db.parse(file_id).tree(); + let src = hir::ModuleSource::SourceFile(source_file); let module = match hir::Module::from_definition(db, hir::InFile { file_id: file_id.into(), value: src }) { -- cgit v1.2.3 From a565072ddeac519eea3b415a57e9fd208e20e562 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Fri, 6 Dec 2019 19:30:15 +0100 Subject: Try to make go to definition work in format! SourceAnalyzer didn't work properly within expression macro expansions because it didn't find the enclosing function. Fix this by going up the expansion chain to find ancestors. This makes the test work, but apparently in real usage it's still not working. --- crates/ra_ide/src/goto_definition.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/goto_definition.rs b/crates/ra_ide/src/goto_definition.rs index 76a741207..b1d567ca7 100644 --- a/crates/ra_ide/src/goto_definition.rs +++ b/crates/ra_ide/src/goto_definition.rs @@ -693,4 +693,31 @@ mod tests { "foo FN_DEF FileId(1) [52; 63) [55; 58)", ); } + + #[test] + fn goto_through_format() { + check_goto( + " + //- /lib.rs + #[macro_export] + macro_rules! format { + ($($arg:tt)*) => ($crate::fmt::format($crate::__export::format_args!($($arg)*))) + } + #[rustc_builtin_macro] + #[macro_export] + macro_rules! format_args { + ($fmt:expr) => ({ /* compiler built-in */ }); + ($fmt:expr, $($args:tt)*) => ({ /* compiler built-in */ }) + } + pub mod __export { + pub use crate::format_args; + } + fn foo() -> i8 {} + fn test() { + format!(\"{}\", fo<|>o()) + } + ", + "foo FN_DEF FileId(1) [359; 376) [362; 365)", + ); + } } -- cgit v1.2.3 From c80dc0ad3aee717f9d15c11d300d0eb1c10f1cc8 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Fri, 6 Dec 2019 21:24:41 +0100 Subject: Make the goto_through_format test actually fail :( --- crates/ra_ide/src/goto_definition.rs | 3 +++ 1 file changed, 3 insertions(+) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/goto_definition.rs b/crates/ra_ide/src/goto_definition.rs index b1d567ca7..d3c198813 100644 --- a/crates/ra_ide/src/goto_definition.rs +++ b/crates/ra_ide/src/goto_definition.rs @@ -689,11 +689,13 @@ mod tests { fo<|>o(); } } + mod confuse_index { fn foo(); } ", "foo FN_DEF FileId(1) [52; 63) [55; 58)", ); } + #[should_panic] // currently failing because of expr mapping problems #[test] fn goto_through_format() { check_goto( @@ -711,6 +713,7 @@ mod tests { } pub mod __export { pub use crate::format_args; + fn foo() {} // for index confusion } fn foo() -> i8 {} fn test() { -- cgit v1.2.3 From decc4cb084e7cfb9be845bf4b5ad06ff2c4c42c2 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 7 Dec 2019 19:14:01 +0100 Subject: Show type hints for & patterns --- crates/ra_ide/src/inlay_hints.rs | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/inlay_hints.rs b/crates/ra_ide/src/inlay_hints.rs index 59eced9d7..3730121af 100644 --- a/crates/ra_ide/src/inlay_hints.rs +++ b/crates/ra_ide/src/inlay_hints.rs @@ -122,18 +122,11 @@ fn get_leaf_pats(root_pat: ast::Pat) -> Vec { while let Some(maybe_leaf_pat) = pats_to_process.pop_front() { match &maybe_leaf_pat { - ast::Pat::BindPat(bind_pat) => { - if let Some(pat) = bind_pat.pat() { - pats_to_process.push_back(pat); - } else { - leaf_pats.push(maybe_leaf_pat); - } - } - ast::Pat::TuplePat(tuple_pat) => { - for arg_pat in tuple_pat.args() { - pats_to_process.push_back(arg_pat); - } - } + ast::Pat::BindPat(bind_pat) => match bind_pat.pat() { + Some(pat) => pats_to_process.push_back(pat), + _ => leaf_pats.push(maybe_leaf_pat), + }, + ast::Pat::TuplePat(tuple_pat) => pats_to_process.extend(tuple_pat.args()), ast::Pat::RecordPat(record_pat) => { if let Some(pat_list) = record_pat.record_field_pat_list() { pats_to_process.extend( @@ -151,10 +144,9 @@ fn get_leaf_pats(root_pat: ast::Pat) -> Vec { } } ast::Pat::TupleStructPat(tuple_struct_pat) => { - for arg_pat in tuple_struct_pat.args() { - pats_to_process.push_back(arg_pat); - } + pats_to_process.extend(tuple_struct_pat.args()) } + ast::Pat::RefPat(ref_pat) => pats_to_process.extend(ref_pat.pat()), _ => (), } } @@ -163,9 +155,10 @@ fn get_leaf_pats(root_pat: ast::Pat) -> Vec { #[cfg(test)] mod tests { - use crate::mock_analysis::single_file; use insta::assert_debug_snapshot; + use crate::mock_analysis::single_file; + #[test] fn let_statement() { let (analysis, file_id) = single_file( @@ -202,6 +195,7 @@ fn main() { let test = (42, 'a'); let (a, (b, c, (d, e), f)) = (2, (3, 4, (6.6, 7.7), 5)); + let &x = &92; }"#, ); @@ -257,6 +251,11 @@ fn main() { kind: TypeHint, label: "f64", }, + InlayHint { + range: [627; 628), + kind: TypeHint, + label: "i32", + }, ] "### ); -- cgit v1.2.3 From 1692f07393dba4f5c122df1a609d5b18751bf406 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 7 Dec 2019 18:48:35 +0100 Subject: ToNav for GenericParam --- crates/ra_ide/src/display/navigation_target.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/display/navigation_target.rs b/crates/ra_ide/src/display/navigation_target.rs index add11fbc3..e8c3d980f 100644 --- a/crates/ra_ide/src/display/navigation_target.rs +++ b/crates/ra_ide/src/display/navigation_target.rs @@ -6,7 +6,7 @@ use ra_db::{FileId, SourceDatabase}; use ra_syntax::{ ast::{self, DocCommentsOwner, NameOwner}, match_ast, AstNode, SmolStr, - SyntaxKind::{self, BIND_PAT}, + SyntaxKind::{self, BIND_PAT, TYPE_PARAM}, TextRange, }; @@ -351,6 +351,26 @@ impl ToNav for hir::Local { } } +impl ToNav for hir::GenericParam { + fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { + let src = self.source(db); + let range = match src.value { + Either::Left(it) => it.syntax().text_range(), + Either::Right(it) => it.syntax().text_range(), + }; + NavigationTarget { + file_id: src.file_id.original_file(db), + name: self.name(db).to_string().into(), + kind: TYPE_PARAM, + full_range: range, + focus_range: None, + container_name: None, + description: None, + docs: None, + } + } +} + pub(crate) fn docs_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option { let parse = db.parse(symbol.file_id); let node = symbol.ptr.to_node(parse.tree().syntax()); -- cgit v1.2.3 From d1a01aa2f8ca9eff9ba2321f2f113623742e212c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 7 Dec 2019 18:54:18 +0100 Subject: Gotodef for TypeParameters --- crates/ra_ide/src/goto_definition.rs | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/goto_definition.rs b/crates/ra_ide/src/goto_definition.rs index d3c198813..64c0cbad4 100644 --- a/crates/ra_ide/src/goto_definition.rs +++ b/crates/ra_ide/src/goto_definition.rs @@ -64,9 +64,11 @@ pub(crate) fn reference_definition( let name_kind = classify_name_ref(db, name_ref).map(|d| d.kind); match name_kind { - Some(Macro(mac)) => return Exact(mac.to_nav(db)), - Some(Field(field)) => return Exact(field.to_nav(db)), - Some(AssocItem(assoc)) => return Exact(assoc.to_nav(db)), + Some(Macro(it)) => return Exact(it.to_nav(db)), + Some(Field(it)) => return Exact(it.to_nav(db)), + Some(GenericParam(it)) => return Exact(it.to_nav(db)), + Some(AssocItem(it)) => return Exact(it.to_nav(db)), + Some(Local(it)) => return Exact(it.to_nav(db)), Some(Def(def)) => match NavigationTarget::from_def(db, def) { Some(nav) => return Exact(nav), None => return Approximate(vec![]), @@ -77,10 +79,6 @@ pub(crate) fn reference_definition( // us to the actual type return Exact(imp.to_nav(db)); } - Some(Local(local)) => return Exact(local.to_nav(db)), - Some(GenericParam(_)) => { - // FIXME: go to the generic param def - } None => {} }; @@ -723,4 +721,17 @@ mod tests { "foo FN_DEF FileId(1) [359; 376) [362; 365)", ); } + + #[test] + fn goto_for_type_param() { + check_goto( + " + //- /lib.rs + struct Foo { + t: <|>T, + } + ", + "T TYPE_PARAM FileId(1) [11; 12)", + ); + } } -- cgit v1.2.3 From 7d2080a0311cab62388f416beeb360695dbc5ded Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 7 Dec 2019 19:52:09 +0100 Subject: Classify name works for TypeParams --- crates/ra_ide/src/references/classify.rs | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/references/classify.rs b/crates/ra_ide/src/references/classify.rs index b716d32e5..65df2e335 100644 --- a/crates/ra_ide/src/references/classify.rs +++ b/crates/ra_ide/src/references/classify.rs @@ -110,6 +110,15 @@ pub(crate) fn classify_name(db: &RootDatabase, name: InFile<&ast::Name>) -> Opti kind: NameKind::Macro(def), }) }, + ast::TypeParam(it) => { + let src = name.with_value(it); + let def = hir::GenericParam::from_source(db, src)?; + Some(NameDefinition { + visibility: None, + container: def.module(db), + kind: NameKind::GenericParam(def), + }) + }, _ => None, } } -- cgit v1.2.3 From f4f8b8147426b0096d4b5126e487caaa13d13c27 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 7 Dec 2019 20:05:08 +0100 Subject: Get the right analyzer for impls --- crates/ra_ide/src/references/classify.rs | 1 - crates/ra_ide/src/snapshots/highlighting.html | 9 +++++++++ crates/ra_ide/src/syntax_highlighting.rs | 9 +++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/references/classify.rs b/crates/ra_ide/src/references/classify.rs index 65df2e335..ed98dbc13 100644 --- a/crates/ra_ide/src/references/classify.rs +++ b/crates/ra_ide/src/references/classify.rs @@ -178,7 +178,6 @@ pub(crate) fn classify_name_ref( Some(NameDefinition { kind, container, visibility: None }) } PathResolution::GenericParam(par) => { - // FIXME: get generic param def let kind = NameKind::GenericParam(par); Some(NameDefinition { kind, container, visibility }) } diff --git a/crates/ra_ide/src/snapshots/highlighting.html b/crates/ra_ide/src/snapshots/highlighting.html index b39c4d371..4166a8f90 100644 --- a/crates/ra_ide/src/snapshots/highlighting.html +++ b/crates/ra_ide/src/snapshots/highlighting.html @@ -9,6 +9,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .parameter { color: #94BFF3; } .builtin { color: #DD6718; } .text { color: #DCDCCC; } +.type { color: #7CB8BB; } .attribute { color: #94BFF3; } .literal { color: #BFEBBF; } .macro { color: #94BFF3; } @@ -45,4 +46,12 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd let z = &y; y; +} + +enum E<X> { + V(X) +} + +impl<X> E<X> { + fn new<T>() -> E<T> {} } \ No newline at end of file diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index e6a79541f..20eefeb57 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -255,6 +255,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .parameter { color: #94BFF3; } .builtin { color: #DD6718; } .text { color: #DCDCCC; } +.type { color: #7CB8BB; } .attribute { color: #94BFF3; } .literal { color: #BFEBBF; } .macro { color: #94BFF3; } @@ -303,6 +304,14 @@ fn main() { y; } + +enum E { + V(X) +} + +impl E { + fn new() -> E {} +} "# .trim(), ); -- cgit v1.2.3 From 88c5b1282a5770097c6c768b24bedfc3a6944e08 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 7 Dec 2019 20:09:53 +0100 Subject: Rename GenericParam -> TypeParam We don't have LifetimeParam yet, but they are planned! --- crates/ra_ide/src/display/navigation_target.rs | 2 +- crates/ra_ide/src/goto_definition.rs | 2 +- crates/ra_ide/src/hover.rs | 2 +- crates/ra_ide/src/references.rs | 2 +- crates/ra_ide/src/references/classify.rs | 8 ++++---- crates/ra_ide/src/references/name_definition.rs | 6 +++--- crates/ra_ide/src/snapshots/rainbow_highlighting.html | 1 + crates/ra_ide/src/syntax_highlighting.rs | 3 +-- 8 files changed, 13 insertions(+), 13 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/display/navigation_target.rs b/crates/ra_ide/src/display/navigation_target.rs index e8c3d980f..6a6b49afd 100644 --- a/crates/ra_ide/src/display/navigation_target.rs +++ b/crates/ra_ide/src/display/navigation_target.rs @@ -351,7 +351,7 @@ impl ToNav for hir::Local { } } -impl ToNav for hir::GenericParam { +impl ToNav for hir::TypeParam { fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { let src = self.source(db); let range = match src.value { diff --git a/crates/ra_ide/src/goto_definition.rs b/crates/ra_ide/src/goto_definition.rs index 64c0cbad4..1b968134d 100644 --- a/crates/ra_ide/src/goto_definition.rs +++ b/crates/ra_ide/src/goto_definition.rs @@ -66,7 +66,7 @@ pub(crate) fn reference_definition( match name_kind { Some(Macro(it)) => return Exact(it.to_nav(db)), Some(Field(it)) => return Exact(it.to_nav(db)), - Some(GenericParam(it)) => return Exact(it.to_nav(db)), + Some(TypeParam(it)) => return Exact(it.to_nav(db)), Some(AssocItem(it)) => return Exact(it.to_nav(db)), Some(Local(it)) => return Exact(it.to_nav(db)), Some(Def(def)) => match NavigationTarget::from_def(db, def) { diff --git a/crates/ra_ide/src/hover.rs b/crates/ra_ide/src/hover.rs index d8185c688..d372ca758 100644 --- a/crates/ra_ide/src/hover.rs +++ b/crates/ra_ide/src/hover.rs @@ -138,7 +138,7 @@ fn hover_text_from_name_kind( *no_fallback = true; None } - GenericParam(_) | SelfType(_) => { + TypeParam(_) | SelfType(_) => { // FIXME: Hover for generic param None } diff --git a/crates/ra_ide/src/references.rs b/crates/ra_ide/src/references.rs index 3e7bfd872..e3ecde50d 100644 --- a/crates/ra_ide/src/references.rs +++ b/crates/ra_ide/src/references.rs @@ -85,7 +85,7 @@ pub(crate) fn find_all_refs( NameKind::Def(def) => NavigationTarget::from_def(db, def)?, NameKind::SelfType(imp) => imp.to_nav(db), NameKind::Local(local) => local.to_nav(db), - NameKind::GenericParam(_) => return None, + NameKind::TypeParam(_) => return None, }; let search_scope = { diff --git a/crates/ra_ide/src/references/classify.rs b/crates/ra_ide/src/references/classify.rs index ed98dbc13..c1f091ec0 100644 --- a/crates/ra_ide/src/references/classify.rs +++ b/crates/ra_ide/src/references/classify.rs @@ -112,11 +112,11 @@ pub(crate) fn classify_name(db: &RootDatabase, name: InFile<&ast::Name>) -> Opti }, ast::TypeParam(it) => { let src = name.with_value(it); - let def = hir::GenericParam::from_source(db, src)?; + let def = hir::TypeParam::from_source(db, src)?; Some(NameDefinition { visibility: None, container: def.module(db), - kind: NameKind::GenericParam(def), + kind: NameKind::TypeParam(def), }) }, _ => None, @@ -177,8 +177,8 @@ pub(crate) fn classify_name_ref( let kind = NameKind::Local(local); Some(NameDefinition { kind, container, visibility: None }) } - PathResolution::GenericParam(par) => { - let kind = NameKind::GenericParam(par); + PathResolution::TypeParam(par) => { + let kind = NameKind::TypeParam(par); Some(NameDefinition { kind, container, visibility }) } PathResolution::Macro(def) => { diff --git a/crates/ra_ide/src/references/name_definition.rs b/crates/ra_ide/src/references/name_definition.rs index 10d3a2364..8c67c8863 100644 --- a/crates/ra_ide/src/references/name_definition.rs +++ b/crates/ra_ide/src/references/name_definition.rs @@ -4,8 +4,8 @@ //! Note that the reference search is possible for not all of the classified items. use hir::{ - Adt, AssocItem, GenericParam, HasSource, ImplBlock, Local, MacroDef, Module, ModuleDef, - StructField, VariantDef, + Adt, AssocItem, HasSource, ImplBlock, Local, MacroDef, Module, ModuleDef, StructField, + TypeParam, VariantDef, }; use ra_syntax::{ast, ast::VisibilityOwner}; @@ -19,7 +19,7 @@ pub enum NameKind { Def(ModuleDef), SelfType(ImplBlock), Local(Local), - GenericParam(GenericParam), + TypeParam(TypeParam), } #[derive(PartialEq, Eq)] diff --git a/crates/ra_ide/src/snapshots/rainbow_highlighting.html b/crates/ra_ide/src/snapshots/rainbow_highlighting.html index 79f11ea80..9dfbc8047 100644 --- a/crates/ra_ide/src/snapshots/rainbow_highlighting.html +++ b/crates/ra_ide/src/snapshots/rainbow_highlighting.html @@ -9,6 +9,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .parameter { color: #94BFF3; } .builtin { color: #DD6718; } .text { color: #DCDCCC; } +.type { color: #7CB8BB; } .attribute { color: #94BFF3; } .literal { color: #BFEBBF; } .macro { color: #94BFF3; } diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index 20eefeb57..7ecb1a027 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -225,8 +225,7 @@ fn highlight_name(db: &RootDatabase, name_kind: NameKind) -> &'static str { Def(hir::ModuleDef::Trait(_)) => "type", Def(hir::ModuleDef::TypeAlias(_)) => "type", Def(hir::ModuleDef::BuiltinType(_)) => "type", - SelfType(_) => "type", - GenericParam(_) => "type", + SelfType(_) | TypeParam(_) => "type", Local(local) => { if local.is_mut(db) { "variable.mut" -- cgit v1.2.3 From 08d3166c8b23b5a342f32e088f7e0d6e032ec17b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 8 Dec 2019 12:01:45 +0100 Subject: Cleanup Crate API --- crates/ra_ide/src/references/search_scope.rs | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/references/search_scope.rs b/crates/ra_ide/src/references/search_scope.rs index f5c9589f4..241dd358f 100644 --- a/crates/ra_ide/src/references/search_scope.rs +++ b/crates/ra_ide/src/references/search_scope.rs @@ -5,7 +5,7 @@ use std::mem; use hir::{DefWithBody, HasSource, ModuleSource}; -use ra_db::{FileId, SourceDatabase, SourceDatabaseExt}; +use ra_db::{FileId, SourceDatabaseExt}; use ra_prof::profile; use ra_syntax::{AstNode, TextRange}; use rustc_hash::FxHashMap; @@ -120,15 +120,11 @@ impl NameDefinition { } if vis.as_str() == "pub" { let krate = self.container.krate(); - let crate_graph = db.crate_graph(); - for crate_id in crate_graph.iter() { - let mut crate_deps = crate_graph.dependencies(crate_id); - if crate_deps.any(|dep| dep.crate_id() == krate.crate_id()) { - let root_file = crate_graph.crate_root(crate_id); - let source_root_id = db.file_source_root(root_file); - let source_root = db.source_root(source_root_id); - res.extend(source_root.walk().map(|id| (id, None))); - } + for rev_dep in krate.reverse_dependencies(db) { + let root_file = rev_dep.root_file(db); + let source_root_id = db.file_source_root(root_file); + let source_root = db.source_root(source_root_id); + res.extend(source_root.walk().map(|id| (id, None))); } return SearchScope::new(res); } -- cgit v1.2.3 From b37c6a746b6c7cf85dc1ec6e40ac41455b8f2ec0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 8 Dec 2019 12:44:14 +0100 Subject: Remove ty from code_model --- crates/ra_ide/src/parent_module.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/parent_module.rs b/crates/ra_ide/src/parent_module.rs index aef3fa3df..f5a788c07 100644 --- a/crates/ra_ide/src/parent_module.rs +++ b/crates/ra_ide/src/parent_module.rs @@ -30,7 +30,7 @@ pub(crate) fn crate_for(db: &RootDatabase, file_id: FileId) -> Vec { None => return Vec::new(), }; let krate = module.krate(); - vec![krate.crate_id()] + vec![krate.into()] } #[cfg(test)] -- cgit v1.2.3 From 91f28e43a2686d0ac06f73b5fed11351aad428a9 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 8 Dec 2019 09:26:17 +0100 Subject: Fix expansion of format_args --- crates/ra_ide/src/goto_definition.rs | 1 - 1 file changed, 1 deletion(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/goto_definition.rs b/crates/ra_ide/src/goto_definition.rs index 1b968134d..077b0beda 100644 --- a/crates/ra_ide/src/goto_definition.rs +++ b/crates/ra_ide/src/goto_definition.rs @@ -693,7 +693,6 @@ mod tests { ); } - #[should_panic] // currently failing because of expr mapping problems #[test] fn goto_through_format() { check_goto( -- cgit v1.2.3 From 2223620313e94deb04d1565a75004579aa1a9e07 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 8 Dec 2019 11:22:39 +0100 Subject: Fix range in goto_through_format test --- crates/ra_ide/src/goto_definition.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/goto_definition.rs b/crates/ra_ide/src/goto_definition.rs index 077b0beda..b93d6a931 100644 --- a/crates/ra_ide/src/goto_definition.rs +++ b/crates/ra_ide/src/goto_definition.rs @@ -717,7 +717,7 @@ mod tests { format!(\"{}\", fo<|>o()) } ", - "foo FN_DEF FileId(1) [359; 376) [362; 365)", + "foo FN_DEF FileId(1) [398; 415) [401; 404)", ); } -- cgit v1.2.3 From a1639d0d1ef201b2b9a425eddecfb41a25f10931 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 8 Dec 2019 12:58:43 +0100 Subject: Remove more dead code --- crates/ra_ide/src/db.rs | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/db.rs b/crates/ra_ide/src/db.rs index f739ebecd..47d0aed6f 100644 --- a/crates/ra_ide/src/db.rs +++ b/crates/ra_ide/src/db.rs @@ -5,7 +5,7 @@ use std::sync::Arc; use ra_db::{ salsa::{self, Database, Durability}, Canceled, CheckCanceled, CrateId, FileId, FileLoader, FileLoaderDelegate, RelativePath, - SourceDatabase, SourceDatabaseExt, SourceRootId, + SourceDatabase, SourceRootId, }; use rustc_hash::FxHashMap; @@ -49,18 +49,6 @@ impl FileLoader for RootDatabase { } } -impl hir::debug::HirDebugHelper for RootDatabase { - fn crate_name(&self, krate: CrateId) -> Option { - self.debug_data.crate_names.get(&krate).cloned() - } - fn file_path(&self, file_id: FileId) -> Option { - let source_root_id = self.file_source_root(file_id); - let source_root_path = self.debug_data.root_paths.get(&source_root_id)?; - let file_path = self.file_relative_path(file_id); - Some(format!("{}/{}", source_root_path, file_path)) - } -} - impl salsa::Database for RootDatabase { fn salsa_runtime(&self) -> &salsa::Runtime { &self.runtime -- cgit v1.2.3 From 61c3887b70820283cb759127e3aecf7cbdbdc8c1 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 8 Dec 2019 17:50:43 +0100 Subject: Remove one more Ty --- crates/ra_ide/src/completion/complete_dot.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/completion/complete_dot.rs b/crates/ra_ide/src/completion/complete_dot.rs index a52eb0ee4..294964887 100644 --- a/crates/ra_ide/src/completion/complete_dot.rs +++ b/crates/ra_ide/src/completion/complete_dot.rs @@ -27,7 +27,7 @@ pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) { complete_methods(acc, ctx, &receiver_ty); // Suggest .await syntax for types that implement Future trait - if ctx.analyzer.impls_future(ctx.db, receiver_ty.into_ty()) { + if ctx.analyzer.impls_future(ctx.db, receiver_ty) { CompletionItem::new(CompletionKind::Keyword, ctx.source_range(), "await") .detail("expr.await") .insert_text("await") -- cgit v1.2.3 From b683cbd93db65f5421ef9b7617a2abfe93928af0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 9 Dec 2019 18:42:45 +0100 Subject: Report correct original range in goto_definition --- crates/ra_ide/src/goto_definition.rs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/goto_definition.rs b/crates/ra_ide/src/goto_definition.rs index b93d6a931..cfe62037f 100644 --- a/crates/ra_ide/src/goto_definition.rs +++ b/crates/ra_ide/src/goto_definition.rs @@ -19,25 +19,23 @@ pub(crate) fn goto_definition( position: FilePosition, ) -> Option>> { let file = db.parse_or_expand(position.file_id.into())?; - let token = file.token_at_offset(position.offset).filter(|it| !it.kind().is_trivia()).next()?; - let token = descend_into_macros(db, position.file_id, token); + let original_token = + file.token_at_offset(position.offset).filter(|it| !it.kind().is_trivia()).next()?; + let token = descend_into_macros(db, position.file_id, original_token.clone()); - let res = match_ast! { + let nav_targets = match_ast! { match (token.value.parent()) { ast::NameRef(name_ref) => { - let navs = reference_definition(db, token.with_value(&name_ref)).to_vec(); - RangeInfo::new(name_ref.syntax().text_range(), navs.to_vec()) + reference_definition(db, token.with_value(&name_ref)).to_vec() }, ast::Name(name) => { - let navs = name_definition(db, token.with_value(&name))?; - RangeInfo::new(name.syntax().text_range(), navs) - + name_definition(db, token.with_value(&name))? }, _ => return None, } }; - Some(res) + Some(RangeInfo::new(original_token.text_range(), nav_targets)) } #[derive(Debug)] -- cgit v1.2.3 From 20ccabc01de7306f44e0b2a460152f0c97f19d76 Mon Sep 17 00:00:00 2001 From: Gabriel Luo Date: Tue, 10 Dec 2019 22:18:05 -0500 Subject: Fixed #2250 Tuple in type annotation expands correctly; Expansion will prefer the following delimiter when possible. New regression tests added to verify the consistency between tuple expansion in type annotation and tuple expansion in rvalue. --- crates/ra_ide/src/extend_selection.rs | 78 +++++++++++++++++++++++++++++------ 1 file changed, 66 insertions(+), 12 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/extend_selection.rs b/crates/ra_ide/src/extend_selection.rs index 4b7bfc0b1..c096ca6ae 100644 --- a/crates/ra_ide/src/extend_selection.rs +++ b/crates/ra_ide/src/extend_selection.rs @@ -34,6 +34,7 @@ fn try_extend_selection(root: &SyntaxNode, range: TextRange) -> Option Option { TYPE_BOUND => T![+], _ => T![,], }; - if let Some(delimiter_node) = nearby_delimiter(delimiter, node, Direction::Prev) { - return Some(TextRange::from_to( - delimiter_node.text_range().start(), - node.text_range().end(), - )); - } + if let Some(delimiter_node) = nearby_delimiter(delimiter, node, Direction::Next) { // Include any following whitespace when delimiter is after list item. let final_node = delimiter_node @@ -190,6 +186,12 @@ fn extend_list_item(node: &SyntaxNode) -> Option { return Some(TextRange::from_to(node.text_range().start(), final_node.text_range().end())); } + if let Some(delimiter_node) = nearby_delimiter(delimiter, node, Direction::Prev) { + return Some(TextRange::from_to( + delimiter_node.text_range().start(), + node.text_range().end(), + )); + } None } @@ -250,14 +252,14 @@ mod tests { fn test_extend_selection_list() { do_check(r#"fn foo(<|>x: i32) {}"#, &["x", "x: i32"]); do_check(r#"fn foo(<|>x: i32, y: i32) {}"#, &["x", "x: i32", "x: i32, "]); - do_check(r#"fn foo(<|>x: i32,y: i32) {}"#, &["x", "x: i32", "x: i32,"]); + do_check(r#"fn foo(<|>x: i32,y: i32) {}"#, &["x", "x: i32", "x: i32,", "(x: i32,y: i32)"]); do_check(r#"fn foo(x: i32, <|>y: i32) {}"#, &["y", "y: i32", ", y: i32"]); - do_check(r#"fn foo(x: i32, <|>y: i32, ) {}"#, &["y", "y: i32", ", y: i32"]); + do_check(r#"fn foo(x: i32, <|>y: i32, ) {}"#, &["y", "y: i32", "y: i32, "]); do_check(r#"fn foo(x: i32,<|>y: i32) {}"#, &["y", "y: i32", ",y: i32"]); do_check(r#"const FOO: [usize; 2] = [ 22<|> , 33];"#, &["22", "22 , "]); do_check(r#"const FOO: [usize; 2] = [ 22 , 33<|>];"#, &["33", ", 33"]); - do_check(r#"const FOO: [usize; 2] = [ 22 , 33<|> ,];"#, &["33", ", 33"]); + do_check(r#"const FOO: [usize; 2] = [ 22 , 33<|> ,];"#, &["33", "33 ,", "[ 22 , 33 ,]"]); do_check(r#"fn main() { (1, 2<|>) }"#, &["2", ", 2", "(1, 2)"]); @@ -276,7 +278,7 @@ const FOO: [usize; 2] = [ 22 , 33<|>, ]"#, - &["33", ", 33"], + &["33", "33,"], ); } @@ -424,7 +426,7 @@ fn foo() do_check(r#"fn foo() where T: <|>Copy +Display"#, &["Copy", "Copy +"]); do_check(r#"fn foo() where T: <|>Copy+Display"#, &["Copy", "Copy+"]); do_check(r#"fn foo() where T: Copy + <|>Display"#, &["Display", "+ Display"]); - do_check(r#"fn foo() where T: Copy + <|>Display + Sync"#, &["Display", "+ Display"]); + do_check(r#"fn foo() where T: Copy + <|>Display + Sync"#, &["Display", "Display + "]); do_check(r#"fn foo() where T: Copy +<|>Display"#, &["Display", "+Display"]); } @@ -435,7 +437,7 @@ fn foo() do_check(r#"fn fooCopy +Display>() {}"#, &["Copy", "Copy +"]); do_check(r#"fn fooCopy+Display>() {}"#, &["Copy", "Copy+"]); do_check(r#"fn fooDisplay>() {}"#, &["Display", "+ Display"]); - do_check(r#"fn fooDisplay + Sync>() {}"#, &["Display", "+ Display"]); + do_check(r#"fn fooDisplay + Sync>() {}"#, &["Display", "Display + "]); do_check(r#"fn fooDisplay>() {}"#, &["Display", "+Display"]); do_check( r#"fn foo + Display, U: Copy>() {}"#, @@ -449,4 +451,56 @@ fn foo() ], ); } + + #[test] + fn test_extend_selection_on_tuple_in_type() { + do_check( + r#"fn main() { let _: (krate, <|>_crate_def_map, module_id) = (); }"#, + &["_crate_def_map", "_crate_def_map, ", "(krate, _crate_def_map, module_id)"], + ); + // white space variations + do_check( + r#"fn main() { let _: (krate,<|>_crate_def_map,module_id) = (); }"#, + &["_crate_def_map", "_crate_def_map,", "(krate,_crate_def_map,module_id)"], + ); + do_check( + r#" +fn main() { let _: ( + krate, + _crate<|>_def_map, + module_id +) = (); }"#, + &[ + "_crate_def_map", + "_crate_def_map,", + "(\n krate,\n _crate_def_map,\n module_id\n)", + ], + ); + } + + #[test] + fn test_extend_selection_on_tuple_in_rvalue() { + do_check( + r#"fn main() { let var = (krate, _crate_def_map<|>, module_id); }"#, + &["_crate_def_map", "_crate_def_map, ", "(krate, _crate_def_map, module_id)"], + ); + // white space variations + do_check( + r#"fn main() { let var = (krate,_crate<|>_def_map,module_id); }"#, + &["_crate_def_map", "_crate_def_map,", "(krate,_crate_def_map,module_id)"], + ); + do_check( + r#" +fn main() { let var = ( + krate, + _crate_def_map<|>, + module_id +); }"#, + &[ + "_crate_def_map", + "_crate_def_map,", + "(\n krate,\n _crate_def_map,\n module_id\n)", + ], + ); + } } -- cgit v1.2.3 From 47f2b5d0d9f8206f02924b305cde60404813e8b5 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Fri, 13 Dec 2019 02:39:14 +0800 Subject: Map first and last node in original_range --- crates/ra_ide/src/expand.rs | 53 ++++++++++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 17 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/expand.rs b/crates/ra_ide/src/expand.rs index 216d5cfec..661628ae4 100644 --- a/crates/ra_ide/src/expand.rs +++ b/crates/ra_ide/src/expand.rs @@ -3,7 +3,7 @@ use std::iter::successors; use hir::InFile; use ra_db::FileId; -use ra_syntax::{ast, AstNode, SyntaxNode, SyntaxToken}; +use ra_syntax::{ast, AstNode, SyntaxNode, SyntaxToken, TextRange}; use crate::{db::RootDatabase, FileRange}; @@ -17,26 +17,45 @@ pub(crate) fn original_range(db: &RootDatabase, node: InFile<&SyntaxNode>) -> Fi } Some(it) => it, }; - // FIXME: the following completely wrong. - // - // *First*, we should try to map first and last tokens of node, and, if that - // fails, return the range of the overall macro expansions. - // - // *Second*, we should handle recurside macro expansions - - let token = node - .value - .descendants_with_tokens() - .filter_map(|it| it.into_token()) - .find_map(|it| expansion.map_token_up(node.with_value(&it))); - - match token { - Some(it) => { - FileRange { file_id: it.file_id.original_file(db), range: it.value.text_range() } + // FIXME: We should handle recurside macro expansions + + let range = node.value.descendants_with_tokens().find_map(|it| { + match it.as_token() { + // FIXME: Remove this branch after all `tt::TokenTree`s have a proper `TokenId`, + // and return the range of the overall macro expansions if mapping first and last tokens fails. + Some(token) => { + let token = expansion.map_token_up(node.with_value(&token))?; + Some(token.with_value(token.value.text_range())) + } + None => { + // Try to map first and last tokens of node, and, if success, return the union range of mapped tokens + let n = it.into_node()?; + let first = expansion.map_token_up(node.with_value(&n.first_token()?))?; + let last = expansion.map_token_up(node.with_value(&n.last_token()?))?; + + // FIXME: Is is possible ? + if first.file_id != last.file_id { + return None; + } + + // FIXME: Add union method in TextRange + let range = union_range(first.value.text_range(), last.value.text_range()); + Some(first.with_value(range)) + } } + }); + + return match range { + Some(it) => FileRange { file_id: it.file_id.original_file(db), range: it.value }, None => { FileRange { file_id: node.file_id.original_file(db), range: node.value.text_range() } } + }; + + fn union_range(a: TextRange, b: TextRange) -> TextRange { + let start = a.start().min(b.start()); + let end = a.end().max(b.end()); + TextRange::from_to(start, end) } } -- cgit v1.2.3 From 30672c420ee19faa1acc15ecc84ae8d17c309e95 Mon Sep 17 00:00:00 2001 From: Omer Ben-Amram Date: Fri, 13 Dec 2019 19:00:55 +0200 Subject: scopes resolution is more granular --- crates/ra_ide/src/syntax_highlighting.rs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index 7ecb1a027..cd9d8b058 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -108,14 +108,17 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec highlight_name(db, name_kind), None => name.syntax().parent().map_or("function", |x| match x.kind() { - TYPE_PARAM | STRUCT_DEF | ENUM_DEF | TRAIT_DEF | TYPE_ALIAS_DEF => "type", + STRUCT_DEF | ENUM_DEF | TRAIT_DEF | TYPE_ALIAS_DEF => "type", + TYPE_PARAM => "type.param", RECORD_FIELD_DEF => "field", _ => "function", }), } } - INT_NUMBER | FLOAT_NUMBER | CHAR | BYTE => "literal", - LIFETIME => "parameter", + INT_NUMBER | FLOAT_NUMBER => "literal.numeric", + BYTE => "literal.byte", + CHAR => "literal.char", + LIFETIME => "lifetime", T![unsafe] => "keyword.unsafe", k if is_control_keyword(k) => "keyword.control", k if k.is_keyword() => "keyword", @@ -215,17 +218,18 @@ fn highlight_name(db: &RootDatabase, name_kind: NameKind) -> &'static str { Field(_) => "field", AssocItem(hir::AssocItem::Function(_)) => "function", AssocItem(hir::AssocItem::Const(_)) => "constant", - AssocItem(hir::AssocItem::TypeAlias(_)) => "type", + AssocItem(hir::AssocItem::TypeAlias(_)) => "type.alias", Def(hir::ModuleDef::Module(_)) => "module", Def(hir::ModuleDef::Function(_)) => "function", Def(hir::ModuleDef::Adt(_)) => "type", Def(hir::ModuleDef::EnumVariant(_)) => "constant", Def(hir::ModuleDef::Const(_)) => "constant", Def(hir::ModuleDef::Static(_)) => "constant", - Def(hir::ModuleDef::Trait(_)) => "type", - Def(hir::ModuleDef::TypeAlias(_)) => "type", - Def(hir::ModuleDef::BuiltinType(_)) => "type", - SelfType(_) | TypeParam(_) => "type", + Def(hir::ModuleDef::Trait(_)) => "type.trait", + Def(hir::ModuleDef::TypeAlias(_)) => "type.alias", + Def(hir::ModuleDef::BuiltinType(_)) => "type.builtin", + SelfType(_) => "type.self", + TypeParam(_) => "type.param", Local(local) => { if local.is_mut(db) { "variable.mut" -- cgit v1.2.3 From 4df741ecb2d00c3d8cdbc37d734ff5514dd5e7b0 Mon Sep 17 00:00:00 2001 From: succcubbus <16743652+succcubbus@users.noreply.github.com> Date: Fri, 13 Dec 2019 19:20:02 +0100 Subject: fix goto definition when inbetween tokens fixes both goto_definition and goto_type_definition. before, when running goto between some non-trivia token and an identifier, goto would be attempted for the non-trivia token. but this does not make sense for e.g. L_PAREN or COLONCOLON only for IDENTs. now only IDENTs will be searched for in goto actions. --- crates/ra_ide/src/goto_definition.rs | 16 ++++++++++++++-- crates/ra_ide/src/goto_type_definition.rs | 16 ++++++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/goto_definition.rs b/crates/ra_ide/src/goto_definition.rs index cfe62037f..96a73675f 100644 --- a/crates/ra_ide/src/goto_definition.rs +++ b/crates/ra_ide/src/goto_definition.rs @@ -3,7 +3,7 @@ use hir::{db::AstDatabase, InFile}; use ra_syntax::{ ast::{self, DocCommentsOwner}, - match_ast, AstNode, SyntaxNode, + match_ast, AstNode, SyntaxKind, SyntaxNode, }; use crate::{ @@ -20,7 +20,7 @@ pub(crate) fn goto_definition( ) -> Option>> { let file = db.parse_or_expand(position.file_id.into())?; let original_token = - file.token_at_offset(position.offset).filter(|it| !it.kind().is_trivia()).next()?; + file.token_at_offset(position.offset).find(|it| it.kind() == SyntaxKind::IDENT)?; let token = descend_into_macros(db, position.file_id, original_token.clone()); let nav_targets = match_ast! { @@ -234,6 +234,18 @@ mod tests { ); } + #[test] + fn goto_definition_works_at_start_of_item() { + check_goto( + " + //- /lib.rs + struct Foo; + enum E { X(<|>Foo) } + ", + "Foo STRUCT_DEF FileId(1) [0; 11) [7; 10)", + ); + } + #[test] fn goto_definition_resolves_correct_name() { check_goto( diff --git a/crates/ra_ide/src/goto_type_definition.rs b/crates/ra_ide/src/goto_type_definition.rs index 992a08809..cc1b90925 100644 --- a/crates/ra_ide/src/goto_type_definition.rs +++ b/crates/ra_ide/src/goto_type_definition.rs @@ -1,7 +1,7 @@ //! FIXME: write short doc here use hir::db::AstDatabase; -use ra_syntax::{ast, AstNode}; +use ra_syntax::{ast, AstNode, SyntaxKind}; use crate::{ db::RootDatabase, display::ToNav, expand::descend_into_macros, FilePosition, NavigationTarget, @@ -13,7 +13,7 @@ pub(crate) fn goto_type_definition( position: FilePosition, ) -> Option>> { let file = db.parse_or_expand(position.file_id.into())?; - let token = file.token_at_offset(position.offset).filter(|it| !it.kind().is_trivia()).next()?; + let token = file.token_at_offset(position.offset).find(|it| it.kind() == SyntaxKind::IDENT)?; let token = descend_into_macros(db, position.file_id, token); let node = token.value.ancestors().find_map(|token| { @@ -102,4 +102,16 @@ mod tests { "Foo STRUCT_DEF FileId(1) [52; 65) [59; 62)", ); } + + #[test] + fn goto_type_definition_works_param() { + check_goto( + " + //- /lib.rs + struct Foo; + fn foo(<|>f: Foo) {} + ", + "Foo STRUCT_DEF FileId(1) [0; 11) [7; 10)", + ); + } } -- cgit v1.2.3 From f54fef85aa88db44b1d3f6436551595dcb98ab83 Mon Sep 17 00:00:00 2001 From: succcubbus <16743652+succcubbus@users.noreply.github.com> Date: Fri, 13 Dec 2019 19:54:02 +0100 Subject: use find() instead of filter().next() --- crates/ra_ide/src/hover.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/hover.rs b/crates/ra_ide/src/hover.rs index d372ca758..24f7eeed3 100644 --- a/crates/ra_ide/src/hover.rs +++ b/crates/ra_ide/src/hover.rs @@ -156,7 +156,7 @@ fn hover_text_from_name_kind( pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option> { let file = db.parse_or_expand(position.file_id.into())?; - let token = file.token_at_offset(position.offset).filter(|it| !it.kind().is_trivia()).next()?; + let token = file.token_at_offset(position.offset).find(|it| !it.kind().is_trivia())?; let token = descend_into_macros(db, position.file_id, token); let mut res = HoverResult::new(); -- cgit v1.2.3 From 6c42eb1930f179bca1867eebcbf82c7bc10dd4c5 Mon Sep 17 00:00:00 2001 From: succcubbus <16743652+succcubbus@users.noreply.github.com> Date: Fri, 13 Dec 2019 19:54:07 +0100 Subject: add failing test --- crates/ra_ide/src/hover.rs | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/hover.rs b/crates/ra_ide/src/hover.rs index 24f7eeed3..b2909bf38 100644 --- a/crates/ra_ide/src/hover.rs +++ b/crates/ra_ide/src/hover.rs @@ -504,6 +504,13 @@ fn func(foo: i32) { if true { <|>foo; }; } assert_eq!(trim_markup_opt(hover.info.first()), Some("i32")); } + #[test] + fn hover_for_param_edge() { + let (analysis, position) = single_file_with_position("fn func(<|>foo: i32) {}"); + let hover = analysis.hover(position).unwrap().unwrap(); + assert_eq!(trim_markup_opt(hover.info.first()), Some("i32")); + } + #[test] fn test_type_of_for_function() { let (analysis, range) = single_file_with_range( -- cgit v1.2.3 From 6c133017a80c41db361d7870bf57db3e43c4074a Mon Sep 17 00:00:00 2001 From: succcubbus <16743652+succcubbus@users.noreply.github.com> Date: Fri, 13 Dec 2019 21:10:25 +0100 Subject: try both surrounding tokens for hover --- crates/ra_ide/src/hover.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/hover.rs b/crates/ra_ide/src/hover.rs index b2909bf38..5b48b1998 100644 --- a/crates/ra_ide/src/hover.rs +++ b/crates/ra_ide/src/hover.rs @@ -1,11 +1,11 @@ //! FIXME: write short doc here -use hir::{db::AstDatabase, Adt, HasSource, HirDisplay}; +use hir::{db::AstDatabase, Adt, HasSource, HirDisplay, InFile}; use ra_db::SourceDatabase; use ra_syntax::{ algo::find_covering_element, ast::{self, DocCommentsOwner}, - match_ast, AstNode, + match_ast, AstNode, SyntaxToken, }; use crate::{ @@ -156,9 +156,17 @@ fn hover_text_from_name_kind( pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option> { let file = db.parse_or_expand(position.file_id.into())?; - let token = file.token_at_offset(position.offset).find(|it| !it.kind().is_trivia())?; - let token = descend_into_macros(db, position.file_id, token); + file.token_at_offset(position.offset) + .filter(|token| !token.kind().is_trivia()) + .map(|token| descend_into_macros(db, position.file_id, token)) + .find_map(|token| hover_token(db, position, token)) +} +fn hover_token( + db: &RootDatabase, + position: FilePosition, + token: InFile, +) -> Option> { let mut res = HoverResult::new(); let mut range = match_ast! { -- cgit v1.2.3 From 4f7da04c6735e1d00fceb6fc6f83542626ce03c9 Mon Sep 17 00:00:00 2001 From: succcubbus <16743652+succcubbus@users.noreply.github.com> Date: Fri, 13 Dec 2019 21:59:25 +0100 Subject: add tests for goto on tuple fields --- crates/ra_ide/src/goto_definition.rs | 16 ++++++++++++++++ crates/ra_ide/src/goto_type_definition.rs | 18 +++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/goto_definition.rs b/crates/ra_ide/src/goto_definition.rs index 96a73675f..30118b43f 100644 --- a/crates/ra_ide/src/goto_definition.rs +++ b/crates/ra_ide/src/goto_definition.rs @@ -446,6 +446,22 @@ mod tests { ); } + #[test] + fn goto_for_tuple_fields() { + check_goto( + " + //- /lib.rs + struct Foo(u32); + + fn bar() { + let foo = Foo(0); + foo.<|>0; + } + ", + "TUPLE_FIELD_DEF FileId(1) [11; 14)", + ); + } + #[test] fn goto_definition_works_for_ufcs_inherent_methods() { check_goto( diff --git a/crates/ra_ide/src/goto_type_definition.rs b/crates/ra_ide/src/goto_type_definition.rs index cc1b90925..5501bb742 100644 --- a/crates/ra_ide/src/goto_type_definition.rs +++ b/crates/ra_ide/src/goto_type_definition.rs @@ -104,7 +104,7 @@ mod tests { } #[test] - fn goto_type_definition_works_param() { + fn goto_type_definition_for_param() { check_goto( " //- /lib.rs @@ -114,4 +114,20 @@ mod tests { "Foo STRUCT_DEF FileId(1) [0; 11) [7; 10)", ); } + + #[test] + fn goto_type_definition_for_tuple_field() { + check_goto( + " + //- /lib.rs + struct Foo; + struct Bar(Foo); + fn foo() { + let bar = Bar(Foo); + bar.<|>0; + } + ", + "Foo STRUCT_DEF FileId(1) [0; 11) [7; 10)", + ); + } } -- cgit v1.2.3 From c82529a97f10b1302d2944f1946bcb3479f64e2d Mon Sep 17 00:00:00 2001 From: succcubbus <16743652+succcubbus@users.noreply.github.com> Date: Fri, 13 Dec 2019 22:00:05 +0100 Subject: for goto and hover pick the token based on a priority --- crates/ra_ide/src/goto_definition.rs | 18 +++++++++++++++--- crates/ra_ide/src/goto_type_definition.rs | 15 +++++++++++++-- crates/ra_ide/src/hover.rs | 30 ++++++++++++++++++------------ 3 files changed, 46 insertions(+), 17 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/goto_definition.rs b/crates/ra_ide/src/goto_definition.rs index 30118b43f..27052d72b 100644 --- a/crates/ra_ide/src/goto_definition.rs +++ b/crates/ra_ide/src/goto_definition.rs @@ -3,7 +3,9 @@ use hir::{db::AstDatabase, InFile}; use ra_syntax::{ ast::{self, DocCommentsOwner}, - match_ast, AstNode, SyntaxKind, SyntaxNode, + match_ast, AstNode, + SyntaxKind::*, + SyntaxNode, SyntaxToken, TokenAtOffset, }; use crate::{ @@ -19,8 +21,7 @@ pub(crate) fn goto_definition( position: FilePosition, ) -> Option>> { let file = db.parse_or_expand(position.file_id.into())?; - let original_token = - file.token_at_offset(position.offset).find(|it| it.kind() == SyntaxKind::IDENT)?; + let original_token = pick_best(file.token_at_offset(position.offset))?; let token = descend_into_macros(db, position.file_id, original_token.clone()); let nav_targets = match_ast! { @@ -38,6 +39,17 @@ pub(crate) fn goto_definition( Some(RangeInfo::new(original_token.text_range(), nav_targets)) } +fn pick_best(tokens: TokenAtOffset) -> Option { + return tokens.max_by_key(priority); + fn priority(n: &SyntaxToken) -> usize { + match n.kind() { + IDENT | INT_NUMBER => 2, + kind if kind.is_trivia() => 0, + _ => 1, + } + } +} + #[derive(Debug)] pub(crate) enum ReferenceResult { Exact(NavigationTarget), diff --git a/crates/ra_ide/src/goto_type_definition.rs b/crates/ra_ide/src/goto_type_definition.rs index 5501bb742..ce8b6c72a 100644 --- a/crates/ra_ide/src/goto_type_definition.rs +++ b/crates/ra_ide/src/goto_type_definition.rs @@ -1,7 +1,7 @@ //! FIXME: write short doc here use hir::db::AstDatabase; -use ra_syntax::{ast, AstNode, SyntaxKind}; +use ra_syntax::{ast, AstNode, SyntaxKind::*, SyntaxToken, TokenAtOffset}; use crate::{ db::RootDatabase, display::ToNav, expand::descend_into_macros, FilePosition, NavigationTarget, @@ -13,7 +13,7 @@ pub(crate) fn goto_type_definition( position: FilePosition, ) -> Option>> { let file = db.parse_or_expand(position.file_id.into())?; - let token = file.token_at_offset(position.offset).find(|it| it.kind() == SyntaxKind::IDENT)?; + let token = pick_best(file.token_at_offset(position.offset))?; let token = descend_into_macros(db, position.file_id, token); let node = token.value.ancestors().find_map(|token| { @@ -41,6 +41,17 @@ pub(crate) fn goto_type_definition( Some(RangeInfo::new(node.text_range(), vec![nav])) } +fn pick_best(tokens: TokenAtOffset) -> Option { + return tokens.max_by_key(priority); + fn priority(n: &SyntaxToken) -> usize { + match n.kind() { + IDENT | INT_NUMBER => 2, + kind if kind.is_trivia() => 0, + _ => 1, + } + } +} + #[cfg(test)] mod tests { use crate::mock_analysis::analysis_and_position; diff --git a/crates/ra_ide/src/hover.rs b/crates/ra_ide/src/hover.rs index 5b48b1998..51e320128 100644 --- a/crates/ra_ide/src/hover.rs +++ b/crates/ra_ide/src/hover.rs @@ -1,11 +1,13 @@ //! FIXME: write short doc here -use hir::{db::AstDatabase, Adt, HasSource, HirDisplay, InFile}; +use hir::{db::AstDatabase, Adt, HasSource, HirDisplay}; use ra_db::SourceDatabase; use ra_syntax::{ algo::find_covering_element, ast::{self, DocCommentsOwner}, - match_ast, AstNode, SyntaxToken, + match_ast, AstNode, + SyntaxKind::*, + SyntaxToken, TokenAtOffset, }; use crate::{ @@ -156,17 +158,9 @@ fn hover_text_from_name_kind( pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option> { let file = db.parse_or_expand(position.file_id.into())?; - file.token_at_offset(position.offset) - .filter(|token| !token.kind().is_trivia()) - .map(|token| descend_into_macros(db, position.file_id, token)) - .find_map(|token| hover_token(db, position, token)) -} + let token = pick_best(file.token_at_offset(position.offset))?; + let token = descend_into_macros(db, position.file_id, token); -fn hover_token( - db: &RootDatabase, - position: FilePosition, - token: InFile, -) -> Option> { let mut res = HoverResult::new(); let mut range = match_ast! { @@ -226,6 +220,18 @@ fn hover_token( Some(RangeInfo::new(range, res)) } +fn pick_best(tokens: TokenAtOffset) -> Option { + return tokens.max_by_key(priority); + fn priority(n: &SyntaxToken) -> usize { + match n.kind() { + IDENT | INT_NUMBER => 3, + L_PAREN | R_PAREN => 2, + kind if kind.is_trivia() => 0, + _ => 1, + } + } +} + pub(crate) fn type_of(db: &RootDatabase, frange: FileRange) -> Option { let parse = db.parse(frange.file_id); let leaf_node = find_covering_element(parse.tree().syntax(), frange.range); -- cgit v1.2.3 From 67641d3f5fd1cfd49673c4eea9e3d646ed97e108 Mon Sep 17 00:00:00 2001 From: Omer Ben-Amram Date: Sat, 14 Dec 2019 13:24:07 +0200 Subject: added decorations --- crates/ra_ide/src/syntax_highlighting.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index cd9d8b058..7be25b234 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -225,8 +225,8 @@ fn highlight_name(db: &RootDatabase, name_kind: NameKind) -> &'static str { Def(hir::ModuleDef::EnumVariant(_)) => "constant", Def(hir::ModuleDef::Const(_)) => "constant", Def(hir::ModuleDef::Static(_)) => "constant", - Def(hir::ModuleDef::Trait(_)) => "type.trait", - Def(hir::ModuleDef::TypeAlias(_)) => "type.alias", + Def(hir::ModuleDef::Trait(_)) => "type", + Def(hir::ModuleDef::TypeAlias(_)) => "type", Def(hir::ModuleDef::BuiltinType(_)) => "type.builtin", SelfType(_) => "type.self", TypeParam(_) => "type.param", -- cgit v1.2.3 From 083010f6339e558184f06ce76a18e1ad0b0ee936 Mon Sep 17 00:00:00 2001 From: Omer Ben-Amram Date: Sat, 14 Dec 2019 13:29:42 +0200 Subject: removed `type.alias` --- crates/ra_ide/src/snapshots/highlighting.html | 26 ++++++++++++++------------ crates/ra_ide/src/syntax_highlighting.rs | 6 ++++-- 2 files changed, 18 insertions(+), 14 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/snapshots/highlighting.html b/crates/ra_ide/src/snapshots/highlighting.html index 4166a8f90..40605d9ef 100644 --- a/crates/ra_ide/src/snapshots/highlighting.html +++ b/crates/ra_ide/src/snapshots/highlighting.html @@ -10,8 +10,10 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .builtin { color: #DD6718; } .text { color: #DCDCCC; } .type { color: #7CB8BB; } +.type\.param { color: #20999D; } .attribute { color: #94BFF3; } .literal { color: #BFEBBF; } +.literal\.numeric { color: #6A8759; } .macro { color: #94BFF3; } .variable { color: #DCDCCC; } .variable\.mut { color: #DCDCCC; text-decoration: underline; } @@ -22,36 +24,36 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
#[derive(Clone, Debug)]
 struct Foo {
-    pub x: i32,
-    pub y: i32,
+    pub x: i32,
+    pub y: i32,
 }
 
-fn foo<T>() -> T {
+fn foo<T>() -> T {
     unimplemented!();
-    foo::<i32>();
+    foo::<i32>();
 }
 
 // comment
 fn main() {
-    println!("Hello, {}!", 92);
+    println!("Hello, {}!", 92);
 
     let mut vec = Vec::new();
     if true {
-        vec.push(Foo { x: 0, y: 1 });
+        vec.push(Foo { x: 0, y: 1 });
     }
-    unsafe { vec.set_len(0); }
+    unsafe { vec.set_len(0); }
 
-    let mut x = 42;
+    let mut x = 42;
     let y = &mut x;
     let z = &y;
 
     y;
 }
 
-enum E<X> {
-    V(X)
+enum E<X> {
+    V(X)
 }
 
-impl<X> E<X> {
-    fn new<T>() -> E<T> {}
+impl<X> E<X> {
+    fn new<T>() -> E<T> {}
 }
\ No newline at end of file diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index 7be25b234..235e09ffc 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -118,7 +118,7 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec "literal.numeric", BYTE => "literal.byte", CHAR => "literal.char", - LIFETIME => "lifetime", + LIFETIME => "type.lifetime", T![unsafe] => "keyword.unsafe", k if is_control_keyword(k) => "keyword.control", k if k.is_keyword() => "keyword", @@ -218,7 +218,7 @@ fn highlight_name(db: &RootDatabase, name_kind: NameKind) -> &'static str { Field(_) => "field", AssocItem(hir::AssocItem::Function(_)) => "function", AssocItem(hir::AssocItem::Const(_)) => "constant", - AssocItem(hir::AssocItem::TypeAlias(_)) => "type.alias", + AssocItem(hir::AssocItem::TypeAlias(_)) => "type", Def(hir::ModuleDef::Module(_)) => "module", Def(hir::ModuleDef::Function(_)) => "function", Def(hir::ModuleDef::Adt(_)) => "type", @@ -259,8 +259,10 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .builtin { color: #DD6718; } .text { color: #DCDCCC; } .type { color: #7CB8BB; } +.type\\.param { color: #20999D; } .attribute { color: #94BFF3; } .literal { color: #BFEBBF; } +.literal\\.numeric { color: #6A8759; } .macro { color: #94BFF3; } .variable { color: #DCDCCC; } .variable\\.mut { color: #DCDCCC; text-decoration: underline; } -- cgit v1.2.3 From 61360fdfec981eadef1eefb595c8b32c951771e8 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sun, 15 Dec 2019 01:20:07 +0800 Subject: Fix original_source find order --- crates/ra_ide/src/display/navigation_target.rs | 19 ++++++- crates/ra_ide/src/expand.rs | 79 ++++++++++++++------------ crates/ra_ide/src/goto_definition.rs | 34 ++++++++--- 3 files changed, 85 insertions(+), 47 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/display/navigation_target.rs b/crates/ra_ide/src/display/navigation_target.rs index 6a6b49afd..6a2bf7273 100644 --- a/crates/ra_ide/src/display/navigation_target.rs +++ b/crates/ra_ide/src/display/navigation_target.rs @@ -7,10 +7,14 @@ use ra_syntax::{ ast::{self, DocCommentsOwner, NameOwner}, match_ast, AstNode, SmolStr, SyntaxKind::{self, BIND_PAT, TYPE_PARAM}, - TextRange, + SyntaxNode, TextRange, }; -use crate::{db::RootDatabase, expand::original_range, FileSymbol}; +use crate::{ + db::RootDatabase, + expand::{original_range_by_kind, OriginalRangeKind}, + FileRange, FileSymbol, +}; use super::short_label::ShortLabel; @@ -416,3 +420,14 @@ pub(crate) fn description_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> } } } + +fn original_range(db: &RootDatabase, node: InFile<&SyntaxNode>) -> FileRange { + if let Some(range) = original_range_by_kind(db, node, OriginalRangeKind::CallToken) { + return range; + } + if let Some(range) = original_range_by_kind(db, node, OriginalRangeKind::WholeCall) { + return range; + } + + FileRange { file_id: node.file_id.original_file(db), range: node.value.text_range() } +} diff --git a/crates/ra_ide/src/expand.rs b/crates/ra_ide/src/expand.rs index 661628ae4..327393dbb 100644 --- a/crates/ra_ide/src/expand.rs +++ b/crates/ra_ide/src/expand.rs @@ -1,57 +1,62 @@ //! Utilities to work with files, produced by macros. use std::iter::successors; -use hir::InFile; +use hir::{ExpansionOrigin, InFile}; use ra_db::FileId; use ra_syntax::{ast, AstNode, SyntaxNode, SyntaxToken, TextRange}; use crate::{db::RootDatabase, FileRange}; -pub(crate) fn original_range(db: &RootDatabase, node: InFile<&SyntaxNode>) -> FileRange { - let expansion = match node.file_id.expansion_info(db) { - None => { - return FileRange { - file_id: node.file_id.original_file(db), - range: node.value.text_range(), - } - } - Some(it) => it, - }; +#[derive(Debug, PartialEq, Eq)] +pub(crate) enum OriginalRangeKind { + /// Return range if any token is matched + #[allow(dead_code)] + Any, + /// Return range if token is inside macro_call + CallToken, + /// Return whole macro call range if matched + WholeCall, +} + +pub(crate) fn original_range_by_kind( + db: &RootDatabase, + node: InFile<&SyntaxNode>, + kind: OriginalRangeKind, +) -> Option { + let expansion = node.file_id.expansion_info(db)?; + + // the input node has only one token ? + let single = node.value.first_token()? == node.value.last_token()?; + // FIXME: We should handle recurside macro expansions + let range = match kind { + OriginalRangeKind::WholeCall => expansion.call_node()?.map(|node| node.text_range()), + _ => node.value.descendants().find_map(|it| { + let first = it.first_token()?; + let last = it.last_token()?; - let range = node.value.descendants_with_tokens().find_map(|it| { - match it.as_token() { - // FIXME: Remove this branch after all `tt::TokenTree`s have a proper `TokenId`, - // and return the range of the overall macro expansions if mapping first and last tokens fails. - Some(token) => { - let token = expansion.map_token_up(node.with_value(&token))?; - Some(token.with_value(token.value.text_range())) + if !single && first == last { + return None; } - None => { - // Try to map first and last tokens of node, and, if success, return the union range of mapped tokens - let n = it.into_node()?; - let first = expansion.map_token_up(node.with_value(&n.first_token()?))?; - let last = expansion.map_token_up(node.with_value(&n.last_token()?))?; - // FIXME: Is is possible ? - if first.file_id != last.file_id { - return None; - } + // Try to map first and last tokens of node, and, if success, return the union range of mapped tokens + let (first, first_origin) = expansion.map_token_up(node.with_value(&first))?; + let (last, last_origin) = expansion.map_token_up(node.with_value(&last))?; - // FIXME: Add union method in TextRange - let range = union_range(first.value.text_range(), last.value.text_range()); - Some(first.with_value(range)) + if first.file_id != last.file_id + || first_origin != last_origin + || (kind == OriginalRangeKind::CallToken && first_origin != ExpansionOrigin::Call) + { + return None; } - } - }); - return match range { - Some(it) => FileRange { file_id: it.file_id.original_file(db), range: it.value }, - None => { - FileRange { file_id: node.file_id.original_file(db), range: node.value.text_range() } - } + // FIXME: Add union method in TextRange + Some(first.with_value(union_range(first.value.text_range(), last.value.text_range()))) + })?, }; + return Some(FileRange { file_id: range.file_id.original_file(db), range: range.value }); + fn union_range(a: TextRange, b: TextRange) -> TextRange { let start = a.start().min(b.start()); let end = a.end().max(b.end()); diff --git a/crates/ra_ide/src/goto_definition.rs b/crates/ra_ide/src/goto_definition.rs index cfe62037f..2c634990d 100644 --- a/crates/ra_ide/src/goto_definition.rs +++ b/crates/ra_ide/src/goto_definition.rs @@ -209,7 +209,7 @@ fn named_target(db: &RootDatabase, node: InFile<&SyntaxNode>) -> Option (fn $name() {}) } - define_fn!( - foo - ) + define_fn!(foo); fn bar() { <|>foo(); } ", - "foo FN_DEF FileId(1) [80; 83) [80; 83)", + "foo FN_DEF FileId(1) [64; 80) [75; 78)", + "define_fn!(foo);|foo", ); } #[test] fn goto_definition_works_for_macro_defined_fn_no_arg() { - check_goto( + check_goto_with_range_content( " //- /lib.rs macro_rules! define_fn { @@ -373,7 +390,8 @@ mod tests { <|>foo(); } ", - "foo FN_DEF FileId(1) [39; 42) [39; 42)", + "foo FN_DEF FileId(1) [51; 64) [51; 64)", + "define_fn!();|define_fn!();", ); } -- cgit v1.2.3 From b53587c7bdd67c63bd33a745fdaeb22a847b6c2f Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sun, 15 Dec 2019 01:46:39 +0800 Subject: Re-export Origin to replace ExpansionOrigin --- crates/ra_ide/src/expand.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/expand.rs b/crates/ra_ide/src/expand.rs index 327393dbb..258478bc1 100644 --- a/crates/ra_ide/src/expand.rs +++ b/crates/ra_ide/src/expand.rs @@ -1,7 +1,7 @@ //! Utilities to work with files, produced by macros. use std::iter::successors; -use hir::{ExpansionOrigin, InFile}; +use hir::{InFile, Origin}; use ra_db::FileId; use ra_syntax::{ast, AstNode, SyntaxNode, SyntaxToken, TextRange}; @@ -45,7 +45,7 @@ pub(crate) fn original_range_by_kind( if first.file_id != last.file_id || first_origin != last_origin - || (kind == OriginalRangeKind::CallToken && first_origin != ExpansionOrigin::Call) + || (kind == OriginalRangeKind::CallToken && first_origin != Origin::Call) { return None; } -- cgit v1.2.3 From 2619950b3b405324ab1c1745876165c834b3b4b9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 13 Dec 2019 12:12:36 +0100 Subject: Use different types for path with and without generics --- crates/ra_ide/src/completion/completion_context.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/completion/completion_context.rs b/crates/ra_ide/src/completion/completion_context.rs index ca0a483d4..981da2b79 100644 --- a/crates/ra_ide/src/completion/completion_context.rs +++ b/crates/ra_ide/src/completion/completion_context.rs @@ -188,10 +188,9 @@ impl<'a> CompletionContext<'a> { self.is_path_type = path.syntax().parent().and_then(ast::PathType::cast).is_some(); self.has_type_args = segment.type_arg_list().is_some(); - if let Some(mut path) = hir::Path::from_ast(path.clone()) { - if !path.is_ident() { - path.segments.pop().unwrap(); - self.path_prefix = Some(path); + if let Some(path) = hir::Path::from_ast(path.clone()) { + if let Some(path_prefix) = path.qualifier() { + self.path_prefix = Some(path_prefix); return; } } -- cgit v1.2.3 From 3ba4b3c554ee94cf96d62c57f9bb80eaff19beed Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sun, 15 Dec 2019 02:34:16 +0800 Subject: Use simpler logic on original_range --- crates/ra_ide/src/display/navigation_target.rs | 19 +----- crates/ra_ide/src/expand.rs | 83 ++++++++++++++------------ 2 files changed, 46 insertions(+), 56 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/display/navigation_target.rs b/crates/ra_ide/src/display/navigation_target.rs index 6a2bf7273..6a6b49afd 100644 --- a/crates/ra_ide/src/display/navigation_target.rs +++ b/crates/ra_ide/src/display/navigation_target.rs @@ -7,14 +7,10 @@ use ra_syntax::{ ast::{self, DocCommentsOwner, NameOwner}, match_ast, AstNode, SmolStr, SyntaxKind::{self, BIND_PAT, TYPE_PARAM}, - SyntaxNode, TextRange, + TextRange, }; -use crate::{ - db::RootDatabase, - expand::{original_range_by_kind, OriginalRangeKind}, - FileRange, FileSymbol, -}; +use crate::{db::RootDatabase, expand::original_range, FileSymbol}; use super::short_label::ShortLabel; @@ -420,14 +416,3 @@ pub(crate) fn description_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> } } } - -fn original_range(db: &RootDatabase, node: InFile<&SyntaxNode>) -> FileRange { - if let Some(range) = original_range_by_kind(db, node, OriginalRangeKind::CallToken) { - return range; - } - if let Some(range) = original_range_by_kind(db, node, OriginalRangeKind::WholeCall) { - return range; - } - - FileRange { file_id: node.file_id.original_file(db), range: node.value.text_range() } -} diff --git a/crates/ra_ide/src/expand.rs b/crates/ra_ide/src/expand.rs index 258478bc1..7a22bb0a4 100644 --- a/crates/ra_ide/src/expand.rs +++ b/crates/ra_ide/src/expand.rs @@ -7,55 +7,60 @@ use ra_syntax::{ast, AstNode, SyntaxNode, SyntaxToken, TextRange}; use crate::{db::RootDatabase, FileRange}; -#[derive(Debug, PartialEq, Eq)] -pub(crate) enum OriginalRangeKind { - /// Return range if any token is matched - #[allow(dead_code)] - Any, - /// Return range if token is inside macro_call - CallToken, - /// Return whole macro call range if matched - WholeCall, +pub(crate) fn original_range(db: &RootDatabase, node: InFile<&SyntaxNode>) -> FileRange { + if let Some((range, Origin::Call)) = original_range_and_origin(db, node) { + return range; + } + + if let Some(expansion) = node.file_id.expansion_info(db) { + if let Some(call_node) = expansion.call_node() { + return FileRange { + file_id: call_node.file_id.original_file(db), + range: call_node.value.text_range(), + }; + } + } + + FileRange { file_id: node.file_id.original_file(db), range: node.value.text_range() } } -pub(crate) fn original_range_by_kind( +fn original_range_and_origin( db: &RootDatabase, node: InFile<&SyntaxNode>, - kind: OriginalRangeKind, -) -> Option { +) -> Option<(FileRange, Origin)> { let expansion = node.file_id.expansion_info(db)?; // the input node has only one token ? let single = node.value.first_token()? == node.value.last_token()?; // FIXME: We should handle recurside macro expansions - let range = match kind { - OriginalRangeKind::WholeCall => expansion.call_node()?.map(|node| node.text_range()), - _ => node.value.descendants().find_map(|it| { - let first = it.first_token()?; - let last = it.last_token()?; - - if !single && first == last { - return None; - } - - // Try to map first and last tokens of node, and, if success, return the union range of mapped tokens - let (first, first_origin) = expansion.map_token_up(node.with_value(&first))?; - let (last, last_origin) = expansion.map_token_up(node.with_value(&last))?; - - if first.file_id != last.file_id - || first_origin != last_origin - || (kind == OriginalRangeKind::CallToken && first_origin != Origin::Call) - { - return None; - } - - // FIXME: Add union method in TextRange - Some(first.with_value(union_range(first.value.text_range(), last.value.text_range()))) - })?, - }; - - return Some(FileRange { file_id: range.file_id.original_file(db), range: range.value }); + let (range, origin) = node.value.descendants().find_map(|it| { + let first = it.first_token()?; + let last = it.last_token()?; + + if !single && first == last { + return None; + } + + // Try to map first and last tokens of node, and, if success, return the union range of mapped tokens + let (first, first_origin) = expansion.map_token_up(node.with_value(&first))?; + let (last, last_origin) = expansion.map_token_up(node.with_value(&last))?; + + if first.file_id != last.file_id || first_origin != last_origin { + return None; + } + + // FIXME: Add union method in TextRange + Some(( + first.with_value(union_range(first.value.text_range(), last.value.text_range())), + first_origin, + )) + })?; + + return Some(( + FileRange { file_id: range.file_id.original_file(db), range: range.value }, + origin, + )); fn union_range(a: TextRange, b: TextRange) -> TextRange { let start = a.start().min(b.start()); -- cgit v1.2.3 From feb5a4a8b8b0a30d71cf34c7927bd41cbae4e104 Mon Sep 17 00:00:00 2001 From: Omer Ben-Amram Date: Sun, 15 Dec 2019 10:20:22 +0200 Subject: fixed rainbow-highlighting test --- crates/ra_ide/src/snapshots/rainbow_highlighting.html | 2 ++ 1 file changed, 2 insertions(+) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/snapshots/rainbow_highlighting.html b/crates/ra_ide/src/snapshots/rainbow_highlighting.html index 9dfbc8047..ecf26c708 100644 --- a/crates/ra_ide/src/snapshots/rainbow_highlighting.html +++ b/crates/ra_ide/src/snapshots/rainbow_highlighting.html @@ -10,8 +10,10 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .builtin { color: #DD6718; } .text { color: #DCDCCC; } .type { color: #7CB8BB; } +.type\.param { color: #20999D; } .attribute { color: #94BFF3; } .literal { color: #BFEBBF; } +.literal\.numeric { color: #6A8759; } .macro { color: #94BFF3; } .variable { color: #DCDCCC; } .variable\.mut { color: #DCDCCC; text-decoration: underline; } -- cgit v1.2.3 From 50ecb1e19bc555aa627224d41a5ca243e44296f4 Mon Sep 17 00:00:00 2001 From: Omer Ben-Amram Date: Sun, 15 Dec 2019 12:33:14 +0200 Subject: introduce named constants for highlighting tag names. --- crates/ra_ide/src/syntax_highlighting.rs | 104 ++++++++++++++++++++----------- 1 file changed, 67 insertions(+), 37 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index 235e09ffc..d0cefea0f 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -16,6 +16,32 @@ use crate::{ FileId, }; +const HIGHLIGHT_TAG_FIELD: &'static str = "field"; +const HIGHLIGHT_TAG_FUNCTION: &'static str = "function"; +const HIGHLIGHT_TAG_MODULE: &'static str = "module"; +const HIGHLIGHT_TAG_TYPE: &'static str = "type"; +const HIGHLIGHT_TAG_CONSTANT: &'static str = "constant"; +const HIGHLIGHT_TAG_MACRO: &'static str = "macro"; +const HIGHLIGHT_TAG_VARIABLE: &'static str = "variable"; +const HIGHLIGHT_TAG_VARIABLE_MUT: &'static str = "variable.mut"; +const HIGHLIGHT_TAG_TEXT: &'static str = "text"; + +const HIGHLIGHT_TAG_TYPE_BUILTIN: &'static str = "type.builtin"; +const HIGHLIGHT_TAG_TYPE_SELF: &'static str = "type.self"; +const HIGHLIGHT_TAG_TYPE_PARAM: &'static str = "type.param"; +const HIGHLIGHT_TAG_TYPE_LIFETIME: &'static str = "type.lifetime"; + +const HIGHLIGHT_TAG_LITERAL_BYTE: &'static str = "literal.byte"; +const HIGHLIGHT_TAG_LITERAL_NUMERIC: &'static str = "literal.numeric"; +const HIGHLIGHT_TAG_LITERAL_CHAR: &'static str = "literal.char"; +const HIGHLIGHT_TAG_LITERAL_COMMENT: &'static str = "comment"; +const HIGHLIGHT_TAG_LITERAL_STRING: &'static str = "string"; +const HIGHLIGHT_TAG_LITERAL_ATTRIBUTE: &'static str = "attribute"; + +const HIGHLIGHT_TAG_KEYWORD_UNSAFE: &'static str = "keyword.unsafe"; +const HIGHLIGHT_TAG_KEYWORD_CONTROL: &'static str = "keyword.control"; +const HIGHLIGHT_TAG_KEYWORD: &'static str = "keyword"; + #[derive(Debug)] pub struct HighlightedRange { pub range: TextRange, @@ -71,9 +97,9 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec "comment", - STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => "string", - ATTR => "attribute", + COMMENT => HIGHLIGHT_TAG_LITERAL_COMMENT, + STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => HIGHLIGHT_TAG_LITERAL_STRING, + ATTR => HIGHLIGHT_TAG_LITERAL_ATTRIBUTE, NAME_REF => { if node.ancestors().any(|it| it.kind() == ATTR) { continue; @@ -90,7 +116,7 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec { let name = node.as_node().cloned().and_then(ast::Name::cast).unwrap(); @@ -107,21 +133,25 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec highlight_name(db, name_kind), - None => name.syntax().parent().map_or("function", |x| match x.kind() { - STRUCT_DEF | ENUM_DEF | TRAIT_DEF | TYPE_ALIAS_DEF => "type", - TYPE_PARAM => "type.param", - RECORD_FIELD_DEF => "field", - _ => "function", - }), + None => { + name.syntax().parent().map_or(HIGHLIGHT_TAG_FUNCTION, |x| match x.kind() { + STRUCT_DEF | ENUM_DEF | TRAIT_DEF | TYPE_ALIAS_DEF => { + HIGHLIGHT_TAG_TYPE + } + TYPE_PARAM => HIGHLIGHT_TAG_TYPE_PARAM, + RECORD_FIELD_DEF => HIGHLIGHT_TAG_FIELD, + _ => HIGHLIGHT_TAG_FUNCTION, + }) + } } } - INT_NUMBER | FLOAT_NUMBER => "literal.numeric", - BYTE => "literal.byte", - CHAR => "literal.char", - LIFETIME => "type.lifetime", - T![unsafe] => "keyword.unsafe", - k if is_control_keyword(k) => "keyword.control", - k if k.is_keyword() => "keyword", + INT_NUMBER | FLOAT_NUMBER => HIGHLIGHT_TAG_LITERAL_NUMERIC, + BYTE => HIGHLIGHT_TAG_LITERAL_BYTE, + CHAR => HIGHLIGHT_TAG_LITERAL_CHAR, + LIFETIME => HIGHLIGHT_TAG_TYPE_LIFETIME, + T![unsafe] => HIGHLIGHT_TAG_KEYWORD_UNSAFE, + k if is_control_keyword(k) => HIGHLIGHT_TAG_KEYWORD_CONTROL, + k if k.is_keyword() => HIGHLIGHT_TAG_KEYWORD, _ => { if let Some(macro_call) = node.as_node().cloned().and_then(ast::MacroCall::cast) { if let Some(path) = macro_call.path() { @@ -138,7 +168,7 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec &'static str { match name_kind { - Macro(_) => "macro", - Field(_) => "field", - AssocItem(hir::AssocItem::Function(_)) => "function", - AssocItem(hir::AssocItem::Const(_)) => "constant", - AssocItem(hir::AssocItem::TypeAlias(_)) => "type", - Def(hir::ModuleDef::Module(_)) => "module", - Def(hir::ModuleDef::Function(_)) => "function", - Def(hir::ModuleDef::Adt(_)) => "type", - Def(hir::ModuleDef::EnumVariant(_)) => "constant", - Def(hir::ModuleDef::Const(_)) => "constant", - Def(hir::ModuleDef::Static(_)) => "constant", - Def(hir::ModuleDef::Trait(_)) => "type", - Def(hir::ModuleDef::TypeAlias(_)) => "type", - Def(hir::ModuleDef::BuiltinType(_)) => "type.builtin", - SelfType(_) => "type.self", - TypeParam(_) => "type.param", + Macro(_) => HIGHLIGHT_TAG_MACRO, + Field(_) => HIGHLIGHT_TAG_FIELD, + AssocItem(hir::AssocItem::Function(_)) => HIGHLIGHT_TAG_FUNCTION, + AssocItem(hir::AssocItem::Const(_)) => HIGHLIGHT_TAG_CONSTANT, + AssocItem(hir::AssocItem::TypeAlias(_)) => HIGHLIGHT_TAG_TYPE, + Def(hir::ModuleDef::Module(_)) => HIGHLIGHT_TAG_MODULE, + Def(hir::ModuleDef::Function(_)) => HIGHLIGHT_TAG_FUNCTION, + Def(hir::ModuleDef::Adt(_)) => HIGHLIGHT_TAG_TYPE, + Def(hir::ModuleDef::EnumVariant(_)) => HIGHLIGHT_TAG_CONSTANT, + Def(hir::ModuleDef::Const(_)) => HIGHLIGHT_TAG_CONSTANT, + Def(hir::ModuleDef::Static(_)) => HIGHLIGHT_TAG_CONSTANT, + Def(hir::ModuleDef::Trait(_)) => HIGHLIGHT_TAG_TYPE, + Def(hir::ModuleDef::TypeAlias(_)) => HIGHLIGHT_TAG_TYPE, + Def(hir::ModuleDef::BuiltinType(_)) => HIGHLIGHT_TAG_TYPE_BUILTIN, + SelfType(_) => HIGHLIGHT_TAG_TYPE_SELF, + TypeParam(_) => HIGHLIGHT_TAG_TYPE_PARAM, Local(local) => { if local.is_mut(db) { - "variable.mut" + HIGHLIGHT_TAG_VARIABLE_MUT } else if local.ty(db).is_mutable_reference() { - "variable.mut" + HIGHLIGHT_TAG_VARIABLE_MUT } else { - "variable" + HIGHLIGHT_TAG_VARIABLE } } } -- cgit v1.2.3 From 9a6d496497df35c0dbd8fa40574d47d0463997dd Mon Sep 17 00:00:00 2001 From: Omer Ben-Amram Date: Sun, 15 Dec 2019 13:18:37 +0200 Subject: use a module instead of prefixed consts. --- crates/ra_ide/src/syntax_highlighting.rs | 130 +++++++++++++++---------------- 1 file changed, 64 insertions(+), 66 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index d0cefea0f..eb3dd1779 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -16,31 +16,33 @@ use crate::{ FileId, }; -const HIGHLIGHT_TAG_FIELD: &'static str = "field"; -const HIGHLIGHT_TAG_FUNCTION: &'static str = "function"; -const HIGHLIGHT_TAG_MODULE: &'static str = "module"; -const HIGHLIGHT_TAG_TYPE: &'static str = "type"; -const HIGHLIGHT_TAG_CONSTANT: &'static str = "constant"; -const HIGHLIGHT_TAG_MACRO: &'static str = "macro"; -const HIGHLIGHT_TAG_VARIABLE: &'static str = "variable"; -const HIGHLIGHT_TAG_VARIABLE_MUT: &'static str = "variable.mut"; -const HIGHLIGHT_TAG_TEXT: &'static str = "text"; - -const HIGHLIGHT_TAG_TYPE_BUILTIN: &'static str = "type.builtin"; -const HIGHLIGHT_TAG_TYPE_SELF: &'static str = "type.self"; -const HIGHLIGHT_TAG_TYPE_PARAM: &'static str = "type.param"; -const HIGHLIGHT_TAG_TYPE_LIFETIME: &'static str = "type.lifetime"; - -const HIGHLIGHT_TAG_LITERAL_BYTE: &'static str = "literal.byte"; -const HIGHLIGHT_TAG_LITERAL_NUMERIC: &'static str = "literal.numeric"; -const HIGHLIGHT_TAG_LITERAL_CHAR: &'static str = "literal.char"; -const HIGHLIGHT_TAG_LITERAL_COMMENT: &'static str = "comment"; -const HIGHLIGHT_TAG_LITERAL_STRING: &'static str = "string"; -const HIGHLIGHT_TAG_LITERAL_ATTRIBUTE: &'static str = "attribute"; - -const HIGHLIGHT_TAG_KEYWORD_UNSAFE: &'static str = "keyword.unsafe"; -const HIGHLIGHT_TAG_KEYWORD_CONTROL: &'static str = "keyword.control"; -const HIGHLIGHT_TAG_KEYWORD: &'static str = "keyword"; +pub mod tags { + pub(crate) const FIELD: &'static str = "field"; + pub(crate) const FUNCTION: &'static str = "function"; + pub(crate) const MODULE: &'static str = "module"; + pub(crate) const TYPE: &'static str = "type"; + pub(crate) const CONSTANT: &'static str = "constant"; + pub(crate) const MACRO: &'static str = "macro"; + pub(crate) const VARIABLE: &'static str = "variable"; + pub(crate) const VARIABLE_MUT: &'static str = "variable.mut"; + pub(crate) const TEXT: &'static str = "text"; + + pub(crate) const TYPE_BUILTIN: &'static str = "type.builtin"; + pub(crate) const TYPE_SELF: &'static str = "type.self"; + pub(crate) const TYPE_PARAM: &'static str = "type.param"; + pub(crate) const TYPE_LIFETIME: &'static str = "type.lifetime"; + + pub(crate) const LITERAL_BYTE: &'static str = "literal.byte"; + pub(crate) const LITERAL_NUMERIC: &'static str = "literal.numeric"; + pub(crate) const LITERAL_CHAR: &'static str = "literal.char"; + pub(crate) const LITERAL_COMMENT: &'static str = "comment"; + pub(crate) const LITERAL_STRING: &'static str = "string"; + pub(crate) const LITERAL_ATTRIBUTE: &'static str = "attribute"; + + pub(crate) const KEYWORD_UNSAFE: &'static str = "keyword.unsafe"; + pub(crate) const KEYWORD_CONTROL: &'static str = "keyword.control"; + pub(crate) const KEYWORD: &'static str = "keyword"; +} #[derive(Debug)] pub struct HighlightedRange { @@ -97,9 +99,9 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec HIGHLIGHT_TAG_LITERAL_COMMENT, - STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => HIGHLIGHT_TAG_LITERAL_STRING, - ATTR => HIGHLIGHT_TAG_LITERAL_ATTRIBUTE, + COMMENT => tags::LITERAL_COMMENT, + STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => tags::LITERAL_STRING, + ATTR => tags::LITERAL_ATTRIBUTE, NAME_REF => { if node.ancestors().any(|it| it.kind() == ATTR) { continue; @@ -116,7 +118,7 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec { let name = node.as_node().cloned().and_then(ast::Name::cast).unwrap(); @@ -133,25 +135,21 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec highlight_name(db, name_kind), - None => { - name.syntax().parent().map_or(HIGHLIGHT_TAG_FUNCTION, |x| match x.kind() { - STRUCT_DEF | ENUM_DEF | TRAIT_DEF | TYPE_ALIAS_DEF => { - HIGHLIGHT_TAG_TYPE - } - TYPE_PARAM => HIGHLIGHT_TAG_TYPE_PARAM, - RECORD_FIELD_DEF => HIGHLIGHT_TAG_FIELD, - _ => HIGHLIGHT_TAG_FUNCTION, - }) - } + None => name.syntax().parent().map_or(tags::FUNCTION, |x| match x.kind() { + STRUCT_DEF | ENUM_DEF | TRAIT_DEF | TYPE_ALIAS_DEF => tags::TYPE, + TYPE_PARAM => tags::TYPE_PARAM, + RECORD_FIELD_DEF => tags::FIELD, + _ => tags::FUNCTION, + }), } } - INT_NUMBER | FLOAT_NUMBER => HIGHLIGHT_TAG_LITERAL_NUMERIC, - BYTE => HIGHLIGHT_TAG_LITERAL_BYTE, - CHAR => HIGHLIGHT_TAG_LITERAL_CHAR, - LIFETIME => HIGHLIGHT_TAG_TYPE_LIFETIME, - T![unsafe] => HIGHLIGHT_TAG_KEYWORD_UNSAFE, - k if is_control_keyword(k) => HIGHLIGHT_TAG_KEYWORD_CONTROL, - k if k.is_keyword() => HIGHLIGHT_TAG_KEYWORD, + INT_NUMBER | FLOAT_NUMBER => tags::LITERAL_NUMERIC, + BYTE => tags::LITERAL_BYTE, + CHAR => tags::LITERAL_CHAR, + LIFETIME => tags::TYPE_LIFETIME, + T![unsafe] => tags::KEYWORD_UNSAFE, + k if is_control_keyword(k) => tags::KEYWORD_CONTROL, + k if k.is_keyword() => tags::KEYWORD, _ => { if let Some(macro_call) = node.as_node().cloned().and_then(ast::MacroCall::cast) { if let Some(path) = macro_call.path() { @@ -168,7 +166,7 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec &'static str { match name_kind { - Macro(_) => HIGHLIGHT_TAG_MACRO, - Field(_) => HIGHLIGHT_TAG_FIELD, - AssocItem(hir::AssocItem::Function(_)) => HIGHLIGHT_TAG_FUNCTION, - AssocItem(hir::AssocItem::Const(_)) => HIGHLIGHT_TAG_CONSTANT, - AssocItem(hir::AssocItem::TypeAlias(_)) => HIGHLIGHT_TAG_TYPE, - Def(hir::ModuleDef::Module(_)) => HIGHLIGHT_TAG_MODULE, - Def(hir::ModuleDef::Function(_)) => HIGHLIGHT_TAG_FUNCTION, - Def(hir::ModuleDef::Adt(_)) => HIGHLIGHT_TAG_TYPE, - Def(hir::ModuleDef::EnumVariant(_)) => HIGHLIGHT_TAG_CONSTANT, - Def(hir::ModuleDef::Const(_)) => HIGHLIGHT_TAG_CONSTANT, - Def(hir::ModuleDef::Static(_)) => HIGHLIGHT_TAG_CONSTANT, - Def(hir::ModuleDef::Trait(_)) => HIGHLIGHT_TAG_TYPE, - Def(hir::ModuleDef::TypeAlias(_)) => HIGHLIGHT_TAG_TYPE, - Def(hir::ModuleDef::BuiltinType(_)) => HIGHLIGHT_TAG_TYPE_BUILTIN, - SelfType(_) => HIGHLIGHT_TAG_TYPE_SELF, - TypeParam(_) => HIGHLIGHT_TAG_TYPE_PARAM, + Macro(_) => tags::MACRO, + Field(_) => tags::FIELD, + AssocItem(hir::AssocItem::Function(_)) => tags::FUNCTION, + AssocItem(hir::AssocItem::Const(_)) => tags::CONSTANT, + AssocItem(hir::AssocItem::TypeAlias(_)) => tags::TYPE, + Def(hir::ModuleDef::Module(_)) => tags::MODULE, + Def(hir::ModuleDef::Function(_)) => tags::FUNCTION, + Def(hir::ModuleDef::Adt(_)) => tags::TYPE, + Def(hir::ModuleDef::EnumVariant(_)) => tags::CONSTANT, + Def(hir::ModuleDef::Const(_)) => tags::CONSTANT, + Def(hir::ModuleDef::Static(_)) => tags::CONSTANT, + Def(hir::ModuleDef::Trait(_)) => tags::TYPE, + Def(hir::ModuleDef::TypeAlias(_)) => tags::TYPE, + Def(hir::ModuleDef::BuiltinType(_)) => tags::TYPE_BUILTIN, + SelfType(_) => tags::TYPE_SELF, + TypeParam(_) => tags::TYPE_PARAM, Local(local) => { if local.is_mut(db) { - HIGHLIGHT_TAG_VARIABLE_MUT + tags::VARIABLE_MUT } else if local.ty(db).is_mutable_reference() { - HIGHLIGHT_TAG_VARIABLE_MUT + tags::VARIABLE_MUT } else { - HIGHLIGHT_TAG_VARIABLE + tags::VARIABLE } } } -- cgit v1.2.3 From 1c8467e20aa8d481a4583a1c2cf40fad3d2ff53c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 17 Dec 2019 14:43:37 +0100 Subject: Fix highlighting token names --- crates/ra_ide/src/snapshots/highlighting.html | 2 +- crates/ra_ide/src/snapshots/rainbow_highlighting.html | 2 +- crates/ra_ide/src/syntax_highlighting.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/snapshots/highlighting.html b/crates/ra_ide/src/snapshots/highlighting.html index 40605d9ef..2157139f6 100644 --- a/crates/ra_ide/src/snapshots/highlighting.html +++ b/crates/ra_ide/src/snapshots/highlighting.html @@ -7,9 +7,9 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .string { color: #CC9393; } .function { color: #93E0E3; } .parameter { color: #94BFF3; } -.builtin { color: #DD6718; } .text { color: #DCDCCC; } .type { color: #7CB8BB; } +.type\.builtin { color: #8CD0D3; } .type\.param { color: #20999D; } .attribute { color: #94BFF3; } .literal { color: #BFEBBF; } diff --git a/crates/ra_ide/src/snapshots/rainbow_highlighting.html b/crates/ra_ide/src/snapshots/rainbow_highlighting.html index ecf26c708..871a52cf6 100644 --- a/crates/ra_ide/src/snapshots/rainbow_highlighting.html +++ b/crates/ra_ide/src/snapshots/rainbow_highlighting.html @@ -7,9 +7,9 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .string { color: #CC9393; } .function { color: #93E0E3; } .parameter { color: #94BFF3; } -.builtin { color: #DD6718; } .text { color: #DCDCCC; } .type { color: #7CB8BB; } +.type\.builtin { color: #8CD0D3; } .type\.param { color: #20999D; } .attribute { color: #94BFF3; } .literal { color: #BFEBBF; } diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index eb3dd1779..15e75709c 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -284,9 +284,9 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .string { color: #CC9393; } .function { color: #93E0E3; } .parameter { color: #94BFF3; } -.builtin { color: #DD6718; } .text { color: #DCDCCC; } .type { color: #7CB8BB; } +.type\\.builtin { color: #8CD0D3; } .type\\.param { color: #20999D; } .attribute { color: #94BFF3; } .literal { color: #BFEBBF; } -- cgit v1.2.3 From 7ec43ee07a26667bb1458990ec667f82670aff22 Mon Sep 17 00:00:00 2001 From: kjeremy Date: Mon, 18 Nov 2019 18:08:39 -0500 Subject: WIP: See through Macros for SignatureHelp Note: we meed to skip the trivia filter to make sure that `covers!(call_info_bad_offset)` succeeds otherwise we exit call_info too early. Also the test doesn't pass: `FnCallNode::with_node` always detects a MacroCall. --- crates/ra_ide/src/call_info.rs | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/call_info.rs b/crates/ra_ide/src/call_info.rs index b3c323d38..2da9c4e76 100644 --- a/crates/ra_ide/src/call_info.rs +++ b/crates/ra_ide/src/call_info.rs @@ -1,22 +1,22 @@ //! FIXME: write short doc here -use ra_db::SourceDatabase; +use hir::db::AstDatabase; use ra_syntax::{ - algo::ancestors_at_offset, ast::{self, ArgListOwner}, - match_ast, AstNode, SyntaxNode, TextUnit, + match_ast, AstNode, SyntaxNode, }; use test_utils::tested_by; -use crate::{db::RootDatabase, CallInfo, FilePosition, FunctionSignature}; +use crate::{db::RootDatabase, expand::descend_into_macros, CallInfo, FilePosition, FunctionSignature}; /// Computes parameter information for the given call expression. pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option { - let parse = db.parse(position.file_id); - let syntax = parse.tree().syntax().clone(); + let file = db.parse_or_expand(position.file_id.into())?; + let token = file.token_at_offset(position.offset).next()?; + let token = descend_into_macros(db, position.file_id, token); // Find the calling expression and it's NameRef - let calling_node = FnCallNode::with_node(&syntax, position.offset)?; + let calling_node = FnCallNode::with_node(&token.value.parent())?; let name_ref = calling_node.name_ref()?; let name_ref = hir::InFile::new(position.file_id.into(), name_ref.syntax()); @@ -93,8 +93,8 @@ enum FnCallNode { } impl FnCallNode { - fn with_node(syntax: &SyntaxNode, offset: TextUnit) -> Option { - ancestors_at_offset(syntax, offset).find_map(|node| { + fn with_node(syntax: &SyntaxNode) -> Option { + syntax.ancestors().find_map(|node| { match_ast! { match node { ast::CallExpr(it) => { Some(FnCallNode::CallExpr(it)) }, @@ -589,4 +589,25 @@ fn f() { assert_eq!(info.label(), "foo!()"); assert_eq!(info.doc().map(|it| it.into()), Some("empty macro".to_string())); } + + #[test] + fn fn_signature_for_call_in_macro() { + let info = call_info( + r#" + macro_rules! id { + ($($tt:tt)*) => { $($tt)* } + } + fn foo() { + + } + id! { + fn bar() { + foo(<|>); + } + } + "#, + ); + + assert_eq!(info.label(), "fn foo()"); + } } -- cgit v1.2.3 From cdc6af6bda50641a061608f433ec2cff53bf66d3 Mon Sep 17 00:00:00 2001 From: Jeremy Kolb Date: Wed, 18 Dec 2019 08:58:48 -0500 Subject: Pass test --- crates/ra_ide/src/call_info.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/call_info.rs b/crates/ra_ide/src/call_info.rs index 2da9c4e76..3495cfa6d 100644 --- a/crates/ra_ide/src/call_info.rs +++ b/crates/ra_ide/src/call_info.rs @@ -18,7 +18,7 @@ pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option Date: Wed, 18 Dec 2019 09:11:47 -0500 Subject: cargo fmt --- crates/ra_ide/src/call_info.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/call_info.rs b/crates/ra_ide/src/call_info.rs index 3495cfa6d..2c2b6fa48 100644 --- a/crates/ra_ide/src/call_info.rs +++ b/crates/ra_ide/src/call_info.rs @@ -7,7 +7,9 @@ use ra_syntax::{ }; use test_utils::tested_by; -use crate::{db::RootDatabase, expand::descend_into_macros, CallInfo, FilePosition, FunctionSignature}; +use crate::{ + db::RootDatabase, expand::descend_into_macros, CallInfo, FilePosition, FunctionSignature, +}; /// Computes parameter information for the given call expression. pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option { -- cgit v1.2.3 From 69c944a1e27212604ce85154fd40cff1d46ad6f3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 18 Dec 2019 14:52:58 +0100 Subject: Add blank lines for readability --- crates/ra_ide/src/goto_definition.rs | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/goto_definition.rs b/crates/ra_ide/src/goto_definition.rs index bee8e9df2..3b4d89e3e 100644 --- a/crates/ra_ide/src/goto_definition.rs +++ b/crates/ra_ide/src/goto_definition.rs @@ -285,8 +285,10 @@ mod tests { mod a; mod b; enum E { X(Foo<|>) } + //- /a.rs struct Foo; + //- /b.rs struct Foo; ", @@ -300,6 +302,7 @@ mod tests { " //- /lib.rs mod <|>foo; + //- /foo.rs // empty ", @@ -310,6 +313,7 @@ mod tests { " //- /lib.rs mod <|>foo; + //- /foo/mod.rs // empty ", -- cgit v1.2.3 From 46a299bceece6f8633d7c1518939efbb3a57fae3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 18 Dec 2019 15:33:36 +0100 Subject: Refactor goto tests to always specify texts --- crates/ra_ide/src/goto_definition.rs | 124 +++++++++++++++++++++-------------- 1 file changed, 74 insertions(+), 50 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/goto_definition.rs b/crates/ra_ide/src/goto_definition.rs index 3b4d89e3e..48757f170 100644 --- a/crates/ra_ide/src/goto_definition.rs +++ b/crates/ra_ide/src/goto_definition.rs @@ -225,30 +225,35 @@ mod tests { use crate::mock_analysis::analysis_and_position; - fn check_goto(fixture: &str, expected: &str) { + fn check_goto(fixture: &str, expected: &str, expected_range: &str) { let (analysis, pos) = analysis_and_position(fixture); let mut navs = analysis.goto_definition(pos).unwrap().unwrap().info; assert_eq!(navs.len(), 1); - let nav = navs.pop().unwrap(); - nav.assert_match(expected); - } - - fn check_goto_with_range_content(fixture: &str, expected: &str, expected_range: &str) { - let (analysis, pos) = analysis_and_position(fixture); - let mut navs = analysis.goto_definition(pos).unwrap().unwrap().info; - assert_eq!(navs.len(), 1); let nav = navs.pop().unwrap(); - let file_text = analysis.file_text(pos.file_id).unwrap(); + let file_text = analysis.file_text(nav.file_id()).unwrap(); - let actual_full_range = &file_text[nav.full_range()]; - let actual_range = &file_text[nav.range()]; + let mut actual = file_text[nav.full_range()].to_string(); + if let Some(focus) = nav.focus_range() { + actual += "|"; + actual += &file_text[focus]; + } + + if !expected_range.contains("...") { + test_utils::assert_eq_text!(&actual, expected_range); + } else { + let mut parts = expected_range.split("..."); + let prefix = parts.next().unwrap(); + let suffix = parts.next().unwrap(); + assert!( + actual.starts_with(prefix) && actual.ends_with(suffix), + "\nExpected: {}\n Actual: {}\n", + expected_range, + actual + ); + } - test_utils::assert_eq_text!( - &format!("{}|{}", actual_full_range, actual_range), - expected_range - ); nav.assert_match(expected); } @@ -261,6 +266,7 @@ mod tests { enum E { X(Foo<|>) } ", "Foo STRUCT_DEF FileId(1) [0; 11) [7; 10)", + "struct Foo;|Foo", ); } @@ -273,6 +279,7 @@ mod tests { enum E { X(<|>Foo) } ", "Foo STRUCT_DEF FileId(1) [0; 11) [7; 10)", + "struct Foo;|Foo", ); } @@ -293,6 +300,7 @@ mod tests { struct Foo; ", "Foo STRUCT_DEF FileId(2) [0; 11) [7; 10)", + "struct Foo;|Foo", ); } @@ -307,6 +315,7 @@ mod tests { // empty ", "foo SOURCE_FILE FileId(2) [0; 10)", + "// empty\n\n", ); check_goto( @@ -318,6 +327,7 @@ mod tests { // empty ", "foo SOURCE_FILE FileId(2) [0; 10)", + "// empty\n\n", ); } @@ -327,17 +337,14 @@ mod tests { check_goto( " //- /lib.rs - macro_rules! foo { - () => { - {} - }; - } + macro_rules! foo { () => { () } } fn bar() { <|>foo!(); } ", - "foo MACRO_CALL FileId(1) [0; 50) [13; 16)", + "foo MACRO_CALL FileId(1) [0; 33) [13; 16)", + "macro_rules! foo { () => { () } }|foo", ); } @@ -354,13 +361,10 @@ mod tests { //- /foo/lib.rs #[macro_export] - macro_rules! foo { - () => { - {} - }; - } + macro_rules! foo { () => { () } } ", - "foo MACRO_CALL FileId(2) [0; 66) [29; 32)", + "foo MACRO_CALL FileId(2) [0; 49) [29; 32)", + "#[macro_export]\nmacro_rules! foo { () => { () } }|foo", ); } @@ -373,19 +377,16 @@ mod tests { //- /foo/lib.rs #[macro_export] - macro_rules! foo { - () => { - {} - }; - } + macro_rules! foo { () => { () } } ", - "foo MACRO_CALL FileId(2) [0; 66) [29; 32)", + "foo MACRO_CALL FileId(2) [0; 49) [29; 32)", + "#[macro_export]\nmacro_rules! foo { () => { () } }|foo", ); } #[test] fn goto_definition_works_for_macro_defined_fn_with_arg() { - check_goto_with_range_content( + check_goto( " //- /lib.rs macro_rules! define_fn { @@ -405,7 +406,7 @@ mod tests { #[test] fn goto_definition_works_for_macro_defined_fn_no_arg() { - check_goto_with_range_content( + check_goto( " //- /lib.rs macro_rules! define_fn { @@ -431,14 +432,15 @@ mod tests { //- /lib.rs struct Foo; impl Foo { - fn frobnicate(&self) { } + fn frobnicate(&self) { } } fn bar(foo: &Foo) { foo.frobnicate<|>(); } ", - "frobnicate FN_DEF FileId(1) [27; 52) [30; 40)", + "frobnicate FN_DEF FileId(1) [27; 51) [30; 40)", + "fn frobnicate(&self) { }|frobnicate", ); } @@ -457,6 +459,7 @@ mod tests { } ", "spam RECORD_FIELD_DEF FileId(1) [17; 26) [17; 21)", + "spam: u32|spam", ); } @@ -477,6 +480,7 @@ mod tests { } ", "spam RECORD_FIELD_DEF FileId(1) [17; 26) [17; 21)", + "spam: u32|spam", ); } @@ -493,6 +497,7 @@ mod tests { } ", "TUPLE_FIELD_DEF FileId(1) [11; 14)", + "u32", ); } @@ -503,14 +508,15 @@ mod tests { //- /lib.rs struct Foo; impl Foo { - fn frobnicate() { } + fn frobnicate() { } } fn bar(foo: &Foo) { Foo::frobnicate<|>(); } ", - "frobnicate FN_DEF FileId(1) [27; 47) [30; 40)", + "frobnicate FN_DEF FileId(1) [27; 46) [30; 40)", + "fn frobnicate() { }|frobnicate", ); } @@ -528,6 +534,7 @@ mod tests { } ", "frobnicate FN_DEF FileId(1) [16; 32) [19; 29)", + "fn frobnicate();|frobnicate", ); } @@ -547,6 +554,7 @@ mod tests { } ", "frobnicate FN_DEF FileId(1) [30; 46) [33; 43)", + "fn frobnicate();|frobnicate", ); } @@ -563,6 +571,7 @@ mod tests { } ", "impl IMPL_BLOCK FileId(1) [12; 73)", + "impl Foo {...}", ); check_goto( @@ -576,6 +585,7 @@ mod tests { } ", "impl IMPL_BLOCK FileId(1) [12; 73)", + "impl Foo {...}", ); check_goto( @@ -589,6 +599,7 @@ mod tests { } ", "impl IMPL_BLOCK FileId(1) [15; 75)", + "impl Foo {...}", ); check_goto( @@ -601,6 +612,7 @@ mod tests { } ", "impl IMPL_BLOCK FileId(1) [15; 62)", + "impl Foo {...}", ); } @@ -620,6 +632,7 @@ mod tests { } ", "impl IMPL_BLOCK FileId(1) [49; 115)", + "impl Make for Foo {...}", ); check_goto( @@ -636,6 +649,7 @@ mod tests { } ", "impl IMPL_BLOCK FileId(1) [49; 115)", + "impl Make for Foo {...}", ); } @@ -647,6 +661,7 @@ mod tests { struct Foo<|> { value: u32 } ", "Foo STRUCT_DEF FileId(1) [0; 25) [7; 10)", + "struct Foo { value: u32 }|Foo", ); check_goto( @@ -657,15 +672,16 @@ mod tests { } "#, "field RECORD_FIELD_DEF FileId(1) [17; 30) [17; 22)", + "field: string|field", ); check_goto( " //- /lib.rs - fn foo_test<|>() { - } + fn foo_test<|>() { } ", "foo_test FN_DEF FileId(1) [0; 17) [3; 11)", + "fn foo_test() { }|foo_test", ); check_goto( @@ -676,6 +692,7 @@ mod tests { } ", "Foo ENUM_DEF FileId(1) [0; 25) [5; 8)", + "enum Foo {...}|Foo", ); check_goto( @@ -688,22 +705,25 @@ mod tests { } ", "Variant2 ENUM_VARIANT FileId(1) [29; 37) [29; 37)", + "Variant2|Variant2", ); check_goto( r#" //- /lib.rs - static inner<|>: &str = ""; + static INNER<|>: &str = ""; "#, - "inner STATIC_DEF FileId(1) [0; 24) [7; 12)", + "INNER STATIC_DEF FileId(1) [0; 24) [7; 12)", + "static INNER: &str = \"\";|INNER", ); check_goto( r#" //- /lib.rs - const inner<|>: &str = ""; + const INNER<|>: &str = ""; "#, - "inner CONST_DEF FileId(1) [0; 23) [6; 11)", + "INNER CONST_DEF FileId(1) [0; 23) [6; 11)", + "const INNER: &str = \"\";|INNER", ); check_goto( @@ -712,24 +732,25 @@ mod tests { type Thing<|> = Option<()>; "#, "Thing TYPE_ALIAS_DEF FileId(1) [0; 24) [5; 10)", + "type Thing = Option<()>;|Thing", ); check_goto( r#" //- /lib.rs - trait Foo<|> { - } + trait Foo<|> { } "#, "Foo TRAIT_DEF FileId(1) [0; 13) [6; 9)", + "trait Foo { }|Foo", ); check_goto( r#" //- /lib.rs - mod bar<|> { - } + mod bar<|> { } "#, "bar MODULE FileId(1) [0; 11) [4; 7)", + "mod bar { }|bar", ); } @@ -750,6 +771,7 @@ mod tests { mod confuse_index { fn foo(); } ", "foo FN_DEF FileId(1) [52; 63) [55; 58)", + "fn foo() {}|foo", ); } @@ -778,6 +800,7 @@ mod tests { } ", "foo FN_DEF FileId(1) [398; 415) [401; 404)", + "fn foo() -> i8 {}|foo", ); } @@ -791,6 +814,7 @@ mod tests { } ", "T TYPE_PARAM FileId(1) [11; 12)", + "T", ); } } -- cgit v1.2.3 From 7c25224f0522cb828c4aa2d791562b84ee2995f9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 18 Dec 2019 16:25:15 +0100 Subject: Don't bother with focus range for navigation to locals --- crates/ra_ide/src/display/navigation_target.rs | 15 +++++----- crates/ra_ide/src/goto_definition.rs | 41 ++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 7 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/display/navigation_target.rs b/crates/ra_ide/src/display/navigation_target.rs index 6a6b49afd..b9ae67828 100644 --- a/crates/ra_ide/src/display/navigation_target.rs +++ b/crates/ra_ide/src/display/navigation_target.rs @@ -328,22 +328,23 @@ impl ToNav for hir::AssocItem { impl ToNav for hir::Local { fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { let src = self.source(db); - let (full_range, focus_range) = match src.value { - Either::Left(it) => { - (it.syntax().text_range(), it.name().map(|it| it.syntax().text_range())) + let node = match &src.value { + Either::Left(bind_pat) => { + bind_pat.name().map_or_else(|| bind_pat.syntax().clone(), |it| it.syntax().clone()) } - Either::Right(it) => (it.syntax().text_range(), Some(it.self_kw_token().text_range())), + Either::Right(it) => it.syntax().clone(), }; + let full_range = original_range(db, src.with_value(&node)); let name = match self.name(db) { Some(it) => it.to_string().into(), None => "".into(), }; NavigationTarget { - file_id: src.file_id.original_file(db), + file_id: full_range.file_id, name, kind: BIND_PAT, - full_range, - focus_range, + full_range: full_range.range, + focus_range: None, container_name: None, description: None, docs: None, diff --git a/crates/ra_ide/src/goto_definition.rs b/crates/ra_ide/src/goto_definition.rs index 48757f170..184555792 100644 --- a/crates/ra_ide/src/goto_definition.rs +++ b/crates/ra_ide/src/goto_definition.rs @@ -817,4 +817,45 @@ mod tests { "T", ); } + + #[test] + fn goto_within_macro() { + check_goto( + " + //- /lib.rs + macro_rules! id { + ($($tt:tt)*) => ($($tt)*) + } + + fn foo() { + let x = 1; + id!({ + let y = <|>x; + let z = y; + }); + } + ", + "x BIND_PAT FileId(1) [69; 70)", + "x", + ); + + check_goto( + " + //- /lib.rs + macro_rules! id { + ($($tt:tt)*) => ($($tt)*) + } + + fn foo() { + let x = 1; + id!({ + let y = x; + let z = <|>y; + }); + } + ", + "y BIND_PAT FileId(1) [98; 99)", + "y", + ); + } } -- cgit v1.2.3 From 14c167a9f6da07024a5101ffa04bc2f79ce64353 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Sun, 8 Dec 2019 00:54:18 +0200 Subject: Omit default parameter types --- crates/ra_ide/src/inlay_hints.rs | 90 +++++++++++++++++++++++++++++++--------- crates/ra_ide/src/lib.rs | 7 +++- 2 files changed, 77 insertions(+), 20 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/inlay_hints.rs b/crates/ra_ide/src/inlay_hints.rs index 3730121af..8674912a6 100644 --- a/crates/ra_ide/src/inlay_hints.rs +++ b/crates/ra_ide/src/inlay_hints.rs @@ -1,7 +1,7 @@ //! FIXME: write short doc here use crate::{db::RootDatabase, FileId}; -use hir::{HirDisplay, SourceAnalyzer}; +use hir::{HirDisplay, SourceAnalyzer, TruncateOptions}; use ra_syntax::{ ast::{self, AstNode, TypeAscriptionOwner}, match_ast, SmolStr, SourceFile, SyntaxKind, SyntaxNode, TextRange, @@ -23,11 +23,11 @@ pub(crate) fn inlay_hints( db: &RootDatabase, file_id: FileId, file: &SourceFile, - max_inlay_hint_length: Option, + truncate_options: &TruncateOptions, ) -> Vec { file.syntax() .descendants() - .map(|node| get_inlay_hints(db, file_id, &node, max_inlay_hint_length).unwrap_or_default()) + .map(|node| get_inlay_hints(db, file_id, &node, truncate_options).unwrap_or_default()) .flatten() .collect() } @@ -36,7 +36,7 @@ fn get_inlay_hints( db: &RootDatabase, file_id: FileId, node: &SyntaxNode, - max_inlay_hint_length: Option, + truncate_options: &TruncateOptions, ) -> Option> { let analyzer = SourceAnalyzer::new(db, hir::InFile::new(file_id.into(), node), None); match_ast! { @@ -46,7 +46,7 @@ fn get_inlay_hints( return None; } let pat = it.pat()?; - Some(get_pat_type_hints(db, &analyzer, pat, false, max_inlay_hint_length)) + Some(get_pat_type_hints(db, &analyzer, pat, false, truncate_options)) }, ast::LambdaExpr(it) => { it.param_list().map(|param_list| { @@ -54,22 +54,22 @@ fn get_inlay_hints( .params() .filter(|closure_param| closure_param.ascribed_type().is_none()) .filter_map(|closure_param| closure_param.pat()) - .map(|root_pat| get_pat_type_hints(db, &analyzer, root_pat, false, max_inlay_hint_length)) + .map(|root_pat| get_pat_type_hints(db, &analyzer, root_pat, false, truncate_options)) .flatten() .collect() }) }, ast::ForExpr(it) => { let pat = it.pat()?; - Some(get_pat_type_hints(db, &analyzer, pat, false, max_inlay_hint_length)) + Some(get_pat_type_hints(db, &analyzer, pat, false, truncate_options)) }, ast::IfExpr(it) => { let pat = it.condition()?.pat()?; - Some(get_pat_type_hints(db, &analyzer, pat, true, max_inlay_hint_length)) + Some(get_pat_type_hints(db, &analyzer, pat, true, truncate_options)) }, ast::WhileExpr(it) => { let pat = it.condition()?.pat()?; - Some(get_pat_type_hints(db, &analyzer, pat, true, max_inlay_hint_length)) + Some(get_pat_type_hints(db, &analyzer, pat, true, truncate_options)) }, ast::MatchArmList(it) => { Some( @@ -77,7 +77,7 @@ fn get_inlay_hints( .arms() .map(|match_arm| match_arm.pats()) .flatten() - .map(|root_pat| get_pat_type_hints(db, &analyzer, root_pat, true, max_inlay_hint_length)) + .map(|root_pat| get_pat_type_hints(db, &analyzer, root_pat, true, truncate_options)) .flatten() .collect(), ) @@ -92,7 +92,7 @@ fn get_pat_type_hints( analyzer: &SourceAnalyzer, root_pat: ast::Pat, skip_root_pat_hint: bool, - max_inlay_hint_length: Option, + truncate_options: &TruncateOptions, ) -> Vec { let original_pat = &root_pat.clone(); @@ -109,7 +109,7 @@ fn get_pat_type_hints( .map(|(range, pat_type)| InlayHint { range, kind: InlayKind::TypeHint, - label: pat_type.display_truncated(db, max_inlay_hint_length).to_string().into(), + label: pat_type.display_truncated(db, truncate_options).to_string().into(), }) .collect() } @@ -159,6 +159,58 @@ mod tests { use crate::mock_analysis::single_file; + #[test] + fn default_generic_types_disabled() { + let (analysis, file_id) = single_file( + r#" +struct Test { +k: K, + t: T, +} + +fn main() { + let zz = Test { t: 23, k: 33 }; +}"#, + ); + + assert_debug_snapshot!(analysis.inlay_hints(file_id, None, false).unwrap(), @r###" + [ + InlayHint { + range: [65; 67), + kind: TypeHint, + label: "Test", + }, + ] + "### + ); + } + + #[test] + fn default_generic_types_enabled() { + let (analysis, file_id) = single_file( + r#" +struct Test { + k: K, + t: T, +} + +fn main() { + let zz = Test { t: 23, k: 33 }; +}"#, + ); + + assert_debug_snapshot!(analysis.inlay_hints(file_id, None, true).unwrap(), @r###" + [ + InlayHint { + range: [69; 71), + kind: TypeHint, + label: "Test", + }, + ] + "### + ); + } + #[test] fn let_statement() { let (analysis, file_id) = single_file( @@ -199,7 +251,7 @@ fn main() { }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, None, true).unwrap(), @r###" [ InlayHint { range: [193; 197), @@ -273,7 +325,7 @@ fn main() { }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, None, true).unwrap(), @r###" [ InlayHint { range: [21; 30), @@ -302,7 +354,7 @@ fn main() { }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, None, true).unwrap(), @r###" [ InlayHint { range: [21; 30), @@ -350,7 +402,7 @@ fn main() { }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, None, true).unwrap(), @r###" [ InlayHint { range: [166; 170), @@ -413,7 +465,7 @@ fn main() { }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, None, true).unwrap(), @r###" [ InlayHint { range: [166; 170), @@ -476,7 +528,7 @@ fn main() { }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, None, true).unwrap(), @r###" [ InlayHint { range: [311; 315), @@ -518,7 +570,7 @@ fn main() { }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, Some(8)).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, Some(8), true).unwrap(), @r###" [ InlayHint { range: [74; 75), diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs index 779a81b2c..c3244a8dd 100644 --- a/crates/ra_ide/src/lib.rs +++ b/crates/ra_ide/src/lib.rs @@ -348,9 +348,14 @@ impl Analysis { &self, file_id: FileId, max_inlay_hint_length: Option, + show_default_types_in_inlay_hints: bool, ) -> Cancelable> { + let truncate_options = hir::TruncateOptions { + max_length: max_inlay_hint_length, + show_default_types: show_default_types_in_inlay_hints, + }; self.with_db(|db| { - inlay_hints::inlay_hints(db, file_id, &db.parse(file_id).tree(), max_inlay_hint_length) + inlay_hints::inlay_hints(db, file_id, &db.parse(file_id).tree(), &truncate_options) }) } -- cgit v1.2.3 From 3969c7c85373554fcd80aee359cd0def14f7a528 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Thu, 19 Dec 2019 12:45:00 +0200 Subject: Ensure hover shows full type declaration --- crates/ra_ide/src/hover.rs | 17 +++++++++++++++++ crates/ra_ide/src/inlay_hints.rs | 4 ++-- 2 files changed, 19 insertions(+), 2 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/hover.rs b/crates/ra_ide/src/hover.rs index 51e320128..7d2f160e7 100644 --- a/crates/ra_ide/src/hover.rs +++ b/crates/ra_ide/src/hover.rs @@ -424,6 +424,23 @@ mod tests { ); } + #[test] + fn hover_default_generic_type() { + check_hover_result( + r#" +//- /main.rs +struct Test { + k: K, + t: T, +} + +fn main() { + let zz<|> = Test { t: 23, k: 33 }; +}"#, + &["Test"], + ); + } + #[test] fn hover_some() { let (analysis, position) = single_file_with_position( diff --git a/crates/ra_ide/src/inlay_hints.rs b/crates/ra_ide/src/inlay_hints.rs index 8674912a6..319ac0048 100644 --- a/crates/ra_ide/src/inlay_hints.rs +++ b/crates/ra_ide/src/inlay_hints.rs @@ -164,7 +164,7 @@ mod tests { let (analysis, file_id) = single_file( r#" struct Test { -k: K, + k: K, t: T, } @@ -176,7 +176,7 @@ fn main() { assert_debug_snapshot!(analysis.inlay_hints(file_id, None, false).unwrap(), @r###" [ InlayHint { - range: [65; 67), + range: [69; 71), kind: TypeHint, label: "Test", }, -- cgit v1.2.3 From 4fb25ef43bd96da94467ffa4de8fbf0af82b28d1 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Thu, 19 Dec 2019 16:18:09 +0200 Subject: Do not add any new configuration parameters --- crates/ra_ide/src/inlay_hints.rs | 44 ++++++++-------------------------------- crates/ra_ide/src/lib.rs | 7 ++----- 2 files changed, 11 insertions(+), 40 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/inlay_hints.rs b/crates/ra_ide/src/inlay_hints.rs index 319ac0048..4c5004f67 100644 --- a/crates/ra_ide/src/inlay_hints.rs +++ b/crates/ra_ide/src/inlay_hints.rs @@ -160,7 +160,7 @@ mod tests { use crate::mock_analysis::single_file; #[test] - fn default_generic_types_disabled() { + fn default_generic_types_should_not_be_displayed() { let (analysis, file_id) = single_file( r#" struct Test { @@ -173,7 +173,7 @@ fn main() { }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, None, false).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" [ InlayHint { range: [69; 71), @@ -185,32 +185,6 @@ fn main() { ); } - #[test] - fn default_generic_types_enabled() { - let (analysis, file_id) = single_file( - r#" -struct Test { - k: K, - t: T, -} - -fn main() { - let zz = Test { t: 23, k: 33 }; -}"#, - ); - - assert_debug_snapshot!(analysis.inlay_hints(file_id, None, true).unwrap(), @r###" - [ - InlayHint { - range: [69; 71), - kind: TypeHint, - label: "Test", - }, - ] - "### - ); - } - #[test] fn let_statement() { let (analysis, file_id) = single_file( @@ -251,7 +225,7 @@ fn main() { }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, None, true).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" [ InlayHint { range: [193; 197), @@ -325,7 +299,7 @@ fn main() { }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, None, true).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" [ InlayHint { range: [21; 30), @@ -354,7 +328,7 @@ fn main() { }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, None, true).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" [ InlayHint { range: [21; 30), @@ -402,7 +376,7 @@ fn main() { }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, None, true).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" [ InlayHint { range: [166; 170), @@ -465,7 +439,7 @@ fn main() { }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, None, true).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" [ InlayHint { range: [166; 170), @@ -528,7 +502,7 @@ fn main() { }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, None, true).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" [ InlayHint { range: [311; 315), @@ -570,7 +544,7 @@ fn main() { }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, Some(8), true).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, Some(8)).unwrap(), @r###" [ InlayHint { range: [74; 75), diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs index c3244a8dd..875919e60 100644 --- a/crates/ra_ide/src/lib.rs +++ b/crates/ra_ide/src/lib.rs @@ -348,12 +348,9 @@ impl Analysis { &self, file_id: FileId, max_inlay_hint_length: Option, - show_default_types_in_inlay_hints: bool, ) -> Cancelable> { - let truncate_options = hir::TruncateOptions { - max_length: max_inlay_hint_length, - show_default_types: show_default_types_in_inlay_hints, - }; + let truncate_options = + hir::TruncateOptions { max_length: max_inlay_hint_length, show_default_types: false }; self.with_db(|db| { inlay_hints::inlay_hints(db, file_id, &db.parse(file_id).tree(), &truncate_options) }) -- cgit v1.2.3 From 4ed78f80f4cc3cf32681fce6722293da6c8df76d Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Thu, 19 Dec 2019 16:43:41 +0200 Subject: Remove TruncateOptions struct --- crates/ra_ide/src/inlay_hints.rs | 24 ++++++++++++------------ crates/ra_ide/src/lib.rs | 4 +--- 2 files changed, 13 insertions(+), 15 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/inlay_hints.rs b/crates/ra_ide/src/inlay_hints.rs index 4c5004f67..3154df457 100644 --- a/crates/ra_ide/src/inlay_hints.rs +++ b/crates/ra_ide/src/inlay_hints.rs @@ -1,7 +1,7 @@ //! FIXME: write short doc here use crate::{db::RootDatabase, FileId}; -use hir::{HirDisplay, SourceAnalyzer, TruncateOptions}; +use hir::{HirDisplay, SourceAnalyzer}; use ra_syntax::{ ast::{self, AstNode, TypeAscriptionOwner}, match_ast, SmolStr, SourceFile, SyntaxKind, SyntaxNode, TextRange, @@ -23,11 +23,11 @@ pub(crate) fn inlay_hints( db: &RootDatabase, file_id: FileId, file: &SourceFile, - truncate_options: &TruncateOptions, + max_inlay_hint_length: Option, ) -> Vec { file.syntax() .descendants() - .map(|node| get_inlay_hints(db, file_id, &node, truncate_options).unwrap_or_default()) + .map(|node| get_inlay_hints(db, file_id, &node, max_inlay_hint_length).unwrap_or_default()) .flatten() .collect() } @@ -36,7 +36,7 @@ fn get_inlay_hints( db: &RootDatabase, file_id: FileId, node: &SyntaxNode, - truncate_options: &TruncateOptions, + max_inlay_hint_length: Option, ) -> Option> { let analyzer = SourceAnalyzer::new(db, hir::InFile::new(file_id.into(), node), None); match_ast! { @@ -46,7 +46,7 @@ fn get_inlay_hints( return None; } let pat = it.pat()?; - Some(get_pat_type_hints(db, &analyzer, pat, false, truncate_options)) + Some(get_pat_type_hints(db, &analyzer, pat, false, max_inlay_hint_length)) }, ast::LambdaExpr(it) => { it.param_list().map(|param_list| { @@ -54,22 +54,22 @@ fn get_inlay_hints( .params() .filter(|closure_param| closure_param.ascribed_type().is_none()) .filter_map(|closure_param| closure_param.pat()) - .map(|root_pat| get_pat_type_hints(db, &analyzer, root_pat, false, truncate_options)) + .map(|root_pat| get_pat_type_hints(db, &analyzer, root_pat, false, max_inlay_hint_length)) .flatten() .collect() }) }, ast::ForExpr(it) => { let pat = it.pat()?; - Some(get_pat_type_hints(db, &analyzer, pat, false, truncate_options)) + Some(get_pat_type_hints(db, &analyzer, pat, false, max_inlay_hint_length)) }, ast::IfExpr(it) => { let pat = it.condition()?.pat()?; - Some(get_pat_type_hints(db, &analyzer, pat, true, truncate_options)) + Some(get_pat_type_hints(db, &analyzer, pat, true, max_inlay_hint_length)) }, ast::WhileExpr(it) => { let pat = it.condition()?.pat()?; - Some(get_pat_type_hints(db, &analyzer, pat, true, truncate_options)) + Some(get_pat_type_hints(db, &analyzer, pat, true, max_inlay_hint_length)) }, ast::MatchArmList(it) => { Some( @@ -77,7 +77,7 @@ fn get_inlay_hints( .arms() .map(|match_arm| match_arm.pats()) .flatten() - .map(|root_pat| get_pat_type_hints(db, &analyzer, root_pat, true, truncate_options)) + .map(|root_pat| get_pat_type_hints(db, &analyzer, root_pat, true, max_inlay_hint_length)) .flatten() .collect(), ) @@ -92,7 +92,7 @@ fn get_pat_type_hints( analyzer: &SourceAnalyzer, root_pat: ast::Pat, skip_root_pat_hint: bool, - truncate_options: &TruncateOptions, + max_inlay_hint_length: Option, ) -> Vec { let original_pat = &root_pat.clone(); @@ -109,7 +109,7 @@ fn get_pat_type_hints( .map(|(range, pat_type)| InlayHint { range, kind: InlayKind::TypeHint, - label: pat_type.display_truncated(db, truncate_options).to_string().into(), + label: pat_type.display_truncated(db, max_inlay_hint_length).to_string().into(), }) .collect() } diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs index 875919e60..779a81b2c 100644 --- a/crates/ra_ide/src/lib.rs +++ b/crates/ra_ide/src/lib.rs @@ -349,10 +349,8 @@ impl Analysis { file_id: FileId, max_inlay_hint_length: Option, ) -> Cancelable> { - let truncate_options = - hir::TruncateOptions { max_length: max_inlay_hint_length, show_default_types: false }; self.with_db(|db| { - inlay_hints::inlay_hints(db, file_id, &db.parse(file_id).tree(), &truncate_options) + inlay_hints::inlay_hints(db, file_id, &db.parse(file_id).tree(), max_inlay_hint_length) }) } -- cgit v1.2.3 From f407ac2be332e474b25a10aaf3be145c85f4b60b Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Thu, 19 Dec 2019 16:47:09 +0200 Subject: Omit default types for hover pop-ups --- crates/ra_ide/src/hover.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/hover.rs b/crates/ra_ide/src/hover.rs index 7d2f160e7..a227bf546 100644 --- a/crates/ra_ide/src/hover.rs +++ b/crates/ra_ide/src/hover.rs @@ -250,7 +250,7 @@ pub(crate) fn type_of(db: &RootDatabase, frange: FileRange) -> Option { } else { return None; }; - Some(ty.display(db).to_string()) + Some(ty.display_truncated(db, None).to_string()) } #[cfg(test)] @@ -425,7 +425,7 @@ mod tests { } #[test] - fn hover_default_generic_type() { + fn hover_omits_default_generic_types() { check_hover_result( r#" //- /main.rs @@ -437,7 +437,7 @@ struct Test { fn main() { let zz<|> = Test { t: 23, k: 33 }; }"#, - &["Test"], + &["Test"], ); } -- cgit v1.2.3 From a04177f135be89ddbf1788c6f747c26812e90438 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 20 Dec 2019 11:19:09 +0100 Subject: Add local functions to bodies --- crates/ra_ide/src/goto_definition.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/goto_definition.rs b/crates/ra_ide/src/goto_definition.rs index 184555792..ee4ae3e03 100644 --- a/crates/ra_ide/src/goto_definition.rs +++ b/crates/ra_ide/src/goto_definition.rs @@ -858,4 +858,21 @@ mod tests { "y", ); } + + #[test] + fn goto_def_in_local_fn() { + check_goto( + " + //- /lib.rs + fn main() { + fn foo() { + let x = 92; + <|>x; + } + } + ", + "x BIND_PAT FileId(1) [39; 40)", + "x", + ); + } } -- cgit v1.2.3 From d057afb945b34cbefc1f0722f4dd0ff2758b760b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 20 Dec 2019 14:10:31 +0100 Subject: Improve highlighting test --- crates/ra_ide/src/snapshots/highlighting.html | 4 +++- crates/ra_ide/src/snapshots/rainbow_highlighting.html | 1 + crates/ra_ide/src/syntax_highlighting.rs | 4 +++- 3 files changed, 7 insertions(+), 2 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/snapshots/highlighting.html b/crates/ra_ide/src/snapshots/highlighting.html index 2157139f6..748fd9558 100644 --- a/crates/ra_ide/src/snapshots/highlighting.html +++ b/crates/ra_ide/src/snapshots/highlighting.html @@ -5,6 +5,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .comment { color: #7F9F7F; } .string { color: #CC9393; } +.field { color: #94BFF3; } .function { color: #93E0E3; } .parameter { color: #94BFF3; } .text { color: #DCDCCC; } @@ -39,7 +40,8 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd let mut vec = Vec::new(); if true { - vec.push(Foo { x: 0, y: 1 }); + let x = 92; + vec.push(Foo { x, y: 1 }); } unsafe { vec.set_len(0); } diff --git a/crates/ra_ide/src/snapshots/rainbow_highlighting.html b/crates/ra_ide/src/snapshots/rainbow_highlighting.html index 871a52cf6..110556c09 100644 --- a/crates/ra_ide/src/snapshots/rainbow_highlighting.html +++ b/crates/ra_ide/src/snapshots/rainbow_highlighting.html @@ -5,6 +5,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .comment { color: #7F9F7F; } .string { color: #CC9393; } +.field { color: #94BFF3; } .function { color: #93E0E3; } .parameter { color: #94BFF3; } .text { color: #DCDCCC; } diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index 15e75709c..63f5d3cb0 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -282,6 +282,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .comment { color: #7F9F7F; } .string { color: #CC9393; } +.field { color: #94BFF3; } .function { color: #93E0E3; } .parameter { color: #94BFF3; } .text { color: #DCDCCC; } @@ -327,7 +328,8 @@ fn main() { let mut vec = Vec::new(); if true { - vec.push(Foo { x: 0, y: 1 }); + let x = 92; + vec.push(Foo { x, y: 1 }); } unsafe { vec.set_len(0); } -- cgit v1.2.3 From 77af7b087fd95140c28b30bf7127d7d83fb0dcc6 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 20 Dec 2019 14:11:49 +0100 Subject: Fix highlighting for field init shorthand --- crates/ra_ide/src/snapshots/highlighting.html | 2 +- crates/ra_ide/src/syntax_highlighting.rs | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/snapshots/highlighting.html b/crates/ra_ide/src/snapshots/highlighting.html index 748fd9558..a097cf8e8 100644 --- a/crates/ra_ide/src/snapshots/highlighting.html +++ b/crates/ra_ide/src/snapshots/highlighting.html @@ -41,7 +41,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd let mut vec = Vec::new(); if true { let x = 92; - vec.push(Foo { x, y: 1 }); + vec.push(Foo { x, y: 1 }); } unsafe { vec.set_len(0); } diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index 63f5d3cb0..657c7b21a 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -102,11 +102,10 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec tags::LITERAL_COMMENT, STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => tags::LITERAL_STRING, ATTR => tags::LITERAL_ATTRIBUTE, + // Special-case field init shorthand + NAME_REF if node.parent().and_then(ast::RecordField::cast).is_some() => tags::FIELD, + NAME_REF if node.ancestors().any(|it| it.kind() == ATTR) => continue, NAME_REF => { - if node.ancestors().any(|it| it.kind() == ATTR) { - continue; - } - let name_ref = node.as_node().cloned().and_then(ast::NameRef::cast).unwrap(); let name_kind = classify_name_ref(db, InFile::new(file_id.into(), &name_ref)).map(|d| d.kind); -- cgit v1.2.3 From 3d4b48e481da35f19366514c0e22ed42fef037a0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 20 Dec 2019 14:47:01 +0100 Subject: Fix resolve for field init shorthand --- crates/ra_ide/src/goto_definition.rs | 57 +++++++++++++++++++++----------- crates/ra_ide/src/marks.rs | 9 ++--- crates/ra_ide/src/references/classify.rs | 9 ++--- 3 files changed, 47 insertions(+), 28 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/goto_definition.rs b/crates/ra_ide/src/goto_definition.rs index ee4ae3e03..9b5744789 100644 --- a/crates/ra_ide/src/goto_definition.rs +++ b/crates/ra_ide/src/goto_definition.rs @@ -258,7 +258,7 @@ mod tests { } #[test] - fn goto_definition_works_in_items() { + fn goto_def_in_items() { check_goto( " //- /lib.rs @@ -271,7 +271,7 @@ mod tests { } #[test] - fn goto_definition_works_at_start_of_item() { + fn goto_def_at_start_of_item() { check_goto( " //- /lib.rs @@ -305,7 +305,7 @@ mod tests { } #[test] - fn goto_definition_works_for_module_declaration() { + fn goto_def_for_module_declaration() { check_goto( " //- /lib.rs @@ -332,8 +332,8 @@ mod tests { } #[test] - fn goto_definition_works_for_macros() { - covers!(goto_definition_works_for_macros); + fn goto_def_for_macros() { + covers!(goto_def_for_macros); check_goto( " //- /lib.rs @@ -349,8 +349,8 @@ mod tests { } #[test] - fn goto_definition_works_for_macros_from_other_crates() { - covers!(goto_definition_works_for_macros); + fn goto_def_for_macros_from_other_crates() { + covers!(goto_def_for_macros); check_goto( " //- /lib.rs @@ -369,7 +369,7 @@ mod tests { } #[test] - fn goto_definition_works_for_macros_in_use_tree() { + fn goto_def_for_macros_in_use_tree() { check_goto( " //- /lib.rs @@ -385,7 +385,7 @@ mod tests { } #[test] - fn goto_definition_works_for_macro_defined_fn_with_arg() { + fn goto_def_for_macro_defined_fn_with_arg() { check_goto( " //- /lib.rs @@ -405,7 +405,7 @@ mod tests { } #[test] - fn goto_definition_works_for_macro_defined_fn_no_arg() { + fn goto_def_for_macro_defined_fn_no_arg() { check_goto( " //- /lib.rs @@ -425,8 +425,8 @@ mod tests { } #[test] - fn goto_definition_works_for_methods() { - covers!(goto_definition_works_for_methods); + fn goto_def_for_methods() { + covers!(goto_def_for_methods); check_goto( " //- /lib.rs @@ -445,8 +445,8 @@ mod tests { } #[test] - fn goto_definition_works_for_fields() { - covers!(goto_definition_works_for_fields); + fn goto_def_for_fields() { + covers!(goto_def_for_fields); check_goto( " //- /lib.rs @@ -464,8 +464,8 @@ mod tests { } #[test] - fn goto_definition_works_for_record_fields() { - covers!(goto_definition_works_for_record_fields); + fn goto_def_for_record_fields() { + covers!(goto_def_for_record_fields); check_goto( " //- /lib.rs @@ -502,7 +502,7 @@ mod tests { } #[test] - fn goto_definition_works_for_ufcs_inherent_methods() { + fn goto_def_for_ufcs_inherent_methods() { check_goto( " //- /lib.rs @@ -521,7 +521,7 @@ mod tests { } #[test] - fn goto_definition_works_for_ufcs_trait_methods_through_traits() { + fn goto_def_for_ufcs_trait_methods_through_traits() { check_goto( " //- /lib.rs @@ -539,7 +539,7 @@ mod tests { } #[test] - fn goto_definition_works_for_ufcs_trait_methods_through_self() { + fn goto_def_for_ufcs_trait_methods_through_self() { check_goto( " //- /lib.rs @@ -654,7 +654,7 @@ mod tests { } #[test] - fn goto_definition_works_when_used_on_definition_name_itself() { + fn goto_def_when_used_on_definition_name_itself() { check_goto( " //- /lib.rs @@ -875,4 +875,21 @@ mod tests { "x", ); } + + #[test] + fn goto_def_for_field_init_shorthand() { + covers!(goto_def_for_field_init_shorthand); + check_goto( + " + //- /lib.rs + struct Foo { x: i32 } + fn main() { + let x = 92; + Foo { x<|> }; + } + ", + "x RECORD_FIELD_DEF FileId(1) [13; 19) [13; 14)", + "x: i32|x", + ) + } } diff --git a/crates/ra_ide/src/marks.rs b/crates/ra_ide/src/marks.rs index 848ae4dc7..077a44473 100644 --- a/crates/ra_ide/src/marks.rs +++ b/crates/ra_ide/src/marks.rs @@ -3,10 +3,11 @@ test_utils::marks!( inserts_angle_brackets_for_generics inserts_parens_for_function_calls - goto_definition_works_for_macros - goto_definition_works_for_methods - goto_definition_works_for_fields - goto_definition_works_for_record_fields + goto_def_for_macros + goto_def_for_methods + goto_def_for_fields + goto_def_for_record_fields + goto_def_for_field_init_shorthand call_info_bad_offset dont_complete_current_use dont_complete_primitive_in_use diff --git a/crates/ra_ide/src/references/classify.rs b/crates/ra_ide/src/references/classify.rs index c1f091ec0..3483a7176 100644 --- a/crates/ra_ide/src/references/classify.rs +++ b/crates/ra_ide/src/references/classify.rs @@ -134,21 +134,22 @@ pub(crate) fn classify_name_ref( let analyzer = SourceAnalyzer::new(db, name_ref.map(|it| it.syntax()), None); if let Some(method_call) = ast::MethodCallExpr::cast(parent.clone()) { - tested_by!(goto_definition_works_for_methods); + tested_by!(goto_def_for_methods); if let Some(func) = analyzer.resolve_method_call(&method_call) { return Some(from_assoc_item(db, func.into())); } } if let Some(field_expr) = ast::FieldExpr::cast(parent.clone()) { - tested_by!(goto_definition_works_for_fields); + tested_by!(goto_def_for_fields); if let Some(field) = analyzer.resolve_field(&field_expr) { return Some(from_struct_field(db, field)); } } if let Some(record_field) = ast::RecordField::cast(parent.clone()) { - tested_by!(goto_definition_works_for_record_fields); + tested_by!(goto_def_for_record_fields); + tested_by!(goto_def_for_field_init_shorthand); if let Some(field_def) = analyzer.resolve_record_field(&record_field) { return Some(from_struct_field(db, field_def)); } @@ -160,7 +161,7 @@ pub(crate) fn classify_name_ref( let visibility = None; if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) { - tested_by!(goto_definition_works_for_macros); + tested_by!(goto_def_for_macros); if let Some(macro_def) = analyzer.resolve_macro_call(db, name_ref.with_value(¯o_call)) { let kind = NameKind::Macro(macro_def); return Some(NameDefinition { kind, container, visibility }); -- cgit v1.2.3 From 4a7e19946a60b4cba6ef9d9916ae0fbec65c74da Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Fri, 20 Dec 2019 23:11:07 +0800 Subject: Fix parser for macro call in pattern position --- crates/ra_ide/src/goto_definition.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/goto_definition.rs b/crates/ra_ide/src/goto_definition.rs index 9b5744789..79d332e8c 100644 --- a/crates/ra_ide/src/goto_definition.rs +++ b/crates/ra_ide/src/goto_definition.rs @@ -424,6 +424,42 @@ mod tests { ); } + #[test] + fn goto_definition_works_for_macro_inside_pattern() { + check_goto( + " + //- /lib.rs + macro_rules! foo {() => {0}} + + fn bar() { + match (0,1) { + (<|>foo!(), _) => {} + } + } + ", + "foo MACRO_CALL FileId(1) [0; 28) [13; 16)", + "macro_rules! foo {() => {0}}|foo", + ); + } + + #[test] + fn goto_definition_works_for_macro_inside_match_arm_lhs() { + check_goto( + " + //- /lib.rs + macro_rules! foo {() => {0}} + + fn bar() { + match 0 { + <|>foo!() => {} + } + } + ", + "foo MACRO_CALL FileId(1) [0; 28) [13; 16)", + "macro_rules! foo {() => {0}}|foo", + ); + } + #[test] fn goto_def_for_methods() { covers!(goto_def_for_methods); -- cgit v1.2.3 From 428358925c40fe1e15bf0b67bbf007c54b510920 Mon Sep 17 00:00:00 2001 From: kjeremy Date: Fri, 20 Dec 2019 11:12:31 -0500 Subject: Remove the index resolution from hover We are reasonably precise now to do this. --- crates/ra_ide/src/hover.rs | 80 +++++++++++++--------------------------------- 1 file changed, 22 insertions(+), 58 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/hover.rs b/crates/ra_ide/src/hover.rs index a227bf546..35e39f965 100644 --- a/crates/ra_ide/src/hover.rs +++ b/crates/ra_ide/src/hover.rs @@ -12,10 +12,7 @@ use ra_syntax::{ use crate::{ db::RootDatabase, - display::{ - description_from_symbol, docs_from_symbol, macro_label, rust_code_markup, - rust_code_markup_with_doc, ShortLabel, - }, + display::{macro_label, rust_code_markup, rust_code_markup_with_doc, ShortLabel}, expand::descend_into_macros, references::{classify_name, classify_name_ref, NameKind, NameKind::*}, FilePosition, FileRange, RangeInfo, @@ -95,11 +92,7 @@ fn hover_text(docs: Option, desc: Option) -> Option { } } -fn hover_text_from_name_kind( - db: &RootDatabase, - name_kind: NameKind, - no_fallback: &mut bool, -) -> Option { +fn hover_text_from_name_kind(db: &RootDatabase, name_kind: NameKind) -> Option { return match name_kind { Macro(it) => { let src = it.source(db); @@ -135,11 +128,7 @@ fn hover_text_from_name_kind( hir::ModuleDef::TypeAlias(it) => from_def_source(db, it), hir::ModuleDef::BuiltinType(it) => Some(it.to_string()), }, - Local(_) => { - // Hover for these shows type names - *no_fallback = true; - None - } + Local(_) => None, TypeParam(_) | SelfType(_) => { // FIXME: Hover for generic param None @@ -163,60 +152,35 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option { - let mut no_fallback = false; - if let Some(name_kind) = - classify_name_ref(db, token.with_value(&name_ref)).map(|d| d.kind) - { - res.extend(hover_text_from_name_kind(db, name_kind, &mut no_fallback)) - } - - if res.is_empty() && !no_fallback { - // Fallback index based approach: - let symbols = crate::symbol_index::index_resolve(db, &name_ref); - for sym in symbols { - let docs = docs_from_symbol(db, &sym); - let desc = description_from_symbol(db, &sym); - res.extend(hover_text(docs, desc)); - } - } - - if !res.is_empty() { - Some(name_ref.syntax().text_range()) - } else { - None - } + classify_name_ref(db, token.with_value(&name_ref)).map(|d| (name_ref.syntax().text_range(), d.kind)) }, ast::Name(name) => { - if let Some(name_kind) = classify_name(db, token.with_value(&name)).map(|d| d.kind) { - res.extend(hover_text_from_name_kind(db, name_kind, &mut true)); - } - - if !res.is_empty() { - Some(name.syntax().text_range()) - } else { - None - } + classify_name(db, token.with_value(&name)).map(|d| (name.syntax().text_range(), d.kind)) }, _ => None, } - }; + } { + res.extend(hover_text_from_name_kind(db, name_kind)); - if range.is_none() { - let node = token.value.ancestors().find(|n| { - ast::Expr::cast(n.clone()).is_some() || ast::Pat::cast(n.clone()).is_some() - })?; - let frange = FileRange { file_id: position.file_id, range: node.text_range() }; - res.extend(type_of(db, frange).map(rust_code_markup)); - range = Some(node.text_range()); - }; + if !res.is_empty() { + return Some(RangeInfo::new(range, res)); + } + } - let range = range?; + let node = token + .value + .ancestors() + .find(|n| ast::Expr::cast(n.clone()).is_some() || ast::Pat::cast(n.clone()).is_some())?; + let frange = FileRange { file_id: position.file_id, range: node.text_range() }; + res.extend(type_of(db, frange).map(rust_code_markup)); if res.is_empty() { return None; } + let range = node.text_range(); + Some(RangeInfo::new(range, res)) } @@ -314,7 +278,7 @@ mod tests { &["pub fn foo() -> u32"], ); - // Multiple results + // Multiple candidates but results are ambiguous. check_hover_result( r#" //- /a.rs @@ -335,7 +299,7 @@ mod tests { let foo_test = fo<|>o(); } "#, - &["pub fn foo() -> &str", "pub fn foo() -> u32", "pub fn foo(a: u32, b: u32)"], + &["{unknown}"], ); } -- cgit v1.2.3 From 77626d7bdafa26ca539276b975d74348aabe175e Mon Sep 17 00:00:00 2001 From: kjeremy Date: Fri, 20 Dec 2019 11:18:24 -0500 Subject: Remove unused imports --- crates/ra_ide/src/display.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/display.rs b/crates/ra_ide/src/display.rs index 30617412a..fbe89841b 100644 --- a/crates/ra_ide/src/display.rs +++ b/crates/ra_ide/src/display.rs @@ -15,7 +15,7 @@ pub use function_signature::FunctionSignature; pub use navigation_target::NavigationTarget; pub use structure::{file_structure, StructureNode}; -pub(crate) use navigation_target::{description_from_symbol, docs_from_symbol, ToNav}; +pub(crate) use navigation_target::ToNav; pub(crate) use short_label::ShortLabel; pub(crate) fn function_label(node: &ast::FnDef) -> String { -- cgit v1.2.3 From 0d5d63a80ea08f2af439bcc72fff9b24d144c70d Mon Sep 17 00:00:00 2001 From: kjeremy Date: Fri, 20 Dec 2019 15:14:30 -0500 Subject: Clippy lints --- crates/ra_ide/src/completion/complete_postfix.rs | 2 +- crates/ra_ide/src/completion/completion_context.rs | 17 ++++--- crates/ra_ide/src/expand_macro.rs | 13 ++---- crates/ra_ide/src/extend_selection.rs | 2 +- crates/ra_ide/src/syntax_highlighting.rs | 54 +++++++++++----------- 5 files changed, 41 insertions(+), 47 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/completion/complete_postfix.rs b/crates/ra_ide/src/completion/complete_postfix.rs index 646a30c76..5470dc291 100644 --- a/crates/ra_ide/src/completion/complete_postfix.rs +++ b/crates/ra_ide/src/completion/complete_postfix.rs @@ -12,7 +12,7 @@ use crate::{ }; pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) { - if ctx.db.feature_flags.get("completion.enable-postfix") == false { + if !ctx.db.feature_flags.get("completion.enable-postfix") { return; } diff --git a/crates/ra_ide/src/completion/completion_context.rs b/crates/ra_ide/src/completion/completion_context.rs index 981da2b79..4894ea2f6 100644 --- a/crates/ra_ide/src/completion/completion_context.rs +++ b/crates/ra_ide/src/completion/completion_context.rs @@ -239,16 +239,15 @@ impl<'a> CompletionContext<'a> { .expr() .map(|e| e.syntax().text_range()) .and_then(|r| find_node_with_range(original_file.syntax(), r)); - self.dot_receiver_is_ambiguous_float_literal = if let Some(ast::Expr::Literal(l)) = - &self.dot_receiver - { - match l.kind() { - ast::LiteralKind::FloatNumber { suffix: _ } => l.token().text().ends_with('.'), - _ => false, + self.dot_receiver_is_ambiguous_float_literal = + if let Some(ast::Expr::Literal(l)) = &self.dot_receiver { + match l.kind() { + ast::LiteralKind::FloatNumber { .. } => l.token().text().ends_with('.'), + _ => false, + } + } else { + false } - } else { - false - } } if let Some(method_call_expr) = ast::MethodCallExpr::cast(parent) { // As above diff --git a/crates/ra_ide/src/expand_macro.rs b/crates/ra_ide/src/expand_macro.rs index 862c03304..bdbc31704 100644 --- a/crates/ra_ide/src/expand_macro.rs +++ b/crates/ra_ide/src/expand_macro.rs @@ -86,21 +86,18 @@ fn insert_whitespaces(syn: SyntaxNode) -> String { let mut is_next = |f: fn(SyntaxKind) -> bool, default| -> bool { token_iter.peek().map(|it| f(it.kind())).unwrap_or(default) }; - let is_last = |f: fn(SyntaxKind) -> bool, default| -> bool { - last.map(|it| f(it)).unwrap_or(default) - }; + let is_last = + |f: fn(SyntaxKind) -> bool, default| -> bool { last.map(f).unwrap_or(default) }; res += &match token.kind() { - k @ _ if is_text(k) && is_next(|it| !it.is_punct(), true) => { - token.text().to_string() + " " - } + k if is_text(k) && is_next(|it| !it.is_punct(), true) => token.text().to_string() + " ", L_CURLY if is_next(|it| it != R_CURLY, true) => { indent += 1; - let leading_space = if is_last(|it| is_text(it), false) { " " } else { "" }; + let leading_space = if is_last(is_text, false) { " " } else { "" }; format!("{}{{\n{}", leading_space, " ".repeat(indent)) } R_CURLY if is_last(|it| it != L_CURLY, true) => { - indent = indent.checked_sub(1).unwrap_or(0); + indent = indent.saturating_sub(1); format!("\n{}}}", " ".repeat(indent)) } R_CURLY => format!("}}\n{}", " ".repeat(indent)), diff --git a/crates/ra_ide/src/extend_selection.rs b/crates/ra_ide/src/extend_selection.rs index c096ca6ae..1ec41a117 100644 --- a/crates/ra_ide/src/extend_selection.rs +++ b/crates/ra_ide/src/extend_selection.rs @@ -138,7 +138,7 @@ fn extend_ws(root: &SyntaxNode, ws: SyntaxToken, offset: TextUnit) -> TextRange ws.text_range() } -fn pick_best<'a>(l: SyntaxToken, r: SyntaxToken) -> SyntaxToken { +fn pick_best(l: SyntaxToken, r: SyntaxToken) -> SyntaxToken { return if priority(&r) > priority(&l) { r } else { l }; fn priority(n: &SyntaxToken) -> usize { match n.kind() { diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index 657c7b21a..0228ee7e9 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -17,31 +17,31 @@ use crate::{ }; pub mod tags { - pub(crate) const FIELD: &'static str = "field"; - pub(crate) const FUNCTION: &'static str = "function"; - pub(crate) const MODULE: &'static str = "module"; - pub(crate) const TYPE: &'static str = "type"; - pub(crate) const CONSTANT: &'static str = "constant"; - pub(crate) const MACRO: &'static str = "macro"; - pub(crate) const VARIABLE: &'static str = "variable"; - pub(crate) const VARIABLE_MUT: &'static str = "variable.mut"; - pub(crate) const TEXT: &'static str = "text"; - - pub(crate) const TYPE_BUILTIN: &'static str = "type.builtin"; - pub(crate) const TYPE_SELF: &'static str = "type.self"; - pub(crate) const TYPE_PARAM: &'static str = "type.param"; - pub(crate) const TYPE_LIFETIME: &'static str = "type.lifetime"; - - pub(crate) const LITERAL_BYTE: &'static str = "literal.byte"; - pub(crate) const LITERAL_NUMERIC: &'static str = "literal.numeric"; - pub(crate) const LITERAL_CHAR: &'static str = "literal.char"; - pub(crate) const LITERAL_COMMENT: &'static str = "comment"; - pub(crate) const LITERAL_STRING: &'static str = "string"; - pub(crate) const LITERAL_ATTRIBUTE: &'static str = "attribute"; - - pub(crate) const KEYWORD_UNSAFE: &'static str = "keyword.unsafe"; - pub(crate) const KEYWORD_CONTROL: &'static str = "keyword.control"; - pub(crate) const KEYWORD: &'static str = "keyword"; + pub(crate) const FIELD: &str = "field"; + pub(crate) const FUNCTION: &str = "function"; + pub(crate) const MODULE: &str = "module"; + pub(crate) const TYPE: &str = "type"; + pub(crate) const CONSTANT: &str = "constant"; + pub(crate) const MACRO: &str = "macro"; + pub(crate) const VARIABLE: &str = "variable"; + pub(crate) const VARIABLE_MUT: &str = "variable.mut"; + pub(crate) const TEXT: &str = "text"; + + pub(crate) const TYPE_BUILTIN: &str = "type.builtin"; + pub(crate) const TYPE_SELF: &str = "type.self"; + pub(crate) const TYPE_PARAM: &str = "type.param"; + pub(crate) const TYPE_LIFETIME: &str = "type.lifetime"; + + pub(crate) const LITERAL_BYTE: &str = "literal.byte"; + pub(crate) const LITERAL_NUMERIC: &str = "literal.numeric"; + pub(crate) const LITERAL_CHAR: &str = "literal.char"; + pub(crate) const LITERAL_COMMENT: &str = "comment"; + pub(crate) const LITERAL_STRING: &str = "string"; + pub(crate) const LITERAL_ATTRIBUTE: &str = "attribute"; + + pub(crate) const KEYWORD_UNSAFE: &str = "keyword.unsafe"; + pub(crate) const KEYWORD_CONTROL: &str = "keyword.control"; + pub(crate) const KEYWORD: &str = "keyword"; } #[derive(Debug)] @@ -258,9 +258,7 @@ fn highlight_name(db: &RootDatabase, name_kind: NameKind) -> &'static str { SelfType(_) => tags::TYPE_SELF, TypeParam(_) => tags::TYPE_PARAM, Local(local) => { - if local.is_mut(db) { - tags::VARIABLE_MUT - } else if local.ty(db).is_mutable_reference() { + if local.is_mut(db) || local.ty(db).is_mutable_reference() { tags::VARIABLE_MUT } else { tags::VARIABLE -- cgit v1.2.3 From 67ba9072fad8698af4e96b38b8b4acfdd801f7f7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 21 Dec 2019 12:15:56 +0100 Subject: Remove hir for imports --- crates/ra_ide/src/completion/complete_path.rs | 17 +++++++---------- crates/ra_ide/src/completion/completion_context.rs | 4 ++++ 2 files changed, 11 insertions(+), 10 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/completion/complete_path.rs b/crates/ra_ide/src/completion/complete_path.rs index 28f94e0a7..8ce86ad7d 100644 --- a/crates/ra_ide/src/completion/complete_path.rs +++ b/crates/ra_ide/src/completion/complete_path.rs @@ -1,7 +1,6 @@ //! FIXME: write short doc here -use either::Either; -use hir::{Adt, HasSource, PathResolution}; +use hir::{Adt, PathResolution, ScopeDef}; use ra_syntax::AstNode; use test_utils::tested_by; @@ -19,17 +18,15 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { match def { hir::ModuleDef::Module(module) => { let module_scope = module.scope(ctx.db); - for (name, def, import) in module_scope { - if let hir::ScopeDef::ModuleDef(hir::ModuleDef::BuiltinType(..)) = def { - if ctx.use_item_syntax.is_some() { + for (name, def) in module_scope { + if ctx.use_item_syntax.is_some() { + if let hir::ScopeDef::ModuleDef(hir::ModuleDef::BuiltinType(..)) = def { tested_by!(dont_complete_primitive_in_use); continue; } - } - if Some(module) == ctx.module { - if let Some(import) = import { - if let Either::Left(use_tree) = import.source(ctx.db).value { - if use_tree.syntax().text_range().contains_inclusive(ctx.offset) { + if let ScopeDef::Unknown = def { + if let Some(name_ref) = ctx.name_ref.as_ref() { + if &name_ref.syntax().text() == name.to_string().as_str() { // for `use self::foo<|>`, don't suggest `foo` as a completion tested_by!(dont_complete_current_use); continue; diff --git a/crates/ra_ide/src/completion/completion_context.rs b/crates/ra_ide/src/completion/completion_context.rs index 4894ea2f6..8f56ce706 100644 --- a/crates/ra_ide/src/completion/completion_context.rs +++ b/crates/ra_ide/src/completion/completion_context.rs @@ -18,6 +18,7 @@ pub(crate) struct CompletionContext<'a> { pub(super) analyzer: hir::SourceAnalyzer, pub(super) offset: TextUnit, pub(super) token: SyntaxToken, + pub(super) name_ref: Option, pub(super) module: Option, pub(super) function_syntax: Option, pub(super) use_item_syntax: Option, @@ -68,6 +69,7 @@ impl<'a> CompletionContext<'a> { analyzer, token, offset: position.offset, + name_ref: None, module, function_syntax: None, use_item_syntax: None, @@ -142,6 +144,8 @@ impl<'a> CompletionContext<'a> { } fn classify_name_ref(&mut self, original_file: SourceFile, name_ref: ast::NameRef) { + self.name_ref = + find_node_at_offset(original_file.syntax(), name_ref.syntax().text_range().start()); let name_range = name_ref.syntax().text_range(); if name_ref.syntax().parent().and_then(ast::RecordField::cast).is_some() { self.record_lit_syntax = find_node_at_offset(original_file.syntax(), self.offset); -- cgit v1.2.3 From d3353118939d5ab77a63218db6ef542843256aac Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 21 Dec 2019 12:44:28 +0100 Subject: Remove import source map --- crates/ra_ide/src/change.rs | 2 -- 1 file changed, 2 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/change.rs b/crates/ra_ide/src/change.rs index 4a76d1dd8..387a9cafb 100644 --- a/crates/ra_ide/src/change.rs +++ b/crates/ra_ide/src/change.rs @@ -270,7 +270,6 @@ impl RootDatabase { self.query(hir::db::AstIdMapQuery).sweep(sweep); - self.query(hir::db::RawItemsWithSourceMapQuery).sweep(sweep); self.query(hir::db::BodyWithSourceMapQuery).sweep(sweep); self.query(hir::db::ExprScopesQuery).sweep(sweep); @@ -309,7 +308,6 @@ impl RootDatabase { hir::db::StructDataQuery hir::db::EnumDataQuery hir::db::TraitDataQuery - hir::db::RawItemsWithSourceMapQuery hir::db::RawItemsQuery hir::db::CrateDefMapQuery hir::db::GenericParamsQuery -- cgit v1.2.3 From 973b5cf7e20842711d59a810b268796b26241382 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 21 Dec 2019 15:04:33 +0100 Subject: Revert "Merge #2629" This reverts commit cdc9d682b066b110e0a44e5f8f1c574b38c16ba9, reversing changes made to 90ef070db3dce0a7acb9cd11d0b0d72de13c9d79. --- crates/ra_ide/src/change.rs | 2 ++ crates/ra_ide/src/completion/complete_path.rs | 17 ++++++++++------- crates/ra_ide/src/completion/completion_context.rs | 4 ---- 3 files changed, 12 insertions(+), 11 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/change.rs b/crates/ra_ide/src/change.rs index 387a9cafb..4a76d1dd8 100644 --- a/crates/ra_ide/src/change.rs +++ b/crates/ra_ide/src/change.rs @@ -270,6 +270,7 @@ impl RootDatabase { self.query(hir::db::AstIdMapQuery).sweep(sweep); + self.query(hir::db::RawItemsWithSourceMapQuery).sweep(sweep); self.query(hir::db::BodyWithSourceMapQuery).sweep(sweep); self.query(hir::db::ExprScopesQuery).sweep(sweep); @@ -308,6 +309,7 @@ impl RootDatabase { hir::db::StructDataQuery hir::db::EnumDataQuery hir::db::TraitDataQuery + hir::db::RawItemsWithSourceMapQuery hir::db::RawItemsQuery hir::db::CrateDefMapQuery hir::db::GenericParamsQuery diff --git a/crates/ra_ide/src/completion/complete_path.rs b/crates/ra_ide/src/completion/complete_path.rs index 8ce86ad7d..28f94e0a7 100644 --- a/crates/ra_ide/src/completion/complete_path.rs +++ b/crates/ra_ide/src/completion/complete_path.rs @@ -1,6 +1,7 @@ //! FIXME: write short doc here -use hir::{Adt, PathResolution, ScopeDef}; +use either::Either; +use hir::{Adt, HasSource, PathResolution}; use ra_syntax::AstNode; use test_utils::tested_by; @@ -18,15 +19,17 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { match def { hir::ModuleDef::Module(module) => { let module_scope = module.scope(ctx.db); - for (name, def) in module_scope { - if ctx.use_item_syntax.is_some() { - if let hir::ScopeDef::ModuleDef(hir::ModuleDef::BuiltinType(..)) = def { + for (name, def, import) in module_scope { + if let hir::ScopeDef::ModuleDef(hir::ModuleDef::BuiltinType(..)) = def { + if ctx.use_item_syntax.is_some() { tested_by!(dont_complete_primitive_in_use); continue; } - if let ScopeDef::Unknown = def { - if let Some(name_ref) = ctx.name_ref.as_ref() { - if &name_ref.syntax().text() == name.to_string().as_str() { + } + if Some(module) == ctx.module { + if let Some(import) = import { + if let Either::Left(use_tree) = import.source(ctx.db).value { + if use_tree.syntax().text_range().contains_inclusive(ctx.offset) { // for `use self::foo<|>`, don't suggest `foo` as a completion tested_by!(dont_complete_current_use); continue; diff --git a/crates/ra_ide/src/completion/completion_context.rs b/crates/ra_ide/src/completion/completion_context.rs index 8f56ce706..4894ea2f6 100644 --- a/crates/ra_ide/src/completion/completion_context.rs +++ b/crates/ra_ide/src/completion/completion_context.rs @@ -18,7 +18,6 @@ pub(crate) struct CompletionContext<'a> { pub(super) analyzer: hir::SourceAnalyzer, pub(super) offset: TextUnit, pub(super) token: SyntaxToken, - pub(super) name_ref: Option, pub(super) module: Option, pub(super) function_syntax: Option, pub(super) use_item_syntax: Option, @@ -69,7 +68,6 @@ impl<'a> CompletionContext<'a> { analyzer, token, offset: position.offset, - name_ref: None, module, function_syntax: None, use_item_syntax: None, @@ -144,8 +142,6 @@ impl<'a> CompletionContext<'a> { } fn classify_name_ref(&mut self, original_file: SourceFile, name_ref: ast::NameRef) { - self.name_ref = - find_node_at_offset(original_file.syntax(), name_ref.syntax().text_range().start()); let name_range = name_ref.syntax().text_range(); if name_ref.syntax().parent().and_then(ast::RecordField::cast).is_some() { self.record_lit_syntax = find_node_at_offset(original_file.syntax(), self.offset); -- cgit v1.2.3 From 4e0168ec14b74003e8388b72b64c2ca78b580274 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 21 Dec 2019 15:17:10 +0100 Subject: Remove imports from hir --- crates/ra_ide/src/completion/complete_path.rs | 18 ++++++++---------- crates/ra_ide/src/completion/completion_context.rs | 4 ++++ 2 files changed, 12 insertions(+), 10 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/completion/complete_path.rs b/crates/ra_ide/src/completion/complete_path.rs index 28f94e0a7..cc1f7c830 100644 --- a/crates/ra_ide/src/completion/complete_path.rs +++ b/crates/ra_ide/src/completion/complete_path.rs @@ -1,7 +1,6 @@ //! FIXME: write short doc here -use either::Either; -use hir::{Adt, HasSource, PathResolution}; +use hir::{Adt, PathResolution, ScopeDef}; use ra_syntax::AstNode; use test_utils::tested_by; @@ -19,17 +18,15 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { match def { hir::ModuleDef::Module(module) => { let module_scope = module.scope(ctx.db); - for (name, def, import) in module_scope { - if let hir::ScopeDef::ModuleDef(hir::ModuleDef::BuiltinType(..)) = def { - if ctx.use_item_syntax.is_some() { + for (name, def) in module_scope { + if ctx.use_item_syntax.is_some() { + if let hir::ScopeDef::ModuleDef(hir::ModuleDef::BuiltinType(..)) = def { tested_by!(dont_complete_primitive_in_use); continue; } - } - if Some(module) == ctx.module { - if let Some(import) = import { - if let Either::Left(use_tree) = import.source(ctx.db).value { - if use_tree.syntax().text_range().contains_inclusive(ctx.offset) { + if let ScopeDef::Unknown = def { + if let Some(name_ref) = ctx.name_ref_syntax.as_ref() { + if &name_ref.syntax().text() == name.to_string().as_str() { // for `use self::foo<|>`, don't suggest `foo` as a completion tested_by!(dont_complete_current_use); continue; @@ -37,6 +34,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { } } } + acc.add_resolution(ctx, name.to_string(), &def); } } diff --git a/crates/ra_ide/src/completion/completion_context.rs b/crates/ra_ide/src/completion/completion_context.rs index 4894ea2f6..48d69f7e5 100644 --- a/crates/ra_ide/src/completion/completion_context.rs +++ b/crates/ra_ide/src/completion/completion_context.rs @@ -19,6 +19,7 @@ pub(crate) struct CompletionContext<'a> { pub(super) offset: TextUnit, pub(super) token: SyntaxToken, pub(super) module: Option, + pub(super) name_ref_syntax: Option, pub(super) function_syntax: Option, pub(super) use_item_syntax: Option, pub(super) record_lit_syntax: Option, @@ -69,6 +70,7 @@ impl<'a> CompletionContext<'a> { token, offset: position.offset, module, + name_ref_syntax: None, function_syntax: None, use_item_syntax: None, record_lit_syntax: None, @@ -142,6 +144,8 @@ impl<'a> CompletionContext<'a> { } fn classify_name_ref(&mut self, original_file: SourceFile, name_ref: ast::NameRef) { + self.name_ref_syntax = + find_node_at_offset(original_file.syntax(), name_ref.syntax().text_range().start()); let name_range = name_ref.syntax().text_range(); if name_ref.syntax().parent().and_then(ast::RecordField::cast).is_some() { self.record_lit_syntax = find_node_at_offset(original_file.syntax(), self.offset); -- cgit v1.2.3 From 02f79e37ca1c4a617a46b85bf897dffbf4abed9e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 21 Dec 2019 17:26:05 +0100 Subject: Remove import source map --- crates/ra_ide/src/change.rs | 2 -- 1 file changed, 2 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/change.rs b/crates/ra_ide/src/change.rs index 4a76d1dd8..387a9cafb 100644 --- a/crates/ra_ide/src/change.rs +++ b/crates/ra_ide/src/change.rs @@ -270,7 +270,6 @@ impl RootDatabase { self.query(hir::db::AstIdMapQuery).sweep(sweep); - self.query(hir::db::RawItemsWithSourceMapQuery).sweep(sweep); self.query(hir::db::BodyWithSourceMapQuery).sweep(sweep); self.query(hir::db::ExprScopesQuery).sweep(sweep); @@ -309,7 +308,6 @@ impl RootDatabase { hir::db::StructDataQuery hir::db::EnumDataQuery hir::db::TraitDataQuery - hir::db::RawItemsWithSourceMapQuery hir::db::RawItemsQuery hir::db::CrateDefMapQuery hir::db::GenericParamsQuery -- cgit v1.2.3 From d4b135f38c8c0050768c50e62043ddca5f09079a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 21 Dec 2019 18:45:46 +0100 Subject: Optimize and profile --- crates/ra_ide/src/inlay_hints.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/inlay_hints.rs b/crates/ra_ide/src/inlay_hints.rs index 3154df457..c5e406977 100644 --- a/crates/ra_ide/src/inlay_hints.rs +++ b/crates/ra_ide/src/inlay_hints.rs @@ -1,12 +1,15 @@ //! FIXME: write short doc here -use crate::{db::RootDatabase, FileId}; use hir::{HirDisplay, SourceAnalyzer}; +use once_cell::unsync::Lazy; +use ra_prof::profile; use ra_syntax::{ ast::{self, AstNode, TypeAscriptionOwner}, match_ast, SmolStr, SourceFile, SyntaxKind, SyntaxNode, TextRange, }; +use crate::{db::RootDatabase, FileId}; + #[derive(Debug, PartialEq, Eq)] pub enum InlayKind { TypeHint, @@ -27,7 +30,7 @@ pub(crate) fn inlay_hints( ) -> Vec { file.syntax() .descendants() - .map(|node| get_inlay_hints(db, file_id, &node, max_inlay_hint_length).unwrap_or_default()) + .flat_map(|node| get_inlay_hints(db, file_id, &node, max_inlay_hint_length)) .flatten() .collect() } @@ -38,7 +41,9 @@ fn get_inlay_hints( node: &SyntaxNode, max_inlay_hint_length: Option, ) -> Option> { - let analyzer = SourceAnalyzer::new(db, hir::InFile::new(file_id.into(), node), None); + let _p = profile("get_inlay_hints"); + let analyzer = + Lazy::new(|| SourceAnalyzer::new(db, hir::InFile::new(file_id.into(), node), None)); match_ast! { match node { ast::LetStmt(it) => { -- cgit v1.2.3 From e424545c0f5cbaf135c52764169ea20df7d07d35 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 22 Dec 2019 20:12:23 +0100 Subject: Rudimentary name resolution for local items --- crates/ra_ide/src/completion/complete_scope.rs | 37 ++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/completion/complete_scope.rs b/crates/ra_ide/src/completion/complete_scope.rs index d5739b58a..458d7525e 100644 --- a/crates/ra_ide/src/completion/complete_scope.rs +++ b/crates/ra_ide/src/completion/complete_scope.rs @@ -873,4 +873,41 @@ mod tests { "### ); } + + #[test] + fn completes_local_item() { + assert_debug_snapshot!( + do_reference_completion( + " + //- /main.rs + fn main() { + return f<|>; + fn frobnicate() {} + } + " + ), + @r###" + [ + CompletionItem { + label: "frobnicate()", + source_range: [23; 24), + delete: [23; 24), + insert: "frobnicate()$0", + kind: Function, + lookup: "frobnicate", + detail: "fn frobnicate()", + }, + CompletionItem { + label: "main()", + source_range: [23; 24), + delete: [23; 24), + insert: "main()$0", + kind: Function, + lookup: "main", + detail: "fn main()", + }, + ] + "### + ) + } } -- cgit v1.2.3