feat: support inlay hints (#16287)

Closes: #11853
This commit is contained in:
Kitson Kelly 2022-10-16 13:39:43 +11:00 committed by GitHub
parent 6d2656fd56
commit 7d78f58187
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 628 additions and 2 deletions

View file

@ -196,6 +196,13 @@ impl LanguageServer {
}
}
pub async fn inlay_hint(
&self,
params: InlayHintParams,
) -> LspResult<Option<Vec<InlayHint>>> {
self.0.lock().await.inlay_hint(params).await
}
pub async fn virtual_text_document(
&self,
params: Option<Value>,
@ -2896,6 +2903,50 @@ impl Inner {
)
}
async fn inlay_hint(
&self,
params: InlayHintParams,
) -> LspResult<Option<Vec<InlayHint>>> {
let specifier = self.url_map.normalize_url(&params.text_document.uri);
let workspace_settings = self.config.get_workspace_settings();
if !self.is_diagnosable(&specifier)
|| !self.config.specifier_enabled(&specifier)
|| !workspace_settings.enabled_inlay_hints()
{
return Ok(None);
}
let mark = self.performance.mark("inlay_hint", Some(&params));
let asset_or_doc = self.get_asset_or_document(&specifier)?;
let line_index = asset_or_doc.line_index();
let range = tsc::TextSpan::from_range(&params.range, line_index.clone())
.map_err(|err| {
error!("Failed to convert range to text_span: {}", err);
LspError::internal_error()
})?;
let req = tsc::RequestMethod::ProvideInlayHints((
specifier.clone(),
range,
(&workspace_settings).into(),
));
let maybe_inlay_hints: Option<Vec<tsc::InlayHint>> = self
.ts_server
.request(self.snapshot(), req)
.await
.map_err(|err| {
error!("Unable to get inlay hints: {}", err);
LspError::internal_error()
})?;
let maybe_inlay_hints = maybe_inlay_hints.map(|hints| {
hints
.iter()
.map(|hint| hint.to_lsp(line_index.clone()))
.collect()
});
self.performance.measure(mark);
Ok(maybe_inlay_hints)
}
async fn reload_import_registries(&mut self) -> LspResult<Option<Value>> {
fs_util::remove_dir_all_if_exists(&self.module_registries_location)
.await