backend-infra-engineer: Release v0.3.2 snapshot
This commit is contained in:
@@ -1,31 +1,110 @@
|
||||
#include "assembly_editor.h"
|
||||
#include "app/editor/system/editor_card_registry.h"
|
||||
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "absl/strings/str_cat.h"
|
||||
#include "app/core/platform/file_dialog.h"
|
||||
#include "app/gui/icons.h"
|
||||
#include "app/gui/modules/text_editor.h"
|
||||
#include "absl/strings/match.h"
|
||||
#include "util/file_util.h"
|
||||
#include "app/gui/core/icons.h"
|
||||
#include "app/gui/core/ui_helpers.h"
|
||||
#include "app/gui/widgets/text_editor.h"
|
||||
|
||||
namespace yaze::editor {
|
||||
|
||||
using core::FileDialogWrapper;
|
||||
using util::FileDialogWrapper;
|
||||
|
||||
namespace {
|
||||
|
||||
static const char *const kKeywords[] = {
|
||||
"ADC", "AND", "ASL", "BCC", "BCS", "BEQ", "BIT", "BMI", "BNE", "BPL",
|
||||
"BRA", "BRL", "BVC", "BVS", "CLC", "CLD", "CLI", "CLV", "CMP", "CPX",
|
||||
"CPY", "DEC", "DEX", "DEY", "EOR", "INC", "INX", "INY", "JMP", "JSR",
|
||||
"JSL", "LDA", "LDX", "LDY", "LSR", "MVN", "NOP", "ORA", "PEA", "PER",
|
||||
"PHA", "PHB", "PHD", "PHP", "PHX", "PHY", "PLA", "PLB", "PLD", "PLP",
|
||||
"PLX", "PLY", "REP", "ROL", "ROR", "RTI", "RTL", "RTS", "SBC", "SEC",
|
||||
"SEI", "SEP", "STA", "STP", "STX", "STY", "STZ", "TAX", "TAY", "TCD",
|
||||
"TCS", "TDC", "TRB", "TSB", "TSC", "TSX", "TXA", "TXS", "TXY", "TYA",
|
||||
"TYX", "WAI", "WDM", "XBA", "XCE", "ORG", "LOROM", "HIROM"};
|
||||
|
||||
static const char *const kIdentifiers[] = {
|
||||
"abort", "abs", "acos", "asin", "atan", "atexit",
|
||||
"atof", "atoi", "atol", "ceil", "clock", "cosh",
|
||||
"ctime", "div", "exit", "fabs", "floor", "fmod",
|
||||
"getchar", "getenv", "isalnum", "isalpha", "isdigit", "isgraph",
|
||||
"ispunct", "isspace", "isupper", "kbhit", "log10", "log2",
|
||||
"log", "memcmp", "modf", "pow", "putchar", "putenv",
|
||||
"puts", "rand", "remove", "rename", "sinh", "sqrt",
|
||||
"srand", "strcat", "strcmp", "strerror", "time", "tolower",
|
||||
"toupper"};
|
||||
|
||||
TextEditor::LanguageDefinition GetAssemblyLanguageDef() {
|
||||
TextEditor::LanguageDefinition language_65816;
|
||||
for (auto &k : kKeywords) language_65816.mKeywords.emplace(k);
|
||||
|
||||
for (auto &k : kIdentifiers) {
|
||||
TextEditor::Identifier id;
|
||||
id.mDeclaration = "Built-in function";
|
||||
language_65816.mIdentifiers.insert(std::make_pair(std::string(k), id));
|
||||
}
|
||||
|
||||
language_65816.mTokenRegexStrings.push_back(
|
||||
std::make_pair<std::string, TextEditor::PaletteIndex>(
|
||||
"[ \\t]*#[ \\t]*[a-zA-Z_]+", TextEditor::PaletteIndex::Preprocessor));
|
||||
language_65816.mTokenRegexStrings.push_back(
|
||||
std::make_pair<std::string, TextEditor::PaletteIndex>(
|
||||
"L?\\\"(\\\\.|[^\\\"])*\\\"", TextEditor::PaletteIndex::String));
|
||||
language_65816.mTokenRegexStrings.push_back(
|
||||
std::make_pair<std::string, TextEditor::PaletteIndex>(
|
||||
"\\'\\\\?[^\\']\\'", TextEditor::PaletteIndex::CharLiteral));
|
||||
language_65816.mTokenRegexStrings.push_back(
|
||||
std::make_pair<std::string, TextEditor::PaletteIndex>(
|
||||
"[+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)([eE][+-]?[0-9]+)?[fF]?",
|
||||
TextEditor::PaletteIndex::Number));
|
||||
language_65816.mTokenRegexStrings.push_back(
|
||||
std::make_pair<std::string, TextEditor::PaletteIndex>(
|
||||
"[+-]?[0-9]+[Uu]?[lL]?[lL]?", TextEditor::PaletteIndex::Number));
|
||||
language_65816.mTokenRegexStrings.push_back(
|
||||
std::make_pair<std::string, TextEditor::PaletteIndex>(
|
||||
"0[0-7]+[Uu]?[lL]?[lL]?", TextEditor::PaletteIndex::Number));
|
||||
language_65816.mTokenRegexStrings.push_back(
|
||||
std::make_pair<std::string, TextEditor::PaletteIndex>(
|
||||
"0[xX][0-9a-fA-F]+[uU]?[lL]?[lL]?",
|
||||
TextEditor::PaletteIndex::Number));
|
||||
language_65816.mTokenRegexStrings.push_back(
|
||||
std::make_pair<std::string, TextEditor::PaletteIndex>(
|
||||
"[a-zA-Z_][a-zA-Z0-9_]*", TextEditor::PaletteIndex::Identifier));
|
||||
language_65816.mTokenRegexStrings.push_back(
|
||||
std::make_pair<std::string, TextEditor::PaletteIndex>(
|
||||
"[\\[\\]\\{\\}\\!\\%\\^\\&\\*\\(\\)\\-\\+\\=\\~\\|\\<\\>\\?\\/"
|
||||
"\\;\\,\\.]",
|
||||
TextEditor::PaletteIndex::Punctuation));
|
||||
|
||||
language_65816.mCommentStart = "/*";
|
||||
language_65816.mCommentEnd = "*/";
|
||||
language_65816.mSingleLineComment = ";";
|
||||
|
||||
language_65816.mCaseSensitive = false;
|
||||
language_65816.mAutoIndentation = true;
|
||||
|
||||
language_65816.mName = "65816";
|
||||
|
||||
return language_65816;
|
||||
}
|
||||
|
||||
std::vector<std::string> RemoveIgnoredFiles(
|
||||
const std::vector<std::string>& files,
|
||||
const std::vector<std::string>& ignored_files) {
|
||||
std::vector<std::string> filtered_files;
|
||||
for (const auto& file : files) {
|
||||
// Remove subdirectory files
|
||||
if (file.contains('/')) {
|
||||
if (absl::StrContains(file, '/')) {
|
||||
continue;
|
||||
}
|
||||
// Make sure the file has an extension
|
||||
if (!file.contains('.')) {
|
||||
if (!absl::StrContains(file, '.')) {
|
||||
continue;
|
||||
}
|
||||
if (std::ranges::find(ignored_files, file) == ignored_files.end()) {
|
||||
@@ -66,11 +145,11 @@ FolderItem LoadFolder(const std::string& folder) {
|
||||
auto folder_files = FileDialogWrapper::GetFilesInFolder(full_folder);
|
||||
for (const auto& files : folder_files) {
|
||||
// Remove subdirectory files
|
||||
if (files.contains('/')) {
|
||||
if (absl::StrContains(files, '/')) {
|
||||
continue;
|
||||
}
|
||||
// Make sure the file has an extension
|
||||
if (!files.contains('.')) {
|
||||
if (!absl::StrContains(files, '.')) {
|
||||
continue;
|
||||
}
|
||||
if (std::ranges::find(ignored_files, files) != ignored_files.end()) {
|
||||
@@ -95,10 +174,29 @@ FolderItem LoadFolder(const std::string& folder) {
|
||||
} // namespace
|
||||
|
||||
void AssemblyEditor::Initialize() {
|
||||
// Set the language definition
|
||||
text_editor_.SetLanguageDefinition(GetAssemblyLanguageDef());
|
||||
|
||||
// Register cards with EditorCardManager
|
||||
if (!dependencies_.card_registry) return;
|
||||
auto* card_registry = dependencies_.card_registry;
|
||||
card_registry->RegisterCard({.card_id = "assembly.editor", .display_name = "Assembly Editor",
|
||||
.icon = ICON_MD_CODE, .category = "Assembly",
|
||||
.shortcut_hint = "", .priority = 10});
|
||||
card_registry->RegisterCard({.card_id = "assembly.file_browser", .display_name = "File Browser",
|
||||
.icon = ICON_MD_FOLDER_OPEN, .category = "Assembly",
|
||||
.shortcut_hint = "", .priority = 20});
|
||||
|
||||
// Don't show by default - only show when user explicitly opens Assembly Editor
|
||||
}
|
||||
|
||||
absl::Status AssemblyEditor::Load() { return absl::OkStatus(); }
|
||||
absl::Status AssemblyEditor::Load() {
|
||||
// Register cards with EditorCardRegistry (dependency injection)
|
||||
// Note: Assembly editor uses dynamic file tabs, so we register the main editor window
|
||||
if (!dependencies_.card_registry) return absl::OkStatus();
|
||||
auto* card_registry = dependencies_.card_registry;
|
||||
|
||||
return absl::OkStatus();
|
||||
}
|
||||
|
||||
void AssemblyEditor::OpenFolder(const std::string& folder_path) {
|
||||
current_folder_ = LoadFolder(folder_path);
|
||||
@@ -113,7 +211,6 @@ void AssemblyEditor::Update(bool& is_loaded) {
|
||||
}
|
||||
|
||||
auto cpos = text_editor_.GetCursorPosition();
|
||||
SetEditorText();
|
||||
ImGui::Text("%6d/%-6d %6d lines | %s | %s | %s | %s", cpos.mLine + 1,
|
||||
cpos.mColumn + 1, text_editor_.GetTotalLines(),
|
||||
text_editor_.IsOverwrite() ? "Ovr" : "Ins",
|
||||
@@ -127,7 +224,6 @@ void AssemblyEditor::Update(bool& is_loaded) {
|
||||
|
||||
void AssemblyEditor::InlineUpdate() {
|
||||
auto cpos = text_editor_.GetCursorPosition();
|
||||
SetEditorText();
|
||||
ImGui::Text("%6d/%-6d %6d lines | %s | %s | %s | %s", cpos.mLine + 1,
|
||||
cpos.mColumn + 1, text_editor_.GetTotalLines(),
|
||||
text_editor_.IsOverwrite() ? "Ovr" : "Ins",
|
||||
@@ -139,41 +235,77 @@ void AssemblyEditor::InlineUpdate() {
|
||||
}
|
||||
|
||||
void AssemblyEditor::UpdateCodeView() {
|
||||
ImGui::BeginTable("##table_view", 2,
|
||||
ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg |
|
||||
ImGuiTableFlags_Resizable);
|
||||
DrawToolset();
|
||||
gui::VerticalSpacing(2.0f);
|
||||
|
||||
// Table headers
|
||||
ImGui::TableSetupColumn("Files", ImGuiTableColumnFlags_WidthFixed, 256.0f);
|
||||
ImGui::TableSetupColumn("Editor", ImGuiTableColumnFlags_WidthStretch);
|
||||
|
||||
ImGui::TableHeadersRow();
|
||||
|
||||
// Table data
|
||||
ImGui::TableNextRow();
|
||||
ImGui::TableNextColumn();
|
||||
if (current_folder_.name != "") {
|
||||
DrawCurrentFolder();
|
||||
} else {
|
||||
if (ImGui::Button("Open Folder")) {
|
||||
current_folder_ = LoadFolder(FileDialogWrapper::ShowOpenFolderDialog());
|
||||
// Create session-aware card (non-static for multi-session support)
|
||||
gui::EditorCard file_browser_card(MakeCardTitle("File Browser").c_str(), ICON_MD_FOLDER);
|
||||
bool file_browser_open = true;
|
||||
if (file_browser_card.Begin(&file_browser_open)) {
|
||||
if (current_folder_.name != "") {
|
||||
DrawCurrentFolder();
|
||||
} else {
|
||||
if (ImGui::Button("Open Folder")) {
|
||||
current_folder_ = LoadFolder(FileDialogWrapper::ShowOpenFolderDialog());
|
||||
}
|
||||
}
|
||||
}
|
||||
file_browser_card.End(); // ALWAYS call End after Begin
|
||||
|
||||
ImGui::TableNextColumn();
|
||||
// Draw open files as individual, dockable EditorCards
|
||||
for (int i = 0; i < active_files_.Size; i++) {
|
||||
int file_id = active_files_[i];
|
||||
bool open = true;
|
||||
|
||||
auto cpos = text_editor_.GetCursorPosition();
|
||||
SetEditorText();
|
||||
ImGui::Text("%6d/%-6d %6d lines | %s | %s | %s | %s", cpos.mLine + 1,
|
||||
cpos.mColumn + 1, text_editor_.GetTotalLines(),
|
||||
text_editor_.IsOverwrite() ? "Ovr" : "Ins",
|
||||
text_editor_.CanUndo() ? "*" : " ",
|
||||
text_editor_.GetLanguageDefinition().mName.c_str(),
|
||||
current_file_.c_str());
|
||||
// Ensure we have a TextEditor instance for this file
|
||||
if (file_id >= open_files_.size()) {
|
||||
open_files_.resize(file_id + 1);
|
||||
}
|
||||
if (file_id >= files_.size()) {
|
||||
// This can happen if a file was closed and its ID is being reused.
|
||||
// For now, we just skip it.
|
||||
continue;
|
||||
}
|
||||
|
||||
text_editor_.Render("##asm_editor");
|
||||
// Create session-aware card title for each file
|
||||
std::string card_name = MakeCardTitle(files_[file_id]);
|
||||
gui::EditorCard file_card(card_name.c_str(), ICON_MD_DESCRIPTION, &open);
|
||||
if (file_card.Begin()) {
|
||||
if (ImGui::IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows)) {
|
||||
active_file_id_ = file_id;
|
||||
}
|
||||
open_files_[file_id].Render(absl::StrCat("##", card_name).c_str());
|
||||
}
|
||||
file_card.End(); // ALWAYS call End after Begin
|
||||
|
||||
ImGui::EndTable();
|
||||
if (!open) {
|
||||
active_files_.erase(active_files_.Data + i);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
absl::Status AssemblyEditor::Save() {
|
||||
if (active_file_id_ != -1 && active_file_id_ < open_files_.size()) {
|
||||
std::string content = open_files_[active_file_id_].GetText();
|
||||
util::SaveFile(files_[active_file_id_], content);
|
||||
return absl::OkStatus();
|
||||
}
|
||||
return absl::FailedPreconditionError("No active file to save.");
|
||||
}
|
||||
|
||||
void AssemblyEditor::DrawToolset() {
|
||||
static gui::Toolset toolbar;
|
||||
toolbar.Begin();
|
||||
|
||||
if (toolbar.AddAction(ICON_MD_FOLDER_OPEN, "Open Folder")) {
|
||||
current_folder_ = LoadFolder(FileDialogWrapper::ShowOpenFolderDialog());
|
||||
}
|
||||
if (toolbar.AddAction(ICON_MD_SAVE, "Save File")) {
|
||||
Save();
|
||||
}
|
||||
|
||||
toolbar.End();
|
||||
}
|
||||
|
||||
void AssemblyEditor::DrawCurrentFolder() {
|
||||
@@ -226,63 +358,11 @@ void AssemblyEditor::DrawCurrentFolder() {
|
||||
}
|
||||
}
|
||||
|
||||
void AssemblyEditor::DrawFileTabView() {
|
||||
static int next_tab_id = 0;
|
||||
|
||||
if (ImGui::BeginTabBar("AssemblyFileTabBar", ImGuiTabBarFlags_None)) {
|
||||
if (ImGui::TabItemButton(ICON_MD_ADD, ImGuiTabItemFlags_None)) {
|
||||
if (std::ranges::find(active_files_, current_file_id_) !=
|
||||
active_files_.end()) {
|
||||
// Room is already open
|
||||
next_tab_id++;
|
||||
}
|
||||
active_files_.push_back(next_tab_id++); // Add new tab
|
||||
}
|
||||
|
||||
// Submit our regular tabs
|
||||
for (int n = 0; n < active_files_.Size;) {
|
||||
bool open = true;
|
||||
|
||||
if (ImGui::BeginTabItem(files_[active_files_[n]].data(), &open,
|
||||
ImGuiTabItemFlags_None)) {
|
||||
auto cpos = text_editor_.GetCursorPosition();
|
||||
{
|
||||
std::ifstream t(current_file_);
|
||||
if (t.good()) {
|
||||
std::string str((std::istreambuf_iterator<char>(t)),
|
||||
std::istreambuf_iterator<char>());
|
||||
text_editor_.SetText(str);
|
||||
} else {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
||||
"Error opening file: %s\n", current_file_.c_str());
|
||||
}
|
||||
}
|
||||
ImGui::Text("%6d/%-6d %6d lines | %s | %s | %s | %s", cpos.mLine + 1,
|
||||
cpos.mColumn + 1, text_editor_.GetTotalLines(),
|
||||
text_editor_.IsOverwrite() ? "Ovr" : "Ins",
|
||||
text_editor_.CanUndo() ? "*" : " ",
|
||||
text_editor_.GetLanguageDefinition().mName.c_str(),
|
||||
current_file_.c_str());
|
||||
|
||||
open_files_[active_files_[n]].Render("##asm_editor");
|
||||
ImGui::EndTabItem();
|
||||
}
|
||||
|
||||
if (!open)
|
||||
active_files_.erase(active_files_.Data + n);
|
||||
else
|
||||
n++;
|
||||
}
|
||||
|
||||
ImGui::EndTabBar();
|
||||
}
|
||||
ImGui::Separator();
|
||||
}
|
||||
|
||||
void AssemblyEditor::DrawFileMenu() {
|
||||
if (ImGui::BeginMenu("File")) {
|
||||
if (ImGui::MenuItem("Open", "Ctrl+O")) {
|
||||
auto filename = core::FileDialogWrapper::ShowOpenFileDialog();
|
||||
auto filename = util::FileDialogWrapper::ShowOpenFileDialog();
|
||||
ChangeActiveFile(filename);
|
||||
}
|
||||
if (ImGui::MenuItem("Save", "Ctrl+S")) {
|
||||
@@ -318,19 +398,36 @@ void AssemblyEditor::DrawEditMenu() {
|
||||
}
|
||||
}
|
||||
|
||||
void AssemblyEditor::SetEditorText() {
|
||||
if (!file_is_loaded_) {
|
||||
std::ifstream t(current_file_);
|
||||
if (t.good()) {
|
||||
std::string str((std::istreambuf_iterator<char>(t)),
|
||||
std::istreambuf_iterator<char>());
|
||||
text_editor_.SetText(str);
|
||||
} else {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error opening file: %s\n",
|
||||
current_file_.c_str());
|
||||
void AssemblyEditor::ChangeActiveFile(const std::string_view &filename) {
|
||||
// Check if file is already open
|
||||
for (int i = 0; i < active_files_.Size; ++i) {
|
||||
int file_id = active_files_[i];
|
||||
if (files_[file_id] == filename) {
|
||||
// Optional: Focus window
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Add new file
|
||||
int new_file_id = files_.size();
|
||||
files_.push_back(std::string(filename));
|
||||
active_files_.push_back(new_file_id);
|
||||
|
||||
// Resize open_files_ if needed
|
||||
if (new_file_id >= open_files_.size()) {
|
||||
open_files_.resize(new_file_id + 1);
|
||||
}
|
||||
|
||||
// Load file content using utility
|
||||
std::string content = util::LoadFile(std::string(filename));
|
||||
if (!content.empty()) {
|
||||
open_files_[new_file_id].SetText(content);
|
||||
open_files_[new_file_id].SetLanguageDefinition(GetAssemblyLanguageDef());
|
||||
open_files_[new_file_id].SetPalette(TextEditor::GetDarkPalette());
|
||||
} else {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error opening file: %s\n",
|
||||
std::string(filename).c_str());
|
||||
}
|
||||
file_is_loaded_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
absl::Status AssemblyEditor::Cut() {
|
||||
|
||||
Reference in New Issue
Block a user