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 import streamlit as st
from streamlit_tree_select import tree_select from streamlit_tree_select import tree_select
# project path fix # ---------------- PATH FIX ----------------
project_root = os.path.abspath(os.path.dirname(__file__)) sys.path.append(os.path.dirname(os.path.abspath(__file__)))
if project_root not in sys.path:
sys.path.insert(0, project_root)
# backend # ---------------- BACKEND ----------------
from backend.file_manager import FileManager from backend.file_manager import FileManager
from ui.navigation import FileNavigation
# your existing modules (keep yours as-is) from ui.navigation import FileNavigation
from backend.chat_manager import ChatManager
from backend.execution_engine import ExecutionEngine
from ui.editor import CodeEditor
from ui.chat import ChatInterface from ui.chat import ChatInterface
from ui.editor import CodeEditor
from ui.output import OutputDisplay 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(): def render_sidebar():
st.sidebar.title("📁 File Explorer") st.sidebar.title("📁 File Explorer")
navigation = FileNavigation() navigation = FileNavigation()
tree_data = navigation.build_tree() tree_data = navigation.build_tree(Path.cwd())
with st.sidebar: with st.sidebar:
selection = tree_select( selection = tree_select(
@ -41,47 +50,87 @@ def render_sidebar():
return None return None
# ---------------- MAIN ----------------
def main(): def main():
st.set_page_config(
page_title="AI Code Editor",
page_icon="💻",
layout="wide"
)
file_manager = FileManager() file_manager = FileManager()
chat_manager = ChatManager() chat_manager = ChatManager()
execution_engine = ExecutionEngine() execution_engine = ExecutionEngine()
# ---------------- STATE ----------------
if "selected_file" not in st.session_state: if "selected_file" not in st.session_state:
st.session_state.selected_file = None 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() selected_file = render_sidebar()
if selected_file: if selected_file:
st.session_state.selected_file = 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.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]) col1, col2 = st.columns([3, 2])
with col1: 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 = CodeEditor()
editor.display_code(content)
if st.button("💾 Save"): # IMPORTANT: only updates AFTER APPLY
file_manager.save_file(st.session_state.selected_file, content) new_value = editor.display_code(
st.success("File saved!") 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: with col2:
chat = ChatInterface() chat = ChatInterface()
chat.display_chat(chat_manager) chat.display_chat(chat_manager)
@ -89,7 +138,7 @@ def main():
output.display_execution(execution_engine) output.display_execution(execution_engine)
else: else:
st.info("Select a file from the sidebar explorer 👈") st.info("Select a file from the explorer 👈")
if __name__ == "__main__": if __name__ == "__main__":

View File

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