Files
text_editor/src/plugin_project_management.cpp
2026-07-04 08:51:41 +02:00

139 lines
4.5 KiB
C++

#if PLUGIN_PROJECT_MANAGEMENT
void SetProjectFolder(String dir) {
ProjectFolder = Intern(&GlobalInternTable, dir);
Scratch scratch;
For (Buffers) {
if (it->special) {
String name = SkipToLastSlash(it->name);
it->name = Intern(&GlobalInternTable, Format(scratch, "%S/%S", dir, name));
}
}
}
void CO_OpenCode(mco_coro *co) {
CCtx *ctx = GetCoroutineContext();
Array<String> patterns = SplitWhitespace(ctx->arena, OpenCodePatterns);
Array<String> exclude_patterns = SplitWhitespace(ctx->arena, OpenCodeExcludePatterns);
Array<String> dirs = {ctx->arena};
Add(&dirs, Copy(ctx->arena, ProjectFolder));
for (int diri = 0; diri < dirs.len; diri += 1) {
for (FileIter it = IterateFiles(ctx->arena, dirs[diri]); IsValid(it); Advance(&it)) {
bool should_open = true;
if (!it.is_directory) {
should_open = false;
ForItem (ending, patterns) {
if (EndsWith(it.absolute_path, ending)) {
should_open = true;
break;
}
}
}
ForItem (ending, exclude_patterns) {
if (EndsWith(it.absolute_path, ending)) {
should_open = false;
break;
}
}
if (!should_open) {
continue;
}
if (it.is_directory) {
Add(&dirs, it.absolute_path);
} else {
BufferOpenFile(it.absolute_path);
}
Yield(co);
}
}
} RegisterCoroutineCommand(
CO_OpenCode,
"",
"Open all code files in current ProjectFolder, the code files are determined through NonCodePatterns_EndsWith config variable list",
data->dont_wait_until_resolved = true;
);
void OpenProject(String directory) {
SetProjectFolder(directory);
#if PLUGIN_CONFIG
Scratch scratch;
for (FileIter it = IterateFiles(scratch, directory); IsValid(it); Advance(&it)) {
if (EndsWith(it.filename, "project.te")) {
LoadConfig(it.absolute_path);
}
}
#endif
}
void CMD_OpenProject() {
BSet main = GetBSet(PrimaryWindowID);
String directory = GetDirectory(main.buffer);
OpenProject(directory);
} RegisterCommand(CMD_OpenProject, "", "Sets the project directory, opens the project file etc.");
String16 CreateProjectBuildBat = uR"==(@echo off
mkdir build
pushd build
cl ../src/main.cpp /Zi /FC /nologo /WX /W3 /wd4200 /wd4334 /diagnostics:column
popd
)==";
String16 CreateProjectBuildSh = uR"==(#!/usr/bin/bash
mkdir build
cd build
clang ../src/main.cpp -g -Wall -Wno-missing-braces -Wno-writable-strings -fdiagnostics-absolute-paths
)==";
String16 CreateProjectMain = uR"==(#include <stdio.h>
int main() {
printf("hello world!\n");
return 0;
}
)==";
void CO_CreateProject(mco_coro *co) {
String16 project_name16 = QueryUserString(co, "Please tell me, how would you like to name your project?");
String directory = GetPrimaryDirectory();
// - project_directory
// - project.te
// - build.bat
// - build.sh
// - src
// - main.c
{
Scratch scratch;
String project_name = ToString(scratch, project_name16);
String project_dir = Format(scratch, "%S/%S", directory, project_name);
String src_dir = Format(scratch, "%S/src", project_dir);
MakeDir(project_dir);
MakeDir(src_dir);
SetProjectFolder(project_dir);
Buffer *project = BufferOpenFile(Format(scratch, "%S/project.te", project_dir));
RawAppendf(project, ":OpenCode\n:Set BinaryUnderDebug '%S/build/main.exe'\n", project_dir);
SaveBuffer(project);
Buffer *build_bat = BufferOpenFile(Format(scratch, "%S/build.bat", project_dir));
RawAppend(build_bat, CreateProjectBuildBat);
SaveBuffer(build_bat);
Buffer *build_sh = BufferOpenFile(Format(scratch, "%S/build.sh", project_dir));
RawAppend(build_sh, CreateProjectBuildSh);
SaveBuffer(build_sh);
Buffer *main_file = BufferOpenFile(Format(scratch, "%S/main.cpp", src_dir));
RawAppend(main_file, CreateProjectMain);
SaveBuffer(main_file);
OpenProject(project_dir);
Open(main_file->name);
}
CO_OpenCode(co);
} RegisterCoroutineCommand(CO_CreateProject, "", "Creates a project in current buffer directory with template files and all that, asks user for input etc.");
#endif