mirror of
https://github.com/erg-lang/erg.git
synced 2025-08-04 18:58:30 +00:00
chore(els): improve doc highlight, type definition
This commit is contained in:
parent
a47271eb00
commit
23142afb81
2 changed files with 27 additions and 9 deletions
|
@ -31,13 +31,13 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
|
|||
if let Some(range) = loc_to_range(vi.def_loc.loc) {
|
||||
res.push(DocumentHighlight {
|
||||
range,
|
||||
kind: Some(DocumentHighlightKind::TEXT),
|
||||
kind: Some(DocumentHighlightKind::WRITE),
|
||||
});
|
||||
}
|
||||
for reference in self.get_refs_from_abs_loc(&vi.def_loc) {
|
||||
res.push(DocumentHighlight {
|
||||
range: reference.range,
|
||||
kind: Some(DocumentHighlightKind::TEXT),
|
||||
kind: Some(DocumentHighlightKind::READ),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
use std::path::Path;
|
||||
|
||||
use erg_common::set::Set;
|
||||
use erg_common::shared::MappedRwLockReadGuard;
|
||||
use erg_compiler::artifact::BuildRunnable;
|
||||
use erg_compiler::erg_parser::parse::Parsable;
|
||||
|
||||
use erg_compiler::ty::{HasType, Type};
|
||||
use lsp_types::request::{GotoTypeDefinitionParams, GotoTypeDefinitionResponse};
|
||||
use lsp_types::{GotoDefinitionResponse, Position, Url};
|
||||
use lsp_types::{GotoDefinitionResponse, Location, Position, Url};
|
||||
|
||||
use crate::server::{ELSResult, RedirectableStdout, Server};
|
||||
use crate::util::{self, NormalizedUrl};
|
||||
|
@ -21,10 +23,7 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
|
|||
Ok(self.get_type_def(&uri, pos))
|
||||
}
|
||||
|
||||
fn get_type_def(&self, uri: &NormalizedUrl, pos: Position) -> Option<GotoDefinitionResponse> {
|
||||
let visitor = self.get_visitor(uri)?;
|
||||
let tok = self.file_cache.get_symbol(uri, pos)?;
|
||||
let typ = &visitor.get_info(&tok)?.t;
|
||||
fn make_type_definition(&self, uri: &NormalizedUrl, typ: &Type) -> Option<Location> {
|
||||
let path = typ.namespace();
|
||||
let module = self
|
||||
.shared
|
||||
|
@ -34,7 +33,26 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
|
|||
let (_, typ_info) = module.context.get_type_info(typ)?;
|
||||
let path = typ_info.def_loc.module.as_ref()?;
|
||||
let def_uri = Url::from_file_path(path).ok()?;
|
||||
let loc = lsp_types::Location::new(def_uri, util::loc_to_range(typ_info.def_loc.loc)?);
|
||||
Some(GotoDefinitionResponse::Scalar(loc))
|
||||
Some(Location::new(
|
||||
def_uri,
|
||||
util::loc_to_range(typ_info.def_loc.loc)?,
|
||||
))
|
||||
}
|
||||
|
||||
fn get_type_def(&self, uri: &NormalizedUrl, pos: Position) -> Option<GotoDefinitionResponse> {
|
||||
let mut locs = vec![];
|
||||
let visitor = self.get_visitor(uri)?;
|
||||
let tok = self.file_cache.get_symbol(uri, pos)?;
|
||||
let typ = &visitor.get_info(&tok)?.t;
|
||||
if let Some(loc) = self.make_type_definition(uri, typ) {
|
||||
locs.push(loc);
|
||||
}
|
||||
let typs = typ.inner_ts().into_iter().collect::<Set<_>>();
|
||||
for typ in typs {
|
||||
if let Some(loc) = self.make_type_definition(uri, &typ) {
|
||||
locs.push(loc);
|
||||
}
|
||||
}
|
||||
Some(GotoDefinitionResponse::Array(locs))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue