1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
// Small functions of utility
use std::{cmp, error, fmt, path};
use std::rc::Rc;

use core::{IndexedSource, Session, SessionExt, Location, LocationExt, Point};
use core::SearchType::{self, ExactMatch, StartsWith};

#[cfg(unix)]
pub const PATH_SEP: char = ':';
#[cfg(windows)]
pub const PATH_SEP: char = ';';

pub fn is_pattern_char(c: char) -> bool {
    c.is_alphanumeric() || c.is_whitespace() || (c == '_') || (c == ':') || (c == '.')
}

pub fn is_search_expr_char(c: char) -> bool {
    c.is_alphanumeric() || (c == '_') || (c == ':') || (c == '.')
}

pub fn is_ident_char(c: char) -> bool {
    c.is_alphanumeric() || (c == '_') || (c == '!')
}

/// Searches for `needle` as a standalone identifier in `haystack`. To be considered a match,
/// the `needle` must occur either at the beginning of `haystack` or after a non-identifier
/// character.
pub fn txt_matches(stype: SearchType, needle: &str, haystack: &str) -> bool {
    match stype {
        ExactMatch => {
            let n_len = needle.len();
            let h_len = haystack.len();

            if n_len == 0 {
                return true;
            }

            for (n, _) in haystack.match_indices(needle) {
                if (n == 0 || !is_ident_char(char_before(haystack, n))) &&
                    (n+n_len == h_len || !is_ident_char(char_at(haystack, n+n_len))) {
                    return true;
                }
            }
            false
        },
        StartsWith => {
            if needle.is_empty() {
                return true;
            }

            for (n, _) in haystack.match_indices(needle) {
                if n == 0 || !is_ident_char(char_before(haystack, n)) {
                    return true;
                }
            }
            false
        }
    }
}

pub fn symbol_matches(stype: SearchType, searchstr: &str, candidate: &str) -> bool {
   match stype {
        ExactMatch => searchstr == candidate,
        StartsWith => candidate.starts_with(searchstr)
    }
}

/// Try to valid if the given scope contains a valid closure arg scope.
pub fn closure_valid_arg_scope(scope_src: &str) -> Option<(usize, usize, &str)> {
    // Try to find the left and right pipe, if one or both are not present, this is not a valid
    // closure definition
    let left_pipe = if let Some(pos) = scope_src.find('|') { pos } else { return None; };
    let rest_scope = &scope_src[left_pipe + 1..];
    let right_pipe = if let Some(pos) = rest_scope.find('|') {
        left_pipe + 1 + pos
    } else {
        return None;
    };

    let pipe_scope = &scope_src[left_pipe..right_pipe+1];

    // For each '{' increase the counter by one and for each '}' decrease the counter by one
    // If we have a equal number of curly brackets, we should get 0 as result
    let curly_brackets = pipe_scope.chars().fold(0,
                           |count, c| {
                               if c == '{' {
                                   count + 1
                               } else if c == '}' {
                                   count - 1
                               } else {
                                   count
                               }
                           });

    // If we found an unequal number of curly brackets in the scope, this can not be a valid
    // closure definition
    if curly_brackets != 0 {
        return None;
    }

    // If we find a ';' --> no closure definition
    if pipe_scope.contains(';') {
        return None;
    }

    Some((left_pipe, right_pipe, pipe_scope))
}

// pub fn get_backtrace() -> String {
//     let mut m = std::old_io::MemWriter::new();
//     let s = std::rt::backtrace::write(&mut m)
//         .ok().map_or("NO backtrace".to_string(),
//                      |_| String::from_utf8_lossy(m.get_ref()).to_string());
//     return s;
// }

#[test]
fn txt_matches_matches_stuff() {
    assert_eq!(true, txt_matches(ExactMatch, "Vec", "Vec"));
    assert_eq!(true, txt_matches(ExactMatch, "Vec", "use Vec"));
    assert_eq!(false, txt_matches(ExactMatch, "Vec", "use Vecä"));

    assert_eq!(true, txt_matches(StartsWith, "Vec", "Vector"));
    assert_eq!(true, txt_matches(StartsWith, "Vec", "use Vector"));
    assert_eq!(true, txt_matches(StartsWith, "Vec", "use Vec"));
    assert_eq!(false, txt_matches(StartsWith, "Vec", "use äVector"));
}

