Skip to content

Commit 41da617

Browse files
committed
fixes virtual merged document command not being multiplatform
1 parent 45bb16e commit 41da617

File tree

4 files changed

+54
-25
lines changed

4 files changed

+54
-25
lines changed

server/src/commands.rs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{collections::HashMap, path::PathBuf, str::FromStr};
1+
use std::{collections::HashMap, path::PathBuf};
22
use std::rc::Rc;
33
use std::cell::RefCell;
44
use std::fs::OpenOptions;
@@ -8,11 +8,11 @@ use serde_json::Value;
88

99
use petgraph::{dot, graph::NodeIndex};
1010

11-
use anyhow::{Result, anyhow};
11+
use anyhow::{Result, format_err};
1212

1313
use std::fs;
1414

15-
use crate::{graph::CachedStableGraph, merge_views};
15+
use crate::{graph::CachedStableGraph, merge_views, url_norm::FromJSON};
1616
use crate::dfs;
1717

1818
pub struct CustomCommandProvider {
@@ -28,24 +28,24 @@ impl CustomCommandProvider {
2828
}
2929
}
3030

31-
pub fn execute(&self, command: &str, args: Vec<Value>, root_path: &PathBuf) -> Result<Value, String> {
31+
pub fn execute(&self, command: &str, args: Vec<Value>, root_path: &PathBuf) -> Result<Value> {
3232
if self.commands.contains_key(command) {
3333
return self.commands.get(command).unwrap().run_command(root_path, args);
3434
}
35-
Err("command doesn't exist".into())
35+
Err(format_err!("command doesn't exist"))
3636
}
3737
}
3838

3939
pub trait Invokeable {
40-
fn run_command(&self, root: &PathBuf, arguments: Vec<Value>) -> Result<Value, String>;
40+
fn run_command(&self, root: &PathBuf, arguments: Vec<Value>) -> Result<Value>;
4141
}
4242

4343
pub struct GraphDotCommand {
4444
pub graph: Rc<RefCell<CachedStableGraph>>
4545
}
4646

4747
impl Invokeable for GraphDotCommand {
48-
fn run_command(&self, root: &PathBuf, _: Vec<Value>) -> Result<Value, String> {
48+
fn run_command(&self, root: &PathBuf, _: Vec<Value>) -> Result<Value> {
4949
let filepath = root.join("graph.dot");
5050
eprintln!("generating dot file at {:?}", filepath);
5151
let mut file = OpenOptions::new()
@@ -66,7 +66,7 @@ impl Invokeable for GraphDotCommand {
6666
};
6767

6868
match write_data_closure() {
69-
Err(err) => Err(format!("Error generating graphviz data: {}", err)),
69+
Err(err) => Err(format_err!("Error generating graphviz data: {}", err)),
7070
_ => Ok(Value::Null)
7171
}
7272
}
@@ -81,7 +81,7 @@ impl VirtualMergedDocument {
8181
fn get_file_toplevel_ancestors(&self, uri: &PathBuf) -> Result<Option<Vec<petgraph::stable_graph::NodeIndex>>> {
8282
let curr_node = match self.graph.borrow_mut().find_node(uri) {
8383
Some(n) => n,
84-
None => return Err(anyhow!("node not found {:?}", uri)),
84+
None => return Err(format_err!("node not found {:?}", uri)),
8585
};
8686
let roots = self.graph.borrow().collect_root_ancestors(curr_node);
8787
if roots.is_empty() {
@@ -111,7 +111,7 @@ impl VirtualMergedDocument {
111111

112112
let source = match fs::read_to_string(&path) {
113113
Ok(s) => s,
114-
Err(e) => return Err(anyhow!("error reading {:?}: {}", path, e))
114+
Err(e) => return Err(format_err!("error reading {:?}: {}", path, e))
115115
};
116116
sources.insert(path.clone(), source);
117117
}
@@ -121,19 +121,15 @@ impl VirtualMergedDocument {
121121
}
122122

123123
impl Invokeable for VirtualMergedDocument {
124-
fn run_command(&self, root: &PathBuf, arguments: Vec<Value>) -> Result<Value, String> {
125-
let path = arguments.get(0).unwrap().to_string();
126-
let path = percent_encoding::percent_decode_str(
127-
path.trim_start_matches('"').trim_end_matches('"').strip_prefix("/").unwrap()
128-
).decode_utf8().unwrap();
129-
let path = PathBuf::from_str(&path).unwrap();
124+
fn run_command(&self, root: &PathBuf, arguments: Vec<Value>) -> Result<Value> {
125+
let path = PathBuf::from_json(arguments.get(0).unwrap())?;
130126

131127
let file_ancestors = match self.get_file_toplevel_ancestors(&path) {
132128
Ok(opt) => match opt {
133129
Some(ancestors) => ancestors,
134130
None => vec![],
135131
},
136-
Err(e) => return Err(e.to_string()),
132+
Err(e) => return Err(e),
137133
};
138134

139135
//eprintln!("ancestors for {}:\n\t{:?}", path, file_ancestors.iter().map(|e| self.graph.borrow().graph.node_weight(*e).unwrap().clone()).collect::<Vec<String>>());
@@ -147,19 +143,19 @@ impl Invokeable for VirtualMergedDocument {
147143
let root = self.graph.borrow_mut().find_node(&path).unwrap();
148144
let tree = match self.get_dfs_for_node(root) {
149145
Ok(tree) => tree,
150-
Err(e) => return Err(e.to_string()),
146+
Err(e) => return Err(e.into()),
151147
};
152148

153149
let sources = match self.load_sources(&tree) {
154150
Ok(s) => s,
155-
Err(e) => return Err(e.to_string())
151+
Err(e) => return Err(e)
156152
};
157153
all_sources.extend(sources);
158154

159155
let graph = self.graph.borrow();
160156
let view = merge_views::generate_merge_list(&tree, &all_sources, &graph);
161157
return Ok(serde_json::value::Value::String(view));
162158
}
163-
return Err(format!("{:?} is not a top-level file aka has ancestors", path.strip_prefix(root).unwrap()))
159+
return Err(format_err!("{:?} is not a top-level file aka has ancestors", path.strip_prefix(root).unwrap()))
164160
}
165161
}

server/src/dfs.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,16 @@ impl <'a> Iterator for Dfs<'a> {
101101
pub mod error {
102102
use petgraph::stable_graph::NodeIndex;
103103

104-
use thiserror::Error;
105-
106-
use std::{fmt::{Debug, Display}, path::PathBuf};
104+
use std::{fmt::{Debug, Display}, path::PathBuf, error::Error as StdError};
107105

108106
use crate::{graph::CachedStableGraph, consts};
109107

110108
use rust_lsp::lsp_types::{Diagnostic, DiagnosticSeverity, Position, Range};
111109

112-
#[derive(Debug, Error)]
110+
#[derive(Debug)]
113111
pub struct CycleError(Vec<PathBuf>);
112+
113+
impl StdError for CycleError {}
114114

115115
impl CycleError {
116116
pub fn new(nodes: &[NodeIndex], current_node: NodeIndex, graph: &CachedStableGraph) -> Self {

server/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ impl LanguageServerHandling for MinecraftShaderLanguageServer {
630630
message: format!("Failed to execute `{}`. Reason: {}", params.command, err),
631631
}).expect("failed to send popup/show message notification");
632632
eprintln!("failed to execute {}: {}", params.command, err);
633-
completable.complete(Err(MethodError::new(32420, err, ())))
633+
completable.complete(Err(MethodError::new(32420, err.to_string(), ())))
634634
},
635635
}
636636
}

server/src/url_norm.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@ use std::path::PathBuf;
22

33
use path_slash::PathBufExt;
44
use url::Url;
5+
use anyhow::Result;
56

67
pub trait FromUrl {
78
fn from_url(u: Url) -> Self;
89
}
910

11+
pub trait FromJSON {
12+
fn from_json(v: &serde_json::value::Value) -> Result<Self> where Self: Sized;
13+
}
14+
1015
impl FromUrl for PathBuf {
1116
#[cfg(target_family = "windows")]
1217
fn from_url(u: Url) -> Self {
@@ -19,4 +24,32 @@ impl FromUrl for PathBuf {
1924
let path = percent_encoding::percent_decode_str(u.path()).decode_utf8().unwrap();
2025
PathBuf::from_slash(path)
2126
}
27+
}
28+
29+
impl FromJSON for PathBuf {
30+
#[cfg(target_family = "windows")]
31+
fn from_json(v: &serde_json::value::Value) -> Result<Self>
32+
where Self: Sized {
33+
if !v.is_string() {
34+
return Err(anyhow::format_err!("cannot convert {:?} to PathBuf", v));
35+
}
36+
let path = v.to_string();
37+
let path = percent_encoding::percent_decode_str(
38+
path.trim_start_matches('"').trim_end_matches('"').strip_prefix("/").unwrap()
39+
).decode_utf8()?;
40+
Ok(PathBuf::from_slash(path))
41+
}
42+
43+
#[cfg(target_family = "unix")]
44+
fn from_json(v: &serde_json::value::Value) -> Result<Self>
45+
where Self: Sized {
46+
if !v.is_string() {
47+
return Err(anyhow::format_err!("cannot convert {:?} to PathBuf", v));
48+
}
49+
let path = v.to_string();
50+
let path = percent_encoding::percent_decode_str(
51+
path.trim_start_matches('"').trim_end_matches('"')
52+
).decode_utf8()?;
53+
Ok(PathBuf::from_slash(path))
54+
}
2255
}

0 commit comments

Comments
 (0)