Improve prettier_server LSP names in the log panel

This commit is contained in:
Kirill Bulatov 2023-09-21 15:46:50 +03:00
parent 1b70e7d0df
commit f42cb109a0
2 changed files with 76 additions and 14 deletions

View file

@ -13,6 +13,9 @@ use serde::{Deserialize, Serialize};
use util::paths::DEFAULT_PRETTIER_DIR;
pub struct Prettier {
worktree_id: Option<usize>,
default: bool,
prettier_dir: PathBuf,
server: Arc<LanguageServer>,
}
@ -143,11 +146,12 @@ impl Prettier {
.with_context(|| format!("finding prettier starting with {starting_path:?}"))?
{
Some(prettier_dir) => Ok(prettier_dir),
None => Ok(util::paths::DEFAULT_PRETTIER_DIR.to_path_buf()),
None => Ok(DEFAULT_PRETTIER_DIR.to_path_buf()),
}
}
pub async fn start(
worktree_id: Option<usize>,
server_id: LanguageServerId,
prettier_dir: PathBuf,
node: Arc<dyn NodeRuntime>,
@ -171,7 +175,7 @@ impl Prettier {
server_id,
LanguageServerBinary {
path: node_path,
arguments: vec![prettier_server.into(), prettier_dir.into()],
arguments: vec![prettier_server.into(), prettier_dir.as_path().into()],
},
Path::new("/"),
None,
@ -182,7 +186,12 @@ impl Prettier {
.spawn(server.initialize(None))
.await
.context("prettier server initialization")?;
Ok(Self { server })
Ok(Self {
worktree_id,
server,
default: prettier_dir == DEFAULT_PRETTIER_DIR.as_path(),
prettier_dir,
})
}
pub async fn format(
@ -237,6 +246,18 @@ impl Prettier {
pub fn server(&self) -> &Arc<LanguageServer> {
&self.server
}
pub fn is_default(&self) -> bool {
self.default
}
pub fn prettier_dir(&self) -> &Path {
&self.prettier_dir
}
pub fn worktree_id(&self) -> Option<usize> {
self.worktree_id
}
}
async fn find_closest_prettier_dir(

View file

@ -8289,19 +8289,60 @@ impl Project {
this.update(&mut cx, |this, _| this.languages.next_language_server_id());
let new_prettier_task = cx
.spawn(|mut cx| async move {
let prettier =
Prettier::start(new_server_id, task_prettier_dir, node, cx.clone())
.await
.context("prettier start")
.map_err(Arc::new)?;
let prettier = Prettier::start(
worktree_id.map(|id| id.to_usize()),
new_server_id,
task_prettier_dir,
node,
cx.clone(),
)
.await
.context("prettier start")
.map_err(Arc::new)?;
if let Some(project) = weak_project.upgrade(&mut cx) {
let prettier_server = Arc::clone(prettier.server());
project.update(&mut cx, |project, cx| {
project.supplementary_language_servers.insert(
new_server_id,
// TODO kb same name repeats for different prettiers, distinguish
(LanguageServerName(Arc::from("prettier")), prettier_server),
);
let name = if prettier.is_default() {
LanguageServerName(Arc::from("prettier (default)"))
} else {
let prettier_dir = prettier.prettier_dir();
let worktree_path = prettier
.worktree_id()
.map(WorktreeId::from_usize)
.and_then(|id| project.worktree_for_id(id, cx))
.map(|worktree| worktree.read(cx).abs_path());
match worktree_path {
Some(worktree_path) => {
if worktree_path.as_ref() == prettier_dir {
LanguageServerName(Arc::from(format!(
"prettier ({})",
prettier_dir
.file_name()
.and_then(|name| name.to_str())
.unwrap_or_default()
)))
} else {
let dir_to_display = match prettier_dir
.strip_prefix(&worktree_path)
.ok()
{
Some(relative_path) => relative_path,
None => prettier_dir,
};
LanguageServerName(Arc::from(format!(
"prettier ({})",
dir_to_display.display(),
)))
}
}
None => LanguageServerName(Arc::from(format!(
"prettier ({})",
prettier_dir.display(),
))),
}
};
project
.supplementary_language_servers
.insert(new_server_id, (name, Arc::clone(prettier.server())));
cx.emit(Event::LanguageServerAdded(new_server_id));
});
}