From 1e6ba1901550fb1610a1a464c48ec358cd3c339c Mon Sep 17 00:00:00 2001 From: Pascal Hertleif Date: Mon, 27 May 2019 11:26:15 +0200 Subject: [PATCH] Make rainbows optional --- crates/ra_syntax/src/syntax_text.rs | 3 ++- docs/user/features.md | 9 +++++++++ editors/code/package.json | 5 +++++ editors/code/src/config.ts | 7 +++++++ editors/code/src/highlighting.ts | 11 ++++++----- 5 files changed, 29 insertions(+), 6 deletions(-) diff --git a/crates/ra_syntax/src/syntax_text.rs b/crates/ra_syntax/src/syntax_text.rs index bff1ed5a0d..c9038cd5c0 100644 --- a/crates/ra_syntax/src/syntax_text.rs +++ b/crates/ra_syntax/src/syntax_text.rs @@ -35,7 +35,8 @@ impl<'a> SyntaxText<'a> { } pub fn to_smol_string(&self) -> SmolStr { - // TODO: `impl iter::FromIterator<&str> for SmolStr` + // FIXME: use `self.chunks().collect()` here too once + // https://github.com/matklad/smol_str/pull/12 is merged and published self.to_string().into() } diff --git a/docs/user/features.md b/docs/user/features.md index 22470bc563..b6e6008c42 100644 --- a/docs/user/features.md +++ b/docs/user/features.md @@ -470,3 +470,12 @@ There also snippet completions: - `tfn` -> `#[test] fn f(){}` +### Code highlighting + +Experimental feature to let rust-analyzer highlight Rust code instead of using the +default highlighter. + +#### Rainbow highlighting + +Experimental feature that, given code highlighting using rust-analyzer is +active, will pick unique colors for identifiers. diff --git a/editors/code/package.json b/editors/code/package.json index d8ba914f56..05c8083949 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -164,6 +164,11 @@ "default": false, "description": "Highlight Rust code (overrides built-in syntax highlighting)" }, + "rust-analyzer.rainbowHighlightingOn": { + "type": "boolean", + "default": false, + "description": "When highlighting Rust code, use a unique color per identifier" + }, "rust-analyzer.showWorkspaceLoadedNotification": { "type": "boolean", "default": true, diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts index 481a5e5f18..8d73a6b340 100644 --- a/editors/code/src/config.ts +++ b/editors/code/src/config.ts @@ -15,6 +15,7 @@ export interface CargoWatchOptions { export class Config { public highlightingOn = true; + public rainbowHighlightingOn = false; public enableEnhancedTyping = true; public raLspServerPath = RA_LSP_DEBUG || 'ra_lsp_server'; public showWorkspaceLoadedNotification = true; @@ -39,6 +40,12 @@ export class Config { this.highlightingOn = config.get('highlightingOn') as boolean; } + if (config.has('rainbowHighlightingOn')) { + this.rainbowHighlightingOn = config.get( + 'rainbowHighlightingOn' + ) as boolean; + } + if (config.has('showWorkspaceLoadedNotification')) { this.showWorkspaceLoadedNotification = config.get( 'showWorkspaceLoadedNotification' diff --git a/editors/code/src/highlighting.ts b/editors/code/src/highlighting.ts index 4597db08fe..52a0bd4bb1 100644 --- a/editors/code/src/highlighting.ts +++ b/editors/code/src/highlighting.ts @@ -7,7 +7,7 @@ import { Server } from './server'; export interface Decoration { range: lc.Range; tag: string; - id?: string; + bindingHash?: string; } // Based on this HSL-based color generator: https://gist.github.com/bendc/76c48ce53299e6078a76 @@ -92,6 +92,7 @@ export class Highlighter { const byTag: Map = new Map(); const colorfulIdents: Map = new Map(); + const rainbowTime = Server.config.rainbowHighlightingOn; for (const tag of this.decorations.keys()) { byTag.set(tag, []); @@ -102,12 +103,12 @@ export class Highlighter { continue; } - if (d.id) { - if (!colorfulIdents.has(d.id)) { - colorfulIdents.set(d.id, []); + if (rainbowTime && d.bindingHash) { + if (!colorfulIdents.has(d.bindingHash)) { + colorfulIdents.set(d.bindingHash, []); } colorfulIdents - .get(d.id)! + .get(d.bindingHash)! .push( Server.client.protocol2CodeConverter.asRange(d.range) );