Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion src/studio/editors/code.c
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,51 @@ static s32 getLineSize(const char* line)
return size;
}

static s32 getTabColumnWidth(Code* code, s32 column)
{
s32 tabSize = getConfig(code->studio)->options.tabSize;
if(tabSize <= 0) tabSize = 1;

return tabSize - column % tabSize;
}

static char* getVisualPosByLine(Code* code, char* line, s32 column)
{
if(column <= 0) return line;

s32 visual = 0;
char* ptr = line;

while(*ptr && *ptr != '\n')
{
if(*ptr == '\t')
{
s32 width = getTabColumnWidth(code, visual);

if(column < visual + width)
{
s32 left = column - visual;
s32 right = visual + width - column;

return right <= left ? ptr + 1 : ptr;
}

visual += width;
ptr++;

continue;
}

if(column == visual)
return ptr;

visual++;
ptr++;
}

return ptr;
}

static void updateColumn(Code* code)
{
code->cursor.column = (s32)(code->cursor.position - getLine(code));
Expand All @@ -826,6 +871,12 @@ static void updateCursorPosition(Code* code, char* position)
updateEditor(code);
}

static void setCursorPositionVisual(Code* code, s32 cx, s32 cy)
{
char* line = getPosByLine(code->src, cy);
updateCursorPosition(code, getVisualPosByLine(code, line, cx));
}

static void setCursorPosition(Code* code, s32 cx, s32 cy)
{
s32 x = 0;
Expand Down Expand Up @@ -3140,7 +3191,7 @@ static void processMouse(Code* code)
s32 y = (my - rect.y) / STUDIO_TEXT_HEIGHT;

char* position = code->cursor.position;
setCursorPosition(code, x + code->scroll.x, y + code->scroll.y);
setCursorPositionVisual(code, x + code->scroll.x, y + code->scroll.y);

if(tic_api_key(tic, tic_key_shift))
{
Expand Down