Get JSON LSP running, still work to be done

This commit is contained in:
Isaac Clayton 2022-06-06 16:02:54 +02:00
parent 38d7321511
commit fbaff615a3
7 changed files with 269 additions and 88 deletions

View file

@ -3,11 +3,26 @@ use serde_json::json;
use std::fs;
use std::path::PathBuf;
// import
// #[import]
// fn command(string: String) -> Option<String>;
// TODO: some sort of macro to generate ABI bindings
extern "C" {
pub fn hello(item: u32) -> u32;
pub fn bye(item: u32) -> u32;
}
// #[bind]
// pub async fn name(u32) -> u32 {
// }
#[bind]
pub fn name() -> &'static str {
let number = unsafe { hello(27) };
println!("got: {}", number);
let number = unsafe { bye(28) };
println!("got: {}", number);
"vscode-json-languageserver"
}
@ -34,65 +49,68 @@ pub fn fetch_latest_server_version() -> Option<String> {
Some("1.3.4".into())
}
// #[bind]
// pub fn fetch_server_binary(version: String) -> Option<PathBuf> {
// let version_dir = container_dir.join(version.as_str());
// fs::create_dir_all(&version_dir)
// .await
// .context("failed to create version directory")?;
// let binary_path = version_dir.join(Self::BIN_PATH);
#[bind]
pub fn fetch_server_binary(container_dir: PathBuf, version: String) -> Option<PathBuf> {
println!("Fetching server binary");
return None;
// let version_dir = container_dir.join(version.as_str());
// fs::create_dir_all(&version_dir)
// .await
// .context("failed to create version directory")?;
// let binary_path = version_dir.join(Self::BIN_PATH);
// if fs::metadata(&binary_path).await.is_err() {
// let output = smol::process::Command::new("npm")
// .current_dir(&version_dir)
// .arg("install")
// .arg(format!("vscode-json-languageserver@{}", version))
// .output()
// .await
// .context("failed to run npm install")?;
// if !output.status.success() {
// Err(anyhow!("failed to install vscode-json-languageserver"))?;
// }
// if fs::metadata(&binary_path).await.is_err() {
// let output = smol::process::Command::new("npm")
// .current_dir(&version_dir)
// .arg("install")
// .arg(format!("vscode-json-languageserver@{}", version))
// .output()
// .await
// .context("failed to run npm install")?;
// if !output.status.success() {
// Err(anyhow!("failed to install vscode-json-languageserver"))?;
// }
// if let Some(mut entries) = fs::read_dir(&container_dir).await.log_err() {
// while let Some(entry) = entries.next().await {
// if let Some(entry) = entry.log_err() {
// let entry_path = entry.path();
// if entry_path.as_path() != version_dir {
// fs::remove_dir_all(&entry_path).await.log_err();
// }
// }
// }
// }
// }
// if let Some(mut entries) = fs::read_dir(&container_dir).await.log_err() {
// while let Some(entry) = entries.next().await {
// if let Some(entry) = entry.log_err() {
// let entry_path = entry.path();
// if entry_path.as_path() != version_dir {
// fs::remove_dir_all(&entry_path).await.log_err();
// }
// }
// }
// }
// }
// Ok(binary_path)
// }
// Ok(binary_path)
}
const BIN_PATH: &'static str =
"node_modules/vscode-json-languageserver/bin/vscode-json-languageserver";
// #[bind]
// pub fn cached_server_binary(container_dir: PathBuf) -> Option<PathBuf> {
// println!("Finding cached server binary...");
// let mut last_version_dir = None;
// let mut entries = fs::read_dir(&container_dir).ok()?;
// println!("Read Entries...");
// while let Some(entry) = entries.next() {
// let entry = entry.ok()?;
// if entry.file_type().ok()?.is_dir() {
// last_version_dir = Some(entry.path());
// }
// }
// let last_version_dir = last_version_dir?;
// let bin_path = last_version_dir.join(BIN_PATH);
// if bin_path.exists() {
// println!("{}", bin_path.display());
// Some(bin_path)
// } else {
// None
// }
// }
#[bind]
pub fn cached_server_binary(container_dir: PathBuf) -> Option<PathBuf> {
println!("Finding cached server binary...");
let mut last_version_dir = None;
println!("{}", container_dir.exists());
let mut entries = fs::read_dir(&container_dir).ok()?;
println!("Read Entries...");
while let Some(entry) = entries.next() {
let entry = entry.ok()?;
if entry.file_type().ok()?.is_dir() {
last_version_dir = Some(entry.path());
}
}
let last_version_dir = last_version_dir?;
let bin_path = last_version_dir.join(BIN_PATH);
if bin_path.exists() {
println!("this is the path: {}", bin_path.display());
Some(bin_path)
} else {
None
}
}
#[bind]
pub fn initialization_options() -> Option<serde_json::Value> {

View file

@ -0,0 +1,88 @@
use zed_plugin::RopeRef;
// Host
struct Handles {
items: Vec<Box<dyn Any>>,
}
struct Rope;
impl Link for Rope {
fn link(linker: &mut Linker) -> Result<()> {
linker.add(|this: &mut Rope| {
});
linker.func_wrap("env", "len", |handles, arg| {
let rope = handles.downcast::<Rope>(arg.0);
let rope = Arc::from_raw(ptr);
let result = rope.len();
Arc::leak(rope);
result
});
}
fn to_handle(self) -> Handle<Rope> {
todo!()
}
}
// -- Host
pub fn edit_rope(&mut self) {
let rope: &mut Rope = self......rope();
let handle: RopeRef = self.runtime.to_handle(rope);
self.runtime.call("edit_rope", handle);
}
// Guest
extern "C" long rope__len(u32 handle);
struct RopeRef(u32);
impl RopeRef {
fn len(&self) -> usize {
rope__len(self.0);
}
}
pub fn edit_rope(rope: RopeRef) {
rope.len()
}
// Host side ---
pub struct Rope { .. }
RopeRef(u32);
impl Link for RopeRef {
pub fn init(&mut something) {
something.add("length", |rope| )
}
}
// ---
extern "C" {
pub fn length(item: u32) -> u32;
}
struct RopeRef {
handle: u32,
}
pub fn length(ref: RopeRef) -> u32 {
ref.length()
}
// Host side
#[plugin_interface]
trait LspAdapter {
name() -> &'static str;
}
// Guest side