#[test]
fn txt_matches_matches_methods() {
    assert_eq!(true, txt_matches(StartsWith, "do_st", "fn do_stuff"));
    assert_eq!(true, txt_matches(StartsWith, "do_st", "pub fn do_stuff"));
    assert_eq!(true, txt_matches(StartsWith, "do_st", "pub(crate) fn do_stuff"));
    assert_eq!(true, txt_matches(StartsWith, "do_st", "pub(in codegen) fn do_stuff"));
}


/// Given a string and index, return span of identifier
///
/// `pos` is coerced to be within `s`. Note that `expand_ident` only backtracks.
/// If the provided `pos` is in the middle of an identifier, the returned
/// `(start, end)` will have `end` = `pos`.
///
/// # Examples
///
/// ```
/// extern crate racer;
///
/// let src = "let x = this_is_an_identifier;";
/// let pos = racer::Location::Point(29);
/// let path = "lib.rs";
///
/// let cache = racer::FileCache::default();
/// let session = racer::Session::new(&cache);
///
/// session.cache_file_contents(path, src);
///
/// let expanded = racer::expand_ident(path, pos, &session).unwrap();
/// assert_eq!("this_is_an_identifier", expanded.ident());
/// ```
pub fn expand_ident<P, C>(
    filepath: P,
    cursor: C,
    session: &Session
) -> Option<ExpandedIdent>
    where P: AsRef<path::Path>,
          C: Into<Location>
{
    let cursor = cursor.into();
    let indexed_source = session.load_file(filepath.as_ref());
    let (start, pos) = {
        let s = &indexed_source.code[..];
        let pos = match cursor.to_point(&indexed_source) {
            Some(pos) => pos,
            None => {
                debug!("Failed to convert cursor to point");
                return None;
            }
        };

        // TODO: Would this better be an assertion ? Why are out-of-bound values getting here ?
        // They are coming from the command-line, question is, if they should be handled beforehand
        // clamp pos into allowed range
        let pos = cmp::min(s.len(), pos);
        let sb = &s[..pos];
        let mut start = pos;

        // backtrack to find start of word
        for (i, c) in sb.char_indices().rev() {
            if !is_ident_char(c) {
                break;
            }
            start = i;
        }

        (start, pos)
    };

    Some(ExpandedIdent {
        src: indexed_source,
        start: start,
        pos: pos,
    })
}

pub struct ExpandedIdent {
    src: Rc<IndexedSource>,
    start: Point,
    pos: Point,
}

impl ExpandedIdent {
    pub fn ident(&self) -> &str {
        &self.src.code[self.start..self.pos]
    }

    pub fn start(&self) -> Point {
        self.start
    }

    pub fn pos(&self) -> Point {
        self.pos
    }
}

pub fn find_ident_end(s: &str, pos: Point) -> Point {
    // find end of word
    let sa = &s[pos..];
    for (i, c) in sa.char_indices() {
        if !is_ident_char(c) {
            return pos + i;
        }
    }
    s.len()
}

#[test]
fn find_ident_end_ascii() {
    assert_eq!(5, find_ident_end("ident", 0));
    assert_eq!(6, find_ident_end("(ident)", 1));
    assert_eq!(17, find_ident_end("let an_identifier = 100;", 4));
}

#[test]
fn find_ident_end_unicode() {
    assert_eq!(7, find_ident_end("num_µs", 0));
    assert_eq!(10, find_ident_end("ends_in_µ", 0));
}

fn char_before(src: &str, i: usize) -> char {
    let mut prev = '\0';
    for (ii, ch) in src.char_indices() {
        if ii >= i {
            return prev;
        }
        prev = ch;
    }
    return prev;
}

#[test]
fn test_char_before() {
    assert_eq!('ä', char_before("täst", 3));
    assert_eq!('ä', char_before("täst", 2));
    assert_eq!('s', char_before("täst", 4));
    assert_eq!('t', char_before("täst", 100));
}

pub fn char_at(src: &str, i: usize) -> char {
    src[i..].chars().next().unwrap()
}

/// Error type returned from validate_rust_src_path()
#[derive(Debug, PartialEq)]
pub enum RustSrcPathError {
    Missing,
    DoesNotExist(path::PathBuf),
    NotRustSourceTree(path::PathBuf),
}

impl error::Error for RustSrcPathError {
    fn cause(&self) -> Option<&error::Error> {
        None
    }

    fn description(&self) -> &str {
        match *self {
            RustSrcPathError::Missing => "RUSTC_SRC_PATH not set or not found in sysroot",
            RustSrcPathError::DoesNotExist(_) => "RUSTC_SRC_PATH does not exist on file system",
            RustSrcPathError::NotRustSourceTree(_) => "RUSTC_SRC_PATH isn't a rustc source tree",
        }
    }
}

impl fmt::Display for RustSrcPathError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            RustSrcPathError::Missing => {
                write!(f, "RUST_SRC_PATH environment variable must be set to \
                           point to the src directory of a rust checkout. \
                           E.g. \"/home/foouser/src/rust/src\"")
            },
            RustSrcPathError::DoesNotExist(ref path) => {
                write!(f, "racer can't find the directory pointed to by the \
                           RUST_SRC_PATH variable \"{:?}\". Try using an \
                           absolute fully qualified path and make sure it \
                           points to the src directory of a rust checkout - \
                           e.g. \"/home/foouser/src/rust/src\".", path)
            },
            RustSrcPathError::NotRustSourceTree(ref path) => {
                write!(f, "Unable to find libstd under RUST_SRC_PATH. N.B. \
                           RUST_SRC_PATH variable needs to point to the *src* \
                           directory inside a rust checkout e.g. \
                           \"/home/foouser/src/rust/src\". \
                           Current value \"{:?}\"", path)
            },
        }
    }
}

fn check_rust_sysroot() -> Option<path::PathBuf> {
    use std::process::Command;
    let mut cmd = Command::new("rustc");
    cmd.arg("--print").arg("sysroot");

    if let Ok(output) = cmd.output() {
        if let Ok(s) = String::from_utf8(output.stdout) {
            let sysroot = path::Path::new(s.trim());
            let srcpath = sysroot.join("lib/rustlib/src/rust/src");
            if srcpath.exists() {
                return Some(srcpath);
            }
        }
    }
    None
}

/// Get the path for Rust standard library source code.
/// Checks first the paths in the `RUST_SRC_PATH` environment variable.
///
/// If the environment variable is _not_ set, it checks the rust sys
/// root for the `rust-src` component.
///
/// If that isn't available, checks `/usr/local/src/rust/src` and
/// `/usr/src/rust/src` as default values.
///
/// If the Rust standard library source code cannot be found, returns
/// `Err(racer::RustSrcPathError::Missing)`.
///
/// If the path in `RUST_SRC_PATH` or the path in rust sys root is invalid,
/// returns a corresponding error. If a valid path is found, returns that path.
///
/// # Examples
///
/// ```
/// extern crate racer;
///
/// match racer::get_rust_src_path() {
///     Ok(_path) => {
///         // RUST_SRC_PATH is valid
///     },
///     Err(racer::RustSrcPathError::Missing) => {
///         // path is not set
///     },
///     Err(racer::RustSrcPathError::DoesNotExist(_path)) => {
///         // provided path doesnt point to valid file
///     },
///     Err(racer::RustSrcPathError::NotRustSourceTree(_path)) => {
///         // provided path doesn't have rustc src
///     }
/// }
/// ```
pub fn get_rust_src_path() -> Result<path::PathBuf, RustSrcPathError> {
    use std::env;

    debug!("Getting rust source path. Trying env var RUST_SRC_PATH.");

    if let Ok(ref srcpaths) = env::var("RUST_SRC_PATH") {
         if !srcpaths.is_empty() {
            for path in srcpaths.split(PATH_SEP) {
                return validate_rust_src_path(path::PathBuf::from(path));
            }
        }
    };

    debug!("Nope. Trying rustc --sysroot and appending lib/rustlib/src/rust/src to that.");

    if let Some(path) = check_rust_sysroot() {
        return validate_rust_src_path(path);
    };

    debug!("Nope. Trying default paths: /usr/local/src/rust/src and /usr/src/rust/src");

    let default_paths = [
        "/usr/local/src/rust/src",
        "/usr/src/rust/src",
    ];

    for path in default_paths.iter() {
        if let Ok(path) = validate_rust_src_path(path::PathBuf::from(path)) {
            return Ok(path);
        }
    }

    debug!("Nope. Rust source path not found!");

    return Err(RustSrcPathError::Missing)
}

fn validate_rust_src_path(path: path::PathBuf) -> Result<path::PathBuf, RustSrcPathError> {
    if !path.exists() {
        Err(RustSrcPathError::DoesNotExist(path.to_path_buf()))
    } else if !path.join("libstd").exists() {
        Err(RustSrcPathError::NotRustSourceTree(path.join("libstd")))
    } else {
        Ok(path.to_path_buf())
    }
}

#[cfg(test)]
lazy_static! {
    static ref TEST_SEMAPHORE: ::std::sync::Mutex<()> = Default::default();
}

#[test]
fn test_get_rust_src_path_env_ok() {
    use std::env;

    let _guard = TEST_SEMAPHORE.lock().unwrap();

    let original = env::var_os("RUST_SRC_PATH");
    if env::var_os("RUST_SRC_PATH").is_none() {
        env::set_var("RUST_SRC_PATH", check_rust_sysroot().unwrap());
    }
    let result = get_rust_src_path();

    match original {
        Some(path) => env::set_var("RUST_SRC_PATH", path),
        None => env::remove_var("RUST_SRC_PATH"),
    }
    assert!(result.is_ok());
}

#[test]
fn test_get_rust_src_path_does_not_exist() {
    use std::env;

    let _guard = TEST_SEMAPHORE.lock().unwrap();

    let original = env::var_os("RUST_SRC_PATH");
    env::set_var("RUST_SRC_PATH", "test_path");
    let result = get_rust_src_path();

    match original {
        Some(path) => env::set_var("RUST_SRC_PATH", path),
        None => env::remove_var("RUST_SRC_PATH"),
    }

    assert_eq!(Err(RustSrcPathError::DoesNotExist(path::PathBuf::from("test_path"))),
               result);
}

#[test]
fn test_get_rust_src_path_not_rust_source_tree() {
    use std::env;

    let _guard = TEST_SEMAPHORE.lock().unwrap();

    let original = env::var_os("RUST_SRC_PATH");

    env::set_var("RUST_SRC_PATH", "/");

    let result = get_rust_src_path();

    match original {
        Some(path) => env::set_var("RUST_SRC_PATH", path),
        None => env::remove_var("RUST_SRC_PATH"),
    }

    assert_eq!(Err(RustSrcPathError::NotRustSourceTree(path::PathBuf::from("/libstd"))),
               result);

}

#[test]
fn test_get_rust_src_path_missing() {
    use std::env;

    let _guard = TEST_SEMAPHORE.lock().unwrap();

    let path = env::var_os("PATH").unwrap();
    let original = env::var_os("RUST_SRC_PATH");

    env::remove_var("RUST_SRC_PATH");
    env::remove_var("PATH");

    let result = get_rust_src_path();

    env::set_var("PATH", path);
    match original {
        Some(path) => env::set_var("RUST_SRC_PATH", path),
        None => env::remove_var("RUST_SRC_PATH"),
    }

    assert_eq!(Err(RustSrcPathError::Missing),
        result);
}

