Invert command window
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
void CommandWindowInit() {
|
||||
Window *window = CreateWind();
|
||||
CommandBarWindowID = window->id;
|
||||
CommandWindowID = window->id;
|
||||
Buffer *buffer = CreateBuffer(SysAllocator, "command_bar");
|
||||
buffer->special = true;
|
||||
View *view = CreateView(buffer->id);
|
||||
@@ -18,34 +18,91 @@ void CommandWindowInit() {
|
||||
}
|
||||
|
||||
void CommandWindowLayout(Rect2I *rect, Int wx, Int wy) {
|
||||
Window *n = GetWindow(CommandBarWindowID);
|
||||
Window *n = GetWindow(CommandWindowID);
|
||||
Rect2I copy_rect = *rect;
|
||||
if (!n->visible) {
|
||||
rect = ©_rect;
|
||||
}
|
||||
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() {
|
||||
ProfileFunction();
|
||||
BSet active = GetBSet(ActiveWindowID);
|
||||
if (active.window->id == CommandBarWindowID) {
|
||||
if (active.window->id == CommandWindowID) {
|
||||
if (!ProcessIsActive(active.view->id)) {
|
||||
Scratch scratch;
|
||||
String16 last_line_string = GetLineStringWithoutNL(active.buffer, active.buffer->line_starts.len - 1);
|
||||
if (active.view->prev_search_line != last_line_string) {
|
||||
active.view->prev_search_line = last_line_string;
|
||||
Array<FuzzyPair> ratings = FuzzySearchLines(scratch, active.buffer, 0, active.buffer->line_starts.len - 1, last_line_string);
|
||||
String16 line_string = GetLineStringWithoutNL(active.buffer, 0);
|
||||
if (active.view->prev_search_line != line_string) {
|
||||
active.view->prev_search_line = 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);
|
||||
RawAppend(temp_buffer, line_string);
|
||||
For(IterateInReverse(&ratings)) {
|
||||
String16 s = GetLineStringWithoutNL(active.buffer, it.index);
|
||||
if (s.len == 0) continue;
|
||||
RawReplaceText(temp_buffer, GetBufferEndAsRange(temp_buffer), s);
|
||||
RawReplaceText(temp_buffer, GetBufferEndAsRange(temp_buffer), u"\n");
|
||||
RawAppend(temp_buffer, u"\n");
|
||||
RawAppend(temp_buffer, s);
|
||||
}
|
||||
RawReplaceText(temp_buffer, GetBufferEndAsRange(temp_buffer), last_line_string);
|
||||
|
||||
Caret caret = active.view->carets[0];
|
||||
SaveCaretHistoryBeforeBeginEdit(active.buffer, active.view->carets);
|
||||
@@ -58,65 +115,69 @@ void CommandWindowUpdate() {
|
||||
}
|
||||
|
||||
void Command_ShowCommands() {
|
||||
if (ActiveWindowID == CommandBarWindowID && LastExecutedCommand == Command_ShowCommands) {
|
||||
if (ActiveWindowID == CommandWindowID && LastExecutedCommand == Command_ShowCommands) {
|
||||
ActiveWindowID = LastActiveLayoutWindowID;
|
||||
return;
|
||||
}
|
||||
ProfileFunction();
|
||||
|
||||
BSet command_bar = GetBSet(CommandBarWindowID);
|
||||
BSet command_bar = GetBSet(CommandWindowID);
|
||||
command_bar.window->visible = true;
|
||||
command_bar.window->eval_command = true;
|
||||
ActiveWindowID = command_bar.window->id;
|
||||
ResetBuffer(command_bar.buffer);
|
||||
For(CommandFunctions) {
|
||||
RawAppendf(command_bar.buffer, "%S\n", it.name);
|
||||
RawAppendf(command_bar.buffer, "\n%S", it.name);
|
||||
}
|
||||
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");
|
||||
|
||||
void Command_ShowDebugBufferList() {
|
||||
if (ActiveWindowID == CommandBarWindowID && LastExecutedCommand == Command_ShowDebugBufferList) {
|
||||
if (ActiveWindowID == CommandWindowID && LastExecutedCommand == Command_ShowDebugBufferList) {
|
||||
ActiveWindowID = LastActiveLayoutWindowID;
|
||||
return;
|
||||
}
|
||||
ProfileFunction();
|
||||
|
||||
BSet command_bar = GetBSet(CommandBarWindowID);
|
||||
BSet command_bar = GetBSet(CommandWindowID);
|
||||
command_bar.window->visible = true;
|
||||
command_bar.window->eval_command = false;
|
||||
ActiveWindowID = command_bar.window->id;
|
||||
ResetBuffer(command_bar.buffer);
|
||||
For(Buffers) {
|
||||
RawAppendf(command_bar.buffer, "%-80S || %S\n", SkipToLastSlash(it->name), it->name);
|
||||
For (Buffers) {
|
||||
RawAppendf(command_bar.buffer, "\n%-80S || %S", SkipToLastSlash(it->name), it->name);
|
||||
}
|
||||
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, "");
|
||||
|
||||
void Command_ShowBufferList() {
|
||||
if (ActiveWindowID == CommandBarWindowID && LastExecutedCommand == Command_ShowBufferList) {
|
||||
if (ActiveWindowID == CommandWindowID && LastExecutedCommand == Command_ShowBufferList) {
|
||||
ActiveWindowID = LastActiveLayoutWindowID;
|
||||
return;
|
||||
}
|
||||
ProfileFunction();
|
||||
|
||||
BSet command_bar = GetBSet(CommandBarWindowID);
|
||||
BSet command_bar = GetBSet(CommandWindowID);
|
||||
command_bar.window->visible = true;
|
||||
command_bar.window->eval_command = false;
|
||||
ActiveWindowID = command_bar.window->id;
|
||||
ResetBuffer(command_bar.buffer);
|
||||
For(Buffers) {
|
||||
For (Buffers) {
|
||||
if (it->special) {
|
||||
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;
|
||||
SelectRange(command_bar.view, GetBufferEndAsRange(command_bar.buffer));
|
||||
SelectRange(command_bar.view, GetBufferBeginAsRange(command_bar.buffer));
|
||||
} RegisterCommand(Command_ShowBufferList, "ctrl-p");
|
||||
|
||||
void EvalCommand(String command) {
|
||||
For (CommandFunctions) {
|
||||
if (it.name == command) {
|
||||
ProfileScopeEx(it.name);
|
||||
it.function();
|
||||
break;
|
||||
}
|
||||
@@ -129,12 +190,13 @@ void EvalCommand(String16 command) {
|
||||
}
|
||||
|
||||
void CommandWindowOpen(BSet active) {
|
||||
ProfileFunction();
|
||||
Range range = active.view->carets[0].range;
|
||||
String16 string = FetchLoadWord(active.view);
|
||||
if (GetSize(range) == 0) {
|
||||
Int line = PosToLine(active.buffer, range.min);
|
||||
if ((active.buffer->line_starts.len - 1) == line) {
|
||||
line = ClampBottom(0ll, line - 1ll);
|
||||
if (line == 0) {
|
||||
line = ClampTop(1ll, active.buffer->line_starts.len - 1ll);
|
||||
}
|
||||
|
||||
string = GetLineStringWithoutNL(active.buffer, line);
|
||||
|
||||
Reference in New Issue
Block a user