This commit is contained in:
FantasyPvP
2024-12-05 02:09:18 +00:00
parent 3f35a00526
commit 00042ea092
4 changed files with 110 additions and 327 deletions
-270
View File
@@ -1,270 +0,0 @@
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
REMOVED
REMOVED
REMOVED
REMOVED
REMOVED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
ADDED
ADDED
ADDED
ADDED
ADDED
ADDED
ADDED
ADDED
ADDED
ADDED
REMOVED
REMOVED
REMOVED
REMOVED
REMOVED
REMOVED
REMOVED
REMOVED
REMOVED
REMOVED
REMOVED
REMOVED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
UNMODIFIED
+19 -6
View File
@@ -177,6 +177,7 @@ void newline(Editor* self) {
move_cursor_on_screen(self, self->buffer_col, self->buffer_line); move_cursor_on_screen(self, self->buffer_col, self->buffer_line);
} }
// instantiates a new editor object given an input string and a filename
Editor editor_from(String_t input_string, String_t filename) { Editor editor_from(String_t input_string, String_t filename) {
Editor e; Editor e;
e.lines = 0; e.lines = 0;
@@ -203,10 +204,12 @@ Editor editor_from(String_t input_string, String_t filename) {
return e; return e;
} }
// creates a new editor object from a filename with an emptry buffer
Editor new_editor(String_t filename) { Editor new_editor(String_t filename) {
return editor_from(str_new(), filename); return editor_from(str_new(), filename);
} }
// prints the changelog for the editor.
int print_diff(Editor* self) { int print_diff(Editor* self) {
char buffer[512]; char buffer[512];
@@ -216,6 +219,8 @@ int print_diff(Editor* self) {
for (size_t i = 0; i < self->diff.size; i++) { for (size_t i = 0; i < self->diff.size; i++) {
LineState state = self->diff.data[i]; LineState state = self->diff.data[i];
// for each token in the state, we print the corresponding line if there is a change.
switch (state) { switch (state) {
// case UNMODIFIED: // case UNMODIFIED:
// original_offset = arr_original_offset(&self->diff, i); // original_offset = arr_original_offset(&self->diff, i);
@@ -227,7 +232,6 @@ int print_diff(Editor* self) {
// ); // );
// str_push_str(&log_string, buffer); // str_push_str(&log_string, buffer);
// break; // break;
case MODIFIED: case MODIFIED:
original_offset = arr_original_offset(&self->diff, i); original_offset = arr_original_offset(&self->diff, i);
modified_offset = arr_modified_offset(&self->diff, i); modified_offset = arr_modified_offset(&self->diff, i);
@@ -279,6 +283,7 @@ int print_diff(Editor* self) {
str_dealloc(&log_string); str_dealloc(&log_string);
} }
// saves the current buffer to a file
int save_file(Editor* self) { int save_file(Editor* self) {
String_t log_string = str_new(); String_t log_string = str_new();
char log_message[64]; char log_message[64];
@@ -299,7 +304,8 @@ int save_file(Editor* self) {
} }
} }
arr_print(&self->diff); // debug method
// arr_print(&self->diff);
print_diff(self); print_diff(self);
free(name); free(name);
@@ -310,12 +316,11 @@ int save_file(Editor* self) {
} }
free(self->original); free(self->original);
self->original = str_clone_all(self->buffer, self->lines); self->original = str_clone_all(self->buffer, self->lines);
self->original_len = self->lines;
snprintf(log_message, 64, "Edited file [%s]\n", to_chars(&self->filename)); snprintf(log_message, 64, "Edited file [%s]\n", to_chars(&self->filename));
str_push_str(&log_string, log_message); str_push_str(&log_string, log_message);
self->original_len = self->lines;
snprintf(log_message, 64, "Old Length: [%d] New Length: [%d]\n", self->original_len, self->lines); snprintf(log_message, 64, "Old Length: [%d] New Length: [%d]\n", self->original_len, self->lines);
str_push_str(&log_string, log_message); str_push_str(&log_string, log_message);
@@ -333,6 +338,8 @@ int save_file(Editor* self) {
return 0; return 0;
} }
// move the cursor around the screen
// this uses extensive bounds checking to ensure that the cursor moves to the next line correctly
void move_cursor(Editor* self, int x, int y) { void move_cursor(Editor* self, int x, int y) {
if (y != 0 if (y != 0
&& (int)(self->buffer_line) + y >= 0 && (int)(self->buffer_line) + y >= 0
@@ -420,8 +427,13 @@ void pressed_enter(Editor* self) {
} }
self->buffer = (String_t*)realloc(self->buffer, sizeof(String_t) * (self->capacity)); self->buffer = (String_t*)realloc(self->buffer, sizeof(String_t) * (self->capacity));
// logs the change of adding a new line if (self->buffer_col == 0) {
arr_insert(&self->diff, self->buffer_line + 1, ADDED); arr_insert(&self->diff, self->buffer_line, ADDED);
} else if (self->buffer_col == str_len(&self->buffer[self->buffer_line])) {
arr_insert(&self->diff, self->buffer_line + 1, ADDED);
} else {
arr_insert(&self->diff, self->buffer_line + 1, ADDED);
}
if (self->buffer_line == self->lines -1) { if (self->buffer_line == self->lines -1) {
// if at end of file add an empty line // if at end of file add an empty line
@@ -490,6 +502,7 @@ void addchar(Editor* self, char c) {
move_cursor(self, 1, 0); move_cursor(self, 1, 0);
} }
// closes the editor and ensures that all used resources are deallocated
void shutdown_editor(Editor* self) { void shutdown_editor(Editor* self) {
for (size_t i = 0; i < self->capacity; i++) { for (size_t i = 0; i < self->capacity; i++) {
str_dealloc(&self->buffer[i]); str_dealloc(&self->buffer[i]);
+52 -23
View File
@@ -1,27 +1,56 @@
REMOVED 16 | ADDED 18 |
REMOVED 17 | ADDED 19 |
REMOVED 18 | modifie ADDED 20 |
Edited file [somefile.c]
Old Length: [254] New Length: [254]
REMOVED 18 |
REMOVED 19 | REMOVED 19 |
REMOVED 20 | REMOVED 20 |
ADDED 35 | } AND modified a line. Edited file [somefile.c]
ADDED 36 | Old Length: [251] New Length: [251]
ADDED 37 |
ADDED 38 | added some lines
ADDED 39 |
ADDED 40 |
ADDED 41 |
ADDED 42 | ADDED 42 |
ADDED 43 | ADDED 43 | fn main() -> idk {
ADDED 44 | ADDED 44 | print("")
REMOVED 40 | } AND modified a line. ADDED 46 |
REMOVED 41 | Edited file [somefile.c]
Old Length: [255] New Length: [255]
REMOVED 42 | REMOVED 42 |
REMOVED 43 | added some lines MODIFIED 43 | fn main() -> idk {
REMOVED 44 | -> 42 |
REMOVED 45 | MODIFIED 44 | print("")
REMOVED 46 | -> 43 |
REMOVED 47 | MODIFIED 45 | }
REMOVED 48 | -> 44 | String_t str_from_chars(const char* string) {
REMOVED 49 | Edited file [somefile.c]
REMOVED 50 | Old Length: [252] New Length: [252]
REMOVED 51 | Opened File [somefile.c]
ADDED 28 |
REMOVED 28 |
Edited file [somefile.c]
Old Length: [252] New Length: [252]
ADDED 42 |
REMOVED 42 |
Edited file [somefile.c]
Old Length: [252] New Length: [252]
ADDED 47 |
ADDED 48 |
Edited file [somefile.c]
Old Length: [254] New Length: [254]
MODIFIED 48 |
-> 48 | fn some() -> idk {
ADDED 49 | print(idk)
ADDED 50 | }
Edited file [somefile.c]
Old Length: [256] New Length: [256]
Opened File [somefile.c]
ADDED 28 |
REMOVED 28 |
Edited file [somefile.c]
Old Length: [256] New Length: [256]
ADDED 28 |
ADDED 29 |
ADDED 30 |
ADDED 31 |
Edited file [somefile.c]
Old Length: [260] New Length: [260]
Opened File [somefile.c]
+38 -27
View File
@@ -21,6 +21,7 @@ typedef struct {
} StateArray; } StateArray;
StateArray arr_init(int capacity) { StateArray arr_init(int capacity) {
// creates a new array to store the state
StateArray arr; StateArray arr;
arr.size = capacity; arr.size = capacity;
arr.capacity = capacity; arr.capacity = capacity;
@@ -32,39 +33,49 @@ StateArray arr_init(int capacity) {
return arr; return arr;
} }
void arr_print(StateArray* self) { // prints the state of the array into a file
FILE* log_file = fopen("diff.txt", "w"); // this method is for debug purposes!
if (log_file == NULL) { // void arr_print(StateArray* self) {
fprintf(stderr, "Error: Could not open log file\n"); // FILE* log_file = fopen("diff.txt", "w");
} // if (log_file == NULL) {
// fprintf(stderr, "Error: Could not open log file\n");
// }
for (int i = 0; i < self->size; i++) { // for (int i = 0; i < self->size; i++) {
switch (self->data[i]) { // switch (self->data[i]) {
case UNMODIFIED: // case UNMODIFIED:
fwrite("UNMODIFIED\n", sizeof(char), strlen("UNMODIFIED\n"), log_file); // fwrite("UNMODIFIED\n", sizeof(char), strlen("UNMODIFIED\n"), log_file);
break; // break;
case MODIFIED: // case MODIFIED:
fwrite("MODIFIED\n", sizeof(char), strlen("MODIFIED\n"), log_file); // fwrite("MODIFIED\n", sizeof(char), strlen("MODIFIED\n"), log_file);
break; // break;
case ADDED: // case ADDED:
fwrite("ADDED\n", sizeof(char), strlen("ADDED\n"), log_file); // fwrite("ADDED\n", sizeof(char), strlen("ADDED\n"), log_file);
break; // break;
case REMOVED: // case REMOVED:
fwrite("REMOVED\n", sizeof(char), strlen("REMOVED\n"), log_file); // fwrite("REMOVED\n", sizeof(char), strlen("REMOVED\n"), log_file);
break; // break;
} // }
} // }
fclose(log_file); // fclose(log_file);
} // }
// calculates an offset to use for indexing into the state relative to the buffer.
// this is because some elements might not be aligned due to needing to insert the "REMOVED" element in some cases.
int arr_added_or_removed_before(StateArray* self, int index) { int arr_added_or_removed_before(StateArray* self, int index) {
int count = 0; int count = 0;
for (int i = 0; i < index; i++) { for (int i = 0; i < index; i++) {
if (self->data[i] == ADDED || self->data[i] == REMOVED) { // if (self->data[i] == ADDED || self->data[i] == REMOVED) {
if (self->data[i] == REMOVED) { // if (self->data[i] == REMOVED) {
index++; // // every time a "REMOVED" element is found, we increase the index by 1
} // index++;
// }
// count++;
// }
if (self->data[i] == REMOVED) {
// every time a "REMOVED" element is found, we increase the index by 1
index++;
count++; count++;
} }
} }