Repository: rust-analyzer/rust-analyzer
Pull request #7199 (2021-01-07 17:27:32)

typo
bors r+
🤖


Patch:
diff --git a/crates/assists/src/handlers/flip_comma.rs b/crates/assists/src/handlers/flip_comma.rs
index 18cf64a34e..a48b0e450a 100644
--- a/crates/assists/src/handlers/flip_comma.rs
+++ b/crates/assists/src/handlers/flip_comma.rs
@@ -49,14 +49,14 @@ mod tests {
     fn flip_comma_works_for_function_parameters() {
         check_assist(
             flip_comma,
-            r#"fn foo(x: i32,$0 y: Result<(), ()>) {}"#,
-            r#"fn foo(y: Result<(), ()>, x: i32) {}"#,
+            "fn foo(x: i32,$0 y: Result<(), ()>) {}",
+            "fn foo(y: Result<(), ()>, x: i32) {}",
         )
     }
 
     #[test]
     fn flip_comma_target() {
-        check_assist_target(flip_comma, r#"fn foo(x: i32,$0 y: Result<(), ()>) {}"#, ",")
+        check_assist_target(flip_comma, "fn foo(x: i32,$0 y: Result<(), ()>) {}", ",")
     }
 
     #[test]
diff --git a/crates/base_db/src/fixture.rs b/crates/base_db/src/fixture.rs
index 98acd61b12..66e6443cb1 100644
--- a/crates/base_db/src/fixture.rs
+++ b/crates/base_db/src/fixture.rs
@@ -61,9 +61,7 @@ use std::{str::FromStr, sync::Arc};
 
 use cfg::CfgOptions;
 use rustc_hash::FxHashMap;
-use test_utils::{
-    extract_range_or_offset, Fixture, RangeOrOffset, CURSOR_MARKER, ESCAPED_CURSOR_MARKER,
-};
+use test_utils::{extract_range_or_offset, Fixture, RangeOrOffset, CURSOR_MARKER};
 use vfs::{file_set::FileSet, VfsPath};
 
 use crate::{
@@ -144,14 +142,10 @@ impl ChangeFixture {
 
         for entry in fixture {
             let text = if entry.text.contains(CURSOR_MARKER) {
-                if entry.text.contains(ESCAPED_CURSOR_MARKER) {
-                    entry.text.replace(ESCAPED_CURSOR_MARKER, CURSOR_MARKER)
-                } else {
-                    let (range_or_offset, text) = extract_range_or_offset(&entry.text);
-                    assert!(file_position.is_none());
-                    file_position = Some((file_id, range_or_offset));
-                    text.to_string()
-                }
+                let (range_or_offset, text) = extract_range_or_offset(&entry.text);
+                assert!(file_position.is_none());
+                file_position = Some((file_id, range_or_offset));
+                text.to_string()
             } else {
                 entry.text.clone()
             };
diff --git a/crates/ide/src/join_lines.rs b/crates/ide/src/join_lines.rs
index 05380f2a10..296893d2fa 100644
--- a/crates/ide/src/join_lines.rs
+++ b/crates/ide/src/join_lines.rs
@@ -198,8 +198,8 @@ mod tests {
 
     use super::*;
 
-    fn check_join_lines(ra_fixture_before: &str, ra_fixture_after: &str) {
-        let (before_cursor_pos, before) = extract_offset(ra_fixture_before);
+    fn check_join_lines(before: &str, after: &str) {
+        let (before_cursor_pos, before) = extract_offset(before);
         let file = SourceFile::parse(&before).ok().unwrap();
 
         let range = TextRange::empty(before_cursor_pos);
@@ -214,7 +214,7 @@ mod tests {
             .apply_to_offset(before_cursor_pos)
             .expect("cursor position is affected by the edit");
         let actual = add_cursor(&actual, actual_cursor_pos);
-        assert_eq_text!(ra_fixture_after, &actual);
+        assert_eq_text!(after, &actual);
     }
 
     #[test]
@@ -604,8 +604,8 @@ fn foo() {
         );
     }
 
-    fn check_join_lines_sel(ra_fixture_before: &str, ra_fixture_after: &str) {
-        let (sel, before) = extract_range(ra_fixture_before);
+    fn check_join_lines_sel(before: &str, after: &str) {
+        let (sel, before) = extract_range(before);
         let parse = SourceFile::parse(&before);
         let result = join_lines(&parse.tree(), sel);
         let actual = {
@@ -613,7 +613,7 @@ fn foo() {
             result.apply(&mut actual);
             actual
         };
-        assert_eq_text!(ra_fixture_after, &actual);
+        assert_eq_text!(after, &actual);
     }
 
     #[test]
diff --git a/crates/ide/src/syntax_highlighting/injection.rs b/crates/ide/src/syntax_highlighting/injection.rs
index d6be9708df..6cbd683c69 100644
--- a/crates/ide/src/syntax_highlighting/injection.rs
+++ b/crates/ide/src/syntax_highlighting/injection.rs
@@ -22,8 +22,7 @@ pub(super) fn highlight_injection(
         return None;
     }
     let value = literal.value()?;
-    let marker_info = MarkerInfo::new(&*value);
-    let (analysis, tmp_file_id) = Analysis::from_single_file(marker_info.cleaned_text.clone());
+    let (analysis, tmp_file_id) = Analysis::from_single_file(value.into_owned());
 
     if let Some(range) = literal.open_quote_text_range() {
         acc.add(HighlightedRange {
@@ -34,10 +33,9 @@ pub(super) fn highlight_injection(
     }
 
     for mut h in analysis.highlight(tmp_file_id).unwrap() {
-        let range = marker_info.map_range_up(h.range);
-        if let Some(range) = literal.map_range_up(range) {
-            h.range = range;
-            acc.add(h);
+        if let Some(r) = literal.map_range_up(h.range) {
+            h.range = r;
+            acc.add(h)
         }
     }
 
@@ -52,52 +50,6 @@ pub(super) fn highlight_injection(
     Some(())
 }
 
-/// Data to remove `$0` from string and map ranges
-#[derive(Default, Debug)]
-struct MarkerInfo {
-    cleaned_text: String,
-    markers: Vec<TextRange>,
-}
-
-impl MarkerInfo {
-    fn new(mut text: &str) -> Self {
-        let marker = "$0";
-
-        let mut res = MarkerInfo::default();
-        let mut offset: TextSize = 0.into();
-        while !text.is_empty() {
-            let idx = text.find(marker).unwrap_or(text.len());
-            let (chunk, next) = text.split_at(idx);
-            text = next;
-            res.cleaned_text.push_str(chunk);
-            offset += TextSize::of(chunk);
-
-            if let Some(next) = text.strip_prefix(marker) {
-                text = next;
-
-                let marker_len = TextSize::of(marker);
-                res.markers.push(TextRange::at(offset, marker_len));
-                offset += marker_len;
-            }
-        }
-        res
-    }
-    fn map_range_up(&self, range: TextRange) -> TextRange {
-        TextRange::new(
-            self.map_offset_up(range.start(), true),
-            self.map_offset_up(range.end(), false),
-        )
-    }
-    fn map_offset_up(&self, mut offset: TextSize, start: bool) -> TextSize {
-        for r in &self.markers {
-            if r.start() < offset || (start && r.start() == offset) {
-                offset += r.len()
-            }
-        }
-        offset
-    }
-}
-
 /// Mapping from extracted documentation code to original code
 type RangesMap = BTreeMap<TextSize, TextSize>;
 
diff --git a/docs/dev/style.md b/docs/dev/style.md
index f4748160bf..67cbc67448 100644
--- a/docs/dev/style.md
+++ b/docs/dev/style.md
@@ -53,7 +53,7 @@ We try to be very conservative with usage of crates.io dependencies.
 Don't use small "helper" crates (exception: `itertools` is allowed).
 If there's some general reusable bit of code you need, consider adding it to the `stdx` crate.
 
-**Rational:** keep compile times low, create ecosystem pressure for faster
+**Rationale:** keep compile times low, create ecosystem pressure for faster
 compiles, reduce the number of things which might break.
 
 ## Commit Style
@@ -78,7 +78,7 @@ Use original span for FileId
 
 This makes it easier to prepare a changelog.
 
-**Rational:** clean history is potentially useful, but rarely used.
+**Rationale:** clean history is potentially useful, but rarely used.
 But many users read changelogs.
 
 ## Clippy
@@ -90,7 +90,7 @@ There's `cargo xtask lint` command which runs a subset of low-FPR lints.
 Careful tweaking of `xtask lint` is welcome.
 Of course, applying Clippy suggestions is welcome as long as they indeed improve the code.
 
-**Rational:** see [rust-lang/clippy#5537](https://github.com/rust-lang/rust-clippy/issues/5537).
+**Rationale:** see [rust-lang/clippy#5537](https://github.com/rust-lang/rust-clippy/issues/5537).
 
 # Code
 
@@ -126,7 +126,7 @@ fn main() {
     }
 ```
 
-**Rational:**
+**Rationale:**
 
 There are many benefits to this:
 
@@ -157,7 +157,7 @@ fn frobnicate(walrus: Option<Walrus>) {
 }
 ```
 
-**Rational:** this makes control flow explicit at the call site.
+**Rationale:** this makes control flow explicit at the call site.
 Call-site has more context, it often happens that the precondition falls out naturally or can be bubbled up higher in the stack.
 
 Avoid splitting precondition check and precondition use across functions:
@@ -195,7 +195,7 @@ fn is_string_literal(s: &str) -> bool {
 In the "Not as good" version, the precondition that `1` is a valid char boundary is checked in `is_string_literal` and used in `foo`.
 In the "Good" version, the precondition check and usage are checked in the same block, and then encoded in the types.
 
-**Rational:** non-local code properties degrade under change.
+**Rationale:** non-local code properties degrade under change.
 
 When checking a boolean precondition, prefer `if !invariant` to `if negated_invariant`:
 
@@ -211,7 +211,7 @@ if idx >= len {
 }
 ```
 
-**Rational:** its useful to see the invariant relied upon by the rest of the function clearly spelled out.
+**Rationale:** its useful to see the invariant relied upon by the rest of the function clearly spelled out.
 
 ## Getters & Setters
 
@@ -241,7 +241,7 @@ impl Person {
 }
 ```
 
-**Rational:** we don't provide public API, it's cheaper to refactor than to pay getters rent.
+**Rationale:** we don't provide public API, it's cheaper to refactor than to pay getters rent.
 Non-local code properties degrade under change, privacy makes invariant local.
 Borrowed own data discloses irrelevant details about origin of data.
 Irrelevant (neither right nor wrong) things obscure correctness.
@@ -271,7 +271,7 @@ impl Foo {
 
 Prefer `Default` even it has to be implemented manually.
 
-**Rational:** less typing in the common case, uniformity.
+**Rationale:** less typing in the common case, uniformity.
 
 ## Functions Over Objects
 
@@ -327,7 +327,7 @@ impl ThingDoer {
 }
 ```
 
-**Rational:** not bothering the caller with irrelevant details, not mixing user API with implementor API.
+**Rationale:** not bothering the caller with irrelevant details, not mixing user API with implementor API.
 
 ## Avoid Monomorphization
 
@@ -360,7 +360,7 @@ fn frbonicate(f: impl AsRef<Path>) {
 }
 ```
 
-**Rational:** Rust uses monomorphization to compile generic code, meaning that for each instantiation of a generic functions with concrete types, the function is compiled afresh, *per crate*.
+**Rationale:** Rust uses monomorphization to compile generic code, meaning that for each instantiation of a generic functions with concrete types, the function is compiled afresh, *per crate*.
 This allows for exceptionally good performance, but leads to increased compile times.
 Runtime performance obeys 80%/20% rule -- only a small fraction of code is hot.
 Compile time **does not** obey this rule -- all code has to be compiled.
@@ -389,7 +389,7 @@ if words.len() != 2 {
 }
 ```
 
-**Rational:** not allocating is almost often faster.
+**Rationale:** not allocating is almost often faster.
 
 ## Push Allocations to the Call Site
 
@@ -408,14 +408,14 @@ fn frobnicate(s: &str) {
 }
 ```
 
-**Rational:** reveals the costs.
+**Rationale:** reveals the costs.
 It is also more efficient when the caller already owns the allocation.
 
 ## Collection types
 
 Prefer `rustc_hash::FxHashMap` and `rustc_hash::FxHashSet` instead of the ones in `std::collections`.
 
-**Rational:** they use a hasher that's significantly faster and using them consistently will reduce code size by some small amount.
+**Rationale:** they use a hasher that's significantly faster and using them consistently will reduce code size by some small amount.
 
 # Style
 
@@ -445,7 +445,7 @@ use crate::{}
 use super::{}
 ```
 
-**Rational:** consistency.
+**Rationale:** consistency.
 Reading order is important for new contributors.
 Grouping by crate allows to spot unwanted dependencies easier.
 
@@ -466,7 +466,7 @@ use syntax::ast::Struct;
 fn frobnicate(func: Function, strukt: Struct) {}
 ```
 
-**Rational:** avoids name clashes, makes the layer clear at a glance.
+**Rationale:** avoids name clashes, makes the layer clear at a glance.
 
 When implementing traits from `std::fmt` or `std::ops`, import the module:
 
@@ -492,14 +492,14 @@ impl Deref for Widget {
 }
 ```
 
-**Rational:** overall, less typing.
+**Rationale:** overall, less typing.
 Makes it clear that a trait is implemented, rather than used.
 
 Avoid local `use MyEnum::*` imports.
-**Rational:** consistency.
+**Rationale:** consistency.
 
 Prefer `use crate::foo::bar` to `use super::bar` or `use self::bar::baz`.
-**Rational:** consistency, this is the style which works in all cases.
+**Rationale:** consistency, this is the style which works in all cases.
 
 ## Order of Items
 
@@ -570,7 +570,7 @@ impl Parent {
 }
 ```
 
-**Rational:** easier to get the sense of the API by visually scanning the file.
+**Rationale:** easier to get the sense of the API by visually scanning the file.
 If function bodies are folded in the editor, the source code should read as documentation for the public API.
 
 ## Variable Naming
@@ -626,7 +626,7 @@ fn foo() -> Option<Bar> {
 }
 ```
 
-**Rational:** reduce congnitive stack usage.
+**Rationale:** reduce congnitive stack usage.
 
 ## Comparisons
 
@@ -640,7 +640,7 @@ assert!(lo <= x && x <= hi);
 assert!(x >= lo && x <= hi>);
 ```
 
-**Rational:** Less-then comparisons are more intuitive, they correspond spatially to [real line](https://en.wikipedia.org/wiki/Real_line).
+**Rationale:** Less-then comparisons are more intuitive, they correspond spatially to [real line](https://en.wikipedia.org/wiki/Real_line).
 
 
 ## Documentation
@@ -648,4 +648,4 @@ assert!(x >= lo && x <= hi>);
 For `.md` and `.adoc` files, prefer a sentence-per-line format, don't wrap lines.
 If the line is too long, you want to split the sentence in two :-)
 
-**Rational:** much easier to edit the text and read the diff.
+**Rationale:** much easier to edit the text and read the diff.


Test patch:
diff --git a/crates/ide/src/syntax_highlighting/test_data/injection.html b/crates/ide/src/syntax_highlighting/test_data/injection.html
deleted file mode 100644
index a54d303b46..0000000000
--- a/crates/ide/src/syntax_highlighting/test_data/injection.html
+++ /dev/null
@@ -1,48 +0,0 @@
-
-<style>
-body                { margin: 0; }
-pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padding: 0.4em; }
-
-.lifetime           { color: #DFAF8F; font-style: italic; }
-.label              { color: #DFAF8F; font-style: italic; }
-.comment            { color: #7F9F7F; }
-.documentation      { color: #629755; }
-.injected           { opacity: 0.65 ; }
-.struct, .enum      { color: #7CB8BB; }
-.enum_variant       { color: #BDE0F3; }
-.string_literal     { color: #CC9393; }
-.field              { color: #94BFF3; }
-.function           { color: #93E0E3; }
-.function.unsafe    { color: #BC8383; }
-.operator.unsafe    { color: #BC8383; }
-.parameter          { color: #94BFF3; }
-.text               { color: #DCDCCC; }
-.type               { color: #7CB8BB; }
-.builtin_type       { color: #8CD0D3; }
-.type_param         { color: #DFAF8F; }
-.attribute          { color: #94BFF3; }
-.numeric_literal    { color: #BFEBBF; }
-.bool_literal       { color: #BFE6EB; }
-.macro              { color: #94BFF3; }
-.module             { color: #AFD8AF; }
-.value_param        { color: #DCDCCC; }
-.variable           { color: #DCDCCC; }
-.format_specifier   { color: #CC696B; }
-.mutable            { text-decoration: underline; }
-.escape_sequence    { color: #94BFF3; }
-.keyword            { color: #F0DFAF; font-weight: bold; }
-.keyword.unsafe     { color: #BC8383; font-weight: bold; }
-.control            { font-style: italic; }
-
-.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
-</style>
-<pre><code><span class="keyword">fn</span> <span class="function declaration">f</span><span class="punctuation">(</span><span class="value_param declaration">ra_fixture</span><span class="punctuation">:</span> <span class="operator">&</span><span class="builtin_type">str</span><span class="punctuation">)</span> <span class="punctuation">{</span><span class="punctuation">}</span>
-<span class="keyword">fn</span> <span class="function declaration">main</span><span class="punctuation">(</span><span class="punctuation">)</span> <span class="punctuation">{</span>
-    <span class="function">f</span><span class="punctuation">(</span><span class="string_literal">r"</span>
-<span class="keyword">fn</span> <span class="function declaration">foo</span><span class="punctuation">(</span><span class="punctuation">)</span> <span class="punctuation">{</span>
-    <span class="function">foo</span><span class="punctuation">(</span>$0<span class="punctuation">{</span>
-        <span class="numeric_literal">92</span>
-    <span class="punctuation">}</span>$0<span class="punctuation">)</span>
-<span class="punctuation">}</span><span class="string_literal">"</span><span class="punctuation">)</span><span class="punctuation">;</span>
-<span class="punctuation">}</span>
-    </code></pre>
\ No newline at end of file
diff --git a/crates/ide/src/syntax_highlighting/tests.rs b/crates/ide/src/syntax_highlighting/tests.rs
index 9e1a3974cb..30b5b648e9 100644
--- a/crates/ide/src/syntax_highlighting/tests.rs
+++ b/crates/ide/src/syntax_highlighting/tests.rs
@@ -555,25 +555,6 @@ impl t for foo {
     )
 }
 
-#[test]
-fn test_injection() {
-    check_highlighting(
-        r##"
-fn f(ra_fixture: &str) {}
-fn main() {
-    f(r"
-fn foo() {
-    foo(\$0{
-        92
-    }\$0)
-}");
-}
-    "##,
-        expect_file!["./test_data/injection.html"],
-        false,
-    );
-}
-
 /// Highlights the code given by the `ra_fixture` argument, renders the
 /// result as HTML, and compares it with the HTML file given as `snapshot`.
 /// Note that the `snapshot` file is overwritten by the rendered HTML.
diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs
index 84c1d7ebbd..05d6e8c9ed 100644
--- a/crates/test_utils/src/lib.rs
+++ b/crates/test_utils/src/lib.rs
@@ -26,7 +26,6 @@ pub use rustc_hash::FxHashMap;
 pub use crate::fixture::Fixture;
 
 pub const CURSOR_MARKER: &str = "$0";
-pub const ESCAPED_CURSOR_MARKER: &str = "\\$0";
 
 /// Asserts that two strings are equal, otherwise displays a rich diff between them.
 ///