Skip to content

Commit 72ea905

Browse files
committed
fixes merger for mixed CRLF/LF projects
1 parent fabbc68 commit 72ea905

File tree

3 files changed

+12
-20
lines changed

3 files changed

+12
-20
lines changed

server/src/commands.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ impl VirtualMergedDocument {
113113
Ok(s) => s,
114114
Err(e) => return Err(format_err!("error reading {:?}: {}", path, e))
115115
};
116+
let source = crate::RE_CRLF.replace_all(&source, "\n").to_string();
116117
sources.insert(path.clone(), source);
117118
}
118119

server/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ lazy_static! {
4545
static ref RE_VERSION: Regex = Regex::new(r#"#version [\d]{3}"#).unwrap();
4646
static ref RE_INCLUDE: Regex = Regex::new(r#"^(?:\s)*?(?:#include) "(.+)"\r?"#).unwrap();
4747
static ref RE_INCLUDE_EXTENSION: Regex = Regex::new(r#"#extension GL_GOOGLE_include_directive ?: ?require"#).unwrap();
48+
pub static ref RE_CRLF: Regex = Regex::new(r#"\r\n"#).unwrap();
4849
}
4950

5051
fn main() {
@@ -456,6 +457,7 @@ impl MinecraftShaderLanguageServer {
456457
Ok(s) => s,
457458
Err(e) => return Err(anyhow!("error reading {:?}: {}", path, e))
458459
};
460+
let source = RE_CRLF.replace_all(&source, "\n").to_string();
459461
sources.insert(path.clone(), source);
460462
}
461463

server/src/merge_views.rs

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,10 @@ pub fn generate_merge_list<'a>(
2929

3030
last_offset_set.insert(first_path.clone(), 0);
3131

32-
let line_ending_offset = if is_crlf(sources.get(&first_path).unwrap()) {
33-
2
34-
} else {
35-
1
36-
};
37-
3832
// stack to keep track of the depth first traversal
3933
let mut stack = VecDeque::<NodeIndex>::new();
4034

41-
create_merge_views(&mut nodes_iter, &mut merge_list, &mut last_offset_set, graph, sources, &mut line_directives, &mut stack, line_ending_offset);
35+
create_merge_views(&mut nodes_iter, &mut merge_list, &mut last_offset_set, graph, sources, &mut line_directives, &mut stack);
4236

4337
// now we add a view of the remainder of the root file
4438
let offset = *last_offset_set.get(&first_path).unwrap();
@@ -58,10 +52,6 @@ pub fn generate_merge_list<'a>(
5852
merged
5953
}
6054

61-
fn is_crlf(source: &String) -> bool {
62-
source.contains("\r\n")
63-
}
64-
6555
fn create_merge_views<'a>(
6656
nodes: &mut Peekable<Iter<(NodeIndex, Option<NodeIndex>)>>,
6757
merge_list: &mut LinkedList<&'a str>,
@@ -70,7 +60,6 @@ fn create_merge_views<'a>(
7060
sources: &'a HashMap<PathBuf, String>,
7161
line_directives: &mut Vec<String>,
7262
stack: &mut VecDeque<NodeIndex>,
73-
line_ending_offset: usize,
7463
) {
7564

7665
loop {
@@ -86,7 +75,7 @@ fn create_merge_views<'a>(
8675
let child_path = graph.get_node(child).clone();
8776

8877
let parent_source = sources.get(&parent_path).unwrap();
89-
let (char_for_line, char_following_line) = char_offset_for_line(edge.line, parent_source, line_ending_offset);
78+
let (char_for_line, char_following_line) = char_offset_for_line(edge.line, parent_source);
9079

9180
let offset = *last_offset_set.insert(parent_path.clone(), char_following_line).get_or_insert(0);
9281
merge_list.push_back(&parent_source[offset..char_for_line]);
@@ -101,7 +90,7 @@ fn create_merge_views<'a>(
10190
// if ends in \n\n, we want to exclude the last \n for some reason. Ask optilad
10291
let offset = {
10392
match child_source.ends_with("\n") {
104-
true => child_source.len()-line_ending_offset,
93+
true => child_source.len()-1,
10594
false => child_source.len(),
10695
}
10796
};
@@ -117,15 +106,15 @@ fn create_merge_views<'a>(
117106
}
118107

119108
stack.push_back(parent);
120-
create_merge_views(nodes, merge_list, last_offset_set, graph, sources, line_directives, stack, line_ending_offset);
109+
create_merge_views(nodes, merge_list, last_offset_set, graph, sources, line_directives, stack);
121110
stack.pop_back();
122111

123112
let offset = *last_offset_set.get(&child_path).unwrap();
124113
let child_source = sources.get(&child_path).unwrap();
125114
// this evaluates to false once the file contents have been exhausted aka offset = child_source.len() + 1
126115
let end_offset = {
127116
match child_source.ends_with("\n") {
128-
true => line_ending_offset/* child_source.len()-1 */,
117+
true => 1/* child_source.len()-1 */,
129118
false => 0/* child_source.len() */,
130119
}
131120
};
@@ -148,7 +137,7 @@ fn create_merge_views<'a>(
148137
// if ends in \n\n, we want to exclude the last \n for some reason. Ask optilad
149138
let offset = {
150139
match child_source.ends_with("\n") {
151-
true => child_source.len()-line_ending_offset,
140+
true => child_source.len()-1,
152141
false => child_source.len(),
153142
}
154143
};
@@ -163,15 +152,15 @@ fn create_merge_views<'a>(
163152

164153
// returns the character offset + 1 of the end of line number `line` and the character
165154
// offset + 1 for the end of the line after the previous one
166-
fn char_offset_for_line(line_num: usize, source: &str, line_ending_offset: usize) -> (usize, usize) {
155+
fn char_offset_for_line(line_num: usize, source: &str) -> (usize, usize) {
167156
let mut char_for_line: usize = 0;
168157
let mut char_following_line: usize = 0;
169158
for (n, line) in source.lines().enumerate() {
170159
if n == line_num {
171-
char_following_line += line.len()+line_ending_offset;
160+
char_following_line += line.len()+1;
172161
break;
173162
}
174-
char_for_line += line.len()+line_ending_offset;
163+
char_for_line += line.len()+1;
175164
char_following_line = char_for_line;
176165
}
177166
(char_for_line, char_following_line)

0 commit comments

Comments
 (0)