Init variables, ReportErrorf now doesn't pop a message cause it doesn't do the queuing

This commit is contained in:
Krzosa Karol
2026-02-03 21:10:33 +01:00
parent 8cb1b49cd8
commit 930620a49e
12 changed files with 124 additions and 56 deletions

View File

@@ -73,10 +73,10 @@ static const char *glsl_fshader_es3 = R"==(#version 300 es
}
)==";
void ReportWarningf(const char *fmt, ...);
void ReportErrorf(const char *fmt, ...);
void GLDebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const void *user) {
Unused(source); Unused(type); Unused(id); Unused(length); Unused(user);
ReportWarningf("OpenGL message: %s", message);
ReportErrorf("OpenGL message: %s", message);
if (severity == GL_DEBUG_SEVERITY_HIGH || severity == GL_DEBUG_SEVERITY_MEDIUM) {
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "OpenGL error", message, NULL);
}

View File

@@ -1557,7 +1557,7 @@ void SaveBuffer(Buffer *buffer) {
buffer->dirty = false;
buffer->temp = false;
} else {
ReportWarningf("Failed to save file with name: %S", buffer->name);
ReportErrorf("Failed to save file with name: %S", buffer->name);
}
}

View File

@@ -67,6 +67,12 @@ void Appendf(View *view, const char *fmt, ...) {
Append(view, string, true);
}
void ReportConsolef(const char *fmt, ...) {
Scratch scratch;
STRING_FORMAT(scratch, fmt, string);
Appendf(LogView, "%S\n", string);
}
void ReportErrorf(const char *fmt, ...) {
ErrorCount += 1;
Scratch scratch;
@@ -77,28 +83,11 @@ void ReportErrorf(const char *fmt, ...) {
if (LogView) {
Appendf(LogView, "%S\n", string);
ShowUIMessagef("%S", string);
} else {
printf("%.*s\n", (int)string.len, string.data);
}
}
void ReportConsolef(const char *fmt, ...) {
Scratch scratch;
STRING_FORMAT(scratch, fmt, string);
Appendf(LogView, "%S\n", string);
}
void ReportWarningf(const char *fmt, ...) {
ErrorCount += 1;
Scratch scratch;
STRING_FORMAT(scratch, fmt, string);
if (BreakOnError) {
BREAK();
}
Appendf(LogView, "%S\n", string);
}
void CMD_CenterView() {
CenterView(PrimaryWindowID);
} RegisterCommand(CMD_CenterView, "", "");

View File

@@ -4,7 +4,7 @@ struct Lexer {
char *start;
char *end;
char *name;
int line, column;
Int line, column;
};
enum TriggerKind {
@@ -34,6 +34,11 @@ struct CachedTrigger {
};
Array<CachedTrigger> CachedTriggers;
Lexer MakeLexer(Allocator allocator, String string, char *file, Int line, Int column) {
Lexer lexer = {allocator, string.data, string.data, string.data + string.len, file, line, column};
return lexer;
}
void Advance(Lexer *lex) {
if (lex->at < lex->end) {
if (lex->at[0] == '\n') {

View File

@@ -61,7 +61,7 @@ void UpdateCoroutines(Event *event) {
_CoroutineContext = &it;
mco_result ok = mco_resume(it.co);
if (ok != MCO_SUCCESS) {
ReportWarningf("failed to resume coroutine %d", ok);
ReportErrorf("failed to resume coroutine %d", ok);
DestroyCoroutine(&it);
remove_item = true;
}

View File

@@ -176,6 +176,7 @@ RegisterVariable(String, OpenCodePatterns, ".c .h .cpp .hpp .cc .cxx .rs .go .zi
RegisterVariable(String, OpenCodeExcludePatterns, "");
RegisterVariable(Int, TrimTrailingWhitespace, 1);
// PROJECT_MANAGEMENT
// Set at the beginning of the program to current directory
RegisterVariable(String, ProjectDirectory, "");

View File

@@ -56,17 +56,6 @@ void CMD_EvalCommandsLineByLine() {
EvalCommandsLineByLine(set);
} RegisterCommand(CMD_EvalCommandsLineByLine, "", "Goes line by line over a buffer and evaluates every line as a command, ignores empty or lines starting with '//'");
Variable *GetVariable(String name) {
Variable *var = NULL;
For (Variables) {
if (name == it.name) {
var = &it;
break;
}
}
return var;
}
BufferID LoadConfig(String config_path) {
ReportConsolef("Loading config %S...", config_path);
Window *window = GetWindow(NullWindowID);

View File

@@ -1,3 +1,4 @@
#if PLUGIN_CONFIG
void Set(String string);
String InsertVariables(Allocator allocator, String string);
#endif

View File

@@ -2133,7 +2133,7 @@ bool RDBG_InitConnection(mco_coro *co, bool create_session = true) {
}
if (file.len == 0) {
ReportWarningf("Couldn't find neither .rdbg file, nor use the BinaryUnderDebug variable to locate the binary");
ReportErrorf("Couldn't find neither .rdbg file, nor use the BinaryUnderDebug variable to locate the binary");
return false;
}
}

View File

@@ -1,5 +1,6 @@
// MAAAAAAAAAAAAN I DONT LIKE THIS CODE, BUT HOPE IT WORKS
// @todo: potentially bad that we are not checking against end! lexer->at[0] == 0 check is not enough
struct Lexer2 {
char16_t *at;
};

View File

@@ -854,25 +854,15 @@ extern char **environ;
int main(int argc, char **argv)
#endif
{
InitScratch();
InitOS(ReportErrorf);
#if OS_WINDOWS
int argc = __argc;
char **argv = __argv;
AttachConsole(ATTACH_PARENT_PROCESS);
#endif
if (1) {
RunArenaTest();
For (TestFunctions) {
it.function();
}
// ReportErrorf("Testing DONE\n");
// return 0;
}
#if OS_WINDOWS
InitScratch();
InitOS(ReportErrorf);
ProjectDirectory = GetWorkingDir(Perm);
#if OS_WINDOWS
{
wchar_t *p = GetEnvironmentStringsW();
for (;p && p[0];) {
@@ -889,7 +879,16 @@ int main(int argc, char **argv)
}
#endif
ProjectDirectory = GetWorkingDir(Perm);
if (1) {
RunArenaTest();
For (TestFunctions) {
it.function();
}
// ReportErrorf("Testing DONE\n");
// return 0;
}
{
String sdl_config_path = SDL_GetPrefPath("krzosa", "text_editor");
if (sdl_config_path.len && sdl_config_path.data[sdl_config_path.len - 1] == '\\') {
@@ -1009,7 +1008,7 @@ int main(int argc, char **argv)
ReloadFont(PathToFont, (U32)FontSize);
CreateWind();
ReopenBuffer(GetBuffer(NullBufferID));
InitOS(ReportWarningf);
InitOS(ReportErrorf);
For (GlobalCommands) {
if (it.binding.len != 0) {

View File

@@ -217,11 +217,88 @@ bool IsOpenBoundary(char c) {
return result;
}
/*
Variable *GetVariable(String name) {
Variable *var = NULL;
For (Variables) {
if (name == it.name) {
var = &it;
break;
}
}
return var;
}
Variables that control the default shell for '!' commands
DefaultShellWindows - "cmd"
DefaultShellUnix - "bash | sh"
String InsertVariables(Allocator allocator, String string) {
Scratch scratch(allocator);
Array<String> parts = {scratch};
String it = string;
for (;;) {
int64_t idx = 0;
bool found = Seek(it, "@", &idx, SeekFlag_None);
if (!found) {
if (it.len > 0) {
Add(&parts, it);
}
break;
}
String prev = GetPrefix(it, idx);
if (prev.len > 0) {
Add(&parts, prev);
}
it = Skip(it, idx + 1);
char c = At(it, 0);
String name = {};
if (c == '@') {
Add(&parts, String{"@", 1});
it = Skip(it, 1);
continue;
} else if (c == '(') {
char *start = it.data + 1;
while (At(it, 0) && At(it, 0) != ')') {
it = Skip(it, 1);
}
Int len = it.data - start;
name = {start, len};
it = Skip(it, 1); // skip ')'
} else {
char *start = it.data;
while (IsAlphanumeric(At(it, 0))) {
it = Skip(it, 1);
}
Int len = it.data - start;
name = {start, len};
}
Variable *variable = GetVariable(name);
if (!variable) {
ReportErrorf("Variable: %S, not found", name);
return string;
}
if (variable->type != VariableType_String) {
// @todo: this will not report - open will override
ReportErrorf("Variable: %S, not of type String", variable->type);
return string;
}
Add(&parts, *variable->string);
}
String result = Merge(allocator, parts, "");
return result;
}
void TestInsertVariable() {
Scratch scratch;
String a = "Thing/@(ProjectDirectory)/Another";
String b = "Thing/@ProjectDirectory/Another";
Assert(InsertVariables(scratch, a) == InsertVariables(scratch, b));
int c = 10;
} RegisterFunction(&TestFunctions, TestInsertVariable);
/*
Variables:
@ProjectDirectory/build/te
@@ -249,7 +326,13 @@ Otherwise it does filepath parsing:
TODO: need to add '~', but where?
TODO: on linux find shell on first command and set as default
USECASE: Wouldn't it be cool to just select a part of codebase pipe that into a script
and get a result in a clipboard or capture the output and change the selection.
TODO: Data desc language
!{bash,Out:Sel} SCRIPT
!{bash,Out:Clip} SCRIPT
Use variables for injecting selection: @Sel
*/
ResolvedOpen ResolveOpen(Allocator alo, Window *window, String path, ResolveOpenMeta meta) {
@@ -258,7 +341,7 @@ ResolvedOpen ResolveOpen(Allocator alo, Window *window, String path, ResolveOpen
bool exec = !(ResolveOpenMeta_DontExec & meta);
#if PLUGIN_CONFIG
// @todo: variable substitution {{ProjectDirectory}}/build/te.exe
path = InsertVariables(alo, path);
if (exec && result.kind == OpenKind_Invalid && StartsWith(path, ":Set ")) {
result.kind = OpenKind_Set;