Step 199: large file handling

This commit is contained in:
Bill
2026-02-10 05:15:09 -07:00
parent ae1f8ba489
commit 02063766a8
10 changed files with 238 additions and 18 deletions

View File

@@ -3,6 +3,7 @@
#include "../EditorUtils.h"
#include <filesystem>
#include <unordered_set>
#include <cstdio>
static int buildCommandContextMask(const EditorState& state) {
int mask = CommandContext_Editor;
@@ -31,6 +32,14 @@ static std::string makeRelativePath(const std::string& path, const std::string&
return rel.generic_string();
}
static std::string formatBytes(size_t bytes) {
const double mb = 1024.0 * 1024.0;
double value = bytes / mb;
char buf[64];
std::snprintf(buf, sizeof(buf), "%.2f MB", value);
return buf;
}
static void drawHighlightedText(ImDrawList* drawList,
const std::string& text,
const std::vector<int>& indices,
@@ -525,3 +534,31 @@ static void renderCommandPalette(EditorState& state) {
}
ImGui::End();
}
static void renderLargeFilePrompt(EditorState& state) {
if (state.showLargeFilePrompt) {
ImGui::OpenPopup("Large File");
}
if (ImGui::BeginPopupModal("Large File",
&state.showLargeFilePrompt,
ImGuiWindowFlags_AlwaysAutoResize)) {
ImGui::TextUnformatted("Large file detected.");
if (!state.largeFilePromptPath.empty()) {
ImGui::TextWrapped("%s", state.largeFilePromptPath.c_str());
}
ImGui::TextDisabled("Size: %s", formatBytes(state.largeFilePromptBytes).c_str());
ImGui::Separator();
ImGui::TextUnformatted("Open in Text Mode for better performance?");
if (ImGui::Button("Open in Text Mode")) {
state.setActiveBufferMode(BufferManager::BufferMode::Text);
state.showLargeFilePrompt = false;
ImGui::CloseCurrentPopup();
}
ImGui::SameLine();
if (ImGui::Button("Keep Structured")) {
state.showLargeFilePrompt = false;
ImGui::CloseCurrentPopup();
}
ImGui::EndPopup();
}
}

View File

@@ -360,6 +360,27 @@ static void renderSettingsPanel(EditorState& state) {
settingsChanged = true;
}
ImGui::Separator();
ImGui::TextUnformatted("Large File Thresholds (MB)");
int warnMb = state.settings.getLargeFileWarnMB();
if (ImGui::InputInt("Warn > MB", &warnMb)) {
warnMb = std::max(1, warnMb);
state.settings.setLargeFileWarnMB(warnMb);
settingsChanged = true;
}
int textMb = state.settings.getLargeFileTextMB();
if (ImGui::InputInt("Auto Text Mode > MB", &textMb)) {
textMb = std::max(1, textMb);
state.settings.setLargeFileTextMB(textMb);
settingsChanged = true;
}
int disableHlMb = state.settings.getLargeFileDisableHighlightMB();
if (ImGui::InputInt("Disable Highlight > MB", &disableHlMb)) {
disableHlMb = std::max(1, disableHlMb);
state.settings.setLargeFileDisableHighlightMB(disableHlMb);
settingsChanged = true;
}
LayoutPreset preset = state.ui.layoutPreset;
int presetIndex = 0;
if (preset == LayoutPreset::Emacs) presetIndex = 1;

View File

@@ -4,6 +4,12 @@
#include "../ThemeEngine.h"
#include <fstream>
#include <filesystem>
#include <cstdio>
#include <string>
#ifdef _WIN32
#include <windows.h>
#include <psapi.h>
#endif
static std::string detectGitBranch(const std::string& root) {
if (root.empty()) return "-";
@@ -22,17 +28,11 @@ static std::string detectGitBranch(const std::string& root) {
static void toggleBufferMode(EditorState& state) {
if (!state.active()) return;
state.active()->bufferMode = state.active()->bufferMode == BufferManager::BufferMode::Text
? BufferManager::BufferMode::Structured
: BufferManager::BufferMode::Text;
state.buffers.setBufferMode(state.active()->path, state.active()->bufferMode);
if (state.active()->bufferMode == BufferManager::BufferMode::Text) {
state.suggestions.clear();
state.whetstoneDiagnostics.clear();
state.analysisPending = false;
} else {
state.onTextChanged();
}
BufferManager::BufferMode next =
state.active()->bufferMode == BufferManager::BufferMode::Text
? BufferManager::BufferMode::Structured
: BufferManager::BufferMode::Text;
state.setActiveBufferMode(next);
}
static std::string detectLineEnding(const std::string& text) {
@@ -40,6 +40,27 @@ static std::string detectLineEnding(const std::string& text) {
return "LF";
}
static size_t processMemoryBytes() {
#ifdef _WIN32
PROCESS_MEMORY_COUNTERS_EX pmc{};
if (GetProcessMemoryInfo(GetCurrentProcess(),
reinterpret_cast<PROCESS_MEMORY_COUNTERS*>(&pmc),
sizeof(pmc))) {
return static_cast<size_t>(pmc.WorkingSetSize);
}
#endif
return 0;
}
static std::string formatMemory(size_t bytes) {
if (bytes == 0) return "-";
const double mb = 1024.0 * 1024.0;
double value = bytes / mb;
char buf[64];
std::snprintf(buf, sizeof(buf), "%.0f MB", value);
return buf;
}
static void renderStatusBar(EditorState& state) {
const ImGuiViewport* viewport = ImGui::GetMainViewport();
ImGuiWindowFlags sbFlags = ImGuiWindowFlags_NoDecoration |
@@ -77,6 +98,14 @@ static void renderStatusBar(EditorState& state) {
if (ImGui::Button(modeLabel.c_str())) {
toggleBufferMode(state);
}
if (state.active() &&
state.active()->bufferMode == BufferManager::BufferMode::Text &&
state.active()->fileSizeBytes > 0) {
ImGui::SameLine(0, 6);
if (ImGui::Button("Reopen Structured")) {
state.setActiveBufferMode(BufferManager::BufferMode::Structured);
}
}
ImGui::SameLine(0, 10);
const char* langLabel = state.active() ? state.active()->language.c_str() : "-";
if (ImGui::Button(langLabel)) {
@@ -140,6 +169,13 @@ static void renderStatusBar(EditorState& state) {
rightText += " | Sel " + std::to_string(selChars) + "c " +
std::to_string(selLines) + "l";
}
if (state.active() && state.active()->largeFileMode) {
rightText += " | Large File";
}
size_t memBytes = processMemoryBytes();
if (memBytes > 0) {
rightText += " | Mem " + formatMemory(memBytes);
}
rightText += " | Zoom " + std::to_string(zoomPercent(state.settings.getFontSize(),
state.baseFontSize)) + "%";
rightText += " | Git " + detectGitBranch(state.workspaceRoot);