mirror of
https://github.com/Myriad-Dreamin/tinymist.git
synced 2025-07-24 13:13:43 +00:00
feat: associate request function
This commit is contained in:
parent
0c7e67ed9a
commit
50ca444915
9 changed files with 215 additions and 206 deletions
|
@ -7,20 +7,23 @@ pub struct CompletionRequest {
|
|||
pub explicit: bool,
|
||||
}
|
||||
|
||||
pub fn completion(
|
||||
impl CompletionRequest {
|
||||
pub fn request(
|
||||
self,
|
||||
world: &TypstSystemWorld,
|
||||
doc: Option<Arc<TypstDocument>>,
|
||||
req: CompletionRequest,
|
||||
position_encoding: PositionEncoding,
|
||||
) -> Option<CompletionResponse> {
|
||||
let source = get_suitable_source_in_workspace(world, &req.path).ok()?;
|
||||
let typst_offset = lsp_to_typst::position_to_offset(req.position, position_encoding, &source);
|
||||
let source = get_suitable_source_in_workspace(world, &self.path).ok()?;
|
||||
let typst_offset =
|
||||
lsp_to_typst::position_to_offset(self.position, position_encoding, &source);
|
||||
|
||||
let (typst_start_offset, completions) =
|
||||
typst_ide::autocomplete(world, doc.as_deref(), &source, typst_offset, req.explicit)?;
|
||||
typst_ide::autocomplete(world, doc.as_deref(), &source, typst_offset, self.explicit)?;
|
||||
|
||||
let lsp_start_position =
|
||||
typst_to_lsp::offset_to_position(typst_start_offset, position_encoding, &source);
|
||||
let replace_range = LspRawRange::new(lsp_start_position, req.position);
|
||||
let replace_range = LspRawRange::new(lsp_start_position, self.position);
|
||||
Some(typst_to_lsp::completions(&completions, replace_range).into())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,18 +5,20 @@ pub struct DocumentSymbolRequest {
|
|||
pub path: PathBuf,
|
||||
}
|
||||
|
||||
pub fn document_symbol(
|
||||
impl DocumentSymbolRequest {
|
||||
pub fn request(
|
||||
self,
|
||||
world: &TypstSystemWorld,
|
||||
req: DocumentSymbolRequest,
|
||||
position_encoding: PositionEncoding,
|
||||
) -> Option<DocumentSymbolResponse> {
|
||||
let source = get_suitable_source_in_workspace(world, &req.path).ok()?;
|
||||
let source = get_suitable_source_in_workspace(world, &self.path).ok()?;
|
||||
|
||||
let uri = Url::from_file_path(req.path).unwrap();
|
||||
let uri = Url::from_file_path(self.path).unwrap();
|
||||
let symbols = get_document_symbols(source, uri, position_encoding);
|
||||
|
||||
symbols.map(DocumentSymbolResponse::Flat)
|
||||
}
|
||||
}
|
||||
|
||||
#[comemo::memoize]
|
||||
pub(crate) fn get_document_symbols(
|
||||
|
|
|
@ -6,14 +6,16 @@ pub struct HoverRequest {
|
|||
pub position: LspPosition,
|
||||
}
|
||||
|
||||
pub fn hover(
|
||||
impl HoverRequest {
|
||||
pub fn request(
|
||||
self,
|
||||
world: &TypstSystemWorld,
|
||||
doc: Option<Arc<TypstDocument>>,
|
||||
req: HoverRequest,
|
||||
position_encoding: PositionEncoding,
|
||||
) -> Option<Hover> {
|
||||
let source = get_suitable_source_in_workspace(world, &req.path).ok()?;
|
||||
let typst_offset = lsp_to_typst::position_to_offset(req.position, position_encoding, &source);
|
||||
let source = get_suitable_source_in_workspace(world, &self.path).ok()?;
|
||||
let typst_offset =
|
||||
lsp_to_typst::position_to_offset(self.position, position_encoding, &source);
|
||||
|
||||
let typst_tooltip = typst_ide::tooltip(world, doc.as_deref(), &source, typst_offset)?;
|
||||
|
||||
|
@ -25,3 +27,4 @@ pub fn hover(
|
|||
range: Some(range.raw_range),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,16 +6,18 @@ pub struct SelectionRangeRequest {
|
|||
pub positions: Vec<LspPosition>,
|
||||
}
|
||||
|
||||
pub fn selection_range(
|
||||
impl SelectionRangeRequest {
|
||||
pub fn request(
|
||||
self,
|
||||
world: &TypstSystemWorld,
|
||||
req: SelectionRangeRequest,
|
||||
position_encoding: PositionEncoding,
|
||||
) -> Option<Vec<SelectionRange>> {
|
||||
let source = get_suitable_source_in_workspace(world, &req.path).ok()?;
|
||||
let source = get_suitable_source_in_workspace(world, &self.path).ok()?;
|
||||
|
||||
let mut ranges = Vec::new();
|
||||
for position in req.positions {
|
||||
let typst_offset = lsp_to_typst::position_to_offset(position, position_encoding, &source);
|
||||
for position in self.positions {
|
||||
let typst_offset =
|
||||
lsp_to_typst::position_to_offset(position, position_encoding, &source);
|
||||
let tree = LinkedNode::new(source.root());
|
||||
let leaf = tree.leaf_at(typst_offset)?;
|
||||
ranges.push(range_for_node(&source, position_encoding, &leaf));
|
||||
|
@ -23,6 +25,7 @@ pub fn selection_range(
|
|||
|
||||
Some(ranges)
|
||||
}
|
||||
}
|
||||
|
||||
fn range_for_node(
|
||||
source: &Source,
|
||||
|
|
|
@ -6,15 +6,16 @@ pub struct SemanticTokensDeltaRequest {
|
|||
pub previous_result_id: String,
|
||||
}
|
||||
|
||||
pub fn semantic_tokens_delta(
|
||||
impl SemanticTokensDeltaRequest {
|
||||
pub fn request(
|
||||
self,
|
||||
cache: &SemanticTokenCache,
|
||||
source: Source,
|
||||
req: SemanticTokensDeltaRequest,
|
||||
position_encoding: PositionEncoding,
|
||||
) -> Option<SemanticTokensFullDeltaResult> {
|
||||
let (tokens, result_id) = cache.try_semantic_tokens_delta_from_result_id(
|
||||
&source,
|
||||
&req.previous_result_id,
|
||||
&self.previous_result_id,
|
||||
position_encoding,
|
||||
);
|
||||
|
||||
|
@ -35,3 +36,4 @@ pub fn semantic_tokens_delta(
|
|||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,10 +5,11 @@ pub struct SemanticTokensFullRequest {
|
|||
pub path: PathBuf,
|
||||
}
|
||||
|
||||
pub fn semantic_tokens_full(
|
||||
impl SemanticTokensFullRequest {
|
||||
pub fn request(
|
||||
self,
|
||||
cache: &SemanticTokenCache,
|
||||
source: Source,
|
||||
_req: SemanticTokensFullRequest,
|
||||
position_encoding: PositionEncoding,
|
||||
) -> Option<SemanticTokensResult> {
|
||||
let (tokens, result_id) = cache.get_semantic_tokens_full(&source, position_encoding);
|
||||
|
@ -21,3 +22,4 @@ pub fn semantic_tokens_full(
|
|||
.into(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,13 +6,15 @@ pub struct SignatureHelpRequest {
|
|||
pub position: LspPosition,
|
||||
}
|
||||
|
||||
pub fn signature_help(
|
||||
impl SignatureHelpRequest {
|
||||
pub fn request(
|
||||
self,
|
||||
world: &TypstSystemWorld,
|
||||
SignatureHelpRequest { path, position }: SignatureHelpRequest,
|
||||
position_encoding: PositionEncoding,
|
||||
) -> Option<SignatureHelp> {
|
||||
let source = get_suitable_source_in_workspace(world, &path).ok()?;
|
||||
let typst_offset = lsp_to_typst::position_to_offset(position, position_encoding, &source);
|
||||
let source = get_suitable_source_in_workspace(world, &self.path).ok()?;
|
||||
let typst_offset =
|
||||
lsp_to_typst::position_to_offset(self.position, position_encoding, &source);
|
||||
|
||||
let ast_node = LinkedNode::new(source.root()).leaf_at(typst_offset)?;
|
||||
let (callee, callee_node, args) = surrounding_function_syntax(&ast_node)?;
|
||||
|
@ -74,6 +76,7 @@ pub fn signature_help(
|
|||
active_parameter: None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn surrounding_function_syntax<'b>(
|
||||
leaf: &'b LinkedNode,
|
||||
|
|
|
@ -8,9 +8,10 @@ pub struct SymbolRequest {
|
|||
pub pattern: Option<String>,
|
||||
}
|
||||
|
||||
pub fn symbol(
|
||||
impl SymbolRequest {
|
||||
pub fn request(
|
||||
self,
|
||||
world: &TypstSystemWorld,
|
||||
SymbolRequest { pattern }: SymbolRequest,
|
||||
position_encoding: PositionEncoding,
|
||||
) -> Option<Vec<SymbolInformation>> {
|
||||
// todo: expose source
|
||||
|
@ -23,7 +24,7 @@ pub fn symbol(
|
|||
};
|
||||
let uri = Url::from_file_path(path).unwrap();
|
||||
let res = get_document_symbols(source, uri, position_encoding).and_then(|symbols| {
|
||||
pattern
|
||||
self.pattern
|
||||
.as_ref()
|
||||
.map(|pattern| filter_document_symbols(symbols, pattern))
|
||||
});
|
||||
|
@ -35,6 +36,7 @@ pub fn symbol(
|
|||
|
||||
Some(symbols)
|
||||
}
|
||||
}
|
||||
|
||||
fn filter_document_symbols(
|
||||
symbols: Vec<SymbolInformation>,
|
||||
|
|
|
@ -7,7 +7,9 @@ use std::{
|
|||
use anyhow::anyhow;
|
||||
use futures::future::join_all;
|
||||
use log::{error, trace, warn};
|
||||
use tinymist_query::{LspDiagnostic, LspRange, PositionEncoding, SemanticTokenCache};
|
||||
use tinymist_query::{
|
||||
DiagnosticsMap, LspDiagnostic, LspRange, PositionEncoding, SemanticTokenCache,
|
||||
};
|
||||
use tokio::sync::{broadcast, mpsc, watch, Mutex, RwLock};
|
||||
use tower_lsp::lsp_types::{
|
||||
CompletionResponse, DocumentSymbolResponse, Hover, SelectionRange,
|
||||
|
@ -42,9 +44,6 @@ type CompileService<H> = CompileActor<Reporter<CompileExporter<CompileDriver>, H
|
|||
type CompileClient<H> = TsCompileClient<CompileService<H>>;
|
||||
|
||||
type DiagnosticsSender = mpsc::UnboundedSender<(String, DiagnosticsMap)>;
|
||||
type DiagnosticsMap = HashMap<Url, Vec<LspDiagnostic>>;
|
||||
|
||||
// type Client = TypstClient<CompileHandler>;
|
||||
|
||||
pub struct CompileCluster {
|
||||
position_encoding: PositionEncoding,
|
||||
|
@ -65,7 +64,7 @@ pub fn create_cluster(
|
|||
let primary = create_server(
|
||||
"primary".to_owned(),
|
||||
cfg,
|
||||
create_compiler(roots.clone(), opts.clone()),
|
||||
CompileDriver::new(roots.clone(), opts.clone()),
|
||||
diag_tx,
|
||||
);
|
||||
|
||||
|
@ -83,11 +82,6 @@ pub fn create_cluster(
|
|||
}
|
||||
}
|
||||
|
||||
fn create_compiler(roots: Vec<PathBuf>, opts: CompileOpts) -> CompileDriver {
|
||||
let world = TypstSystemWorld::new(opts).expect("incorrect options");
|
||||
CompileDriver::new(world, roots)
|
||||
}
|
||||
|
||||
fn create_server(
|
||||
diag_group: String,
|
||||
cfg: &ConstConfig,
|
||||
|
@ -323,31 +317,31 @@ pub enum CompilerQueryResponse {
|
|||
}
|
||||
|
||||
macro_rules! query_state {
|
||||
($self:ident, $method:ident, $query:expr, $req:expr) => {{
|
||||
($self:ident, $method:ident, $req:expr) => {{
|
||||
let doc = $self.handler.result.lock().unwrap().clone().ok();
|
||||
let enc = $self.position_encoding;
|
||||
let res = $self.steal_world(move |w| $query(w, doc, $req, enc)).await;
|
||||
let res = $self.steal_world(move |w| $req.request(w, doc, enc)).await;
|
||||
res.map(CompilerQueryResponse::$method)
|
||||
}};
|
||||
}
|
||||
|
||||
macro_rules! query_world {
|
||||
($self:ident, $method:ident, $query:expr, $req:expr) => {{
|
||||
($self:ident, $method:ident, $req:expr) => {{
|
||||
let enc = $self.position_encoding;
|
||||
let res = $self.steal_world(move |w| $query(w, $req, enc)).await;
|
||||
let res = $self.steal_world(move |w| $req.request(w, enc)).await;
|
||||
res.map(CompilerQueryResponse::$method)
|
||||
}};
|
||||
}
|
||||
|
||||
macro_rules! query_tokens_cache {
|
||||
($self:ident, $method:ident, $query:expr, $req:expr) => {{
|
||||
($self:ident, $method:ident, $req:expr) => {{
|
||||
let path: ImmutPath = $req.path.clone().into();
|
||||
let vfs = $self.memory_changes.read().await;
|
||||
let snapshot = vfs.get(&path).ok_or_else(|| anyhow!("file missing"))?;
|
||||
let source = snapshot.content.clone();
|
||||
|
||||
let enc = $self.position_encoding;
|
||||
let res = $query(&$self.tokens_cache, source, $req, enc);
|
||||
let res = $req.request(&$self.tokens_cache, source, enc);
|
||||
Ok(CompilerQueryResponse::$method(res))
|
||||
}};
|
||||
}
|
||||
|
@ -357,16 +351,11 @@ impl CompileCluster {
|
|||
&self,
|
||||
query: CompilerQueryRequest,
|
||||
) -> anyhow::Result<CompilerQueryResponse> {
|
||||
use tinymist_query::*;
|
||||
use CompilerQueryRequest::*;
|
||||
|
||||
match query {
|
||||
SemanticTokensFull(req) => {
|
||||
query_tokens_cache!(self, SemanticTokensFull, semantic_tokens_full, req)
|
||||
}
|
||||
SemanticTokensDelta(req) => {
|
||||
query_tokens_cache!(self, SemanticTokensDelta, semantic_tokens_delta, req)
|
||||
}
|
||||
SemanticTokensFull(req) => query_tokens_cache!(self, SemanticTokensFull, req),
|
||||
SemanticTokensDelta(req) => query_tokens_cache!(self, SemanticTokensDelta, req),
|
||||
_ => self.primary.query(query).await,
|
||||
}
|
||||
}
|
||||
|
@ -415,7 +404,8 @@ impl CompileMiddleware for CompileDriver {
|
|||
}
|
||||
|
||||
impl CompileDriver {
|
||||
fn new(world: TypstSystemWorld, roots: Vec<PathBuf>) -> Self {
|
||||
fn new(roots: Vec<PathBuf>, opts: CompileOpts) -> Self {
|
||||
let world = TypstSystemWorld::new(opts).expect("incorrect options");
|
||||
let driver = CompileDriverInner::new(world);
|
||||
|
||||
Self {
|
||||
|
@ -679,7 +669,6 @@ impl<H: CompilationHandle> CompileNode<H> {
|
|||
&self,
|
||||
query: CompilerQueryRequest,
|
||||
) -> anyhow::Result<CompilerQueryResponse> {
|
||||
use tinymist_query::*;
|
||||
use CompilerQueryRequest::*;
|
||||
|
||||
match query {
|
||||
|
@ -687,12 +676,12 @@ impl<H: CompilationHandle> CompileNode<H> {
|
|||
self.on_save_export(path).await?;
|
||||
Ok(CompilerQueryResponse::OnSaveExport(()))
|
||||
}
|
||||
Hover(req) => query_state!(self, Hover, hover, req),
|
||||
Completion(req) => query_state!(self, Completion, completion, req),
|
||||
SignatureHelp(req) => query_world!(self, SignatureHelp, signature_help, req),
|
||||
DocumentSymbol(req) => query_world!(self, DocumentSymbol, document_symbol, req),
|
||||
Symbol(req) => query_world!(self, Symbol, symbol, req),
|
||||
SelectionRange(req) => query_world!(self, SelectionRange, selection_range, req),
|
||||
Hover(req) => query_state!(self, Hover, req),
|
||||
Completion(req) => query_state!(self, Completion, req),
|
||||
SignatureHelp(req) => query_world!(self, SignatureHelp, req),
|
||||
DocumentSymbol(req) => query_world!(self, DocumentSymbol, req),
|
||||
Symbol(req) => query_world!(self, Symbol, req),
|
||||
SelectionRange(req) => query_world!(self, SelectionRange, req),
|
||||
CompilerQueryRequest::SemanticTokensDelta(..)
|
||||
| CompilerQueryRequest::SemanticTokensFull(..) => unreachable!(),
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue