|
1 | 1 | use anyhow::anyhow; |
| 2 | +use futures::future::TryFutureExt; |
2 | 3 | use tower_lsp::lsp_types::{Position, Range, Registration, TextEdit, Unregistration}; |
3 | | -use typst::syntax::{FileId, Source, VirtualPath}; |
| 4 | +use typst::{ |
| 5 | + foundations::Bytes, |
| 6 | + syntax::{FileId, Source, VirtualPath}, |
| 7 | +}; |
4 | 8 | use typstfmt_lib::Config; |
5 | 9 |
|
6 | | -use crate::workspace::project::Project; |
| 10 | +use crate::workspace::{fs::FsResult, project::Project}; |
7 | 11 |
|
8 | 12 | use super::TypstServer; |
9 | 13 |
|
10 | 14 | const FORMATTING_REGISTRATION_ID: &str = "formatting"; |
11 | 15 | const DOCUMENT_FORMATTING_METHOD_ID: &str = "textDocument/formatting"; |
12 | | -const CONFIG_PATH: &str = "typstfmt-config.toml"; |
| 16 | +const CONFIG_PATH: &str = "typstfmt.toml"; |
13 | 17 |
|
14 | 18 | pub fn get_formatting_registration() -> Registration { |
15 | 19 | Registration { |
@@ -59,8 +63,15 @@ async fn get_config(project: &Project) -> anyhow::Result<Config> { |
59 | 63 | } |
60 | 64 |
|
61 | 65 | async fn config_from_file(project: &Project) -> Option<anyhow::Result<Config>> { |
62 | | - let file_id = FileId::new(None, VirtualPath::new(CONFIG_PATH)); |
63 | | - let file = project.read_bytes_by_id(file_id).await.ok()?; |
| 66 | + async fn read_file(project: &Project, path: &str) -> FsResult<Bytes> { |
| 67 | + let file_id = FileId::new(None, VirtualPath::new(path)); |
| 68 | + project.read_bytes_by_id(file_id).await |
| 69 | + } |
| 70 | + |
| 71 | + let file = read_file(project, CONFIG_PATH) |
| 72 | + .or_else(|_| async { read_file(project, &format!(".{CONFIG_PATH}")).await }) |
| 73 | + .await |
| 74 | + .ok()?; |
64 | 75 | let bytes = file.as_slice(); |
65 | 76 | Some(config_from_bytes(bytes)) |
66 | 77 | } |
|
0 commit comments