1
1
mirror of https://github.com/oxalica/nil.git synced 2024-11-22 19:49:20 +03:00

Tweak VfsPath conversion interfaces

This commit is contained in:
oxalica 2023-01-24 23:10:03 +08:00
parent 29fdd1fba9
commit 6e5321582c
3 changed files with 47 additions and 27 deletions

View File

@ -1,7 +1,7 @@
use salsa::Durability;
use std::collections::HashMap;
use std::fmt;
use std::path::{Component, Path};
use std::path::{Component, Path, PathBuf};
use std::sync::Arc;
use syntax::{TextRange, TextSize};
@ -35,20 +35,6 @@ impl VfsPath {
Ok(Self(s))
}
pub fn from_path(path: &Path) -> Result<Self, ParseVfsPathError> {
let mut ret = Self::root();
for comp in path.components() {
match comp {
Component::RootDir => {}
Component::Normal(seg) => {
ret.push_segment(seg.to_str().ok_or(ParseVfsPathError)?);
}
_ => return Err(ParseVfsPathError),
}
}
Ok(ret)
}
/// Assume another VfsPath as relative and append it to this one.
pub fn append(&mut self, relative: &Self) {
self.0.push_str(&relative.0);
@ -79,6 +65,32 @@ impl VfsPath {
}
}
impl TryFrom<PathBuf> for VfsPath {
type Error = ParseVfsPathError;
fn try_from(path: PathBuf) -> Result<Self, Self::Error> {
(&*path).try_into()
}
}
impl TryFrom<&'_ Path> for VfsPath {
type Error = ParseVfsPathError;
fn try_from(path: &Path) -> Result<Self, Self::Error> {
let mut ret = Self(String::with_capacity(path.as_os_str().len()));
for comp in path.components() {
match comp {
Component::RootDir => {}
Component::Normal(seg) => {
ret.push_segment(seg.to_str().ok_or(ParseVfsPathError)?);
}
_ => return Err(ParseVfsPathError),
}
}
Ok(ret)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct ParseVfsPathError;

View File

@ -6,9 +6,10 @@ mod semantic_tokens;
mod server;
mod vfs;
use anyhow::Result;
use anyhow::{anyhow, Result};
use ide::VfsPath;
use lsp_server::{Connection, ErrorCode};
use lsp_types::InitializeParams;
use lsp_types::{InitializeParams, Url};
use std::fmt;
pub(crate) use server::{Server, StateSnapshot};
@ -29,6 +30,19 @@ impl fmt::Display for LspError {
impl std::error::Error for LspError {}
pub(crate) trait UrlExt {
fn to_vfs_path(&self) -> Result<VfsPath>;
}
impl UrlExt for Url {
fn to_vfs_path(&self) -> Result<VfsPath> {
let path = self
.to_file_path()
.map_err(|()| anyhow!("Non-file URI: {self}"))?;
Ok(path.try_into()?)
}
}
pub fn main_loop(conn: Connection) -> Result<()> {
let init_params =
conn.initialize(serde_json::to_value(capabilities::server_capabilities()).unwrap())?;

View File

@ -1,4 +1,5 @@
use anyhow::{anyhow, ensure, Context, Result};
use crate::UrlExt;
use anyhow::{ensure, Context, Result};
use ide::{Change, FileId, FileSet, SourceRoot, VfsPath};
use lsp_types::Url;
use std::collections::HashMap;
@ -37,15 +38,8 @@ impl Vfs {
}
}
fn uri_to_vpath(&self, uri: &Url) -> Result<VfsPath> {
let path = uri
.to_file_path()
.map_err(|()| anyhow!("Non-file URI: {uri}"))?;
Ok(VfsPath::from_path(&path).expect("URI is UTF-8"))
}
pub fn set_uri_content(&mut self, uri: &Url, text: String) -> Result<()> {
let vpath = self.uri_to_vpath(uri)?;
let vpath = uri.to_vfs_path()?;
self.set_path_content(vpath, text);
Ok(())
}
@ -109,7 +103,7 @@ impl Vfs {
}
pub fn file_for_uri(&self, uri: &Url) -> Result<FileId> {
let vpath = self.uri_to_vpath(uri)?;
let vpath = uri.to_vfs_path()?;
self.local_file_set
.file_for_path(&vpath)
.with_context(|| format!("URI not found: {uri}"))