Begin design of comment evaluation and {{variables}}

This commit is contained in:
Krzosa Karol
2026-01-31 09:49:44 +01:00
parent 4d9cfcd302
commit 217659256b
7 changed files with 54 additions and 13 deletions

View File

@@ -1172,3 +1172,25 @@ API void CloseStdin(Process *process) {
API double GetTimeSeconds() {
return GetTimeMicros() / 1000000.0;
}
API String WriteTempFile(String data) {
Scratch scratch;
#if OS_WINDOWS
int buffer_len = MAX_PATH+1;
char16_t *buffer = AllocArray(scratch, char16_t, buffer_len);
Assert(sizeof(char16_t) == sizeof(wchar_t));
DWORD result = GetTempPath2W(buffer_len, (LPWSTR)buffer);
Assert(result != 0);
String16 temp16 = {buffer, result};
NormalizePathInPlace(temp16);
String temp_directory = ToString(scratch, temp16);
#else
String temp_directory = "/tmp";
#endif
String temp_filename = Format(scratch, "%S/temp%llu", temp_directory, GetTimeNanos());
bool done = WriteFile(temp_filename, data);
Assert(done);
return temp_filename;
}

View File

@@ -310,7 +310,7 @@ API Int GetWordEnd(Buffer *buffer, Int pos) {
}
API bool IsLoadWord(char16_t w) {
bool result = w == u'-' || w == u'/' || w == u'\\' || w == u':' || w == u'$' || w == u'_' || w == u'.' || w == u'!' || w == u'@';
bool result = w == u'-' || w == u'/' || w == u'\\' || w == u':' || w == u'$' || w == u'_' || w == u'.' || w == u'!' || w == u'@' || w == '{' || w == '}';
if (!result) {
result = !(IsSymbol(w) || IsWhitespace(w));
}

View File

@@ -68,6 +68,7 @@ void Appendf(View *view, const char *fmt, ...) {
}
void ReportErrorf(const char *fmt, ...) {
ErrorCount += 1;
Scratch scratch;
STRING_FORMAT(scratch, fmt, string);
if (BreakOnError) {

View File

@@ -14,6 +14,11 @@ Int ErrorCount;
String16 InitialScratchContent = uR"==(:OpenProject
C:/text_editor/src/text_editor/text_editor.cpp
:Set FontSize 70
/*
!python {{TEMP}}
for it in i:
memes
*/
0
1
2

View File

@@ -56,6 +56,17 @@ 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 = ⁢
break;
}
}
return var;
}
BufferID LoadConfig(String config_path) {
ReportConsolef("Loading config %S...", config_path);
Window *window = GetWindow(NullWindowID);
@@ -69,7 +80,6 @@ BufferID LoadConfig(String config_path) {
return buffer->id;
}
#define ExpectP(x, ...) \
if (!(x)) { \
ReportErrorf("Failed to parse '" __FUNCTION__ "' command, " __VA_ARGS__); \
@@ -80,14 +90,7 @@ void Set(String string) {
String name = SkipIdent(&string);
ExpectP(name.len != 0, "expected a variable name, instead got '%S'", string);
Variable *var = NULL;
For (Variables) {
if (name == it.name) {
var = ⁢
break;
}
}
Variable *var = GetVariable(name);
if (var) {
SkipWhitespace(&string);
if (var->type == VariableType_String) {

View File

@@ -854,7 +854,7 @@ int main(int argc, char **argv)
{
InitScratch();
InitOS((OSErrorReport *)printf);
#if _WIN32
#if OS_WINDOWS
int argc = __argc;
char **argv = __argv;
AttachConsole(ATTACH_PARENT_PROCESS);

View File

@@ -214,7 +214,7 @@ void MouseLoadWord(Event event, ResolveOpenMeta meta = ResolveOpenMeta_Normal) {
}
bool IsOpenBoundary(char c) {
bool result = c == 0 || IsBrace(c) || c == ':' || c == '\t' || c == '\n' || c == '"' || c == '\'';
bool result = c == 0 || c == ':' || c == '\t' || c == '\n' || c == '"' || c == '\'';
return result;
}
@@ -224,6 +224,8 @@ ResolvedOpen ResolveOpen(Allocator alo, Window *window, String path, ResolveOpen
bool exec = !(ResolveOpenMeta_DontExec & meta);
#if PLUGIN_CONFIG
// @todo: variable substitution {{ProjectDirectory}}/build/te.exe
if (exec && result.kind == OpenKind_Invalid && StartsWith(path, ":Set ")) {
result.kind = OpenKind_Set;
result.path = Skip(path, 5);
@@ -253,6 +255,15 @@ ResolvedOpen ResolveOpen(Allocator alo, Window *window, String path, ResolveOpen
if (exec && result.kind == OpenKind_Invalid && StartsWith(path, "!")) {
result.kind = OpenKind_Exec;
result.path = Skip(path, 1);
Int idx = 0;
String needle = "{{TEMP}}";
if (Seek(result.path, needle, &idx, SeekFlag_None)) {
String rest = Skip(result.path, idx + needle.len);
String begin = GetPrefix(result.path, idx);
String temp_filename = WriteTempFile(rest);
result.path = Format(alo, "%S%S", begin, temp_filename);
}
}
// https://web
@@ -291,7 +302,6 @@ ResolvedOpen ResolveOpen(Allocator alo, Window *window, String path, ResolveOpen
}
path = {pstart.data, (Int)(p.data - pstart.data)};
// @todo: verify!
// For (LINE:COLUMN): error: - we can either backtrack at the end since we are including
// the parenthesis and whitespace or alternatively we can look for patterns on every
// character move in the loop... For now let's do backtracking. This doesn't handle all paths