2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-03-24 20:13:37 -04:00
|
|
|
|
2021-12-15 13:23:43 -05:00
|
|
|
use super::client::Client;
|
2022-01-19 17:10:14 -05:00
|
|
|
use super::config::ConfigSnapshot;
|
|
|
|
use super::documents::Documents;
|
2021-06-01 07:53:08 -04:00
|
|
|
use super::lsp_custom;
|
2022-01-19 17:10:14 -05:00
|
|
|
use super::registries::ModuleRegistry;
|
2021-03-24 20:13:37 -04:00
|
|
|
use super::tsc;
|
|
|
|
|
|
|
|
use crate::fs_util::is_supported_ext;
|
2021-11-23 10:38:11 -05:00
|
|
|
use crate::fs_util::specifier_to_file_path;
|
2021-03-24 20:13:37 -04:00
|
|
|
|
2021-12-13 16:28:35 -05:00
|
|
|
use deno_ast::LineAndColumnIndex;
|
|
|
|
use deno_ast::SourceTextInfo;
|
2021-03-24 20:13:37 -04:00
|
|
|
use deno_core::normalize_path;
|
|
|
|
use deno_core::resolve_path;
|
2021-06-01 07:53:08 -04:00
|
|
|
use deno_core::resolve_url;
|
2021-03-24 20:13:37 -04:00
|
|
|
use deno_core::serde::Deserialize;
|
|
|
|
use deno_core::serde::Serialize;
|
|
|
|
use deno_core::url::Position;
|
|
|
|
use deno_core::ModuleSpecifier;
|
2022-02-09 15:13:50 -05:00
|
|
|
use import_map::ImportMap;
|
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
use regex::Regex;
|
|
|
|
use std::sync::Arc;
|
2022-04-03 00:17:30 -04:00
|
|
|
use tower_lsp::lsp_types as lsp;
|
2022-02-09 15:13:50 -05:00
|
|
|
|
|
|
|
static FILE_PROTO_RE: Lazy<Regex> =
|
|
|
|
Lazy::new(|| Regex::new(r#"^file:/{2}(?:/[A-Za-z]:)?"#).unwrap());
|
2021-03-24 20:13:37 -04:00
|
|
|
|
|
|
|
const CURRENT_PATH: &str = ".";
|
|
|
|
const PARENT_PATH: &str = "..";
|
2021-04-08 21:27:27 -04:00
|
|
|
const LOCAL_PATHS: &[&str] = &[CURRENT_PATH, PARENT_PATH];
|
2022-07-11 19:35:18 -04:00
|
|
|
const IMPORT_COMMIT_CHARS: &[&str] = &["\"", "'", "/"];
|
2021-03-24 20:13:37 -04:00
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct CompletionItemData {
|
2021-12-13 14:24:11 -05:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub documentation: Option<String>,
|
2021-03-24 20:13:37 -04:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub tsc: Option<tsc::CompletionItemData>,
|
|
|
|
}
|
|
|
|
|
2021-06-01 07:53:08 -04:00
|
|
|
/// 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,
|
2022-01-19 17:10:14 -05:00
|
|
|
config: &ConfigSnapshot,
|
2021-12-15 13:23:43 -05:00
|
|
|
client: Client,
|
2022-01-19 17:10:14 -05:00
|
|
|
module_registries: &ModuleRegistry,
|
2021-06-01 07:53:08 -04:00
|
|
|
) {
|
|
|
|
// check to see if auto discovery is enabled
|
2022-01-19 17:10:14 -05:00
|
|
|
if config.settings.workspace.suggest.imports.auto_discover {
|
2021-06-01 07:53:08 -04:00
|
|
|
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
|
2022-01-19 17:10:14 -05:00
|
|
|
let in_config =
|
|
|
|
config.settings.workspace.suggest.imports.hosts.iter().any(
|
|
|
|
|(h, _)| {
|
|
|
|
resolve_url(h).map(|u| u.origin()) == Ok(specifier.origin())
|
|
|
|
},
|
|
|
|
);
|
2021-06-01 07:53:08 -04:00
|
|
|
// 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();
|
2022-01-19 17:10:14 -05:00
|
|
|
let suggestions =
|
|
|
|
module_registries.check_origin(&origin).await.is_ok();
|
2021-11-07 17:35:32 -05:00
|
|
|
// 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
|
2021-12-15 13:23:43 -05:00
|
|
|
.send_registry_state_notification(
|
2021-11-07 17:35:32 -05:00
|
|
|
lsp_custom::RegistryStateNotificationParams {
|
|
|
|
origin,
|
|
|
|
suggestions,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
}
|
2021-06-01 07:53:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-13 16:28:35 -05:00
|
|
|
/// Ranges from the graph for specifiers include the leading and maybe trailing quote,
|
2021-10-28 19:56:01 -04:00
|
|
|
/// which we want to ignore when replacing text.
|
2021-12-13 16:28:35 -05:00
|
|
|
fn to_narrow_lsp_range(
|
|
|
|
text_info: &SourceTextInfo,
|
|
|
|
range: &deno_graph::Range,
|
|
|
|
) -> lsp::Range {
|
2022-05-20 16:40:55 -04:00
|
|
|
let end_byte_index = text_info
|
|
|
|
.loc_to_source_pos(LineAndColumnIndex {
|
|
|
|
line_index: range.end.line,
|
|
|
|
column_index: range.end.character,
|
|
|
|
})
|
|
|
|
.as_byte_index(text_info.range().start);
|
2022-07-12 21:56:48 -04:00
|
|
|
let start_byte_index = text_info
|
|
|
|
.loc_to_source_pos(LineAndColumnIndex {
|
|
|
|
line_index: range.start.line,
|
|
|
|
column_index: range.start.character,
|
|
|
|
})
|
|
|
|
.as_byte_index(text_info.range().start);
|
2021-12-13 16:28:35 -05:00
|
|
|
let text_bytes = text_info.text_str().as_bytes();
|
2022-07-12 21:56:48 -04:00
|
|
|
let is_empty = end_byte_index - 1 == start_byte_index;
|
2021-12-13 16:28:35 -05:00
|
|
|
let has_trailing_quote =
|
2022-07-12 21:56:48 -04:00
|
|
|
!is_empty && matches!(text_bytes[end_byte_index - 1], b'"' | b'\'');
|
2021-10-28 19:56:01 -04:00
|
|
|
lsp::Range {
|
|
|
|
start: lsp::Position {
|
|
|
|
line: range.start.line as u32,
|
2021-12-13 16:28:35 -05:00
|
|
|
// skip the leading quote
|
2021-10-28 19:56:01 -04:00
|
|
|
character: (range.start.character + 1) as u32,
|
|
|
|
},
|
|
|
|
end: lsp::Position {
|
|
|
|
line: range.end.line as u32,
|
2021-12-13 16:28:35 -05:00
|
|
|
character: if has_trailing_quote {
|
|
|
|
range.end.character - 1 // do not include it
|
|
|
|
} else {
|
|
|
|
range.end.character
|
|
|
|
} as u32,
|
2021-10-28 19:56:01 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-24 20:13:37 -04:00
|
|
|
/// Given a specifier, a position, and a snapshot, optionally return a
|
|
|
|
/// completion response, which will be valid import completions for the specific
|
|
|
|
/// context.
|
2022-03-23 09:54:22 -04:00
|
|
|
pub async fn get_import_completions(
|
2021-03-24 20:13:37 -04:00
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
position: &lsp::Position,
|
2022-01-19 17:10:14 -05:00
|
|
|
config: &ConfigSnapshot,
|
2021-12-15 13:23:43 -05:00
|
|
|
client: Client,
|
2022-01-19 17:10:14 -05:00
|
|
|
module_registries: &ModuleRegistry,
|
|
|
|
documents: &Documents,
|
2022-02-09 15:13:50 -05:00
|
|
|
maybe_import_map: Option<Arc<ImportMap>>,
|
2021-03-24 20:13:37 -04:00
|
|
|
) -> Option<lsp::CompletionResponse> {
|
2022-01-19 17:10:14 -05:00
|
|
|
let document = documents.get(specifier)?;
|
2021-11-12 11:42:04 -05:00
|
|
|
let (text, _, range) = document.get_maybe_dependency(position)?;
|
2021-12-13 16:28:35 -05:00
|
|
|
let range = to_narrow_lsp_range(&document.text_info(), &range);
|
2022-02-09 15:13:50 -05:00
|
|
|
if let Some(completion_list) = get_import_map_completions(
|
|
|
|
specifier,
|
|
|
|
&text,
|
|
|
|
&range,
|
|
|
|
maybe_import_map.clone(),
|
|
|
|
documents,
|
|
|
|
) {
|
|
|
|
// completions for import map specifiers
|
|
|
|
Some(lsp::CompletionResponse::List(completion_list))
|
|
|
|
} else if text.starts_with("./") || text.starts_with("../") {
|
|
|
|
// completions for local relative modules
|
2021-06-24 19:06:51 -04:00
|
|
|
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
|
2022-01-19 17:10:14 -05:00
|
|
|
check_auto_config_registry(&text, config, client, module_registries).await;
|
2021-06-24 19:06:51 -04:00
|
|
|
let offset = if position.character > range.start.character {
|
|
|
|
(position.character - range.start.character) as usize
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
};
|
2022-01-19 17:10:14 -05:00
|
|
|
let maybe_list = module_registries
|
2021-11-12 11:42:04 -05:00
|
|
|
.get_completions(&text, offset, &range, |specifier| {
|
2022-01-25 09:21:59 -05:00
|
|
|
documents.exists(specifier)
|
2021-11-12 11:42:04 -05:00
|
|
|
})
|
2021-06-24 19:06:51 -04:00
|
|
|
.await;
|
2021-12-13 14:24:11 -05:00
|
|
|
let list = maybe_list.unwrap_or_else(|| lsp::CompletionList {
|
2022-01-19 17:10:14 -05:00
|
|
|
items: get_workspace_completions(specifier, &text, &range, documents),
|
2021-06-24 19:06:51 -04:00
|
|
|
is_incomplete: false,
|
2021-12-13 14:24:11 -05:00
|
|
|
});
|
|
|
|
Some(lsp::CompletionResponse::List(list))
|
2021-06-24 19:06:51 -04:00
|
|
|
} else {
|
2022-02-09 15:13:50 -05:00
|
|
|
// the import specifier is empty, so provide all possible specifiers we are
|
|
|
|
// aware of
|
2021-06-24 19:06:51 -04:00
|
|
|
let mut items: Vec<lsp::CompletionItem> = LOCAL_PATHS
|
|
|
|
.iter()
|
|
|
|
.map(|s| lsp::CompletionItem {
|
|
|
|
label: s.to_string(),
|
2021-11-24 20:10:12 -05:00
|
|
|
kind: Some(lsp::CompletionItemKind::FOLDER),
|
2021-06-24 19:06:51 -04:00
|
|
|
detail: Some("(local)".to_string()),
|
|
|
|
sort_text: Some("1".to_string()),
|
|
|
|
insert_text: Some(s.to_string()),
|
2022-07-11 19:35:18 -04:00
|
|
|
commit_characters: Some(
|
|
|
|
IMPORT_COMMIT_CHARS.iter().map(|&c| c.into()).collect(),
|
|
|
|
),
|
2021-06-24 19:06:51 -04:00
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.collect();
|
2021-12-13 14:24:11 -05:00
|
|
|
let mut is_incomplete = false;
|
2022-02-09 15:13:50 -05:00
|
|
|
if let Some(import_map) = maybe_import_map {
|
|
|
|
items.extend(get_base_import_map_completions(import_map.as_ref()));
|
|
|
|
}
|
2022-01-19 17:10:14 -05:00
|
|
|
if let Some(origin_items) =
|
|
|
|
module_registries.get_origin_completions(&text, &range)
|
2021-03-24 20:13:37 -04:00
|
|
|
{
|
2021-12-13 14:24:11 -05:00
|
|
|
is_incomplete = origin_items.is_incomplete;
|
|
|
|
items.extend(origin_items.items);
|
2021-03-24 20:13:37 -04:00
|
|
|
}
|
2021-06-24 19:06:51 -04:00
|
|
|
Some(lsp::CompletionResponse::List(lsp::CompletionList {
|
2021-12-13 14:24:11 -05:00
|
|
|
is_incomplete,
|
2021-06-24 19:06:51 -04:00
|
|
|
items,
|
|
|
|
}))
|
2021-03-24 20:13:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-09 15:13:50 -05:00
|
|
|
/// When the specifier is an empty string, return all the keys from the import
|
|
|
|
/// map as completion items.
|
|
|
|
fn get_base_import_map_completions(
|
|
|
|
import_map: &ImportMap,
|
|
|
|
) -> Vec<lsp::CompletionItem> {
|
|
|
|
import_map
|
2022-06-14 10:05:37 -04:00
|
|
|
.imports()
|
|
|
|
.keys()
|
2022-02-09 15:13:50 -05:00
|
|
|
.map(|key| {
|
|
|
|
// for some strange reason, keys that start with `/` get stored in the
|
|
|
|
// import map as `file:///`, and so when we pull the keys out, we need to
|
|
|
|
// change the behavior
|
|
|
|
let mut label = if key.starts_with("file://") {
|
|
|
|
FILE_PROTO_RE.replace(key, "").to_string()
|
|
|
|
} else {
|
|
|
|
key.to_string()
|
|
|
|
};
|
|
|
|
let kind = if key.ends_with('/') {
|
|
|
|
label.pop();
|
|
|
|
Some(lsp::CompletionItemKind::FOLDER)
|
|
|
|
} else {
|
|
|
|
Some(lsp::CompletionItemKind::FILE)
|
|
|
|
};
|
|
|
|
lsp::CompletionItem {
|
|
|
|
label: label.clone(),
|
|
|
|
kind,
|
|
|
|
detail: Some("(import map)".to_string()),
|
|
|
|
sort_text: Some(label.clone()),
|
|
|
|
insert_text: Some(label),
|
2022-07-11 19:35:18 -04:00
|
|
|
commit_characters: Some(
|
|
|
|
IMPORT_COMMIT_CHARS.iter().map(|&c| c.into()).collect(),
|
|
|
|
),
|
2022-02-09 15:13:50 -05:00
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Given an existing specifier, return any completions that could apply derived
|
|
|
|
/// from the import map. There are two main type of import map keys, those that
|
|
|
|
/// a literal, which don't end in `/`, which expects a one for one replacement
|
|
|
|
/// of specifier to specifier, and then those that end in `/` which indicates
|
|
|
|
/// that the path post the `/` should be appended to resolved specifier. This
|
|
|
|
/// handles both cases, pulling any completions from the workspace completions.
|
|
|
|
fn get_import_map_completions(
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
text: &str,
|
|
|
|
range: &lsp::Range,
|
|
|
|
maybe_import_map: Option<Arc<ImportMap>>,
|
|
|
|
documents: &Documents,
|
|
|
|
) -> Option<lsp::CompletionList> {
|
|
|
|
if !text.is_empty() {
|
|
|
|
if let Some(import_map) = maybe_import_map {
|
|
|
|
let mut items = Vec::new();
|
2022-06-14 10:05:37 -04:00
|
|
|
for key in import_map.imports().keys() {
|
2022-02-09 15:13:50 -05:00
|
|
|
// for some reason, the import_map stores keys that begin with `/` as
|
|
|
|
// `file:///` in its index, so we have to reverse that here
|
|
|
|
let key = if key.starts_with("file://") {
|
|
|
|
FILE_PROTO_RE.replace(key, "").to_string()
|
|
|
|
} else {
|
|
|
|
key.to_string()
|
|
|
|
};
|
|
|
|
if text.starts_with(&key) && key.ends_with('/') {
|
|
|
|
if let Ok(resolved) = import_map.resolve(&key, specifier) {
|
|
|
|
let resolved = resolved.to_string();
|
|
|
|
let workspace_items: Vec<lsp::CompletionItem> = documents
|
|
|
|
.documents(false, true)
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|d| {
|
|
|
|
let specifier_str = d.specifier().to_string();
|
|
|
|
let new_text = specifier_str.replace(&resolved, &key);
|
|
|
|
if specifier_str.starts_with(&resolved) {
|
|
|
|
let label = specifier_str.replace(&resolved, "");
|
|
|
|
let text_edit =
|
|
|
|
Some(lsp::CompletionTextEdit::Edit(lsp::TextEdit {
|
|
|
|
range: *range,
|
|
|
|
new_text: new_text.clone(),
|
|
|
|
}));
|
|
|
|
Some(lsp::CompletionItem {
|
|
|
|
label,
|
|
|
|
kind: Some(lsp::CompletionItemKind::MODULE),
|
|
|
|
detail: Some("(import map)".to_string()),
|
|
|
|
sort_text: Some("1".to_string()),
|
|
|
|
filter_text: Some(new_text),
|
|
|
|
text_edit,
|
2022-07-11 19:35:18 -04:00
|
|
|
commit_characters: Some(
|
|
|
|
IMPORT_COMMIT_CHARS.iter().map(|&c| c.into()).collect(),
|
|
|
|
),
|
2022-02-09 15:13:50 -05:00
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
items.extend(workspace_items);
|
|
|
|
}
|
|
|
|
} else if key.starts_with(text) && text != key {
|
|
|
|
let mut label = key.to_string();
|
|
|
|
let kind = if key.ends_with('/') {
|
|
|
|
label.pop();
|
|
|
|
Some(lsp::CompletionItemKind::FOLDER)
|
|
|
|
} else {
|
|
|
|
Some(lsp::CompletionItemKind::MODULE)
|
|
|
|
};
|
|
|
|
let text_edit = Some(lsp::CompletionTextEdit::Edit(lsp::TextEdit {
|
|
|
|
range: *range,
|
|
|
|
new_text: label.clone(),
|
|
|
|
}));
|
|
|
|
items.push(lsp::CompletionItem {
|
|
|
|
label: label.clone(),
|
|
|
|
kind,
|
|
|
|
detail: Some("(import map)".to_string()),
|
|
|
|
sort_text: Some("1".to_string()),
|
|
|
|
text_edit,
|
2022-07-11 19:35:18 -04:00
|
|
|
commit_characters: Some(
|
|
|
|
IMPORT_COMMIT_CHARS.iter().map(|&c| c.into()).collect(),
|
|
|
|
),
|
2022-02-09 15:13:50 -05:00
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if !items.is_empty() {
|
|
|
|
return Some(lsp::CompletionList {
|
|
|
|
items,
|
|
|
|
is_incomplete: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2021-03-24 20:13:37 -04:00
|
|
|
/// 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;
|
|
|
|
}
|
|
|
|
|
2021-11-23 10:38:11 -05:00
|
|
|
let mut base_path = specifier_to_file_path(base).ok()?;
|
2021-03-24 20:13:37 -04:00
|
|
|
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,
|
2021-11-24 20:10:12 -05:00
|
|
|
kind: Some(lsp::CompletionItemKind::FOLDER),
|
2021-03-24 20:13:37 -04:00
|
|
|
filter_text,
|
|
|
|
sort_text: Some("1".to_string()),
|
|
|
|
text_edit,
|
2022-07-11 19:35:18 -04:00
|
|
|
commit_characters: Some(
|
|
|
|
IMPORT_COMMIT_CHARS.iter().map(|&c| c.into()).collect(),
|
|
|
|
),
|
2021-03-24 20:13:37 -04:00
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
Ok(file_type) if file_type.is_file() => {
|
|
|
|
if is_supported_ext(&de.path()) {
|
|
|
|
Some(lsp::CompletionItem {
|
|
|
|
label,
|
2021-11-24 20:10:12 -05:00
|
|
|
kind: Some(lsp::CompletionItemKind::FILE),
|
2021-03-24 20:13:37 -04:00
|
|
|
detail: Some("(local)".to_string()),
|
|
|
|
filter_text,
|
|
|
|
sort_text: Some("1".to_string()),
|
|
|
|
text_edit,
|
2022-07-11 19:35:18 -04:00
|
|
|
commit_characters: Some(
|
|
|
|
IMPORT_COMMIT_CHARS.iter().map(|&c| c.into()).collect(),
|
|
|
|
),
|
2021-03-24 20:13:37 -04:00
|
|
|
..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,
|
2022-01-19 17:10:14 -05:00
|
|
|
documents: &Documents,
|
2021-03-24 20:13:37 -04:00
|
|
|
) -> Vec<lsp::CompletionItem> {
|
2022-01-19 17:10:14 -05:00
|
|
|
let workspace_specifiers = documents
|
2021-11-12 11:42:04 -05:00
|
|
|
.documents(false, true)
|
|
|
|
.into_iter()
|
|
|
|
.map(|d| d.specifier().clone())
|
|
|
|
.collect();
|
2021-03-24 20:13:37 -04:00
|
|
|
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,
|
2021-11-24 20:10:12 -05:00
|
|
|
kind: Some(lsp::CompletionItemKind::FILE),
|
2021-03-24 20:13:37 -04:00
|
|
|
detail,
|
|
|
|
sort_text: Some("1".to_string()),
|
|
|
|
text_edit,
|
2022-07-11 19:35:18 -04:00
|
|
|
commit_characters: Some(
|
|
|
|
IMPORT_COMMIT_CHARS.iter().map(|&c| c.into()).collect(),
|
|
|
|
),
|
2021-03-24 20:13:37 -04:00
|
|
|
..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");
|
|
|
|
/// ```
|
|
|
|
///
|
2022-07-11 19:35:18 -04:00
|
|
|
pub fn relative_specifier(
|
2021-03-24 20:13:37 -04:00
|
|
|
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" {
|
2021-11-23 10:38:11 -05:00
|
|
|
specifier_to_file_path(specifier)
|
|
|
|
.unwrap()
|
|
|
|
.to_string_lossy()
|
|
|
|
.into()
|
2021-03-24 20:13:37 -04:00
|
|
|
} 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() {
|
2022-01-15 01:10:12 -05:00
|
|
|
format!("./{}{}", last_a, &specifier[Position::AfterPath..])
|
2021-03-24 20:13:37 -04:00
|
|
|
} else {
|
|
|
|
parts.push(last_a);
|
2022-01-15 01:10:12 -05:00
|
|
|
format!("{}{}", parts.join("/"), &specifier[Position::AfterPath..])
|
2021-03-24 20:13:37 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
specifier[Position::BeforePath..].into()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
specifier[Position::BeforePath..].into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use crate::http_cache::HttpCache;
|
2021-10-28 19:56:01 -04:00
|
|
|
use crate::lsp::documents::Documents;
|
2021-06-02 06:29:58 -04:00
|
|
|
use crate::lsp::documents::LanguageId;
|
2021-03-24 20:13:37 -04:00
|
|
|
use deno_core::resolve_url;
|
2021-12-13 16:28:35 -05:00
|
|
|
use deno_graph::Range;
|
2021-03-24 20:13:37 -04:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::path::Path;
|
2022-04-01 11:15:37 -04:00
|
|
|
use test_util::TempDir;
|
2021-03-24 20:13:37 -04:00
|
|
|
|
2022-01-19 17:10:14 -05:00
|
|
|
fn mock_documents(
|
2021-06-02 06:29:58 -04:00
|
|
|
fixtures: &[(&str, &str, i32, LanguageId)],
|
2021-03-24 20:13:37 -04:00
|
|
|
source_fixtures: &[(&str, &str)],
|
|
|
|
location: &Path,
|
2022-01-19 17:10:14 -05:00
|
|
|
) -> Documents {
|
2021-11-18 13:50:24 -05:00
|
|
|
let mut documents = Documents::new(location);
|
2021-06-02 06:29:58 -04:00
|
|
|
for (specifier, source, version, language_id) in fixtures {
|
2021-03-24 20:13:37 -04:00
|
|
|
let specifier =
|
|
|
|
resolve_url(specifier).expect("failed to create specifier");
|
2021-09-07 10:39:32 -04:00
|
|
|
documents.open(
|
|
|
|
specifier.clone(),
|
|
|
|
*version,
|
2021-10-28 19:56:01 -04:00
|
|
|
language_id.clone(),
|
2022-05-20 16:40:55 -04:00
|
|
|
(*source).into(),
|
2021-09-07 10:39:32 -04:00
|
|
|
);
|
2021-03-24 20:13:37 -04:00
|
|
|
}
|
|
|
|
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!(
|
2021-11-12 11:42:04 -05:00
|
|
|
documents.get(&specifier).is_some(),
|
2021-03-24 20:13:37 -04:00
|
|
|
"source could not be setup"
|
|
|
|
);
|
|
|
|
}
|
2022-01-19 17:10:14 -05:00
|
|
|
documents
|
2021-03-24 20:13:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn setup(
|
2022-04-01 11:15:37 -04:00
|
|
|
temp_dir: &TempDir,
|
2021-06-02 06:29:58 -04:00
|
|
|
documents: &[(&str, &str, i32, LanguageId)],
|
2021-03-24 20:13:37 -04:00
|
|
|
sources: &[(&str, &str)],
|
2022-01-19 17:10:14 -05:00
|
|
|
) -> Documents {
|
2021-03-24 20:13:37 -04:00
|
|
|
let location = temp_dir.path().join("deps");
|
2022-01-19 17:10:14 -05:00
|
|
|
mock_documents(documents, sources, &location)
|
2021-03-24 20:13:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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() {
|
2022-04-01 11:15:37 -04:00
|
|
|
let temp_dir = TempDir::new();
|
2021-03-24 20:13:37 -04:00
|
|
|
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!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-08 21:27:27 -04:00
|
|
|
#[tokio::test]
|
2021-06-01 07:53:08 -04:00
|
|
|
async fn test_get_workspace_completions() {
|
2021-03-24 20:13:37 -04:00
|
|
|
let specifier = resolve_url("file:///a/b/c.ts").unwrap();
|
2021-06-01 07:53:08 -04:00
|
|
|
let range = lsp::Range {
|
|
|
|
start: lsp::Position {
|
|
|
|
line: 0,
|
|
|
|
character: 20,
|
|
|
|
},
|
|
|
|
end: lsp::Position {
|
|
|
|
line: 0,
|
|
|
|
character: 21,
|
|
|
|
},
|
2021-03-24 20:13:37 -04:00
|
|
|
};
|
2022-04-01 11:15:37 -04:00
|
|
|
let temp_dir = TempDir::new();
|
2022-01-19 17:10:14 -05:00
|
|
|
let documents = setup(
|
2022-04-01 11:15:37 -04:00
|
|
|
&temp_dir,
|
2021-03-24 20:13:37 -04:00
|
|
|
&[
|
2021-06-02 06:29:58 -04:00
|
|
|
(
|
|
|
|
"file:///a/b/c.ts",
|
|
|
|
"import * as d from \"h\"",
|
|
|
|
1,
|
|
|
|
LanguageId::TypeScript,
|
|
|
|
),
|
|
|
|
("file:///a/c.ts", r#""#, 1, LanguageId::TypeScript),
|
2021-03-24 20:13:37 -04:00
|
|
|
],
|
|
|
|
&[("https://deno.land/x/a/b/c.ts", "console.log(1);\n")],
|
|
|
|
);
|
2022-01-19 17:10:14 -05:00
|
|
|
let actual = get_workspace_completions(&specifier, "h", &range, &documents);
|
2021-03-24 20:13:37 -04:00
|
|
|
assert_eq!(
|
|
|
|
actual,
|
2021-06-01 07:53:08 -04:00
|
|
|
vec![lsp::CompletionItem {
|
|
|
|
label: "https://deno.land/x/a/b/c.ts".to_string(),
|
2021-11-24 20:10:12 -05:00
|
|
|
kind: Some(lsp::CompletionItemKind::FILE),
|
2021-06-01 07:53:08 -04:00
|
|
|
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
|
2021-03-24 20:13:37 -04:00
|
|
|
},
|
2021-06-01 07:53:08 -04:00
|
|
|
end: lsp::Position {
|
|
|
|
line: 0,
|
|
|
|
character: 21,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
new_text: "https://deno.land/x/a/b/c.ts".to_string(),
|
|
|
|
})),
|
2022-07-11 19:35:18 -04:00
|
|
|
commit_characters: Some(
|
|
|
|
IMPORT_COMMIT_CHARS.iter().map(|&c| c.into()).collect()
|
|
|
|
),
|
2021-06-01 07:53:08 -04:00
|
|
|
..Default::default()
|
|
|
|
}]
|
2021-03-24 20:13:37 -04:00
|
|
|
);
|
|
|
|
}
|
2021-12-13 16:28:35 -05:00
|
|
|
|
|
|
|
#[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
|
|
|
|
);
|
|
|
|
}
|
2021-03-24 20:13:37 -04:00
|
|
|
}
|