done
This commit is contained in:
+20
-7
@@ -177,6 +177,7 @@ void newline(Editor* self) {
|
||||
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 e;
|
||||
e.lines = 0;
|
||||
@@ -203,10 +204,12 @@ Editor editor_from(String_t input_string, String_t filename) {
|
||||
return e;
|
||||
}
|
||||
|
||||
// creates a new editor object from a filename with an emptry buffer
|
||||
Editor new_editor(String_t filename) {
|
||||
return editor_from(str_new(), filename);
|
||||
}
|
||||
|
||||
// prints the changelog for the editor.
|
||||
int print_diff(Editor* self) {
|
||||
|
||||
char buffer[512];
|
||||
@@ -216,6 +219,8 @@ int print_diff(Editor* self) {
|
||||
|
||||
for (size_t i = 0; i < self->diff.size; 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) {
|
||||
// case UNMODIFIED:
|
||||
// original_offset = arr_original_offset(&self->diff, i);
|
||||
@@ -227,7 +232,6 @@ int print_diff(Editor* self) {
|
||||
// );
|
||||
// str_push_str(&log_string, buffer);
|
||||
// break;
|
||||
|
||||
case MODIFIED:
|
||||
original_offset = arr_original_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);
|
||||
}
|
||||
|
||||
// saves the current buffer to a file
|
||||
int save_file(Editor* self) {
|
||||
String_t log_string = str_new();
|
||||
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);
|
||||
|
||||
free(name);
|
||||
@@ -310,12 +316,11 @@ int save_file(Editor* self) {
|
||||
}
|
||||
free(self->original);
|
||||
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));
|
||||
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);
|
||||
str_push_str(&log_string, log_message);
|
||||
|
||||
@@ -324,7 +329,7 @@ int save_file(Editor* self) {
|
||||
fprintf(stderr, "Error writing log\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
printf("wrote log");
|
||||
str_dealloc(&log_string);
|
||||
|
||||
@@ -333,6 +338,8 @@ int save_file(Editor* self) {
|
||||
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) {
|
||||
if (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));
|
||||
|
||||
// logs the change of adding a new line
|
||||
arr_insert(&self->diff, self->buffer_line + 1, ADDED);
|
||||
if (self->buffer_col == 0) {
|
||||
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 at end of file add an empty line
|
||||
@@ -490,6 +502,7 @@ void addchar(Editor* self, char c) {
|
||||
move_cursor(self, 1, 0);
|
||||
}
|
||||
|
||||
// closes the editor and ensures that all used resources are deallocated
|
||||
void shutdown_editor(Editor* self) {
|
||||
for (size_t i = 0; i < self->capacity; i++) {
|
||||
str_dealloc(&self->buffer[i]);
|
||||
|
||||
Reference in New Issue
Block a user