adds support for relative vs absolute include path

This commit is contained in:
Noah Santschi-Cooney 2021-02-12 00:52:43 +00:00
parent 41da617c7f
commit 35b8667229
No known key found for this signature in database
GPG key ID: 3B22282472C8AE48
7 changed files with 131 additions and 15 deletions

View file

@ -149,13 +149,12 @@ impl MinecraftShaderLanguageServer {
Some(entry.into_path())
}).for_each(|path| {
// iterate all valid found files, search for includes, add a node into the graph for each
// file and add a file->includes KV into the map
// iterate all valid found files, search for includes, add a node into the graph for each
// file and add a file->includes KV into the map
self.add_file_and_includes_to_graph(&path);
});
eprintln!("finished building project include graph");
//std::thread::sleep(std::time::Duration::from_secs(1));
}
fn add_file_and_includes_to_graph(&self, path: &PathBuf) {
@ -163,11 +162,11 @@ impl MinecraftShaderLanguageServer {
let idx = self.graph.borrow_mut().add_node(&path);
//eprintln!("adding {} with\n{:?}", path.clone(), includes);
for include in includes {
self.add_include(include, idx);
}
//eprintln!("adding {:?} with {:?}", path, includes);
for include in includes {
self.add_include(include, idx);
}
}
fn add_include(&self, include: (PathBuf, IncludePosition), node: NodeIndex) {
let child = self.graph.borrow_mut().add_node(&include.0);
@ -191,17 +190,19 @@ impl MinecraftShaderLanguageServer {
.unwrap()
.get(1)
.unwrap();
//eprintln!("{:?}", caps);
let start = cap.start();
let end = cap.end();
let mut path: String = cap.as_str().into();
// TODO: difference between / and not
if path.starts_with('/') {
let full_include = if path.starts_with('/') {
path = path.strip_prefix('/').unwrap().to_string();
}
let full_include = self.root.join("shaders").join(PathBuf::from_slash(&path));
self.root.join("shaders").join(PathBuf::from_slash(&path))
} else {
file.parent().unwrap().join(PathBuf::from_slash(&path))
};
includes.push((
full_include,
IncludePosition {
@ -210,7 +211,6 @@ impl MinecraftShaderLanguageServer {
end,
}
));
//eprintln!("{} includes {}", file, full_include);
});
includes
@ -219,6 +219,8 @@ impl MinecraftShaderLanguageServer {
fn update_includes(&self, file: &PathBuf) {
let includes = self.find_includes(file);
eprintln!("updating {:?} with {:?}", file, includes);
let idx = match self.graph.borrow_mut().find_node(&file) {
None => {
return
@ -232,6 +234,8 @@ impl MinecraftShaderLanguageServer {
let to_be_added = new_children.difference(&prev_children);
let to_be_removed = prev_children.difference(&new_children);
eprintln!("removing:\n\t{:?}\nadding:\n\t{:?}", to_be_removed, to_be_added);
for removal in to_be_removed {
let child = self.graph.borrow_mut().find_node(&removal.0).unwrap();
self.graph.borrow_mut().remove_edge(idx, child);
@ -367,7 +371,7 @@ impl MinecraftShaderLanguageServer {
eprintln!("match {:?}", diagnostic_capture);
let msg = diagnostic_capture.name("output").unwrap().as_str();//.replace("'' : ", "");
let msg = diagnostic_capture.name("output").unwrap().as_str();
let line = match diagnostic_capture.name("linenum") {
Some(c) => match c.as_str().parse::<u32>() {

View file

@ -110,7 +110,7 @@ fn test_empty_initialize() {
match respu.result_or_error {
ResponseResult::Result(_) => {}
ResponseResult::Error(e) => {
panic!(format!("expected ResponseResult::Result(..), got {:?}", e))
panic!("expected ResponseResult::Result(..), got {:?}", e)
}
}
};
@ -162,7 +162,7 @@ fn test_01_initialize() {
match respu.result_or_error {
ResponseResult::Result(_) => {}
ResponseResult::Error(e) => {
panic!(format!("expected ResponseResult::Result(..), got {:?}", e))
panic!("expected ResponseResult::Result(..), got {:?}", e)
}
}
};
@ -198,6 +198,77 @@ fn test_01_initialize() {
);
}
#[test]
fn test_05_initialize() {
let mut server = new_temp_server();
let (_tmp_dir, tmp_path) = copy_to_tmp_dir("./testdata/05");
let initialize_params = InitializeParams {
process_id: None,
root_path: None,
root_uri: Some(Url::from_directory_path(tmp_path.clone()).unwrap()),
client_info: None,
initialization_options: None,
capabilities: ClientCapabilities {
workspace: None,
text_document: None,
experimental: None,
window: None,
general: Option::None,
},
trace: None,
workspace_folders: None,
locale: Option::None,
};
let on_response = |resp: Option<Response>| {
assert!(resp.is_some());
let respu = resp.unwrap();
match respu.result_or_error {
ResponseResult::Result(_) => {}
ResponseResult::Error(e) => {
panic!("expected ResponseResult::Result(..), got {:?}", e)
}
}
};
let completable = MethodCompletable::new(ResponseCompletable::new(
Some(Id::Number(1)),
Box::new(on_response),
));
server.initialize(initialize_params, completable);
server.endpoint.request_shutdown();
// Assert there is one edge between two nodes
assert_eq!(server.graph.borrow().graph.edge_count(), 3);
assert_eq!(server.graph.borrow().graph.node_count(), 4);
let pairs: HashSet<(PathBuf, PathBuf)> = vec![
(
tmp_path.join("shaders").join("final.fsh").to_str().unwrap().to_string().into(),
tmp_path.join("shaders").join("common.glsl").to_str().unwrap().to_string().into()
),
(
tmp_path.join("shaders").join("final.fsh").to_str().unwrap().to_string().into(),
tmp_path.join("shaders").join("test").join("banana.glsl").to_str().unwrap().to_string().into()
),
(
tmp_path.join("shaders").join("test").join("banana.glsl").to_str().unwrap().to_string().into(),
tmp_path.join("shaders").join("test").join("burger.glsl").to_str().unwrap().to_string().into()
)
].into_iter().collect();
for edge in server.graph.borrow().graph.edge_indices() {
let endpoints = server.graph.borrow().graph.edge_endpoints(edge).unwrap();
let first = server.graph.borrow().get_node(endpoints.0);
let second = server.graph.borrow().get_node(endpoints.1);
let contains = pairs.contains(&(first.clone(), second.clone()));
assert!(contains, "doesn't contain ({:?}, {:?})", first, second);
}
}
#[test]
fn test_graph_two_connected_nodes() {
let mut graph = graph::CachedStableGraph::new();

3
server/testdata/05/common.glsl vendored Normal file
View file

@ -0,0 +1,3 @@
float test() {
return 0.5;
}

8
server/testdata/05/final.fsh vendored Normal file
View file

@ -0,0 +1,8 @@
#version 120
#include "/common.glsl"
#include "/test/banana.glsl"
void main() {
gl_FragColor = vec4(0.0);
}

22
server/testdata/05/final.fsh.merge vendored Normal file
View file

@ -0,0 +1,22 @@
#version 120
#line 1 "!!"
float test() {
return 0.5;
}
#line 4 "!!"
#line 1 "!!"
#line 1 "!!"
void dont() {
}
#line 2 "!!"
void ok() {
}
#line 5 "!!"
void main() {
gl_FragColor = vec4(0.0);
}

5
server/testdata/05/test/banana.glsl vendored Normal file
View file

@ -0,0 +1,5 @@
#include "burger.glsl"
void ok() {
}

3
server/testdata/05/test/burger.glsl vendored Normal file
View file

@ -0,0 +1,3 @@
void dont() {
}