Invert command window

This commit is contained in:
krzosa
2025-12-22 21:24:35 +01:00
parent 05652240f1
commit edb2379bce
7 changed files with 102 additions and 105 deletions

View File

@@ -553,55 +553,6 @@ API Range GetIndentRangeAtPos(Buffer *buffer, Int pos) {
return {range.min, range.min + indent}; return {range.min, range.min + indent};
} }
const Int FuzzyCloserWordBegin = 5;
const Int FuzzyConsecutiveMultiplier = 3;
API Int FuzzyRate(String16 string, String16 with) {
if (with.len == 0) return 0;
Int points = 0;
Int consecutive = 0;
Int with_i = 0;
for (Int i = 0; i < string.len; i++) {
if (string.data[i] == with[with_i]) {
Int closer_begin = ClampBottom((Int)0, FuzzyCloserWordBegin - i);
points += closer_begin;
consecutive++;
with_i += 1;
} else {
points += consecutive * FuzzyConsecutiveMultiplier;
consecutive = 0;
with_i = 0;
}
if (with_i >= with.len) with_i = 0;
}
points += consecutive * FuzzyConsecutiveMultiplier;
return points;
}
API Array<FuzzyPair> FuzzySearchLines(Allocator allocator, Buffer *buffer, Int line_min, Int line_max, String16 needle) {
if (line_min < 0 || line_min >= buffer->line_starts.len) return {};
if (line_max < 0 || line_min > buffer->line_starts.len) return {};
Array<FuzzyPair> ratings = {allocator};
for (Int i = line_min; i < line_max; i += 1) {
String16 s = GetLineStringWithoutNL(buffer, i);
Int rating = FuzzyRate(s, needle);
Add(&ratings, {i, rating});
}
// bubble sort
for (Int i = 0; i < ratings.len - 1; i++) {
for (Int j = 0; j < ratings.len - 1; j++) {
if (ratings[j].rating < ratings[j + 1].rating) {
FuzzyPair temp = ratings[j];
ratings[j] = ratings[j + 1];
ratings[j + 1] = temp;
}
}
}
return ratings;
}
API Int FindRangeByPos(Array<Range> *ranges, Int pos) { API Int FindRangeByPos(Array<Range> *ranges, Int pos) {
// binary search // binary search
Int low = 0; Int low = 0;
@@ -1538,6 +1489,7 @@ String16 ToUnixString16(Allocator allocator, String string_) {
} }
Buffer *BufferOpenFile(String path) { Buffer *BufferOpenFile(String path) {
ProfileFunction();
Allocator sys_allocator = GetSystemAllocator(); Allocator sys_allocator = GetSystemAllocator();
Scratch scratch; Scratch scratch;

View File

@@ -648,6 +648,7 @@ void Command_SetWorkDir() {
String CodeEndings[] = { ".c", ".cpp", ".h", ".hpp", ".py", ".lua", ".cxx", ".hxx", }; String CodeEndings[] = { ".c", ".cpp", ".h", ".hpp", ".py", ".lua", ".cxx", ".hxx", };
void OpenCodeRecursive(String dir) { void OpenCodeRecursive(String dir) {
ProfileFunction();
Scratch scratch; Scratch scratch;
for (FileIter it = IterateFiles(scratch, dir); IsValid(it); Advance(&it)) { for (FileIter it = IterateFiles(scratch, dir); IsValid(it); Advance(&it)) {
if (it.filename == ".git") { if (it.filename == ".git") {
@@ -818,7 +819,7 @@ void Command_MakeFontSmaller() {
void Command_Open() { void Command_Open() {
BSet active = GetBSet(ActiveWindowID); BSet active = GetBSet(ActiveWindowID);
if (active.window->id == CommandBarWindowID) { if (active.window->id == CommandWindowID) {
CommandWindowOpen(active); CommandWindowOpen(active);
} else { } else {
Open(FetchLoadWord(active.view)); Open(FetchLoadWord(active.view));
@@ -1043,7 +1044,7 @@ void Command_InsertNewLineDown() {
void Command_NewLine() { void Command_NewLine() {
BSet active = GetBSet(ActiveWindowID); BSet active = GetBSet(ActiveWindowID);
if (active.window->id == CommandBarWindowID) { if (active.window->id == CommandWindowID) {
CommandWindowOpen(active); CommandWindowOpen(active);
} else { } else {
IdentedNewLine(active.view); IdentedNewLine(active.view);

View File

@@ -25,7 +25,7 @@ WindowID DebugWindowID;
ViewID DebugViewID; ViewID DebugViewID;
BufferID DebugBufferID; BufferID DebugBufferID;
WindowID CommandBarWindowID; WindowID CommandWindowID;
WindowID StatusBarWindowID; WindowID StatusBarWindowID;
WindowID SearchBarWindowID; WindowID SearchBarWindowID;
ViewID SearchViewID; ViewID SearchViewID;

View File

@@ -396,12 +396,12 @@ void EndProfileScope() {
(uint64_t)GetTimeMicros() // timestamp in microseconds -- end of your timing block (uint64_t)GetTimeMicros() // timestamp in microseconds -- end of your timing block
); );
} }
#define ProfileScopeD(name) ProfileScopeEx PROFILE_SCOPE_VAR_##name((name).data, (name).len) #define ProfileScopeEx(name) ProfileScopeClass PROFILE_SCOPE_VAR_((name).data, (int)(name).len)
#define ProfileScope(name) ProfileScopeEx PROFILE_SCOPE_VAR_##name(#name, sizeof(#name) - 1) #define ProfileScope(name) ProfileScopeClass PROFILE_SCOPE_VAR_##name(#name, sizeof(#name) - 1)
#define ProfileFunction() ProfileScopeEx PROFILE_SCOPE_FUNCTION(__FUNCTION__, sizeof(__FUNCTION__) - 1) #define ProfileFunction() ProfileScopeClass PROFILE_SCOPE_FUNCTION(__FUNCTION__, sizeof(__FUNCTION__) - 1)
struct ProfileScopeEx { struct ProfileScopeClass {
ProfileScopeEx(const char *name, int len) { _BeginProfileScope(name, len); } ProfileScopeClass(const char *name, int len) { _BeginProfileScope(name, len); }
~ProfileScopeEx() { EndProfileScope(); } ~ProfileScopeClass() { EndProfileScope(); }
}; };
#else #else
#define ProfileScope(name) #define ProfileScope(name)

View File

@@ -392,7 +392,7 @@ void OnCommand(Event event) {
For (CommandFunctions) { For (CommandFunctions) {
if (it.trigger && MatchEvent(it.trigger, &event)) { if (it.trigger && MatchEvent(it.trigger, &event)) {
ProfileScopeEx(it.name.data, (int)it.name.len); ProfileScopeEx(it.name);
it.function(); it.function();
LastExecutedCommand = it.function; LastExecutedCommand = it.function;
} }
@@ -500,6 +500,7 @@ void GarbageCollect() {
} }
void Update(Event event) { void Update(Event event) {
ProfileFunction();
LayoutWindows(event.xwindow, event.ywindow); LayoutWindows(event.xwindow, event.ywindow);
Scratch scratch; Scratch scratch;

View File

@@ -182,25 +182,6 @@ void Find(View *seek_view, String16 needle, bool forward = true) {
IF_DEBUG(AssertRanges(seek_view->carets)); IF_DEBUG(AssertRanges(seek_view->carets));
} }
void FuzzySortView(View *view, String16 needle) {
Buffer *buffer = GetBuffer(view->active_buffer);
Scratch scratch;
Array<FuzzyPair> ratings = FuzzySearchLines(scratch, buffer, 0, buffer->line_starts.len, needle);
Buffer *temp_buffer = CreateTempBuffer(scratch, buffer->cap);
For(ratings) {
String16 s = GetLineStringWithoutNL(buffer, it.index);
if (s.len == 0) continue;
RawReplaceText(temp_buffer, GetBufferEndAsRange(temp_buffer), s);
RawReplaceText(temp_buffer, GetBufferEndAsRange(temp_buffer), u"\n");
}
SelectEntireBuffer(view);
Replace(view, GetString(temp_buffer));
SelectRange(view, MakeRange(0));
}
void SelectAll(View *view, String16 needle) { void SelectAll(View *view, String16 needle) {
Buffer *buffer = GetBuffer(view->active_buffer); Buffer *buffer = GetBuffer(view->active_buffer);
String16 string_buffer = GetString(buffer); String16 string_buffer = GetString(buffer);

View File

@@ -1,6 +1,6 @@
void CommandWindowInit() { void CommandWindowInit() {
Window *window = CreateWind(); Window *window = CreateWind();
CommandBarWindowID = window->id; CommandWindowID = window->id;
Buffer *buffer = CreateBuffer(SysAllocator, "command_bar"); Buffer *buffer = CreateBuffer(SysAllocator, "command_bar");
buffer->special = true; buffer->special = true;
View *view = CreateView(buffer->id); View *view = CreateView(buffer->id);
@@ -18,34 +18,91 @@ void CommandWindowInit() {
} }
void CommandWindowLayout(Rect2I *rect, Int wx, Int wy) { void CommandWindowLayout(Rect2I *rect, Int wx, Int wy) {
Window *n = GetWindow(CommandBarWindowID); Window *n = GetWindow(CommandWindowID);
Rect2I copy_rect = *rect; Rect2I copy_rect = *rect;
if (!n->visible) { if (!n->visible) {
rect = &copy_rect; rect = &copy_rect;
} }
Int barsize = Clamp((Int)n->font->line_spacing*10, (Int)0, (Int)wx - 100); Int barsize = Clamp((Int)n->font->line_spacing*10, (Int)0, (Int)wx - 100);
n->document_rect = n->total_rect = CutBottom(rect, barsize); n->document_rect = n->total_rect = CutTop(rect, barsize);
}
const Int FuzzyCloserWordBegin = 5;
const Int FuzzyConsecutiveMultiplier = 3;
Int FuzzyRate(String16 string, String16 with) {
ProfileFunction();
if (with.len == 0) return 0;
Int points = 0;
Int consecutive = 0;
Int with_i = 0;
for (Int i = 0; i < string.len; i++) {
if (string.data[i] == with[with_i]) {
Int closer_begin = ClampBottom((Int)0, FuzzyCloserWordBegin - i);
points += closer_begin;
consecutive++;
with_i += 1;
} else {
points += consecutive * FuzzyConsecutiveMultiplier;
consecutive = 0;
with_i = 0;
}
if (with_i >= with.len) with_i = 0;
}
points += consecutive * FuzzyConsecutiveMultiplier;
return points;
}
Array<FuzzyPair> FuzzySearchLines(Allocator allocator, Buffer *buffer, Int line_min, Int line_max, String16 needle) {
ProfileFunction();
if (line_min < 0 || line_min >= buffer->line_starts.len) return {};
if (line_max < 0 || line_min > buffer->line_starts.len) return {};
Array<FuzzyPair> ratings = {allocator};
for (Int i = line_min; i < line_max; i += 1) {
String16 s = GetLineStringWithoutNL(buffer, i);
Int idx = 0;
if (Seek(s, u" || ", &idx)) {
s.len = idx;
}
s = Trim(s);
Int rating = FuzzyRate(s, needle);
Add(&ratings, {i, rating});
}
// bubble sort
for (Int i = 0; i < ratings.len - 1; i++) {
for (Int j = 0; j < ratings.len - 1; j++) {
if (ratings[j].rating > ratings[j + 1].rating) {
FuzzyPair temp = ratings[j];
ratings[j] = ratings[j + 1];
ratings[j + 1] = temp;
}
}
}
return ratings;
} }
void CommandWindowUpdate() { void CommandWindowUpdate() {
ProfileFunction(); ProfileFunction();
BSet active = GetBSet(ActiveWindowID); BSet active = GetBSet(ActiveWindowID);
if (active.window->id == CommandBarWindowID) { if (active.window->id == CommandWindowID) {
if (!ProcessIsActive(active.view->id)) { if (!ProcessIsActive(active.view->id)) {
Scratch scratch; Scratch scratch;
String16 last_line_string = GetLineStringWithoutNL(active.buffer, active.buffer->line_starts.len - 1); String16 line_string = GetLineStringWithoutNL(active.buffer, 0);
if (active.view->prev_search_line != last_line_string) { if (active.view->prev_search_line != line_string) {
active.view->prev_search_line = last_line_string; active.view->prev_search_line = line_string;
Array<FuzzyPair> ratings = FuzzySearchLines(scratch, active.buffer, 0, active.buffer->line_starts.len - 1, last_line_string); Array<FuzzyPair> ratings = FuzzySearchLines(scratch, active.buffer, 1, active.buffer->line_starts.len, line_string);
Buffer *temp_buffer = CreateTempBuffer(scratch, active.buffer->cap); Buffer *temp_buffer = CreateTempBuffer(scratch, active.buffer->cap);
RawAppend(temp_buffer, line_string);
For(IterateInReverse(&ratings)) { For(IterateInReverse(&ratings)) {
String16 s = GetLineStringWithoutNL(active.buffer, it.index); String16 s = GetLineStringWithoutNL(active.buffer, it.index);
if (s.len == 0) continue; if (s.len == 0) continue;
RawReplaceText(temp_buffer, GetBufferEndAsRange(temp_buffer), s); RawAppend(temp_buffer, u"\n");
RawReplaceText(temp_buffer, GetBufferEndAsRange(temp_buffer), u"\n"); RawAppend(temp_buffer, s);
} }
RawReplaceText(temp_buffer, GetBufferEndAsRange(temp_buffer), last_line_string);
Caret caret = active.view->carets[0]; Caret caret = active.view->carets[0];
SaveCaretHistoryBeforeBeginEdit(active.buffer, active.view->carets); SaveCaretHistoryBeforeBeginEdit(active.buffer, active.view->carets);
@@ -58,65 +115,69 @@ void CommandWindowUpdate() {
} }
void Command_ShowCommands() { void Command_ShowCommands() {
if (ActiveWindowID == CommandBarWindowID && LastExecutedCommand == Command_ShowCommands) { if (ActiveWindowID == CommandWindowID && LastExecutedCommand == Command_ShowCommands) {
ActiveWindowID = LastActiveLayoutWindowID; ActiveWindowID = LastActiveLayoutWindowID;
return; return;
} }
ProfileFunction();
BSet command_bar = GetBSet(CommandBarWindowID); BSet command_bar = GetBSet(CommandWindowID);
command_bar.window->visible = true; command_bar.window->visible = true;
command_bar.window->eval_command = true; command_bar.window->eval_command = true;
ActiveWindowID = command_bar.window->id; ActiveWindowID = command_bar.window->id;
ResetBuffer(command_bar.buffer); ResetBuffer(command_bar.buffer);
For(CommandFunctions) { For(CommandFunctions) {
RawAppendf(command_bar.buffer, "%S\n", it.name); RawAppendf(command_bar.buffer, "\n%S", it.name);
} }
command_bar.view->update_scroll = true; command_bar.view->update_scroll = true;
SelectRange(command_bar.view, GetBufferEndAsRange(command_bar.buffer)); SelectRange(command_bar.view, GetBufferBeginAsRange(command_bar.buffer));
} RegisterCommand(Command_ShowCommands, "ctrl-shift-p"); } RegisterCommand(Command_ShowCommands, "ctrl-shift-p");
void Command_ShowDebugBufferList() { void Command_ShowDebugBufferList() {
if (ActiveWindowID == CommandBarWindowID && LastExecutedCommand == Command_ShowDebugBufferList) { if (ActiveWindowID == CommandWindowID && LastExecutedCommand == Command_ShowDebugBufferList) {
ActiveWindowID = LastActiveLayoutWindowID; ActiveWindowID = LastActiveLayoutWindowID;
return; return;
} }
ProfileFunction();
BSet command_bar = GetBSet(CommandBarWindowID); BSet command_bar = GetBSet(CommandWindowID);
command_bar.window->visible = true; command_bar.window->visible = true;
command_bar.window->eval_command = false; command_bar.window->eval_command = false;
ActiveWindowID = command_bar.window->id; ActiveWindowID = command_bar.window->id;
ResetBuffer(command_bar.buffer); ResetBuffer(command_bar.buffer);
For(Buffers) { For (Buffers) {
RawAppendf(command_bar.buffer, "%-80S || %S\n", SkipToLastSlash(it->name), it->name); RawAppendf(command_bar.buffer, "\n%-80S || %S", SkipToLastSlash(it->name), it->name);
} }
command_bar.view->update_scroll = true; command_bar.view->update_scroll = true;
SelectRange(command_bar.view, GetBufferEndAsRange(command_bar.buffer)); SelectRange(command_bar.view, GetBufferBeginAsRange(command_bar.buffer));
} RegisterCommand(Command_ShowDebugBufferList, ""); } RegisterCommand(Command_ShowDebugBufferList, "");
void Command_ShowBufferList() { void Command_ShowBufferList() {
if (ActiveWindowID == CommandBarWindowID && LastExecutedCommand == Command_ShowBufferList) { if (ActiveWindowID == CommandWindowID && LastExecutedCommand == Command_ShowBufferList) {
ActiveWindowID = LastActiveLayoutWindowID; ActiveWindowID = LastActiveLayoutWindowID;
return; return;
} }
ProfileFunction();
BSet command_bar = GetBSet(CommandBarWindowID); BSet command_bar = GetBSet(CommandWindowID);
command_bar.window->visible = true; command_bar.window->visible = true;
command_bar.window->eval_command = false; command_bar.window->eval_command = false;
ActiveWindowID = command_bar.window->id; ActiveWindowID = command_bar.window->id;
ResetBuffer(command_bar.buffer); ResetBuffer(command_bar.buffer);
For(Buffers) { For (Buffers) {
if (it->special) { if (it->special) {
continue; continue;
} }
RawAppendf(command_bar.buffer, "%-80S || %S\n", SkipToLastSlash(it->name), it->name); RawAppendf(command_bar.buffer, "\n%-80S || %S", SkipToLastSlash(it->name), it->name);
} }
command_bar.view->update_scroll = true; command_bar.view->update_scroll = true;
SelectRange(command_bar.view, GetBufferEndAsRange(command_bar.buffer)); SelectRange(command_bar.view, GetBufferBeginAsRange(command_bar.buffer));
} RegisterCommand(Command_ShowBufferList, "ctrl-p"); } RegisterCommand(Command_ShowBufferList, "ctrl-p");
void EvalCommand(String command) { void EvalCommand(String command) {
For (CommandFunctions) { For (CommandFunctions) {
if (it.name == command) { if (it.name == command) {
ProfileScopeEx(it.name);
it.function(); it.function();
break; break;
} }
@@ -129,12 +190,13 @@ void EvalCommand(String16 command) {
} }
void CommandWindowOpen(BSet active) { void CommandWindowOpen(BSet active) {
ProfileFunction();
Range range = active.view->carets[0].range; Range range = active.view->carets[0].range;
String16 string = FetchLoadWord(active.view); String16 string = FetchLoadWord(active.view);
if (GetSize(range) == 0) { if (GetSize(range) == 0) {
Int line = PosToLine(active.buffer, range.min); Int line = PosToLine(active.buffer, range.min);
if ((active.buffer->line_starts.len - 1) == line) { if (line == 0) {
line = ClampBottom(0ll, line - 1ll); line = ClampTop(1ll, active.buffer->line_starts.len - 1ll);
} }
string = GetLineStringWithoutNL(active.buffer, line); string = GetLineStringWithoutNL(active.buffer, line);