mirror of
https://github.com/Myriad-Dreamin/tinymist.git
synced 2025-11-23 12:46:43 +00:00
feat: make symbol queries infallible (#1964)
This commit is contained in:
parent
2ecdbd42a5
commit
ab774377f7
4 changed files with 17 additions and 17 deletions
|
|
@ -1,5 +1,3 @@
|
|||
use lsp_types::SymbolKind;
|
||||
|
||||
use crate::{
|
||||
prelude::*,
|
||||
syntax::{get_lexical_hierarchy, LexicalHierarchy, LexicalScopeKind},
|
||||
|
|
@ -46,14 +44,14 @@ fn symbols_in_hierarchy(
|
|||
) -> Vec<DocumentSymbol> {
|
||||
hierarchy
|
||||
.iter()
|
||||
.filter(|hierarchy| TryInto::<SymbolKind>::try_into(hierarchy.info.kind.clone()).is_ok())
|
||||
.filter(|hierarchy| hierarchy.info.kind.is_valid_lsp_symbol())
|
||||
.map(|hierarchy| {
|
||||
let range = to_lsp_range(hierarchy.info.range.clone(), source, position_encoding);
|
||||
|
||||
DocumentSymbol {
|
||||
name: hierarchy.info.name.to_string(),
|
||||
detail: None,
|
||||
kind: hierarchy.info.kind.clone().try_into().unwrap(),
|
||||
kind: hierarchy.info.kind.clone().into(),
|
||||
tags: None,
|
||||
deprecated: None,
|
||||
range,
|
||||
|
|
|
|||
|
|
@ -32,8 +32,6 @@ impl SemanticRequest for SymbolRequest {
|
|||
type Response = Vec<SymbolInformation>;
|
||||
|
||||
fn request(self, ctx: &mut LocalContext) -> Option<Self::Response> {
|
||||
// todo: let typst.ts expose source
|
||||
|
||||
let mut symbols = vec![];
|
||||
|
||||
for id in ctx.depended_files() {
|
||||
|
|
@ -75,6 +73,7 @@ fn filter_document_symbols(
|
|||
.into_iter()
|
||||
.chain(hierarchy.children.as_deref().into_iter().flatten())
|
||||
})
|
||||
.filter(|hierarchy| hierarchy.info.kind.is_valid_lsp_symbol())
|
||||
.flat_map(|hierarchy| {
|
||||
if query_string.is_some_and(|s| !hierarchy.info.name.contains(s)) {
|
||||
return None;
|
||||
|
|
@ -84,7 +83,7 @@ fn filter_document_symbols(
|
|||
|
||||
Some(SymbolInformation {
|
||||
name: hierarchy.info.name.to_string(),
|
||||
kind: hierarchy.info.kind.clone().try_into().unwrap(),
|
||||
kind: hierarchy.info.kind.clone().into(),
|
||||
tags: None,
|
||||
deprecated: None,
|
||||
location: LspLocation {
|
||||
|
|
|
|||
|
|
@ -89,18 +89,21 @@ impl LexicalKind {
|
|||
const fn variable() -> LexicalKind {
|
||||
LexicalKind::Var(LexicalVarKind::Variable)
|
||||
}
|
||||
|
||||
pub fn is_valid_lsp_symbol(&self) -> bool {
|
||||
!matches!(self, LexicalKind::Block | LexicalKind::CommentGroup)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<LexicalKind> for SymbolKind {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(value: LexicalKind) -> Result<Self, Self::Error> {
|
||||
impl From<LexicalKind> for SymbolKind {
|
||||
fn from(value: LexicalKind) -> Self {
|
||||
use LexicalVarKind::*;
|
||||
match value {
|
||||
LexicalKind::Heading(..) => Ok(SymbolKind::NAMESPACE),
|
||||
LexicalKind::Var(LexicalVarKind::Variable) => Ok(SymbolKind::VARIABLE),
|
||||
LexicalKind::Var(LexicalVarKind::Function) => Ok(SymbolKind::FUNCTION),
|
||||
LexicalKind::Var(LexicalVarKind::Label) => Ok(SymbolKind::CONSTANT),
|
||||
LexicalKind::Var(..) | LexicalKind::Block | LexicalKind::CommentGroup => Err(()),
|
||||
LexicalKind::Heading(..) => SymbolKind::NAMESPACE,
|
||||
LexicalKind::Var(ValRef | Variable) => SymbolKind::VARIABLE,
|
||||
LexicalKind::Var(Function) => SymbolKind::FUNCTION,
|
||||
LexicalKind::Var(LabelRef | Label | BibKey) => SymbolKind::CONSTANT,
|
||||
LexicalKind::Block | LexicalKind::CommentGroup => SymbolKind::CONSTANT,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -64,7 +64,7 @@ fn filter_document_labels(
|
|||
|
||||
Some(SymbolInformation {
|
||||
name: hierarchy.info.name.to_string(),
|
||||
kind: hierarchy.info.kind.clone().try_into().unwrap(),
|
||||
kind: hierarchy.info.kind.clone().into(),
|
||||
tags: None,
|
||||
deprecated: None,
|
||||
location: LspLocation {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue