Skip to content

Commit

Permalink
faster cursor position calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixKratz committed Feb 17, 2022
1 parent 862fa8a commit 4112c67
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 20 deletions.
5 changes: 2 additions & 3 deletions src/ax.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "ax.h"
#include "buffer.h"
#include <stdint.h>

void ax_begin(struct ax* ax) {
buffer_begin(&ax->buffer);
Expand Down Expand Up @@ -77,7 +76,7 @@ bool ax_get_cursor(struct ax* ax) {

bool ax_set_text(struct ax* ax) {
if (!ax->is_supported) return false;
if (!ax->buffer.did_change) return true;
if (!ax->buffer.lines_changed) return true;

CFStringRef text_ref = CFStringCreateWithCString(NULL,
ax->buffer.raw,
Expand All @@ -99,7 +98,7 @@ bool ax_set_cursor(struct ax* ax) {
AXValueRef value = AXValueCreate(kAXValueCFRangeType, &text_range);
// HACK: This is needed when the text has been set to give the
// HACK: AX API some time to breathe...
if (ax->buffer.did_change) usleep(15000);
if (ax->buffer.lines_changed) usleep(15000);

AXError error = AXUIElementSetAttributeValue(ax->selected_element,
kAXSelectedTextRangeAttribute,
Expand Down
23 changes: 11 additions & 12 deletions src/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,24 @@ void buffer_sync_text(struct buffer* buffer) {
buffer->lines = realloc(buffer->lines, sizeof(struct line*) * lines);
buffer->line_count = lines;

buffer->did_change = false;
buffer->lines_changed = 0;
uint32_t cursor_pos_cummulative = 0;
for (int i = 1; i <= lines; i++) {
char_u* line = vimBufferGetLine(buffer->vbuf, i);
if (i > old_count) buffer->lines[i - 1] = line_create();
line_set_text(buffer->lines[i - 1], line);
buffer->did_change |= buffer->lines[i - 1]->changed;
buffer->lines[i - 1]->cursor_offset = cursor_pos_cummulative;
cursor_pos_cummulative += buffer->lines[i - 1]->length + 1;
buffer->lines_changed += buffer->lines[i - 1]->changed;
}
}

void buffer_sync_cursor(struct buffer* buffer) {
pos_T cursor_pos = vimCursorGetPosition();
uint32_t pos = line_get_position_from_raw_position(buffer->lines[cursor_pos.lnum - 1],
cursor_pos.col);

uint32_t cursor_pos_cummulative = 0;
for (int i = 0; i < cursor_pos.lnum - 1; i++) {
cursor_pos_cummulative += buffer->lines[i]->length + 1;
}

uint32_t pos = line_get_position_from_raw_position(buffer->lines[cursor_pos.lnum - 1], cursor_pos.col);

buffer->cursor.position = cursor_pos_cummulative + pos;
buffer->cursor.position = buffer->lines[cursor_pos.lnum - 1]->cursor_offset + pos;

if (buffer->cursor.mode & NORMAL) {
buffer->cursor.selection = 1;
Expand Down Expand Up @@ -108,7 +106,8 @@ void buffer_sync_cursor(struct buffer* buffer) {
int visual_mode = vimVisualGetType();

if (visual_mode == VISUAL_LINE) {
buffer->cursor.position = cursor_pos_cummulative - (inverted ? 0 : selection);
buffer->cursor.position = buffer->lines[cursor_pos.lnum - 1]->cursor_offset
- (inverted ? 0 : selection);
selection += buffer->lines[end.lnum - 1]->length;
buffer->cursor.selection = selection;
}
Expand Down Expand Up @@ -253,7 +252,7 @@ void buffer_clear(struct buffer* buffer) {
line_clear(&buffer->command_line);

buffer->cursor = (struct cursor){0, 0, 0};
buffer->did_change = false;
buffer->lines_changed = 0;
buffer->line_count = 0;
buffer->lines = NULL;
buffer->raw = NULL;
Expand Down
3 changes: 2 additions & 1 deletion src/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define NORMAL_MODE "<esc>"
#define INSERT_MODE "i"

#define VISUAL_BLOCK 0x16
#define VISUAL_LINE 0x56

extern const char* read_file(char* path);
Expand All @@ -26,7 +27,7 @@ struct buffer {
buf_T* vbuf;
char_u* raw;

bool did_change;
uint32_t lines_changed;
struct line command_line;
char cmd_line_mode;

Expand Down
1 change: 0 additions & 1 deletion src/event_tap.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "event_tap.h"
#include <stdint.h>

bool event_tap_check_blacklist(struct event_tap* event_tap, char* app) {
if (!app) return true;
Expand Down
4 changes: 1 addition & 3 deletions src/line.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#include "line.h"
#include <stdint.h>
#include <string.h>
#include <wchar.h>

struct line* line_create() {
struct line* line = malloc(sizeof(struct line));
Expand Down Expand Up @@ -40,6 +37,7 @@ void line_clear(struct line* line) {
if (line->raw) free(line->raw);
line->raw = NULL;
line->text = NULL;
line->cursor_offset = 0;
line->length = 0;
line->raw_length = 0;
}
Expand Down
3 changes: 3 additions & 0 deletions src/line.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@

struct line {
bool changed;

uint32_t length;
uint32_t raw_length;
uint32_t cursor_offset;

wchar_t* text;
char* raw;
};
Expand Down

0 comments on commit 4112c67

Please sign in to comment.