mirror of
https://github.com/zed-industries/zed.git
synced 2025-02-06 10:42:08 +00:00
Merge adjacent chunks with the same highlight name in copied JSON
This commit is contained in:
parent
195215f1e0
commit
3dfedd1b21
1 changed files with 17 additions and 3 deletions
|
@ -6345,14 +6345,14 @@ impl Editor {
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct Chunk<'a> {
|
struct Chunk<'a> {
|
||||||
text: &'a str,
|
text: String,
|
||||||
highlight: Option<&'a str>,
|
highlight: Option<&'a str>,
|
||||||
}
|
}
|
||||||
|
|
||||||
let snapshot = buffer.read(cx).snapshot();
|
let snapshot = buffer.read(cx).snapshot();
|
||||||
let chunks = snapshot.chunks(0..snapshot.len(), true);
|
let chunks = snapshot.chunks(0..snapshot.len(), true);
|
||||||
let mut lines = Vec::new();
|
let mut lines = Vec::new();
|
||||||
let mut line = Vec::new();
|
let mut line: Vec<Chunk> = Vec::new();
|
||||||
|
|
||||||
let theme = &cx.global::<Settings>().theme.editor.syntax;
|
let theme = &cx.global::<Settings>().theme.editor.syntax;
|
||||||
|
|
||||||
|
@ -6360,7 +6360,21 @@ impl Editor {
|
||||||
let highlight = chunk.syntax_highlight_id.and_then(|id| id.name(theme));
|
let highlight = chunk.syntax_highlight_id.and_then(|id| id.name(theme));
|
||||||
let mut chunk_lines = chunk.text.split("\n").peekable();
|
let mut chunk_lines = chunk.text.split("\n").peekable();
|
||||||
while let Some(text) = chunk_lines.next() {
|
while let Some(text) = chunk_lines.next() {
|
||||||
line.push(Chunk { text, highlight });
|
let mut merged_with_last_token = false;
|
||||||
|
if let Some(last_token) = line.last_mut() {
|
||||||
|
if last_token.highlight == highlight {
|
||||||
|
last_token.text.push_str(text);
|
||||||
|
merged_with_last_token = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !merged_with_last_token {
|
||||||
|
line.push(Chunk {
|
||||||
|
text: text.into(),
|
||||||
|
highlight,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if chunk_lines.peek().is_some() {
|
if chunk_lines.peek().is_some() {
|
||||||
lines.push(mem::take(&mut line));
|
lines.push(mem::take(&mut line));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue