Skip to content

Commit d913207

Browse files
authored
Merge pull request #29 from sandbox-science/operationContextMenu
Operation context menu
2 parents c7fa2de + 46d2d41 commit d913207

13 files changed

+682
-243
lines changed

CMakeLists.txt

Lines changed: 18 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,141 +1,35 @@
11
cmake_minimum_required(VERSION 3.16)
22

3-
# Project name
4-
set(TARGET_NAME CodeAstraApp)
5-
set(EXECUTABLE_NAME CodeAstra)
3+
project(CodeAstra VERSION 0.1.0 DESCRIPTION "Code Editor written in modern C++ using Qt6")
64

7-
set(QT_MAJOR_VERSION 6)
8-
9-
project(${TARGET_NAME} VERSION 0.0.1 DESCRIPTION "Code Editor written in C++ using Qt6")
10-
11-
# Enable automatic MOC (Meta-Object Compiler) handling for Qt
12-
set(CMAKE_AUTOMOC ON)
13-
14-
# Set the CXX standard
155
set(CMAKE_CXX_STANDARD 20)
166
set(CMAKE_CXX_STANDARD_REQUIRED ON)
177

18-
# Set default build output directories
19-
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR})
20-
set(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR})
8+
# Use cmake/ for custom modules
9+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
2110

22-
# Detect operating system
23-
if(WIN32)
24-
set(OS_NAME "Windows")
25-
elseif(APPLE)
26-
set(OS_NAME "macOS")
27-
else()
28-
set(OS_NAME "Linux")
29-
endif()
11+
# Set output directories
12+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
13+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
14+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
3015

31-
message(STATUS "Building for ${OS_NAME}")
16+
# Enable Qt tools
17+
set(CMAKE_AUTOMOC ON)
3218

33-
# Locate Qt installation
34-
if(DEFINED ENV{Qt${QT_MAJOR_VERSION}_HOME})
35-
set(Qt_DIR "$ENV{Qt${QT_MAJOR_VERSION}_HOME}")
36-
message(STATUS "Using Qt from: ${Qt_DIR}")
37-
else()
38-
if(WIN32)
39-
set(Qt_DIR "C:/Qt/${QT_MAJOR_VERSION}/msvc2022_64/lib/cmake/Qt${QT_MAJOR_VERSION}")
40-
elseif(APPLE)
41-
set(Qt_DIR "/usr/local/opt/qt/lib/cmake/Qt${QT_MAJOR_VERSION}")
42-
else()
43-
set(Qt_DIR "/usr/lib/cmake/Qt${QT_MAJOR_VERSION}")
44-
endif()
45-
message(STATUS "Using default Qt path: ${Qt_DIR}")
46-
endif()
19+
# Define target names
20+
set(TARGET_NAME CodeAstra)
21+
set(EXECUTABLE_NAME ${TARGET_NAME}App)
4722

48-
# Set Qt path for find_package
49-
set(CMAKE_PREFIX_PATH ${Qt_DIR})
23+
# Set Qt version
24+
set(QT_MAJOR_VERSION 6)
5025

51-
# Find Qt components
26+
# Find Qt
5227
find_package(Qt${QT_MAJOR_VERSION} REQUIRED COMPONENTS Core Widgets Test)
5328

54-
# Locate yaml-cpp
55-
if(APPLE)
56-
set(yaml-cpp_DIR "/opt/homebrew/Cellar/yaml-cpp/0.8.0/lib/cmake/yaml-cpp")
57-
endif()
29+
# yaml-cpp
5830
find_package(yaml-cpp REQUIRED CONFIG)
5931

60-
# Copy YAML files to the build directory (non-macOS case)
61-
set(YAML_SOURCE_DIR ${CMAKE_SOURCE_DIR}/config)
62-
set(YAML_DEST_DIR ${CMAKE_BINARY_DIR}/config)
63-
file(MAKE_DIRECTORY ${YAML_DEST_DIR})
64-
file(GLOB YAML_FILES "${YAML_SOURCE_DIR}/*.yaml")
65-
66-
foreach(YAML_FILE ${YAML_FILES})
67-
file(COPY ${YAML_FILE} DESTINATION ${YAML_DEST_DIR})
68-
endforeach()
69-
70-
# Create the CodeAstra library
71-
add_library(${TARGET_NAME}
72-
src/MainWindow.cpp
73-
src/CodeEditor.cpp
74-
src/Tree.cpp
75-
src/FileManager.cpp
76-
src/Syntax.cpp
77-
src/SyntaxManager.cpp
78-
include/MainWindow.h
79-
include/CodeEditor.h
80-
include/Tree.h
81-
include/LineNumberArea.h
82-
include/FileManager.h
83-
include/SyntaxManager.h
84-
include/Syntax.h
85-
)
86-
87-
# Link YAML-CPP to the CodeAstra library
88-
target_link_libraries(${TARGET_NAME} PRIVATE yaml-cpp)
89-
90-
# Create the executable for the application
91-
add_executable(${EXECUTABLE_NAME} src/main.cpp)
92-
93-
# Ensure YAML config files are copied into macOS app bundle
94-
# if(APPLE)
95-
# add_custom_command(TARGET ${EXECUTABLE_NAME} POST_BUILD
96-
# COMMAND ${CMAKE_COMMAND} -E make_directory "$<TARGET_BUNDLE_DIR:${EXECUTABLE_NAME}>/Contents/Resources/config"
97-
# COMMAND ${CMAKE_COMMAND} -E copy_directory "${YAML_SOURCE_DIR}" "$<TARGET_BUNDLE_DIR:${EXECUTABLE_NAME}>/Contents/Resources/config"
98-
# COMMENT "Copying YAML config files into macOS app bundle..."
99-
# )
100-
# endif()
101-
102-
# Link the main executable with the CodeAstra library and Qt libraries
103-
target_link_libraries(${EXECUTABLE_NAME} PRIVATE ${TARGET_NAME} Qt${QT_MAJOR_VERSION}::Core Qt${QT_MAJOR_VERSION}::Widgets)
104-
105-
# Add the tests subdirectory
32+
# Add subdirectories
33+
add_subdirectory(src)
10634
add_subdirectory(tests)
10735

