-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
completions.rs
750 lines (728 loc) · 22 KB
/
completions.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
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
use super::client::Client;
use super::language_server;
use super::lsp_custom;
use super::tsc;
use crate::fs_util::is_supported_ext;
use crate::fs_util::specifier_to_file_path;
use deno_ast::swc::common::BytePos;
use deno_ast::LineAndColumnIndex;
use deno_ast::SourceTextInfo;
use deno_core::normalize_path;
use deno_core::resolve_path;
use deno_core::resolve_url;
use deno_core::serde::Deserialize;
use deno_core::serde::Serialize;
use deno_core::url::Position;
use deno_core::ModuleSpecifier;
use lspower::lsp;
const CURRENT_PATH: &str = ".";
const PARENT_PATH: &str = "..";
const LOCAL_PATHS: &[&str] = &[CURRENT_PATH, PARENT_PATH];
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CompletionItemData {
#[serde(skip_serializing_if = "Option::is_none")]
pub documentation: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tsc: Option<tsc::CompletionItemData>,
}
/// Check if the origin can be auto-configured for completions, and if so, send
/// a notification to the client.
async fn check_auto_config_registry(
url_str: &str,
snapshot: &language_server::StateSnapshot,
client: Client,
) {
// check to see if auto discovery is enabled
if snapshot
.config
.settings
.workspace
.suggest
.imports
.auto_discover
{
if let Ok(specifier) = resolve_url(url_str) {
let scheme = specifier.scheme();
let path = &specifier[Position::BeforePath..];
if scheme.starts_with("http")
&& !path.is_empty()
&& url_str.ends_with(path)
{
// check to see if this origin is already explicitly set
let in_config = snapshot
.config
.settings
.workspace
.suggest
.imports
.hosts
.iter()
.any(|(h, _)| {
resolve_url(h).map(|u| u.origin()) == Ok(specifier.origin())
});
// if it isn't in the configuration, we will check to see if it supports
// suggestions and send a notification to the client.
if !in_config {
let origin = specifier.origin().ascii_serialization();
let suggestions = snapshot
.module_registries
.check_origin(&origin)
.await
.is_ok();
// we are only sending registry state when enabled now, but changing
// the custom notification would make older versions of the plugin
// incompatible.
// TODO(@kitsonk) clean up protocol when doing v2 of suggestions
if suggestions {
client
.send_registry_state_notification(
lsp_custom::RegistryStateNotificationParams {
origin,
suggestions,
},
)
.await;
}
}
}
}
}
}
/// Ranges from the graph for specifiers include the leading and maybe trailing quote,
/// which we want to ignore when replacing text.
fn to_narrow_lsp_range(
text_info: &SourceTextInfo,
range: &deno_graph::Range,
) -> lsp::Range {
let end_byte_index = text_info.byte_index(LineAndColumnIndex {
line_index: range.end.line,
column_index: range.end.character,
});
let text_bytes = text_info.text_str().as_bytes();
let has_trailing_quote =
matches!(text_bytes[end_byte_index.0 as usize - 1], (b'"' | b'\''));
lsp::Range {
start: lsp::Position {
line: range.start.line as u32,
// skip the leading quote
character: (range.start.character + 1) as u32,
},
end: lsp::Position {
line: range.end.line as u32,
character: if has_trailing_quote {
range.end.character - 1 // do not include it
} else {
range.end.character
} as u32,
},
}
}
/// Given a specifier, a position, and a snapshot, optionally return a
/// completion response, which will be valid import completions for the specific
/// context.
pub(crate) async fn get_import_completions(
specifier: &ModuleSpecifier,
position: &lsp::Position,
state_snapshot: &language_server::StateSnapshot,
client: Client,
) -> Option<lsp::CompletionResponse> {
let document = state_snapshot.documents.get(specifier)?;
let (text, _, range) = document.get_maybe_dependency(position)?;
let range = to_narrow_lsp_range(&document.text_info(), &range);
// completions for local relative modules
if text.starts_with("./") || text.starts_with("../") {
Some(lsp::CompletionResponse::List(lsp::CompletionList {
is_incomplete: false,
items: get_local_completions(specifier, &text, &range)?,
}))
} else if !text.is_empty() {
// completion of modules from a module registry or cache
check_auto_config_registry(&text, state_snapshot, client).await;
let offset = if position.character > range.start.character {
(position.character - range.start.character) as usize
} else {
0
};
let maybe_list = state_snapshot
.module_registries
.get_completions(&text, offset, &range, |specifier| {
state_snapshot.documents.contains_specifier(specifier)
})
.await;
let list = maybe_list.unwrap_or_else(|| lsp::CompletionList {
items: get_workspace_completions(
specifier,
&text,
&range,
state_snapshot,
),
is_incomplete: false,
});
Some(lsp::CompletionResponse::List(list))
} else {
let mut items: Vec<lsp::CompletionItem> = LOCAL_PATHS
.iter()
.map(|s| lsp::CompletionItem {
label: s.to_string(),
kind: Some(lsp::CompletionItemKind::FOLDER),
detail: Some("(local)".to_string()),
sort_text: Some("1".to_string()),
insert_text: Some(s.to_string()),
..Default::default()
})
.collect();
let mut is_incomplete = false;
if let Some(origin_items) = state_snapshot
.module_registries
.get_origin_completions(&text, &range)
{
is_incomplete = origin_items.is_incomplete;
items.extend(origin_items.items);
}
Some(lsp::CompletionResponse::List(lsp::CompletionList {
is_incomplete,
items,
}))
// TODO(@kitsonk) add bare specifiers from import map
}
}
/// Return local completions that are relative to the base specifier.
fn get_local_completions(
base: &ModuleSpecifier,
current: &str,
range: &lsp::Range,
) -> Option<Vec<lsp::CompletionItem>> {
if base.scheme() != "file" {
return None;
}
let mut base_path = specifier_to_file_path(base).ok()?;
base_path.pop();
let mut current_path = normalize_path(base_path.join(current));
// if the current text does not end in a `/` then we are still selecting on
// the parent and should show all completions from there.
let is_parent = if !current.ends_with('/') {
current_path.pop();
true
} else {
false
};
if current_path.is_dir() {
let items = std::fs::read_dir(current_path).ok()?;
Some(
items
.filter_map(|de| {
let de = de.ok()?;
let label = de.path().file_name()?.to_string_lossy().to_string();
let entry_specifier = resolve_path(de.path().to_str()?).ok()?;
if &entry_specifier == base {
return None;
}
let full_text = relative_specifier(&entry_specifier, base);
// this weeds out situations where we are browsing in the parent, but
// we want to filter out non-matches when the completion is manually
// invoked by the user, but still allows for things like `../src/../`
// which is silly, but no reason to not allow it.
if is_parent && !full_text.starts_with(current) {
return None;
}
let text_edit = Some(lsp::CompletionTextEdit::Edit(lsp::TextEdit {
range: *range,
new_text: full_text.clone(),
}));
let filter_text = if full_text.starts_with(current) {
Some(full_text)
} else {
Some(format!("{}{}", current, label))
};
match de.file_type() {
Ok(file_type) if file_type.is_dir() => Some(lsp::CompletionItem {
label,
kind: Some(lsp::CompletionItemKind::FOLDER),
filter_text,
sort_text: Some("1".to_string()),
text_edit,
..Default::default()
}),
Ok(file_type) if file_type.is_file() => {
if is_supported_ext(&de.path()) {
Some(lsp::CompletionItem {
label,
kind: Some(lsp::CompletionItemKind::FILE),
detail: Some("(local)".to_string()),
filter_text,
sort_text: Some("1".to_string()),
text_edit,
..Default::default()
})
} else {
None
}
}
_ => None,
}
})
.collect(),
)
} else {
None
}
}
fn get_relative_specifiers(
base: &ModuleSpecifier,
specifiers: Vec<ModuleSpecifier>,
) -> Vec<String> {
specifiers
.iter()
.filter_map(|s| {
if s != base {
Some(relative_specifier(s, base))
} else {
None
}
})
.collect()
}
/// Get workspace completions that include modules in the Deno cache which match
/// the current specifier string.
fn get_workspace_completions(
specifier: &ModuleSpecifier,
current: &str,
range: &lsp::Range,
state_snapshot: &language_server::StateSnapshot,
) -> Vec<lsp::CompletionItem> {
let workspace_specifiers = state_snapshot
.documents
.documents(false, true)
.into_iter()
.map(|d| d.specifier().clone())
.collect();
let specifier_strings =
get_relative_specifiers(specifier, workspace_specifiers);
specifier_strings
.into_iter()
.filter_map(|label| {
if label.starts_with(¤t) {
let detail = Some(
if label.starts_with("http:") || label.starts_with("https:") {
"(remote)".to_string()
} else if label.starts_with("data:") {
"(data)".to_string()
} else {
"(local)".to_string()
},
);
let text_edit = Some(lsp::CompletionTextEdit::Edit(lsp::TextEdit {
range: *range,
new_text: label.clone(),
}));
Some(lsp::CompletionItem {
label,
kind: Some(lsp::CompletionItemKind::FILE),
detail,
sort_text: Some("1".to_string()),
text_edit,
..Default::default()
})
} else {
None
}
})
.collect()
}
/// Converts a specifier into a relative specifier to the provided base
/// specifier as a string. If a relative path cannot be found, then the
/// specifier is simply returned as a string.
///
/// ```
/// use deno_core::resolve_url;
///
/// let specifier = resolve_url("file:///a/b.ts").unwrap();
/// let base = resolve_url("file:///a/c/d.ts").unwrap();
/// assert_eq!(relative_specifier(&specifier, &base), "../b.ts");
/// ```
///
fn relative_specifier(
specifier: &ModuleSpecifier,
base: &ModuleSpecifier,
) -> String {
if specifier.cannot_be_a_base()
|| base.cannot_be_a_base()
|| specifier.scheme() != base.scheme()
|| specifier.host() != base.host()
|| specifier.port_or_known_default() != base.port_or_known_default()
{
if specifier.scheme() == "file" {
specifier_to_file_path(specifier)
.unwrap()
.to_string_lossy()
.into()
} else {
specifier.as_str().into()
}
} else if let (Some(iter_a), Some(iter_b)) =
(specifier.path_segments(), base.path_segments())
{
let mut vec_a: Vec<&str> = iter_a.collect();
let mut vec_b: Vec<&str> = iter_b.collect();
let last_a = if !specifier.path().ends_with('/') && !vec_a.is_empty() {
vec_a.pop().unwrap()
} else {
""
};
let is_dir_b = base.path().ends_with('/');
if !is_dir_b && !vec_b.is_empty() {
vec_b.pop();
}
if !vec_a.is_empty() && !vec_b.is_empty() && base.path() != "/" {
let mut parts: Vec<&str> = Vec::new();
let mut segments_a = vec_a.into_iter();
let mut segments_b = vec_b.into_iter();
loop {
match (segments_a.next(), segments_b.next()) {
(None, None) => break,
(Some(a), None) => {
if parts.is_empty() {
parts.push(CURRENT_PATH);
}
parts.push(a);
parts.extend(segments_a.by_ref());
break;
}
(None, _) if is_dir_b => parts.push(CURRENT_PATH),
(None, _) => parts.push(PARENT_PATH),
(Some(a), Some(b)) if parts.is_empty() && a == b => (),
(Some(a), Some(b)) if b == CURRENT_PATH => parts.push(a),
(Some(_), Some(b)) if b == PARENT_PATH => {
return specifier[Position::BeforePath..].to_string()
}
(Some(a), Some(_)) => {
if parts.is_empty() && is_dir_b {
parts.push(CURRENT_PATH);
} else {
parts.push(PARENT_PATH);
}
// actually the clippy suggestions here are less readable for once
#[allow(clippy::same_item_push)]
for _ in segments_b {
parts.push(PARENT_PATH);
}
parts.push(a);
parts.extend(segments_a.by_ref());
break;
}
}
}
if parts.is_empty() {
format!(
"./{}{}",
last_a,
specifier[Position::AfterPath..].to_string()
)
} else {
parts.push(last_a);
format!(
"{}{}",
parts.join("/"),
specifier[Position::AfterPath..].to_string()
)
}
} else {
specifier[Position::BeforePath..].into()
}
} else {
specifier[Position::BeforePath..].into()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::http_cache::HttpCache;
use crate::lsp::documents::Documents;
use crate::lsp::documents::LanguageId;
use deno_core::resolve_url;
use deno_graph::Range;
use std::collections::HashMap;
use std::path::Path;
use std::sync::Arc;
use tempfile::TempDir;
fn mock_state_snapshot(
fixtures: &[(&str, &str, i32, LanguageId)],
source_fixtures: &[(&str, &str)],
location: &Path,
) -> language_server::StateSnapshot {
let mut documents = Documents::new(location);
for (specifier, source, version, language_id) in fixtures {
let specifier =
resolve_url(specifier).expect("failed to create specifier");
documents.open(
specifier.clone(),
*version,
language_id.clone(),
Arc::new(source.to_string()),
);
}
let http_cache = HttpCache::new(location);
for (specifier, source) in source_fixtures {
let specifier =
resolve_url(specifier).expect("failed to create specifier");
http_cache
.set(&specifier, HashMap::default(), source.as_bytes())
.expect("could not cache file");
assert!(
documents.get(&specifier).is_some(),
"source could not be setup"
);
}
language_server::StateSnapshot {
documents,
..Default::default()
}
}
fn setup(
documents: &[(&str, &str, i32, LanguageId)],
sources: &[(&str, &str)],
) -> language_server::StateSnapshot {
let temp_dir = TempDir::new().expect("could not create temp dir");
let location = temp_dir.path().join("deps");
mock_state_snapshot(documents, sources, &location)
}
#[test]
fn test_get_relative_specifiers() {
let base = resolve_url("file:///a/b/c.ts").unwrap();
let specifiers = vec![
resolve_url("file:///a/b/c.ts").unwrap(),
resolve_url("file:///a/b/d.ts").unwrap(),
resolve_url("file:///a/c/c.ts").unwrap(),
resolve_url("file:///a/b/d/d.ts").unwrap(),
resolve_url("https://deno.land/x/a/b/c.ts").unwrap(),
];
assert_eq!(
get_relative_specifiers(&base, specifiers),
vec![
"./d.ts".to_string(),
"../c/c.ts".to_string(),
"./d/d.ts".to_string(),
"https://deno.land/x/a/b/c.ts".to_string(),
]
);
}
#[test]
fn test_relative_specifier() {
let fixtures: Vec<(&str, &str, &str)> = vec![
(
"https://deno.land/x/a/b/c.ts",
"https://deno.land/x/a/b/d.ts",
"./c.ts",
),
(
"https://deno.land/x/a/c.ts",
"https://deno.land/x/a/b/d.ts",
"../c.ts",
),
(
"https://deno.land/x/a/b/c/d.ts",
"https://deno.land/x/a/b/d.ts",
"./c/d.ts",
),
(
"https://deno.land/x/a/b/c/d.ts",
"https://deno.land/x/a/b/c/",
"./d.ts",
),
(
"https://deno.land/x/a/b/c/d/e.ts",
"https://deno.land/x/a/b/c/",
"./d/e.ts",
),
(
"https://deno.land/x/a/b/c/d/e.ts",
"https://deno.land/x/a/b/c/f.ts",
"./d/e.ts",
),
(
"https://deno.land/x/a/c.ts?foo=bar",
"https://deno.land/x/a/b/d.ts",
"../c.ts?foo=bar",
),
(
"https://deno.land/x/a/b/c.ts",
"https://deno.land/x/a/b/d.ts?foo=bar",
"./c.ts",
),
#[cfg(not(windows))]
("file:///a/b/c.ts", "file:///a/b/d.ts", "./c.ts"),
#[cfg(not(windows))]
(
"file:///a/b/c.ts",
"https://deno.land/x/a/b/c.ts",
"/a/b/c.ts",
),
(
"https://deno.land/x/a/b/c.ts",
"https://deno.land/",
"/x/a/b/c.ts",
),
(
"https://deno.land/x/a/b/c.ts",
"https://deno.land/x/d/e/f.ts",
"../../a/b/c.ts",
),
];
for (specifier_str, base_str, expected) in fixtures {
let specifier = resolve_url(specifier_str).unwrap();
let base = resolve_url(base_str).unwrap();
let actual = relative_specifier(&specifier, &base);
assert_eq!(
actual, expected,
"specifier: \"{}\" base: \"{}\"",
specifier_str, base_str
);
}
}
#[test]
fn test_get_local_completions() {
let temp_dir = TempDir::new().expect("could not create temp dir");
let fixtures = temp_dir.path().join("fixtures");
std::fs::create_dir(&fixtures).expect("could not create");
let dir_a = fixtures.join("a");
std::fs::create_dir(&dir_a).expect("could not create");
let dir_b = dir_a.join("b");
std::fs::create_dir(&dir_b).expect("could not create");
let file_c = dir_a.join("c.ts");
std::fs::write(&file_c, b"").expect("could not create");
let file_d = dir_b.join("d.ts");
std::fs::write(&file_d, b"").expect("could not create");
let file_e = dir_a.join("e.txt");
std::fs::write(&file_e, b"").expect("could not create");
let file_f = dir_a.join("f.mjs");
std::fs::write(&file_f, b"").expect("could not create");
let specifier =
ModuleSpecifier::from_file_path(file_c).expect("could not create");
let actual = get_local_completions(
&specifier,
"./",
&lsp::Range {
start: lsp::Position {
line: 0,
character: 20,
},
end: lsp::Position {
line: 0,
character: 22,
},
},
);
assert!(actual.is_some());
let actual = actual.unwrap();
assert_eq!(actual.len(), 2);
for item in actual {
match item.text_edit {
Some(lsp::CompletionTextEdit::Edit(text_edit)) => {
assert!(
text_edit.new_text == "./f.mjs" || text_edit.new_text == "./b"
);
}
_ => unreachable!(),
}
}
}
#[tokio::test]
async fn test_get_workspace_completions() {
let specifier = resolve_url("file:///a/b/c.ts").unwrap();
let range = lsp::Range {
start: lsp::Position {
line: 0,
character: 20,
},
end: lsp::Position {
line: 0,
character: 21,
},
};
let state_snapshot = setup(
&[
(
"file:///a/b/c.ts",
"import * as d from \"h\"",
1,
LanguageId::TypeScript,
),
("file:///a/c.ts", r#""#, 1, LanguageId::TypeScript),
],
&[("https://deno.land/x/a/b/c.ts", "console.log(1);\n")],
);
let actual =
get_workspace_completions(&specifier, "h", &range, &state_snapshot);
assert_eq!(
actual,
vec![lsp::CompletionItem {
label: "https://deno.land/x/a/b/c.ts".to_string(),
kind: Some(lsp::CompletionItemKind::FILE),
detail: Some("(remote)".to_string()),
sort_text: Some("1".to_string()),
text_edit: Some(lsp::CompletionTextEdit::Edit(lsp::TextEdit {
range: lsp::Range {
start: lsp::Position {
line: 0,
character: 20
},
end: lsp::Position {
line: 0,
character: 21,
}
},
new_text: "https://deno.land/x/a/b/c.ts".to_string(),
})),
..Default::default()
}]
);
}
#[test]
fn test_to_narrow_lsp_range() {
let text_info = SourceTextInfo::from_string(r#""te""#.to_string());
let range = to_narrow_lsp_range(
&text_info,
&Range {
specifier: ModuleSpecifier::parse("https://deno.land").unwrap(),
start: deno_graph::Position {
line: 0,
character: 0,
},
end: deno_graph::Position {
line: 0,
character: text_info.text_str().chars().count(),
},
},
);
assert_eq!(range.start.character, 1);
assert_eq!(
range.end.character,
(text_info.text_str().chars().count() - 1) as u32
);
}
#[test]
fn test_to_narrow_lsp_range_no_trailing_quote() {
let text_info = SourceTextInfo::from_string(r#""te"#.to_string());
let range = to_narrow_lsp_range(
&text_info,
&Range {
specifier: ModuleSpecifier::parse("https://deno.land").unwrap(),
start: deno_graph::Position {
line: 0,
character: 0,
},
end: deno_graph::Position {
line: 0,
character: text_info.text_str().chars().count(),
},
},
);
assert_eq!(range.start.character, 1);
assert_eq!(
range.end.character,
text_info.text_str().chars().count() as u32
);
}
}