mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 04:44:57 +00:00
Add ConstParams to the ide layer
This commit is contained in:
parent
0acdb73076
commit
18bf2e5af5
13 changed files with 66 additions and 5 deletions
|
@ -204,7 +204,8 @@ impl<'a> AstTransform<'a> for QualifyPaths<'a> {
|
||||||
}
|
}
|
||||||
PathResolution::Local(_)
|
PathResolution::Local(_)
|
||||||
| PathResolution::TypeParam(_)
|
| PathResolution::TypeParam(_)
|
||||||
| PathResolution::SelfType(_) => None,
|
| PathResolution::SelfType(_)
|
||||||
|
| PathResolution::ConstParam(_) => None,
|
||||||
PathResolution::Macro(_) => None,
|
PathResolution::Macro(_) => None,
|
||||||
PathResolution::AssocItem(_) => None,
|
PathResolution::AssocItem(_) => None,
|
||||||
}
|
}
|
||||||
|
|
|
@ -2376,7 +2376,6 @@ fn infer_operator_overload() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn infer_const_params() {
|
fn infer_const_params() {
|
||||||
check_infer(
|
check_infer(
|
||||||
|
|
|
@ -24,6 +24,7 @@ pub enum SymbolKind {
|
||||||
Impl,
|
Impl,
|
||||||
Field,
|
Field,
|
||||||
TypeParam,
|
TypeParam,
|
||||||
|
ConstParam,
|
||||||
LifetimeParam,
|
LifetimeParam,
|
||||||
ValueParam,
|
ValueParam,
|
||||||
SelfParam,
|
SelfParam,
|
||||||
|
@ -225,6 +226,7 @@ impl TryToNav for Definition {
|
||||||
Definition::TypeParam(it) => Some(it.to_nav(db)),
|
Definition::TypeParam(it) => Some(it.to_nav(db)),
|
||||||
Definition::LifetimeParam(it) => Some(it.to_nav(db)),
|
Definition::LifetimeParam(it) => Some(it.to_nav(db)),
|
||||||
Definition::Label(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.
|
/// Get a description of a symbol.
|
||||||
///
|
///
|
||||||
/// e.g. `struct Name`, `enum Name`, `fn Name`
|
/// e.g. `struct Name`, `enum Name`, `fn Name`
|
||||||
|
|
|
@ -193,6 +193,7 @@ fn rewrite_intra_doc_link(
|
||||||
Definition::SelfType(_)
|
Definition::SelfType(_)
|
||||||
| Definition::Local(_)
|
| Definition::Local(_)
|
||||||
| Definition::TypeParam(_)
|
| Definition::TypeParam(_)
|
||||||
|
| Definition::ConstParam(_)
|
||||||
| Definition::LifetimeParam(_)
|
| Definition::LifetimeParam(_)
|
||||||
| Definition::Label(_) => return None,
|
| Definition::Label(_) => return None,
|
||||||
}?;
|
}?;
|
||||||
|
|
|
@ -370,7 +370,10 @@ fn hover_for_definition(db: &RootDatabase, def: Definition) -> Option<Markup> {
|
||||||
Adt::Enum(it) => from_def_source(db, it, mod_path),
|
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
|
// FIXME: Hover for generic param
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
"#]],
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -819,6 +819,7 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight {
|
||||||
},
|
},
|
||||||
Definition::SelfType(_) => HighlightTag::Symbol(SymbolKind::Impl),
|
Definition::SelfType(_) => HighlightTag::Symbol(SymbolKind::Impl),
|
||||||
Definition::TypeParam(_) => HighlightTag::Symbol(SymbolKind::TypeParam),
|
Definition::TypeParam(_) => HighlightTag::Symbol(SymbolKind::TypeParam),
|
||||||
|
Definition::ConstParam(_) => HighlightTag::Symbol(SymbolKind::ConstParam),
|
||||||
Definition::Local(local) => {
|
Definition::Local(local) => {
|
||||||
let tag = if local.is_param(db) {
|
let tag = if local.is_param(db) {
|
||||||
HighlightTag::Symbol(SymbolKind::ValueParam)
|
HighlightTag::Symbol(SymbolKind::ValueParam)
|
||||||
|
|
|
@ -77,6 +77,7 @@ impl HighlightTag {
|
||||||
SymbolKind::Function => "function",
|
SymbolKind::Function => "function",
|
||||||
SymbolKind::TypeAlias => "type_alias",
|
SymbolKind::TypeAlias => "type_alias",
|
||||||
SymbolKind::TypeParam => "type_param",
|
SymbolKind::TypeParam => "type_param",
|
||||||
|
SymbolKind::ConstParam => "const_param",
|
||||||
SymbolKind::LifetimeParam => "lifetime",
|
SymbolKind::LifetimeParam => "lifetime",
|
||||||
SymbolKind::Macro => "macro",
|
SymbolKind::Macro => "macro",
|
||||||
SymbolKind::Local => "variable",
|
SymbolKind::Local => "variable",
|
||||||
|
|
|
@ -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="keyword control">loop</span> <span class="punctuation">{</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"><</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">></span><span class="punctuation">(</span><span class="punctuation">)</span> <span class="operator">-></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">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"><</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">-></span> <span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">></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="keyword">fn</span> <span class="function declaration">baz</span><span class="punctuation"><</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">-></span> <span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">></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>
|
<span class="value_param callable">f</span><span class="punctuation">(</span><span class="punctuation">)</span>
|
||||||
|
|
|
@ -91,6 +91,10 @@ fn never() -> ! {
|
||||||
loop {}
|
loop {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn const_param<const FOO: usize>() -> usize {
|
||||||
|
FOO
|
||||||
|
}
|
||||||
|
|
||||||
use ops::Fn;
|
use ops::Fn;
|
||||||
fn baz<F: Fn() -> ()>(f: F) {
|
fn baz<F: Fn() -> ()>(f: F) {
|
||||||
f()
|
f()
|
||||||
|
|
|
@ -6,8 +6,8 @@
|
||||||
// FIXME: this badly needs rename/rewrite (matklad, 2020-02-06).
|
// FIXME: this badly needs rename/rewrite (matklad, 2020-02-06).
|
||||||
|
|
||||||
use hir::{
|
use hir::{
|
||||||
db::HirDatabase, Crate, Field, HasVisibility, Impl, Label, LifetimeParam, Local, MacroDef,
|
db::HirDatabase, ConstParam, Crate, Field, HasVisibility, Impl, Label, LifetimeParam, Local,
|
||||||
Module, ModuleDef, Name, PathResolution, Semantics, TypeParam, Visibility,
|
MacroDef, Module, ModuleDef, Name, PathResolution, Semantics, TypeParam, Visibility,
|
||||||
};
|
};
|
||||||
use syntax::{
|
use syntax::{
|
||||||
ast::{self, AstNode},
|
ast::{self, AstNode},
|
||||||
|
@ -26,6 +26,7 @@ pub enum Definition {
|
||||||
Local(Local),
|
Local(Local),
|
||||||
TypeParam(TypeParam),
|
TypeParam(TypeParam),
|
||||||
LifetimeParam(LifetimeParam),
|
LifetimeParam(LifetimeParam),
|
||||||
|
ConstParam(ConstParam),
|
||||||
Label(Label),
|
Label(Label),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,6 +40,7 @@ impl Definition {
|
||||||
Definition::Local(it) => Some(it.module(db)),
|
Definition::Local(it) => Some(it.module(db)),
|
||||||
Definition::TypeParam(it) => Some(it.module(db)),
|
Definition::TypeParam(it) => Some(it.module(db)),
|
||||||
Definition::LifetimeParam(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)),
|
Definition::Label(it) => Some(it.module(db)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -52,6 +54,7 @@ impl Definition {
|
||||||
Definition::Local(_) => None,
|
Definition::Local(_) => None,
|
||||||
Definition::TypeParam(_) => None,
|
Definition::TypeParam(_) => None,
|
||||||
Definition::LifetimeParam(_) => None,
|
Definition::LifetimeParam(_) => None,
|
||||||
|
Definition::ConstParam(_) => None,
|
||||||
Definition::Label(_) => None,
|
Definition::Label(_) => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -79,6 +82,7 @@ impl Definition {
|
||||||
Definition::Local(it) => it.name(db)?,
|
Definition::Local(it) => it.name(db)?,
|
||||||
Definition::TypeParam(it) => it.name(db),
|
Definition::TypeParam(it) => it.name(db),
|
||||||
Definition::LifetimeParam(it) => it.name(db),
|
Definition::LifetimeParam(it) => it.name(db),
|
||||||
|
Definition::ConstParam(it) => it.name(db),
|
||||||
Definition::Label(it) => it.name(db),
|
Definition::Label(it) => it.name(db),
|
||||||
};
|
};
|
||||||
Some(name)
|
Some(name)
|
||||||
|
@ -233,6 +237,10 @@ impl NameClass {
|
||||||
let def = sema.to_def(&it)?;
|
let def = sema.to_def(&it)?;
|
||||||
Some(NameClass::Definition(Definition::TypeParam(def)))
|
Some(NameClass::Definition(Definition::TypeParam(def)))
|
||||||
},
|
},
|
||||||
|
ast::ConstParam(it) => {
|
||||||
|
let def = sema.to_def(&it)?;
|
||||||
|
Some(NameClass::Definition(Definition::ConstParam(def)))
|
||||||
|
},
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -417,6 +425,7 @@ impl From<PathResolution> for Definition {
|
||||||
PathResolution::TypeParam(par) => Definition::TypeParam(par),
|
PathResolution::TypeParam(par) => Definition::TypeParam(par),
|
||||||
PathResolution::Macro(def) => Definition::Macro(def),
|
PathResolution::Macro(def) => Definition::Macro(def),
|
||||||
PathResolution::SelfType(impl_def) => Definition::SelfType(impl_def),
|
PathResolution::SelfType(impl_def) => Definition::SelfType(impl_def),
|
||||||
|
PathResolution::ConstParam(par) => Definition::ConstParam(par),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,6 +44,7 @@ define_semantic_token_types![
|
||||||
(ESCAPE_SEQUENCE, "escapeSequence"),
|
(ESCAPE_SEQUENCE, "escapeSequence"),
|
||||||
(FORMAT_SPECIFIER, "formatSpecifier"),
|
(FORMAT_SPECIFIER, "formatSpecifier"),
|
||||||
(GENERIC, "generic"),
|
(GENERIC, "generic"),
|
||||||
|
(CONST_PARAMETER, "constParameter"),
|
||||||
(LIFETIME, "lifetime"),
|
(LIFETIME, "lifetime"),
|
||||||
(LABEL, "label"),
|
(LABEL, "label"),
|
||||||
(PUNCTUATION, "punctuation"),
|
(PUNCTUATION, "punctuation"),
|
||||||
|
|
|
@ -42,6 +42,7 @@ pub(crate) fn symbol_kind(symbol_kind: SymbolKind) -> lsp_types::SymbolKind {
|
||||||
SymbolKind::Field => lsp_types::SymbolKind::Field,
|
SymbolKind::Field => lsp_types::SymbolKind::Field,
|
||||||
SymbolKind::Static => lsp_types::SymbolKind::Constant,
|
SymbolKind::Static => lsp_types::SymbolKind::Constant,
|
||||||
SymbolKind::Const => lsp_types::SymbolKind::Constant,
|
SymbolKind::Const => lsp_types::SymbolKind::Constant,
|
||||||
|
SymbolKind::ConstParam => lsp_types::SymbolKind::Constant,
|
||||||
SymbolKind::Impl => lsp_types::SymbolKind::Object,
|
SymbolKind::Impl => lsp_types::SymbolKind::Object,
|
||||||
SymbolKind::Local
|
SymbolKind::Local
|
||||||
| SymbolKind::SelfParam
|
| SymbolKind::SelfParam
|
||||||
|
@ -378,6 +379,7 @@ fn semantic_token_type_and_modifiers(
|
||||||
SymbolKind::Impl => lsp_types::SemanticTokenType::TYPE,
|
SymbolKind::Impl => lsp_types::SemanticTokenType::TYPE,
|
||||||
SymbolKind::Field => lsp_types::SemanticTokenType::PROPERTY,
|
SymbolKind::Field => lsp_types::SemanticTokenType::PROPERTY,
|
||||||
SymbolKind::TypeParam => lsp_types::SemanticTokenType::TYPE_PARAMETER,
|
SymbolKind::TypeParam => lsp_types::SemanticTokenType::TYPE_PARAMETER,
|
||||||
|
SymbolKind::ConstParam => semantic_tokens::CONST_PARAMETER,
|
||||||
SymbolKind::LifetimeParam => semantic_tokens::LIFETIME,
|
SymbolKind::LifetimeParam => semantic_tokens::LIFETIME,
|
||||||
SymbolKind::Label => semantic_tokens::LABEL,
|
SymbolKind::Label => semantic_tokens::LABEL,
|
||||||
SymbolKind::ValueParam => lsp_types::SemanticTokenType::PARAMETER,
|
SymbolKind::ValueParam => lsp_types::SemanticTokenType::PARAMETER,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue