aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide')
-rw-r--r--crates/ra_ide/src/lib.rs7
-rw-r--r--crates/ra_ide/src/syntax_highlighting.rs49
2 files changed, 53 insertions, 3 deletions
diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs
index 82e10bc7e..c02bb08a0 100644
--- a/crates/ra_ide/src/lib.rs
+++ b/crates/ra_ide/src/lib.rs
@@ -430,6 +430,13 @@ impl Analysis {
430 self.with_db(|db| syntax_highlighting::highlight(db, file_id)) 430 self.with_db(|db| syntax_highlighting::highlight(db, file_id))
431 } 431 }
432 432
433 /// Computes syntax highlighting for the given file range.
434 pub fn highlight_range(&self, frange: FileRange) -> Cancelable<Vec<HighlightedRange>> {
435 self.with_db(|db| {
436 syntax_highlighting::highlight_range(db, frange.file_id, Some(frange.range))
437 })
438 }
439
433 /// Computes syntax highlighting for the given file. 440 /// Computes syntax highlighting for the given file.
434 pub fn highlight_as_html(&self, file_id: FileId, rainbow: bool) -> Cancelable<String> { 441 pub fn highlight_as_html(&self, file_id: FileId, rainbow: bool) -> Cancelable<String> {
435 self.with_db(|db| syntax_highlighting::highlight_as_html(db, file_id, rainbow)) 442 self.with_db(|db| syntax_highlighting::highlight_as_html(db, file_id, rainbow))
diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs
index 812229b4e..22c84561f 100644
--- a/crates/ra_ide/src/syntax_highlighting.rs
+++ b/crates/ra_ide/src/syntax_highlighting.rs
@@ -5,8 +5,8 @@ use ra_db::SourceDatabase;
5use ra_ide_db::{defs::NameDefinition, RootDatabase}; 5use ra_ide_db::{defs::NameDefinition, RootDatabase};
6use ra_prof::profile; 6use ra_prof::profile;
7use ra_syntax::{ 7use ra_syntax::{
8 ast, AstNode, Direction, SyntaxElement, SyntaxKind, SyntaxKind::*, SyntaxToken, TextRange, 8 ast, AstNode, Direction, NodeOrToken, SyntaxElement, SyntaxKind, SyntaxKind::*, SyntaxToken,
9 WalkEvent, T, 9 TextRange, WalkEvent, T,
10}; 10};
11use rustc_hash::FxHashMap; 11use rustc_hash::FxHashMap;
12 12
@@ -69,6 +69,16 @@ fn is_control_keyword(kind: SyntaxKind) -> bool {
69 69
70pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRange> { 70pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRange> {
71 let _p = profile("highlight"); 71 let _p = profile("highlight");
72 highlight_range(db, file_id, None)
73}
74
75pub(crate) fn highlight_range(
76 db: &RootDatabase,
77 file_id: FileId,
78 range: Option<TextRange>,
79) -> Vec<HighlightedRange> {
80 let _p = profile("highlight_range");
81
72 let parse = db.parse(file_id); 82 let parse = db.parse(file_id);
73 let root = parse.tree().syntax().clone(); 83 let root = parse.tree().syntax().clone();
74 84
@@ -79,6 +89,15 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
79 89
80 let mut in_macro_call = None; 90 let mut in_macro_call = None;
81 91
92 // Determine the root based on the range
93 let root = match range {
94 Some(range) => match root.covering_element(range) {
95 NodeOrToken::Node(node) => node,
96 NodeOrToken::Token(token) => token.parent(),
97 },
98 None => root,
99 };
100
82 for event in root.preorder_with_tokens() { 101 for event in root.preorder_with_tokens() {
83 match event { 102 match event {
84 WalkEvent::Enter(node) => match node.kind() { 103 WalkEvent::Enter(node) => match node.kind() {
@@ -374,7 +393,10 @@ mod tests {
374 393
375 use test_utils::{assert_eq_text, project_dir, read_text}; 394 use test_utils::{assert_eq_text, project_dir, read_text};
376 395
377 use crate::mock_analysis::{single_file, MockAnalysis}; 396 use crate::{
397 mock_analysis::{single_file, MockAnalysis},
398 FileRange, TextRange,
399 };
378 400
379 #[test] 401 #[test]
380 fn test_highlighting() { 402 fn test_highlighting() {
@@ -475,4 +497,25 @@ fn bar() {
475 let _ = host.analysis().highlight(file_id).unwrap(); 497 let _ = host.analysis().highlight(file_id).unwrap();
476 // eprintln!("elapsed: {:?}", t.elapsed()); 498 // eprintln!("elapsed: {:?}", t.elapsed());
477 } 499 }
500
501 #[test]
502 fn test_ranges() {
503 let (analysis, file_id) = single_file(
504 r#"
505 #[derive(Clone, Debug)]
506 struct Foo {
507 pub x: i32,
508 pub y: i32,
509 }"#,
510 );
511
512 let highlights = &analysis
513 .highlight_range(FileRange {
514 file_id,
515 range: TextRange::offset_len(82.into(), 1.into()), // "x"
516 })
517 .unwrap();
518
519 assert_eq!(highlights[0].tag, "field");
520 }
478} 521}