This commit is contained in:
leart-ramushi 2026-05-25 01:54:58 +02:00
parent c3e37d1709
commit d9a46ba701
3 changed files with 115 additions and 56 deletions

View File

@ -5,28 +5,37 @@ from pathlib import Path
import streamlit as st
from streamlit_tree_select import tree_select
# project path fix
project_root = os.path.abspath(os.path.dirname(__file__))
if project_root not in sys.path:
sys.path.insert(0, project_root)
# ---------------- PATH FIX ----------------
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
# backend
# ---------------- BACKEND ----------------
from backend.file_manager import FileManager
from ui.navigation import FileNavigation
# your existing modules (keep yours as-is)
from backend.chat_manager import ChatManager
from backend.execution_engine import ExecutionEngine
from ui.editor import CodeEditor
from ui.navigation import FileNavigation
from ui.chat import ChatInterface
from ui.editor import CodeEditor
from ui.output import OutputDisplay
from backend.chat_manager import ChatManager
from backend.execution_engine import ExecutionEngine
# ---------------- PAGE ----------------
st.set_page_config(
page_title="AI Code Editor",
page_icon="💻",
layout="wide"
)
# ---------------- SIDEBAR ----------------
def render_sidebar():
st.sidebar.title("📁 File Explorer")
navigation = FileNavigation()
tree_data = navigation.build_tree()
tree_data = navigation.build_tree(Path.cwd())
with st.sidebar:
selection = tree_select(
@ -41,47 +50,87 @@ def render_sidebar():
return None
# ---------------- MAIN ----------------
def main():
st.set_page_config(
page_title="AI Code Editor",
page_icon="💻",
layout="wide"
)
file_manager = FileManager()
chat_manager = ChatManager()
execution_engine = ExecutionEngine()
# ---------------- STATE ----------------
if "selected_file" not in st.session_state:
st.session_state.selected_file = None
# --- SIDEBAR ---
if "active_file" not in st.session_state:
st.session_state.active_file = None
if "editor_content" not in st.session_state:
st.session_state.editor_content = ""
if "last_applied_content" not in st.session_state:
st.session_state.last_applied_content = ""
# ---------------- FILE SELECT ----------------
selected_file = render_sidebar()
if selected_file:
st.session_state.selected_file = selected_file
current_file = st.session_state.selected_file
# ---------------- FILE SWITCH ----------------
if current_file != st.session_state.active_file:
st.session_state.active_file = current_file
if current_file:
content = file_manager.read_file(current_file)
st.session_state.editor_content = content
st.session_state.last_applied_content = content
else:
st.session_state.editor_content = ""
st.sidebar.markdown("---")
st.sidebar.caption("AISE501 AI in Software Engineering")
st.sidebar.caption("AI Code Editor")
# --- MAIN AREA ---
if st.session_state.selected_file:
content = file_manager.read_file(st.session_state.selected_file)
# ---------------- MAIN UI ----------------
if st.session_state.active_file:
col1, col2 = st.columns([3, 2])
with col1:
st.subheader(f"Editing: {Path(st.session_state.selected_file).name}")
st.subheader(f"Editing: {Path(st.session_state.active_file).name}")
editor = CodeEditor()
editor.display_code(content)
if st.button("💾 Save"):
file_manager.save_file(st.session_state.selected_file, content)
st.success("File saved!")
# IMPORTANT: only updates AFTER APPLY
new_value = editor.display_code(
st.session_state.editor_content,
filename=st.session_state.active_file
)
# ---------------- APPLY DETECTION ----------------
# Apply only triggers when Streamlit reruns with new value
if new_value is not None:
if new_value != st.session_state.last_applied_content:
st.session_state.editor_content = new_value
st.session_state.last_applied_content = new_value
file_manager.save_file(
st.session_state.active_file,
new_value
)
st.success("Saved via Apply ✔")
with col2:
chat = ChatInterface()
chat.display_chat(chat_manager)
@ -89,7 +138,7 @@ def main():
output.display_execution(execution_engine)
else:
st.info("Select a file from the sidebar explorer 👈")
st.info("Select a file from the explorer 👈")
if __name__ == "__main__":

View File

@ -3,4 +3,6 @@
from .navigation import FileNavigation
from .editor import CodeEditor
from .chat import ChatInterface
from .output import OutputDisplay
from .output import OutputDisplay
# Test

View File

@ -1,30 +1,38 @@
import streamlit as st
from streamlit_ace import st_ace
class CodeEditor:
"""
Code Editor & Anzeige Komponente [1]
"""
def __init__(self):
self.language = "python"
self.show_line_numbers = True
def display_code(self, code_content):
"""
Zeigt den Code-Editor mit Inhalt an
"""
if code_content:
st.code(code_content, language=self.language)
else:
st.info("Bitte wählen Sie eine Datei aus der Sidebar aus")
def get_code_content(self):
"""
Liest den aktuellen Code-Inhalt vom Editor
"""
return st.session_state.get("code_content", "")
def update_code_content(self, new_content):
"""
Aktualisiert den Code-Inhalt im Session State
"""
st.session_state.code_content = new_content
def detect_language(self, filename):
mapping = {
".py": "python",
".js": "javascript",
".html": "html",
".css": "css",
".json": "json",
".md": "markdown",
}
for ext, lang in mapping.items():
if filename.endswith(ext):
return lang
return "text"
def display_code(self, code_content, filename=""):
language = self.detect_language(filename)
return st_ace(
value=code_content,
language=language,
theme="chrome",
key=f"ace_{filename}",
height=500,
font_size=14,
tab_size=4,
wrap=False,
auto_update=False,
show_gutter=True,
show_print_margin=False
)