erg/crates/els/command.rs
2023-05-10 12:23:12 +09:00

59 lines
2.2 KiB
Rust

use erg_compiler::varinfo::AbsLocation;
use serde_json::Value;
use erg_compiler::artifact::BuildRunnable;
use erg_compiler::hir::Expr;
use lsp_types::{Command, ExecuteCommandParams, Location, Url};
use crate::server::{send_log, ELSResult, Server};
use crate::util::{self, NormalizedUrl};
impl<Checker: BuildRunnable> Server<Checker> {
pub(crate) fn handle_execute_command(
&mut self,
params: ExecuteCommandParams,
) -> ELSResult<Option<Value>> {
send_log(format!("command requested: {}", params.command))?;
#[allow(clippy::match_single_binding)]
match &params.command[..] {
other => {
send_log(format!("unknown command: {other}"))?;
Ok(None)
}
}
}
pub(crate) fn gen_show_trait_impls_command(
&self,
trait_loc: AbsLocation,
) -> ELSResult<Option<Command>> {
let refs = self.get_refs_from_abs_loc(&trait_loc);
let filter = |loc: Location| {
let uri = NormalizedUrl::new(loc.uri.clone());
let token = self.file_cache.get_token(&uri, loc.range.start)?;
let opt_visitor = self.get_visitor(&uri);
let min_expr = opt_visitor
.as_ref()
.and_then(|visitor| visitor.get_min_expr(&token))?;
matches!(min_expr, Expr::ClassDef(_)).then_some(loc)
};
let impls = refs.into_iter().filter_map(filter).collect::<Vec<_>>();
let impl_len = impls.len();
let locations = serde_json::to_value(impls)?;
let Ok(uri) = trait_loc.module.ok_or(()).and_then(Url::from_file_path) else {
return Ok(None);
};
let uri = serde_json::to_value(uri)?;
let Some(position) = util::loc_to_pos(trait_loc.loc) else {
return Ok(None);
};
let position = serde_json::to_value(position)?;
Ok(Some(Command {
title: format!("{impl_len} implementations"),
// the command is defined in: https://github.com/erg-lang/vscode-erg/blob/20e6e2154b045ab56fedbc8769d03633acfd12e0/src/extension.ts#L92-L94
command: "erg.showReferences".to_string(),
arguments: Some(vec![uri, position, locations]),
}))
}
}