-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added patch/diff file highlighting for easy at a glance viewing. Does not try to highlight based on language, just a simple green for additions and red for deletions, plus some extra colours for metadata. (This could be changed in the future if we did want to highlight per language since the regions I added could stay the same and just change the token colours... That would involve needing to detect language based on the file extensions of the diff'd files though which might be kind of slow, especially for large patches that involve many files.)
- Loading branch information
1 parent
948529b
commit d43050d
Showing
12 changed files
with
233 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -357,4 +357,7 @@ Token_Type :: enum u8 { | |
header4; | ||
header5; | ||
header6; | ||
|
||
addition; | ||
deletion; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
tokenize_diff :: (using buffer: *Buffer, start_offset := -1, count := -1) -> [] Buffer_Region { | ||
tokenizer: Diff_Tokenizer; | ||
tokenizer.base = get_tokenizer(buffer, start_offset, count); | ||
|
||
while true { | ||
token := get_next_token(*tokenizer); | ||
if token.type == .eof break; | ||
|
||
memset(tokens.data + token.start, xx token.type, token.len); | ||
} | ||
|
||
if tokenizer.current_region_id >= 0 then end_region(*tokenizer, tokenizer.t - tokenizer.buf.data); | ||
quick_sort(tokenizer.regions, (a, b) => (a.start - b.start)); | ||
return tokenizer.regions; | ||
} | ||
|
||
#scope_file | ||
|
||
get_next_token :: (using tokenizer: *Diff_Tokenizer) -> Token { | ||
token := Token.{ | ||
start = cast(s32) (t - buf.data), | ||
type = .eof, | ||
}; | ||
|
||
if t >= max_t return token; | ||
|
||
start_t = t; | ||
char := t.*; | ||
|
||
if char == { | ||
case #char "+"; parse_plus (tokenizer, *token); | ||
case #char "-"; parse_minus (tokenizer, *token); | ||
case #char "@"; parse_range (tokenizer, *token); | ||
case #char " "; parse_unchanged(tokenizer, *token); | ||
case; parse_metadata (tokenizer, *token); | ||
} | ||
|
||
if t >= max_t then t = max_t; | ||
token.len = cast(s32) (t - start_t); | ||
return token; | ||
} | ||
|
||
parse_plus :: (using tokenizer: *Diff_Tokenizer, token: *Token) { | ||
token.type = .addition; | ||
|
||
t += 1; | ||
if t >= max_t then return; | ||
|
||
current := t.*; | ||
next := (t + 1).*; | ||
if current == #char "+" && next == #char "+" { | ||
eat_until_newline(tokenizer); | ||
t += 1; | ||
} | ||
else { | ||
if current_region_id == -1 || regions[current_region_id].kind != .addition { | ||
start_region(tokenizer, token.start, .addition); | ||
} | ||
eat_until_newline(tokenizer); | ||
t += 1; | ||
} | ||
|
||
if t > max_t then t = max_t; | ||
} | ||
|
||
parse_minus :: (using tokenizer: *Diff_Tokenizer, token: *Token) { | ||
token.type = .deletion; | ||
|
||
t += 1; | ||
if t >= max_t then return; | ||
|
||
current := t.*; | ||
next := (t + 1).*; | ||
if current == #char "-" && next == #char "-" { | ||
eat_until_newline(tokenizer); | ||
t += 1; | ||
} | ||
else { | ||
if current_region_id == -1 || regions[current_region_id].kind != .deletion { | ||
start_region(tokenizer, token.start, .deletion); | ||
} | ||
eat_until_newline(tokenizer); | ||
t += 1; | ||
} | ||
|
||
if t > max_t then t = max_t; | ||
} | ||
|
||
parse_range :: (using tokenizer: *Diff_Tokenizer, token: *Token) { | ||
t += 1; | ||
if t >= max_t || t.* != #char "@" { | ||
parse_metadata(tokenizer, token); | ||
return; | ||
} | ||
|
||
token.type = .number; | ||
if current_region_id != -1 then end_region(tokenizer, token.start); | ||
eat_until_newline(tokenizer); | ||
t += 1; | ||
} | ||
|
||
parse_unchanged :: (using tokenizer: *Diff_Tokenizer, token: *Token) { | ||
token.type = .identifier; | ||
if current_region_id != -1 then end_region(tokenizer, token.start); | ||
eat_until_newline(tokenizer); | ||
t += 1; | ||
} | ||
|
||
parse_metadata :: (using tokenizer: *Diff_Tokenizer, token: *Token) { | ||
token.type = .string_literal; | ||
if current_region_id != -1 then end_region(tokenizer, token.start); | ||
eat_until_newline(tokenizer); | ||
t += 1; | ||
} | ||
|
||
start_region :: (using tokenizer: *Diff_Tokenizer, offset: s64, kind: Buffer_Region.Kind) { | ||
if current_region_id >= 0 then end_region(tokenizer, offset); | ||
current_region_id = regions.count; | ||
|
||
region := Buffer_Region.{ | ||
start = xx offset, | ||
end = -1, | ||
kind = kind, | ||
}; | ||
array_add(*regions, region); | ||
} | ||
|
||
end_region :: inline (using tokenizer: *Diff_Tokenizer, offset: s64) { | ||
regions[current_region_id].end = xx offset; | ||
current_region_id = -1; | ||
} | ||
|
||
Diff_Tokenizer :: struct { | ||
using #as base: Tokenizer; | ||
|
||
regions: [..] Buffer_Region; | ||
regions.allocator = temp; | ||
current_region_id := -1; | ||
} | ||
|
||
Token :: struct { | ||
start, len: s32; | ||
type: Token_Type; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters