2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-12-07 05:46:39 -05:00
|
|
|
|
2020-12-24 05:53:03 -05:00
|
|
|
use super::analysis::ResolvedDependency;
|
2021-02-09 17:46:12 -05:00
|
|
|
use super::analysis::ResolvedDependencyErr;
|
2021-06-04 17:31:44 -04:00
|
|
|
use super::code_lens;
|
2021-03-15 18:01:41 -04:00
|
|
|
use super::config;
|
2021-02-06 07:39:01 -05:00
|
|
|
use super::language_server;
|
2020-12-21 08:44:26 -05:00
|
|
|
use super::language_server::StateSnapshot;
|
2021-08-05 21:46:32 -04:00
|
|
|
use super::refactor::RefactorCodeActionData;
|
|
|
|
use super::refactor::ALL_KNOWN_REFACTOR_ACTION_KINDS;
|
|
|
|
use super::refactor::EXTRACT_CONSTANT;
|
|
|
|
use super::refactor::EXTRACT_INTERFACE;
|
|
|
|
use super::refactor::EXTRACT_TYPE;
|
2021-04-19 21:26:36 -04:00
|
|
|
use super::semantic_tokens::SemanticTokensBuilder;
|
|
|
|
use super::semantic_tokens::TsTokenEncodingConsts;
|
2020-12-07 05:46:39 -05:00
|
|
|
use super::text;
|
2021-01-22 05:03:16 -05:00
|
|
|
use super::text::LineIndex;
|
2021-07-25 01:33:42 -04:00
|
|
|
use super::urls::INVALID_SPECIFIER;
|
2020-12-07 05:46:39 -05:00
|
|
|
|
2021-05-10 12:16:39 -04:00
|
|
|
use crate::config_file::TsConfig;
|
2020-12-21 08:44:26 -05:00
|
|
|
use crate::tokio_util::create_basic_runtime;
|
2020-12-15 14:34:39 -05:00
|
|
|
use crate::tsc;
|
2020-12-07 05:46:39 -05:00
|
|
|
use crate::tsc::ResolveArgs;
|
|
|
|
|
2021-09-07 10:39:32 -04:00
|
|
|
use deno_ast::MediaType;
|
2020-12-21 08:44:26 -05:00
|
|
|
use deno_core::error::anyhow;
|
2020-12-07 05:46:39 -05:00
|
|
|
use deno_core::error::custom_error;
|
|
|
|
use deno_core::error::AnyError;
|
2021-06-21 19:45:41 -04:00
|
|
|
use deno_core::located_script_name;
|
2021-04-12 15:55:05 -04:00
|
|
|
use deno_core::op_sync;
|
2021-02-17 13:47:18 -05:00
|
|
|
use deno_core::resolve_url;
|
2021-02-24 22:15:55 -05:00
|
|
|
use deno_core::serde::de;
|
2020-12-07 05:46:39 -05:00
|
|
|
use deno_core::serde::Deserialize;
|
2020-12-08 05:36:13 -05:00
|
|
|
use deno_core::serde::Serialize;
|
2020-12-07 05:46:39 -05:00
|
|
|
use deno_core::serde_json;
|
|
|
|
use deno_core::serde_json::json;
|
|
|
|
use deno_core::serde_json::Value;
|
2020-12-29 19:58:20 -05:00
|
|
|
use deno_core::url::Url;
|
2020-12-07 05:46:39 -05:00
|
|
|
use deno_core::JsRuntime;
|
|
|
|
use deno_core::ModuleSpecifier;
|
|
|
|
use deno_core::OpFn;
|
|
|
|
use deno_core::RuntimeOptions;
|
2021-03-26 12:34:25 -04:00
|
|
|
use log::warn;
|
2021-01-29 14:34:33 -05:00
|
|
|
use lspower::lsp;
|
2020-12-07 05:46:39 -05:00
|
|
|
use regex::Captures;
|
|
|
|
use regex::Regex;
|
2021-10-10 17:26:22 -04:00
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::cmp;
|
|
|
|
use std::collections::HashMap;
|
2021-03-15 18:01:41 -04:00
|
|
|
use std::collections::HashSet;
|
2021-10-10 17:26:22 -04:00
|
|
|
use std::path::Path;
|
2021-09-07 10:39:32 -04:00
|
|
|
use std::sync::Arc;
|
2020-12-21 08:44:26 -05:00
|
|
|
use std::thread;
|
2021-10-10 17:26:22 -04:00
|
|
|
use text_size::TextRange;
|
|
|
|
use text_size::TextSize;
|
2020-12-21 08:44:26 -05:00
|
|
|
use tokio::sync::mpsc;
|
|
|
|
use tokio::sync::oneshot;
|
|
|
|
|
2021-09-15 22:07:52 -04:00
|
|
|
lazy_static::lazy_static! {
|
|
|
|
static ref BRACKET_ACCESSOR_RE: Regex = Regex::new(r#"^\[['"](.+)[\['"]\]$"#).unwrap();
|
|
|
|
static ref CAPTION_RE: Regex = Regex::new(r"<caption>(.*?)</caption>\s*\r?\n((?:\s|\S)*)").unwrap();
|
|
|
|
static ref CODEBLOCK_RE: Regex = Regex::new(r"^\s*[~`]{3}").unwrap();
|
|
|
|
static ref EMAIL_MATCH_RE: Regex = Regex::new(r"(.+)\s<([-.\w]+@[-.\w]+)>").unwrap();
|
|
|
|
static ref JSDOC_LINKS_RE: Regex = Regex::new(r"(?i)\{@(link|linkplain|linkcode) (https?://[^ |}]+?)(?:[| ]([^{}\n]+?))?\}").unwrap();
|
|
|
|
static ref PART_KIND_MODIFIER_RE: Regex = Regex::new(r",|\s+").unwrap();
|
|
|
|
static ref PART_RE: Regex = Regex::new(r"^(\S+)\s*-?\s*").unwrap();
|
|
|
|
static ref SCOPE_RE: Regex = Regex::new(r"scope_(\d)").unwrap();
|
|
|
|
}
|
|
|
|
|
2021-03-15 18:01:41 -04:00
|
|
|
const FILE_EXTENSION_KIND_MODIFIERS: &[&str] =
|
|
|
|
&[".d.ts", ".ts", ".tsx", ".js", ".jsx", ".json"];
|
|
|
|
|
2020-12-21 08:44:26 -05:00
|
|
|
type Request = (
|
|
|
|
RequestMethod,
|
|
|
|
StateSnapshot,
|
|
|
|
oneshot::Sender<Result<Value, AnyError>>,
|
|
|
|
);
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct TsServer(mpsc::UnboundedSender<Request>);
|
|
|
|
|
|
|
|
impl TsServer {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
let (tx, mut rx) = mpsc::unbounded_channel::<Request>();
|
|
|
|
let _join_handle = thread::spawn(move || {
|
2021-06-21 17:18:32 -04:00
|
|
|
let mut ts_runtime = load().expect("could not load tsc");
|
2020-12-21 08:44:26 -05:00
|
|
|
|
2021-01-12 02:50:02 -05:00
|
|
|
let runtime = create_basic_runtime();
|
2020-12-21 08:44:26 -05:00
|
|
|
runtime.block_on(async {
|
2021-06-21 17:18:32 -04:00
|
|
|
let mut started = false;
|
2020-12-21 08:44:26 -05:00
|
|
|
while let Some((req, state_snapshot, tx)) = rx.recv().await {
|
2021-06-21 17:18:32 -04:00
|
|
|
if !started {
|
|
|
|
// TODO(@kitsonk) need to reflect the debug state of the lsp here
|
|
|
|
start(&mut ts_runtime, false, &state_snapshot)
|
|
|
|
.expect("could not start tsc");
|
|
|
|
started = true;
|
|
|
|
}
|
2020-12-21 08:44:26 -05:00
|
|
|
let value = request(&mut ts_runtime, state_snapshot, req);
|
|
|
|
if tx.send(value).is_err() {
|
|
|
|
warn!("Unable to send result to client.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
Self(tx)
|
|
|
|
}
|
|
|
|
|
2021-02-24 22:15:55 -05:00
|
|
|
pub async fn request<R>(
|
2020-12-21 08:44:26 -05:00
|
|
|
&self,
|
|
|
|
snapshot: StateSnapshot,
|
|
|
|
req: RequestMethod,
|
2021-02-24 22:15:55 -05:00
|
|
|
) -> Result<R, AnyError>
|
|
|
|
where
|
|
|
|
R: de::DeserializeOwned,
|
|
|
|
{
|
2020-12-21 08:44:26 -05:00
|
|
|
let (tx, rx) = oneshot::channel::<Result<Value, AnyError>>();
|
|
|
|
if self.0.send((req, snapshot, tx)).is_err() {
|
|
|
|
return Err(anyhow!("failed to send request to tsc thread"));
|
|
|
|
}
|
2021-02-24 22:15:55 -05:00
|
|
|
rx.await?.map(|v| serde_json::from_value::<R>(v).unwrap())
|
2020-12-21 08:44:26 -05:00
|
|
|
}
|
|
|
|
}
|
2020-12-07 05:46:39 -05:00
|
|
|
|
2021-01-22 05:03:16 -05:00
|
|
|
/// An lsp representation of an asset in memory, that has either been retrieved
|
|
|
|
/// from static assets built into Rust, or static assets built into tsc.
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct AssetDocument {
|
2021-09-07 10:39:32 -04:00
|
|
|
pub text: Arc<String>,
|
2021-02-12 06:49:42 -05:00
|
|
|
pub length: usize,
|
2021-01-22 05:03:16 -05:00
|
|
|
pub line_index: LineIndex,
|
2021-06-04 17:31:44 -04:00
|
|
|
pub maybe_navigation_tree: Option<NavigationTree>,
|
2021-01-22 05:03:16 -05:00
|
|
|
}
|
|
|
|
|
2021-02-12 06:49:42 -05:00
|
|
|
impl AssetDocument {
|
|
|
|
pub fn new<T: AsRef<str>>(text: T) -> Self {
|
|
|
|
let text = text.as_ref();
|
|
|
|
Self {
|
2021-09-07 10:39:32 -04:00
|
|
|
text: Arc::new(text.to_string()),
|
2021-02-12 06:49:42 -05:00
|
|
|
length: text.encode_utf16().count(),
|
|
|
|
line_index: LineIndex::new(text),
|
2021-06-04 17:31:44 -04:00
|
|
|
maybe_navigation_tree: None,
|
2021-02-12 06:49:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Assets(HashMap<ModuleSpecifier, Option<AssetDocument>>);
|
|
|
|
|
|
|
|
impl Default for Assets {
|
|
|
|
fn default() -> Self {
|
|
|
|
let assets = tsc::STATIC_ASSETS
|
|
|
|
.iter()
|
|
|
|
.map(|(k, v)| {
|
|
|
|
let url_str = format!("asset:///{}", k);
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier = resolve_url(&url_str).unwrap();
|
2021-02-12 06:49:42 -05:00
|
|
|
let asset = AssetDocument::new(v);
|
|
|
|
(specifier, Some(asset))
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
Self(assets)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Assets {
|
|
|
|
pub fn contains_key(&self, k: &ModuleSpecifier) -> bool {
|
|
|
|
self.0.contains_key(k)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get(&self, k: &ModuleSpecifier) -> Option<&Option<AssetDocument>> {
|
|
|
|
self.0.get(k)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn insert(
|
|
|
|
&mut self,
|
|
|
|
k: ModuleSpecifier,
|
|
|
|
v: Option<AssetDocument>,
|
|
|
|
) -> Option<Option<AssetDocument>> {
|
|
|
|
self.0.insert(k, v)
|
|
|
|
}
|
2021-06-04 17:31:44 -04:00
|
|
|
|
|
|
|
pub fn set_navigation_tree(
|
|
|
|
&mut self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
navigation_tree: NavigationTree,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let maybe_doc = self
|
|
|
|
.0
|
|
|
|
.get_mut(specifier)
|
|
|
|
.ok_or_else(|| anyhow!("Missing asset."))?;
|
|
|
|
let doc = maybe_doc
|
|
|
|
.as_mut()
|
|
|
|
.ok_or_else(|| anyhow!("Cannot get doc mutable"))?;
|
|
|
|
doc.maybe_navigation_tree = Some(navigation_tree);
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-02-12 06:49:42 -05:00
|
|
|
}
|
|
|
|
|
2020-12-15 14:34:39 -05:00
|
|
|
/// Optionally returns an internal asset, first checking for any static assets
|
|
|
|
/// in Rust, then checking any previously retrieved static assets from the
|
|
|
|
/// isolate, and then finally, the tsc isolate itself.
|
2020-12-21 08:44:26 -05:00
|
|
|
pub async fn get_asset(
|
2020-12-15 14:34:39 -05:00
|
|
|
specifier: &ModuleSpecifier,
|
2020-12-21 08:44:26 -05:00
|
|
|
ts_server: &TsServer,
|
2021-02-12 06:49:42 -05:00
|
|
|
state_snapshot: StateSnapshot,
|
2021-01-22 05:03:16 -05:00
|
|
|
) -> Result<Option<AssetDocument>, AnyError> {
|
2020-12-15 14:34:39 -05:00
|
|
|
let specifier_str = specifier.to_string().replace("asset:///", "");
|
2021-01-22 05:03:16 -05:00
|
|
|
if let Some(text) = tsc::get_asset(&specifier_str) {
|
2021-02-12 06:49:42 -05:00
|
|
|
let maybe_asset = Some(AssetDocument::new(text));
|
2021-01-22 05:03:16 -05:00
|
|
|
Ok(maybe_asset)
|
2020-12-15 14:34:39 -05:00
|
|
|
} else {
|
2021-01-22 05:03:16 -05:00
|
|
|
let res = ts_server
|
2021-02-12 06:49:42 -05:00
|
|
|
.request(state_snapshot, RequestMethod::GetAsset(specifier.clone()))
|
2021-01-22 05:03:16 -05:00
|
|
|
.await?;
|
|
|
|
let maybe_text: Option<String> = serde_json::from_value(res)?;
|
2021-03-25 14:17:37 -04:00
|
|
|
let maybe_asset = maybe_text.map(AssetDocument::new);
|
2021-01-22 05:03:16 -05:00
|
|
|
Ok(maybe_asset)
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 18:01:41 -04:00
|
|
|
fn display_parts_to_string(parts: &[SymbolDisplayPart]) -> String {
|
2021-02-15 21:34:09 -05:00
|
|
|
parts
|
2021-03-15 18:01:41 -04:00
|
|
|
.iter()
|
|
|
|
.map(|p| p.text.to_string())
|
2021-02-15 21:34:09 -05:00
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join("")
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
2021-03-25 14:17:37 -04:00
|
|
|
fn get_tag_body_text(tag: &JsDocTagInfo) -> Option<String> {
|
2021-05-27 19:33:11 -04:00
|
|
|
tag.text.as_ref().map(|display_parts| {
|
|
|
|
// TODO(@kitsonk) check logic in vscode about handling this API change in
|
|
|
|
// tsserver
|
|
|
|
let text = display_parts_to_string(display_parts);
|
|
|
|
match tag.name.as_str() {
|
|
|
|
"example" => {
|
2021-09-15 22:07:52 -04:00
|
|
|
if CAPTION_RE.is_match(&text) {
|
|
|
|
CAPTION_RE
|
2021-05-27 19:33:11 -04:00
|
|
|
.replace(&text, |c: &Captures| {
|
|
|
|
format!("{}\n\n{}", &c[1], make_codeblock(&c[2]))
|
|
|
|
})
|
|
|
|
.to_string()
|
|
|
|
} else {
|
|
|
|
make_codeblock(&text)
|
|
|
|
}
|
|
|
|
}
|
2021-09-15 22:07:52 -04:00
|
|
|
"author" => EMAIL_MATCH_RE
|
|
|
|
.replace(&text, |c: &Captures| format!("{} {}", &c[1], &c[2]))
|
|
|
|
.to_string(),
|
2021-05-27 19:33:11 -04:00
|
|
|
"default" => make_codeblock(&text),
|
|
|
|
_ => replace_links(&text),
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:17:37 -04:00
|
|
|
fn get_tag_documentation(tag: &JsDocTagInfo) -> String {
|
2020-12-07 05:46:39 -05:00
|
|
|
match tag.name.as_str() {
|
|
|
|
"augments" | "extends" | "param" | "template" => {
|
2021-05-27 19:33:11 -04:00
|
|
|
if let Some(display_parts) = &tag.text {
|
|
|
|
// TODO(@kitsonk) check logic in vscode about handling this API change
|
|
|
|
// in tsserver
|
|
|
|
let text = display_parts_to_string(display_parts);
|
2021-09-15 22:07:52 -04:00
|
|
|
let body: Vec<&str> = PART_RE.split(&text).collect();
|
2020-12-07 05:46:39 -05:00
|
|
|
if body.len() == 3 {
|
|
|
|
let param = body[1];
|
|
|
|
let doc = body[2];
|
|
|
|
let label = format!("*@{}* `{}`", tag.name, param);
|
|
|
|
if doc.is_empty() {
|
|
|
|
return label;
|
|
|
|
}
|
|
|
|
if doc.contains('\n') {
|
|
|
|
return format!("{} \n{}", label, replace_links(doc));
|
|
|
|
} else {
|
|
|
|
return format!("{} - {}", label, replace_links(doc));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
let label = format!("*@{}*", tag.name);
|
|
|
|
let maybe_text = get_tag_body_text(tag);
|
|
|
|
if let Some(text) = maybe_text {
|
|
|
|
if text.contains('\n') {
|
|
|
|
format!("{} \n{}", label, text)
|
|
|
|
} else {
|
|
|
|
format!("{} - {}", label, text)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
label
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn make_codeblock(text: &str) -> String {
|
2021-09-15 22:07:52 -04:00
|
|
|
if CODEBLOCK_RE.is_match(text) {
|
2020-12-07 05:46:39 -05:00
|
|
|
text.to_string()
|
|
|
|
} else {
|
|
|
|
format!("```\n{}\n```", text)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Replace JSDoc like links (`{@link http://example.com}`) with markdown links
|
|
|
|
fn replace_links(text: &str) -> String {
|
2021-09-15 22:07:52 -04:00
|
|
|
JSDOC_LINKS_RE
|
2020-12-07 05:46:39 -05:00
|
|
|
.replace_all(text, |c: &Captures| match &c[1] {
|
|
|
|
"linkcode" => format!(
|
|
|
|
"[`{}`]({})",
|
|
|
|
if c.get(3).is_none() {
|
|
|
|
&c[2]
|
|
|
|
} else {
|
|
|
|
c[3].trim()
|
|
|
|
},
|
|
|
|
&c[2]
|
|
|
|
),
|
|
|
|
_ => format!(
|
|
|
|
"[{}]({})",
|
|
|
|
if c.get(3).is_none() {
|
|
|
|
&c[2]
|
|
|
|
} else {
|
|
|
|
c[3].trim()
|
|
|
|
},
|
|
|
|
&c[2]
|
|
|
|
),
|
|
|
|
})
|
|
|
|
.to_string()
|
|
|
|
}
|
|
|
|
|
2021-03-15 18:01:41 -04:00
|
|
|
fn parse_kind_modifier(kind_modifiers: &str) -> HashSet<&str> {
|
2021-09-15 22:07:52 -04:00
|
|
|
PART_KIND_MODIFIER_RE.split(kind_modifiers).collect()
|
2021-03-15 18:01:41 -04:00
|
|
|
}
|
|
|
|
|
2021-04-19 01:11:26 -04:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(untagged)]
|
|
|
|
pub enum OneOrMany<T> {
|
|
|
|
One(T),
|
|
|
|
Many(Vec<T>),
|
|
|
|
}
|
|
|
|
|
2021-03-15 18:01:41 -04:00
|
|
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
|
2020-12-07 05:46:39 -05:00
|
|
|
pub enum ScriptElementKind {
|
|
|
|
#[serde(rename = "")]
|
|
|
|
Unknown,
|
|
|
|
#[serde(rename = "warning")]
|
|
|
|
Warning,
|
|
|
|
#[serde(rename = "keyword")]
|
|
|
|
Keyword,
|
|
|
|
#[serde(rename = "script")]
|
|
|
|
ScriptElement,
|
|
|
|
#[serde(rename = "module")]
|
|
|
|
ModuleElement,
|
|
|
|
#[serde(rename = "class")]
|
|
|
|
ClassElement,
|
|
|
|
#[serde(rename = "local class")]
|
|
|
|
LocalClassElement,
|
|
|
|
#[serde(rename = "interface")]
|
|
|
|
InterfaceElement,
|
|
|
|
#[serde(rename = "type")]
|
|
|
|
TypeElement,
|
|
|
|
#[serde(rename = "enum")]
|
|
|
|
EnumElement,
|
|
|
|
#[serde(rename = "enum member")]
|
|
|
|
EnumMemberElement,
|
|
|
|
#[serde(rename = "var")]
|
|
|
|
VariableElement,
|
|
|
|
#[serde(rename = "local var")]
|
|
|
|
LocalVariableElement,
|
|
|
|
#[serde(rename = "function")]
|
|
|
|
FunctionElement,
|
|
|
|
#[serde(rename = "local function")]
|
|
|
|
LocalFunctionElement,
|
|
|
|
#[serde(rename = "method")]
|
|
|
|
MemberFunctionElement,
|
|
|
|
#[serde(rename = "getter")]
|
|
|
|
MemberGetAccessorElement,
|
|
|
|
#[serde(rename = "setter")]
|
|
|
|
MemberSetAccessorElement,
|
|
|
|
#[serde(rename = "property")]
|
|
|
|
MemberVariableElement,
|
|
|
|
#[serde(rename = "constructor")]
|
|
|
|
ConstructorImplementationElement,
|
|
|
|
#[serde(rename = "call")]
|
|
|
|
CallSignatureElement,
|
|
|
|
#[serde(rename = "index")]
|
|
|
|
IndexSignatureElement,
|
|
|
|
#[serde(rename = "construct")]
|
|
|
|
ConstructSignatureElement,
|
|
|
|
#[serde(rename = "parameter")]
|
|
|
|
ParameterElement,
|
|
|
|
#[serde(rename = "type parameter")]
|
|
|
|
TypeParameterElement,
|
|
|
|
#[serde(rename = "primitive type")]
|
|
|
|
PrimitiveType,
|
|
|
|
#[serde(rename = "label")]
|
|
|
|
Label,
|
|
|
|
#[serde(rename = "alias")]
|
|
|
|
Alias,
|
|
|
|
#[serde(rename = "const")]
|
|
|
|
ConstElement,
|
|
|
|
#[serde(rename = "let")]
|
|
|
|
LetElement,
|
|
|
|
#[serde(rename = "directory")]
|
|
|
|
Directory,
|
|
|
|
#[serde(rename = "external module name")]
|
|
|
|
ExternalModuleName,
|
|
|
|
#[serde(rename = "JSX attribute")]
|
|
|
|
JsxAttribute,
|
|
|
|
#[serde(rename = "string")]
|
|
|
|
String,
|
|
|
|
}
|
|
|
|
|
2021-03-15 18:01:41 -04:00
|
|
|
impl Default for ScriptElementKind {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::Unknown
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 14:34:33 -05:00
|
|
|
impl From<ScriptElementKind> for lsp::CompletionItemKind {
|
2020-12-08 05:36:13 -05:00
|
|
|
fn from(kind: ScriptElementKind) -> Self {
|
|
|
|
match kind {
|
|
|
|
ScriptElementKind::PrimitiveType | ScriptElementKind::Keyword => {
|
2021-03-15 18:01:41 -04:00
|
|
|
lsp::CompletionItemKind::Keyword
|
2020-12-08 05:36:13 -05:00
|
|
|
}
|
2021-03-15 18:01:41 -04:00
|
|
|
ScriptElementKind::ConstElement
|
|
|
|
| ScriptElementKind::LetElement
|
2020-12-08 05:36:13 -05:00
|
|
|
| ScriptElementKind::VariableElement
|
|
|
|
| ScriptElementKind::LocalVariableElement
|
2021-03-15 18:01:41 -04:00
|
|
|
| ScriptElementKind::Alias
|
|
|
|
| ScriptElementKind::ParameterElement => {
|
|
|
|
lsp::CompletionItemKind::Variable
|
|
|
|
}
|
2020-12-08 05:36:13 -05:00
|
|
|
ScriptElementKind::MemberVariableElement
|
|
|
|
| ScriptElementKind::MemberGetAccessorElement
|
|
|
|
| ScriptElementKind::MemberSetAccessorElement => {
|
2021-03-15 18:01:41 -04:00
|
|
|
lsp::CompletionItemKind::Field
|
|
|
|
}
|
|
|
|
ScriptElementKind::FunctionElement
|
|
|
|
| ScriptElementKind::LocalFunctionElement => {
|
|
|
|
lsp::CompletionItemKind::Function
|
2020-12-08 05:36:13 -05:00
|
|
|
}
|
|
|
|
ScriptElementKind::MemberFunctionElement
|
|
|
|
| ScriptElementKind::ConstructSignatureElement
|
|
|
|
| ScriptElementKind::CallSignatureElement
|
2021-03-15 18:01:41 -04:00
|
|
|
| ScriptElementKind::IndexSignatureElement => {
|
|
|
|
lsp::CompletionItemKind::Method
|
|
|
|
}
|
|
|
|
ScriptElementKind::EnumElement => lsp::CompletionItemKind::Enum,
|
|
|
|
ScriptElementKind::EnumMemberElement => {
|
|
|
|
lsp::CompletionItemKind::EnumMember
|
|
|
|
}
|
2020-12-08 05:36:13 -05:00
|
|
|
ScriptElementKind::ModuleElement
|
2021-03-15 18:01:41 -04:00
|
|
|
| ScriptElementKind::ExternalModuleName => {
|
|
|
|
lsp::CompletionItemKind::Module
|
2020-12-08 05:36:13 -05:00
|
|
|
}
|
2021-03-15 18:01:41 -04:00
|
|
|
ScriptElementKind::ClassElement | ScriptElementKind::TypeElement => {
|
|
|
|
lsp::CompletionItemKind::Class
|
2020-12-08 05:36:13 -05:00
|
|
|
}
|
2021-03-15 18:01:41 -04:00
|
|
|
ScriptElementKind::InterfaceElement => lsp::CompletionItemKind::Interface,
|
|
|
|
ScriptElementKind::Warning => lsp::CompletionItemKind::Text,
|
|
|
|
ScriptElementKind::ScriptElement => lsp::CompletionItemKind::File,
|
|
|
|
ScriptElementKind::Directory => lsp::CompletionItemKind::Folder,
|
|
|
|
ScriptElementKind::String => lsp::CompletionItemKind::Constant,
|
|
|
|
_ => lsp::CompletionItemKind::Property,
|
2020-12-08 05:36:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-19 01:11:26 -04:00
|
|
|
impl From<ScriptElementKind> for lsp::SymbolKind {
|
|
|
|
fn from(kind: ScriptElementKind) -> Self {
|
|
|
|
match kind {
|
|
|
|
ScriptElementKind::ModuleElement => lsp::SymbolKind::Module,
|
|
|
|
ScriptElementKind::ClassElement => lsp::SymbolKind::Class,
|
|
|
|
ScriptElementKind::EnumElement => lsp::SymbolKind::Enum,
|
|
|
|
ScriptElementKind::InterfaceElement => lsp::SymbolKind::Interface,
|
|
|
|
ScriptElementKind::MemberFunctionElement => lsp::SymbolKind::Method,
|
|
|
|
ScriptElementKind::MemberVariableElement => lsp::SymbolKind::Property,
|
|
|
|
ScriptElementKind::MemberGetAccessorElement => lsp::SymbolKind::Property,
|
|
|
|
ScriptElementKind::MemberSetAccessorElement => lsp::SymbolKind::Property,
|
|
|
|
ScriptElementKind::VariableElement => lsp::SymbolKind::Variable,
|
|
|
|
ScriptElementKind::ConstElement => lsp::SymbolKind::Variable,
|
|
|
|
ScriptElementKind::LocalVariableElement => lsp::SymbolKind::Variable,
|
|
|
|
ScriptElementKind::FunctionElement => lsp::SymbolKind::Function,
|
|
|
|
ScriptElementKind::LocalFunctionElement => lsp::SymbolKind::Function,
|
|
|
|
ScriptElementKind::ConstructSignatureElement => {
|
|
|
|
lsp::SymbolKind::Constructor
|
|
|
|
}
|
|
|
|
ScriptElementKind::ConstructorImplementationElement => {
|
|
|
|
lsp::SymbolKind::Constructor
|
|
|
|
}
|
|
|
|
_ => lsp::SymbolKind::Variable,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-04 13:53:02 -05:00
|
|
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
|
2020-12-07 05:46:39 -05:00
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct TextSpan {
|
2021-01-31 22:30:41 -05:00
|
|
|
pub start: u32,
|
|
|
|
pub length: u32,
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TextSpan {
|
2021-01-29 14:34:33 -05:00
|
|
|
pub fn to_range(&self, line_index: &LineIndex) -> lsp::Range {
|
|
|
|
lsp::Range {
|
2021-01-22 05:03:16 -05:00
|
|
|
start: line_index.position_tsc(self.start.into()),
|
|
|
|
end: line_index.position_tsc(TextSize::from(self.start + self.length)),
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct SymbolDisplayPart {
|
|
|
|
text: String,
|
|
|
|
kind: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-03-25 14:17:37 -04:00
|
|
|
pub struct JsDocTagInfo {
|
2020-12-07 05:46:39 -05:00
|
|
|
name: String,
|
2021-05-27 19:33:11 -04:00
|
|
|
text: Option<Vec<SymbolDisplayPart>>,
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct QuickInfo {
|
|
|
|
kind: ScriptElementKind,
|
|
|
|
kind_modifiers: String,
|
|
|
|
text_span: TextSpan,
|
|
|
|
display_parts: Option<Vec<SymbolDisplayPart>>,
|
|
|
|
documentation: Option<Vec<SymbolDisplayPart>>,
|
2021-03-25 14:17:37 -04:00
|
|
|
tags: Option<Vec<JsDocTagInfo>>,
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl QuickInfo {
|
2021-01-29 14:34:33 -05:00
|
|
|
pub fn to_hover(&self, line_index: &LineIndex) -> lsp::Hover {
|
|
|
|
let mut contents = Vec::<lsp::MarkedString>::new();
|
2021-03-15 18:01:41 -04:00
|
|
|
if let Some(display_string) = self
|
|
|
|
.display_parts
|
|
|
|
.clone()
|
|
|
|
.map(|p| display_parts_to_string(&p))
|
2020-12-07 05:46:39 -05:00
|
|
|
{
|
2021-01-29 14:34:33 -05:00
|
|
|
contents.push(lsp::MarkedString::from_language_code(
|
2020-12-07 05:46:39 -05:00
|
|
|
"typescript".to_string(),
|
|
|
|
display_string,
|
|
|
|
));
|
|
|
|
}
|
2021-03-15 18:01:41 -04:00
|
|
|
if let Some(documentation) = self
|
|
|
|
.documentation
|
|
|
|
.clone()
|
|
|
|
.map(|p| display_parts_to_string(&p))
|
2020-12-07 05:46:39 -05:00
|
|
|
{
|
2021-01-29 14:34:33 -05:00
|
|
|
contents.push(lsp::MarkedString::from_markdown(documentation));
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
if let Some(tags) = &self.tags {
|
|
|
|
let tags_preview = tags
|
|
|
|
.iter()
|
|
|
|
.map(get_tag_documentation)
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join(" \n\n");
|
|
|
|
if !tags_preview.is_empty() {
|
2021-01-29 14:34:33 -05:00
|
|
|
contents.push(lsp::MarkedString::from_markdown(format!(
|
2020-12-07 05:46:39 -05:00
|
|
|
"\n\n{}",
|
|
|
|
tags_preview
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
}
|
2021-01-29 14:34:33 -05:00
|
|
|
lsp::Hover {
|
|
|
|
contents: lsp::HoverContents::Array(contents),
|
2020-12-07 05:46:39 -05:00
|
|
|
range: Some(self.text_span.to_range(line_index)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-12 16:53:27 -05:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct DocumentSpan {
|
|
|
|
text_span: TextSpan,
|
|
|
|
pub file_name: String,
|
|
|
|
original_text_span: Option<TextSpan>,
|
|
|
|
original_file_name: Option<String>,
|
|
|
|
context_span: Option<TextSpan>,
|
|
|
|
original_context_span: Option<TextSpan>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DocumentSpan {
|
2021-02-06 07:39:01 -05:00
|
|
|
pub(crate) async fn to_link(
|
2021-01-12 16:53:27 -05:00
|
|
|
&self,
|
2021-01-22 05:03:16 -05:00
|
|
|
line_index: &LineIndex,
|
2021-02-06 07:39:01 -05:00
|
|
|
language_server: &mut language_server::Inner,
|
|
|
|
) -> Option<lsp::LocationLink> {
|
2021-07-25 01:33:42 -04:00
|
|
|
let target_specifier = normalize_specifier(&self.file_name).ok()?;
|
2021-02-17 23:37:05 -05:00
|
|
|
let target_line_index = language_server
|
|
|
|
.get_line_index(target_specifier.clone())
|
|
|
|
.await
|
|
|
|
.ok()?;
|
|
|
|
let target_uri = language_server
|
|
|
|
.url_map
|
|
|
|
.normalize_specifier(&target_specifier)
|
2021-07-25 01:33:42 -04:00
|
|
|
.ok()?;
|
2021-02-17 23:37:05 -05:00
|
|
|
let (target_range, target_selection_range) =
|
|
|
|
if let Some(context_span) = &self.context_span {
|
|
|
|
(
|
|
|
|
context_span.to_range(&target_line_index),
|
|
|
|
self.text_span.to_range(&target_line_index),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
(
|
|
|
|
self.text_span.to_range(&target_line_index),
|
|
|
|
self.text_span.to_range(&target_line_index),
|
|
|
|
)
|
2021-01-12 16:53:27 -05:00
|
|
|
};
|
2021-02-17 23:37:05 -05:00
|
|
|
let origin_selection_range =
|
|
|
|
if let Some(original_context_span) = &self.original_context_span {
|
|
|
|
Some(original_context_span.to_range(line_index))
|
|
|
|
} else {
|
2021-03-25 14:17:37 -04:00
|
|
|
self
|
|
|
|
.original_text_span
|
|
|
|
.as_ref()
|
|
|
|
.map(|original_text_span| original_text_span.to_range(line_index))
|
2021-02-17 23:37:05 -05:00
|
|
|
};
|
|
|
|
let link = lsp::LocationLink {
|
|
|
|
origin_selection_range,
|
|
|
|
target_uri,
|
|
|
|
target_range,
|
|
|
|
target_selection_range,
|
|
|
|
};
|
|
|
|
Some(link)
|
2021-01-12 16:53:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-31 22:30:41 -05:00
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct NavigationTree {
|
|
|
|
pub text: String,
|
|
|
|
pub kind: ScriptElementKind,
|
|
|
|
pub kind_modifiers: String,
|
|
|
|
pub spans: Vec<TextSpan>,
|
|
|
|
pub name_span: Option<TextSpan>,
|
|
|
|
pub child_items: Option<Vec<NavigationTree>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NavigationTree {
|
|
|
|
pub fn to_code_lens(
|
|
|
|
&self,
|
|
|
|
line_index: &LineIndex,
|
|
|
|
specifier: &ModuleSpecifier,
|
2021-06-04 17:31:44 -04:00
|
|
|
source: &code_lens::CodeLensSource,
|
2021-01-31 22:30:41 -05:00
|
|
|
) -> lsp::CodeLens {
|
2021-02-08 05:45:10 -05:00
|
|
|
let range = if let Some(name_span) = &self.name_span {
|
|
|
|
name_span.to_range(line_index)
|
|
|
|
} else if !self.spans.is_empty() {
|
|
|
|
let span = &self.spans[0];
|
|
|
|
span.to_range(line_index)
|
|
|
|
} else {
|
|
|
|
lsp::Range::default()
|
|
|
|
};
|
2021-01-31 22:30:41 -05:00
|
|
|
lsp::CodeLens {
|
2021-02-08 05:45:10 -05:00
|
|
|
range,
|
2021-01-31 22:30:41 -05:00
|
|
|
command: None,
|
|
|
|
data: Some(json!({
|
|
|
|
"specifier": specifier,
|
|
|
|
"source": source
|
|
|
|
})),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-19 21:29:27 -04:00
|
|
|
pub fn collect_document_symbols(
|
|
|
|
&self,
|
|
|
|
line_index: &LineIndex,
|
|
|
|
document_symbols: &mut Vec<lsp::DocumentSymbol>,
|
|
|
|
) -> bool {
|
|
|
|
let mut should_include = self.should_include_entry();
|
|
|
|
if !should_include
|
|
|
|
&& self.child_items.as_ref().map_or(true, |v| v.is_empty())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let children = self
|
|
|
|
.child_items
|
|
|
|
.as_ref()
|
|
|
|
.map_or(&[] as &[NavigationTree], |v| v.as_slice());
|
|
|
|
for span in self.spans.iter() {
|
|
|
|
let range = TextRange::at(span.start.into(), span.length.into());
|
|
|
|
let mut symbol_children = Vec::<lsp::DocumentSymbol>::new();
|
|
|
|
for child in children.iter() {
|
|
|
|
let should_traverse_child = child
|
|
|
|
.spans
|
|
|
|
.iter()
|
|
|
|
.map(|child_span| {
|
|
|
|
TextRange::at(child_span.start.into(), child_span.length.into())
|
|
|
|
})
|
|
|
|
.any(|child_range| range.intersect(child_range).is_some());
|
|
|
|
if should_traverse_child {
|
|
|
|
let included_child =
|
|
|
|
child.collect_document_symbols(line_index, &mut symbol_children);
|
|
|
|
should_include = should_include || included_child;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if should_include {
|
|
|
|
let mut selection_span = span;
|
|
|
|
if let Some(name_span) = self.name_span.as_ref() {
|
|
|
|
let name_range =
|
|
|
|
TextRange::at(name_span.start.into(), name_span.length.into());
|
|
|
|
if range.contains_range(name_range) {
|
|
|
|
selection_span = name_span;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut tags: Option<Vec<lsp::SymbolTag>> = None;
|
|
|
|
let kind_modifiers = parse_kind_modifier(&self.kind_modifiers);
|
|
|
|
if kind_modifiers.contains("deprecated") {
|
|
|
|
tags = Some(vec![lsp::SymbolTag::Deprecated]);
|
|
|
|
}
|
|
|
|
|
|
|
|
let children = if !symbol_children.is_empty() {
|
|
|
|
Some(symbol_children)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
// The field `deprecated` is deprecated but DocumentSymbol does not have
|
|
|
|
// a default, therefore we have to supply the deprecated deprecated
|
|
|
|
// field. It is like a bad version of Inception.
|
|
|
|
#[allow(deprecated)]
|
|
|
|
document_symbols.push(lsp::DocumentSymbol {
|
|
|
|
name: self.text.clone(),
|
|
|
|
kind: self.kind.clone().into(),
|
|
|
|
range: span.to_range(line_index),
|
|
|
|
selection_range: selection_span.to_range(line_index),
|
|
|
|
tags,
|
|
|
|
children,
|
|
|
|
detail: None,
|
|
|
|
deprecated: None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
should_include
|
|
|
|
}
|
|
|
|
|
|
|
|
fn should_include_entry(&self) -> bool {
|
|
|
|
if let ScriptElementKind::Alias = self.kind {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
!self.text.is_empty() && self.text != "<function>" && self.text != "<class>"
|
|
|
|
}
|
|
|
|
|
2021-01-31 22:30:41 -05:00
|
|
|
pub fn walk<F>(&self, callback: &F)
|
|
|
|
where
|
|
|
|
F: Fn(&NavigationTree, Option<&NavigationTree>),
|
|
|
|
{
|
|
|
|
callback(self, None);
|
|
|
|
if let Some(child_items) = &self.child_items {
|
|
|
|
for child in child_items {
|
|
|
|
child.walk_child(callback, self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn walk_child<F>(&self, callback: &F, parent: &NavigationTree)
|
|
|
|
where
|
|
|
|
F: Fn(&NavigationTree, Option<&NavigationTree>),
|
|
|
|
{
|
|
|
|
callback(self, Some(parent));
|
|
|
|
if let Some(child_items) = &self.child_items {
|
|
|
|
for child in child_items {
|
|
|
|
child.walk_child(callback, self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-12 16:53:27 -05:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct ImplementationLocation {
|
|
|
|
#[serde(flatten)]
|
|
|
|
pub document_span: DocumentSpan,
|
|
|
|
// ImplementationLocation props
|
|
|
|
kind: ScriptElementKind,
|
|
|
|
display_parts: Vec<SymbolDisplayPart>,
|
|
|
|
}
|
|
|
|
|
2021-02-08 05:45:10 -05:00
|
|
|
impl ImplementationLocation {
|
2021-02-17 23:37:05 -05:00
|
|
|
pub(crate) fn to_location(
|
|
|
|
&self,
|
|
|
|
line_index: &LineIndex,
|
|
|
|
language_server: &mut language_server::Inner,
|
|
|
|
) -> lsp::Location {
|
2021-07-25 01:33:42 -04:00
|
|
|
let specifier = normalize_specifier(&self.document_span.file_name)
|
|
|
|
.unwrap_or_else(|_| ModuleSpecifier::parse("deno://invalid").unwrap());
|
2021-02-17 23:37:05 -05:00
|
|
|
let uri = language_server
|
|
|
|
.url_map
|
|
|
|
.normalize_specifier(&specifier)
|
2021-07-25 01:33:42 -04:00
|
|
|
.unwrap_or_else(|_| ModuleSpecifier::parse("deno://invalid").unwrap());
|
2021-02-08 05:45:10 -05:00
|
|
|
lsp::Location {
|
|
|
|
uri,
|
|
|
|
range: self.document_span.text_span.to_range(line_index),
|
|
|
|
}
|
|
|
|
}
|
2021-02-17 22:15:13 -05:00
|
|
|
|
|
|
|
pub(crate) async fn to_link(
|
|
|
|
&self,
|
|
|
|
line_index: &LineIndex,
|
|
|
|
language_server: &mut language_server::Inner,
|
|
|
|
) -> Option<lsp::LocationLink> {
|
|
|
|
self
|
|
|
|
.document_span
|
|
|
|
.to_link(line_index, language_server)
|
|
|
|
.await
|
|
|
|
}
|
2021-02-08 05:45:10 -05:00
|
|
|
}
|
|
|
|
|
2020-12-29 19:58:20 -05:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct RenameLocation {
|
2021-01-16 07:00:42 -05:00
|
|
|
#[serde(flatten)]
|
|
|
|
document_span: DocumentSpan,
|
2020-12-29 19:58:20 -05:00
|
|
|
// RenameLocation props
|
|
|
|
prefix_text: Option<String>,
|
|
|
|
suffix_text: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct RenameLocations {
|
|
|
|
pub locations: Vec<RenameLocation>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RenameLocations {
|
2021-02-06 07:39:01 -05:00
|
|
|
pub(crate) async fn into_workspace_edit(
|
2020-12-29 19:58:20 -05:00
|
|
|
self,
|
|
|
|
new_name: &str,
|
2021-02-06 07:39:01 -05:00
|
|
|
language_server: &mut language_server::Inner,
|
|
|
|
) -> Result<lsp::WorkspaceEdit, AnyError> {
|
2021-01-29 14:34:33 -05:00
|
|
|
let mut text_document_edit_map: HashMap<Url, lsp::TextDocumentEdit> =
|
2020-12-29 19:58:20 -05:00
|
|
|
HashMap::new();
|
|
|
|
for location in self.locations.iter() {
|
2021-06-21 17:18:32 -04:00
|
|
|
let specifier = normalize_specifier(&location.document_span.file_name)?;
|
2021-02-17 23:37:05 -05:00
|
|
|
let uri = language_server.url_map.normalize_specifier(&specifier)?;
|
2020-12-29 19:58:20 -05:00
|
|
|
|
|
|
|
// ensure TextDocumentEdit for `location.file_name`.
|
|
|
|
if text_document_edit_map.get(&uri).is_none() {
|
|
|
|
text_document_edit_map.insert(
|
|
|
|
uri.clone(),
|
2021-01-29 14:34:33 -05:00
|
|
|
lsp::TextDocumentEdit {
|
|
|
|
text_document: lsp::OptionalVersionedTextDocumentIdentifier {
|
2020-12-29 19:58:20 -05:00
|
|
|
uri: uri.clone(),
|
2021-02-06 07:39:01 -05:00
|
|
|
version: language_server.document_version(specifier.clone()),
|
2020-12-29 19:58:20 -05:00
|
|
|
},
|
2021-01-29 14:34:33 -05:00
|
|
|
edits:
|
|
|
|
Vec::<lsp::OneOf<lsp::TextEdit, lsp::AnnotatedTextEdit>>::new(),
|
2020-12-29 19:58:20 -05:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// push TextEdit for ensured `TextDocumentEdit.edits`.
|
|
|
|
let document_edit = text_document_edit_map.get_mut(&uri).unwrap();
|
2021-01-29 14:34:33 -05:00
|
|
|
document_edit.edits.push(lsp::OneOf::Left(lsp::TextEdit {
|
|
|
|
range: location
|
|
|
|
.document_span
|
|
|
|
.text_span
|
2021-02-06 07:39:01 -05:00
|
|
|
.to_range(&language_server.get_line_index(specifier.clone()).await?),
|
2021-01-29 14:34:33 -05:00
|
|
|
new_text: new_name.to_string(),
|
|
|
|
}));
|
2020-12-29 19:58:20 -05:00
|
|
|
}
|
|
|
|
|
2021-01-29 14:34:33 -05:00
|
|
|
Ok(lsp::WorkspaceEdit {
|
2021-01-12 02:50:02 -05:00
|
|
|
change_annotations: None,
|
2020-12-29 19:58:20 -05:00
|
|
|
changes: None,
|
2021-01-29 14:34:33 -05:00
|
|
|
document_changes: Some(lsp::DocumentChanges::Edits(
|
2020-12-29 19:58:20 -05:00
|
|
|
text_document_edit_map.values().cloned().collect(),
|
|
|
|
)),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-07 05:46:39 -05:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
pub enum HighlightSpanKind {
|
|
|
|
#[serde(rename = "none")]
|
|
|
|
None,
|
|
|
|
#[serde(rename = "definition")]
|
|
|
|
Definition,
|
|
|
|
#[serde(rename = "reference")]
|
|
|
|
Reference,
|
|
|
|
#[serde(rename = "writtenReference")]
|
|
|
|
WrittenReference,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct HighlightSpan {
|
|
|
|
file_name: Option<String>,
|
|
|
|
is_in_string: Option<bool>,
|
|
|
|
text_span: TextSpan,
|
|
|
|
context_span: Option<TextSpan>,
|
|
|
|
kind: HighlightSpanKind,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct DefinitionInfo {
|
|
|
|
kind: ScriptElementKind,
|
|
|
|
name: String,
|
|
|
|
container_kind: Option<ScriptElementKind>,
|
|
|
|
container_name: Option<String>,
|
2021-01-12 16:53:27 -05:00
|
|
|
|
|
|
|
#[serde(flatten)]
|
|
|
|
pub document_span: DocumentSpan,
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct DefinitionInfoAndBoundSpan {
|
|
|
|
pub definitions: Option<Vec<DefinitionInfo>>,
|
|
|
|
text_span: TextSpan,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DefinitionInfoAndBoundSpan {
|
2021-02-06 07:39:01 -05:00
|
|
|
pub(crate) async fn to_definition(
|
2020-12-07 05:46:39 -05:00
|
|
|
&self,
|
2021-01-22 05:03:16 -05:00
|
|
|
line_index: &LineIndex,
|
2021-02-06 07:39:01 -05:00
|
|
|
language_server: &mut language_server::Inner,
|
|
|
|
) -> Option<lsp::GotoDefinitionResponse> {
|
2020-12-07 05:46:39 -05:00
|
|
|
if let Some(definitions) = &self.definitions {
|
2021-01-29 14:34:33 -05:00
|
|
|
let mut location_links = Vec::<lsp::LocationLink>::new();
|
2020-12-21 08:44:26 -05:00
|
|
|
for di in definitions {
|
2021-02-06 07:39:01 -05:00
|
|
|
if let Some(link) =
|
|
|
|
di.document_span.to_link(line_index, language_server).await
|
2021-01-12 16:53:27 -05:00
|
|
|
{
|
|
|
|
location_links.push(link);
|
2020-12-21 08:44:26 -05:00
|
|
|
}
|
|
|
|
}
|
2021-01-29 14:34:33 -05:00
|
|
|
Some(lsp::GotoDefinitionResponse::Link(location_links))
|
2020-12-07 05:46:39 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct DocumentHighlights {
|
|
|
|
file_name: String,
|
|
|
|
highlight_spans: Vec<HighlightSpan>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DocumentHighlights {
|
|
|
|
pub fn to_highlight(
|
|
|
|
&self,
|
2021-01-22 05:03:16 -05:00
|
|
|
line_index: &LineIndex,
|
2021-01-29 14:34:33 -05:00
|
|
|
) -> Vec<lsp::DocumentHighlight> {
|
2020-12-07 05:46:39 -05:00
|
|
|
self
|
|
|
|
.highlight_spans
|
|
|
|
.iter()
|
2021-01-29 14:34:33 -05:00
|
|
|
.map(|hs| lsp::DocumentHighlight {
|
2020-12-07 05:46:39 -05:00
|
|
|
range: hs.text_span.to_range(line_index),
|
|
|
|
kind: match hs.kind {
|
|
|
|
HighlightSpanKind::WrittenReference => {
|
2021-01-29 14:34:33 -05:00
|
|
|
Some(lsp::DocumentHighlightKind::Write)
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
2021-01-29 14:34:33 -05:00
|
|
|
_ => Some(lsp::DocumentHighlightKind::Read),
|
2020-12-07 05:46:39 -05:00
|
|
|
},
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-04 13:53:02 -05:00
|
|
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct TextChange {
|
2021-05-29 07:21:11 -04:00
|
|
|
pub span: TextSpan,
|
|
|
|
pub new_text: String,
|
2021-02-04 13:53:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TextChange {
|
|
|
|
pub fn as_text_edit(
|
|
|
|
&self,
|
|
|
|
line_index: &LineIndex,
|
|
|
|
) -> lsp::OneOf<lsp::TextEdit, lsp::AnnotatedTextEdit> {
|
|
|
|
lsp::OneOf::Left(lsp::TextEdit {
|
|
|
|
range: self.span.to_range(line_index),
|
|
|
|
new_text: self.new_text.clone(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct FileTextChanges {
|
2021-05-29 07:21:11 -04:00
|
|
|
pub file_name: String,
|
|
|
|
pub text_changes: Vec<TextChange>,
|
2021-02-04 13:53:02 -05:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2021-05-29 07:21:11 -04:00
|
|
|
pub is_new_file: Option<bool>,
|
2021-02-04 13:53:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FileTextChanges {
|
2021-02-06 07:39:01 -05:00
|
|
|
pub(crate) async fn to_text_document_edit(
|
2021-02-04 13:53:02 -05:00
|
|
|
&self,
|
2021-02-06 07:39:01 -05:00
|
|
|
language_server: &mut language_server::Inner,
|
|
|
|
) -> Result<lsp::TextDocumentEdit, AnyError> {
|
2021-06-21 17:18:32 -04:00
|
|
|
let specifier = normalize_specifier(&self.file_name)?;
|
2021-02-06 07:39:01 -05:00
|
|
|
let line_index = language_server.get_line_index(specifier.clone()).await?;
|
2021-02-04 13:53:02 -05:00
|
|
|
let edits = self
|
|
|
|
.text_changes
|
|
|
|
.iter()
|
|
|
|
.map(|tc| tc.as_text_edit(&line_index))
|
|
|
|
.collect();
|
|
|
|
Ok(lsp::TextDocumentEdit {
|
|
|
|
text_document: lsp::OptionalVersionedTextDocumentIdentifier {
|
2021-02-17 13:47:18 -05:00
|
|
|
uri: specifier.clone(),
|
2021-02-06 07:39:01 -05:00
|
|
|
version: language_server.document_version(specifier),
|
2021-02-04 13:53:02 -05:00
|
|
|
},
|
|
|
|
edits,
|
|
|
|
})
|
|
|
|
}
|
2021-08-05 21:46:32 -04:00
|
|
|
|
|
|
|
pub(crate) async fn to_text_document_change_ops(
|
|
|
|
&self,
|
|
|
|
language_server: &mut language_server::Inner,
|
|
|
|
) -> Result<Vec<lsp::DocumentChangeOperation>, AnyError> {
|
|
|
|
let mut ops = Vec::<lsp::DocumentChangeOperation>::new();
|
|
|
|
let specifier = normalize_specifier(&self.file_name)?;
|
|
|
|
let line_index = if !self.is_new_file.unwrap_or(false) {
|
|
|
|
language_server.get_line_index(specifier.clone()).await?
|
|
|
|
} else {
|
|
|
|
LineIndex::new("")
|
|
|
|
};
|
|
|
|
|
|
|
|
if self.is_new_file.unwrap_or(false) {
|
|
|
|
ops.push(lsp::DocumentChangeOperation::Op(lsp::ResourceOp::Create(
|
|
|
|
lsp::CreateFile {
|
|
|
|
uri: specifier.clone(),
|
|
|
|
options: Some(lsp::CreateFileOptions {
|
|
|
|
ignore_if_exists: Some(true),
|
|
|
|
overwrite: None,
|
|
|
|
}),
|
|
|
|
annotation_id: None,
|
|
|
|
},
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
|
|
|
let edits = self
|
|
|
|
.text_changes
|
|
|
|
.iter()
|
|
|
|
.map(|tc| tc.as_text_edit(&line_index))
|
|
|
|
.collect();
|
|
|
|
ops.push(lsp::DocumentChangeOperation::Edit(lsp::TextDocumentEdit {
|
|
|
|
text_document: lsp::OptionalVersionedTextDocumentIdentifier {
|
|
|
|
uri: specifier.clone(),
|
|
|
|
version: language_server.document_version(specifier),
|
|
|
|
},
|
|
|
|
edits,
|
|
|
|
}));
|
|
|
|
|
|
|
|
Ok(ops)
|
|
|
|
}
|
2021-02-04 13:53:02 -05:00
|
|
|
}
|
|
|
|
|
2021-04-19 21:26:36 -04:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct Classifications {
|
|
|
|
spans: Vec<u32>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Classifications {
|
|
|
|
pub fn to_semantic_tokens(
|
|
|
|
&self,
|
|
|
|
line_index: &LineIndex,
|
|
|
|
) -> lsp::SemanticTokens {
|
|
|
|
let token_count = self.spans.len() / 3;
|
|
|
|
let mut builder = SemanticTokensBuilder::new();
|
|
|
|
for i in 0..token_count {
|
|
|
|
let src_offset = 3 * i;
|
|
|
|
let offset = self.spans[src_offset];
|
|
|
|
let length = self.spans[src_offset + 1];
|
|
|
|
let ts_classification = self.spans[src_offset + 2];
|
|
|
|
|
|
|
|
let token_type =
|
|
|
|
Classifications::get_token_type_from_classification(ts_classification);
|
|
|
|
let token_modifiers =
|
|
|
|
Classifications::get_token_modifier_from_classification(
|
|
|
|
ts_classification,
|
|
|
|
);
|
|
|
|
|
|
|
|
let start_pos = line_index.position_tsc(offset.into());
|
|
|
|
let end_pos = line_index.position_tsc(TextSize::from(offset + length));
|
|
|
|
|
|
|
|
// start_pos.line == end_pos.line is always true as there are no multiline tokens
|
|
|
|
builder.push(
|
|
|
|
start_pos.line,
|
|
|
|
start_pos.character,
|
|
|
|
end_pos.character - start_pos.character,
|
|
|
|
token_type,
|
|
|
|
token_modifiers,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
builder.build(None)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_token_type_from_classification(ts_classification: u32) -> u32 {
|
|
|
|
(ts_classification >> (TsTokenEncodingConsts::TypeOffset as u32)) - 1
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_token_modifier_from_classification(ts_classification: u32) -> u32 {
|
|
|
|
ts_classification & (TsTokenEncodingConsts::ModifierMask as u32)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-05 21:46:32 -04:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct RefactorActionInfo {
|
|
|
|
name: String,
|
|
|
|
description: String,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
not_applicable_reason: Option<String>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
kind: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RefactorActionInfo {
|
|
|
|
pub fn get_action_kind(&self) -> lsp::CodeActionKind {
|
|
|
|
if let Some(kind) = &self.kind {
|
|
|
|
kind.clone().into()
|
|
|
|
} else {
|
|
|
|
let maybe_match = ALL_KNOWN_REFACTOR_ACTION_KINDS
|
|
|
|
.iter()
|
|
|
|
.find(|action| action.matches(&self.name));
|
|
|
|
maybe_match
|
|
|
|
.map_or(lsp::CodeActionKind::REFACTOR, |action| action.kind.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_preferred(&self, all_actions: &[RefactorActionInfo]) -> bool {
|
|
|
|
if EXTRACT_CONSTANT.matches(&self.name) {
|
|
|
|
let get_scope = |name: &str| -> Option<u32> {
|
2021-09-15 22:07:52 -04:00
|
|
|
if let Some(captures) = SCOPE_RE.captures(name) {
|
2021-08-05 21:46:32 -04:00
|
|
|
captures[1].parse::<u32>().ok()
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return if let Some(scope) = get_scope(&self.name) {
|
|
|
|
all_actions
|
|
|
|
.iter()
|
|
|
|
.filter(|other| {
|
|
|
|
!std::ptr::eq(&self, other) && EXTRACT_CONSTANT.matches(&other.name)
|
|
|
|
})
|
|
|
|
.all(|other| {
|
|
|
|
if let Some(other_scope) = get_scope(&other.name) {
|
|
|
|
scope < other_scope
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if EXTRACT_TYPE.matches(&self.name) || EXTRACT_INTERFACE.matches(&self.name)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct ApplicableRefactorInfo {
|
|
|
|
name: String,
|
|
|
|
description: String,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
inlineable: Option<bool>,
|
|
|
|
actions: Vec<RefactorActionInfo>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ApplicableRefactorInfo {
|
|
|
|
pub fn to_code_actions(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
range: &lsp::Range,
|
|
|
|
) -> Vec<lsp::CodeAction> {
|
|
|
|
let mut code_actions = Vec::<lsp::CodeAction>::new();
|
|
|
|
// All typescript refactoring actions are inlineable
|
|
|
|
for action in self.actions.iter() {
|
|
|
|
code_actions
|
|
|
|
.push(self.as_inline_code_action(action, specifier, range, &self.name));
|
|
|
|
}
|
|
|
|
code_actions
|
|
|
|
}
|
|
|
|
|
|
|
|
fn as_inline_code_action(
|
|
|
|
&self,
|
|
|
|
action: &RefactorActionInfo,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
range: &lsp::Range,
|
|
|
|
refactor_name: &str,
|
|
|
|
) -> lsp::CodeAction {
|
|
|
|
let disabled = action.not_applicable_reason.as_ref().map(|reason| {
|
|
|
|
lsp::CodeActionDisabled {
|
|
|
|
reason: reason.clone(),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
lsp::CodeAction {
|
|
|
|
title: action.description.to_string(),
|
|
|
|
kind: Some(action.get_action_kind()),
|
|
|
|
is_preferred: Some(action.is_preferred(&self.actions)),
|
|
|
|
disabled,
|
|
|
|
data: Some(
|
|
|
|
serde_json::to_value(RefactorCodeActionData {
|
|
|
|
specifier: specifier.clone(),
|
|
|
|
range: *range,
|
|
|
|
refactor_name: refactor_name.to_owned(),
|
|
|
|
action_name: action.name.clone(),
|
|
|
|
})
|
|
|
|
.unwrap(),
|
|
|
|
),
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct RefactorEditInfo {
|
|
|
|
edits: Vec<FileTextChanges>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub rename_location: Option<u32>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RefactorEditInfo {
|
|
|
|
pub(crate) async fn to_workspace_edit(
|
|
|
|
&self,
|
|
|
|
language_server: &mut language_server::Inner,
|
|
|
|
) -> Result<Option<lsp::WorkspaceEdit>, AnyError> {
|
|
|
|
let mut all_ops = Vec::<lsp::DocumentChangeOperation>::new();
|
|
|
|
for edit in self.edits.iter() {
|
|
|
|
let ops = edit.to_text_document_change_ops(language_server).await?;
|
|
|
|
all_ops.extend(ops);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Some(lsp::WorkspaceEdit {
|
|
|
|
document_changes: Some(lsp::DocumentChanges::Operations(all_ops)),
|
|
|
|
..Default::default()
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 18:01:41 -04:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct CodeAction {
|
|
|
|
description: String,
|
|
|
|
changes: Vec<FileTextChanges>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
commands: Option<Vec<Value>>,
|
|
|
|
}
|
|
|
|
|
2021-02-04 13:53:02 -05:00
|
|
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct CodeFixAction {
|
|
|
|
pub description: String,
|
|
|
|
pub changes: Vec<FileTextChanges>,
|
|
|
|
// These are opaque types that should just be passed back when applying the
|
|
|
|
// action.
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub commands: Option<Vec<Value>>,
|
|
|
|
pub fix_name: String,
|
|
|
|
// It appears currently that all fixIds are strings, but the protocol
|
|
|
|
// specifies an opaque type, the problem is that we need to use the id as a
|
|
|
|
// hash key, and `Value` does not implement hash (and it could provide a false
|
|
|
|
// positive depending on JSON whitespace, so we deserialize it but it might
|
|
|
|
// break in the future)
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub fix_id: Option<String>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub fix_all_description: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct CombinedCodeActions {
|
|
|
|
pub changes: Vec<FileTextChanges>,
|
|
|
|
pub commands: Option<Vec<Value>>,
|
|
|
|
}
|
|
|
|
|
2020-12-07 05:46:39 -05:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct ReferenceEntry {
|
|
|
|
is_write_access: bool,
|
|
|
|
pub is_definition: bool,
|
|
|
|
is_in_string: Option<bool>,
|
2021-01-16 07:00:42 -05:00
|
|
|
#[serde(flatten)]
|
|
|
|
pub document_span: DocumentSpan,
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ReferenceEntry {
|
2021-02-17 23:37:05 -05:00
|
|
|
pub(crate) fn to_location(
|
|
|
|
&self,
|
|
|
|
line_index: &LineIndex,
|
|
|
|
language_server: &mut language_server::Inner,
|
|
|
|
) -> lsp::Location {
|
2021-07-25 01:33:42 -04:00
|
|
|
let specifier = normalize_specifier(&self.document_span.file_name)
|
|
|
|
.unwrap_or_else(|_| INVALID_SPECIFIER.clone());
|
2021-02-17 23:37:05 -05:00
|
|
|
let uri = language_server
|
|
|
|
.url_map
|
|
|
|
.normalize_specifier(&specifier)
|
2021-07-25 01:33:42 -04:00
|
|
|
.unwrap_or_else(|_| INVALID_SPECIFIER.clone());
|
2021-01-29 14:34:33 -05:00
|
|
|
lsp::Location {
|
2020-12-07 05:46:39 -05:00
|
|
|
uri,
|
2021-01-16 07:00:42 -05:00
|
|
|
range: self.document_span.text_span.to_range(line_index),
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-19 01:11:26 -04:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct CallHierarchyItem {
|
|
|
|
name: String,
|
|
|
|
kind: ScriptElementKind,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
kind_modifiers: Option<String>,
|
|
|
|
file: String,
|
|
|
|
span: TextSpan,
|
|
|
|
selection_span: TextSpan,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
container_name: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CallHierarchyItem {
|
|
|
|
pub(crate) async fn try_resolve_call_hierarchy_item(
|
|
|
|
&self,
|
|
|
|
language_server: &mut language_server::Inner,
|
|
|
|
maybe_root_path: Option<&Path>,
|
|
|
|
) -> Option<lsp::CallHierarchyItem> {
|
2021-07-25 01:33:42 -04:00
|
|
|
let target_specifier = normalize_specifier(&self.file).ok()?;
|
2021-04-19 01:11:26 -04:00
|
|
|
let target_line_index = language_server
|
|
|
|
.get_line_index(target_specifier)
|
|
|
|
.await
|
|
|
|
.ok()?;
|
|
|
|
|
|
|
|
Some(self.to_call_hierarchy_item(
|
|
|
|
&target_line_index,
|
|
|
|
language_server,
|
|
|
|
maybe_root_path,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn to_call_hierarchy_item(
|
|
|
|
&self,
|
|
|
|
line_index: &LineIndex,
|
|
|
|
language_server: &mut language_server::Inner,
|
|
|
|
maybe_root_path: Option<&Path>,
|
|
|
|
) -> lsp::CallHierarchyItem {
|
2021-07-25 01:33:42 -04:00
|
|
|
let target_specifier = normalize_specifier(&self.file)
|
|
|
|
.unwrap_or_else(|_| INVALID_SPECIFIER.clone());
|
2021-04-19 01:11:26 -04:00
|
|
|
let uri = language_server
|
|
|
|
.url_map
|
|
|
|
.normalize_specifier(&target_specifier)
|
2021-07-25 01:33:42 -04:00
|
|
|
.unwrap_or_else(|_| INVALID_SPECIFIER.clone());
|
2021-04-19 01:11:26 -04:00
|
|
|
|
|
|
|
let use_file_name = self.is_source_file_item();
|
|
|
|
let maybe_file_path = if uri.scheme() == "file" {
|
|
|
|
uri.to_file_path().ok()
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
let name = if use_file_name {
|
|
|
|
if let Some(file_path) = maybe_file_path.as_ref() {
|
|
|
|
file_path.file_name().unwrap().to_string_lossy().to_string()
|
|
|
|
} else {
|
|
|
|
uri.to_string()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.name.clone()
|
|
|
|
};
|
|
|
|
let detail = if use_file_name {
|
|
|
|
if let Some(file_path) = maybe_file_path.as_ref() {
|
|
|
|
// TODO: update this to work with multi root workspaces
|
|
|
|
let parent_dir = file_path.parent().unwrap();
|
|
|
|
if let Some(root_path) = maybe_root_path {
|
|
|
|
parent_dir
|
|
|
|
.strip_prefix(root_path)
|
|
|
|
.unwrap_or(parent_dir)
|
|
|
|
.to_string_lossy()
|
|
|
|
.to_string()
|
|
|
|
} else {
|
|
|
|
parent_dir.to_string_lossy().to_string()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
String::new()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.container_name.as_ref().cloned().unwrap_or_default()
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut tags: Option<Vec<lsp::SymbolTag>> = None;
|
|
|
|
if let Some(modifiers) = self.kind_modifiers.as_ref() {
|
|
|
|
let kind_modifiers = parse_kind_modifier(modifiers);
|
|
|
|
if kind_modifiers.contains("deprecated") {
|
|
|
|
tags = Some(vec![lsp::SymbolTag::Deprecated]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lsp::CallHierarchyItem {
|
|
|
|
name,
|
|
|
|
tags,
|
|
|
|
uri,
|
|
|
|
detail: Some(detail),
|
|
|
|
kind: self.kind.clone().into(),
|
|
|
|
range: self.span.to_range(line_index),
|
|
|
|
selection_range: self.selection_span.to_range(line_index),
|
|
|
|
data: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_source_file_item(&self) -> bool {
|
|
|
|
self.kind == ScriptElementKind::ScriptElement
|
|
|
|
|| self.kind == ScriptElementKind::ModuleElement
|
|
|
|
&& self.selection_span.start == 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct CallHierarchyIncomingCall {
|
|
|
|
from: CallHierarchyItem,
|
|
|
|
from_spans: Vec<TextSpan>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CallHierarchyIncomingCall {
|
|
|
|
pub(crate) async fn try_resolve_call_hierarchy_incoming_call(
|
|
|
|
&self,
|
|
|
|
language_server: &mut language_server::Inner,
|
|
|
|
maybe_root_path: Option<&Path>,
|
|
|
|
) -> Option<lsp::CallHierarchyIncomingCall> {
|
2021-07-25 01:33:42 -04:00
|
|
|
let target_specifier = normalize_specifier(&self.from.file).ok()?;
|
2021-04-19 01:11:26 -04:00
|
|
|
let target_line_index = language_server
|
|
|
|
.get_line_index(target_specifier)
|
|
|
|
.await
|
|
|
|
.ok()?;
|
|
|
|
|
|
|
|
Some(lsp::CallHierarchyIncomingCall {
|
|
|
|
from: self.from.to_call_hierarchy_item(
|
|
|
|
&target_line_index,
|
|
|
|
language_server,
|
|
|
|
maybe_root_path,
|
|
|
|
),
|
|
|
|
from_ranges: self
|
|
|
|
.from_spans
|
|
|
|
.iter()
|
|
|
|
.map(|span| span.to_range(&target_line_index))
|
|
|
|
.collect(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct CallHierarchyOutgoingCall {
|
|
|
|
to: CallHierarchyItem,
|
|
|
|
from_spans: Vec<TextSpan>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CallHierarchyOutgoingCall {
|
|
|
|
pub(crate) async fn try_resolve_call_hierarchy_outgoing_call(
|
|
|
|
&self,
|
|
|
|
line_index: &LineIndex,
|
|
|
|
language_server: &mut language_server::Inner,
|
|
|
|
maybe_root_path: Option<&Path>,
|
|
|
|
) -> Option<lsp::CallHierarchyOutgoingCall> {
|
2021-07-25 01:33:42 -04:00
|
|
|
let target_specifier = normalize_specifier(&self.to.file).ok()?;
|
2021-04-19 01:11:26 -04:00
|
|
|
let target_line_index = language_server
|
|
|
|
.get_line_index(target_specifier)
|
|
|
|
.await
|
|
|
|
.ok()?;
|
|
|
|
|
|
|
|
Some(lsp::CallHierarchyOutgoingCall {
|
|
|
|
to: self.to.to_call_hierarchy_item(
|
|
|
|
&target_line_index,
|
|
|
|
language_server,
|
|
|
|
maybe_root_path,
|
|
|
|
),
|
|
|
|
from_ranges: self
|
|
|
|
.from_spans
|
|
|
|
.iter()
|
2021-07-30 09:03:41 -04:00
|
|
|
.map(|span| span.to_range(line_index))
|
2021-04-19 01:11:26 -04:00
|
|
|
.collect(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-08 05:36:13 -05:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-03-15 18:01:41 -04:00
|
|
|
pub struct CompletionEntryDetails {
|
|
|
|
name: String,
|
|
|
|
kind: ScriptElementKind,
|
|
|
|
kind_modifiers: String,
|
|
|
|
display_parts: Vec<SymbolDisplayPart>,
|
|
|
|
documentation: Option<Vec<SymbolDisplayPart>>,
|
2021-03-25 14:17:37 -04:00
|
|
|
tags: Option<Vec<JsDocTagInfo>>,
|
2021-03-15 18:01:41 -04:00
|
|
|
code_actions: Option<Vec<CodeAction>>,
|
|
|
|
source: Option<Vec<SymbolDisplayPart>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CompletionEntryDetails {
|
|
|
|
pub fn as_completion_item(
|
|
|
|
&self,
|
|
|
|
original_item: &lsp::CompletionItem,
|
|
|
|
) -> lsp::CompletionItem {
|
|
|
|
let detail = if original_item.detail.is_some() {
|
|
|
|
original_item.detail.clone()
|
|
|
|
} else if !self.display_parts.is_empty() {
|
|
|
|
Some(replace_links(&display_parts_to_string(&self.display_parts)))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
let documentation = if let Some(parts) = &self.documentation {
|
|
|
|
let mut value = display_parts_to_string(parts);
|
|
|
|
if let Some(tags) = &self.tags {
|
|
|
|
let tag_documentation = tags
|
|
|
|
.iter()
|
|
|
|
.map(get_tag_documentation)
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join("");
|
|
|
|
value = format!("{}\n\n{}", value, tag_documentation);
|
|
|
|
}
|
|
|
|
Some(lsp::Documentation::MarkupContent(lsp::MarkupContent {
|
|
|
|
kind: lsp::MarkupKind::Markdown,
|
|
|
|
value,
|
|
|
|
}))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
// TODO(@kitsonk) add `self.code_actions`
|
|
|
|
// TODO(@kitsonk) add `use_code_snippet`
|
|
|
|
|
|
|
|
lsp::CompletionItem {
|
|
|
|
data: None,
|
|
|
|
detail,
|
|
|
|
documentation,
|
|
|
|
..original_item.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2020-12-08 05:36:13 -05:00
|
|
|
pub struct CompletionInfo {
|
|
|
|
entries: Vec<CompletionEntry>,
|
2021-03-15 18:01:41 -04:00
|
|
|
is_global_completion: bool,
|
2020-12-08 05:36:13 -05:00
|
|
|
is_member_completion: bool,
|
2021-03-15 18:01:41 -04:00
|
|
|
is_new_identifier_location: bool,
|
|
|
|
metadata: Option<Value>,
|
|
|
|
optional_replacement_span: Option<TextSpan>,
|
2020-12-08 05:36:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CompletionInfo {
|
2021-03-15 18:01:41 -04:00
|
|
|
pub fn as_completion_response(
|
|
|
|
&self,
|
2021-01-22 05:03:16 -05:00
|
|
|
line_index: &LineIndex,
|
2021-03-15 18:01:41 -04:00
|
|
|
settings: &config::CompletionSettings,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
position: u32,
|
2021-01-29 14:34:33 -05:00
|
|
|
) -> lsp::CompletionResponse {
|
2020-12-08 05:36:13 -05:00
|
|
|
let items = self
|
|
|
|
.entries
|
2021-03-15 18:01:41 -04:00
|
|
|
.iter()
|
|
|
|
.map(|entry| {
|
|
|
|
entry
|
|
|
|
.as_completion_item(line_index, self, settings, specifier, position)
|
|
|
|
})
|
2020-12-08 05:36:13 -05:00
|
|
|
.collect();
|
2021-03-15 18:01:41 -04:00
|
|
|
let is_incomplete = self
|
|
|
|
.metadata
|
|
|
|
.clone()
|
|
|
|
.map(|v| {
|
|
|
|
v.as_object()
|
|
|
|
.unwrap()
|
|
|
|
.get("isIncomplete")
|
|
|
|
.unwrap_or(&json!(false))
|
|
|
|
.as_bool()
|
|
|
|
.unwrap()
|
|
|
|
})
|
|
|
|
.unwrap_or(false);
|
|
|
|
lsp::CompletionResponse::List(lsp::CompletionList {
|
|
|
|
is_incomplete,
|
|
|
|
items,
|
|
|
|
})
|
2020-12-08 05:36:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 18:01:41 -04:00
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct CompletionItemData {
|
|
|
|
pub specifier: ModuleSpecifier,
|
|
|
|
pub position: u32,
|
|
|
|
pub name: String,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub source: Option<String>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub data: Option<Value>,
|
|
|
|
pub use_code_snippet: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Default, Deserialize, Serialize)]
|
2020-12-08 05:36:13 -05:00
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct CompletionEntry {
|
2021-03-15 18:01:41 -04:00
|
|
|
name: String,
|
2020-12-08 05:36:13 -05:00
|
|
|
kind: ScriptElementKind,
|
2021-03-15 18:01:41 -04:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2020-12-08 05:36:13 -05:00
|
|
|
kind_modifiers: Option<String>,
|
|
|
|
sort_text: String,
|
2021-03-15 18:01:41 -04:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2020-12-08 05:36:13 -05:00
|
|
|
insert_text: Option<String>,
|
2021-03-15 18:01:41 -04:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2020-12-08 05:36:13 -05:00
|
|
|
replacement_span: Option<TextSpan>,
|
2021-03-15 18:01:41 -04:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2020-12-08 05:36:13 -05:00
|
|
|
has_action: Option<bool>,
|
2021-03-15 18:01:41 -04:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2020-12-08 05:36:13 -05:00
|
|
|
source: Option<String>,
|
2021-03-15 18:01:41 -04:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2020-12-08 05:36:13 -05:00
|
|
|
is_recommended: Option<bool>,
|
2021-03-15 18:01:41 -04:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
is_from_unchecked_file: Option<bool>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
data: Option<Value>,
|
2020-12-08 05:36:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CompletionEntry {
|
2021-03-15 18:01:41 -04:00
|
|
|
fn get_commit_characters(
|
|
|
|
&self,
|
|
|
|
info: &CompletionInfo,
|
|
|
|
settings: &config::CompletionSettings,
|
|
|
|
) -> Option<Vec<String>> {
|
|
|
|
if info.is_new_identifier_location {
|
|
|
|
return None;
|
|
|
|
}
|
2020-12-08 05:36:13 -05:00
|
|
|
|
2021-03-15 18:01:41 -04:00
|
|
|
let mut commit_characters = vec![];
|
|
|
|
match self.kind {
|
|
|
|
ScriptElementKind::MemberGetAccessorElement
|
|
|
|
| ScriptElementKind::MemberSetAccessorElement
|
|
|
|
| ScriptElementKind::ConstructSignatureElement
|
|
|
|
| ScriptElementKind::CallSignatureElement
|
|
|
|
| ScriptElementKind::IndexSignatureElement
|
|
|
|
| ScriptElementKind::EnumElement
|
|
|
|
| ScriptElementKind::InterfaceElement => {
|
|
|
|
commit_characters.push(".");
|
|
|
|
commit_characters.push(";");
|
|
|
|
}
|
|
|
|
ScriptElementKind::ModuleElement
|
|
|
|
| ScriptElementKind::Alias
|
|
|
|
| ScriptElementKind::ConstElement
|
|
|
|
| ScriptElementKind::LetElement
|
|
|
|
| ScriptElementKind::VariableElement
|
|
|
|
| ScriptElementKind::LocalVariableElement
|
|
|
|
| ScriptElementKind::MemberVariableElement
|
|
|
|
| ScriptElementKind::ClassElement
|
|
|
|
| ScriptElementKind::FunctionElement
|
|
|
|
| ScriptElementKind::MemberFunctionElement
|
|
|
|
| ScriptElementKind::Keyword
|
|
|
|
| ScriptElementKind::ParameterElement => {
|
|
|
|
commit_characters.push(".");
|
|
|
|
commit_characters.push(",");
|
|
|
|
commit_characters.push(";");
|
|
|
|
if !settings.complete_function_calls {
|
|
|
|
commit_characters.push("(");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (),
|
2020-12-08 05:36:13 -05:00
|
|
|
}
|
|
|
|
|
2021-03-15 18:01:41 -04:00
|
|
|
if commit_characters.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(commit_characters.into_iter().map(String::from).collect())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_filter_text(&self) -> Option<String> {
|
2021-09-15 22:07:52 -04:00
|
|
|
if self.name.starts_with('#') {
|
|
|
|
if let Some(insert_text) = &self.insert_text {
|
|
|
|
if insert_text.starts_with("this.#") {
|
|
|
|
return Some(insert_text.replace("this.#", ""));
|
|
|
|
} else {
|
|
|
|
return Some(insert_text.clone());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Some(self.name.replace("#", ""));
|
|
|
|
}
|
2021-03-15 18:01:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(insert_text) = &self.insert_text {
|
|
|
|
if insert_text.starts_with("this.") {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
if insert_text.starts_with('[') {
|
2021-09-15 22:07:52 -04:00
|
|
|
return Some(
|
|
|
|
BRACKET_ACCESSOR_RE
|
|
|
|
.replace(insert_text, |caps: &Captures| format!(".{}", &caps[1]))
|
|
|
|
.to_string(),
|
|
|
|
);
|
2020-12-08 05:36:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 18:01:41 -04:00
|
|
|
self.insert_text.clone()
|
|
|
|
}
|
2020-12-08 05:36:13 -05:00
|
|
|
|
2021-03-15 18:01:41 -04:00
|
|
|
pub fn as_completion_item(
|
|
|
|
&self,
|
|
|
|
line_index: &LineIndex,
|
|
|
|
info: &CompletionInfo,
|
|
|
|
settings: &config::CompletionSettings,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
position: u32,
|
|
|
|
) -> lsp::CompletionItem {
|
|
|
|
let mut label = self.name.clone();
|
|
|
|
let mut kind: Option<lsp::CompletionItemKind> =
|
|
|
|
Some(self.kind.clone().into());
|
2020-12-08 05:36:13 -05:00
|
|
|
|
2021-03-15 18:01:41 -04:00
|
|
|
let sort_text = if self.source.is_some() {
|
|
|
|
Some(format!("\u{ffff}{}", self.sort_text))
|
|
|
|
} else {
|
|
|
|
Some(self.sort_text.clone())
|
|
|
|
};
|
|
|
|
|
|
|
|
let preselect = self.is_recommended;
|
|
|
|
let use_code_snippet = settings.complete_function_calls
|
|
|
|
&& (kind == Some(lsp::CompletionItemKind::Function)
|
|
|
|
|| kind == Some(lsp::CompletionItemKind::Method));
|
|
|
|
// TODO(@kitsonk) missing from types: https://github.com/gluon-lang/lsp-types/issues/204
|
|
|
|
let _commit_characters = self.get_commit_characters(info, settings);
|
|
|
|
let mut insert_text = self.insert_text.clone();
|
|
|
|
let range = self.replacement_span.clone();
|
|
|
|
let mut filter_text = self.get_filter_text();
|
|
|
|
let mut tags = None;
|
|
|
|
let mut detail = None;
|
|
|
|
|
|
|
|
if let Some(kind_modifiers) = &self.kind_modifiers {
|
|
|
|
let kind_modifiers = parse_kind_modifier(kind_modifiers);
|
|
|
|
if kind_modifiers.contains("optional") {
|
2020-12-08 05:36:13 -05:00
|
|
|
if insert_text.is_none() {
|
2021-03-15 18:01:41 -04:00
|
|
|
insert_text = Some(label.clone());
|
2020-12-08 05:36:13 -05:00
|
|
|
}
|
2021-03-15 18:01:41 -04:00
|
|
|
if filter_text.is_none() {
|
|
|
|
filter_text = Some(label.clone());
|
|
|
|
}
|
|
|
|
label += "?";
|
|
|
|
}
|
|
|
|
if kind_modifiers.contains("deprecated") {
|
|
|
|
tags = Some(vec![lsp::CompletionItemTag::Deprecated]);
|
|
|
|
}
|
|
|
|
if kind_modifiers.contains("color") {
|
|
|
|
kind = Some(lsp::CompletionItemKind::Color);
|
|
|
|
}
|
|
|
|
if self.kind == ScriptElementKind::ScriptElement {
|
|
|
|
for ext_modifier in FILE_EXTENSION_KIND_MODIFIERS {
|
|
|
|
if kind_modifiers.contains(ext_modifier) {
|
|
|
|
detail = if self.name.to_lowercase().ends_with(ext_modifier) {
|
|
|
|
Some(self.name.clone())
|
|
|
|
} else {
|
|
|
|
Some(format!("{}{}", self.name, ext_modifier))
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
}
|
2020-12-08 05:36:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 18:01:41 -04:00
|
|
|
let text_edit =
|
2021-04-01 05:18:51 -04:00
|
|
|
if let (Some(text_span), Some(new_text)) = (range, &insert_text) {
|
2021-03-15 18:01:41 -04:00
|
|
|
let range = text_span.to_range(line_index);
|
|
|
|
let insert_replace_edit = lsp::InsertReplaceEdit {
|
2021-04-01 05:18:51 -04:00
|
|
|
new_text: new_text.clone(),
|
2021-03-15 18:01:41 -04:00
|
|
|
insert: range,
|
|
|
|
replace: range,
|
|
|
|
};
|
|
|
|
Some(insert_replace_edit.into())
|
2020-12-08 05:36:13 -05:00
|
|
|
} else {
|
2021-03-15 18:01:41 -04:00
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2021-03-24 20:13:37 -04:00
|
|
|
let tsc = CompletionItemData {
|
2021-03-15 18:01:41 -04:00
|
|
|
specifier: specifier.clone(),
|
|
|
|
position,
|
|
|
|
name: self.name.clone(),
|
|
|
|
source: self.source.clone(),
|
|
|
|
data: self.data.clone(),
|
|
|
|
use_code_snippet,
|
|
|
|
};
|
2020-12-08 05:36:13 -05:00
|
|
|
|
2021-03-15 18:01:41 -04:00
|
|
|
lsp::CompletionItem {
|
|
|
|
label,
|
|
|
|
kind,
|
|
|
|
sort_text,
|
|
|
|
preselect,
|
|
|
|
text_edit,
|
|
|
|
filter_text,
|
2021-04-01 05:18:51 -04:00
|
|
|
insert_text,
|
2021-03-15 18:01:41 -04:00
|
|
|
detail,
|
|
|
|
tags,
|
2021-03-24 20:13:37 -04:00
|
|
|
data: Some(json!({
|
|
|
|
"tsc": tsc,
|
|
|
|
})),
|
2021-03-15 18:01:41 -04:00
|
|
|
..Default::default()
|
|
|
|
}
|
2020-12-08 05:36:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-02 02:21:07 -04:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
pub enum OutliningSpanKind {
|
|
|
|
#[serde(rename = "comment")]
|
|
|
|
Comment,
|
|
|
|
#[serde(rename = "region")]
|
|
|
|
Region,
|
|
|
|
#[serde(rename = "code")]
|
|
|
|
Code,
|
|
|
|
#[serde(rename = "imports")]
|
|
|
|
Imports,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct OutliningSpan {
|
|
|
|
text_span: TextSpan,
|
|
|
|
hint_span: TextSpan,
|
|
|
|
banner_text: String,
|
|
|
|
auto_collapse: bool,
|
|
|
|
kind: OutliningSpanKind,
|
|
|
|
}
|
|
|
|
|
|
|
|
const FOLD_END_PAIR_CHARACTERS: &[u8] = &[b'}', b']', b')', b'`'];
|
|
|
|
|
|
|
|
impl OutliningSpan {
|
|
|
|
pub fn to_folding_range(
|
|
|
|
&self,
|
|
|
|
line_index: &LineIndex,
|
|
|
|
content: &[u8],
|
|
|
|
line_folding_only: bool,
|
|
|
|
) -> lsp::FoldingRange {
|
|
|
|
let range = self.text_span.to_range(line_index);
|
|
|
|
lsp::FoldingRange {
|
|
|
|
start_line: range.start.line,
|
|
|
|
start_character: if line_folding_only {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(range.start.character)
|
|
|
|
},
|
|
|
|
end_line: self.adjust_folding_end_line(
|
|
|
|
&range,
|
|
|
|
line_index,
|
|
|
|
content,
|
|
|
|
line_folding_only,
|
|
|
|
),
|
|
|
|
end_character: if line_folding_only {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(range.end.character)
|
|
|
|
},
|
|
|
|
kind: self.get_folding_range_kind(&self.kind),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn adjust_folding_end_line(
|
|
|
|
&self,
|
|
|
|
range: &lsp::Range,
|
|
|
|
line_index: &LineIndex,
|
|
|
|
content: &[u8],
|
|
|
|
line_folding_only: bool,
|
|
|
|
) -> u32 {
|
2021-04-05 23:27:27 -04:00
|
|
|
if line_folding_only && range.end.line > 0 && range.end.character > 0 {
|
2021-04-02 02:21:07 -04:00
|
|
|
let offset_end: usize = line_index.offset(range.end).unwrap().into();
|
|
|
|
let fold_end_char = content[offset_end - 1];
|
|
|
|
if FOLD_END_PAIR_CHARACTERS.contains(&fold_end_char) {
|
|
|
|
return cmp::max(range.end.line - 1, range.start.line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
range.end.line
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_folding_range_kind(
|
|
|
|
&self,
|
|
|
|
span_kind: &OutliningSpanKind,
|
|
|
|
) -> Option<lsp::FoldingRangeKind> {
|
|
|
|
match span_kind {
|
|
|
|
OutliningSpanKind::Comment => Some(lsp::FoldingRangeKind::Comment),
|
|
|
|
OutliningSpanKind::Region => Some(lsp::FoldingRangeKind::Region),
|
|
|
|
OutliningSpanKind::Imports => Some(lsp::FoldingRangeKind::Imports),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-15 21:34:09 -05:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct SignatureHelpItems {
|
|
|
|
items: Vec<SignatureHelpItem>,
|
|
|
|
applicable_span: TextSpan,
|
|
|
|
selected_item_index: u32,
|
|
|
|
argument_index: u32,
|
|
|
|
argument_count: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SignatureHelpItems {
|
|
|
|
pub fn into_signature_help(self) -> lsp::SignatureHelp {
|
|
|
|
lsp::SignatureHelp {
|
|
|
|
signatures: self
|
|
|
|
.items
|
|
|
|
.into_iter()
|
|
|
|
.map(|item| item.into_signature_information())
|
|
|
|
.collect(),
|
|
|
|
active_parameter: Some(self.argument_index),
|
|
|
|
active_signature: Some(self.selected_item_index),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct SignatureHelpItem {
|
|
|
|
is_variadic: bool,
|
|
|
|
prefix_display_parts: Vec<SymbolDisplayPart>,
|
|
|
|
suffix_display_parts: Vec<SymbolDisplayPart>,
|
|
|
|
separator_display_parts: Vec<SymbolDisplayPart>,
|
|
|
|
parameters: Vec<SignatureHelpParameter>,
|
|
|
|
documentation: Vec<SymbolDisplayPart>,
|
2021-03-25 14:17:37 -04:00
|
|
|
tags: Vec<JsDocTagInfo>,
|
2021-02-15 21:34:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SignatureHelpItem {
|
|
|
|
pub fn into_signature_information(self) -> lsp::SignatureInformation {
|
2021-03-15 18:01:41 -04:00
|
|
|
let prefix_text = display_parts_to_string(&self.prefix_display_parts);
|
2021-02-15 21:34:09 -05:00
|
|
|
let params_text = self
|
|
|
|
.parameters
|
|
|
|
.iter()
|
2021-03-15 18:01:41 -04:00
|
|
|
.map(|param| display_parts_to_string(¶m.display_parts))
|
2021-02-15 21:34:09 -05:00
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join(", ");
|
2021-03-15 18:01:41 -04:00
|
|
|
let suffix_text = display_parts_to_string(&self.suffix_display_parts);
|
2021-02-15 21:34:09 -05:00
|
|
|
lsp::SignatureInformation {
|
|
|
|
label: format!("{}{}{}", prefix_text, params_text, suffix_text),
|
|
|
|
documentation: Some(lsp::Documentation::String(display_parts_to_string(
|
2021-03-15 18:01:41 -04:00
|
|
|
&self.documentation,
|
2021-02-15 21:34:09 -05:00
|
|
|
))),
|
|
|
|
parameters: Some(
|
|
|
|
self
|
|
|
|
.parameters
|
|
|
|
.into_iter()
|
|
|
|
.map(|param| param.into_parameter_information())
|
|
|
|
.collect(),
|
|
|
|
),
|
|
|
|
active_parameter: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct SignatureHelpParameter {
|
|
|
|
name: String,
|
|
|
|
documentation: Vec<SymbolDisplayPart>,
|
|
|
|
display_parts: Vec<SymbolDisplayPart>,
|
|
|
|
is_optional: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SignatureHelpParameter {
|
|
|
|
pub fn into_parameter_information(self) -> lsp::ParameterInformation {
|
|
|
|
lsp::ParameterInformation {
|
|
|
|
label: lsp::ParameterLabel::Simple(display_parts_to_string(
|
2021-03-15 18:01:41 -04:00
|
|
|
&self.display_parts,
|
2021-02-15 21:34:09 -05:00
|
|
|
)),
|
|
|
|
documentation: Some(lsp::Documentation::String(display_parts_to_string(
|
2021-03-15 18:01:41 -04:00
|
|
|
&self.documentation,
|
2021-02-15 21:34:09 -05:00
|
|
|
))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-23 19:33:25 -04:00
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct SelectionRange {
|
|
|
|
text_span: TextSpan,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
parent: Option<Box<SelectionRange>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SelectionRange {
|
|
|
|
pub fn to_selection_range(
|
|
|
|
&self,
|
|
|
|
line_index: &LineIndex,
|
|
|
|
) -> lsp::SelectionRange {
|
|
|
|
lsp::SelectionRange {
|
|
|
|
range: self.text_span.to_range(line_index),
|
2021-03-25 14:17:37 -04:00
|
|
|
parent: self.parent.as_ref().map(|parent_selection| {
|
|
|
|
Box::new(parent_selection.to_selection_range(line_index))
|
|
|
|
}),
|
2021-03-23 19:33:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-07 05:46:39 -05:00
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
|
|
struct Response {
|
|
|
|
id: usize,
|
|
|
|
data: Value,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct State<'a> {
|
|
|
|
last_id: usize,
|
|
|
|
response: Option<Response>,
|
2020-12-21 08:44:26 -05:00
|
|
|
state_snapshot: StateSnapshot,
|
2021-02-28 19:53:20 -05:00
|
|
|
snapshots: HashMap<(ModuleSpecifier, Cow<'a, str>), String>,
|
2021-06-21 17:18:32 -04:00
|
|
|
specifiers: HashMap<String, String>,
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> State<'a> {
|
2020-12-21 08:44:26 -05:00
|
|
|
fn new(state_snapshot: StateSnapshot) -> Self {
|
2020-12-07 05:46:39 -05:00
|
|
|
Self {
|
|
|
|
last_id: 1,
|
|
|
|
response: None,
|
2020-12-21 08:44:26 -05:00
|
|
|
state_snapshot,
|
2021-03-10 05:39:16 -05:00
|
|
|
snapshots: HashMap::default(),
|
2021-06-21 17:18:32 -04:00
|
|
|
specifiers: HashMap::default(),
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
}
|
2021-06-21 17:18:32 -04:00
|
|
|
|
|
|
|
/// If a normalized version of the specifier has been stored for tsc, this
|
|
|
|
/// will "restore" it for communicating back to the tsc language server,
|
|
|
|
/// otherwise it will just convert the specifier to a string.
|
|
|
|
fn denormalize_specifier(&self, specifier: &ModuleSpecifier) -> String {
|
|
|
|
let specifier_str = specifier.to_string();
|
|
|
|
self
|
|
|
|
.specifiers
|
|
|
|
.get(&specifier_str)
|
|
|
|
.unwrap_or(&specifier_str)
|
|
|
|
.to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// In certain situations, tsc can request "invalid" specifiers and this will
|
|
|
|
/// normalize and memoize the specifier.
|
|
|
|
fn normalize_specifier<S: AsRef<str>>(
|
|
|
|
&mut self,
|
|
|
|
specifier: S,
|
|
|
|
) -> Result<ModuleSpecifier, AnyError> {
|
|
|
|
let specifier_str = specifier.as_ref().replace(".d.ts.d.ts", ".d.ts");
|
|
|
|
if specifier_str != specifier.as_ref() {
|
|
|
|
self
|
|
|
|
.specifiers
|
|
|
|
.insert(specifier_str.clone(), specifier.as_ref().to_string());
|
|
|
|
}
|
|
|
|
ModuleSpecifier::parse(&specifier_str).map_err(|err| err.into())
|
|
|
|
}
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// If a snapshot is missing from the state cache, add it.
|
|
|
|
fn cache_snapshot(
|
|
|
|
state: &mut State,
|
2021-02-28 19:53:20 -05:00
|
|
|
specifier: &ModuleSpecifier,
|
2020-12-07 05:46:39 -05:00
|
|
|
version: String,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
if !state
|
|
|
|
.snapshots
|
2021-02-28 19:53:20 -05:00
|
|
|
.contains_key(&(specifier.clone(), version.clone().into()))
|
2020-12-07 05:46:39 -05:00
|
|
|
{
|
2021-03-10 05:39:16 -05:00
|
|
|
let content = if state.state_snapshot.documents.contains_key(specifier) {
|
|
|
|
state
|
|
|
|
.state_snapshot
|
|
|
|
.documents
|
2021-09-07 10:39:32 -04:00
|
|
|
.content(specifier)
|
2021-03-10 05:39:16 -05:00
|
|
|
.ok_or_else(|| {
|
|
|
|
anyhow!("Specifier unexpectedly doesn't have content: {}", specifier)
|
|
|
|
})?
|
|
|
|
} else {
|
|
|
|
state.state_snapshot.sources.get_source(specifier).ok_or_else(|| {
|
|
|
|
anyhow!("Specifier (\"{}\") is not an in memory document or on disk resource.", specifier)
|
|
|
|
})?
|
|
|
|
};
|
2020-12-07 05:46:39 -05:00
|
|
|
state
|
|
|
|
.snapshots
|
2021-09-07 10:39:32 -04:00
|
|
|
.insert((specifier.clone(), version.into()), content.to_string());
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-06-21 17:18:32 -04:00
|
|
|
fn normalize_specifier<S: AsRef<str>>(
|
|
|
|
specifier: S,
|
|
|
|
) -> Result<ModuleSpecifier, AnyError> {
|
|
|
|
resolve_url(specifier.as_ref().replace(".d.ts.d.ts", ".d.ts").as_str())
|
|
|
|
.map_err(|err| err.into())
|
|
|
|
}
|
|
|
|
|
2021-03-31 10:37:38 -04:00
|
|
|
// buffer-less json_sync ops
|
2021-02-24 22:15:55 -05:00
|
|
|
fn op<F, V, R>(op_fn: F) -> Box<OpFn>
|
2020-12-07 05:46:39 -05:00
|
|
|
where
|
2021-02-24 22:15:55 -05:00
|
|
|
F: Fn(&mut State, V) -> Result<R, AnyError> + 'static,
|
|
|
|
V: de::DeserializeOwned,
|
2021-03-31 10:37:38 -04:00
|
|
|
R: Serialize + 'static,
|
2020-12-07 05:46:39 -05:00
|
|
|
{
|
2021-05-06 13:32:03 -04:00
|
|
|
op_sync(move |s, args, _: ()| {
|
2020-12-07 05:46:39 -05:00
|
|
|
let state = s.borrow_mut::<State>();
|
|
|
|
op_fn(state, args)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-05-11 00:54:10 -04:00
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
2020-12-07 05:46:39 -05:00
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct SourceSnapshotArgs {
|
|
|
|
specifier: String,
|
|
|
|
version: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The language service is dropping a reference to a source file snapshot, and
|
|
|
|
/// we can drop our version of that document.
|
2021-05-02 08:29:19 -04:00
|
|
|
fn op_dispose(
|
2021-02-24 22:15:55 -05:00
|
|
|
state: &mut State,
|
|
|
|
args: SourceSnapshotArgs,
|
|
|
|
) -> Result<bool, AnyError> {
|
2021-05-11 00:54:10 -04:00
|
|
|
let mark = state
|
|
|
|
.state_snapshot
|
|
|
|
.performance
|
|
|
|
.mark("op_dispose", Some(&args));
|
2021-06-21 17:18:32 -04:00
|
|
|
let specifier = state.normalize_specifier(&args.specifier)?;
|
2021-02-28 19:53:20 -05:00
|
|
|
state.snapshots.remove(&(specifier, args.version.into()));
|
2021-02-11 23:17:48 -05:00
|
|
|
state.state_snapshot.performance.measure(mark);
|
2021-02-24 22:15:55 -05:00
|
|
|
Ok(true)
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
2021-06-21 17:18:32 -04:00
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct SpecifierArgs {
|
|
|
|
specifier: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn op_exists(state: &mut State, args: SpecifierArgs) -> Result<bool, AnyError> {
|
|
|
|
let mark = state
|
|
|
|
.state_snapshot
|
|
|
|
.performance
|
|
|
|
.mark("op_exists", Some(&args));
|
|
|
|
let specifier = state.normalize_specifier(args.specifier)?;
|
|
|
|
let result = state.state_snapshot.sources.contains_key(&specifier);
|
|
|
|
state.state_snapshot.performance.measure(mark);
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
|
2021-05-11 00:54:10 -04:00
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
2020-12-07 05:46:39 -05:00
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct GetChangeRangeArgs {
|
|
|
|
specifier: String,
|
|
|
|
old_length: u32,
|
|
|
|
old_version: String,
|
|
|
|
version: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The language service wants to compare an old snapshot with a new snapshot to
|
2021-03-10 05:39:16 -05:00
|
|
|
/// determine what source has changed.
|
2021-05-02 08:29:19 -04:00
|
|
|
fn op_get_change_range(
|
2021-02-24 22:15:55 -05:00
|
|
|
state: &mut State,
|
|
|
|
args: GetChangeRangeArgs,
|
|
|
|
) -> Result<Value, AnyError> {
|
2021-05-11 00:54:10 -04:00
|
|
|
let mark = state
|
|
|
|
.state_snapshot
|
|
|
|
.performance
|
|
|
|
.mark("op_get_change_range", Some(&args));
|
2021-06-21 17:18:32 -04:00
|
|
|
let specifier = state.normalize_specifier(&args.specifier)?;
|
2021-03-10 05:39:16 -05:00
|
|
|
cache_snapshot(state, &specifier, args.version.clone())?;
|
2021-05-11 00:54:10 -04:00
|
|
|
let r = if let Some(current) = state
|
2020-12-07 05:46:39 -05:00
|
|
|
.snapshots
|
2021-02-28 19:53:20 -05:00
|
|
|
.get(&(specifier.clone(), args.version.clone().into()))
|
2020-12-07 05:46:39 -05:00
|
|
|
{
|
2021-02-28 19:53:20 -05:00
|
|
|
if let Some(prev) = state
|
|
|
|
.snapshots
|
|
|
|
.get(&(specifier, args.old_version.clone().into()))
|
|
|
|
{
|
2020-12-07 05:46:39 -05:00
|
|
|
Ok(text::get_range_change(prev, current))
|
|
|
|
} else {
|
2021-02-11 23:17:48 -05:00
|
|
|
let new_length = current.encode_utf16().count();
|
2020-12-07 05:46:39 -05:00
|
|
|
// when a local file is opened up in the editor, the compiler might
|
|
|
|
// already have a snapshot of it in memory, and will request it, but we
|
|
|
|
// now are working off in memory versions of the document, and so need
|
|
|
|
// to tell tsc to reset the whole document
|
|
|
|
Ok(json!({
|
|
|
|
"span": {
|
|
|
|
"start": 0,
|
2021-02-24 22:15:55 -05:00
|
|
|
"length": args.old_length,
|
2020-12-07 05:46:39 -05:00
|
|
|
},
|
2021-02-11 23:17:48 -05:00
|
|
|
"newLength": new_length,
|
2020-12-07 05:46:39 -05:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Err(custom_error(
|
|
|
|
"MissingSnapshot",
|
|
|
|
format!(
|
2021-02-24 22:15:55 -05:00
|
|
|
"The current snapshot version is missing.\n Args: \"{:?}\"",
|
2020-12-07 05:46:39 -05:00
|
|
|
args
|
|
|
|
),
|
|
|
|
))
|
2021-05-11 00:54:10 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
state.state_snapshot.performance.measure(mark);
|
|
|
|
r
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
2021-05-02 08:29:19 -04:00
|
|
|
fn op_get_length(
|
2021-02-24 22:15:55 -05:00
|
|
|
state: &mut State,
|
|
|
|
args: SourceSnapshotArgs,
|
|
|
|
) -> Result<usize, AnyError> {
|
2021-05-11 00:54:10 -04:00
|
|
|
let mark = state
|
|
|
|
.state_snapshot
|
|
|
|
.performance
|
|
|
|
.mark("op_get_length", Some(&args));
|
2021-06-21 17:18:32 -04:00
|
|
|
let specifier = state.normalize_specifier(args.specifier)?;
|
2021-05-11 00:54:10 -04:00
|
|
|
let r = if let Some(Some(asset)) = state.state_snapshot.assets.get(&specifier)
|
|
|
|
{
|
2021-02-24 22:15:55 -05:00
|
|
|
Ok(asset.length)
|
2021-03-10 05:39:16 -05:00
|
|
|
} else {
|
2021-02-28 19:53:20 -05:00
|
|
|
cache_snapshot(state, &specifier, args.version.clone())?;
|
2020-12-07 05:46:39 -05:00
|
|
|
let content = state
|
|
|
|
.snapshots
|
2021-02-28 19:53:20 -05:00
|
|
|
.get(&(specifier, args.version.into()))
|
2020-12-07 05:46:39 -05:00
|
|
|
.unwrap();
|
2021-02-24 22:15:55 -05:00
|
|
|
Ok(content.encode_utf16().count())
|
2021-05-11 00:54:10 -04:00
|
|
|
};
|
|
|
|
state.state_snapshot.performance.measure(mark);
|
|
|
|
r
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
2021-05-11 00:54:10 -04:00
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
2020-12-07 05:46:39 -05:00
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct GetTextArgs {
|
|
|
|
specifier: String,
|
|
|
|
version: String,
|
|
|
|
start: usize,
|
|
|
|
end: usize,
|
|
|
|
}
|
|
|
|
|
2021-05-02 08:29:19 -04:00
|
|
|
fn op_get_text(
|
|
|
|
state: &mut State,
|
|
|
|
args: GetTextArgs,
|
|
|
|
) -> Result<String, AnyError> {
|
2021-05-11 00:54:10 -04:00
|
|
|
let mark = state
|
|
|
|
.state_snapshot
|
|
|
|
.performance
|
|
|
|
.mark("op_get_text", Some(&args));
|
2021-06-21 17:18:32 -04:00
|
|
|
let specifier = state.normalize_specifier(args.specifier)?;
|
2021-02-12 06:49:42 -05:00
|
|
|
let content =
|
|
|
|
if let Some(Some(content)) = state.state_snapshot.assets.get(&specifier) {
|
2021-09-07 10:39:32 -04:00
|
|
|
content.text.as_str()
|
2021-03-10 05:39:16 -05:00
|
|
|
} else {
|
2021-02-28 19:53:20 -05:00
|
|
|
cache_snapshot(state, &specifier, args.version.clone())?;
|
2021-02-12 06:49:42 -05:00
|
|
|
state
|
|
|
|
.snapshots
|
2021-02-28 19:53:20 -05:00
|
|
|
.get(&(specifier, args.version.into()))
|
2021-02-12 06:49:42 -05:00
|
|
|
.unwrap()
|
|
|
|
};
|
2021-02-11 23:17:48 -05:00
|
|
|
state.state_snapshot.performance.measure(mark);
|
2021-09-07 10:39:32 -04:00
|
|
|
Ok(text::slice(content, args.start..args.end).to_string())
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
2021-06-21 17:18:32 -04:00
|
|
|
fn op_load(
|
|
|
|
state: &mut State,
|
|
|
|
args: SpecifierArgs,
|
|
|
|
) -> Result<Option<String>, AnyError> {
|
|
|
|
let mark = state
|
|
|
|
.state_snapshot
|
|
|
|
.performance
|
|
|
|
.mark("op_load", Some(&args));
|
|
|
|
let specifier = state.normalize_specifier(args.specifier)?;
|
|
|
|
let result = state.state_snapshot.sources.get_source(&specifier);
|
|
|
|
state.state_snapshot.performance.measure(mark);
|
2021-09-07 10:39:32 -04:00
|
|
|
Ok(result.map(|t| t.to_string()))
|
2021-06-21 17:18:32 -04:00
|
|
|
}
|
|
|
|
|
2021-05-02 08:29:19 -04:00
|
|
|
fn op_resolve(
|
2021-02-24 22:15:55 -05:00
|
|
|
state: &mut State,
|
|
|
|
args: ResolveArgs,
|
|
|
|
) -> Result<Vec<Option<(String, String)>>, AnyError> {
|
2021-05-11 00:54:10 -04:00
|
|
|
let mark = state
|
|
|
|
.state_snapshot
|
|
|
|
.performance
|
|
|
|
.mark("op_resolve", Some(&args));
|
2021-02-24 22:15:55 -05:00
|
|
|
let mut resolved = Vec::new();
|
2021-06-21 17:18:32 -04:00
|
|
|
let referrer = state.normalize_specifier(&args.base)?;
|
2021-02-06 07:39:01 -05:00
|
|
|
let sources = &mut state.state_snapshot.sources;
|
2020-12-07 05:46:39 -05:00
|
|
|
|
2021-02-12 06:49:42 -05:00
|
|
|
if state.state_snapshot.documents.contains_key(&referrer) {
|
2021-01-25 18:47:12 -05:00
|
|
|
if let Some(dependencies) =
|
|
|
|
state.state_snapshot.documents.dependencies(&referrer)
|
|
|
|
{
|
2021-02-24 22:15:55 -05:00
|
|
|
for specifier in &args.specifiers {
|
2020-12-07 05:46:39 -05:00
|
|
|
if specifier.starts_with("asset:///") {
|
|
|
|
resolved.push(Some((
|
|
|
|
specifier.clone(),
|
2021-02-17 23:37:05 -05:00
|
|
|
MediaType::from(specifier).as_ts_extension().into(),
|
2020-12-07 05:46:39 -05:00
|
|
|
)))
|
|
|
|
} else if let Some(dependency) = dependencies.get(specifier) {
|
|
|
|
let resolved_import =
|
|
|
|
if let Some(resolved_import) = &dependency.maybe_type {
|
|
|
|
resolved_import.clone()
|
|
|
|
} else if let Some(resolved_import) = &dependency.maybe_code {
|
|
|
|
resolved_import.clone()
|
|
|
|
} else {
|
2021-02-09 17:46:12 -05:00
|
|
|
ResolvedDependency::Err(ResolvedDependencyErr::Missing)
|
2020-12-07 05:46:39 -05:00
|
|
|
};
|
2020-12-24 05:53:03 -05:00
|
|
|
if let ResolvedDependency::Resolved(resolved_specifier) =
|
|
|
|
resolved_import
|
2020-12-07 05:46:39 -05:00
|
|
|
{
|
2021-02-12 06:49:42 -05:00
|
|
|
if state
|
|
|
|
.state_snapshot
|
|
|
|
.documents
|
|
|
|
.contains_key(&resolved_specifier)
|
|
|
|
{
|
2021-02-11 23:17:48 -05:00
|
|
|
let media_type = MediaType::from(&resolved_specifier);
|
|
|
|
resolved.push(Some((
|
|
|
|
resolved_specifier.to_string(),
|
2021-02-17 23:37:05 -05:00
|
|
|
media_type.as_ts_extension().into(),
|
2021-02-11 23:17:48 -05:00
|
|
|
)));
|
2021-02-12 06:49:42 -05:00
|
|
|
} else if sources.contains_key(&resolved_specifier) {
|
2020-12-09 14:50:47 -05:00
|
|
|
let media_type = if let Some(media_type) =
|
|
|
|
sources.get_media_type(&resolved_specifier)
|
|
|
|
{
|
|
|
|
media_type
|
|
|
|
} else {
|
|
|
|
MediaType::from(&resolved_specifier)
|
|
|
|
};
|
|
|
|
resolved.push(Some((
|
|
|
|
resolved_specifier.to_string(),
|
2021-02-17 23:37:05 -05:00
|
|
|
media_type.as_ts_extension().into(),
|
2020-12-09 14:50:47 -05:00
|
|
|
)));
|
2020-12-07 05:46:39 -05:00
|
|
|
} else {
|
2020-12-09 14:50:47 -05:00
|
|
|
resolved.push(None);
|
|
|
|
}
|
2020-12-07 05:46:39 -05:00
|
|
|
} else {
|
|
|
|
resolved.push(None);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-12 06:49:42 -05:00
|
|
|
} else if sources.contains_key(&referrer) {
|
2021-02-24 22:15:55 -05:00
|
|
|
for specifier in &args.specifiers {
|
2020-12-07 05:46:39 -05:00
|
|
|
if let Some((resolved_specifier, media_type)) =
|
|
|
|
sources.resolve_import(specifier, &referrer)
|
|
|
|
{
|
|
|
|
resolved.push(Some((
|
|
|
|
resolved_specifier.to_string(),
|
2021-02-17 23:37:05 -05:00
|
|
|
media_type.as_ts_extension().into(),
|
2020-12-07 05:46:39 -05:00
|
|
|
)));
|
|
|
|
} else {
|
|
|
|
resolved.push(None);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2021-02-11 23:17:48 -05:00
|
|
|
state.state_snapshot.performance.measure(mark);
|
2020-12-07 05:46:39 -05:00
|
|
|
return Err(custom_error(
|
|
|
|
"NotFound",
|
2021-01-22 05:03:16 -05:00
|
|
|
format!(
|
|
|
|
"the referring ({}) specifier is unexpectedly missing",
|
|
|
|
referrer
|
|
|
|
),
|
2020-12-07 05:46:39 -05:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2021-02-11 23:17:48 -05:00
|
|
|
state.state_snapshot.performance.measure(mark);
|
2021-02-24 22:15:55 -05:00
|
|
|
Ok(resolved)
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
2021-05-02 08:29:19 -04:00
|
|
|
fn op_respond(state: &mut State, args: Response) -> Result<bool, AnyError> {
|
2021-02-24 22:15:55 -05:00
|
|
|
state.response = Some(args);
|
|
|
|
Ok(true)
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
2021-05-02 08:29:19 -04:00
|
|
|
fn op_script_names(
|
2021-02-24 22:15:55 -05:00
|
|
|
state: &mut State,
|
|
|
|
_args: Value,
|
|
|
|
) -> Result<Vec<ModuleSpecifier>, AnyError> {
|
|
|
|
Ok(
|
|
|
|
state
|
|
|
|
.state_snapshot
|
|
|
|
.documents
|
|
|
|
.open_specifiers()
|
|
|
|
.into_iter()
|
|
|
|
.cloned()
|
|
|
|
.collect(),
|
|
|
|
)
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
2021-05-11 00:54:10 -04:00
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
2020-12-07 05:46:39 -05:00
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct ScriptVersionArgs {
|
|
|
|
specifier: String,
|
|
|
|
}
|
|
|
|
|
2021-05-02 08:29:19 -04:00
|
|
|
fn op_script_version(
|
2021-02-24 22:15:55 -05:00
|
|
|
state: &mut State,
|
|
|
|
args: ScriptVersionArgs,
|
|
|
|
) -> Result<Option<String>, AnyError> {
|
2021-05-11 00:54:10 -04:00
|
|
|
let mark = state
|
|
|
|
.state_snapshot
|
|
|
|
.performance
|
|
|
|
.mark("op_script_version", Some(&args));
|
2021-06-21 17:18:32 -04:00
|
|
|
let specifier = state.normalize_specifier(args.specifier)?;
|
2021-05-11 00:54:10 -04:00
|
|
|
let r = if specifier.scheme() == "asset" {
|
|
|
|
if state.state_snapshot.assets.contains_key(&specifier) {
|
2021-02-24 22:15:55 -05:00
|
|
|
Ok(Some("1".to_string()))
|
2021-02-12 06:49:42 -05:00
|
|
|
} else {
|
2021-02-24 22:15:55 -05:00
|
|
|
Ok(None)
|
2021-05-11 00:54:10 -04:00
|
|
|
}
|
2021-02-12 06:49:42 -05:00
|
|
|
} else if let Some(version) =
|
|
|
|
state.state_snapshot.documents.version(&specifier)
|
|
|
|
{
|
2021-05-11 00:54:10 -04:00
|
|
|
Ok(Some(version.to_string()))
|
2020-12-07 05:46:39 -05:00
|
|
|
} else {
|
2021-02-06 07:39:01 -05:00
|
|
|
let sources = &mut state.state_snapshot.sources;
|
2020-12-07 05:46:39 -05:00
|
|
|
if let Some(version) = sources.get_script_version(&specifier) {
|
2021-05-11 00:54:10 -04:00
|
|
|
Ok(Some(version))
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
2021-05-11 00:54:10 -04:00
|
|
|
};
|
2020-12-07 05:46:39 -05:00
|
|
|
|
2021-02-11 23:17:48 -05:00
|
|
|
state.state_snapshot.performance.measure(mark);
|
2021-05-11 00:54:10 -04:00
|
|
|
r
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create and setup a JsRuntime based on a snapshot. It is expected that the
|
|
|
|
/// supplied snapshot is an isolate that contains the TypeScript language
|
|
|
|
/// server.
|
2021-06-21 17:18:32 -04:00
|
|
|
fn load() -> Result<JsRuntime, AnyError> {
|
2020-12-07 05:46:39 -05:00
|
|
|
let mut runtime = JsRuntime::new(RuntimeOptions {
|
2021-01-05 20:38:23 -05:00
|
|
|
startup_snapshot: Some(tsc::compiler_snapshot()),
|
2020-12-07 05:46:39 -05:00
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
|
|
|
|
{
|
|
|
|
let op_state = runtime.op_state();
|
|
|
|
let mut op_state = op_state.borrow_mut();
|
2020-12-21 08:44:26 -05:00
|
|
|
op_state.put(State::new(StateSnapshot::default()));
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
2021-05-02 08:29:19 -04:00
|
|
|
runtime.register_op("op_dispose", op(op_dispose));
|
2021-06-21 17:18:32 -04:00
|
|
|
runtime.register_op("op_exists", op(op_exists));
|
2021-05-02 08:29:19 -04:00
|
|
|
runtime.register_op("op_get_change_range", op(op_get_change_range));
|
|
|
|
runtime.register_op("op_get_length", op(op_get_length));
|
|
|
|
runtime.register_op("op_get_text", op(op_get_text));
|
2021-06-21 17:18:32 -04:00
|
|
|
runtime.register_op("op_load", op(op_load));
|
2021-05-02 08:29:19 -04:00
|
|
|
runtime.register_op("op_resolve", op(op_resolve));
|
|
|
|
runtime.register_op("op_respond", op(op_respond));
|
|
|
|
runtime.register_op("op_script_names", op(op_script_names));
|
|
|
|
runtime.register_op("op_script_version", op(op_script_version));
|
2021-04-25 16:00:05 -04:00
|
|
|
runtime.sync_ops_cache();
|
2020-12-07 05:46:39 -05:00
|
|
|
|
2021-06-21 17:18:32 -04:00
|
|
|
Ok(runtime)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Instruct a language server runtime to start the language server and provide
|
|
|
|
/// it with a minimal bootstrap configuration.
|
|
|
|
fn start(
|
|
|
|
runtime: &mut JsRuntime,
|
|
|
|
debug: bool,
|
|
|
|
state_snapshot: &StateSnapshot,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let root_uri = state_snapshot
|
|
|
|
.config
|
|
|
|
.root_uri
|
|
|
|
.clone()
|
|
|
|
.unwrap_or_else(|| Url::parse("cache:///").unwrap());
|
|
|
|
let init_config = json!({ "debug": debug, "rootUri": root_uri });
|
2020-12-07 05:46:39 -05:00
|
|
|
let init_src = format!("globalThis.serverInit({});", init_config);
|
|
|
|
|
2021-07-08 12:56:53 -04:00
|
|
|
runtime.execute_script(&located_script_name!(), &init_src)?;
|
|
|
|
Ok(())
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
2020-12-08 05:36:13 -05:00
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
#[serde(rename_all = "kebab-case")]
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub enum QuotePreference {
|
|
|
|
Auto,
|
|
|
|
Double,
|
|
|
|
Single,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
#[serde(rename_all = "kebab-case")]
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub enum ImportModuleSpecifierPreference {
|
|
|
|
Auto,
|
|
|
|
Relative,
|
|
|
|
NonRelative,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
#[serde(rename_all = "kebab-case")]
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub enum ImportModuleSpecifierEnding {
|
|
|
|
Auto,
|
|
|
|
Minimal,
|
|
|
|
Index,
|
|
|
|
Js,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
#[serde(rename_all = "kebab-case")]
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub enum IncludePackageJsonAutoImports {
|
|
|
|
Auto,
|
|
|
|
On,
|
|
|
|
Off,
|
|
|
|
}
|
|
|
|
|
2021-03-15 18:01:41 -04:00
|
|
|
#[derive(Debug, Default, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct GetCompletionsAtPositionOptions {
|
|
|
|
#[serde(flatten)]
|
|
|
|
pub user_preferences: UserPreferences,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub trigger_character: Option<String>,
|
|
|
|
}
|
|
|
|
|
2020-12-08 05:36:13 -05:00
|
|
|
#[derive(Debug, Default, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct UserPreferences {
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub disable_suggestions: Option<bool>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub quote_preference: Option<QuotePreference>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub include_completions_for_module_exports: Option<bool>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2021-09-15 22:07:52 -04:00
|
|
|
pub include_completions_for_import_statements: Option<bool>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub include_completions_with_snippet_text: Option<bool>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2020-12-08 05:36:13 -05:00
|
|
|
pub include_automatic_optional_chain_completions: Option<bool>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub include_completions_with_insert_text: Option<bool>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2021-09-15 22:07:52 -04:00
|
|
|
pub allow_incomplete_completions: Option<bool>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2020-12-08 05:36:13 -05:00
|
|
|
pub import_module_specifier_preference:
|
|
|
|
Option<ImportModuleSpecifierPreference>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub import_module_specifier_ending: Option<ImportModuleSpecifierEnding>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub allow_text_changes_in_new_files: Option<bool>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub provide_prefix_and_suffix_text_for_rename: Option<bool>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub include_package_json_auto_imports: Option<IncludePackageJsonAutoImports>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub provide_refactor_not_applicable_reason: Option<bool>,
|
|
|
|
}
|
|
|
|
|
2021-02-15 21:34:09 -05:00
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct SignatureHelpItemsOptions {
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub trigger_reason: Option<SignatureHelpTriggerReason>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
pub enum SignatureHelpTriggerKind {
|
|
|
|
#[serde(rename = "characterTyped")]
|
|
|
|
CharacterTyped,
|
|
|
|
#[serde(rename = "invoked")]
|
|
|
|
Invoked,
|
|
|
|
#[serde(rename = "retrigger")]
|
|
|
|
Retrigger,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<lsp::SignatureHelpTriggerKind> for SignatureHelpTriggerKind {
|
|
|
|
fn from(kind: lsp::SignatureHelpTriggerKind) -> Self {
|
|
|
|
match kind {
|
|
|
|
lsp::SignatureHelpTriggerKind::Invoked => Self::Invoked,
|
|
|
|
lsp::SignatureHelpTriggerKind::TriggerCharacter => Self::CharacterTyped,
|
|
|
|
lsp::SignatureHelpTriggerKind::ContentChange => Self::Retrigger,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct SignatureHelpTriggerReason {
|
|
|
|
pub kind: SignatureHelpTriggerKind,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub trigger_character: Option<String>,
|
|
|
|
}
|
|
|
|
|
2021-03-15 18:01:41 -04:00
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct GetCompletionDetailsArgs {
|
|
|
|
pub specifier: ModuleSpecifier,
|
|
|
|
pub position: u32,
|
|
|
|
pub name: String,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub source: Option<String>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub data: Option<Value>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<CompletionItemData> for GetCompletionDetailsArgs {
|
|
|
|
fn from(item_data: CompletionItemData) -> Self {
|
|
|
|
Self {
|
|
|
|
specifier: item_data.specifier,
|
|
|
|
position: item_data.position,
|
|
|
|
name: item_data.name,
|
|
|
|
source: item_data.source,
|
|
|
|
data: item_data.data,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-07 05:46:39 -05:00
|
|
|
/// Methods that are supported by the Language Service in the compiler isolate.
|
2021-01-22 05:03:16 -05:00
|
|
|
#[derive(Debug)]
|
2020-12-07 05:46:39 -05:00
|
|
|
pub enum RequestMethod {
|
|
|
|
/// Configure the compilation settings for the server.
|
|
|
|
Configure(TsConfig),
|
2021-01-31 22:30:41 -05:00
|
|
|
/// Get rename locations at a given position.
|
2021-06-19 11:23:50 -04:00
|
|
|
FindRenameLocations {
|
|
|
|
specifier: ModuleSpecifier,
|
|
|
|
position: u32,
|
|
|
|
find_in_strings: bool,
|
|
|
|
find_in_comments: bool,
|
|
|
|
provide_prefix_and_suffix_text_for_rename: bool,
|
|
|
|
},
|
2020-12-15 14:34:39 -05:00
|
|
|
/// Retrieve the text of an assets that exists in memory in the isolate.
|
|
|
|
GetAsset(ModuleSpecifier),
|
2021-08-05 21:46:32 -04:00
|
|
|
/// Retrieve the possible refactor info for a range of a file.
|
|
|
|
GetApplicableRefactors((ModuleSpecifier, TextSpan, String)),
|
|
|
|
/// Retrieve the refactor edit info for a range.
|
|
|
|
GetEditsForRefactor((ModuleSpecifier, TextSpan, String, String)),
|
2021-02-04 13:53:02 -05:00
|
|
|
/// Retrieve code fixes for a range of a file with the provided error codes.
|
|
|
|
GetCodeFixes((ModuleSpecifier, u32, u32, Vec<String>)),
|
2021-01-31 22:30:41 -05:00
|
|
|
/// Get completion information at a given position (IntelliSense).
|
2021-03-15 18:01:41 -04:00
|
|
|
GetCompletions((ModuleSpecifier, u32, GetCompletionsAtPositionOptions)),
|
|
|
|
/// Get details about a specific completion entry.
|
|
|
|
GetCompletionDetails(GetCompletionDetailsArgs),
|
2021-02-04 13:53:02 -05:00
|
|
|
/// Retrieve the combined code fixes for a fix id for a module.
|
|
|
|
GetCombinedCodeFix((ModuleSpecifier, Value)),
|
2021-01-31 22:30:41 -05:00
|
|
|
/// Get declaration information for a specific position.
|
|
|
|
GetDefinition((ModuleSpecifier, u32)),
|
2020-12-29 20:46:58 -05:00
|
|
|
/// Return diagnostics for given file.
|
2021-01-22 05:03:16 -05:00
|
|
|
GetDiagnostics(Vec<ModuleSpecifier>),
|
2020-12-07 05:46:39 -05:00
|
|
|
/// Return document highlights at position.
|
|
|
|
GetDocumentHighlights((ModuleSpecifier, u32, Vec<ModuleSpecifier>)),
|
2021-04-19 21:26:36 -04:00
|
|
|
/// Get semantic highlights information for a particular file.
|
|
|
|
GetEncodedSemanticClassifications((ModuleSpecifier, TextSpan)),
|
2021-01-12 16:53:27 -05:00
|
|
|
/// Get implementation information for a specific position.
|
|
|
|
GetImplementation((ModuleSpecifier, u32)),
|
2021-01-31 22:30:41 -05:00
|
|
|
/// Get a "navigation tree" for a specifier.
|
|
|
|
GetNavigationTree(ModuleSpecifier),
|
2021-04-02 02:21:07 -04:00
|
|
|
/// Get outlining spans for a specifier.
|
|
|
|
GetOutliningSpans(ModuleSpecifier),
|
2021-01-31 22:30:41 -05:00
|
|
|
/// Return quick info at position (hover information).
|
|
|
|
GetQuickInfo((ModuleSpecifier, u32)),
|
|
|
|
/// Get document references for a specific position.
|
|
|
|
GetReferences((ModuleSpecifier, u32)),
|
2021-02-15 21:34:09 -05:00
|
|
|
/// Get signature help items for a specific position.
|
|
|
|
GetSignatureHelpItems((ModuleSpecifier, u32, SignatureHelpItemsOptions)),
|
2021-03-23 19:33:25 -04:00
|
|
|
/// Get a selection range for a specific position.
|
|
|
|
GetSmartSelectionRange((ModuleSpecifier, u32)),
|
2021-02-04 13:53:02 -05:00
|
|
|
/// Get the diagnostic codes that support some form of code fix.
|
|
|
|
GetSupportedCodeFixes,
|
2021-04-19 01:11:26 -04:00
|
|
|
/// Resolve a call hierarchy item for a specific position.
|
|
|
|
PrepareCallHierarchy((ModuleSpecifier, u32)),
|
|
|
|
/// Resolve incoming call hierarchy items for a specific position.
|
|
|
|
ProvideCallHierarchyIncomingCalls((ModuleSpecifier, u32)),
|
|
|
|
/// Resolve outgoing call hierarchy items for a specific position.
|
|
|
|
ProvideCallHierarchyOutgoingCalls((ModuleSpecifier, u32)),
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RequestMethod {
|
2021-06-21 17:18:32 -04:00
|
|
|
fn to_value(&self, state: &State, id: usize) -> Value {
|
2020-12-07 05:46:39 -05:00
|
|
|
match self {
|
|
|
|
RequestMethod::Configure(config) => json!({
|
|
|
|
"id": id,
|
|
|
|
"method": "configure",
|
|
|
|
"compilerOptions": config,
|
|
|
|
}),
|
2021-06-19 11:23:50 -04:00
|
|
|
RequestMethod::FindRenameLocations {
|
2021-01-31 22:30:41 -05:00
|
|
|
specifier,
|
|
|
|
position,
|
|
|
|
find_in_strings,
|
|
|
|
find_in_comments,
|
|
|
|
provide_prefix_and_suffix_text_for_rename,
|
2021-06-19 11:23:50 -04:00
|
|
|
} => {
|
2021-01-31 22:30:41 -05:00
|
|
|
json!({
|
|
|
|
"id": id,
|
|
|
|
"method": "findRenameLocations",
|
2021-06-21 17:18:32 -04:00
|
|
|
"specifier": state.denormalize_specifier(specifier),
|
2021-01-31 22:30:41 -05:00
|
|
|
"position": position,
|
|
|
|
"findInStrings": find_in_strings,
|
|
|
|
"findInComments": find_in_comments,
|
|
|
|
"providePrefixAndSuffixTextForRename": provide_prefix_and_suffix_text_for_rename
|
|
|
|
})
|
|
|
|
}
|
2020-12-15 14:34:39 -05:00
|
|
|
RequestMethod::GetAsset(specifier) => json!({
|
|
|
|
"id": id,
|
|
|
|
"method": "getAsset",
|
|
|
|
"specifier": specifier,
|
|
|
|
}),
|
2021-08-05 21:46:32 -04:00
|
|
|
RequestMethod::GetApplicableRefactors((specifier, span, kind)) => json!({
|
|
|
|
"id": id,
|
|
|
|
"method": "getApplicableRefactors",
|
|
|
|
"specifier": state.denormalize_specifier(specifier),
|
|
|
|
"range": { "pos": span.start, "end": span.start + span.length},
|
|
|
|
"kind": kind,
|
|
|
|
}),
|
|
|
|
RequestMethod::GetEditsForRefactor((
|
|
|
|
specifier,
|
|
|
|
span,
|
|
|
|
refactor_name,
|
|
|
|
action_name,
|
|
|
|
)) => json!({
|
|
|
|
"id": id,
|
|
|
|
"method": "getEditsForRefactor",
|
|
|
|
"specifier": state.denormalize_specifier(specifier),
|
|
|
|
"range": { "pos": span.start, "end": span.start + span.length},
|
|
|
|
"refactorName": refactor_name,
|
|
|
|
"actionName": action_name,
|
|
|
|
}),
|
2021-02-04 13:53:02 -05:00
|
|
|
RequestMethod::GetCodeFixes((
|
|
|
|
specifier,
|
|
|
|
start_pos,
|
|
|
|
end_pos,
|
|
|
|
error_codes,
|
|
|
|
)) => json!({
|
|
|
|
"id": id,
|
|
|
|
"method": "getCodeFixes",
|
2021-06-21 17:18:32 -04:00
|
|
|
"specifier": state.denormalize_specifier(specifier),
|
2021-02-04 13:53:02 -05:00
|
|
|
"startPosition": start_pos,
|
|
|
|
"endPosition": end_pos,
|
|
|
|
"errorCodes": error_codes,
|
|
|
|
}),
|
|
|
|
RequestMethod::GetCombinedCodeFix((specifier, fix_id)) => json!({
|
|
|
|
"id": id,
|
|
|
|
"method": "getCombinedCodeFix",
|
2021-06-21 17:18:32 -04:00
|
|
|
"specifier": state.denormalize_specifier(specifier),
|
2021-02-04 13:53:02 -05:00
|
|
|
"fixId": fix_id,
|
|
|
|
}),
|
2021-03-15 18:01:41 -04:00
|
|
|
RequestMethod::GetCompletionDetails(args) => json!({
|
|
|
|
"id": id,
|
|
|
|
"method": "getCompletionDetails",
|
|
|
|
"args": args
|
|
|
|
}),
|
2021-01-31 22:30:41 -05:00
|
|
|
RequestMethod::GetCompletions((specifier, position, preferences)) => {
|
|
|
|
json!({
|
|
|
|
"id": id,
|
|
|
|
"method": "getCompletions",
|
2021-06-21 17:18:32 -04:00
|
|
|
"specifier": state.denormalize_specifier(specifier),
|
2021-01-31 22:30:41 -05:00
|
|
|
"position": position,
|
|
|
|
"preferences": preferences,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
RequestMethod::GetDefinition((specifier, position)) => json!({
|
|
|
|
"id": id,
|
|
|
|
"method": "getDefinition",
|
2021-06-21 17:18:32 -04:00
|
|
|
"specifier": state.denormalize_specifier(specifier),
|
2021-01-31 22:30:41 -05:00
|
|
|
"position": position,
|
|
|
|
}),
|
2021-01-22 05:03:16 -05:00
|
|
|
RequestMethod::GetDiagnostics(specifiers) => json!({
|
2020-12-07 05:46:39 -05:00
|
|
|
"id": id,
|
2020-12-29 20:46:58 -05:00
|
|
|
"method": "getDiagnostics",
|
2021-06-21 17:18:32 -04:00
|
|
|
"specifiers": specifiers.iter().map(|s| state.denormalize_specifier(s)).collect::<Vec<String>>(),
|
2020-12-07 05:46:39 -05:00
|
|
|
}),
|
|
|
|
RequestMethod::GetDocumentHighlights((
|
|
|
|
specifier,
|
|
|
|
position,
|
|
|
|
files_to_search,
|
|
|
|
)) => json!({
|
|
|
|
"id": id,
|
|
|
|
"method": "getDocumentHighlights",
|
2021-06-21 17:18:32 -04:00
|
|
|
"specifier": state.denormalize_specifier(specifier),
|
2020-12-07 05:46:39 -05:00
|
|
|
"position": position,
|
|
|
|
"filesToSearch": files_to_search,
|
|
|
|
}),
|
2021-04-19 21:26:36 -04:00
|
|
|
RequestMethod::GetEncodedSemanticClassifications((specifier, span)) => {
|
|
|
|
json!({
|
|
|
|
"id": id,
|
|
|
|
"method": "getEncodedSemanticClassifications",
|
2021-06-21 17:18:32 -04:00
|
|
|
"specifier": state.denormalize_specifier(specifier),
|
2021-04-19 21:26:36 -04:00
|
|
|
"span": span,
|
|
|
|
})
|
|
|
|
}
|
2021-01-31 22:30:41 -05:00
|
|
|
RequestMethod::GetImplementation((specifier, position)) => json!({
|
2020-12-07 05:46:39 -05:00
|
|
|
"id": id,
|
2021-01-31 22:30:41 -05:00
|
|
|
"method": "getImplementation",
|
2021-06-21 17:18:32 -04:00
|
|
|
"specifier": state.denormalize_specifier(specifier),
|
2020-12-07 05:46:39 -05:00
|
|
|
"position": position,
|
|
|
|
}),
|
2021-01-31 22:30:41 -05:00
|
|
|
RequestMethod::GetNavigationTree(specifier) => json!({
|
2020-12-07 05:46:39 -05:00
|
|
|
"id": id,
|
2021-01-31 22:30:41 -05:00
|
|
|
"method": "getNavigationTree",
|
2021-06-21 17:18:32 -04:00
|
|
|
"specifier": state.denormalize_specifier(specifier),
|
2021-01-31 22:30:41 -05:00
|
|
|
}),
|
2021-04-02 02:21:07 -04:00
|
|
|
RequestMethod::GetOutliningSpans(specifier) => json!({
|
|
|
|
"id": id,
|
|
|
|
"method": "getOutliningSpans",
|
2021-06-21 17:18:32 -04:00
|
|
|
"specifier": state.denormalize_specifier(specifier),
|
2021-04-02 02:21:07 -04:00
|
|
|
}),
|
2021-01-31 22:30:41 -05:00
|
|
|
RequestMethod::GetQuickInfo((specifier, position)) => json!({
|
|
|
|
"id": id,
|
|
|
|
"method": "getQuickInfo",
|
2021-06-21 17:18:32 -04:00
|
|
|
"specifier": state.denormalize_specifier(specifier),
|
2020-12-07 05:46:39 -05:00
|
|
|
"position": position,
|
|
|
|
}),
|
2021-01-31 22:30:41 -05:00
|
|
|
RequestMethod::GetReferences((specifier, position)) => json!({
|
|
|
|
"id": id,
|
|
|
|
"method": "getReferences",
|
2021-06-21 17:18:32 -04:00
|
|
|
"specifier": state.denormalize_specifier(specifier),
|
2021-01-31 22:30:41 -05:00
|
|
|
"position": position,
|
2021-01-12 16:53:27 -05:00
|
|
|
}),
|
2021-02-15 21:34:09 -05:00
|
|
|
RequestMethod::GetSignatureHelpItems((specifier, position, options)) => {
|
|
|
|
json!({
|
|
|
|
"id": id,
|
|
|
|
"method": "getSignatureHelpItems",
|
2021-06-21 17:18:32 -04:00
|
|
|
"specifier": state.denormalize_specifier(specifier),
|
2021-02-15 21:34:09 -05:00
|
|
|
"position": position,
|
|
|
|
"options": options,
|
|
|
|
})
|
|
|
|
}
|
2021-03-23 19:33:25 -04:00
|
|
|
RequestMethod::GetSmartSelectionRange((specifier, position)) => {
|
|
|
|
json!({
|
|
|
|
"id": id,
|
|
|
|
"method": "getSmartSelectionRange",
|
2021-06-21 17:18:32 -04:00
|
|
|
"specifier": state.denormalize_specifier(specifier),
|
2021-03-23 19:33:25 -04:00
|
|
|
"position": position
|
|
|
|
})
|
|
|
|
}
|
2021-02-04 13:53:02 -05:00
|
|
|
RequestMethod::GetSupportedCodeFixes => json!({
|
|
|
|
"id": id,
|
|
|
|
"method": "getSupportedCodeFixes",
|
|
|
|
}),
|
2021-04-19 01:11:26 -04:00
|
|
|
RequestMethod::PrepareCallHierarchy((specifier, position)) => {
|
|
|
|
json!({
|
|
|
|
"id": id,
|
|
|
|
"method": "prepareCallHierarchy",
|
2021-06-21 17:18:32 -04:00
|
|
|
"specifier": state.denormalize_specifier(specifier),
|
2021-04-19 01:11:26 -04:00
|
|
|
"position": position
|
|
|
|
})
|
|
|
|
}
|
|
|
|
RequestMethod::ProvideCallHierarchyIncomingCalls((
|
|
|
|
specifier,
|
|
|
|
position,
|
|
|
|
)) => {
|
|
|
|
json!({
|
|
|
|
"id": id,
|
|
|
|
"method": "provideCallHierarchyIncomingCalls",
|
2021-06-21 17:18:32 -04:00
|
|
|
"specifier": state.denormalize_specifier(specifier),
|
2021-04-19 01:11:26 -04:00
|
|
|
"position": position
|
|
|
|
})
|
|
|
|
}
|
|
|
|
RequestMethod::ProvideCallHierarchyOutgoingCalls((
|
|
|
|
specifier,
|
|
|
|
position,
|
|
|
|
)) => {
|
|
|
|
json!({
|
|
|
|
"id": id,
|
|
|
|
"method": "provideCallHierarchyOutgoingCalls",
|
2021-06-21 17:18:32 -04:00
|
|
|
"specifier": state.denormalize_specifier(specifier),
|
2021-04-19 01:11:26 -04:00
|
|
|
"position": position
|
|
|
|
})
|
|
|
|
}
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Send a request into a runtime and return the JSON value of the response.
|
|
|
|
pub fn request(
|
|
|
|
runtime: &mut JsRuntime,
|
2020-12-21 08:44:26 -05:00
|
|
|
state_snapshot: StateSnapshot,
|
2020-12-07 05:46:39 -05:00
|
|
|
method: RequestMethod,
|
|
|
|
) -> Result<Value, AnyError> {
|
2021-05-11 00:54:10 -04:00
|
|
|
let performance = state_snapshot.performance.clone();
|
2021-06-21 17:18:32 -04:00
|
|
|
let request_params = {
|
2020-12-07 05:46:39 -05:00
|
|
|
let op_state = runtime.op_state();
|
|
|
|
let mut op_state = op_state.borrow_mut();
|
|
|
|
let state = op_state.borrow_mut::<State>();
|
2020-12-21 08:44:26 -05:00
|
|
|
state.state_snapshot = state_snapshot;
|
2020-12-07 05:46:39 -05:00
|
|
|
state.last_id += 1;
|
2021-06-21 17:18:32 -04:00
|
|
|
let id = state.last_id;
|
|
|
|
method.to_value(state, id)
|
2020-12-07 05:46:39 -05:00
|
|
|
};
|
2021-05-11 00:54:10 -04:00
|
|
|
let mark = performance.mark("request", Some(request_params.clone()));
|
2020-12-07 05:46:39 -05:00
|
|
|
let request_src = format!("globalThis.serverRequest({});", request_params);
|
2021-06-21 19:45:41 -04:00
|
|
|
runtime.execute_script(&located_script_name!(), &request_src)?;
|
2020-12-07 05:46:39 -05:00
|
|
|
|
|
|
|
let op_state = runtime.op_state();
|
|
|
|
let mut op_state = op_state.borrow_mut();
|
|
|
|
let state = op_state.borrow_mut::<State>();
|
|
|
|
|
2021-05-11 00:54:10 -04:00
|
|
|
performance.measure(mark);
|
2020-12-07 05:46:39 -05:00
|
|
|
if let Some(response) = state.response.clone() {
|
|
|
|
state.response = None;
|
|
|
|
Ok(response.data)
|
|
|
|
} else {
|
|
|
|
Err(custom_error(
|
|
|
|
"RequestError",
|
|
|
|
"The response was not received for the request.",
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2021-03-10 05:39:16 -05:00
|
|
|
use crate::http_cache::HttpCache;
|
|
|
|
use crate::http_util::HeadersMap;
|
|
|
|
use crate::lsp::analysis;
|
2021-01-22 05:03:16 -05:00
|
|
|
use crate::lsp::documents::DocumentCache;
|
2021-06-02 06:29:58 -04:00
|
|
|
use crate::lsp::documents::LanguageId;
|
2021-03-10 05:39:16 -05:00
|
|
|
use crate::lsp::sources::Sources;
|
2021-03-15 18:01:41 -04:00
|
|
|
use crate::lsp::text::LineIndex;
|
2021-03-10 05:39:16 -05:00
|
|
|
use std::path::Path;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use tempfile::TempDir;
|
|
|
|
|
|
|
|
fn mock_state_snapshot(
|
2021-06-02 06:29:58 -04:00
|
|
|
fixtures: &[(&str, &str, i32, LanguageId)],
|
2021-03-10 05:39:16 -05:00
|
|
|
location: &Path,
|
|
|
|
) -> StateSnapshot {
|
2021-01-22 05:03:16 -05:00
|
|
|
let mut documents = DocumentCache::default();
|
2021-06-02 06:29:58 -04:00
|
|
|
for (specifier, source, version, language_id) in fixtures {
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier =
|
|
|
|
resolve_url(specifier).expect("failed to create specifier");
|
2021-09-07 10:39:32 -04:00
|
|
|
documents.open(
|
|
|
|
specifier.clone(),
|
|
|
|
*version,
|
|
|
|
*language_id,
|
|
|
|
Arc::new(source.to_string()),
|
|
|
|
);
|
2021-03-24 20:13:37 -04:00
|
|
|
let media_type = MediaType::from(&specifier);
|
2021-09-07 10:39:32 -04:00
|
|
|
if let Some(Ok(parsed_module)) =
|
|
|
|
documents.get(&specifier).unwrap().source().module()
|
2021-03-24 20:13:37 -04:00
|
|
|
{
|
|
|
|
let (deps, _) = analysis::analyze_dependencies(
|
|
|
|
&specifier,
|
2021-09-07 10:39:32 -04:00
|
|
|
media_type,
|
|
|
|
parsed_module,
|
2021-03-24 20:13:37 -04:00
|
|
|
&None,
|
|
|
|
);
|
2021-06-24 19:06:51 -04:00
|
|
|
let dep_ranges =
|
2021-09-07 10:39:32 -04:00
|
|
|
analysis::analyze_dependency_ranges(parsed_module).ok();
|
2021-06-24 19:06:51 -04:00
|
|
|
documents
|
|
|
|
.set_dependencies(&specifier, Some(deps), dep_ranges)
|
|
|
|
.unwrap();
|
2021-03-10 05:39:16 -05:00
|
|
|
}
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
2021-03-10 05:39:16 -05:00
|
|
|
let sources = Sources::new(location);
|
2020-12-21 08:44:26 -05:00
|
|
|
StateSnapshot {
|
2021-01-25 18:47:12 -05:00
|
|
|
documents,
|
2021-03-10 05:39:16 -05:00
|
|
|
sources,
|
2021-02-11 23:17:48 -05:00
|
|
|
..Default::default()
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn setup(
|
|
|
|
debug: bool,
|
|
|
|
config: Value,
|
2021-06-02 06:29:58 -04:00
|
|
|
sources: &[(&str, &str, i32, LanguageId)],
|
2021-03-10 05:39:16 -05:00
|
|
|
) -> (JsRuntime, StateSnapshot, PathBuf) {
|
|
|
|
let temp_dir = TempDir::new().expect("could not create temp dir");
|
|
|
|
let location = temp_dir.path().join("deps");
|
|
|
|
let state_snapshot = mock_state_snapshot(sources, &location);
|
2021-06-21 17:18:32 -04:00
|
|
|
let mut runtime = load().expect("could not start server");
|
|
|
|
start(&mut runtime, debug, &state_snapshot)
|
|
|
|
.expect("could not start server");
|
2020-12-07 05:46:39 -05:00
|
|
|
let ts_config = TsConfig::new(config);
|
|
|
|
assert_eq!(
|
|
|
|
request(
|
|
|
|
&mut runtime,
|
2020-12-21 08:44:26 -05:00
|
|
|
state_snapshot.clone(),
|
2020-12-07 05:46:39 -05:00
|
|
|
RequestMethod::Configure(ts_config)
|
|
|
|
)
|
|
|
|
.expect("failed request"),
|
|
|
|
json!(true)
|
|
|
|
);
|
2021-03-10 05:39:16 -05:00
|
|
|
(runtime, state_snapshot, location)
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_replace_links() {
|
|
|
|
let actual = replace_links(r"test {@link http://deno.land/x/mod.ts} test");
|
|
|
|
assert_eq!(
|
|
|
|
actual,
|
|
|
|
r"test [http://deno.land/x/mod.ts](http://deno.land/x/mod.ts) test"
|
|
|
|
);
|
|
|
|
let actual =
|
|
|
|
replace_links(r"test {@link http://deno.land/x/mod.ts a link} test");
|
|
|
|
assert_eq!(actual, r"test [a link](http://deno.land/x/mod.ts) test");
|
|
|
|
let actual =
|
|
|
|
replace_links(r"test {@linkcode http://deno.land/x/mod.ts a link} test");
|
|
|
|
assert_eq!(actual, r"test [`a link`](http://deno.land/x/mod.ts) test");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_project_configure() {
|
|
|
|
setup(
|
|
|
|
false,
|
|
|
|
json!({
|
|
|
|
"target": "esnext",
|
|
|
|
"module": "esnext",
|
|
|
|
"noEmit": true,
|
|
|
|
}),
|
2021-03-10 05:39:16 -05:00
|
|
|
&[],
|
2020-12-07 05:46:39 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_project_reconfigure() {
|
2021-03-10 05:39:16 -05:00
|
|
|
let (mut runtime, state_snapshot, _) = setup(
|
2020-12-07 05:46:39 -05:00
|
|
|
false,
|
|
|
|
json!({
|
|
|
|
"target": "esnext",
|
|
|
|
"module": "esnext",
|
|
|
|
"noEmit": true,
|
|
|
|
}),
|
2021-03-10 05:39:16 -05:00
|
|
|
&[],
|
2020-12-07 05:46:39 -05:00
|
|
|
);
|
|
|
|
let ts_config = TsConfig::new(json!({
|
|
|
|
"target": "esnext",
|
|
|
|
"module": "esnext",
|
|
|
|
"noEmit": true,
|
|
|
|
"lib": ["deno.ns", "deno.worker"]
|
|
|
|
}));
|
|
|
|
let result = request(
|
|
|
|
&mut runtime,
|
2020-12-21 08:44:26 -05:00
|
|
|
state_snapshot,
|
2020-12-07 05:46:39 -05:00
|
|
|
RequestMethod::Configure(ts_config),
|
|
|
|
);
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let response = result.unwrap();
|
|
|
|
assert_eq!(response, json!(true));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-12-29 20:46:58 -05:00
|
|
|
fn test_get_diagnostics() {
|
2021-03-10 05:39:16 -05:00
|
|
|
let (mut runtime, state_snapshot, _) = setup(
|
2020-12-07 05:46:39 -05:00
|
|
|
false,
|
|
|
|
json!({
|
|
|
|
"target": "esnext",
|
|
|
|
"module": "esnext",
|
|
|
|
"noEmit": true,
|
|
|
|
}),
|
2021-06-02 06:29:58 -04:00
|
|
|
&[(
|
|
|
|
"file:///a.ts",
|
|
|
|
r#"console.log("hello deno");"#,
|
|
|
|
1,
|
|
|
|
LanguageId::TypeScript,
|
|
|
|
)],
|
2020-12-07 05:46:39 -05:00
|
|
|
);
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier = resolve_url("file:///a.ts").expect("could not resolve url");
|
2020-12-07 05:46:39 -05:00
|
|
|
let result = request(
|
|
|
|
&mut runtime,
|
2020-12-21 08:44:26 -05:00
|
|
|
state_snapshot,
|
2021-01-22 05:03:16 -05:00
|
|
|
RequestMethod::GetDiagnostics(vec![specifier]),
|
2020-12-07 05:46:39 -05:00
|
|
|
);
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let response = result.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
response,
|
2021-01-22 05:03:16 -05:00
|
|
|
json!({
|
|
|
|
"file:///a.ts": [
|
|
|
|
{
|
|
|
|
"start": {
|
|
|
|
"line": 0,
|
|
|
|
"character": 0,
|
|
|
|
},
|
|
|
|
"end": {
|
|
|
|
"line": 0,
|
|
|
|
"character": 7
|
|
|
|
},
|
|
|
|
"fileName": "file:///a.ts",
|
2021-05-27 19:33:11 -04:00
|
|
|
"messageText": "Cannot find name 'console'. Do you need to change your target library? Try changing the \'lib\' compiler option to include 'dom'.",
|
2021-01-22 05:03:16 -05:00
|
|
|
"sourceLine": "console.log(\"hello deno\");",
|
|
|
|
"category": 1,
|
|
|
|
"code": 2584
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
2020-12-07 05:46:39 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-02-12 06:49:42 -05:00
|
|
|
#[test]
|
|
|
|
fn test_get_diagnostics_lib() {
|
2021-03-10 05:39:16 -05:00
|
|
|
let (mut runtime, state_snapshot, _) = setup(
|
2021-02-12 06:49:42 -05:00
|
|
|
false,
|
|
|
|
json!({
|
|
|
|
"target": "esnext",
|
|
|
|
"module": "esnext",
|
|
|
|
"jsx": "react",
|
|
|
|
"lib": ["esnext", "dom", "deno.ns"],
|
|
|
|
"noEmit": true,
|
|
|
|
}),
|
2021-06-02 06:29:58 -04:00
|
|
|
&[(
|
|
|
|
"file:///a.ts",
|
|
|
|
r#"console.log(document.location);"#,
|
|
|
|
1,
|
|
|
|
LanguageId::TypeScript,
|
|
|
|
)],
|
2021-02-12 06:49:42 -05:00
|
|
|
);
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier = resolve_url("file:///a.ts").expect("could not resolve url");
|
2021-02-12 06:49:42 -05:00
|
|
|
let result = request(
|
|
|
|
&mut runtime,
|
|
|
|
state_snapshot,
|
|
|
|
RequestMethod::GetDiagnostics(vec![specifier]),
|
|
|
|
);
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let response = result.unwrap();
|
|
|
|
assert_eq!(response, json!({ "file:///a.ts": [] }));
|
|
|
|
}
|
|
|
|
|
2020-12-07 05:46:39 -05:00
|
|
|
#[test]
|
|
|
|
fn test_module_resolution() {
|
2021-03-10 05:39:16 -05:00
|
|
|
let (mut runtime, state_snapshot, _) = setup(
|
2020-12-07 05:46:39 -05:00
|
|
|
false,
|
|
|
|
json!({
|
|
|
|
"target": "esnext",
|
|
|
|
"module": "esnext",
|
|
|
|
"lib": ["deno.ns", "deno.window"],
|
|
|
|
"noEmit": true,
|
|
|
|
}),
|
2021-03-10 05:39:16 -05:00
|
|
|
&[(
|
2020-12-07 05:46:39 -05:00
|
|
|
"file:///a.ts",
|
|
|
|
r#"
|
|
|
|
import { B } from "https://deno.land/x/b/mod.ts";
|
|
|
|
|
|
|
|
const b = new B();
|
|
|
|
|
|
|
|
console.log(b);
|
|
|
|
"#,
|
|
|
|
1,
|
2021-06-02 06:29:58 -04:00
|
|
|
LanguageId::TypeScript,
|
2020-12-07 05:46:39 -05:00
|
|
|
)],
|
|
|
|
);
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier = resolve_url("file:///a.ts").expect("could not resolve url");
|
2020-12-07 05:46:39 -05:00
|
|
|
let result = request(
|
|
|
|
&mut runtime,
|
2020-12-21 08:44:26 -05:00
|
|
|
state_snapshot,
|
2021-01-22 05:03:16 -05:00
|
|
|
RequestMethod::GetDiagnostics(vec![specifier]),
|
2020-12-07 05:46:39 -05:00
|
|
|
);
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let response = result.unwrap();
|
2021-01-22 05:03:16 -05:00
|
|
|
assert_eq!(response, json!({ "file:///a.ts": [] }));
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_bad_module_specifiers() {
|
2021-03-10 05:39:16 -05:00
|
|
|
let (mut runtime, state_snapshot, _) = setup(
|
2020-12-07 05:46:39 -05:00
|
|
|
false,
|
|
|
|
json!({
|
|
|
|
"target": "esnext",
|
|
|
|
"module": "esnext",
|
|
|
|
"lib": ["deno.ns", "deno.window"],
|
|
|
|
"noEmit": true,
|
|
|
|
}),
|
2021-03-10 05:39:16 -05:00
|
|
|
&[(
|
2020-12-07 05:46:39 -05:00
|
|
|
"file:///a.ts",
|
|
|
|
r#"
|
|
|
|
import { A } from ".";
|
|
|
|
"#,
|
|
|
|
1,
|
2021-06-02 06:29:58 -04:00
|
|
|
LanguageId::TypeScript,
|
2020-12-07 05:46:39 -05:00
|
|
|
)],
|
|
|
|
);
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier = resolve_url("file:///a.ts").expect("could not resolve url");
|
2020-12-07 05:46:39 -05:00
|
|
|
let result = request(
|
|
|
|
&mut runtime,
|
2020-12-21 08:44:26 -05:00
|
|
|
state_snapshot,
|
2021-01-22 05:03:16 -05:00
|
|
|
RequestMethod::GetDiagnostics(vec![specifier]),
|
2020-12-07 05:46:39 -05:00
|
|
|
);
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let response = result.unwrap();
|
2020-12-29 20:46:58 -05:00
|
|
|
assert_eq!(
|
|
|
|
response,
|
2021-01-22 05:03:16 -05:00
|
|
|
json!({
|
|
|
|
"file:///a.ts": [{
|
|
|
|
"start": {
|
|
|
|
"line": 1,
|
|
|
|
"character": 8
|
|
|
|
},
|
|
|
|
"end": {
|
|
|
|
"line": 1,
|
|
|
|
"character": 30
|
|
|
|
},
|
|
|
|
"fileName": "file:///a.ts",
|
|
|
|
"messageText": "\'A\' is declared but its value is never read.",
|
|
|
|
"sourceLine": " import { A } from \".\";",
|
|
|
|
"category": 2,
|
|
|
|
"code": 6133,
|
|
|
|
"reportsUnnecessary": true,
|
|
|
|
}]
|
|
|
|
})
|
2020-12-29 20:46:58 -05:00
|
|
|
);
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_remote_modules() {
|
2021-03-10 05:39:16 -05:00
|
|
|
let (mut runtime, state_snapshot, _) = setup(
|
2020-12-07 05:46:39 -05:00
|
|
|
false,
|
|
|
|
json!({
|
|
|
|
"target": "esnext",
|
|
|
|
"module": "esnext",
|
|
|
|
"lib": ["deno.ns", "deno.window"],
|
|
|
|
"noEmit": true,
|
|
|
|
}),
|
2021-03-10 05:39:16 -05:00
|
|
|
&[(
|
2020-12-07 05:46:39 -05:00
|
|
|
"file:///a.ts",
|
|
|
|
r#"
|
|
|
|
import { B } from "https://deno.land/x/b/mod.ts";
|
|
|
|
|
|
|
|
const b = new B();
|
|
|
|
|
|
|
|
console.log(b);
|
|
|
|
"#,
|
|
|
|
1,
|
2021-06-02 06:29:58 -04:00
|
|
|
LanguageId::TypeScript,
|
2020-12-07 05:46:39 -05:00
|
|
|
)],
|
|
|
|
);
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier = resolve_url("file:///a.ts").expect("could not resolve url");
|
2020-12-07 05:46:39 -05:00
|
|
|
let result = request(
|
|
|
|
&mut runtime,
|
2020-12-21 08:44:26 -05:00
|
|
|
state_snapshot,
|
2021-01-22 05:03:16 -05:00
|
|
|
RequestMethod::GetDiagnostics(vec![specifier]),
|
2020-12-07 05:46:39 -05:00
|
|
|
);
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let response = result.unwrap();
|
2021-01-22 05:03:16 -05:00
|
|
|
assert_eq!(response, json!({ "file:///a.ts": [] }));
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_partial_modules() {
|
2021-03-10 05:39:16 -05:00
|
|
|
let (mut runtime, state_snapshot, _) = setup(
|
2020-12-07 05:46:39 -05:00
|
|
|
false,
|
|
|
|
json!({
|
|
|
|
"target": "esnext",
|
|
|
|
"module": "esnext",
|
|
|
|
"lib": ["deno.ns", "deno.window"],
|
|
|
|
"noEmit": true,
|
|
|
|
}),
|
2021-03-10 05:39:16 -05:00
|
|
|
&[(
|
2020-12-07 05:46:39 -05:00
|
|
|
"file:///a.ts",
|
|
|
|
r#"
|
|
|
|
import {
|
|
|
|
Application,
|
|
|
|
Context,
|
|
|
|
Router,
|
|
|
|
Status,
|
|
|
|
} from "https://deno.land/x/oak@v6.3.2/mod.ts";
|
2021-01-29 14:34:33 -05:00
|
|
|
|
2020-12-07 05:46:39 -05:00
|
|
|
import * as test from
|
|
|
|
"#,
|
|
|
|
1,
|
2021-06-02 06:29:58 -04:00
|
|
|
LanguageId::TypeScript,
|
2020-12-07 05:46:39 -05:00
|
|
|
)],
|
|
|
|
);
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier = resolve_url("file:///a.ts").expect("could not resolve url");
|
2020-12-07 05:46:39 -05:00
|
|
|
let result = request(
|
|
|
|
&mut runtime,
|
2020-12-21 08:44:26 -05:00
|
|
|
state_snapshot,
|
2021-01-22 05:03:16 -05:00
|
|
|
RequestMethod::GetDiagnostics(vec![specifier]),
|
2020-12-07 05:46:39 -05:00
|
|
|
);
|
2020-12-15 14:34:39 -05:00
|
|
|
assert!(result.is_ok());
|
|
|
|
let response = result.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
response,
|
2021-01-22 05:03:16 -05:00
|
|
|
json!({
|
|
|
|
"file:///a.ts": [{
|
|
|
|
"start": {
|
|
|
|
"line": 1,
|
|
|
|
"character": 8
|
|
|
|
},
|
|
|
|
"end": {
|
|
|
|
"line": 6,
|
|
|
|
"character": 55,
|
|
|
|
},
|
|
|
|
"fileName": "file:///a.ts",
|
|
|
|
"messageText": "All imports in import declaration are unused.",
|
|
|
|
"sourceLine": " import {",
|
|
|
|
"category": 2,
|
|
|
|
"code": 6192,
|
|
|
|
"reportsUnnecessary": true
|
|
|
|
}, {
|
|
|
|
"start": {
|
|
|
|
"line": 8,
|
|
|
|
"character": 29
|
|
|
|
},
|
|
|
|
"end": {
|
|
|
|
"line": 8,
|
|
|
|
"character": 29
|
|
|
|
},
|
|
|
|
"fileName": "file:///a.ts",
|
|
|
|
"messageText": "Expression expected.",
|
|
|
|
"sourceLine": " import * as test from",
|
|
|
|
"category": 1,
|
|
|
|
"code": 1109
|
|
|
|
}]
|
|
|
|
})
|
2020-12-15 14:34:39 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-12-29 20:46:58 -05:00
|
|
|
#[test]
|
|
|
|
fn test_no_debug_failure() {
|
2021-03-10 05:39:16 -05:00
|
|
|
let (mut runtime, state_snapshot, _) = setup(
|
2020-12-29 20:46:58 -05:00
|
|
|
false,
|
|
|
|
json!({
|
|
|
|
"target": "esnext",
|
|
|
|
"module": "esnext",
|
|
|
|
"lib": ["deno.ns", "deno.window"],
|
|
|
|
"noEmit": true,
|
|
|
|
}),
|
2021-06-02 06:29:58 -04:00
|
|
|
&[(
|
|
|
|
"file:///a.ts",
|
|
|
|
r#"const url = new URL("b.js", import."#,
|
|
|
|
1,
|
|
|
|
LanguageId::TypeScript,
|
|
|
|
)],
|
2020-12-29 20:46:58 -05:00
|
|
|
);
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier = resolve_url("file:///a.ts").expect("could not resolve url");
|
2020-12-29 20:46:58 -05:00
|
|
|
let result = request(
|
|
|
|
&mut runtime,
|
|
|
|
state_snapshot,
|
2021-01-22 05:03:16 -05:00
|
|
|
RequestMethod::GetDiagnostics(vec![specifier]),
|
2020-12-29 20:46:58 -05:00
|
|
|
);
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let response = result.unwrap();
|
2021-08-26 20:12:59 -04:00
|
|
|
assert_eq!(
|
|
|
|
response,
|
|
|
|
json!({
|
|
|
|
"file:///a.ts": [
|
|
|
|
{
|
|
|
|
"start": {
|
|
|
|
"line": 0,
|
|
|
|
"character": 35,
|
|
|
|
},
|
|
|
|
"end": {
|
|
|
|
"line": 0,
|
|
|
|
"character": 35
|
|
|
|
},
|
|
|
|
"fileName": "file:///a.ts",
|
|
|
|
"messageText": "Identifier expected.",
|
|
|
|
"sourceLine": "const url = new URL(\"b.js\", import.",
|
|
|
|
"category": 1,
|
|
|
|
"code": 1003,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
|
|
|
);
|
2020-12-29 20:46:58 -05:00
|
|
|
}
|
|
|
|
|
2020-12-15 14:34:39 -05:00
|
|
|
#[test]
|
|
|
|
fn test_request_asset() {
|
2021-03-10 05:39:16 -05:00
|
|
|
let (mut runtime, state_snapshot, _) = setup(
|
2020-12-15 14:34:39 -05:00
|
|
|
false,
|
|
|
|
json!({
|
|
|
|
"target": "esnext",
|
|
|
|
"module": "esnext",
|
|
|
|
"lib": ["deno.ns", "deno.window"],
|
|
|
|
"noEmit": true,
|
|
|
|
}),
|
2021-03-10 05:39:16 -05:00
|
|
|
&[],
|
2020-12-15 14:34:39 -05:00
|
|
|
);
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier =
|
|
|
|
resolve_url("asset:///lib.esnext.d.ts").expect("could not resolve url");
|
2020-12-21 08:44:26 -05:00
|
|
|
let result = request(
|
|
|
|
&mut runtime,
|
|
|
|
state_snapshot,
|
|
|
|
RequestMethod::GetAsset(specifier),
|
|
|
|
);
|
2020-12-15 14:34:39 -05:00
|
|
|
assert!(result.is_ok());
|
2020-12-21 08:44:26 -05:00
|
|
|
let response: Option<String> =
|
|
|
|
serde_json::from_value(result.unwrap()).unwrap();
|
2020-12-15 14:34:39 -05:00
|
|
|
assert!(response.is_some());
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
2021-03-10 05:39:16 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_modify_sources() {
|
|
|
|
let (mut runtime, state_snapshot, location) = setup(
|
2021-10-10 17:26:22 -04:00
|
|
|
false,
|
2021-03-10 05:39:16 -05:00
|
|
|
json!({
|
|
|
|
"target": "esnext",
|
|
|
|
"module": "esnext",
|
|
|
|
"lib": ["deno.ns", "deno.window"],
|
|
|
|
"noEmit": true,
|
|
|
|
}),
|
|
|
|
&[(
|
|
|
|
"file:///a.ts",
|
|
|
|
r#"
|
|
|
|
import * as a from "https://deno.land/x/example/a.ts";
|
|
|
|
if (a.a === "b") {
|
|
|
|
console.log("fail");
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
1,
|
2021-06-02 06:29:58 -04:00
|
|
|
LanguageId::TypeScript,
|
2021-03-10 05:39:16 -05:00
|
|
|
)],
|
|
|
|
);
|
|
|
|
let cache = HttpCache::new(&location);
|
|
|
|
let specifier_dep =
|
|
|
|
resolve_url("https://deno.land/x/example/a.ts").unwrap();
|
|
|
|
cache
|
|
|
|
.set(
|
|
|
|
&specifier_dep,
|
|
|
|
HeadersMap::default(),
|
|
|
|
b"export const b = \"b\";\n",
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let specifier = resolve_url("file:///a.ts").unwrap();
|
|
|
|
let result = request(
|
|
|
|
&mut runtime,
|
|
|
|
state_snapshot.clone(),
|
|
|
|
RequestMethod::GetDiagnostics(vec![specifier]),
|
|
|
|
);
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let response = result.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
response,
|
|
|
|
json!({
|
|
|
|
"file:///a.ts": [
|
|
|
|
{
|
|
|
|
"start": {
|
|
|
|
"line": 2,
|
|
|
|
"character": 16,
|
|
|
|
},
|
|
|
|
"end": {
|
|
|
|
"line": 2,
|
|
|
|
"character": 17
|
|
|
|
},
|
|
|
|
"fileName": "file:///a.ts",
|
|
|
|
"messageText": "Property \'a\' does not exist on type \'typeof import(\"https://deno.land/x/example/a\")\'.",
|
|
|
|
"sourceLine": " if (a.a === \"b\") {",
|
|
|
|
"code": 2339,
|
|
|
|
"category": 1,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
|
|
|
);
|
|
|
|
cache
|
|
|
|
.set(
|
|
|
|
&specifier_dep,
|
|
|
|
HeadersMap::default(),
|
|
|
|
b"export const b = \"b\";\n\nexport const a = \"b\";\n",
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let specifier = resolve_url("file:///a.ts").unwrap();
|
|
|
|
let result = request(
|
|
|
|
&mut runtime,
|
|
|
|
state_snapshot,
|
|
|
|
RequestMethod::GetDiagnostics(vec![specifier]),
|
|
|
|
);
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let response = result.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
response,
|
|
|
|
json!({
|
|
|
|
"file:///a.ts": []
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2021-03-15 18:01:41 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_completion_entry_filter_text() {
|
|
|
|
let fixture = CompletionEntry {
|
|
|
|
kind: ScriptElementKind::MemberVariableElement,
|
|
|
|
name: "['foo']".to_string(),
|
|
|
|
insert_text: Some("['foo']".to_string()),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
let actual = fixture.get_filter_text();
|
|
|
|
assert_eq!(actual, Some(".foo".to_string()));
|
2021-09-15 22:07:52 -04:00
|
|
|
|
|
|
|
let fixture = CompletionEntry {
|
|
|
|
kind: ScriptElementKind::MemberVariableElement,
|
|
|
|
name: "#abc".to_string(),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
let actual = fixture.get_filter_text();
|
|
|
|
assert_eq!(actual, Some("abc".to_string()));
|
|
|
|
|
|
|
|
let fixture = CompletionEntry {
|
|
|
|
kind: ScriptElementKind::MemberVariableElement,
|
|
|
|
name: "#abc".to_string(),
|
|
|
|
insert_text: Some("this.#abc".to_string()),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
let actual = fixture.get_filter_text();
|
|
|
|
assert_eq!(actual, Some("abc".to_string()));
|
2021-03-15 18:01:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_completions() {
|
|
|
|
let fixture = r#"
|
|
|
|
import { B } from "https://deno.land/x/b/mod.ts";
|
|
|
|
|
|
|
|
const b = new B();
|
|
|
|
|
|
|
|
console.
|
|
|
|
"#;
|
|
|
|
let line_index = LineIndex::new(fixture);
|
|
|
|
let position = line_index
|
|
|
|
.offset_tsc(lsp::Position {
|
|
|
|
line: 5,
|
|
|
|
character: 16,
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
let (mut runtime, state_snapshot, _) = setup(
|
|
|
|
false,
|
|
|
|
json!({
|
|
|
|
"target": "esnext",
|
|
|
|
"module": "esnext",
|
|
|
|
"lib": ["deno.ns", "deno.window"],
|
|
|
|
"noEmit": true,
|
|
|
|
}),
|
2021-06-02 06:29:58 -04:00
|
|
|
&[("file:///a.ts", fixture, 1, LanguageId::TypeScript)],
|
2021-03-15 18:01:41 -04:00
|
|
|
);
|
|
|
|
let specifier = resolve_url("file:///a.ts").expect("could not resolve url");
|
|
|
|
let result = request(
|
|
|
|
&mut runtime,
|
|
|
|
state_snapshot.clone(),
|
|
|
|
RequestMethod::GetDiagnostics(vec![specifier.clone()]),
|
|
|
|
);
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let result = request(
|
|
|
|
&mut runtime,
|
|
|
|
state_snapshot.clone(),
|
|
|
|
RequestMethod::GetCompletions((
|
|
|
|
specifier.clone(),
|
|
|
|
position,
|
|
|
|
GetCompletionsAtPositionOptions {
|
|
|
|
user_preferences: UserPreferences {
|
|
|
|
include_completions_with_insert_text: Some(true),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
trigger_character: Some(".".to_string()),
|
|
|
|
},
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let response: CompletionInfo =
|
|
|
|
serde_json::from_value(result.unwrap()).unwrap();
|
2021-05-01 06:41:38 -04:00
|
|
|
assert_eq!(response.entries.len(), 19);
|
2021-03-15 18:01:41 -04:00
|
|
|
let result = request(
|
|
|
|
&mut runtime,
|
|
|
|
state_snapshot,
|
|
|
|
RequestMethod::GetCompletionDetails(GetCompletionDetailsArgs {
|
|
|
|
specifier,
|
|
|
|
position,
|
|
|
|
name: "log".to_string(),
|
|
|
|
source: None,
|
|
|
|
data: None,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let response = result.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
response,
|
|
|
|
json!({
|
|
|
|
"name": "log",
|
|
|
|
"kindModifiers": "declare",
|
|
|
|
"kind": "method",
|
|
|
|
"displayParts": [
|
|
|
|
{
|
|
|
|
"text": "(",
|
|
|
|
"kind": "punctuation"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"text": "method",
|
|
|
|
"kind": "text"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"text": ")",
|
|
|
|
"kind": "punctuation"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"text": " ",
|
|
|
|
"kind": "space"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"text": "Console",
|
|
|
|
"kind": "interfaceName"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"text": ".",
|
|
|
|
"kind": "punctuation"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"text": "log",
|
|
|
|
"kind": "methodName"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"text": "(",
|
|
|
|
"kind": "punctuation"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"text": "...",
|
|
|
|
"kind": "punctuation"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"text": "data",
|
|
|
|
"kind": "parameterName"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"text": ":",
|
|
|
|
"kind": "punctuation"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"text": " ",
|
|
|
|
"kind": "space"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"text": "any",
|
|
|
|
"kind": "keyword"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"text": "[",
|
|
|
|
"kind": "punctuation"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"text": "]",
|
|
|
|
"kind": "punctuation"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"text": ")",
|
|
|
|
"kind": "punctuation"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"text": ":",
|
|
|
|
"kind": "punctuation"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"text": " ",
|
|
|
|
"kind": "space"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"text": "void",
|
|
|
|
"kind": "keyword"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"documentation": []
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|