Skip to content

Commit 1d1f4bf

Browse files
dev: reduce three marco uses (#393)
1 parent 1662d58 commit 1d1f4bf

File tree

8 files changed

+42
-50
lines changed

8 files changed

+42
-50
lines changed

crates/sync-lsp/src/lib.rs

+16-26
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,20 @@ pub type QueryFuture = anyhow::Result<ResponseFuture<anyhow::Result<CompilerQuer
3535
pub type SchedulableResponse<T> = LspResponseFuture<LspResult<T>>;
3636
pub type AnySchedulableResponse = SchedulableResponse<JsonValue>;
3737

38-
#[macro_export]
39-
macro_rules! just_ok {
40-
($expr:expr) => {
41-
Ok(futures::future::MaybeDone::Done(Ok($expr)))
42-
};
38+
pub fn just_ok<T, E>(res: T) -> Result<ResponseFuture<Result<T, E>>, E> {
39+
Ok(futures::future::MaybeDone::Done(Ok(res)))
4340
}
4441

45-
#[macro_export]
46-
macro_rules! just_result {
47-
($expr:expr) => {
48-
Ok(futures::future::MaybeDone::Done($expr))
49-
};
42+
pub fn just_result<T, E>(res: Result<T, E>) -> Result<ResponseFuture<Result<T, E>>, E> {
43+
Ok(futures::future::MaybeDone::Done(res))
5044
}
5145

52-
#[macro_export]
53-
macro_rules! just_future {
54-
($expr:expr) => {
55-
Ok(futures::future::MaybeDone::Future(Box::pin($expr)))
56-
};
46+
pub fn just_future<T, E>(
47+
fut: impl std::future::Future<Output = Result<T, E>> + Send + 'static,
48+
) -> Result<ResponseFuture<Result<T, E>>, E> {
49+
Ok(futures::future::MaybeDone::Future(Box::pin(fut)))
5750
}
5851

59-
#[macro_export]
6052
macro_rules! reschedule {
6153
($expr:expr) => {
6254
match $expr {
@@ -540,9 +532,9 @@ impl<Args: Initializer> LspDriver<Args> {
540532
let args = match &mut self.state {
541533
State::Uninitialized(args) => args,
542534
_ => {
543-
return just_result!(Err(resp_err(
535+
return just_result(Err(resp_err(
544536
ErrorCode::InvalidRequest,
545-
"Server is already initialized"
537+
"Server is already initialized",
546538
)))
547539
}
548540
};
@@ -645,13 +637,11 @@ where
645637
self.state = State::Initializing(s);
646638
res
647639
}
648-
(State::Uninitialized(..) | State::Initializing(..), _) => {
649-
just_result!(Err(resp_err(
650-
ErrorCode::ServerNotInitialized,
651-
"Server is not initialized yet",
652-
)))
653-
}
654-
(_, request::Initialize::METHOD) => just_result!(Err(resp_err(
640+
(State::Uninitialized(..) | State::Initializing(..), _) => just_result(Err(resp_err(
641+
ErrorCode::ServerNotInitialized,
642+
"Server is not initialized yet",
643+
))),
644+
(_, request::Initialize::METHOD) => just_result(Err(resp_err(
655645
ErrorCode::InvalidRequest,
656646
"Server is already initialized",
657647
))),
@@ -676,7 +666,7 @@ where
676666

677667
return;
678668
}
679-
(State::ShuttingDown, _) => just_result!(Err(resp_err(
669+
(State::ShuttingDown, _) => just_result(Err(resp_err(
680670
ErrorCode::InvalidRequest,
681671
"Server is shutting down",
682672
))),

crates/tinymist/src/actor/typ_client.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ impl CompileClientActor {
340340

341341
let entry = self.config.determine_entry(Some(path.as_path().into()));
342342

343-
just_future!(async move {
343+
just_future(async move {
344344
let snap = snap.snapshot().await?;
345345
let snap = snap.task(TaskInputs {
346346
entry: Some(entry),
@@ -405,7 +405,7 @@ impl CompileClientActor {
405405
let dg = self.handle.diag_group.clone();
406406

407407
let snap = self.snapshot()?;
408-
just_future!(async move {
408+
just_future(async move {
409409
let snap = snap.snapshot().await?;
410410
let w = &snap.world;
411411

crates/tinymist/src/cmd.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl LanguageState {
5959
pub fn clear_cache(&mut self, _arguments: Vec<JsonValue>) -> AnySchedulableResponse {
6060
comemo::evict(0);
6161
self.primary().clear_cache();
62-
just_ok!(JsonValue::Null)
62+
just_ok(JsonValue::Null)
6363
}
6464

6565
/// Pin main file to some path.
@@ -70,7 +70,7 @@ impl LanguageState {
7070
update_result.map_err(|err| internal_error(format!("could not pin file: {err}")))?;
7171

7272
log::info!("file pinned: {entry:?}");
73-
just_ok!(JsonValue::Null)
73+
just_ok(JsonValue::Null)
7474
}
7575

7676
/// Focus main file to some path.
@@ -88,7 +88,7 @@ impl LanguageState {
8888
if ok {
8989
log::info!("file focused: {entry:?}");
9090
}
91-
just_ok!(JsonValue::Null)
91+
just_ok(JsonValue::Null)
9292
}
9393

9494
/// Start a preview instance.
@@ -168,7 +168,7 @@ impl LanguageState {
168168

169169
let snap = self.primary().snapshot().map_err(z_internal_error)?;
170170

171-
just_future!(async move {
171+
just_future(async move {
172172
let snap = snap.snapshot().await.map_err(z_internal_error)?;
173173

174174
// Parse the package specification. If the user didn't specify the version,
@@ -213,7 +213,7 @@ impl LanguageState {
213213

214214
let snap = self.primary().snapshot().map_err(z_internal_error)?;
215215

216-
just_future!(async move {
216+
just_future(async move {
217217
let snap = snap.snapshot().await.map_err(z_internal_error)?;
218218

219219
// Parse the package specification. If the user didn't specify the version,
@@ -282,19 +282,21 @@ impl LanguageState {
282282
let snap = self.primary().snapshot().map_err(z_internal_error)?;
283283
let user_action = self.user_action;
284284

285-
just_future!(async move {
285+
just_future(async move {
286286
let snap = snap.snapshot().await.map_err(z_internal_error)?;
287+
let display_entry = || format!("{entry:?}");
287288

288289
// todo: rootless file
289290
// todo: memory dirty file
290-
let root = entry.root().ok_or_else(
291-
|| error_once!("root must be determined for trace, got", entry: format!("{entry:?}")),
292-
).map_err(z_internal_error)?;
291+
let root = entry
292+
.root()
293+
.ok_or_else(|| error_once!("root must be determined for trace, got", entry: display_entry()))
294+
.map_err(z_internal_error)?;
293295
let main = entry
294296
.main()
295297
.and_then(|e| e.vpath().resolve(&root))
296298
.ok_or_else(
297-
|| error_once!("main file must be resolved, got", entry: format!("{entry:?}")),
299+
|| error_once!("main file must be resolved, got", entry: display_entry()),
298300
)
299301
.map_err(z_internal_error)?;
300302

@@ -338,13 +340,13 @@ impl LanguageState {
338340
/// Get the all valid symbols
339341
pub fn resource_symbols(&mut self, _arguments: Vec<JsonValue>) -> AnySchedulableResponse {
340342
let snapshot = self.primary().snapshot().map_err(z_internal_error)?;
341-
just_future!(Self::get_symbol_resources(snapshot))
343+
just_future(Self::get_symbol_resources(snapshot))
342344
}
343345

344346
/// Get resource preview html
345347
pub fn resource_preview_html(&mut self, _arguments: Vec<JsonValue>) -> AnySchedulableResponse {
346348
let resp = serde_json::to_value(TYPST_PREVIEW_HTML);
347-
just_result!(resp.map_err(|e| internal_error(e.to_string())))
349+
just_result(resp.map_err(|e| internal_error(e.to_string())))
348350
}
349351

350352
/// Get tutorial web page

crates/tinymist/src/init.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ impl Initializer for SuperInit {
245245
};
246246

247247
let res = serde_json::to_value(res).map_err(|e| invalid_params(e.to_string()));
248-
(service, just_result!(res))
248+
(service, just_result(res))
249249
}
250250
}
251251

crates/tinymist/src/server.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ impl LanguageState {
422422
/// request to the server again, the server will respond with JSON-RPC
423423
/// error code `-32600` (invalid request).
424424
fn shutdown(&mut self, _params: ()) -> SchedulableResponse<()> {
425-
just_ok!(())
425+
just_ok(())
426426
}
427427
}
428428

@@ -949,7 +949,7 @@ macro_rules! query_state {
949949
($self:ident, $method:ident, $req:expr) => {{
950950
let snap = $self.snapshot()?;
951951
let handle = $self.handle.clone();
952-
just_future!(async move {
952+
just_future(async move {
953953
let snap = snap.snapshot().await?;
954954
let w = &snap.world;
955955
let doc = snap.success_doc.map(|doc| VersionedDocument {
@@ -967,7 +967,7 @@ macro_rules! query_world {
967967
($self:ident, $method:ident, $req:expr) => {{
968968
let snap = $self.snapshot()?;
969969
let handle = $self.handle.clone();
970-
just_future!(async move {
970+
just_future(async move {
971971
let snap = snap.snapshot().await?;
972972
let w = &snap.world;
973973
handle
@@ -1005,7 +1005,7 @@ impl LanguageState {
10051005
}
10061006
};
10071007

1008-
just_ok!(query)
1008+
just_ok(query)
10091009
}
10101010

10111011
fn query_on(client: &CompileClientActor, query: CompilerQueryRequest) -> QueryFuture {

crates/tinymist/src/task/format.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl FormatTask {
6262

6363
pub fn exec(&self, source: Source) -> SchedulableResponse<Option<Vec<TextEdit>>> {
6464
let data = self.factory.task();
65-
just_future!(async move { (data.0)(source) })
65+
just_future(async move { (data.0)(source) })
6666
}
6767
}
6868

crates/tinymist/src/task/user_action.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub struct UserActionTask;
2525

2626
impl UserActionTask {
2727
pub fn trace(&self, params: TraceParams) -> SchedulableResponse<TraceReport> {
28-
just_future!(async move {
28+
just_future(async move {
2929
run_trace_program(params)
3030
.await
3131
.map_err(|e| internal_error(format!("failed to run trace program: {e:?}")))

crates/tinymist/src/tool/preview.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ impl PreviewState {
335335

336336
let preview_tx = self.preview_tx.clone();
337337
let handle = self.client.handle.clone();
338-
just_future!(async move {
338+
just_future(async move {
339339
let previewer = previewer.await;
340340
compile_handler.register_preview(previewer.compile_watcher().clone());
341341

@@ -374,15 +374,15 @@ impl PreviewState {
374374
let sent = self.preview_tx.send(PreviewRequest::Kill(task_id, tx));
375375
sent.map_err(|_| internal_error("failed to send kill request"))?;
376376

377-
just_future!(async move { rx.await.map_err(|_| internal_error("cancelled"))? })
377+
just_future(async move { rx.await.map_err(|_| internal_error("cancelled"))? })
378378
}
379379

380380
/// Scroll the preview to a given position.
381381
pub fn scroll(&self, task_id: String, req: ControlPlaneMessage) -> AnySchedulableResponse {
382382
let sent = self.preview_tx.send(PreviewRequest::Scroll(task_id, req));
383383
sent.map_err(|_| internal_error("failed to send scroll request"))?;
384384

385-
just_ok!(JsonValue::Null)
385+
just_ok(JsonValue::Null)
386386
}
387387
}
388388

0 commit comments

Comments
 (0)