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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
use {core, ast, matchers, scopes, typeinf};
use core::SearchType::{self, ExactMatch, StartsWith};
use core::{Match, Src, Session};
use core::MatchType::{Module, Function, Struct, Enum, FnArg, Trait, StructField, Impl, MatchArm};
use core::Namespace::{self, TypeNamespace, ValueNamespace, BothNamespaces};
use util::{symbol_matches, txt_matches, find_ident_end, path_exists};
use cargo;
use std::path::{Path, PathBuf};
use std::{self, vec};
#[cfg(unix)]
pub const PATH_SEP: &'static str = ":";
#[cfg(windows)]
pub const PATH_SEP: &'static str = ";";
fn search_struct_fields(searchstr: &str, structmatch: &Match,
search_type: SearchType, session: &Session) -> vec::IntoIter<Match> {
let src = session.load_file(&structmatch.filepath);
let opoint = scopes::find_stmt_start(src, structmatch.point);
let structsrc = scopes::end_of_next_scope(&src[opoint.unwrap()..]);
let fields = ast::parse_struct_fields(structsrc.to_owned(),
core::Scope::from_match(structmatch));
let mut out = Vec::new();
for (field, fpos, _) in fields.into_iter() {
if symbol_matches(search_type, searchstr, &field) {
out.push(Match { matchstr: field.clone(),
filepath: structmatch.filepath.to_path_buf(),
point: fpos + opoint.unwrap(),
local: structmatch.local,
mtype: StructField,
contextstr: field,
generic_args: Vec::new(), generic_types: Vec::new()
});
}
}
out.into_iter()
}
pub fn search_for_impl_methods(implsearchstr: &str,
fieldsearchstr: &str, point: usize,
fpath: &Path, local: bool,
search_type: SearchType,
session: &Session) -> vec::IntoIter<Match> {
debug!("searching for impl methods |{}| |{}| {:?}", implsearchstr, fieldsearchstr, fpath.to_str());
let mut out = Vec::new();
for m in search_for_impls(point, implsearchstr, fpath, local, true, session) {
debug!("found impl!! |{:?}| looking for methods", m);
let src = session.load_file(&m.filepath);
(&src[m.point..]).find("{").map(|n| {
let point = m.point + n + 1;
for m in search_scope_for_methods(point, src, fieldsearchstr, &m.filepath, search_type) {
out.push(m);
}
});
};
out.into_iter()
}
fn search_scope_for_methods(point: usize, src: Src, searchstr: &str, filepath: &Path,
search_type: SearchType) -> vec::IntoIter<Match> {
debug!("searching scope for methods {} |{}| {:?}", point, searchstr, filepath.to_str());
let scopesrc = src.from(point);
let mut out = Vec::new();
for (blobstart,blobend) in scopesrc.iter_stmts() {
let blob = &scopesrc[blobstart..blobend];
blob.find("{").map(|n| {
let signature = &blob[..n -1];
if txt_matches(search_type, &format!("fn {}", searchstr), signature)
&& typeinf::first_param_is_self(blob) {
debug!("found a method starting |{}| |{}|", searchstr, blob);
let start = blob.find(&format!("fn {}", searchstr)).unwrap() + 3;
let end = find_ident_end(blob, start);
let l = &blob[start..end];
let m = Match {
matchstr: l.to_owned(),
filepath: filepath.to_path_buf(),
point: point + blobstart + start,
local: true,
mtype: Function,
contextstr: signature.to_owned(),
generic_args: Vec::new(), generic_types: Vec::new()
};
out.push(m);
}
});
}
out.into_iter()
}
pub fn search_for_impls(pos: usize, searchstr: &str, filepath: &Path, local: bool, include_traits: bool,
session: &Session) -> vec::IntoIter<Match> {
debug!("search_for_impls {}, {}, {:?}", pos, searchstr, filepath.to_str());
let s = session.load_file(filepath);
let src = s.from(pos);
let mut out = Vec::new();
for (start, end) in src.iter_stmts() {
let blob = &src[start..end];
if blob.starts_with("impl") {
blob.find("{").map(|n| {
let mut decl = (&blob[..n+1]).to_owned();
decl.push_str("}");
if txt_matches(ExactMatch, searchstr, &decl) {
debug!("impl decl {}", decl);
let implres = ast::parse_impl(decl);
let is_trait_impl = implres.trait_path.is_some();
implres.name_path.map(|name_path| {
name_path.segments.last().map(|name| {
if symbol_matches(ExactMatch, searchstr, &name.name) {
let m = Match {
matchstr: name.name.clone(),
filepath: filepath.to_path_buf(),
point: pos + start + 5,
local: local || is_trait_impl,
mtype: Impl,
contextstr: "".into(),
generic_args: Vec::new(),
generic_types: Vec::new()
};
out.push(m);
}
});
});
if include_traits && is_trait_impl {
let trait_path = implres.trait_path.unwrap();
let m = resolve_path(&trait_path,
filepath, pos + start, ExactMatch, TypeNamespace,
session).nth(0);
debug!("found trait |{:?}| {:?}", trait_path, m);
m.map(|m| out.push(m));
}
}
});
}
}
out.into_iter()
}
fn search_scope_headers(point: usize, scopestart: usize, msrc: Src, searchstr: &str,
filepath: &Path, search_type: SearchType) -> vec::IntoIter<Match> {
debug!("search_scope_headers for |{}| pt: {}", searchstr, scopestart);
if let Some(stmtstart) = scopes::find_stmt_start(msrc, scopestart) {
let preblock = &msrc[stmtstart..scopestart];
debug!("search_scope_headers preblock is |{}|", preblock);
if preblock.starts_with("fn") || preblock.starts_with("pub fn") || preblock.starts_with("pub const fn") {
return search_fn_args(stmtstart, scopestart, &msrc, searchstr, filepath, search_type, true);
} else if let Some(n) = preblock.find("if let") {
let ifletstart = stmtstart + n;
let src = (&msrc[ifletstart..scopestart+1]).to_owned() + "}";
if txt_matches(search_type, searchstr, &src) {
let mut out = matchers::match_if_let(&src, 0, src.len(), searchstr,
filepath, search_type, true);
for m in &mut out {
m.point += ifletstart;
}
return out.into_iter();
}
} else if preblock.starts_with("while let") {
let src = (&msrc[stmtstart..scopestart+1]).to_owned() + "}";
if txt_matches(search_type, searchstr, &src) {
let mut out = matchers::match_while_let(&src, 0, src.len(), searchstr,
filepath, search_type, true);
for m in &mut out {
m.point += stmtstart;
}
return out.into_iter();
}
} else if preblock.starts_with("for ") {
let src = (&msrc[stmtstart..scopestart+1]).to_owned() + "}";
if txt_matches(search_type, searchstr, &msrc[..scopestart]) {
let mut out = matchers::match_for(&src, 0, src.len(), searchstr,
filepath, search_type, true);
for m in &mut out {
m.point += stmtstart;
}
return out.into_iter();
}
} else if let Some(n) = preblock.rfind("match ") {
let matchstart = stmtstart + n;
let matchstmt = typeinf::get_first_stmt(msrc.from(matchstart));
let masked_matchstmt = mask_matchstmt(&matchstmt, scopestart + 1 - matchstart);
debug!("found match stmt, masked is len {} |{}|",
masked_matchstmt.len(), masked_matchstmt);
let arm = match masked_matchstmt[..point-matchstart].rfind("=>") {
None =>
return Vec::new().into_iter(),
Some(arm) => {
if let Some(next_arm) = masked_matchstmt[arm+2..].find("=>") {
let enum_start = scopes::get_start_of_pattern(&masked_matchstmt, arm+next_arm+1);
if point > matchstart+enum_start { return Vec::new().into_iter(); }
}
arm
}
};
debug!("PHIL matched arm rhs is |{}|", &masked_matchstmt[arm..]);
let lhs_start = scopes::get_start_of_pattern(&msrc, matchstart + arm);
let lhs = &msrc[lhs_start..matchstart + arm];
let faux_prefix_size = scopestart - matchstart + 1;
let fauxmatchstmt = format!("{}{{{} => () }};", &msrc[matchstart..scopestart], lhs);
debug!("PHIL arm lhs is |{}|", lhs);
debug!("PHIL arm fauxmatchstmt is |{}|, {}", fauxmatchstmt, faux_prefix_size);
let mut out = Vec::new();
for (start,end) in ast::parse_pat_idents(fauxmatchstmt) {
let (start,end) = (lhs_start + start - faux_prefix_size,
lhs_start + end - faux_prefix_size);
let s = &msrc[start..end];
if symbol_matches(search_type, searchstr, s) {
out.push(Match {
matchstr: s.to_owned(),
filepath: filepath.to_path_buf(),
point: start,
local: true,
mtype: MatchArm,
contextstr: lhs.trim().to_owned(),
generic_args: Vec::new(),
generic_types: Vec::new()
});
if let SearchType::ExactMatch = search_type {
break;
}
}
}
return out.into_iter();
}
}
Vec::new().into_iter()
}
fn mask_matchstmt(matchstmt_src: &str, innerscope_start: usize) -> String {
let s = scopes::mask_sub_scopes(&matchstmt_src[innerscope_start..]);
(&matchstmt_src[..innerscope_start]).to_owned() + &s
}
#[test]
fn does_it() {
let src = "
match foo {
Some(a) => { something }
}";
let res = mask_matchstmt(src, src.find('{').unwrap()+1);
debug!("PHIL res is |{}|",res);
}
fn search_fn_args(fnstart: usize, open_brace_pos: usize, msrc: &str,
searchstr: &str, filepath: &Path,
search_type: SearchType, local: bool) -> vec::IntoIter<Match> {
let mut out = Vec::new();
let mut fndecl = String::new();
fndecl.push_str("impl blah {");
let impl_header_len = fndecl.len();
fndecl.push_str(&msrc[fnstart..(open_brace_pos+1)]);
fndecl.push_str("}}");
debug!("search_fn_args: found start of fn!! {} |{}| {}", fnstart, fndecl, searchstr);
if txt_matches(search_type, searchstr, &fndecl) {
let coords = ast::parse_fn_args(fndecl.clone());
for (start,end) in coords {
let s = &fndecl[start..end];
debug!("search_fn_args: arg str is |{}|", s);
if symbol_matches(search_type, searchstr, s) {
let m = Match {
matchstr: s.to_owned(),
filepath: filepath.to_path_buf(),
point: fnstart + start - impl_header_len,
local: local,
mtype: FnArg,
contextstr: s.to_owned(),
generic_args: Vec::new(),
generic_types: Vec::new()
};
debug!("search_fn_args matched: {:?}", m);
out.push(m);
}
}
}
out.into_iter()
}
pub fn do_file_search(searchstr: &str, currentdir: &Path) -> vec::IntoIter<Match> {
debug!("do_file_search {}", searchstr);
let mut out = Vec::new();
let srcpaths = std::env::var("RUST_SRC_PATH").unwrap_or("".into());
debug!("do_file_search srcpaths {}", srcpaths);
let mut v = srcpaths.split(PATH_SEP).collect::<Vec<_>>();
v.push(currentdir.to_str().unwrap());
debug!("do_file_search v is {:?}", v);
for srcpath in v.into_iter() {
if let Ok(iter) = std::fs::read_dir(&Path::new(srcpath)) {
for fpath_buf in iter.filter_map(|res| res.ok().map(|entry| entry.path())) {
let fname = match fpath_buf.file_name().and_then(|n| n.to_str()) {
Some(fname) => fname,
None => continue,
};
if fname.starts_with(&format!("lib{}", searchstr)) {
let filepath = fpath_buf.join("lib.rs");
if path_exists(&filepath) {
let m = Match {
matchstr: (&fname[3..]).to_owned(),
filepath: filepath.to_path_buf(),
point: 0,
local: false,
mtype: Module,
contextstr: (&fname[3..]).to_owned(),
generic_args: Vec::new(),
generic_types: Vec::new()
};
out.push(m);
}
}
if fname.starts_with(searchstr) {
for name in &[&format!("{}.rs", fname)[..], "mod.rs", "lib.rs"] {
let filepath = fpath_buf.join(name);
if path_exists(&filepath) {
let m = Match {
matchstr: fname.to_owned(),
filepath: filepath.to_path_buf(),
point: 0,
local: false,
mtype: Module,
contextstr: filepath.to_str().unwrap().to_owned(),
generic_args: Vec::new(),
generic_types: Vec::new()
};
out.push(m);
}
}
if fname.ends_with(".rs") && path_exists(&fpath_buf) {
let m = Match {
matchstr: (&fname[..(fname.len()-3)]).to_owned(),
filepath: fpath_buf.clone(),
point: 0,
local: false,
mtype: Module,
contextstr: fpath_buf.to_str().unwrap().to_owned(),
generic_args: Vec::new(),
generic_types: Vec::new()
};
out.push(m);
}
}
}
}
}
out.into_iter()
}
pub fn search_crate_root(pathseg: &core::PathSegment, modfpath: &Path,
searchtype: SearchType, namespace: Namespace,
session: &Session) -> vec::IntoIter<Match> {
debug!("search_crate_root |{:?}| {:?}", pathseg, modfpath.to_str());
let crateroots = find_possible_crate_root_modules(modfpath.parent().unwrap());
let mut out = Vec::new();
for crateroot in crateroots {
if *modfpath == *crateroot {
continue;
}
debug!("going to search for {:?} in crateroot {:?}", pathseg, crateroot.to_str());
for m in resolve_name(pathseg, &crateroot, 0, searchtype, namespace, session) {
out.push(m);
if let ExactMatch = searchtype {
break;
}
}
break
}
out.into_iter()
}
pub fn find_possible_crate_root_modules(currentdir: &Path) -> Vec<PathBuf> {
let mut res = Vec::new();
{
let filepath = currentdir.join("lib.rs");
if path_exists(&filepath) {
res.push(filepath.to_path_buf());
return res;
}
}
{
let filepath = currentdir.join("main.rs");
if path_exists(&filepath) {
res.push(filepath.to_path_buf());
return res;
}
}
{
if let Some(parentdir) = currentdir.parent() {
if parentdir != currentdir {
res.extend(find_possible_crate_root_modules(&parentdir).iter().cloned());
return res;
}
}
}
res
}
pub fn search_next_scope(mut startpoint: usize, pathseg: &core::PathSegment,
filepath:&Path, search_type: SearchType, local: bool,
namespace: Namespace,
session: &Session) -> vec::IntoIter<Match> {
let filesrc = session.load_file(filepath);
if startpoint != 0 {
let src = &filesrc[startpoint..];
src.find("{").map(|n| {
startpoint += n + 1;
});
}
search_scope(startpoint, startpoint, filesrc, pathseg, filepath, search_type, local, namespace, session)
}
pub fn get_crate_file(name: &str, from_path: &Path) -> Option<PathBuf> {
debug!("get_crate_file {}", name);
if let Some(p) = cargo::get_crate_file(name, from_path) {
debug!("nameres::get_crate_file - found the crate file! {:?}", p);
return Some(p);
}
let srcpaths = std::env::var("RUST_SRC_PATH").unwrap();
let v = (&srcpaths).split(PATH_SEP).collect::<Vec<_>>();
for srcpath in v.into_iter() {
{
let cratelibname = format!("lib{}", name);
let filepath = Path::new(srcpath).join(cratelibname).join("lib.rs");
if path_exists(&filepath) {
return Some(filepath.to_path_buf());
}
}
{
let filepath = Path::new(srcpath).join(name).join("lib.rs");
if path_exists(&filepath) {
return Some(filepath.to_path_buf());
}
}
}
None
}
pub fn get_module_file(name: &str, parentdir: &Path) -> Option<PathBuf> {
{
let filepath = parentdir.join(format!("{}.rs", name));
if path_exists(&filepath) {
return Some(filepath.to_path_buf());
}
}
{
let filepath = parentdir.join(name).join("mod.rs");
if path_exists(&filepath) {
return Some(filepath.to_path_buf());
}
}
None
}
pub fn search_scope(start: usize, point: usize, src: Src,
pathseg: &core::PathSegment,
filepath:&Path, search_type: SearchType, local: bool,
namespace: Namespace,
session: &Session) -> vec::IntoIter<Match> {
let searchstr = &pathseg.name;
let mut out = Vec::new();
debug!("searching scope {:?} start: {} point: {} '{}' {:?} {:?} local: {}, session: {:?}",
namespace, start, point, searchstr, filepath.to_str(), search_type, local, session);
let scopesrc = src.from(start);
let mut skip_next_block = false;
let mut delayed_use_globs = Vec::new();
let mut codeit = scopesrc.iter_stmts();
let mut v = Vec::new();
for (blobstart, blobend) in &mut codeit {
if skip_next_block {
skip_next_block = false;
continue;
}
let blob = &scopesrc[blobstart..blobend];
if blob.starts_with("#[cfg(test)") {
skip_next_block = true;
continue;
}
v.push((blobstart,blobend));
if blobstart > point {
break;
}
}
for &(blobstart, blobend) in v.iter().rev() {
if (start+blobend) >= point {
continue;
}
for m in matchers::match_let(&src, start+blobstart,
start+blobend,
searchstr,
filepath, search_type, local).into_iter() {
out.push(m);
if let ExactMatch = search_type {
return out.into_iter();
}
}
}
let mut codeit = v.into_iter().chain(codeit);
for (blobstart, blobend) in &mut codeit {
if skip_next_block {
skip_next_block = false;
continue;
}
let blob = &scopesrc[blobstart..blobend];
if blob.starts_with("#[cfg(test)") {
skip_next_block = true;
continue;
}
let is_a_use_glob = (blob.starts_with("use") || blob.starts_with("pub use"))
&& blob.find("::*").is_some();
if is_a_use_glob {
delayed_use_globs.push((blobstart, blobend));
continue;
}
if searchstr == "core" && blob.starts_with("#![no_std]") {
debug!("Looking for core and found #![no_std], which implicitly imports it");
get_crate_file("core", filepath).map(|cratepath| {
out.push(Match { matchstr: "core".into(),
filepath: cratepath.to_path_buf(),
point: 0,
local: false,
mtype: Module,
contextstr: cratepath.to_str().unwrap().to_owned(),
generic_args: Vec::new(),
generic_types: Vec::new()
});
});
}
if blob.find(searchstr).is_none() {
continue;
}
out.extend(run_matchers_on_blob(src, start+blobstart, start+blobend,
searchstr,
filepath, search_type, local, namespace, session));
if let ExactMatch = search_type {
if !out.is_empty() {
return out.into_iter();
}
}
}
for (blobstart, blobend) in delayed_use_globs {
for m in run_matchers_on_blob(src, start+blobstart, start+blobend,
searchstr, filepath, search_type,
local, namespace, session).into_iter() {
out.push(m);
if let ExactMatch = search_type {
return out.into_iter();
}
}
}
debug!("search_scope found matches {:?} {:?}", search_type, out);
out.into_iter()
}
fn run_matchers_on_blob(src: Src, start: usize, end: usize, searchstr: &str,
filepath: &Path, search_type: SearchType, local: bool,
namespace: Namespace, session: &Session) -> Vec<Match> {
let mut out = Vec::new();
match namespace {
TypeNamespace =>
for m in matchers::match_types(src, start,
end, searchstr,
filepath, search_type, local, session) {
out.push(m);
if let ExactMatch = search_type {
return out;
}
},
ValueNamespace =>
for m in matchers::match_values(src, start,
end, searchstr,
filepath, search_type, local) {
out.push(m);
if let ExactMatch = search_type {
return out;
}
},
BothNamespaces => {
for m in matchers::match_types(src, start,
end, searchstr,
filepath, search_type, local, session) {
out.push(m);
if let ExactMatch = search_type {
return out;
}
}
for m in matchers::match_values(src, start,
end, searchstr,
filepath, search_type, local) {
out.push(m);
if let ExactMatch = search_type {
return out;
}
}
}
}
out
}
fn search_local_scopes(pathseg: &core::PathSegment, filepath: &Path,
msrc: Src, point: usize, search_type: SearchType,
namespace: Namespace,
session: &Session) -> vec::IntoIter<Match> {
debug!("search_local_scopes {:?} {:?} {} {:?} {:?}", pathseg, filepath.to_str(), point,
search_type, namespace);
if point == 0 {
search_scope(0, 0, msrc, pathseg, filepath, search_type, true, namespace, session)
} else {
let mut out = Vec::new();
let mut start = point;
while start > 0 {
start = scopes::scope_start(msrc, start);
for m in search_scope(start, point, msrc, pathseg, filepath, search_type, true, namespace, session) {
out.push(m);
if let ExactMatch = search_type {
return out.into_iter();
}
}
if start == 0 {
break;
}
start = start-1;
let searchstr = &pathseg.name;
for m in search_scope_headers(point, start, msrc, searchstr, filepath, search_type) {
out.push(m);
if let ExactMatch = search_type {
return out.into_iter();
}
}
}
out.into_iter()
}
}
pub fn search_prelude_file(pathseg: &core::PathSegment, search_type: SearchType,
namespace: Namespace, session: &Session) -> vec::IntoIter<Match> {
debug!("search_prelude file {:?} {:?} {:?}", pathseg, search_type, namespace);
let mut out : Vec<Match> = Vec::new();
let srcpaths = match std::env::var("RUST_SRC_PATH") {
Ok(paths) => paths,
Err(_) => return out.into_iter()
};
let v = srcpaths.split(PATH_SEP).collect::<Vec<_>>();
for srcpath in v.into_iter() {
let filepath = Path::new(srcpath).join("libstd").join("prelude").join("v1.rs");
if path_exists(&filepath) {
let msrc = session.load_file_and_mask_comments(&filepath);
let is_local = true;
for m in search_scope(0, 0, msrc, pathseg, &filepath, search_type, is_local, namespace, session) {
out.push(m);
}
}
}
out.into_iter()
}
pub fn resolve_path_with_str(path: &core::Path, filepath: &Path, pos: usize,
search_type: SearchType, namespace: Namespace,
session: &Session) -> vec::IntoIter<Match> {
debug!("resolve_path_with_str {:?}", path);
let mut out = Vec::new();
if path.segments.len() == 1 && path.segments[0].name == "str" {
debug!("{:?} == {:?}", path.segments[0], "str");
let str_pathseg = core::PathSegment{ name: "Str".into(), types: Vec::new() };
let str_match = resolve_name(&str_pathseg, filepath, pos, ExactMatch, namespace, session).nth(0);
debug!("str_match {:?}", str_match);
str_match.map(|str_match| {
debug!("found Str, converting to str");
let m = Match {
matchstr: "str".into(),
filepath: str_match.filepath.to_path_buf(),
point: str_match.point,
local: false,
mtype: Struct,
contextstr: "str".into(),
generic_args: Vec::new(),
generic_types: Vec::new()
};
out.push(m);
});
} else {
if path.segments.len() == 1 && path.segments[0].name == "Box" {
let container = path.segments[0].types[0].segments[0].name.clone();
debug!("Found Box Object Containing {:?}", path.segments[0]);
let container_path = core::PathSegment {
name: container,
types: Vec::new(),
};
for m in resolve_name(&container_path, filepath, pos, search_type, namespace, session){
out.push(m);
}
}
for m in resolve_path(path, filepath, pos, search_type, namespace, session) {
out.push(m);
if let ExactMatch = search_type {
break;
}
}
}
out.into_iter()
}
thread_local!(pub static SEARCH_STACK: Vec<Search> = Vec::new());
#[derive(PartialEq,Debug)]
pub struct Search {
path: Vec<String>,
filepath: String,
pos: usize
}
pub fn is_a_repeat_search(new_search: &Search) -> bool {
SEARCH_STACK.with(|v| {
for s in v {
if s == new_search {
debug!("is a repeat search {:?} Stack: {:?}", new_search, v);
return true;
}
}
false
})
}
pub fn resolve_name(pathseg: &core::PathSegment, filepath: &Path, pos: usize,
search_type: SearchType, namespace: Namespace,
session: &Session) -> vec::IntoIter<Match> {
let mut out = Vec::new();
let searchstr = &pathseg.name;
debug!("resolve_name {} {:?} {} {:?} {:?}", searchstr, filepath.to_str(), pos, search_type, namespace);
let msrc = session.load_file_and_mask_comments(filepath);
let is_exact_match = match search_type { ExactMatch => true, StartsWith => false };
if (is_exact_match && &searchstr[..] == "std") ||
(!is_exact_match && "std".starts_with(searchstr)) {
get_crate_file("std", filepath).map(|cratepath| {
out.push(Match {
matchstr: "std".into(),
filepath: cratepath.to_path_buf(),
point: 0,
local: false,
mtype: Module,
contextstr: cratepath.to_str().unwrap().to_owned(),
generic_args: Vec::new(), generic_types: Vec::new()
});
});
if let ExactMatch = search_type {
if !out.is_empty() {
return out.into_iter();
}
}
}
for m in search_local_scopes(pathseg, filepath, msrc, pos, search_type, namespace, session) {
out.push(m);
if let ExactMatch = search_type {
if !out.is_empty() {
return out.into_iter();
}
}
}
for m in search_crate_root(pathseg, &filepath, search_type, namespace, session) {
out.push(m);
if let ExactMatch = search_type {
if !out.is_empty() {
return out.into_iter();
}
}
}
for m in search_prelude_file(pathseg, search_type, namespace, session) {
out.push(m);
if let ExactMatch = search_type {
if !out.is_empty() {
return out.into_iter();
}
}
}
if let StartsWith = search_type {
for m in do_file_search(searchstr, &filepath.parent().unwrap()) {
out.push(m);
}
}
out.into_iter()
}
pub fn get_super_scope(filepath: &Path, pos: usize, session: &Session) -> Option<core::Scope> {
let msrc = session.load_file_and_mask_comments(filepath);
let mut path = scopes::get_local_module_path(msrc, pos);
debug!("get_super_scope: path: {:?} filepath: {:?} {} {:?}", path, filepath, pos, session);
if path.is_empty() {
let moduledir;
if filepath.ends_with("mod.rs") || filepath.ends_with("lib.rs"){
moduledir = filepath.parent().unwrap().parent().unwrap();
} else {
moduledir = filepath.parent().unwrap();
}
for filename in &[ "mod.rs", "lib.rs" ] {
let fpath = moduledir.join(&filename);
if path_exists(&fpath) {
return Some(core::Scope{ filepath: fpath, point: 0 })
}
}
None
} else if path.len() == 1 {
Some(core::Scope{ filepath: filepath.to_path_buf(), point: 0 })
} else {
path.pop();
let path = core::Path::from_svec(false, path);
debug!("get_super_scope looking for local scope {:?}", path);
resolve_path(&path, filepath, 0, SearchType::ExactMatch,
Namespace::TypeNamespace, session).nth(0)
.and_then(|m| msrc[m.point..].find("{")
.map(|p| core::Scope{ filepath: filepath.to_path_buf(),
point:m.point + p + 1 }))
}
}
pub fn resolve_path(path: &core::Path, filepath: &Path, pos: usize,
search_type: SearchType, namespace: Namespace,
session: &Session) -> vec::IntoIter<Match> {
debug!("resolve_path {:?} {:?} {} {:?}", path, filepath.to_str(), pos, search_type);
let len = path.segments.len();
if len == 1 {
let ref pathseg = path.segments[0];
resolve_name(pathseg, filepath, pos, search_type, namespace, session)
} else if len != 0 {
if path.segments[0].name == "self" {
let mut newpath: core::Path = path.clone();
newpath.segments.remove(0);
return resolve_path(&newpath, filepath, pos, search_type, namespace, session);
}
if path.segments[0].name == "super" {
if let Some(scope) = get_super_scope(filepath, pos, session) {
debug!("PHIL super scope is {:?}", scope);
let mut newpath: core::Path = path.clone();
newpath.segments.remove(0);
return resolve_path(&newpath, &scope.filepath,
scope.point, search_type, namespace, session);
} else {
debug!("can't resolve path {:?}, returning no matches", path);
return Vec::new().into_iter();
}
}
let mut out = Vec::new();
let mut parent_path: core::Path = path.clone();
parent_path.segments.remove(len-1);
let context = resolve_path(&parent_path, filepath, pos, ExactMatch, TypeNamespace, session).nth(0);
context.map(|m| {
match m.mtype {
Module => {
let ref pathseg = path.segments[len-1];
debug!("searching a module '{}' for {} (whole path: {:?})", m.matchstr, pathseg.name, path);
for m in search_next_scope(m.point, pathseg, &m.filepath, search_type, false, namespace, session) {
out.push(m);
}
}
Enum => {
let ref pathseg = path.segments[len-1];
debug!("searching an enum '{}' (whole path: {:?}) searchtype: {:?}", m.matchstr, path, search_type);
let filesrc = session.load_file(&m.filepath);
let scopestart = scopes::find_stmt_start(filesrc, m.point).unwrap();
let scopesrc = filesrc.from(scopestart);
scopesrc.iter_stmts().nth(0).map(|(blobstart,blobend)| {
for m in matchers::match_enum_variants(&filesrc,
scopestart+blobstart,
scopestart+blobend,
&pathseg.name, &m.filepath, search_type, true) {
debug!("Found enum variant: {}", m.matchstr);
out.push(m);
}
});
}
Struct => {
debug!("found a struct. Now need to look for impl");
for m in search_for_impls(m.point, &m.matchstr, &m.filepath, m.local, false, session) {
debug!("found impl!! {:?}", m);
let ref pathseg = path.segments[len-1];
let src = session.load_file(&m.filepath);
(&src[m.point..]).find("{").map(|n| {
let point = m.point + n + 1;
for m in search_scope(point, point, src, pathseg, &m.filepath, search_type, m.local, namespace, session) {
out.push(m);
}
});
};
}
_ => ()
}
});
debug!("resolve_path returning {:?}", out);
out.into_iter()
} else {
Vec::new().into_iter()
}
}
pub fn do_external_search(path: &[&str], filepath: &Path, pos: usize, search_type: SearchType, namespace: Namespace,
session: &Session) -> vec::IntoIter<Match> {
debug!("do_external_search path {:?} {:?}", path, filepath.to_str());
let mut out = Vec::new();
if path.len() == 1 {
let searchstr = path[0];
let pathseg = core::PathSegment{name: searchstr.to_owned(),
types: Vec::new()};
for m in search_next_scope(pos, &pathseg, filepath, search_type, false, namespace, session) {
out.push(m);
}
get_module_file(searchstr, &filepath.parent().unwrap()).map(|path| {
out.push(Match {
matchstr: searchstr.to_owned(),
filepath: path.to_path_buf(),
point: 0,
local: false,
mtype: Module,
contextstr: path.to_str().unwrap().to_owned(),
generic_args: Vec::new(),
generic_types: Vec::new()
});
});
} else {
let parent_path = &path[..(path.len()-1)];
let context = do_external_search(parent_path, filepath, pos, ExactMatch, TypeNamespace, session).nth(0);
context.map(|m| {
match m.mtype {
Module => {
debug!("found an external module {}", m.matchstr);
let searchstr = path[path.len()-1];
let pathseg = core::PathSegment{name: searchstr.to_owned(),
types: Vec::new()};
for m in search_next_scope(m.point, &pathseg, &m.filepath, search_type, false, namespace, session) {
out.push(m);
}
}
Struct => {
debug!("found a pub struct. Now need to look for impl");
for m in search_for_impls(m.point, &m.matchstr, &m.filepath, m.local, false, session) {
debug!("found impl2!! {}", m.matchstr);
let searchstr = path[path.len()-1];
let pathseg = core::PathSegment{name: searchstr.to_owned(),
types: Vec::new()};
debug!("about to search impl scope...");
for m in search_next_scope(m.point, &pathseg, &m.filepath, search_type, m.local, namespace, session) {
out.push(m);
}
};
}
_ => ()
}
});
}
out.into_iter()
}
pub fn search_for_field_or_method(context: Match, searchstr: &str, search_type: SearchType,
session: &Session) -> vec::IntoIter<Match> {
let m = context;
let mut out = Vec::new();
match m.mtype {
Struct => {
debug!("got a struct, looking for fields and impl methods!! {}", m.matchstr);
for m in search_struct_fields(searchstr, &m, search_type, session) {
out.push(m);
}
for m in search_for_impl_methods(&m.matchstr,
searchstr,
m.point,
&m.filepath,
m.local,
search_type,
session) {
out.push(m);
}
},
Enum => {
debug!("got an enum, looking for impl methods {}", m.matchstr);
for m in search_for_impl_methods(&m.matchstr,
searchstr,
m.point,
&m.filepath,
m.local,
search_type,
session) {
out.push(m);
}
},
Trait => {
debug!("got a trait, looking for methods {}", m.matchstr);
let src = session.load_file(&m.filepath);
(&src[m.point..]).find("{").map(|n| {
let point = m.point + n + 1;
for m in search_scope_for_methods(point, src, searchstr, &m.filepath, search_type) {
out.push(m);
}
});
}
_ => { debug!("WARN!! context wasn't a Struct, Enum or Trait {:?}",m);}
};
out.into_iter()
}