Export file referrer query as experimental/parentModule

See: https://github.com/microsoft/language-server-protocol/issues/1002
This commit is contained in:
oxalica 2023-02-17 22:42:56 +08:00
parent 87d007b2ab
commit d4e6677846
6 changed files with 55 additions and 1 deletions

View file

@ -0,0 +1,15 @@
use crate::{DefDatabase, FileId};
pub(crate) fn file_references(db: &dyn DefDatabase, file: FileId) -> Vec<FileId> {
let mut refs = db
.module_references(file)
.iter()
.copied()
.collect::<Vec<_>>();
refs.sort();
refs
}
pub(crate) fn file_referrers(db: &dyn DefDatabase, file: FileId) -> Vec<FileId> {
db.module_referrers(file).into_vec()
}

View file

@ -2,6 +2,7 @@ mod assists;
mod completion;
mod diagnostics;
mod expand_selection;
mod file_references;
mod goto_definition;
mod highlight_related;
mod hover;
@ -134,6 +135,8 @@ impl Analysis {
Cancelled::catch(|| f(&self.db))
}
//// LSP standard ////
pub fn expand_selection(&self, frange: FileRange) -> Cancellable<Option<Vec<TextRange>>> {
self.with_db(|db| expand_selection::expand_selection(db, frange))
}
@ -197,4 +200,14 @@ impl Analysis {
pub fn highlight_related(&self, fpos: FilePos) -> Cancellable<Vec<HlRelated>> {
self.with_db(|db| highlight_related::highlight_related(db, fpos).unwrap_or_default())
}
//// Custom extensions ////
pub fn file_references(&self, file: FileId) -> Cancellable<Vec<FileId>> {
self.with_db(|db| file_references::file_references(db, file))
}
pub fn file_referrers(&self, file: FileId) -> Cancellable<Vec<FileId>> {
self.with_db(|db| file_references::file_referrers(db, file))
}
}

View file

@ -330,3 +330,17 @@ pub(crate) fn document_highlight(
let ret = convert::to_document_highlight(&line_map, &ret);
Ok(Some(ret))
}
pub(crate) fn parent_module(
snap: StateSnapshot,
params: TextDocumentPositionParams,
) -> Result<Option<GotoDefinitionResponse>> {
let (file, _) = convert::from_file(&snap.vfs(), &params.text_document)?;
let files = snap.analysis.file_referrers(file)?;
let vfs = snap.vfs();
let locs = files
.into_iter()
.map(|file| convert::to_location(&vfs, FileRange::new(file, TextRange::default())))
.collect();
Ok(Some(GotoDefinitionResponse::Array(locs)))
}

View file

@ -2,6 +2,7 @@ mod capabilities;
mod config;
mod convert;
mod handler;
mod lsp_ext;
mod semantic_tokens;
mod server;
mod vfs;

10
crates/nil/src/lsp_ext.rs Normal file
View file

@ -0,0 +1,10 @@
use lsp_types::request::Request;
/// https://github.com/microsoft/language-server-protocol/issues/1002
pub enum ParentModule {}
impl Request for ParentModule {
type Params = lsp_types::TextDocumentPositionParams;
type Result = Option<lsp_types::GotoDefinitionResponse>;
const METHOD: &'static str = "experimental/parentModule";
}

View file

@ -1,5 +1,5 @@
use crate::config::{Config, CONFIG_KEY};
use crate::{convert, handler, LspError, UrlExt, Vfs, MAX_FILE_LEN};
use crate::{convert, handler, lsp_ext, LspError, UrlExt, Vfs, MAX_FILE_LEN};
use anyhow::{anyhow, bail, Context, Result};
use crossbeam_channel::{Receiver, Sender};
use ide::{Analysis, AnalysisHost, Cancelled, FlakeInfo, VfsPath};
@ -273,6 +273,7 @@ impl Server {
.on::<req::DocumentLinkRequest>(handler::document_links)
.on::<req::CodeActionRequest>(handler::code_action)
.on::<req::DocumentHighlightRequest>(handler::document_highlight)
.on::<lsp_ext::ParentModule>(handler::parent_module)
.finish();
}