Add ConstParams to the ide layer

This commit is contained in:
Lukas Wirth 2021-01-01 10:07:01 +01:00
parent 0acdb73076
commit 18bf2e5af5
13 changed files with 66 additions and 5 deletions

View file

@ -204,7 +204,8 @@ impl<'a> AstTransform<'a> for QualifyPaths<'a> {
}
PathResolution::Local(_)
| PathResolution::TypeParam(_)
| PathResolution::SelfType(_) => None,
| PathResolution::SelfType(_)
| PathResolution::ConstParam(_) => None,
PathResolution::Macro(_) => None,
PathResolution::AssocItem(_) => None,
}

View file

@ -2376,7 +2376,6 @@ fn infer_operator_overload() {
);
}
#[test]
fn infer_const_params() {
check_infer(

View file

@ -24,6 +24,7 @@ pub enum SymbolKind {
Impl,
Field,
TypeParam,
ConstParam,
LifetimeParam,
ValueParam,
SelfParam,
@ -225,6 +226,7 @@ impl TryToNav for Definition {
Definition::TypeParam(it) => Some(it.to_nav(db)),
Definition::LifetimeParam(it) => Some(it.to_nav(db)),
Definition::Label(it) => Some(it.to_nav(db)),
Definition::ConstParam(it) => Some(it.to_nav(db)),
}
}
}
@ -485,6 +487,23 @@ impl ToNav for hir::LifetimeParam {
}
}
impl ToNav for hir::ConstParam {
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
let src = self.source(db);
let full_range = src.value.syntax().text_range();
NavigationTarget {
file_id: src.file_id.original_file(db),
name: self.name(db).to_string().into(),
kind: Some(SymbolKind::ConstParam),
full_range,
focus_range: src.value.name().map(|n| n.syntax().text_range()),
container_name: None,
description: None,
docs: None,
}
}
}
/// Get a description of a symbol.
///
/// e.g. `struct Name`, `enum Name`, `fn Name`

View file

@ -193,6 +193,7 @@ fn rewrite_intra_doc_link(
Definition::SelfType(_)
| Definition::Local(_)
| Definition::TypeParam(_)
| Definition::ConstParam(_)
| Definition::LifetimeParam(_)
| Definition::Label(_) => return None,
}?;

View file

@ -370,7 +370,10 @@ fn hover_for_definition(db: &RootDatabase, def: Definition) -> Option<Markup> {
Adt::Enum(it) => from_def_source(db, it, mod_path),
})
}
Definition::TypeParam(_) | Definition::LifetimeParam(_) | Definition::Label(_) => {
Definition::TypeParam(_)
| Definition::LifetimeParam(_)
| Definition::ConstParam(_)
| Definition::Label(_) => {
// FIXME: Hover for generic param
None
}

View file

@ -1144,4 +1144,20 @@ fn foo<'a>() -> &'a () {
"#]],
);
}
#[test]
fn test_find_const_param() {
check(
r#"
fn foo<const FOO<|>: usize>() -> usize {
FOO
}
"#,
expect![[r#"
FOO ConstParam FileId(0) 7..23 13..16 Other
FileId(0) 42..45 Other
"#]],
);
}
}

View file

@ -819,6 +819,7 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight {
},
Definition::SelfType(_) => HighlightTag::Symbol(SymbolKind::Impl),
Definition::TypeParam(_) => HighlightTag::Symbol(SymbolKind::TypeParam),
Definition::ConstParam(_) => HighlightTag::Symbol(SymbolKind::ConstParam),
Definition::Local(local) => {
let tag = if local.is_param(db) {
HighlightTag::Symbol(SymbolKind::ValueParam)

View file

@ -77,6 +77,7 @@ impl HighlightTag {
SymbolKind::Function => "function",
SymbolKind::TypeAlias => "type_alias",
SymbolKind::TypeParam => "type_param",
SymbolKind::ConstParam => "const_param",
SymbolKind::LifetimeParam => "lifetime",
SymbolKind::Macro => "macro",
SymbolKind::Local => "variable",

View file

@ -118,6 +118,10 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
<span class="keyword control">loop</span> <span class="punctuation">{</span><span class="punctuation">}</span>
<span class="punctuation">}</span>
<span class="keyword">fn</span> <span class="function declaration">const_param</span><span class="punctuation">&lt;</span><span class="keyword">const</span> <span class="const_param declaration">FOO</span><span class="punctuation">:</span> <span class="builtin_type">usize</span><span class="punctuation">&gt;</span><span class="punctuation">(</span><span class="punctuation">)</span> <span class="operator">-&gt;</span> <span class="builtin_type">usize</span> <span class="punctuation">{</span>
<span class="const_param">FOO</span>
<span class="punctuation">}</span>
<span class="keyword">use</span> <span class="module">ops</span><span class="operator">::</span><span class="trait">Fn</span><span class="punctuation">;</span>
<span class="keyword">fn</span> <span class="function declaration">baz</span><span class="punctuation">&lt;</span><span class="type_param declaration">F</span><span class="punctuation">:</span> <span class="trait">Fn</span><span class="punctuation">(</span><span class="punctuation">)</span> <span class="operator">-&gt;</span> <span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">&gt;</span><span class="punctuation">(</span><span class="value_param declaration callable">f</span><span class="punctuation">:</span> <span class="type_param">F</span><span class="punctuation">)</span> <span class="punctuation">{</span>
<span class="value_param callable">f</span><span class="punctuation">(</span><span class="punctuation">)</span>

View file

@ -91,6 +91,10 @@ fn never() -> ! {
loop {}
}
fn const_param<const FOO: usize>() -> usize {
FOO
}
use ops::Fn;
fn baz<F: Fn() -> ()>(f: F) {
f()

View file

@ -6,8 +6,8 @@
// FIXME: this badly needs rename/rewrite (matklad, 2020-02-06).
use hir::{
db::HirDatabase, Crate, Field, HasVisibility, Impl, Label, LifetimeParam, Local, MacroDef,
Module, ModuleDef, Name, PathResolution, Semantics, TypeParam, Visibility,
db::HirDatabase, ConstParam, Crate, Field, HasVisibility, Impl, Label, LifetimeParam, Local,
MacroDef, Module, ModuleDef, Name, PathResolution, Semantics, TypeParam, Visibility,
};
use syntax::{
ast::{self, AstNode},
@ -26,6 +26,7 @@ pub enum Definition {
Local(Local),
TypeParam(TypeParam),
LifetimeParam(LifetimeParam),
ConstParam(ConstParam),
Label(Label),
}
@ -39,6 +40,7 @@ impl Definition {
Definition::Local(it) => Some(it.module(db)),
Definition::TypeParam(it) => Some(it.module(db)),
Definition::LifetimeParam(it) => Some(it.module(db)),
Definition::ConstParam(it) => Some(it.module(db)),
Definition::Label(it) => Some(it.module(db)),
}
}
@ -52,6 +54,7 @@ impl Definition {
Definition::Local(_) => None,
Definition::TypeParam(_) => None,
Definition::LifetimeParam(_) => None,
Definition::ConstParam(_) => None,
Definition::Label(_) => None,
}
}
@ -79,6 +82,7 @@ impl Definition {
Definition::Local(it) => it.name(db)?,
Definition::TypeParam(it) => it.name(db),
Definition::LifetimeParam(it) => it.name(db),
Definition::ConstParam(it) => it.name(db),
Definition::Label(it) => it.name(db),
};
Some(name)
@ -233,6 +237,10 @@ impl NameClass {
let def = sema.to_def(&it)?;
Some(NameClass::Definition(Definition::TypeParam(def)))
},
ast::ConstParam(it) => {
let def = sema.to_def(&it)?;
Some(NameClass::Definition(Definition::ConstParam(def)))
},
_ => None,
}
}
@ -417,6 +425,7 @@ impl From<PathResolution> for Definition {
PathResolution::TypeParam(par) => Definition::TypeParam(par),
PathResolution::Macro(def) => Definition::Macro(def),
PathResolution::SelfType(impl_def) => Definition::SelfType(impl_def),
PathResolution::ConstParam(par) => Definition::ConstParam(par),
}
}
}

View file

@ -44,6 +44,7 @@ define_semantic_token_types![
(ESCAPE_SEQUENCE, "escapeSequence"),
(FORMAT_SPECIFIER, "formatSpecifier"),
(GENERIC, "generic"),
(CONST_PARAMETER, "constParameter"),
(LIFETIME, "lifetime"),
(LABEL, "label"),
(PUNCTUATION, "punctuation"),

View file

@ -42,6 +42,7 @@ pub(crate) fn symbol_kind(symbol_kind: SymbolKind) -> lsp_types::SymbolKind {
SymbolKind::Field => lsp_types::SymbolKind::Field,
SymbolKind::Static => lsp_types::SymbolKind::Constant,
SymbolKind::Const => lsp_types::SymbolKind::Constant,
SymbolKind::ConstParam => lsp_types::SymbolKind::Constant,
SymbolKind::Impl => lsp_types::SymbolKind::Object,
SymbolKind::Local
| SymbolKind::SelfParam
@ -378,6 +379,7 @@ fn semantic_token_type_and_modifiers(
SymbolKind::Impl => lsp_types::SemanticTokenType::TYPE,
SymbolKind::Field => lsp_types::SemanticTokenType::PROPERTY,
SymbolKind::TypeParam => lsp_types::SemanticTokenType::TYPE_PARAMETER,
SymbolKind::ConstParam => semantic_tokens::CONST_PARAMETER,
SymbolKind::LifetimeParam => semantic_tokens::LIFETIME,
SymbolKind::Label => semantic_tokens::LABEL,
SymbolKind::ValueParam => lsp_types::SemanticTokenType::PARAMETER,