Skip to content

Commit 35b8667

Browse files
committed
adds support for relative vs absolute include path
1 parent 41da617 commit 35b8667

File tree

7 files changed

+131
-15
lines changed

7 files changed

+131
-15
lines changed

server/src/main.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -149,25 +149,24 @@ impl MinecraftShaderLanguageServer {
149149

150150
Some(entry.into_path())
151151
}).for_each(|path| {
152-
// iterate all valid found files, search for includes, add a node into the graph for each
153-
// file and add a file->includes KV into the map
152+
// iterate all valid found files, search for includes, add a node into the graph for each
153+
// file and add a file->includes KV into the map
154154
self.add_file_and_includes_to_graph(&path);
155155
});
156156

157157
eprintln!("finished building project include graph");
158-
//std::thread::sleep(std::time::Duration::from_secs(1));
159158
}
160159

161160
fn add_file_and_includes_to_graph(&self, path: &PathBuf) {
162161
let includes = self.find_includes(path);
163162

164163
let idx = self.graph.borrow_mut().add_node(&path);
165164

166-
//eprintln!("adding {} with\n{:?}", path.clone(), includes);
167-
for include in includes {
168-
self.add_include(include, idx);
169-
}
165+
//eprintln!("adding {:?} with {:?}", path, includes);
166+
for include in includes {
167+
self.add_include(include, idx);
170168
}
169+
}
171170

172171
fn add_include(&self, include: (PathBuf, IncludePosition), node: NodeIndex) {
173172
let child = self.graph.borrow_mut().add_node(&include.0);
@@ -191,17 +190,19 @@ impl MinecraftShaderLanguageServer {
191190
.unwrap()
192191
.get(1)
193192
.unwrap();
194-
//eprintln!("{:?}", caps);
195193

196194
let start = cap.start();
197195
let end = cap.end();
198196
let mut path: String = cap.as_str().into();
199197

200198
// TODO: difference between / and not
201-
if path.starts_with('/') {
199+
let full_include = if path.starts_with('/') {
202200
path = path.strip_prefix('/').unwrap().to_string();
203-
}
204-
let full_include = self.root.join("shaders").join(PathBuf::from_slash(&path));
201+
self.root.join("shaders").join(PathBuf::from_slash(&path))
202+
} else {
203+
file.parent().unwrap().join(PathBuf::from_slash(&path))
204+
};
205+
205206
includes.push((
206207
full_include,
207208
IncludePosition {
@@ -210,7 +211,6 @@ impl MinecraftShaderLanguageServer {
210211
end,
211212
}
212213
));
213-
//eprintln!("{} includes {}", file, full_include);
214214
});
215215

216216
includes
@@ -219,6 +219,8 @@ impl MinecraftShaderLanguageServer {
219219
fn update_includes(&self, file: &PathBuf) {
220220
let includes = self.find_includes(file);
221221

222+
eprintln!("updating {:?} with {:?}", file, includes);
223+
222224
let idx = match self.graph.borrow_mut().find_node(&file) {
223225
None => {
224226
return
@@ -232,6 +234,8 @@ impl MinecraftShaderLanguageServer {
232234
let to_be_added = new_children.difference(&prev_children);
233235
let to_be_removed = prev_children.difference(&new_children);
234236

237+
eprintln!("removing:\n\t{:?}\nadding:\n\t{:?}", to_be_removed, to_be_added);
238+
235239
for removal in to_be_removed {
236240
let child = self.graph.borrow_mut().find_node(&removal.0).unwrap();
237241
self.graph.borrow_mut().remove_edge(idx, child);
@@ -367,7 +371,7 @@ impl MinecraftShaderLanguageServer {
367371

368372
eprintln!("match {:?}", diagnostic_capture);
369373

370-
let msg = diagnostic_capture.name("output").unwrap().as_str();//.replace("'' : ", "");
374+
let msg = diagnostic_capture.name("output").unwrap().as_str();
371375

372376
let line = match diagnostic_capture.name("linenum") {
373377
Some(c) => match c.as_str().parse::<u32>() {

server/src/test.rs

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ fn test_empty_initialize() {
110110
match respu.result_or_error {
111111
ResponseResult::Result(_) => {}
112112
ResponseResult::Error(e) => {
113-
panic!(format!("expected ResponseResult::Result(..), got {:?}", e))
113+
panic!("expected ResponseResult::Result(..), got {:?}", e)
114114
}
115115
}
116116
};
@@ -162,7 +162,7 @@ fn test_01_initialize() {
162162
match respu.result_or_error {
163163
ResponseResult::Result(_) => {}
164164
ResponseResult::Error(e) => {
165-
panic!(format!("expected ResponseResult::Result(..), got {:?}", e))
165+
panic!("expected ResponseResult::Result(..), got {:?}", e)
166166
}
167167
}
168168
};
@@ -198,6 +198,77 @@ fn test_01_initialize() {
198198
);
199199
}
200200

201+
#[test]
202+
fn test_05_initialize() {
203+
let mut server = new_temp_server();
204+
205+
let (_tmp_dir, tmp_path) = copy_to_tmp_dir("./testdata/05");
206+
207+
let initialize_params = InitializeParams {
208+
process_id: None,
209+
root_path: None,
210+
root_uri: Some(Url::from_directory_path(tmp_path.clone()).unwrap()),
211+
client_info: None,
212+
initialization_options: None,
213+
capabilities: ClientCapabilities {
214+
workspace: None,
215+
text_document: None,
216+
experimental: None,
217+
window: None,
218+
general: Option::None,
219+
},
220+
trace: None,
221+
workspace_folders: None,
222+
locale: Option::None,
223+
};
224+
225+
let on_response = |resp: Option<Response>| {
226+
assert!(resp.is_some());
227+
let respu = resp.unwrap();
228+
match respu.result_or_error {
229+
ResponseResult::Result(_) => {}
230+
ResponseResult::Error(e) => {
231+
panic!("expected ResponseResult::Result(..), got {:?}", e)
232+
}
233+
}
234+
};
235+
236+
let completable = MethodCompletable::new(ResponseCompletable::new(
237+
Some(Id::Number(1)),
238+
Box::new(on_response),
239+
));
240+
server.initialize(initialize_params, completable);
241+
server.endpoint.request_shutdown();
242+
243+
// Assert there is one edge between two nodes
244+
assert_eq!(server.graph.borrow().graph.edge_count(), 3);
245+
246+
assert_eq!(server.graph.borrow().graph.node_count(), 4);
247+
248+
let pairs: HashSet<(PathBuf, PathBuf)> = vec![
249+
(
250+
tmp_path.join("shaders").join("final.fsh").to_str().unwrap().to_string().into(),
251+
tmp_path.join("shaders").join("common.glsl").to_str().unwrap().to_string().into()
252+
),
253+
(
254+
tmp_path.join("shaders").join("final.fsh").to_str().unwrap().to_string().into(),
255+
tmp_path.join("shaders").join("test").join("banana.glsl").to_str().unwrap().to_string().into()
256+
),
257+
(
258+
tmp_path.join("shaders").join("test").join("banana.glsl").to_str().unwrap().to_string().into(),
259+
tmp_path.join("shaders").join("test").join("burger.glsl").to_str().unwrap().to_string().into()
260+
)
261+
].into_iter().collect();
262+
263+
for edge in server.graph.borrow().graph.edge_indices() {
264+
let endpoints = server.graph.borrow().graph.edge_endpoints(edge).unwrap();
265+
let first = server.graph.borrow().get_node(endpoints.0);
266+
let second = server.graph.borrow().get_node(endpoints.1);
267+
let contains = pairs.contains(&(first.clone(), second.clone()));
268+
assert!(contains, "doesn't contain ({:?}, {:?})", first, second);
269+
}
270+
}
271+
201272
#[test]
202273
fn test_graph_two_connected_nodes() {
203274
let mut graph = graph::CachedStableGraph::new();

server/testdata/05/common.glsl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
float test() {
2+
return 0.5;
3+
}

server/testdata/05/final.fsh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#version 120
2+
3+
#include "/common.glsl"
4+
#include "/test/banana.glsl"
5+
6+
void main() {
7+
gl_FragColor = vec4(0.0);
8+
}

server/testdata/05/final.fsh.merge

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#version 120
2+
3+
#line 1 "!!"
4+
float test() {
5+
return 0.5;
6+
}
7+
#line 4 "!!"
8+
#line 1 "!!"
9+
#line 1 "!!"
10+
void dont() {
11+
12+
}
13+
#line 2 "!!"
14+
15+
void ok() {
16+
17+
}
18+
#line 5 "!!"
19+
20+
void main() {
21+
gl_FragColor = vec4(0.0);
22+
}

server/testdata/05/test/banana.glsl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "burger.glsl"
2+
3+
void ok() {
4+
5+
}

server/testdata/05/test/burger.glsl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
void dont() {
2+
3+
}

0 commit comments

Comments
 (0)