diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 55d1298cfd..081974e2b6 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -68,6 +68,7 @@ pub use self::{ source_binder::{PathResolution, ScopeEntryWithSyntax, SourceAnalyzer}, source_id::{AstIdMap, ErasedFileAstId}, ty::{display::HirDisplay, ApplicationTy, CallableDef, Substs, TraitRef, Ty, TypeCtor}, + type_ref::Mutability, }; pub use self::code_model::{ diff --git a/crates/ra_ide_api/src/snapshots/highlighting.html b/crates/ra_ide_api/src/snapshots/highlighting.html index d79d35bf31..709816d0d3 100644 --- a/crates/ra_ide_api/src/snapshots/highlighting.html +++ b/crates/ra_ide_api/src/snapshots/highlighting.html @@ -1,21 +1,22 @@
#[derive(Clone, Debug)]
struct Foo {
@@ -32,9 +33,15 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padding: 0.4e
fn main() {
println!("Hello, {}!", 92);
- let mut vec = Vec::new();
+ 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 y = &mut x;
+ let z = &y;
+
+ y;
}
\ No newline at end of file
diff --git a/crates/ra_ide_api/src/snapshots/rainbow_highlighting.html b/crates/ra_ide_api/src/snapshots/rainbow_highlighting.html
index 729d129d07..ad3935b5dc 100644
--- a/crates/ra_ide_api/src/snapshots/rainbow_highlighting.html
+++ b/crates/ra_ide_api/src/snapshots/rainbow_highlighting.html
@@ -1,21 +1,22 @@
fn main() {
let hello = "hello";
diff --git a/crates/ra_ide_api/src/syntax_highlighting.rs b/crates/ra_ide_api/src/syntax_highlighting.rs
index d70ceb7d15..d84ae2cb26 100644
--- a/crates/ra_ide_api/src/syntax_highlighting.rs
+++ b/crates/ra_ide_api/src/syntax_highlighting.rs
@@ -1,9 +1,11 @@
use rustc_hash::{FxHashMap, FxHashSet};
+use hir::{Mutability, Ty};
use ra_db::SourceDatabase;
use ra_prof::profile;
use ra_syntax::{
- ast, AstNode, Direction, SmolStr, SyntaxElement, SyntaxKind, SyntaxKind::*, TextRange, T,
+ ast, AstNode, Direction, Pat, PatKind, SmolStr, SyntaxElement, SyntaxKind, SyntaxKind::*,
+ TextRange, T,
};
use crate::{db::RootDatabase, FileId};
@@ -30,6 +32,27 @@ fn is_control_keyword(kind: SyntaxKind) -> bool {
}
}
+fn is_variable_mutable(db: &RootDatabase, analyzer: &hir::SourceAnalyzer, pat: &Pat) -> bool {
+ let ty = analyzer.type_of_pat(db, pat).unwrap_or(Ty::Unknown);
+ let is_ty_mut = {
+ if let Some((_, mutability)) = ty.as_reference() {
+ match mutability {
+ Mutability::Shared => false,
+ Mutability::Mut => true,
+ }
+ } else {
+ false
+ }
+ };
+
+ let is_pat_mut = match pat.kind() {
+ PatKind::BindPat(bind_pat) => bind_pat.is_mutable(),
+ _ => false,
+ };
+
+ is_ty_mut || is_pat_mut
+}
+
pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec {
let _p = profile("highlight");
let parse = db.parse(file_id);
@@ -97,7 +120,11 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec "type",
Some(GenericParam(_)) => "type",
@@ -109,7 +136,8 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec {
if let Some(name) = node.as_node().and_then(ast::Name::cast) {
- if name.syntax().ancestors().any(|x| ast::BindPat::cast(x).is_some()) {
+ let analyzer = hir::SourceAnalyzer::new(db, file_id, name.syntax(), None);
+ if let Some(pat) = name.syntax().ancestors().find_map(Pat::cast) {
binding_hash = Some({
let text = name.syntax().text().to_smol_string();
let shadow_count =
@@ -117,7 +145,12 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec String {
const STYLE: &str = "
";
@@ -289,12 +323,18 @@ fn main() {
vec.push(Foo { x: 0, y: 1 });
}
unsafe { vec.set_len(0); }
+
+ let mut x = 42;
+ let y = &mut x;
+ let z = &y;
+
+ y;
}
"#
.trim(),
);
let dst_file = project_dir().join("crates/ra_ide_api/src/snapshots/highlighting.html");
- let actual_html = &analysis.highlight_as_html(file_id, true).unwrap();
+ let actual_html = &analysis.highlight_as_html(file_id, false).unwrap();
let expected_html = &read_text(&dst_file);
std::fs::write(dst_file, &actual_html).unwrap();
assert_eq_text!(expected_html, actual_html);
diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs
index 06d3ea7275..ff347a5670 100644
--- a/crates/ra_syntax/src/lib.rs
+++ b/crates/ra_syntax/src/lib.rs
@@ -38,7 +38,7 @@ use ra_text_edit::AtomTextEdit;
use crate::syntax_node::GreenNode;
pub use crate::{
- ast::AstNode,
+ ast::{AstNode, Pat, PatKind},
parsing::{classify_literal, tokenize, Token},
ptr::{AstPtr, SyntaxNodePtr},
syntax_error::{Location, SyntaxError, SyntaxErrorKind},
diff --git a/editors/code/package.json b/editors/code/package.json
index 052f0b3b3c..86076753ba 100644
--- a/editors/code/package.json
+++ b/editors/code/package.json
@@ -432,6 +432,15 @@
"highContrast": "#4EC9B0"
}
},
+ {
+ "id": "ralsp.variable.mut",
+ "description": "Color for mutable variables",
+ "defaults": {
+ "dark": "#4e65c9",
+ "light": "#263199",
+ "highContrast": "#4e65c9"
+ }
+ },
{
"id": "ralsp.module",
"description": "Color for modules",
diff --git a/editors/code/src/highlighting.ts b/editors/code/src/highlighting.ts
index 52a0bd4bb1..f3ed663656 100644
--- a/editors/code/src/highlighting.ts
+++ b/editors/code/src/highlighting.ts
@@ -56,6 +56,7 @@ export class Highlighter {
colorContrib('literal'),
colorContrib('macro'),
colorContrib('variable'),
+ colorContrib('variable.mut'),
colorContrib('field'),
colorContrib('module')
];