fix: empty url sent from neovim (#130)

* fix: empty url sent from neovim

* dev: move e2e build script

* dev: make stronger name for empty urls

* fix: work around empty url

* fix: convert empty link on unix system
This commit is contained in:
Myriad-Dreamin 2024-03-30 20:04:03 +08:00 committed by GitHub
parent b76e80bad3
commit edbb7bc1af
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 311 additions and 46 deletions

View file

@ -6,6 +6,7 @@
use std::path::{Path, PathBuf};
use lsp_types::{self, Url};
use once_cell::sync::Lazy;
use reflexo::path::PathClean;
pub type LspPosition = lsp_types::Position;
@ -68,21 +69,35 @@ pub type TypstCompletion = crate::upstream::Completion;
pub type TypstCompletionKind = crate::upstream::CompletionKind;
const UNTITLED_ROOT: &str = "/untitled";
static EMPTY_URL: Lazy<Url> = Lazy::new(|| Url::parse("file://").unwrap());
pub fn path_to_url(path: &Path) -> anyhow::Result<Url> {
if let Ok(untitled) = path.strip_prefix(UNTITLED_ROOT) {
// rust-url will panic on converting an empty path.
if untitled == Path::new("nEoViM-BuG") {
return Ok(EMPTY_URL.clone());
}
return Ok(Url::parse(&format!("untitled:{}", untitled.display()))?);
}
Url::from_file_path(path).map_err(|e| {
Url::from_file_path(path).or_else(|e| {
let _: () = e;
anyhow::anyhow!("could not convert path to URI: path: {path:?}",)
anyhow::bail!("could not convert path to URI: path: {path:?}",)
})
}
pub fn url_to_path(uri: Url) -> PathBuf {
if uri.scheme() == "file" {
return uri.to_file_path().unwrap();
// typst converts an empty path to `Path::new("/")`, which is undesirable.
if !uri.has_host() && uri.path() == "/" {
return PathBuf::from("/untitled/nEoViM-BuG");
}
return uri
.to_file_path()
.unwrap_or_else(|_| panic!("could not convert URI to path: URI: {uri:?}",));
}
if uri.scheme() == "untitled" {
@ -375,6 +390,17 @@ mod test {
assert_eq!(path, Path::new("/untitled/test").clean());
}
#[test]
fn unnamed_buffer() {
// https://github.com/neovim/nvim-lspconfig/pull/2226
let uri = EMPTY_URL.clone();
let path = url_to_path(uri);
assert_eq!(path, Path::new("/untitled/nEoViM-BuG"));
let uri2 = path_to_url(&path).unwrap();
assert_eq!(EMPTY_URL.clone(), uri2);
}
const ENCODING_TEST_STRING: &str = "test 🥺 test";
#[test]