108-
# Qt resource files
109-
qt_add_resources(APP_RESOURCES resources.qrc)
110-
target_sources(${EXECUTABLE_NAME} PRIVATE ${APP_RESOURCES})
111-
112-
# Compiler flags per OS
113-
if(MSVC)
114-
target_compile_options(${EXECUTABLE_NAME} PRIVATE /W4 /WX)
115-
elseif(APPLE)
116-
target_compile_options(${EXECUTABLE_NAME} PRIVATE -Wall -Wextra -pedantic -Werror)
117-
# set_target_properties(${EXECUTABLE_NAME} PROPERTIES MACOSX_BUNDLE TRUE)
118-
else()
119-
target_compile_options(${EXECUTABLE_NAME} PRIVATE -Wall -Wextra -pedantic -Werror)
120-
endif()
121-
122-
# Include directories
123-
target_include_directories(${EXECUTABLE_NAME} PRIVATE ${Qt${QT_MAJOR_VERSION}_INCLUDE_DIRS})
124-
target_include_directories(${EXECUTABLE_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/include)
125-
target_include_directories(${TARGET_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/include)
126-
127-
# Set output names properly for Debug and Release
128-
set_target_properties(${EXECUTABLE_NAME} PROPERTIES
129-
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}"
130-
DEBUG_OUTPUT_NAME "${EXECUTABLE_NAME}d"
131-
RELEASE_OUTPUT_NAME ${EXECUTABLE_NAME}
132-
)
133-
134-
# Link necessary Qt libraries to CodeAstra library
135-
target_link_libraries(${TARGET_NAME} PRIVATE Qt${QT_MAJOR_VERSION}::Core Qt${QT_MAJOR_VERSION}::Widgets)
136-
137-
# Ensure correct linking of yaml-cpp (macOS)
138-
if(APPLE)
139-
target_include_directories(${EXECUTABLE_NAME} PRIVATE /opt/homebrew/include)
140-
target_link_directories(${EXECUTABLE_NAME} PRIVATE /opt/homebrew/lib)
141-
endif()

Makefile

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,39 @@
11
PROJECT = CodeAstra
22
BUILD_DIR = $(PWD)/build
3-
EXECUTABLE = $(PROJECT)
43

5-
# Set CMake options
64
CMAKE_OPTIONS = ..
75

8-
# Default target: Run CMake and install the project
9-
all: build install
6+
.PHONY: all build clean install build_tests test
7+
8+
all: install
109

11-
# Run CMake to build the project
1210
build:
13-
@echo "Building $(PROJECT) with CMake..."
11+
@echo "Building $(PROJECT)..."
1412
@mkdir -p $(BUILD_DIR)
1513
@cd $(BUILD_DIR) && cmake $(CMAKE_OPTIONS)
1614

17-
# Clean the build directory
1815
clean:
1916
@echo "Cleaning the build directory..."
2017
@rm -rf $(BUILD_DIR)
2118

22-
# Uninstalling the software
23-
uninstall: clean
24-
@echo "Uninstalling $(PROJECT)..."
25-
@rm -rf $(EXECUTABLE).app $(EXECUTABLE)d.app
26-
27-
# Install the project
2819
install: build
2920
@echo "Installing $(PROJECT)..."
30-
@cd $(BUILD_DIR) && make
31-
@echo "$(PROJECT) installed."
21+
@cmake --build $(BUILD_DIR)
22+
@echo "Installation complete."
3223

3324
build_tests: build
34-
@cd $(BUILD_DIR)/tests/ && make
25+
@echo "Building tests..."
26+
@$(MAKE) -C $(BUILD_DIR)/tests
3527

3628
test: build_tests
29+
@echo "Running tests..."
3730
@for test in ./build/tests/test_*; do \
3831
if [ -f $$test ]; then \
3932
echo "Running $$test..."; \
4033
$$test; \
4134
fi; \
4235
done
36+
37+
run:
38+
@echo "Running $(PROJECT)..."
39+
@./build/bin/$(PROJECT)

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Please, check the [wiki](https://github.com/sandbox-science/CodeAstra/wiki) for
5252
- [ ] Create a new file ~ in progress
5353
- [x] File tree navigation
5454
- [ ] Syntax highlighting ~ in progress
55-
- Supported Languagues:
55+
- Supported Languages:
5656
- [x] Markdown (**foundation**)
5757
- [x] YAML (**foundation**)
5858
- [ ] C/C++ (**in progress**)
@@ -63,4 +63,4 @@ Please, check the [wiki](https://github.com/sandbox-science/CodeAstra/wiki) for
6363
- [ ] Plugin system
6464

6565
## To-Do
66-
Find tasks to-do on our open [issues](https://github.com/sandbox-science/CodeAstra/issues)
66+
Find tasks to do on our open [issues](https://github.com/sandbox-science/CodeAstra/issues)

include/CodeEditor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class CodeEditor : public QPlainTextEdit
2828
Mode mode = NORMAL;
2929
void lineNumberAreaPaintEvent(QPaintEvent *event);
3030
int lineNumberAreaWidth();
31+
void autoIndentation();
3132

3233
signals:
3334
void statusMessageChanged(const QString &message);

include/FileManager.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,17 @@
33
#include <QObject>
44
#include <memory>
55
#include <QSyntaxHighlighter>
6+
#include <QFileInfo>
67

78
class CodeEditor;
89
class MainWindow;
910

11+
struct OperationResult
12+
{
13+
bool success;
14+
std::string message;
15+
};
16+
1017
/**
1118
* @class FileManager
1219
* @brief Manages file operations such as creating, saving, and opening files.
@@ -25,9 +32,11 @@ class FileManager : public QObject
2532
static FileManager &getInstance(CodeEditor *editor = nullptr, MainWindow *mainWindow = nullptr)
2633
{
2734
static FileManager instance(editor, mainWindow);
35+
if (editor && mainWindow) {
36+
instance.initialize(editor, mainWindow);
37+
}
2838
return instance;
2939
}
30-
3140
FileManager(const FileManager &) = delete;
3241
FileManager &operator=(const FileManager &) = delete;
3342

@@ -37,6 +46,12 @@ class FileManager : public QObject
3746
void setCurrentFileName(const QString fileName);
3847
void initialize(CodeEditor *editor, MainWindow *mainWindow);
3948

49+
static OperationResult renamePath(const QFileInfo &pathInfo, const QString &newName);
50+
static OperationResult newFile(const QFileInfo &pathInfo, QString newFilePath);
51+
static OperationResult newFolder(const QFileInfo &pathInfo, QString newFolderPath);
52+
static OperationResult duplicatePath(const QFileInfo &pathInfo);
53+
static OperationResult deletePath(const QFileInfo &pathInfo);
54+
4055
public slots:
4156
void newFile();
4257
void saveFile();
@@ -54,4 +69,4 @@ public slots:
5469
MainWindow *m_mainWindow;
5570
QSyntaxHighlighter *m_currentHighlighter = nullptr;
5671
QString m_currentFileName;
57-
};
72+
};

include/Tree.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
#pragma once
22

3+
#include "FileManager.h"
4+
35
#include <QSplitter>
46
#include <QObject>
57
#include <memory>
8+
#include <QFileInfo>
69

710
// Forward declarations
811
class QTreeView;
912
class QFileSystemModel;
1013
class QFileIconProvider;
11-
class FileManager;
1214

1315
/**
1416
* @class Tree
@@ -30,8 +32,12 @@ class Tree : public QObject
3032
void setupTree();
3133
void openFile(const QModelIndex &index);
3234

35+
QFileSystemModel* getModel() const;
36+
3337
private:
3438
void showContextMenu(const QPoint &pos);
39+
QFileInfo getPathInfo();
40+
void isSuccessful(OperationResult result);
3541

3642
std::unique_ptr<QFileIconProvider> m_iconProvider;
3743
std::unique_ptr<QFileSystemModel> m_model;

src/CMakeLists.txt

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
set(TARGET_NAME CodeAstraApp)
2+
set(EXECUTABLE_NAME CodeAstra)
3+
4+
# Source files
5+
set(SOURCES
6+
MainWindow.cpp
7+
CodeEditor.cpp
8+
Tree.cpp
9+
FileManager.cpp
10+
Syntax.cpp
11+
SyntaxManager.cpp
12+
)
13+
14+
# Headers
15+
set(HEADERS
16+
${CMAKE_SOURCE_DIR}/include/MainWindow.h
17+
${CMAKE_SOURCE_DIR}/include/CodeEditor.h
18+
${CMAKE_SOURCE_DIR}/include/Tree.h
19+
${CMAKE_SOURCE_DIR}/include/FileManager.h
20+
${CMAKE_SOURCE_DIR}/include/Syntax.h
21+
${CMAKE_SOURCE_DIR}/include/SyntaxManager.h
22+
${CMAKE_SOURCE_DIR}/include/LineNumberArea.h
23+
)
24+
25+
# Find yaml-cpp using CMake's package config
26+
find_package(yaml-cpp REQUIRED)
27+
28+
# Library
29+
add_library(${TARGET_NAME} ${SOURCES} ${HEADERS})
30+
target_include_directories(${TARGET_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/include)
31+
32+
# Link against the proper target
33+
target_link_libraries(${TARGET_NAME} PRIVATE Qt${QT_MAJOR_VERSION}::Core Qt${QT_MAJOR_VERSION}::Widgets yaml-cpp::yaml-cpp)
34+
set_target_properties(${TARGET_NAME} PROPERTIES POSITION_INDEPENDENT_CODE ON)
35+
36+
# Executable
37+
add_executable(${EXECUTABLE_NAME} ${CMAKE_SOURCE_DIR}/src/main.cpp)
38+
target_link_libraries(${EXECUTABLE_NAME} PRIVATE ${TARGET_NAME} Qt6::Core Qt6::Widgets)
39+
target_include_directories(${EXECUTABLE_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/include)
40+
41+
# Resources
42+
qt_add_resources(APP_RESOURCES ${CMAKE_SOURCE_DIR}/resources.qrc)
43+
target_sources(${EXECUTABLE_NAME} PRIVATE ${APP_RESOURCES})
44+
45+
# OS-specific flags
46+
if(MSVC)
47+
target_compile_options(${EXECUTABLE_NAME} PRIVATE /W4 /WX /analyze /sdl /guard:cf)
48+
elseif(APPLE OR UNIX)
49+
target_compile_options(${EXECUTABLE_NAME} PRIVATE -Wall -Wextra -Wpedantic -Werror -Wshadow -Wconversion -Wsign-conversion -fsanitize=address,undefined -fstack-protector)
50+
target_link_options(${EXECUTABLE_NAME} PRIVATE -fsanitize=address,undefined)
51+
endif()
52+
53+
# Copy config files
54+
file(GLOB YAML_FILES "${CMAKE_SOURCE_DIR}/config/*.yaml")
55+
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/config)
56+
foreach(YAML_FILE ${YAML_FILES})
57+
configure_file(${YAML_FILE} ${CMAKE_BINARY_DIR}/config/ COPYONLY)
58+
endforeach()

0 commit comments

Comments
 (0)