EncloseLoadWord

This commit is contained in:
Krzosa Karol
2024-08-03 15:50:55 +02:00
parent 7655ece178
commit 0c4dc62170
8 changed files with 51 additions and 22 deletions

View File

@@ -374,7 +374,7 @@ void GenerateConfig() {
Array<Var> colors = {}; Array<Var> colors = {};
colors.add({"Text", "GruvboxDark0Hard"}); colors.add({"Text", "GruvboxDark0Hard"});
colors.add({"TextUnderline", "GruvboxDark0Hard"}); colors.add({"LoadTextHighlight", "0x0000000F"});
colors.add({"Background", "GruvboxLight0Hard"}); colors.add({"Background", "GruvboxLight0Hard"});
colors.add({"InactiveWindow", "0x0000000F"}); colors.add({"InactiveWindow", "0x0000000F"});
colors.add({"TextLineNumbers", "GruvboxDark4"}); colors.add({"TextLineNumbers", "GruvboxDark4"});

View File

@@ -24,8 +24,16 @@ bool IsNonWord(wchar_t w) {
} }
bool IsWord(wchar_t w) { bool IsWord(wchar_t w) {
bool result = IsSymbol(w) || IsWhitespace(w); bool result = !IsNonWord(w);
return !result; return result;
}
bool IsLoadWord(wchar_t w) {
bool result = w == '/' || w == '\\' || w == ':' || w == '*' || w == '_' || w == '.' || w == '-';
if (!result) {
result = !(IsSymbol(w) || IsWhitespace(w));
}
return result;
} }
bool IsAlphabetic(wchar_t a) { bool IsAlphabetic(wchar_t a) {

View File

@@ -220,7 +220,7 @@ Int XYToPosWithoutNL(Buffer &buffer, XY xy) {
Int GetWordStart(Buffer *buffer, Int pos) { Int GetWordStart(Buffer *buffer, Int pos) {
pos = Clamp(pos, (Int)0, buffer->len); pos = Clamp(pos, (Int)0, buffer->len);
for (Int i = pos - 1; i >= 0; i -= 1) { for (Int i = pos - 1; i >= 0; i -= 1) {
if (IsNonWord(buffer->str[i])) break; if (!IsWord(buffer->str[i])) break;
pos = i; pos = i;
} }
return pos; return pos;
@@ -230,7 +230,25 @@ Int GetWordEnd(Buffer *buffer, Int pos) {
pos = Clamp(pos, (Int)0, buffer->len); pos = Clamp(pos, (Int)0, buffer->len);
for (Int i = pos; i < buffer->len; i += 1) { for (Int i = pos; i < buffer->len; i += 1) {
pos = i; pos = i;
if (IsNonWord(buffer->str[i])) break; if (!IsWord(buffer->str[i])) break;
}
return pos;
}
Int GetLoadWordStart(Buffer *buffer, Int pos) {
pos = Clamp(pos, (Int)0, buffer->len);
for (Int i = pos - 1; i >= 0; i -= 1) {
if (!IsLoadWord(buffer->str[i])) break;
pos = i;
}
return pos;
}
Int GetLoadWordEnd(Buffer *buffer, Int pos) {
pos = Clamp(pos, (Int)0, buffer->len);
for (Int i = pos; i < buffer->len; i += 1) {
pos = i;
if (!IsLoadWord(buffer->str[i])) break;
} }
return pos; return pos;
} }
@@ -321,4 +339,14 @@ Int GetPrevEmptyLineStart(Buffer *buffer, Int pos) {
if (whitespace_line) break; if (whitespace_line) break;
} }
return result; return result;
} }
Range EncloseWord(Buffer *buffer, Int pos) {
Range result = {GetWordStart(buffer, pos), GetWordEnd(buffer, pos)};
return result;
}
Range EncloseLoadWord(Buffer *buffer, Int pos) {
Range result = {GetLoadWordStart(buffer, pos), GetLoadWordEnd(buffer, pos)};
return result;
}

View File

@@ -94,13 +94,6 @@ Int MovePos(Buffer &buffer, Int pos, int direction, bool ctrl_pressed = false) {
} }
} }
Range EncloseWord(Buffer &buffer, Int pos) {
Range result = {};
result.min = MoveOnWhitespaceBoundary(buffer, pos, DIR_LEFT);
result.max = MoveOnWhitespaceBoundary(buffer, pos, DIR_RIGHT);
return result;
}
Caret FindInBuffer(Buffer *buffer, String16 needle, Caret caret, bool find_next = false) { Caret FindInBuffer(Buffer *buffer, String16 needle, Caret caret, bool find_next = false) {
Int pos = caret.range.min; Int pos = caret.range.min;
String16 medium = GetString(*buffer, {pos, INT64_MAX}); String16 medium = GetString(*buffer, {pos, INT64_MAX});

View File

@@ -702,7 +702,7 @@ void WindowCommand(Event event, Window *window, View *view) {
} else if (mouse_in_document && Mouse(LEFT) && event.mouse_double_click) { } else if (mouse_in_document && Mouse(LEFT) && event.mouse_double_click) {
Caret *c = &view->carets[0]; Caret *c = &view->carets[0];
if (InBounds({c->range.min - 1, c->range.max + 1}, p)) { if (InBounds({c->range.min - 1, c->range.max + 1}, p)) {
c->range = EncloseWord(*buffer, p); c->range = EncloseWord(buffer, p);
view->selection_anchor = c->range; view->selection_anchor = c->range;
} else { } else {
view->selection_anchor = Rng(p); view->selection_anchor = Rng(p);

View File

@@ -38,7 +38,7 @@ local GruvboxFadedAqua = 0x427b58ff
local GruvboxFadedOrange = 0xaf3a03ff local GruvboxFadedOrange = 0xaf3a03ff
Color = {} Color = {}
Color.Text = GruvboxDark0Hard Color.Text = GruvboxDark0Hard
Color.TextUnderline = GruvboxDark0Hard Color.LoadTextHighlight = 0x0000000F
Color.Background = GruvboxLight0Hard Color.Background = GruvboxLight0Hard
Color.InactiveWindow = 0x0000000F Color.InactiveWindow = 0x0000000F
Color.TextLineNumbers = GruvboxDark4 Color.TextLineNumbers = GruvboxDark4
@@ -199,7 +199,7 @@ end
)=="; )==";
void ReloadStyle() { void ReloadStyle() {
ColorText = GetColor("Text", ColorText); ColorText = GetColor("Text", ColorText);
ColorTextUnderline = GetColor("TextUnderline", ColorTextUnderline); ColorLoadTextHighlight = GetColor("LoadTextHighlight", ColorLoadTextHighlight);
ColorBackground = GetColor("Background", ColorBackground); ColorBackground = GetColor("Background", ColorBackground);
ColorInactiveWindow = GetColor("InactiveWindow", ColorInactiveWindow); ColorInactiveWindow = GetColor("InactiveWindow", ColorInactiveWindow);
ColorTextLineNumbers = GetColor("TextLineNumbers", ColorTextLineNumbers); ColorTextLineNumbers = GetColor("TextLineNumbers", ColorTextLineNumbers);

View File

@@ -36,7 +36,7 @@ Color GruvboxFadedPurple = {0x8f, 0x3f, 0x71, 0xff};
Color GruvboxFadedAqua = {0x42, 0x7b, 0x58, 0xff}; Color GruvboxFadedAqua = {0x42, 0x7b, 0x58, 0xff};
Color GruvboxFadedOrange = {0xaf, 0x3a, 0x03, 0xff}; Color GruvboxFadedOrange = {0xaf, 0x3a, 0x03, 0xff};
Color ColorText = GruvboxDark0Hard; Color ColorText = GruvboxDark0Hard;
Color ColorTextUnderline = GruvboxDark0Hard; Color ColorLoadTextHighlight = {0x00, 0x00, 0x00, 0x0F};
Color ColorBackground = GruvboxLight0Hard; Color ColorBackground = GruvboxLight0Hard;
Color ColorInactiveWindow = {0x00, 0x00, 0x00, 0x0F}; Color ColorInactiveWindow = {0x00, 0x00, 0x00, 0x0F};
Color ColorTextLineNumbers = GruvboxDark4; Color ColorTextLineNumbers = GruvboxDark4;

View File

@@ -167,22 +167,22 @@ void DrawWindow(Window *window) {
} }
} }
{ if (is_active) {
Caret caret = view->carets[0]; Caret caret = view->carets[0];
if (GetSize(caret.range) == 0) { if (GetSize(caret.range) == 0) {
Int pos = caret.range.min; Int pos = caret.range.min;
if (pos < buffer->len && !IsWhitespace(buffer->str[pos])) { if (pos < buffer->len) {
Range range = EncloseWord(*buffer, pos); Range range = EncloseLoadWord(buffer, pos);
XY xy_min = PosToXY(*buffer, range.min); XY xy_min = PosToXY(*buffer, range.min);
XY xy_max = PosToXY(*buffer, range.max); XY xy_max = PosToXY(*buffer, range.max);
Vec2I min = {xy_min.col * FontCharSpacing, (xy_min.line + 1) * FontLineSpacing - 2}; Vec2I min = {xy_min.col * FontCharSpacing, (xy_min.line) * FontLineSpacing};
Vec2I max = {xy_max.col * FontCharSpacing, (xy_max.line + 1) * FontLineSpacing}; Vec2I max = {xy_max.col * FontCharSpacing, (xy_max.line + 1) * FontLineSpacing};
Rect2I rect = {min, max}; Rect2I rect = {min, max};
rect -= view->scroll; rect -= view->scroll;
rect += window->document_rect.min; rect += window->document_rect.min;
DrawRect(rect, ColorTextUnderline); DrawRect(rect, ColorLoadTextHighlight);
} }
} }
} }