Преглед изворни кода

HackStudio: Add statusbar with file and selected text information

ry-sev пре 4 година
родитељ
комит
2634cab7a8

+ 1 - 0
Userland/DevTools/HackStudio/CodeDocument.cpp

@@ -23,6 +23,7 @@ CodeDocument::CodeDocument(const String& file_path, Client* client)
     , m_file_path(file_path)
 {
     m_language = language_from_file_extension(LexicalPath::extension(file_path));
+    m_language_name = language_name_from_file_extension(LexicalPath::extension(file_path));
 }
 
 CodeDocument::CodeDocument(Client* client)

+ 2 - 0
Userland/DevTools/HackStudio/CodeDocument.h

@@ -24,6 +24,7 @@ public:
     void set_execution_position(size_t line) { m_execution_position = line; }
     void clear_execution_position() { m_execution_position.clear(); }
     const String& file_path() const { return m_file_path; }
+    const String& language_name() const { return m_language_name; };
     Language language() const { return m_language; }
 
     virtual bool is_code_document() const override final { return true; }
@@ -33,6 +34,7 @@ private:
     explicit CodeDocument(Client* client = nullptr);
 
     String m_file_path;
+    String m_language_name;
     Language m_language { Language::Unknown };
     Vector<size_t> m_breakpoint_lines;
     Optional<size_t> m_execution_position;

+ 0 - 8
Userland/DevTools/HackStudio/EditorWrapper.cpp

@@ -31,18 +31,10 @@ EditorWrapper::EditorWrapper()
     m_filename_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
     m_filename_label->set_fixed_height(14);
 
-    m_cursor_label = label_wrapper.add<GUI::Label>("(Cursor)");
-    m_cursor_label->set_text_alignment(Gfx::TextAlignment::CenterRight);
-    m_cursor_label->set_fixed_height(14);
-
     m_editor = add<Editor>();
     m_editor->set_ruler_visible(true);
     m_editor->set_automatic_indentation_enabled(true);
 
-    m_editor->on_cursor_change = [this] {
-        m_cursor_label->set_text(String::formatted("Line: {}, Column: {}", m_editor->cursor().line() + 1, m_editor->cursor().column()));
-    };
-
     m_editor->on_focus = [this] {
         set_current_editor_wrapper(this);
     };

+ 0 - 1
Userland/DevTools/HackStudio/EditorWrapper.h

@@ -59,7 +59,6 @@ private:
 
     String m_filename;
     RefPtr<GUI::Label> m_filename_label;
-    RefPtr<GUI::Label> m_cursor_label;
     RefPtr<Editor> m_editor;
     bool m_document_dirty { false };
 

+ 21 - 0
Userland/DevTools/HackStudio/HackStudioWidget.cpp

@@ -50,6 +50,7 @@
 #include <LibGUI/RegularEditingEngine.h>
 #include <LibGUI/Splitter.h>
 #include <LibGUI/StackWidget.h>
+#include <LibGUI/Statusbar.h>
 #include <LibGUI/TabWidget.h>
 #include <LibGUI/TableView.h>
 #include <LibGUI/TextBox.h>
@@ -135,6 +136,8 @@ HackStudioWidget::HackStudioWidget(const String& path_to_project)
 
     create_toolbar(toolbar_container);
 
+    m_statusbar = add<GUI::Statusbar>(3);
+
     auto maybe_watcher = Core::FileWatcher::create();
     if (maybe_watcher.is_error()) {
         warnln("Couldn't create a file watcher, deleted files won't be noticed! Error: {}", maybe_watcher.error());
@@ -280,6 +283,8 @@ bool HackStudioWidget::open_file(const String& full_filename)
     current_editor_wrapper().set_filename(filename);
 
     current_editor().set_focus(true);
+
+    current_editor().on_cursor_change = [this] { update_statusbar(); };
     return true;
 }
 
@@ -496,6 +501,7 @@ void HackStudioWidget::add_new_editor(GUI::Widget& parent)
     m_all_editor_wrappers.append(wrapper);
     wrapper->editor().set_focus(true);
     wrapper->set_project_root(LexicalPath(m_project->root_path()));
+    wrapper->editor().on_cursor_change = [this] { update_statusbar(); };
 }
 
 NonnullRefPtr<GUI::Action> HackStudioWidget::create_switch_to_next_editor_action()
@@ -1038,6 +1044,21 @@ void HackStudioWidget::initialize_menubar(GUI::Menubar& menubar)
     create_help_menubar(menubar);
 }
 
+void HackStudioWidget::update_statusbar()
+{
+    m_statusbar->set_text(0, String::formatted("Ln {}, Col {}", current_editor().cursor().line() + 1, current_editor().cursor().column()));
+
+    StringBuilder builder;
+    if (current_editor().has_selection()) {
+        String selected_text = current_editor().selected_text();
+        auto word_count = current_editor().number_of_selected_words();
+        builder.appendff("Selected: {} {} ({} {})", selected_text.length(), selected_text.length() == 1 ? "character" : "characters", word_count, word_count != 1 ? "words" : "word");
+    }
+
+    m_statusbar->set_text(1, builder.to_string());
+    m_statusbar->set_text(2, current_editor_wrapper().editor().code_document().language_name());
+}
+
 void HackStudioWidget::handle_external_file_deletion(const String& filepath)
 {
     m_open_files.remove(filepath);

+ 2 - 0
Userland/DevTools/HackStudio/HackStudioWidget.h

@@ -97,6 +97,7 @@ private:
     void on_action_tab_change();
     void reveal_action_tab(GUI::Widget&);
     void initialize_debugger();
+    void update_statusbar();
 
     void handle_external_file_deletion(const String& filepath);
 
@@ -136,6 +137,7 @@ private:
     RefPtr<GitWidget> m_git_widget;
     RefPtr<ClassViewWidget> m_class_view;
     RefPtr<GUI::Menu> m_project_tree_view_context_menu;
+    RefPtr<GUI::Statusbar> m_statusbar;
     RefPtr<GUI::TabWidget> m_action_tab_widget;
     RefPtr<GUI::TabWidget> m_project_tab;
     RefPtr<TerminalWrapper> m_terminal_wrapper;

+ 23 - 0
Userland/DevTools/HackStudio/Language.cpp

@@ -37,4 +37,27 @@ Language language_from_name(const String& name)
     return Language::Unknown;
 }
 
+String language_name_from_file_extension(const String& extension)
+{
+    VERIFY(!extension.starts_with("."));
+    if (extension == "cpp" || extension == "h")
+        return "C++";
+    else if (extension == "js")
+        return "JavaScript";
+    else if (extension == "gml")
+        return "GML";
+    else if (extension == "ini")
+        return "Ini";
+    else if (extension == "sh")
+        return "Shell";
+    else if (extension == "md")
+        return "Markdown";
+    else if (extension == "html")
+        return "HTML";
+    else if (extension == "txt")
+        return "Plaintext";
+
+    return "Unknown";
+}
+
 }

+ 1 - 0
Userland/DevTools/HackStudio/Language.h

@@ -20,5 +20,6 @@ enum class Language {
 
 Language language_from_file_extension(const String&);
 Language language_from_name(const String&);
+String language_name_from_file_extension(const String&);
 
 }