diff --git a/AK/LexicalPath.h b/AK/LexicalPath.h index 8c4d71f409..82db790747 100644 --- a/AK/LexicalPath.h +++ b/AK/LexicalPath.h @@ -14,7 +14,6 @@ namespace AK { class LexicalPath { public: - LexicalPath() = default; explicit LexicalPath(String); bool is_absolute() const { return !m_string.is_empty() && m_string[0] == '/'; } diff --git a/Tests/AK/TestLexicalPath.cpp b/Tests/AK/TestLexicalPath.cpp index 61832724fb..be6f9f43e6 100644 --- a/Tests/AK/TestLexicalPath.cpp +++ b/Tests/AK/TestLexicalPath.cpp @@ -149,11 +149,6 @@ TEST_CASE(has_extension) EXPECT(path.has_extension(".png")); } - { - LexicalPath path; - EXPECT_EQ(path.has_extension(".png"), false); - } - { LexicalPath path("png"); EXPECT_EQ(path.has_extension(".png"), false); diff --git a/Userland/Applications/HexEditor/HexEditorWidget.cpp b/Userland/Applications/HexEditor/HexEditorWidget.cpp index 44805956c0..5534ebe744 100644 --- a/Userland/Applications/HexEditor/HexEditorWidget.cpp +++ b/Userland/Applications/HexEditor/HexEditorWidget.cpp @@ -78,7 +78,7 @@ HexEditorWidget::HexEditorWidget() if (file_size.has_value() && file_size.value() > 0) { m_document_dirty = false; m_editor->set_buffer(ByteBuffer::create_zeroed(file_size.value())); - set_path(LexicalPath()); + set_path({}); update_title(); } else { GUI::MessageBox::show(window(), "Invalid file size entered.", "Error", GUI::MessageBox::Type::Error); @@ -130,7 +130,7 @@ HexEditorWidget::HexEditorWidget() } m_document_dirty = false; - set_path(LexicalPath(save_path.value())); + set_path(save_path.value()); dbgln("Wrote document to {}", save_path.value()); }); @@ -311,11 +311,18 @@ void HexEditorWidget::initialize_menubar(GUI::Menubar& menubar) help_menu.add_action(GUI::CommonActions::make_about_action("Hex Editor", GUI::Icon::default_icon("app-hex-editor"), window())); } -void HexEditorWidget::set_path(const LexicalPath& lexical_path) +void HexEditorWidget::set_path(StringView const& path) { - m_path = lexical_path.string(); - m_name = lexical_path.title(); - m_extension = lexical_path.extension(); + if (path.is_empty()) { + m_path = {}; + m_name = {}; + m_extension = {}; + } else { + auto lexical_path = LexicalPath(path); + m_path = lexical_path.string(); + m_name = lexical_path.title(); + m_extension = lexical_path.extension(); + } update_title(); } @@ -342,7 +349,7 @@ void HexEditorWidget::open_file(const String& path) m_document_dirty = false; m_editor->set_buffer(file->read_all()); // FIXME: On really huge files, this is never going to work. Should really create a framework to fetch data from the file on-demand. - set_path(LexicalPath(path)); + set_path(path); } bool HexEditorWidget::request_close() diff --git a/Userland/Applications/HexEditor/HexEditorWidget.h b/Userland/Applications/HexEditor/HexEditorWidget.h index 14278ab37d..c2a689461f 100644 --- a/Userland/Applications/HexEditor/HexEditorWidget.h +++ b/Userland/Applications/HexEditor/HexEditorWidget.h @@ -27,7 +27,7 @@ public: private: HexEditorWidget(); - void set_path(const LexicalPath& file); + void set_path(StringView const&); void update_title(); void set_search_results_visible(bool visible); diff --git a/Userland/Applications/TextEditor/MainWidget.cpp b/Userland/Applications/TextEditor/MainWidget.cpp index 807d3ed9c9..cee46407ca 100644 --- a/Userland/Applications/TextEditor/MainWidget.cpp +++ b/Userland/Applications/TextEditor/MainWidget.cpp @@ -254,7 +254,7 @@ MainWidget::MainWidget() } m_editor->set_text(StringView()); - set_path(LexicalPath()); + set_path({}); update_title(); }); @@ -287,7 +287,7 @@ MainWidget::MainWidget() // FIXME: It would be cool if this would propagate from GUI::TextDocument somehow. window()->set_modified(false); - set_path(LexicalPath(save_path.value())); + set_path(save_path.value()); dbgln("Wrote document to {}", save_path.value()); }); @@ -595,11 +595,18 @@ void MainWidget::initialize_menubar(GUI::Menubar& menubar) help_menu.add_action(GUI::CommonActions::make_about_action("Text Editor", GUI::Icon::default_icon("app-text-editor"), window())); } -void MainWidget::set_path(const LexicalPath& lexical_path) +void MainWidget::set_path(StringView const& path) { - m_path = lexical_path.string(); - m_name = lexical_path.title(); - m_extension = lexical_path.extension(); + if (path.is_empty()) { + m_path = {}; + m_name = {}; + m_extension = {}; + } else { + auto lexical_path = LexicalPath(path); + m_path = lexical_path.string(); + m_name = lexical_path.title(); + m_extension = lexical_path.extension(); + } if (m_extension == "c" || m_extension == "cc" || m_extension == "cxx" || m_extension == "cpp" || m_extension == "h") { m_cpp_highlight->activate(); @@ -660,7 +667,7 @@ bool MainWidget::open_file(const String& path) m_editor->set_text(file->read_all()); - set_path(LexicalPath(path)); + set_path(path); m_editor->set_focus(true); diff --git a/Userland/Applications/TextEditor/MainWidget.h b/Userland/Applications/TextEditor/MainWidget.h index bc26b57fe7..4cccac8181 100644 --- a/Userland/Applications/TextEditor/MainWidget.h +++ b/Userland/Applications/TextEditor/MainWidget.h @@ -42,7 +42,7 @@ public: private: MainWidget(); - void set_path(const LexicalPath& file); + void set_path(StringView const&); void update_preview(); void update_markdown_preview(); void update_html_preview(); diff --git a/Userland/DevTools/HackStudio/EditorWrapper.cpp b/Userland/DevTools/HackStudio/EditorWrapper.cpp index 1a5487eac5..71aebd2696 100644 --- a/Userland/DevTools/HackStudio/EditorWrapper.cpp +++ b/Userland/DevTools/HackStudio/EditorWrapper.cpp @@ -113,8 +113,7 @@ void EditorWrapper::update_diff() void EditorWrapper::set_project_root(LexicalPath const& project_root) { m_project_root = project_root; - - auto result = GitRepo::try_to_create(m_project_root); + auto result = GitRepo::try_to_create(*m_project_root); switch (result.type) { case GitRepo::CreateResult::Type::Success: m_git_repo = result.repo; diff --git a/Userland/DevTools/HackStudio/EditorWrapper.h b/Userland/DevTools/HackStudio/EditorWrapper.h index 27851485b8..2dd3353fc9 100644 --- a/Userland/DevTools/HackStudio/EditorWrapper.h +++ b/Userland/DevTools/HackStudio/EditorWrapper.h @@ -43,7 +43,7 @@ public: const String& filename() const { return m_filename; } bool document_dirty() const { return m_document_dirty; } - LexicalPath const& project_root() const { return m_project_root; } + Optional const& project_root() const { return m_project_root; } void set_project_root(LexicalPath const& project_root); GitRepo const* git_repo() const { return m_git_repo; } @@ -62,7 +62,7 @@ private: RefPtr m_editor; bool m_document_dirty { false }; - LexicalPath m_project_root; + Optional m_project_root; RefPtr m_git_repo; Vector m_hunks; }; diff --git a/Userland/Libraries/LibCore/FileWatcher.cpp b/Userland/Libraries/LibCore/FileWatcher.cpp index 85abe92803..a38313d76e 100644 --- a/Userland/Libraries/LibCore/FileWatcher.cpp +++ b/Userland/Libraries/LibCore/FileWatcher.cpp @@ -83,18 +83,19 @@ static Optional get_event_from_fd(int fd, HashMap FileWatcherBase::add_watch(String path, FileWatcherEvent::Type event_mask) { - LexicalPath lexical_path; - if (path.length() > 0 && path[0] == '/') { - lexical_path = LexicalPath { path }; - } else { - char* buf = getcwd(nullptr, 0); - lexical_path = LexicalPath::join(String(buf), path); - free(buf); - } + String canonical_path = canonicalize_path(move(path)); - auto const& canonical_path = lexical_path.string(); if (m_path_to_wd.find(canonical_path) != m_path_to_wd.end()) { dbgln_if(FILE_WATCHER_DEBUG, "add_watch: path '{}' is already being watched", canonical_path); return false; @@ -125,16 +126,8 @@ Result FileWatcherBase::add_watch(String path, FileWatcherEvent::T Result FileWatcherBase::remove_watch(String path) { - LexicalPath lexical_path; - if (path.length() > 0 && path[0] == '/') { - lexical_path = LexicalPath { path }; - } else { - char* buf = getcwd(nullptr, 0); - lexical_path = LexicalPath::join(String(buf), path); - free(buf); - } + String canonical_path = canonicalize_path(move(path)); - auto const& canonical_path = lexical_path.string(); auto it = m_path_to_wd.find(canonical_path); if (it == m_path_to_wd.end()) { dbgln_if(FILE_WATCHER_DEBUG, "remove_watch: path '{}' is not being watched", canonical_path); diff --git a/Userland/Libraries/LibGUI/FileSystemModel.cpp b/Userland/Libraries/LibGUI/FileSystemModel.cpp index fe5f31a4be..bc0c6f4156 100644 --- a/Userland/Libraries/LibGUI/FileSystemModel.cpp +++ b/Userland/Libraries/LibGUI/FileSystemModel.cpp @@ -189,14 +189,14 @@ ModelIndex FileSystemModel::index(String path, int column) const FileSystemModel::Node const* FileSystemModel::node_for_path(String const& path) const { - LexicalPath lexical_path; - if (path == m_root_path) { - lexical_path = LexicalPath { "/" }; - } else if (!m_root_path.is_empty() && path.starts_with(m_root_path)) { - lexical_path = LexicalPath { LexicalPath::relative_path(path, m_root_path) }; - } else { - lexical_path = LexicalPath { move(path) }; - } + String resolved_path; + if (path == m_root_path) + resolved_path = "/"; + else if (!m_root_path.is_empty() && path.starts_with(m_root_path)) + resolved_path = LexicalPath::relative_path(path, m_root_path); + else + resolved_path = path; + LexicalPath lexical_path(resolved_path); const Node* node = m_root->m_parent_of_root ? &m_root->children.first() : m_root; if (lexical_path.string() == "/")