Skip to content

Commit

Permalink
*Optimized global searching.
Browse files Browse the repository at this point in the history
  • Loading branch information
paladin-t committed Oct 29, 2023
1 parent 9efbf44 commit ca330fe
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 31 deletions.
124 changes: 93 additions & 31 deletions src/editor_code.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class EditorCodeImpl : public EditorCode, public ImGui::CodeEditor {
std::string _name;
Code::Ptr _object = nullptr;
Editing::Data::Checkpoint _checkpoint;
int _index = -1;

bool _acquireFocus = false;
int _breaking = -1;
Expand Down Expand Up @@ -611,38 +612,39 @@ class EditorCodeImpl : public EditorCode, public ImGui::CodeEditor {
Coordinates srcBegin, srcEnd;
GetSelection(srcBegin, srcEnd);

int index = -1;
Text::Array* strings = nullptr;
Editing::Tools::TextPages &cache = _shared.cache(&strings);
strings->clear();
cache.clear();
do {
LockGuard<RecursiveMutex>::UniquePtr acquired;
Project* prj = project->acquire(acquired);
if (!prj)
break;

for (int i = 0; i < prj->count(); ++i) {
Asset* asset = prj->get(Asset::List::Index(i, false));
if (!asset)
break;

if (asset->type() != Code::TYPE()) {
strings->push_back("");
cache.push_back(nullptr);
Asset* asset = prj->get(Asset::List::Index(_index, false));
if (!asset)
break;

continue;
}
if (asset->type() != Code::TYPE())
break;

if (_name == asset->entry().name())
index = i;
const bool readyForEditing = asset->readyFor(Asset::EDITING);
if (readyForEditing) {
EditorCodeImpl* editor = (EditorCodeImpl*)asset->editor();
if (!editor)
break;

const bool readyForEditing = asset->readyFor(Asset::EDITING);
if (!readyForEditing)
asset->prepare(Asset::EDITING, true);
size_t len = 0;
const char* txt = editor->text(&len);
std::string str;
str.assign(txt, len);
if (_index < (int)strings->size() && _index < (int)cache.size()) {
(*strings)[_index] = txt;
cache[_index] = &strings->back();
}
} else {
asset->prepare(Asset::EDITING, true);
Object::Ptr obj = asset->object(Asset::EDITING);
if (!readyForEditing)
asset->finish(Asset::EDITING, true);
asset->finish(Asset::EDITING, true);

if (!obj)
break;
Expand All @@ -654,13 +656,15 @@ class EditorCodeImpl : public EditorCode, public ImGui::CodeEditor {
const char* txt = code->text(&len);
std::string str;
str.assign(txt, len);
strings->push_back(txt);
cache.push_back(&strings->back());
if (_index < (int)strings->size() && _index < (int)cache.size()) {
(*strings)[_index] = txt;
cache[_index] = &strings->back();
}
}
} while (false);
_tools.marker = Editing::Tools::Marker(
Editing::Tools::Marker::Coordinates(index, srcBegin.Line, srcBegin.Column),
Editing::Tools::Marker::Coordinates(index, srcEnd.Line, srcEnd.Column)
Editing::Tools::Marker::Coordinates(_index, srcBegin.Line, srcBegin.Column),
Editing::Tools::Marker::Coordinates(_index, srcEnd.Line, srcEnd.Column)
);

const float y = ImGui::GetCursorPosY();
Expand All @@ -670,21 +674,21 @@ class EditorCodeImpl : public EditorCode, public ImGui::CodeEditor {
width,
&_tools.initialized, &_tools.focused,
&cache, &_shared.word(),
Editing::Tools::Marker::Coordinates(index, GetTotalLines(), GetColumnsAt(GetTotalLines())),
Editing::Tools::Marker::Coordinates(_index, GetTotalLines(), GetColumnsAt(GetTotalLines())),
&_tools.direction,
&ws->settings()->editorCaseSensitive, &ws->settings()->editorMatchWholeWord, &ws->settings()->editorGlobalSearch,
_shared.finding,
[&] (const Editing::Tools::Marker::Coordinates &pos, Editing::Tools::Marker &src) -> std::string {
Coordinates srcBegin, srcEnd;
const std::string result = GetWordAt(Coordinates(pos.line, pos.column), &srcBegin, &srcEnd);
src.begin = Editing::Tools::Marker::Coordinates(index, srcBegin.Line, srcBegin.Column);
src.end = Editing::Tools::Marker::Coordinates(index, srcEnd.Line, srcEnd.Column);
src.begin = Editing::Tools::Marker::Coordinates(_index, srcBegin.Line, srcBegin.Column);
src.end = Editing::Tools::Marker::Coordinates(_index, srcEnd.Line, srcEnd.Column);

return result;
}
);
if (stepped && !_tools.marker.empty()) {
if (_tools.marker.begin.index == index) {
if (_tools.marker.begin.index == _index) {
const Coordinates begin(_tools.marker.begin.line, _tools.marker.begin.column);
const Coordinates end(_tools.marker.end.line, _tools.marker.end.column);

Expand Down Expand Up @@ -768,10 +772,68 @@ class EditorCodeImpl : public EditorCode, public ImGui::CodeEditor {
}

virtual void lostFocus(class Renderer* /* rnd */, const class Project* /* project */) override {
// Do nothing.
_index = -1;
Text::Array* strings = nullptr;
Editing::Tools::TextPages &cache = _shared.cache(&strings);
strings->clear();
cache.clear();
}
virtual void gainFocus(class Renderer* /* rnd */, const class Project* /* project */) override {
// Do nothing.
virtual void gainFocus(class Renderer* /* rnd */, const class Project* project) override {
Text::Array* strings = nullptr;
Editing::Tools::TextPages &cache = _shared.cache(&strings);
do {
LockGuard<RecursiveMutex>::UniquePtr acquired;
Project* prj = project->acquire(acquired);
if (!prj)
break;

for (int i = 0; i < prj->count(); ++i) {
Asset* asset = prj->get(Asset::List::Index(i, false));
if (!asset)
break;

if (asset->type() != Code::TYPE()) {
strings->push_back("");
cache.push_back(nullptr);

continue;
}

if (_name == asset->entry().name())
_index = i;

const bool readyForEditing = asset->readyFor(Asset::EDITING);
if (readyForEditing) {
EditorCodeImpl* editor = (EditorCodeImpl*)asset->editor();
if (!editor)
break;

size_t len = 0;
const char* txt = editor->text(&len);
std::string str;
str.assign(txt, len);
strings->push_back(txt);
cache.push_back(&strings->back());
} else {
asset->prepare(Asset::EDITING, true);
Object::Ptr obj = asset->object(Asset::EDITING);
asset->finish(Asset::EDITING, true);

if (!obj)
break;
Code::Ptr code = Object::as<Code::Ptr>(obj);
if (!code)
break;

size_t len = 0;
const char* txt = code->text(&len);
std::string str;
str.assign(txt, len);
strings->push_back(txt);
cache.push_back(&strings->back());
}
}
} while (false);
}

private:
Expand Down
6 changes: 6 additions & 0 deletions src/workspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1791,6 +1791,7 @@ void Workspace::editing(class Window* wnd, class Renderer* rnd, const class Proj

Editable* editor = asset->editor();

bool toGainFocus = false;
if (lastEditing != index) {
switched = true;

Expand All @@ -1811,6 +1812,8 @@ void Workspace::editing(class Window* wnd, class Renderer* rnd, const class Proj

if (editor)
editor->gainFocus(rnd, project);
else
toGainFocus = true;
}

if (!editor) {
Expand All @@ -1834,6 +1837,9 @@ void Workspace::editing(class Window* wnd, class Renderer* rnd, const class Proj
editor->readonly(true);

fillAssetEditorSettings(editor);

if (toGainFocus)
editor->gainFocus(rnd, project);
}
}
if (editor) {
Expand Down

0 comments on commit ca330fe

Please sign in to comment.