EAPI void elm_code_line_token_add(Elm_Code_Line *line, int start, int end, int lines, Elm_Code_Token_Type type) { Elm_Code_Token *tok; unsigned int end_line; Elm_Code_Line *next_line; if (!line) return; tok = calloc(1, sizeof(Elm_Code_Token)); end_line = line->number; if (lines > 1) end_line += lines - 1; tok->start = start; tok->end = end; tok->end_line = end_line; tok->type = type; line->tokens = eina_list_append(line->tokens, tok); if (end_line > line->number) { next_line = elm_code_file_line_get(line->file, line->number + 1); elm_code_line_token_add(next_line, 1, end, lines - 1, type); } }
static void _elm_code_widget_undo_change(Evas_Object *widget, Elm_Code_Widget_Change_Info *info) { Elm_Code_Widget_Data *pd; unsigned int textlen, position, row, col, newrow, remainlen; short nllen; char *content; Elm_Code_Line *line; pd = efl_data_scope_get(widget, ELM_CODE_WIDGET_CLASS); if (info->insert) { elm_code_widget_selection_start(widget, info->start_line, info->start_col); elm_code_widget_selection_end(widget, info->end_line, info->end_col); _elm_code_widget_selection_delete_no_undo(widget); } else { newrow = info->start_line; content = info->content; remainlen = info->length; elm_code_widget_selection_clear(widget); elm_code_widget_cursor_position_set(widget, info->start_line, info->start_col); while (newrow <= info->end_line) { line = elm_code_file_line_get(pd->code->file, newrow); if (newrow != info->end_line) { textlen = remainlen; textlen = elm_code_text_newlinenpos(content, remainlen, &nllen); remainlen -= textlen + nllen; _elm_code_widget_text_at_cursor_insert_no_undo(widget, content, textlen); elm_obj_code_widget_cursor_position_get(widget, &row, &col); position = elm_code_widget_line_text_position_for_column_get(widget, line, col); elm_code_line_split_at(line, position); elm_code_widget_cursor_position_set(widget, newrow + 1, 1); content += textlen + nllen; } else { _elm_code_widget_text_at_cursor_insert_no_undo(widget, content, strlen(content)); } newrow++; } elm_code_widget_cursor_position_set(widget, info->end_line, info->end_col + 1); } efl_event_callback_legacy_call(widget, ELM_OBJ_CODE_WIDGET_EVENT_CHANGED_USER, NULL); }
unsigned int _previous_line_scope(Elm_Code_Line *line) { Elm_Code_Line *prev; if (line->number < 2) return 0; prev = elm_code_file_line_get(line->file, line->number - 1); if (!prev) return 0; return prev->scope; }
END_TEST START_TEST (elm_code_test_widget_undo_newline) { Elm_Code *code; Elm_Code_File *file; Elm_Code_Line *line; Elm_Code_Widget *widget; Evas_Object *win; unsigned int length; const char *content; elm_init(1, NULL); code = elm_code_create(); file = elm_code_file_new(code); elm_code_file_line_append(file, "test", 4, NULL); win = elm_win_add(NULL, "entry", ELM_WIN_BASIC); widget = elm_code_widget_add(win, code); elm_code_widget_cursor_position_set(widget, 1, 5); _elm_code_widget_newline(widget); ck_assert_int_eq(2, elm_code_file_lines_get(file)); line = elm_code_file_line_get(file, 1); content = elm_code_line_text_get(line, &length); ck_assert_strn_eq("test", content, 1); elm_code_widget_undo(widget); ck_assert_int_eq(1, elm_code_file_lines_get(file)); line = elm_code_file_line_get(file, 1); content = elm_code_line_text_get(line, &length); ck_assert_strn_eq("test", content, 4); elm_code_free(code); elm_shutdown(); }
EAPI void elm_code_line_split_at(Elm_Code_Line *line, unsigned int position) { Elm_Code_Line *newline; char *content; unsigned int length; content = (char *) elm_code_line_text_get(line, &length); content = strndup(content, length); elm_code_file_line_insert(line->file, line->number + 1, "", 0, NULL); newline = elm_code_file_line_get(line->file, line->number + 1); // TODO we need to split tokens from these lines elm_code_line_text_set(newline, content + position, length - position); elm_code_line_text_set(line, content, position); free(content); }
static Elm_Code_Token_Type _previous_line_continue_type(Elm_Code_Line *line) { Elm_Code_Line *prev; Elm_Code_Token *token; Eina_List *item; if (line->number < 2) return ELM_CODE_TOKEN_TYPE_DEFAULT; prev = elm_code_file_line_get(line->file, line->number - 1); if (!prev || !prev->tokens) return ELM_CODE_TOKEN_TYPE_DEFAULT; EINA_LIST_FOREACH(prev->tokens, item, token) if (token->continues) return token->type; return ELM_CODE_TOKEN_TYPE_DEFAULT; }