mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 14:47:35 +00:00
LibCodeComprehension: Re-organize code comprehension related code
This moves all code comprehension-related code to a new library, LibCodeComprehension. This also moves some types related to code comprehension tasks (such as autocomplete, find declaration) out of LibGUI and into LibCodeComprehension.
This commit is contained in:
parent
a2c34554cd
commit
b35293d945
65 changed files with 685 additions and 491 deletions
|
@ -19,7 +19,7 @@ namespace GUI {
|
|||
|
||||
class AutocompleteSuggestionModel final : public GUI::Model {
|
||||
public:
|
||||
explicit AutocompleteSuggestionModel(Vector<AutocompleteProvider::Entry>&& suggestions)
|
||||
explicit AutocompleteSuggestionModel(Vector<CodeComprehension::AutocompleteResultEntry>&& suggestions)
|
||||
: m_suggestions(move(suggestions))
|
||||
{
|
||||
}
|
||||
|
@ -50,13 +50,13 @@ public:
|
|||
return suggestion.completion;
|
||||
}
|
||||
if (index.column() == Column::Icon) {
|
||||
if (suggestion.language == GUI::AutocompleteProvider::Language::Cpp) {
|
||||
if (suggestion.language == CodeComprehension::Language::Cpp) {
|
||||
if (!s_cpp_identifier_icon) {
|
||||
s_cpp_identifier_icon = Gfx::Bitmap::try_load_from_file("/res/icons/16x16/completion/cpp-identifier.png").release_value_but_fixme_should_propagate_errors();
|
||||
}
|
||||
return *s_cpp_identifier_icon;
|
||||
}
|
||||
if (suggestion.language == GUI::AutocompleteProvider::Language::Unspecified) {
|
||||
if (suggestion.language == CodeComprehension::Language::Unspecified) {
|
||||
if (!s_unspecified_identifier_icon) {
|
||||
s_unspecified_identifier_icon = Gfx::Bitmap::try_load_from_file("/res/icons/16x16/completion/unspecified-identifier.png").release_value_but_fixme_should_propagate_errors();
|
||||
}
|
||||
|
@ -73,15 +73,15 @@ public:
|
|||
return suggestion.completion;
|
||||
|
||||
if ((int)role == InternalRole::HideAutocompleteAfterApplying)
|
||||
return suggestion.hide_autocomplete_after_applying == AutocompleteProvider::Entry::HideAutocompleteAfterApplying::Yes;
|
||||
return suggestion.hide_autocomplete_after_applying == CodeComprehension::AutocompleteResultEntry::HideAutocompleteAfterApplying::Yes;
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
void set_suggestions(Vector<AutocompleteProvider::Entry>&& suggestions) { m_suggestions = move(suggestions); }
|
||||
void set_suggestions(Vector<CodeComprehension::AutocompleteResultEntry>&& suggestions) { m_suggestions = move(suggestions); }
|
||||
|
||||
private:
|
||||
Vector<AutocompleteProvider::Entry> m_suggestions;
|
||||
Vector<CodeComprehension::AutocompleteResultEntry> m_suggestions;
|
||||
};
|
||||
|
||||
AutocompleteBox::AutocompleteBox(TextEditor& editor)
|
||||
|
@ -109,7 +109,7 @@ AutocompleteBox::AutocompleteBox(TextEditor& editor)
|
|||
m_no_suggestions_view = main_widget.add<GUI::Label>("No suggestions");
|
||||
}
|
||||
|
||||
void AutocompleteBox::update_suggestions(Vector<AutocompleteProvider::Entry>&& suggestions)
|
||||
void AutocompleteBox::update_suggestions(Vector<CodeComprehension::AutocompleteResultEntry>&& suggestions)
|
||||
{
|
||||
// FIXME: There's a potential race here if, after the user selected an autocomplete suggestion,
|
||||
// the LanguageServer sends an update and this function is executed before AutocompleteBox::apply_suggestion()
|
||||
|
@ -182,9 +182,9 @@ void AutocompleteBox::previous_suggestion()
|
|||
}
|
||||
}
|
||||
|
||||
AutocompleteProvider::Entry::HideAutocompleteAfterApplying AutocompleteBox::apply_suggestion()
|
||||
CodeComprehension::AutocompleteResultEntry::HideAutocompleteAfterApplying AutocompleteBox::apply_suggestion()
|
||||
{
|
||||
auto hide_when_done = AutocompleteProvider::Entry::HideAutocompleteAfterApplying::Yes;
|
||||
auto hide_when_done = CodeComprehension::AutocompleteResultEntry::HideAutocompleteAfterApplying::Yes;
|
||||
|
||||
if (m_editor.is_null())
|
||||
return hide_when_done;
|
||||
|
@ -202,7 +202,7 @@ AutocompleteProvider::Entry::HideAutocompleteAfterApplying AutocompleteBox::appl
|
|||
auto hide_after_applying = suggestion_index.data((GUI::ModelRole)AutocompleteSuggestionModel::InternalRole::HideAutocompleteAfterApplying).to_bool();
|
||||
|
||||
if (!hide_after_applying)
|
||||
hide_when_done = AutocompleteProvider::Entry::HideAutocompleteAfterApplying::No;
|
||||
hide_when_done = CodeComprehension::AutocompleteResultEntry::HideAutocompleteAfterApplying::No;
|
||||
|
||||
VERIFY(completion.length() >= partial_length);
|
||||
if (!m_editor->has_selection()) {
|
||||
|
@ -219,14 +219,4 @@ AutocompleteProvider::Entry::HideAutocompleteAfterApplying AutocompleteBox::appl
|
|||
return hide_when_done;
|
||||
}
|
||||
|
||||
bool AutocompleteProvider::Declaration::operator==(AutocompleteProvider::Declaration const& other) const
|
||||
{
|
||||
return name == other.name && position == other.position && type == other.type && scope == other.scope;
|
||||
}
|
||||
|
||||
bool AutocompleteProvider::ProjectLocation::operator==(ProjectLocation const& other) const
|
||||
{
|
||||
return file == other.file && line == other.line && column == other.column;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <LibCodeComprehension/Types.h>
|
||||
#include <LibGUI/Forward.h>
|
||||
#include <LibGUI/Label.h>
|
||||
#include <LibGUI/TableView.h>
|
||||
|
@ -22,98 +23,7 @@ class AutocompleteProvider {
|
|||
public:
|
||||
virtual ~AutocompleteProvider() = default;
|
||||
|
||||
enum class Language {
|
||||
Unspecified,
|
||||
Cpp,
|
||||
};
|
||||
|
||||
struct Entry {
|
||||
String completion;
|
||||
size_t partial_input_length { 0 };
|
||||
Language language { Language::Unspecified };
|
||||
String display_text {};
|
||||
|
||||
enum class HideAutocompleteAfterApplying {
|
||||
No,
|
||||
Yes,
|
||||
};
|
||||
HideAutocompleteAfterApplying hide_autocomplete_after_applying { HideAutocompleteAfterApplying::Yes };
|
||||
};
|
||||
|
||||
struct ProjectLocation {
|
||||
String file;
|
||||
size_t line { 0 };
|
||||
size_t column { 0 };
|
||||
|
||||
bool operator==(ProjectLocation const&) const;
|
||||
};
|
||||
|
||||
enum class DeclarationType {
|
||||
Function,
|
||||
Struct,
|
||||
Class,
|
||||
Variable,
|
||||
PreprocessorDefinition,
|
||||
Namespace,
|
||||
Member,
|
||||
};
|
||||
|
||||
struct Declaration {
|
||||
String name;
|
||||
ProjectLocation position;
|
||||
DeclarationType type;
|
||||
String scope;
|
||||
|
||||
bool operator==(Declaration const&) const;
|
||||
};
|
||||
|
||||
virtual void provide_completions(Function<void(Vector<Entry>)>) = 0;
|
||||
|
||||
#define FOR_EACH_SEMANTIC_TYPE \
|
||||
__SEMANTIC(Unknown) \
|
||||
__SEMANTIC(Regular) \
|
||||
__SEMANTIC(Keyword) \
|
||||
__SEMANTIC(Type) \
|
||||
__SEMANTIC(Identifier) \
|
||||
__SEMANTIC(String) \
|
||||
__SEMANTIC(Number) \
|
||||
__SEMANTIC(IncludePath) \
|
||||
__SEMANTIC(PreprocessorStatement) \
|
||||
__SEMANTIC(Comment) \
|
||||
__SEMANTIC(Whitespace) \
|
||||
__SEMANTIC(Function) \
|
||||
__SEMANTIC(Variable) \
|
||||
__SEMANTIC(CustomType) \
|
||||
__SEMANTIC(Namespace) \
|
||||
__SEMANTIC(Member) \
|
||||
__SEMANTIC(Parameter) \
|
||||
__SEMANTIC(PreprocessorMacro)
|
||||
|
||||
struct TokenInfo {
|
||||
|
||||
enum class SemanticType : u32 {
|
||||
#define __SEMANTIC(x) x,
|
||||
FOR_EACH_SEMANTIC_TYPE
|
||||
#undef __SEMANTIC
|
||||
|
||||
} type { SemanticType::Unknown };
|
||||
size_t start_line { 0 };
|
||||
size_t start_column { 0 };
|
||||
size_t end_line { 0 };
|
||||
size_t end_column { 0 };
|
||||
|
||||
static constexpr char const* type_to_string(SemanticType t)
|
||||
{
|
||||
switch (t) {
|
||||
#define __SEMANTIC(x) \
|
||||
case SemanticType::x: \
|
||||
return #x;
|
||||
FOR_EACH_SEMANTIC_TYPE
|
||||
#undef __SEMANTIC
|
||||
}
|
||||
VERIFY_NOT_REACHED();
|
||||
};
|
||||
};
|
||||
virtual void provide_completions(Function<void(Vector<CodeComprehension::AutocompleteResultEntry>)>) = 0;
|
||||
|
||||
void attach(TextEditor& editor)
|
||||
{
|
||||
|
@ -133,7 +43,7 @@ public:
|
|||
explicit AutocompleteBox(TextEditor&);
|
||||
~AutocompleteBox() = default;
|
||||
|
||||
void update_suggestions(Vector<AutocompleteProvider::Entry>&& suggestions);
|
||||
void update_suggestions(Vector<CodeComprehension::AutocompleteResultEntry>&& suggestions);
|
||||
bool is_visible() const;
|
||||
void show(Gfx::IntPoint suggestion_box_location);
|
||||
void close();
|
||||
|
@ -141,7 +51,7 @@ public:
|
|||
bool has_suggestions() { return m_suggestion_view->model()->row_count() > 0; }
|
||||
void next_suggestion();
|
||||
void previous_suggestion();
|
||||
AutocompleteProvider::Entry::HideAutocompleteAfterApplying apply_suggestion();
|
||||
CodeComprehension::AutocompleteResultEntry::HideAutocompleteAfterApplying apply_suggestion();
|
||||
|
||||
private:
|
||||
WeakPtr<TextEditor> m_editor;
|
||||
|
@ -149,5 +59,4 @@ private:
|
|||
RefPtr<GUI::TableView> m_suggestion_view;
|
||||
RefPtr<GUI::Label> m_no_suggestions_view;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
|
||||
namespace GUI::GML {
|
||||
|
||||
void AutocompleteProvider::provide_completions(Function<void(Vector<Entry>)> callback)
|
||||
void AutocompleteProvider::provide_completions(Function<void(Vector<CodeComprehension::AutocompleteResultEntry>)> callback)
|
||||
{
|
||||
auto cursor = m_editor->cursor();
|
||||
auto text = m_editor->text();
|
||||
|
@ -121,7 +121,7 @@ void AutocompleteProvider::provide_completions(Function<void(Vector<Entry>)> cal
|
|||
return fuzzy_str_builder.build();
|
||||
};
|
||||
|
||||
Vector<AutocompleteProvider::Entry> class_entries, identifier_entries;
|
||||
Vector<CodeComprehension::AutocompleteResultEntry> class_entries, identifier_entries;
|
||||
|
||||
auto register_layouts_matching_pattern = [&](String pattern, size_t partial_input_length) {
|
||||
Core::ObjectClassRegistration::for_each([&](const Core::ObjectClassRegistration& registration) {
|
||||
|
@ -146,15 +146,15 @@ void AutocompleteProvider::provide_completions(Function<void(Vector<Entry>)> cal
|
|||
if (auto instance = registration->construct()) {
|
||||
for (auto& it : instance->properties()) {
|
||||
if (!it.value->is_readonly() && it.key.matches(pattern))
|
||||
identifier_entries.empend(String::formatted("{}: ", it.key), partial_input_length, Language::Unspecified, it.key);
|
||||
identifier_entries.empend(String::formatted("{}: ", it.key), partial_input_length, CodeComprehension::Language::Unspecified, it.key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (can_have_declared_layout(class_names.last()) && "layout"sv.matches(pattern))
|
||||
identifier_entries.empend("layout: ", partial_input_length, Language::Unspecified, "layout", AutocompleteProvider::Entry::HideAutocompleteAfterApplying::No);
|
||||
identifier_entries.empend("layout: ", partial_input_length, CodeComprehension::Language::Unspecified, "layout", CodeComprehension::AutocompleteResultEntry::HideAutocompleteAfterApplying::No);
|
||||
if (class_names.last() == "GUI::ScrollableContainerWidget" && "content_widget"sv.matches(pattern))
|
||||
identifier_entries.empend("content_widget: ", partial_input_length, Language::Unspecified, "content_widget", AutocompleteProvider::Entry::HideAutocompleteAfterApplying::No);
|
||||
identifier_entries.empend("content_widget: ", partial_input_length, CodeComprehension::Language::Unspecified, "content_widget", CodeComprehension::AutocompleteResultEntry::HideAutocompleteAfterApplying::No);
|
||||
};
|
||||
|
||||
auto register_properties_and_widgets_matching_pattern = [&](String pattern, size_t partial_input_length) {
|
||||
|
@ -235,7 +235,7 @@ void AutocompleteProvider::provide_completions(Function<void(Vector<Entry>)> cal
|
|||
quick_sort(class_entries, [](auto& a, auto& b) { return a.completion < b.completion; });
|
||||
quick_sort(identifier_entries, [](auto& a, auto& b) { return a.completion < b.completion; });
|
||||
|
||||
Vector<GUI::AutocompleteProvider::Entry> entries;
|
||||
Vector<CodeComprehension::AutocompleteResultEntry> entries;
|
||||
entries.extend(move(identifier_entries));
|
||||
entries.extend(move(class_entries));
|
||||
|
||||
|
|
|
@ -22,7 +22,7 @@ private:
|
|||
return class_name.is_one_of("GUI::Widget", "GUI::Frame");
|
||||
}
|
||||
|
||||
virtual void provide_completions(Function<void(Vector<Entry>)> callback) override;
|
||||
virtual void provide_completions(Function<void(Vector<CodeComprehension::AutocompleteResultEntry>)> callback) override;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -757,7 +757,7 @@ void TextEditor::keydown_event(KeyEvent& event)
|
|||
{
|
||||
if (m_autocomplete_box && m_autocomplete_box->is_visible() && (event.key() == KeyCode::Key_Return || event.key() == KeyCode::Key_Tab)) {
|
||||
TemporaryChange change { m_should_keep_autocomplete_box, true };
|
||||
if (m_autocomplete_box->apply_suggestion() == AutocompleteProvider::Entry::HideAutocompleteAfterApplying::Yes)
|
||||
if (m_autocomplete_box->apply_suggestion() == CodeComprehension::AutocompleteResultEntry::HideAutocompleteAfterApplying::Yes)
|
||||
hide_autocomplete();
|
||||
else
|
||||
try_update_autocomplete();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue