mirror of
https://github.com/Strum355/mcshader-lsp.git
synced 2025-08-31 05:47:22 +00:00
THE BIG MERGE v0.9
This commit is contained in:
parent
88f165dd33
commit
d35781b718
8 changed files with 141 additions and 45 deletions
|
@ -13,22 +13,4 @@ pub struct StatusParams {
|
|||
pub status: String,
|
||||
pub message: Option<String>,
|
||||
pub icon: Option<String>,
|
||||
}
|
||||
|
||||
pub enum ConfigUpdate {}
|
||||
|
||||
impl Notification for ConfigUpdate {
|
||||
type Params = ConfigUpdateParams;
|
||||
const METHOD: &'static str = "mc-glsl/updateConfig";
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize)]
|
||||
pub struct ConfigUpdateParams {
|
||||
pub kv: Vec<KV>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize)]
|
||||
pub struct KV {
|
||||
key: String,
|
||||
value: String
|
||||
}
|
|
@ -29,14 +29,13 @@ impl <'a> MergeViewGenerator<'a> {
|
|||
let mut last_offset: Vec<String> = Vec::new();
|
||||
|
||||
let mut nodes_iter = nodes.iter().peekable();
|
||||
nodes_iter.next();
|
||||
let first = nodes_iter.next().unwrap().0;
|
||||
let first_path = self.graph.get_node(first).clone();
|
||||
|
||||
self.create_merge_views(nodes_iter, &mut merge_list, &mut last_offset_set, &mut last_offset);
|
||||
|
||||
for file in last_offset.iter().rev() {
|
||||
let offset = *last_offset_set.get(file).unwrap();
|
||||
merge_list.push_back(&self.sources.get(file).unwrap().as_str()[offset..]);
|
||||
}
|
||||
let offset = *last_offset_set.get(&first_path).unwrap();
|
||||
merge_list.push_back(&self.sources.get(&first_path).unwrap().as_str()[offset..]);
|
||||
|
||||
merge_list
|
||||
}
|
||||
|
@ -62,12 +61,13 @@ impl <'a> MergeViewGenerator<'a> {
|
|||
if !last_offset_set.contains_key(&parent_path) {
|
||||
last_offset.push(parent_path.clone());
|
||||
}
|
||||
|
||||
let offset = *last_offset_set.insert(parent_path.clone(), edge.line).get_or_insert(0);
|
||||
|
||||
let source = self.sources.get(&parent_path).unwrap();
|
||||
let mut char_for_line: usize = 0;
|
||||
let mut char_following_line: usize = 0;
|
||||
for (n, line) in source.as_str().lines().enumerate() {//.map(|s| s.len()).sum();
|
||||
for (n, line) in source.as_str().lines().enumerate() {
|
||||
if n == edge.line {
|
||||
char_following_line += line.len()+1;
|
||||
break;
|
||||
|
@ -75,28 +75,59 @@ impl <'a> MergeViewGenerator<'a> {
|
|||
char_for_line += line.len()+1;
|
||||
char_following_line = char_for_line;
|
||||
}
|
||||
|
||||
|
||||
// TODO: update after finding offset? why set it to 0 on L71 then
|
||||
last_offset_set.insert(parent_path.clone(), char_following_line);
|
||||
merge_list.push_back(&source.as_str()[offset..char_for_line]);
|
||||
merge_list.push_back(&"#line 1\n"[..]);
|
||||
|
||||
match nodes.peek() {
|
||||
Some(_) => self.create_merge_views(nodes, merge_list, last_offset_set, last_offset),
|
||||
Some(next) => {
|
||||
let next = (*next).clone();
|
||||
// if the next element is not a child of this element, we dump the rest of this elements source
|
||||
if next.1.unwrap() != child {
|
||||
let source = self.sources.get(&child_path).unwrap();
|
||||
merge_list.push_back(&source.as_str()[..]);
|
||||
// +2 because edge.line is 0 indexed but #line is 1 indexed and references the *following* line
|
||||
let line_directive = format!("\n#line {}\n", edge.line+2);
|
||||
|
||||
self.line_directives.borrow_mut().push(line_directive);
|
||||
unsafe {
|
||||
self.unsafe_get_and_insert(merge_list);
|
||||
}
|
||||
}
|
||||
self.create_merge_views(nodes, merge_list, last_offset_set, last_offset);
|
||||
|
||||
if next.1.unwrap() == child {
|
||||
let offset = *last_offset_set.get(&child_path).unwrap();
|
||||
merge_list.push_back(&self.sources.get(&child_path).unwrap().as_str()[offset..]);
|
||||
|
||||
// +2 because edge.line is 0 indexed but #line is 1 indexed and references the *following* line
|
||||
let line_directive = format!("\n#line {}\n", edge.line+2);
|
||||
|
||||
self.line_directives.borrow_mut().push(line_directive);
|
||||
unsafe {
|
||||
self.unsafe_get_and_insert(merge_list);
|
||||
}
|
||||
}
|
||||
},
|
||||
None => {
|
||||
let source = self.sources.get(&child_path).unwrap();
|
||||
merge_list.push_back(&source.as_str()[..]);
|
||||
// +2 because edge.line is 0 indexed but #line is 1 indexed and references the *following* line
|
||||
let line_directive = format!("\n#line {}\n", edge.line+2);
|
||||
|
||||
self.line_directives.borrow_mut().push(line_directive);
|
||||
unsafe {
|
||||
self.unsafe_get_and_insert(merge_list);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// +2 because edge.line is 0 indexed but #line is 1 indexed and references the *following* line
|
||||
let line_directive = format!("\n#line {}\n", edge.line+2);
|
||||
|
||||
self.line_directives.borrow_mut().push(line_directive);
|
||||
unsafe {
|
||||
// :^)
|
||||
let vec_ptr_offset = self.line_directives.borrow().as_ptr().add(self.line_directives.borrow().len()-1);
|
||||
merge_list.push_back(&vec_ptr_offset.as_ref().unwrap().as_str()[..]);
|
||||
}
|
||||
unsafe fn unsafe_get_and_insert(&self, merge_list: &mut LinkedList<&str>) {
|
||||
// :^)
|
||||
let vec_ptr_offset = self.line_directives.borrow().as_ptr().add(self.line_directives.borrow().len()-1);
|
||||
merge_list.push_back(&vec_ptr_offset.as_ref().unwrap().as_str()[..]);
|
||||
}
|
||||
}
|
|
@ -53,11 +53,11 @@ fn copy_files(files: &str, dest: &TempDir) {
|
|||
}
|
||||
|
||||
fn copy_to_and_set_root(test_path: &str, server: &mut MinecraftShaderLanguageServer) -> (Rc<TempDir>, String) {
|
||||
let (tmp_dir, tmp_path) = copy_to_tmp_dir(test_path);
|
||||
let (_tmp_dir, tmp_path) = copy_to_tmp_dir(test_path);
|
||||
|
||||
server.root = Some(format!("{}{}", "file://", tmp_path));
|
||||
|
||||
(tmp_dir, tmp_path)
|
||||
(_tmp_dir, tmp_path)
|
||||
}
|
||||
|
||||
fn copy_to_tmp_dir(test_path: &str) -> (Rc<TempDir>, String) {
|
||||
|
@ -119,7 +119,7 @@ fn test_empty_initialize() {
|
|||
fn test_01_initialize() {
|
||||
let mut server = new_temp_server();
|
||||
|
||||
let (tmp_dir, tmp_path) = copy_to_tmp_dir("./testdata/01");
|
||||
let (_tmp_dir, tmp_path) = copy_to_tmp_dir("./testdata/01");
|
||||
|
||||
let initialize_params = InitializeParams{
|
||||
process_id: None,
|
||||
|
@ -354,7 +354,6 @@ fn test_graph_dfs_cycle() {
|
|||
let next = dfs.next().unwrap();
|
||||
assert_that!(next, err());
|
||||
}
|
||||
|
||||
{
|
||||
let mut graph = graph::CachedStableGraph::new();
|
||||
|
||||
|
@ -373,10 +372,10 @@ fn test_graph_dfs_cycle() {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn test_generate_merge_list() {
|
||||
fn test_generate_merge_list_01() {
|
||||
let mut server = new_temp_server();
|
||||
|
||||
let (tmp_dir, tmp_path) = copy_to_and_set_root("./testdata/01", &mut server);
|
||||
let (_tmp_dir, tmp_path) = copy_to_and_set_root("./testdata/01", &mut server);
|
||||
|
||||
let final_idx = server.graph.borrow_mut().add_node(format!("{}/shaders/{}", tmp_path, "final.fsh"));
|
||||
let common_idx = server.graph.borrow_mut().add_node(format!("{}/shaders/{}", tmp_path, "common.glsl"));
|
||||
|
@ -386,15 +385,48 @@ fn test_generate_merge_list() {
|
|||
let nodes = server.get_dfs_for_node(final_idx).unwrap();
|
||||
let mut sources = server.load_sources(&nodes).unwrap();
|
||||
|
||||
println!("{:?}", nodes);
|
||||
|
||||
let graph_borrow = server.graph.borrow();
|
||||
let mut merger = merge_views::MergeViewGenerator::new(&mut sources, &graph_borrow);
|
||||
|
||||
let result = merger.generate_merge_list(&nodes);
|
||||
//let result: LinkedList<&str> = LinkedList::new();
|
||||
|
||||
//assert_that!(&result, ok());
|
||||
let total: String = result
|
||||
.iter()
|
||||
.map(|s| &**s)
|
||||
.collect::<Vec<&str>>()
|
||||
.join("");
|
||||
|
||||
let merge_file = tmp_path + "/shaders/final.fsh.merge";
|
||||
|
||||
let truth = String::from_utf8(fs::read::<String>(merge_file).unwrap()).unwrap();
|
||||
|
||||
assert_that!(total, eq(truth));
|
||||
|
||||
server.endpoint.request_shutdown();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_generate_merge_list_02() {
|
||||
let mut server = new_temp_server();
|
||||
|
||||
let (_tmp_dir, tmp_path) = copy_to_and_set_root("./testdata/02", &mut server);
|
||||
|
||||
let final_idx = server.graph.borrow_mut().add_node(format!("{}/shaders/{}", tmp_path, "final.fsh"));
|
||||
let test_idx = server.graph.borrow_mut().add_node(format!("{}/shaders/utils/{}", tmp_path, "test.glsl"));
|
||||
let burger_idx = server.graph.borrow_mut().add_node(format!("{}/shaders/utils/{}", tmp_path, "burger.glsl"));
|
||||
let sample_idx = server.graph.borrow_mut().add_node(format!("{}/shaders/utils/{}", tmp_path, "sample.glsl"));
|
||||
|
||||
server.graph.borrow_mut().add_edge(final_idx, sample_idx, 2, 0, 0);
|
||||
server.graph.borrow_mut().add_edge(sample_idx, burger_idx, 4, 0, 0);
|
||||
server.graph.borrow_mut().add_edge(sample_idx, test_idx, 6, 0, 0);
|
||||
|
||||
let nodes = server.get_dfs_for_node(final_idx).unwrap();
|
||||
let mut sources = server.load_sources(&nodes).unwrap();
|
||||
|
||||
let graph_borrow = server.graph.borrow();
|
||||
let mut merger = merge_views::MergeViewGenerator::new(&mut sources, &graph_borrow);
|
||||
|
||||
let result = merger.generate_merge_list(&nodes);
|
||||
|
||||
let total: String = result
|
||||
.iter()
|
||||
|
|
7
server/testdata/02/final.fsh
vendored
Normal file
7
server/testdata/02/final.fsh
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
#version 120
|
||||
|
||||
#include "/utils/sample.glsl"
|
||||
|
||||
void main() {
|
||||
gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
|
||||
}
|
27
server/testdata/02/final.fsh.merge
vendored
Normal file
27
server/testdata/02/final.fsh.merge
vendored
Normal file
|
@ -0,0 +1,27 @@
|
|||
#version 120
|
||||
|
||||
#line 1
|
||||
int sample() {
|
||||
return 5;
|
||||
}
|
||||
|
||||
#line 1
|
||||
void burger() {
|
||||
// sample text
|
||||
}
|
||||
#line 6
|
||||
|
||||
#line 1
|
||||
float test() {
|
||||
return 3.0;
|
||||
}
|
||||
#line 8
|
||||
|
||||
int sample_more() {
|
||||
return 5;
|
||||
}
|
||||
#line 4
|
||||
|
||||
void main() {
|
||||
gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
|
||||
}
|
3
server/testdata/02/utils/burger.glsl
vendored
Normal file
3
server/testdata/02/utils/burger.glsl
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
void burger() {
|
||||
// sample text
|
||||
}
|
11
server/testdata/02/utils/sample.glsl
vendored
Normal file
11
server/testdata/02/utils/sample.glsl
vendored
Normal file
|
@ -0,0 +1,11 @@
|
|||
int sample() {
|
||||
return 5;
|
||||
}
|
||||
|
||||
#include "/utils/burger.glsl"
|
||||
|
||||
#include "/utils/test.glsl"
|
||||
|
||||
int sample_more() {
|
||||
return 5;
|
||||
}
|
3
server/testdata/02/utils/test.glsl
vendored
Normal file
3
server/testdata/02/utils/test.glsl
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
float test() {
|
||||
return 3.0;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue