Browse Source

AK+Everywhere: Remove "null state" of LexicalPath

This removes the default constructor of LexicalPath, and subsequently
modifies all its users to accommodate the change.
Max Wipfli 4 years ago
parent
commit
d8be530397

+ 0 - 1
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] == '/'; }

+ 0 - 5
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);

+ 14 - 7
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()

+ 1 - 1
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);
 

+ 14 - 7
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);
 

+ 1 - 1
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();

+ 1 - 2
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;

+ 2 - 2
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<LexicalPath> 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<Editor> m_editor;
     bool m_document_dirty { false };
 
-    LexicalPath m_project_root;
+    Optional<LexicalPath> m_project_root;
     RefPtr<GitRepo> m_git_repo;
     Vector<Diff::Hunk> m_hunks;
 };

+ 11 - 18
Userland/Libraries/LibCore/FileWatcher.cpp

@@ -83,18 +83,19 @@ static Optional<FileWatcherEvent> get_event_from_fd(int fd, HashMap<unsigned, St
     return result;
 }
 
+static String canonicalize_path(String path)
+{
+    if (!path.is_empty() && path[0] == '/')
+        return LexicalPath::canonicalized_path(move(path));
+    char* cwd = getcwd(nullptr, 0);
+    VERIFY(cwd);
+    return LexicalPath::join(cwd, move(path)).string();
+}
+
 Result<bool, String> 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<bool, String> FileWatcherBase::add_watch(String path, FileWatcherEvent::T
 
 Result<bool, String> 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);

+ 8 - 8
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() == "/")