-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpersistence.rs
3239 lines (2783 loc) · 120 KB
/
persistence.rs
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
use filetime::FileTime;
use jwalk::WalkDirGeneric;
use lib_ruby_parser::source::DecodedInput;
use lib_ruby_parser::{nodes::*, Loc, Node, Parser, ParserOptions};
use log::info;
use phf::phf_map;
use regex::Regex;
use serde_json::json;
use std::collections::{HashMap, HashSet};
use std::fs;
use std::process::Command;
use std::str;
use tantivy::collector::TopDocs;
use tantivy::query::{BooleanQuery, BoostQuery, Occur, Query, RegexQuery, TermQuery};
use tantivy::{schema::*, ReloadPolicy, Document};
use tantivy::{Index, IndexWriter};
use tower_lsp::lsp_types::InitializeParams;
use tower_lsp::lsp_types::{
DocumentHighlight, DocumentHighlightKind, Location, Position, Range, SymbolInformation,
SymbolKind, TextDocumentPositionParams, TextEdit, Url, WorkspaceEdit,
};
use tower_lsp::Client;
static USAGE_TYPE_RESTRICTIONS: phf::Map<&'static str, &[&str]> = phf_map! {
"Alias" => &[
"Alias", "Def", "Defs",
"CSend", "Send", "Super", "ZSuper",
],
"Const" => &[
"Casgn", "Class", "Module",
"Const"
],
"CSend" => &[
"Alias", "Def", "Defs",
"CSend", "Send", "Super", "ZSuper",
],
"Cvar" => &[
"Cvasgn",
"Cvar"
],
"Gvar" => &[
"Gvasgn",
"Gvar"
],
"Ivar" => &[
"Ivasgn",
"Ivar"
],
"Lvar" => &[
"Arg", "Kwarg", "Kwoptarg", "Kwrestarg", "Lvasgn", "MatchVar", "Optarg", "Restarg", "Shadowarg",
"Lvar"
],
"Send" => &[
"Alias", "Def", "Defs",
"CSend", "Send", "Super", "ZSuper",
],
"Super" => &[
"Alias", "Def", "Defs",
"CSend", "Send", "Super", "ZSuper",
],
"ZSuper" => &[
"Alias", "Def", "Defs",
"CSend", "Send", "Super", "ZSuper",
],
};
static ASSIGNMENT_TYPE_RESTRICTIONS: phf::Map<&'static str, &[&str]> = phf_map! {
"Alias" => &[
"Alias", "CSend", "Send", "Super", "ZSuper",
"Def", "Defs"
],
"Arg" => &[
"Lvar",
"Arg", "Kwarg", "Kwoptarg", "Kwrestarg", "Lvasgn", "MatchVar", "Optarg", "Restarg", "Shadowarg"
],
"Casgn" => &[
"Const",
"Casgn", "Class", "Module"
],
"Class" => &[
"Const",
"Casgn", "Class", "Module"
],
"Cvasgn" => &[
"Cvar",
"Cvasgn"
],
"Def" => &[
"Alias", "CSend", "Send", "Super", "ZSuper",
"Def"
],
"Defs" => &[
"Alias", "CSend", "Send", "Super", "ZSuper",
"Defs"
],
"Gvasgn" => &[
"Gvar",
"Gvasgn"
],
"Ivasgn" => &[
"Ivar",
"Ivasgn"
],
"Kwarg" => &[
"Lvar",
"Arg", "Kwarg", "Kwoptarg", "Kwrestarg", "Lvasgn", "MatchVar", "Optarg", "Restarg", "Shadowarg"
],
"Kwoptarg" => &[
"Lvar",
"Arg", "Kwarg", "Kwoptarg", "Kwrestarg", "Lvasgn", "MatchVar", "Optarg", "Restarg", "Shadowarg"
],
"Kwrestarg" => &[
"Lvar",
"Arg", "Kwarg", "Kwoptarg", "Kwrestarg", "Lvasgn", "MatchVar", "Optarg", "Restarg", "Shadowarg"
],
"Lvasgn" => &[
"Lvar",
"Arg", "Kwarg", "Kwoptarg", "Kwrestarg", "Lvasgn", "MatchVar", "Optarg", "Restarg", "Shadowarg"
],
"MatchVar" => &[
"Lvar",
"Arg", "Kwarg", "Kwoptarg", "Kwrestarg", "Lvasgn", "MatchVar", "Optarg", "Restarg", "Shadowarg"
],
"Module" => &[
"Const",
"Casgn", "Class", "Module"
],
"Optarg" => &[
"Lvar",
"Arg", "Kwarg", "Kwoptarg", "Kwrestarg", "Lvasgn", "MatchVar", "Optarg", "Restarg", "Shadowarg"
],
"Restarg" => &[
"Lvar",
"Arg", "Kwarg", "Kwoptarg", "Kwrestarg", "Lvasgn", "MatchVar", "Optarg", "Restarg", "Shadowarg"
],
"Shadowarg" => &[
"Lvar",
"Arg", "Kwarg", "Kwoptarg", "Kwrestarg", "Lvasgn", "MatchVar", "Optarg", "Restarg", "Shadowarg"
],
};
#[derive(Clone)]
pub struct IndexableDir {
path: String,
interface_only: bool,
}
pub struct Persistence {
schema: Schema,
schema_fields: SchemaFields,
index: Option<Index>,
workspace_path: String,
last_reindex_time: i64,
indexed_file_paths: HashSet<String>,
process_id: Option<u32>,
no_workspace: bool,
gems_indexed: bool,
include_dirs_indexed: bool,
index_interface_only: bool,
class_scope: Vec<String>,
include_dirs: Vec<IndexableDir>,
pub report_diagnostics: bool,
}
struct SchemaFields {
file_path_id: Field,
file_path: Field,
category_field: Field,
fuzzy_ruby_scope_field: Field,
class_scope_field: Field,
name_field: Field,
node_type_field: Field,
line_field: Field,
start_column_field: Field,
end_column_field: Field,
columns_field: Field,
user_space_field: Field,
}
#[derive(Debug)]
struct FuzzyNode<'a> {
category: &'a str,
fuzzy_ruby_scope: Vec<String>,
class_scope: Vec<String>,
name: String,
node_type: &'a str,
line: usize,
start_column: usize,
end_column: usize,
}
impl Persistence {
pub fn new() -> tantivy::Result<Persistence> {
let mut schema_builder = Schema::builder();
let schema_fields = SchemaFields {
file_path_id: schema_builder.add_text_field(
"file_path_id",
TextOptions::default()
.set_indexing_options(
TextFieldIndexing::default()
.set_tokenizer("raw")
.set_index_option(IndexRecordOption::Basic),
)
.set_stored(),
),
file_path: schema_builder.add_text_field(
"file_path",
TextOptions::default()
.set_indexing_options(
TextFieldIndexing::default()
.set_tokenizer("raw")
.set_index_option(IndexRecordOption::Basic),
)
.set_stored(),
),
category_field: schema_builder.add_text_field(
"category",
TextOptions::default()
.set_indexing_options(
TextFieldIndexing::default()
.set_tokenizer("raw")
.set_index_option(IndexRecordOption::Basic),
)
.set_stored(),
),
fuzzy_ruby_scope_field: schema_builder.add_text_field(
"fuzzy_ruby_scope",
TextOptions::default()
.set_indexing_options(
TextFieldIndexing::default()
.set_tokenizer("raw")
.set_index_option(IndexRecordOption::Basic),
)
.set_stored(),
),
class_scope_field: schema_builder.add_text_field(
"class_scope",
TextOptions::default()
.set_indexing_options(
TextFieldIndexing::default()
.set_tokenizer("raw")
.set_index_option(IndexRecordOption::Basic),
)
.set_stored(),
),
name_field: schema_builder.add_text_field(
"name",
TextOptions::default()
.set_indexing_options(
TextFieldIndexing::default()
.set_tokenizer("raw")
.set_index_option(IndexRecordOption::Basic),
)
.set_stored(),
),
node_type_field: schema_builder.add_text_field(
"node_type",
TextOptions::default()
.set_indexing_options(
TextFieldIndexing::default()
.set_tokenizer("raw")
.set_index_option(IndexRecordOption::Basic),
)
.set_stored(),
),
line_field: schema_builder.add_u64_field("line", INDEXED | STORED),
start_column_field: schema_builder.add_u64_field("start_column", INDEXED | STORED),
end_column_field: schema_builder.add_u64_field("end_column", INDEXED | STORED),
columns_field: schema_builder.add_u64_field("columns", INDEXED | STORED),
user_space_field: schema_builder.add_bool_field("user_space", INDEXED | STORED),
};
let schema = schema_builder.build();
let index = None;
let workspace_path = "unset".to_string();
let last_reindex_time = FileTime::from_unix_time(0, 0).seconds();
let indexed_file_paths = HashSet::new();
let process_id: Option<u32> = None;
let no_workspace = false;
let gems_indexed = false;
let index_interface_only = false;
let class_scope = vec![];
let report_diagnostics = true;
let include_dirs = Vec::new();
let include_dirs_indexed = false;
Ok(Self {
schema,
schema_fields,
index,
workspace_path,
last_reindex_time,
indexed_file_paths,
process_id,
no_workspace,
gems_indexed,
index_interface_only,
class_scope,
report_diagnostics,
include_dirs,
include_dirs_indexed,
})
}
pub fn initialize(&mut self, params: &InitializeParams) {
let uri = params.root_uri.as_ref().unwrap_or_else(|| {
info!("root_uri wasn't given to initialize, exiting.");
quit::with_code(1);
});
self.workspace_path = uri.path().to_string();
let default_user_config = json!({});
let default_allocation_type = json!("ram");
let user_config = ¶ms
.initialization_options
.as_ref()
.unwrap_or(&default_user_config)
.as_object()
.unwrap();
let allocation_type = user_config
.get("allocationType")
.unwrap_or(&default_allocation_type)
.as_str()
.unwrap();
self.index = match allocation_type {
"ram" => Some(Index::create_in_ram(self.schema.clone())),
"tempdir" => Some(Index::create_from_tempdir(self.schema.clone()).unwrap()),
_ => {
info!("Unknown allocation_type, defaulting to tempdir");
Some(Index::create_from_tempdir(self.schema.clone()).unwrap())
}
};
if let Some(included_dirs) = user_config.get("includeDirs") {
if let Some(dirs) = included_dirs.as_array() {
let dirs = dirs
.iter()
.map(|v| {
// v.as_str().unwrap().to_string()
let dir_params = v.as_object().unwrap();
let dir_path = dir_params.get("path").unwrap().as_str().unwrap();
let interface_only = {
let param = dir_params.get("interface_only");
match param {
Some(val) => val.as_bool().unwrap(),
None => true,
}
};
let dir_path = dir_path.to_string();
let absolute_dir_path = if dir_path.starts_with("/") {
dir_path
} else {
format!("{}/{}", &self.workspace_path, dir_path)
};
IndexableDir {
path: absolute_dir_path,
interface_only,
}
})
.collect();
self.include_dirs = dirs;
};
}
let default_index_gems = json!(true);
let skip_indexing_gems = !user_config
.get("indexGems")
.unwrap_or(&default_index_gems)
.as_bool()
.unwrap();
if skip_indexing_gems {
self.gems_indexed = true;
}
let default_report_diagnostics = json!(true);
let report_diagnostics = user_config
.get("reportDiagnostics")
.unwrap_or(&default_report_diagnostics)
.as_bool()
.unwrap();
if !report_diagnostics {
self.report_diagnostics = false;
}
}
pub fn reindex_modified_files(&mut self) -> tantivy::Result<()> {
let start_time = FileTime::from_unix_time(FileTime::now().unix_seconds(), 0).seconds() - 1;
let last_reindex_time = self.last_reindex_time.clone();
let walk_dir = WalkDirGeneric::<(usize, bool)>::new(&self.workspace_path).process_read_dir(
move |_depth, _path, _read_dir_state, children| {
children.retain(|dir_entry_result| {
dir_entry_result
.as_ref()
.map(|dir_entry| {
if let Some(file_name) = dir_entry.file_name.to_str() {
let ruby_file = file_name.ends_with(".rb");
dir_entry.file_type.is_dir() || ruby_file
} else {
false
}
})
.unwrap_or(false)
});
children.iter_mut().for_each(|dir_entry_result| {
if let Ok(dir_entry) = dir_entry_result {
if let Some(file_name) = dir_entry.file_name.to_str() {
if file_name.contains("node_modules")
|| file_name.contains("tmp")
|| file_name.contains(".git")
{
dir_entry.read_children_path = None;
}
}
}
});
},
);
let mut new_indexable_file_paths = HashSet::new();
let mut indexed_file_paths = HashSet::new();
for entry in walk_dir {
let path = entry.unwrap().path();
let path = path.to_str().unwrap();
let ruby_file = path.ends_with(".rb");
if ruby_file {
indexed_file_paths.insert(path.to_string());
self.indexed_file_paths.remove(path);
let metadata = fs::metadata(path).unwrap();
let mtime = FileTime::from_last_modification_time(&metadata);
let recently_modified = mtime.seconds() >= last_reindex_time;
if recently_modified {
new_indexable_file_paths.insert(path.to_string());
}
}
}
if let Some(index) = &self.index {
let files_added = new_indexable_file_paths.len() > 0;
let files_deleted = self.indexed_file_paths.len() > 0;
if files_added || files_deleted {
let mut index_writer = index.writer(256_000_000).unwrap();
for path in &self.indexed_file_paths {
let relative_path = path.replace(&self.workspace_path, "");
let file_path_id = blake3::hash(&relative_path.as_bytes());
let path_term = Term::from_field_text(
self.schema_fields.file_path_id,
&file_path_id.to_string(),
);
index_writer.delete_term(path_term);
}
for path in &new_indexable_file_paths {
let text = fs::read_to_string(&path).unwrap();
let uri = Url::from_file_path(&path).unwrap();
let relative_path = uri.path().replace(&self.workspace_path, "");
self.reindex_modified_file_without_commit(
&text,
relative_path,
&index_writer,
true,
);
}
index_writer.commit().unwrap();
info!("Indexing workspace complete!");
} else {
info!("No file changes, skipping periodic reindexing.")
}
}
self.last_reindex_time = start_time;
self.indexed_file_paths = indexed_file_paths;
Ok(())
}
pub fn index_included_dirs_once(&mut self) -> tantivy::Result<()> {
if self.include_dirs_indexed {
return Ok(());
}
self.index_interface_only = true;
if self.include_dirs.len() > 0 {
let index = match &self.index {
Some(index) => index,
None => {
info!("missing index");
quit::with_code(1);
}
};
let mut index_writer = index.writer(256_000_000).unwrap();
for indexable_dir in self.include_dirs.clone() {
let walk_dir = WalkDirGeneric::<(usize, bool)>::new(indexable_dir.path.clone())
.process_read_dir(move |_depth, _path, _read_dir_state, children| {
children.retain(|dir_entry_result| {
dir_entry_result
.as_ref()
.map(|dir_entry| {
if let Some(file_name) = dir_entry.file_name.to_str() {
let ruby_file = file_name.ends_with(".rb");
dir_entry.file_type.is_dir() || ruby_file
} else {
false
}
})
.unwrap_or(false)
});
children.iter_mut().for_each(|dir_entry_result| {
if let Ok(dir_entry) = dir_entry_result {
if let Some(file_name) = dir_entry.file_name.to_str() {
if file_name.contains("node_modules")
|| file_name.contains("vendor")
|| file_name.contains("tmp")
|| file_name.contains(".git")
{
dir_entry.read_children_path = None;
}
}
}
});
});
let mut indexable_file_paths = Vec::new();
for entry in walk_dir {
let path = entry.unwrap().path();
let path = path.to_str().unwrap();
let ruby_file = path.ends_with(".rb");
if ruby_file {
indexable_file_paths.push(path.to_string());
}
}
self.index_interface_only = indexable_dir.interface_only;
for path in &indexable_file_paths {
if let Ok(text) = fs::read_to_string(&path) {
let uri = Url::from_file_path(&path).unwrap();
let relative_path = uri.path().replace(&self.workspace_path, "");
self.reindex_modified_file_without_commit(
&text,
relative_path,
&index_writer,
false,
);
}
}
}
index_writer.commit().unwrap();
}
self.include_dirs_indexed = true;
self.index_interface_only = false;
Ok(())
}
pub fn index_gems_once(&mut self) -> tantivy::Result<()> {
if self.gems_indexed {
return Ok(());
}
self.index_interface_only = true;
// Four leading spaces dictates that it's a gem version
// https://github.com/rubygems/bundler/blob/v2.1.4/lib/bundler/lockfile_parser.rb#L174-L181
let gem_version = Regex::new(r"^\s{4}([a-zA-Z\d\.\-_]+)\s\(([\d\w\.\-_]+)\)").unwrap();
let gemfile_path = format!("{}/{}", &self.workspace_path, "Gemfile.lock");
if let Ok(gemfile_contents) = fs::read_to_string(gemfile_path) {
let mut gem_paths = vec![];
let mut base_gem_path = "unset";
let gem_home_path_result = Command::new("sh")
.arg("-c")
// .arg(format!("eval \"$(/usr/local/bin/rbenv init -)\" && cd {} && gem environment home", &self.workspace_path))
.arg(format!(
"cd {} && gem environment home",
&self.workspace_path
))
.output();
if let Ok(gem_home_path) = gem_home_path_result {
if let Ok(gem_home_path) = str::from_utf8(gem_home_path.stdout.as_slice()) {
base_gem_path = gem_home_path;
}
// Index Ruby
let ruby_source_path = base_gem_path.replace("gems/", "").replace("\n", "");
info!("Added Ruby source path: {}", ruby_source_path);
gem_paths.push(ruby_source_path);
// Index Gems
for line in gemfile_contents.lines() {
if let Some(captures) = gem_version.captures(line) {
let name = captures[1].to_string();
let version = captures[2].to_string();
let gem_folder_name =
format!("{}/gems/{}-{}", base_gem_path, name, version);
// Not 100% sure where this newline is coming from. `gemfile_contents.lines()` I think.
let gem_folder_name = gem_folder_name.replace("\n", "");
info!("gem folder name: {}", gem_folder_name);
gem_paths.push(gem_folder_name)
}
}
}
let index = match &self.index {
Some(index) => index,
None => {
info!("missing index");
quit::with_code(1);
}
};
let mut index_writer = index.writer(256_000_000).unwrap();
for gem_path in gem_paths {
let walk_dir = WalkDirGeneric::<(usize, bool)>::new(gem_path.clone())
.process_read_dir(move |_depth, _path, _read_dir_state, children| {
children.retain(|dir_entry_result| {
dir_entry_result
.as_ref()
.map(|dir_entry| {
if let Some(file_name) = dir_entry.file_name.to_str() {
let ruby_file = file_name.ends_with(".rb");
dir_entry.file_type.is_dir() || ruby_file
} else {
false
}
})
.unwrap_or(false)
});
children.iter_mut().for_each(|dir_entry_result| {
if let Ok(dir_entry) = dir_entry_result {
if let Some(file_name) = dir_entry.file_name.to_str() {
if file_name.contains("node_modules")
|| file_name.contains("vendor")
|| file_name.contains("tmp")
|| file_name.contains(".git")
{
dir_entry.read_children_path = None;
}
}
}
});
});
let mut indexable_file_paths = Vec::new();
for entry in walk_dir {
let path = entry.unwrap().path();
let path = path.to_str().unwrap();
let ruby_file = path.ends_with(".rb");
if ruby_file {
indexable_file_paths.push(path.to_string());
}
}
for path in &indexable_file_paths {
if let Ok(text) = fs::read_to_string(&path) {
let uri = Url::from_file_path(&path).unwrap();
let relative_path = uri.path().replace(&self.workspace_path, "");
self.reindex_modified_file_without_commit(
&text,
relative_path,
&index_writer,
false,
);
}
}
}
index_writer.commit().unwrap();
} else {
info!("Gemfile not found, skipping indexing workspace gems.");
}
self.gems_indexed = true;
self.index_interface_only = false;
Ok(())
}
pub fn reindex_modified_file_without_commit(
&mut self,
text: &String,
relative_path: String,
index_writer: &IndexWriter,
user_space: bool,
) -> tantivy::Result<Vec<Option<tower_lsp::lsp_types::Diagnostic>>> {
if let Some(_) = &self.index {
let mut documents = Vec::new();
let diagnostics = match self.parse(text, &mut documents) {
Ok(diagnostics) => diagnostics,
Err(diagnostics) => {
// Return early so existing documents are not deleted when
// there is a syntax error
return Ok(diagnostics);
}
};
let file_path_id = blake3::hash(&relative_path.as_bytes());
for document in documents {
let mut fuzzy_doc = Document::default();
fuzzy_doc.add_text(self.schema_fields.file_path_id, &file_path_id.to_string());
for path_part in relative_path.split("/") {
if path_part.len() > 0 {
fuzzy_doc.add_text(self.schema_fields.file_path, path_part);
}
}
for fuzzy_scope in document.fuzzy_ruby_scope {
fuzzy_doc.add_text(self.schema_fields.fuzzy_ruby_scope_field, fuzzy_scope);
}
for class_scope in document.class_scope {
fuzzy_doc.add_text(self.schema_fields.class_scope_field, class_scope);
}
fuzzy_doc.add_text(
self.schema_fields.category_field,
document.category.to_string(),
);
fuzzy_doc.add_text(self.schema_fields.name_field, document.name);
fuzzy_doc.add_text(self.schema_fields.node_type_field, document.node_type);
fuzzy_doc.add_u64(
self.schema_fields.line_field,
document.line.try_into().unwrap(),
);
fuzzy_doc.add_u64(
self.schema_fields.start_column_field,
document.start_column.try_into().unwrap(),
);
fuzzy_doc.add_u64(
self.schema_fields.end_column_field,
document.end_column.try_into().unwrap(),
);
fuzzy_doc.add_bool(self.schema_fields.user_space_field, user_space);
let start_col = document.start_column;
let end_col = document.end_column;
let col_range = start_col..(end_col + 1);
for col in col_range {
fuzzy_doc.add_u64(self.schema_fields.columns_field, col as u64);
}
index_writer.add_document(fuzzy_doc)?;
}
Ok(diagnostics)
} else {
Ok(vec![])
}
}
pub async fn reindex_modified_file(&mut self, client: &Client, text: &String, uri: &Url) {
let mut documents = Vec::new();
let diagnostics = match self.parse(text, &mut documents) {
Ok(diagnostics) => diagnostics,
Err(diagnostics) => {
// Return early so existing documents are not deleted when
// there is a syntax error
// return Ok(diagnostics);
diagnostics
}
};
if self.report_diagnostics {
let mut reported_diagnostics = vec![];
for diagnostic in &diagnostics {
for unwrapped_diagnostic in diagnostic {
reported_diagnostics.push(unwrapped_diagnostic.clone());
}
}
client
.publish_diagnostics(uri.clone(), reported_diagnostics, None)
.await;
// .await;
}
if diagnostics.len() > 0 {
return;
}
if let Some(index) = &self.index {
let mut index_writer = index.writer_with_num_threads(1, 30_000_000).unwrap();
let user_space: bool;
let relative_path: String;
if uri.path().contains(&self.workspace_path) {
user_space = true;
relative_path = uri.path().replace(&self.workspace_path, "");
} else {
user_space = false;
relative_path = uri.path().to_string();
}
let file_path_id = blake3::hash(&relative_path.as_bytes());
let file_path_id_term =
Term::from_field_text(self.schema_fields.file_path_id, &file_path_id.to_string());
index_writer.delete_term(file_path_id_term);
for document in documents {
let mut fuzzy_doc = Document::default();
fuzzy_doc.add_text(self.schema_fields.file_path_id, &file_path_id.to_string());
for path_part in relative_path.split("/") {
if path_part.len() > 0 {
fuzzy_doc.add_text(self.schema_fields.file_path, path_part);
}
}
for fuzzy_scope in document.fuzzy_ruby_scope {
fuzzy_doc.add_text(self.schema_fields.fuzzy_ruby_scope_field, fuzzy_scope);
}
for class_scope in document.class_scope {
fuzzy_doc.add_text(self.schema_fields.class_scope_field, class_scope);
}
fuzzy_doc.add_text(
self.schema_fields.category_field,
document.category.to_string(),
);
fuzzy_doc.add_text(self.schema_fields.name_field, document.name);
fuzzy_doc.add_text(self.schema_fields.node_type_field, document.node_type);
fuzzy_doc.add_u64(
self.schema_fields.line_field,
document.line.try_into().unwrap(),
);
fuzzy_doc.add_u64(
self.schema_fields.start_column_field,
document.start_column.try_into().unwrap(),
);
fuzzy_doc.add_u64(
self.schema_fields.end_column_field,
document.end_column.try_into().unwrap(),
);
fuzzy_doc.add_bool(self.schema_fields.user_space_field, user_space);
let start_col = document.start_column;
let end_col = document.end_column;
let col_range = start_col..(end_col + 1);
for col in col_range {
fuzzy_doc.add_u64(self.schema_fields.columns_field, col as u64);
}
index_writer.add_document(fuzzy_doc).unwrap();
}
index_writer.commit().unwrap();
}
}
pub fn diagnostics(
&mut self,
text: &String,
_uri: &Url,
) -> tantivy::Result<Vec<Option<tower_lsp::lsp_types::Diagnostic>>> {
let mut documents = Vec::new();
match self.parse(text, &mut documents) {
Ok(diagnostics) => Ok(diagnostics),
Err(diagnostics) => Ok(diagnostics),
}
}
pub fn find_definitions(
&self,
params: TextDocumentPositionParams,
) -> tantivy::Result<Vec<Location>> {
let path = params.text_document.uri.path();
let relative_path = path.replace(&self.workspace_path, "");
let position = params.position;
if let Some(index) = &self.index {
let reader = index
.reader_builder()
.reload_policy(ReloadPolicy::OnCommit)
.try_into()?;
let searcher = reader.searcher();
let character_position = position.character;
let character_line = position.line;
let file_path_id = blake3::hash(&relative_path.as_bytes());
let file_path_query: Box<dyn Query> = Box::new(TermQuery::new(
Term::from_field_text(self.schema_fields.file_path_id, &file_path_id.to_string()),
IndexRecordOption::Basic,
));
let category_query: Box<dyn Query> = Box::new(TermQuery::new(
Term::from_field_text(self.schema_fields.category_field, "usage"),
IndexRecordOption::Basic,
));
let line_query: Box<dyn Query> = Box::new(TermQuery::new(
Term::from_field_u64(self.schema_fields.line_field, character_line.into()),
IndexRecordOption::Basic,
));
let column_query: Box<dyn Query> = Box::new(TermQuery::new(
Term::from_field_u64(self.schema_fields.columns_field, character_position.into()),
IndexRecordOption::Basic,
));
let query = BooleanQuery::new(vec![
(Occur::Must, file_path_query),
(Occur::Must, category_query),
(Occur::Must, line_query),
(Occur::Must, column_query),
]);
let usage_top_docs = searcher.search(&query, &TopDocs::with_limit(1))?;
let mut locations = Vec::new();
if usage_top_docs.len() == 0 {
info!("No usages docs found");
return Ok(locations);
}
let doc_address = usage_top_docs[0].1;
let retrieved_doc = searcher.doc(doc_address)?;
let category_query: Box<dyn Query> = Box::new(TermQuery::new(
Term::from_field_text(self.schema_fields.category_field, "assignment"),
IndexRecordOption::Basic,
));
let usage_name = retrieved_doc
.get_first(self.schema_fields.name_field)
.unwrap()
.as_text()
.unwrap();
let usage_type = retrieved_doc
.get_first(self.schema_fields.node_type_field)
.unwrap()
.as_text()
.unwrap();
let name_query: Box<dyn Query> = Box::new(TermQuery::new(
Term::from_field_text(self.schema_fields.name_field, usage_name),
IndexRecordOption::Basic,
));
let mut assignment_type_queries = vec![];
for possible_assignment_type in USAGE_TYPE_RESTRICTIONS.get(usage_type).unwrap().iter()
{
let assignment_type_query: Box<dyn Query> = Box::new(TermQuery::new(
Term::from_field_text(
self.schema_fields.node_type_field,
possible_assignment_type,
),
IndexRecordOption::Basic,
));
assignment_type_queries.push((Occur::Should, assignment_type_query));
}