aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/syntax_highlighting
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide/src/syntax_highlighting')
-rw-r--r--crates/ra_ide/src/syntax_highlighting/html.rs4
-rw-r--r--crates/ra_ide/src/syntax_highlighting/injection.rs16
-rw-r--r--crates/ra_ide/src/syntax_highlighting/tags.rs2
-rw-r--r--crates/ra_ide/src/syntax_highlighting/tests.rs32
4 files changed, 47 insertions, 7 deletions
diff --git a/crates/ra_ide/src/syntax_highlighting/html.rs b/crates/ra_ide/src/syntax_highlighting/html.rs
index 5bada6252..99b6b25ab 100644
--- a/crates/ra_ide/src/syntax_highlighting/html.rs
+++ b/crates/ra_ide/src/syntax_highlighting/html.rs
@@ -19,7 +19,7 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo
19 ) 19 )
20 } 20 }
21 21
22 let ranges = highlight(db, file_id, None); 22 let ranges = highlight(db, file_id, None, false);
23 let text = parse.tree().syntax().to_string(); 23 let text = parse.tree().syntax().to_string();
24 let mut prev_pos = TextSize::from(0); 24 let mut prev_pos = TextSize::from(0);
25 let mut buf = String::new(); 25 let mut buf = String::new();
@@ -84,6 +84,8 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
84.variable { color: #DCDCCC; } 84.variable { color: #DCDCCC; }
85.format_specifier { color: #CC696B; } 85.format_specifier { color: #CC696B; }
86.mutable { text-decoration: underline; } 86.mutable { text-decoration: underline; }
87.unresolved_reference { color: #FC5555; }
88.escape_sequence { color: #94BFF3; }
87 89
88.keyword { color: #F0DFAF; font-weight: bold; } 90.keyword { color: #F0DFAF; font-weight: bold; }
89.keyword.unsafe { color: #BC8383; font-weight: bold; } 91.keyword.unsafe { color: #BC8383; font-weight: bold; }
diff --git a/crates/ra_ide/src/syntax_highlighting/injection.rs b/crates/ra_ide/src/syntax_highlighting/injection.rs
index 3575a0fc6..929a5cc5c 100644
--- a/crates/ra_ide/src/syntax_highlighting/injection.rs
+++ b/crates/ra_ide/src/syntax_highlighting/injection.rs
@@ -53,6 +53,10 @@ pub(super) fn highlight_injection(
53/// Mapping from extracted documentation code to original code 53/// Mapping from extracted documentation code to original code
54type RangesMap = BTreeMap<TextSize, TextSize>; 54type RangesMap = BTreeMap<TextSize, TextSize>;
55 55
56const RUSTDOC_FENCE: &'static str = "```";
57const RUSTDOC_FENCE_TOKENS: &[&'static str] =
58 &["", "rust", "should_panic", "ignore", "no_run", "compile_fail", "edition2015", "edition2018"];
59
56/// Extracts Rust code from documentation comments as well as a mapping from 60/// Extracts Rust code from documentation comments as well as a mapping from
57/// the extracted source code back to the original source ranges. 61/// the extracted source code back to the original source ranges.
58/// Lastly, a vector of new comment highlight ranges (spanning only the 62/// Lastly, a vector of new comment highlight ranges (spanning only the
@@ -67,6 +71,7 @@ pub(super) fn extract_doc_comments(
67 // Mapping from extracted documentation code to original code 71 // Mapping from extracted documentation code to original code
68 let mut range_mapping: RangesMap = BTreeMap::new(); 72 let mut range_mapping: RangesMap = BTreeMap::new();
69 let mut line_start = TextSize::try_from(prefix.len()).unwrap(); 73 let mut line_start = TextSize::try_from(prefix.len()).unwrap();
74 let mut is_codeblock = false;
70 let mut is_doctest = false; 75 let mut is_doctest = false;
71 // Replace the original, line-spanning comment ranges by new, only comment-prefix 76 // Replace the original, line-spanning comment ranges by new, only comment-prefix
72 // spanning comment ranges. 77 // spanning comment ranges.
@@ -76,8 +81,13 @@ pub(super) fn extract_doc_comments(
76 .filter_map(|el| el.into_token().and_then(ast::Comment::cast)) 81 .filter_map(|el| el.into_token().and_then(ast::Comment::cast))
77 .filter(|comment| comment.kind().doc.is_some()) 82 .filter(|comment| comment.kind().doc.is_some())
78 .filter(|comment| { 83 .filter(|comment| {
79 if comment.text().contains("```") { 84 if let Some(idx) = comment.text().find(RUSTDOC_FENCE) {
80 is_doctest = !is_doctest; 85 is_codeblock = !is_codeblock;
86 // Check whether code is rust by inspecting fence guards
87 let guards = &comment.text()[idx + RUSTDOC_FENCE.len()..];
88 let is_rust =
89 guards.split(',').all(|sub| RUSTDOC_FENCE_TOKENS.contains(&sub.trim()));
90 is_doctest = is_codeblock && is_rust;
81 false 91 false
82 } else { 92 } else {
83 is_doctest 93 is_doctest
@@ -137,7 +147,7 @@ pub(super) fn highlight_doc_comment(
137 let (analysis, tmp_file_id) = Analysis::from_single_file(text); 147 let (analysis, tmp_file_id) = Analysis::from_single_file(text);
138 148
139 stack.push(); 149 stack.push();
140 for mut h in analysis.highlight(tmp_file_id).unwrap() { 150 for mut h in analysis.with_db(|db| super::highlight(db, tmp_file_id, None, true)).unwrap() {
141 // Determine start offset and end offset in case of multi-line ranges 151 // Determine start offset and end offset in case of multi-line ranges
142 let mut start_offset = None; 152 let mut start_offset = None;
143 let mut end_offset = None; 153 let mut end_offset = None;
diff --git a/crates/ra_ide/src/syntax_highlighting/tags.rs b/crates/ra_ide/src/syntax_highlighting/tags.rs
index 94f466966..400d22fb6 100644
--- a/crates/ra_ide/src/syntax_highlighting/tags.rs
+++ b/crates/ra_ide/src/syntax_highlighting/tags.rs
@@ -23,6 +23,7 @@ pub enum HighlightTag {
23 Constant, 23 Constant,
24 Enum, 24 Enum,
25 EnumVariant, 25 EnumVariant,
26 EscapeSequence,
26 Field, 27 Field,
27 FormatSpecifier, 28 FormatSpecifier,
28 Function, 29 Function,
@@ -71,6 +72,7 @@ impl HighlightTag {
71 HighlightTag::Constant => "constant", 72 HighlightTag::Constant => "constant",
72 HighlightTag::Enum => "enum", 73 HighlightTag::Enum => "enum",
73 HighlightTag::EnumVariant => "enum_variant", 74 HighlightTag::EnumVariant => "enum_variant",
75 HighlightTag::EscapeSequence => "escape_sequence",
74 HighlightTag::Field => "field", 76 HighlightTag::Field => "field",
75 HighlightTag::FormatSpecifier => "format_specifier", 77 HighlightTag::FormatSpecifier => "format_specifier",
76 HighlightTag::Function => "function", 78 HighlightTag::Function => "function",
diff --git a/crates/ra_ide/src/syntax_highlighting/tests.rs b/crates/ra_ide/src/syntax_highlighting/tests.rs
index 949bf59a0..b4d56a7a0 100644
--- a/crates/ra_ide/src/syntax_highlighting/tests.rs
+++ b/crates/ra_ide/src/syntax_highlighting/tests.rs
@@ -43,6 +43,12 @@ def_fn! {
43 } 43 }
44} 44}
45 45
46macro_rules! noop {
47 ($expr:expr) => {
48 $expr
49 }
50}
51
46// comment 52// comment
47fn main() { 53fn main() {
48 println!("Hello, {}!", 92); 54 println!("Hello, {}!", 92);
@@ -61,6 +67,8 @@ fn main() {
61 // Do nothing 67 // Do nothing
62 } 68 }
63 69
70 noop!(noop!(1));
71
64 let mut x = 42; 72 let mut x = 42;
65 let y = &mut x; 73 let y = &mut x;
66 let z = &y; 74 let z = &y;
@@ -238,6 +246,10 @@ fn main() {
238 246
239 println!(r"Hello, {}!", "world"); 247 println!(r"Hello, {}!", "world");
240 248
249 // escape sequences
250 println!("Hello\nWorld");
251 println!("\u{48}\x65\x6C\x6C\x6F World");
252
241 println!("{\x41}", A = 92); 253 println!("{\x41}", A = 92);
242 println!("{ничоси}", ничоси = 92); 254 println!("{ничоси}", ничоси = 92);
243}"# 255}"#
@@ -279,7 +291,13 @@ fn main() {
279fn test_highlight_doctest() { 291fn test_highlight_doctest() {
280 check_highlighting( 292 check_highlighting(
281 r#" 293 r#"
294struct Foo {
295 bar: bool,
296}
297
282impl Foo { 298impl Foo {
299 pub const bar: bool = true;
300
283 /// Constructs a new `Foo`. 301 /// Constructs a new `Foo`.
284 /// 302 ///
285 /// # Examples 303 /// # Examples
@@ -289,7 +307,7 @@ impl Foo {
289 /// let mut foo: Foo = Foo::new(); 307 /// let mut foo: Foo = Foo::new();
290 /// ``` 308 /// ```
291 pub const fn new() -> Foo { 309 pub const fn new() -> Foo {
292 Foo { } 310 Foo { bar: true }
293 } 311 }
294 312
295 /// `bar` method on `Foo`. 313 /// `bar` method on `Foo`.
@@ -297,11 +315,15 @@ impl Foo {
297 /// # Examples 315 /// # Examples
298 /// 316 ///
299 /// ``` 317 /// ```
318 /// use x::y;
319 ///
300 /// let foo = Foo::new(); 320 /// let foo = Foo::new();
301 /// 321 ///
302 /// // calls bar on foo 322 /// // calls bar on foo
303 /// assert!(foo.bar()); 323 /// assert!(foo.bar());
304 /// 324 ///
325 /// let bar = foo.bar || Foo::bar;
326 ///
305 /// /* multi-line 327 /// /* multi-line
306 /// comment */ 328 /// comment */
307 /// 329 ///
@@ -311,9 +333,13 @@ impl Foo {
311 /// 333 ///
312 /// ``` 334 /// ```
313 /// 335 ///
314 /// ``` 336 /// ```rust,no_run
315 /// let foobar = Foo::new().bar(); 337 /// let foobar = Foo::new().bar();
316 /// ``` 338 /// ```
339 ///
340 /// ```sh
341 /// echo 1
342 /// ```
317 pub fn foo(&self) -> bool { 343 pub fn foo(&self) -> bool {
318 true 344 true
319 } 345 }
@@ -322,7 +348,7 @@ impl Foo {
322 .trim(), 348 .trim(),
323 "crates/ra_ide/src/snapshots/highlight_doctest.html", 349 "crates/ra_ide/src/snapshots/highlight_doctest.html",
324 false, 350 false,
325 ) 351 );
326} 352}
327 353
328/// Highlights the code given by the `ra_fixture` argument, renders the 354/// Highlights the code given by the `ra_fixture` argument, renders the