#[test]
fn test_get_rust_src_path_rustup_ok() {
    use std::env;

    let _guard = TEST_SEMAPHORE.lock().unwrap();
    let original = env::var_os("RUST_SRC_PATH");
    env::remove_var("RUST_SRC_PATH");

    let result = get_rust_src_path();

    match original {
        Some(path) => env::set_var("RUST_SRC_PATH", path),
        None => env::remove_var("RUST_SRC_PATH"),
    }

    match result {
        Ok(_) => (),
        Err(_) => panic!("Couldn't get the path via rustup! \
            Rustup and the component rust-src needs to be installed for this test to pass!"),
    }
}


/// An immutable stack implemented as a linked list backed by a thread's stack.
pub struct StackLinkedListNode<'stack, T>(Option<StackLinkedListNodeData<'stack, T>>)
    where T: 'stack;

struct StackLinkedListNodeData<'stack, T>
    where T: 'stack
{
    item: T,
    previous: &'stack StackLinkedListNode<'stack, T>,
}

impl<'stack, T> StackLinkedListNode<'stack, T>
    where T: 'stack
{
    /// Returns an empty node.
    pub fn empty() -> Self {
        StackLinkedListNode(None)
    }

    /// Pushes a new node on the stack. Returns the new node.
    pub fn push(&'stack self, item: T) -> Self {
        StackLinkedListNode(Some(StackLinkedListNodeData {
            item: item,
            previous: self,
        }))
    }
}

impl<'stack, T> StackLinkedListNode<'stack, T>
    where T: 'stack + PartialEq
{
    /// Check if the stack contains the specified item.
    /// Returns `true` if the item is found, or `false` if it's not found.
    pub fn contains(&self, item: &T) -> bool {
        let mut current = self;
        while let &StackLinkedListNode(Some(StackLinkedListNodeData { item: ref current_item, previous })) = current {
            if current_item == item {
                return true;
            }

            current = previous;
        }

        false
    }
}

/// Removes `pub(...)` from the start of a blob so that other code
/// can assess the struct/trait/fn without worrying about restricted
/// visibility.
pub fn trim_visibility(blob: &str) -> &str {
    if !blob.trim_left().starts_with("pub") {
        return blob
    }
    
    let mut level = 0;
    let mut skip_restricted = 0;
    for (i, c) in blob[3..].char_indices() {
        match c {
            '(' => level += 1,
            ')' => level -= 1,
            _ if level >= 1 => (),
            // stop on the first thing that isn't whitespace
            _ if is_ident_char(c) => {
                skip_restricted = i + 3;
                break;
            },
            _ => continue,
        }
    }

    &blob[skip_restricted..]
}

#[test]
fn test_trim_visibility() {
    assert_eq!(trim_visibility("pub fn"), "fn");
    assert_eq!(trim_visibility("pub(crate)   struct"), "struct");
    assert_eq!(trim_visibility("pub (in super)  const fn"), "const fn");
}

/// Checks if the completion point is in a function declaration by looking
/// to see if the second-to-last word is `fn`.
pub fn in_fn_name(line_before_point: &str) -> bool {
    // Determine if the cursor is sitting in the whitespace after typing `fn ` before
    // typing a name.
    let has_started_name = !line_before_point.ends_with(|c: char| c.is_whitespace());

    let mut words = line_before_point.split_whitespace().rev();

    // Make sure we haven't finished the name and started generics or arguments
    if has_started_name {
        if let Some(ident) = words.next() {
            if ident.chars().any(|c| !is_ident_char(c)) {
                return false;
            }
        }
    }
    
    words
        .next()
        .map(|word| word == "fn")
        .unwrap_or_default()
}

#[test]
fn test_in_fn_name() {
    assert!(in_fn_name("fn foo"));
    assert!(in_fn_name(" fn  foo"));
    assert!(in_fn_name("fn "));
    assert!(!in_fn_name("fn foo(b"));
    assert!(!in_fn_name("fn"));
}