mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 03:22:43 +00:00 
			
		
		
		
	 1682f0b760
			
		
	
	
		1682f0b760
		
	
	
	
	
		
			
			SPDX License Identifiers are a more compact / standardized way of representing file license information. See: https://spdx.dev/resources/use/#identifiers This was done with the `ambr` search and replace tool. ambr --no-parent-ignore --key-from-file --rep-from-file key.txt rep.txt *
		
			
				
	
	
		
			73 lines
		
	
	
	
		
			1.8 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			73 lines
		
	
	
	
		
			1.8 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| /*
 | |
|  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 | |
|  *
 | |
|  * SPDX-License-Identifier: BSD-2-Clause
 | |
|  */
 | |
| 
 | |
| #include "Project.h"
 | |
| #include "HackStudio.h"
 | |
| #include <LibCore/File.h>
 | |
| 
 | |
| namespace HackStudio {
 | |
| 
 | |
| Project::Project(const String& root_path)
 | |
|     : m_root_path(root_path)
 | |
| {
 | |
|     m_model = GUI::FileSystemModel::create(root_path, GUI::FileSystemModel::Mode::FilesAndDirectories);
 | |
| }
 | |
| 
 | |
| Project::~Project()
 | |
| {
 | |
| }
 | |
| 
 | |
| OwnPtr<Project> Project::open_with_root_path(const String& root_path)
 | |
| {
 | |
|     if (!Core::File::is_directory(root_path))
 | |
|         return {};
 | |
|     return adopt_own(*new Project(root_path));
 | |
| }
 | |
| 
 | |
| template<typename Callback>
 | |
| static void traverse_model(const GUI::FileSystemModel& model, const GUI::ModelIndex& index, Callback callback)
 | |
| {
 | |
|     if (index.is_valid())
 | |
|         callback(index);
 | |
|     auto row_count = model.row_count(index);
 | |
|     if (!row_count)
 | |
|         return;
 | |
|     for (int row = 0; row < row_count; ++row) {
 | |
|         auto child_index = model.index(row, GUI::FileSystemModel::Column::Name, index);
 | |
|         traverse_model(model, child_index, callback);
 | |
|     }
 | |
| }
 | |
| 
 | |
| void Project::for_each_text_file(Function<void(const ProjectFile&)> callback) const
 | |
| {
 | |
|     traverse_model(model(), {}, [&](auto& index) {
 | |
|         auto file = get_file(model().full_path(index));
 | |
|         if (file)
 | |
|             callback(*file);
 | |
|     });
 | |
| }
 | |
| 
 | |
| NonnullRefPtr<ProjectFile> Project::get_file(const String& path) const
 | |
| {
 | |
|     auto full_path = to_absolute_path(path);
 | |
|     for (auto& file : m_files) {
 | |
|         if (file.name() == full_path)
 | |
|             return file;
 | |
|     }
 | |
|     auto file = ProjectFile::construct_with_name(full_path);
 | |
|     m_files.append(file);
 | |
|     return file;
 | |
| }
 | |
| 
 | |
| String Project::to_absolute_path(const String& path) const
 | |
| {
 | |
|     if (LexicalPath { path }.is_absolute()) {
 | |
|         return path;
 | |
|     }
 | |
|     return LexicalPath { String::formatted("{}/{}", m_root_path, path) }.string();
 | |
| }
 | |
| 
 | |
| }
 |