AK+Everywhere: Remove "null state" of LexicalPath

This removes the default constructor of LexicalPath, and subsequently
modifies all its users to accommodate the change.
This commit is contained in:
Max Wipfli 2021-06-29 20:12:53 +02:00 committed by Andreas Kling
parent 4c018909f7
commit d8be530397
Notes: sideshowbarker 2024-07-18 11:13:24 +09:00
10 changed files with 52 additions and 52 deletions

View file

@ -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] == '/'; }

View file

@ -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);

View file

@ -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)
{
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()

View file

@ -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);

View file

@ -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)
{
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);

View file

@ -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();

View file

@ -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;

View file

@ -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;
};

View file

@ -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);

View file

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