From 9ade271a67a9fae0d89f8138970679c9730e9fce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauren=C8=9Biu=20Nicola?= Date: Tue, 21 May 2019 14:04:54 +0300 Subject: [PATCH] Use ThemeColor and add support for light themes --- crates/ra_ide_api/src/syntax_highlighting.rs | 12 +- editors/code/package.json | 119 +++++++++++++++++++ editors/code/src/highlighting.ts | 35 ++++-- 3 files changed, 152 insertions(+), 14 deletions(-) diff --git a/crates/ra_ide_api/src/syntax_highlighting.rs b/crates/ra_ide_api/src/syntax_highlighting.rs index a03b13839b..ab69a43743 100644 --- a/crates/ra_ide_api/src/syntax_highlighting.rs +++ b/crates/ra_ide_api/src/syntax_highlighting.rs @@ -1,6 +1,6 @@ use rustc_hash::FxHashSet; -use ra_syntax::{ast, AstNode, TextRange, Direction, SyntaxKind::*, SyntaxElement, T}; +use ra_syntax::{ast, AstNode, TextRange, Direction, SyntaxKind, SyntaxKind::*, SyntaxElement, T}; use ra_db::SourceDatabase; use crate::{FileId, db::RootDatabase}; @@ -11,6 +11,14 @@ pub struct HighlightedRange { pub tag: &'static str, } +fn is_control_keyword(kind: SyntaxKind) -> bool { + match kind { + FOR_KW | LOOP_KW | WHILE_KW | CONTINUE_KW | BREAK_KW | IF_KW | ELSE_KW | MATCH_KW + | RETURN_KW => true, + _ => false, + } +} + pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec { let source_file = db.parse(file_id); @@ -29,6 +37,8 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec "function", INT_NUMBER | FLOAT_NUMBER | CHAR | BYTE => "literal", LIFETIME => "parameter", + UNSAFE_KW => "unsafe", + k if is_control_keyword(k) => "control", k if k.is_keyword() => "keyword", _ => { if let Some(macro_call) = node.as_node().and_then(ast::MacroCall::cast) { diff --git a/editors/code/package.json b/editors/code/package.json index 015b912b3c..c72037da91 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -268,6 +268,125 @@ }, "pattern": "$rustc" } + ], + "colors": [ + { + "id": "ralsp.background", + "description": "Background color", + "defaults": { + "dark": "#3F3F3F", + "light": "#001080", + "highContrast": "#000000" + } + }, + { + "id": "ralsp.comment", + "description": "Color for comments", + "defaults": { + "dark": "#7F9F7F", + "light": "#008000", + "highContrast": "#7CA668" + } + }, + { + "id": "ralsp.string", + "description": "Color for strings", + "defaults": { + "dark": "#CC9393", + "light": "#A31515", + "highContrast": "#CE9178" + } + }, + { + "id": "ralsp.unsafe", + "description": "Color for unsafe", + "defaults": { + "dark": "#FF3030", + "light": "#FF1010", + "highContrast": "#FF1010" + } + }, + { + "id": "ralsp.keyword", + "description": "Color for keywords", + "defaults": { + "dark": "#F0DFAF", + "light": "#0000FF", + "highContrast": "#569CD6" + } + }, + { + "id": "ralsp.control", + "description": "Color for control keywords", + "defaults": { + "dark": "#CF20FB", + "light": "#AF00DB", + "highContrast": "#C586C0" + } + }, + { + "id": "ralsp.function", + "description": "Color for functions", + "defaults": { + "dark": "#93E0E3", + "light": "#795E26", + "highContrast": "#DCDCAA" + } + }, + { + "id": "ralsp.parameter", + "description": "Color for parameters", + "defaults": { + "dark": "#94BFF3", + "light": "#001080", + "highContrast": "#9CDCFE" + } + }, + { + "id": "ralsp.builtin", + "description": "Color for builtins", + "defaults": { + "dark": "#DD6718", + "light": "#DD6718", + "highContrast": "#DD6718" + } + }, + { + "id": "ralsp.text", + "description": "Color for text", + "defaults": { + "dark": "#DCDCCC", + "light": "#000000", + "highContrast": "#FFFFFF" + } + }, + { + "id": "ralsp.attribute", + "description": "Color for attributes", + "defaults": { + "dark": "#BFEBBF", + "light": "#1F4B1F", + "highContrast": "#108010" + } + }, + { + "id": "ralsp.literal", + "description": "Color for literals", + "defaults": { + "dark": "#DFAF8F", + "light": "#09885A", + "highContrast": "#B5CEA8" + } + }, + { + "id": "ralsp.macro", + "description": "Color for DFAF8F", + "defaults": { + "dark": "#BFEBBF", + "light": "#DD6718", + "highContrast": "#ED7718" + } + } ] } } diff --git a/editors/code/src/highlighting.ts b/editors/code/src/highlighting.ts index 2521dff622..432f40ff40 100644 --- a/editors/code/src/highlighting.ts +++ b/editors/code/src/highlighting.ts @@ -13,23 +13,32 @@ export class Highlighter { string, vscode.TextEditorDecorationType > { - const decor = (color: string) => - vscode.window.createTextEditorDecorationType({ color }); + const colorContrib = ( + tag: string + ): [string, vscode.TextEditorDecorationType] => { + const color = new vscode.ThemeColor('ralsp.' + tag); + const decor = vscode.window.createTextEditorDecorationType({ + color + }); + return [tag, decor]; + }; const decorations: Iterable< [string, vscode.TextEditorDecorationType] > = [ - ['background', decor('#3F3F3F')], - ['comment', decor('#7F9F7F')], - ['string', decor('#CC9393')], - ['keyword', decor('#F0DFAF')], - ['function', decor('#93E0E3')], - ['parameter', decor('#94BFF3')], - ['builtin', decor('#DD6718')], - ['text', decor('#DCDCCC')], - ['attribute', decor('#BFEBBF')], - ['literal', decor('#DFAF8F')], - ['macro', decor('#DFAF8F')] + colorContrib('background'), + colorContrib('comment'), + colorContrib('string'), + colorContrib('unsafe'), + colorContrib('keyword'), + colorContrib('control'), + colorContrib('function'), + colorContrib('parameter'), + colorContrib('builtin'), + colorContrib('text'), + colorContrib('attribute'), + colorContrib('literal'), + colorContrib('macro') ]; return new Map(decorations);