summaryrefslogtreecommitdiff
path: root/DevTools
diff options
context:
space:
mode:
authorItamar <itamar8910@gmail.com>2020-09-20 20:55:36 +0300
committerAndreas Kling <kling@serenityos.org>2020-09-21 20:16:03 +0200
commit42bdcf182822befd96f8f9fad366f43333b9c05c (patch)
treecbbd21de037f2acd6dedfe078433707a1b05b88a /DevTools
parent253ab7536ac1174075afc038b261cc6d41dddf3f (diff)
downloadserenity-42bdcf182822befd96f8f9fad366f43333b9c05c.zip
HackStudio: Basic C++ autocomplete logic
CppAutoComplete gets a string of code and a position within it, and returns a Vector of auto-complete suggestions that are relevant for the given position. Currently, it's very naive - it uses our CppLexer to find identifiers in the code which the auto-complete target is a prefix of.
Diffstat (limited to 'DevTools')
-rw-r--r--DevTools/HackStudio/CMakeLists.txt1
-rw-r--r--DevTools/HackStudio/CppAutoComplete.cpp92
-rw-r--r--DevTools/HackStudio/CppAutoComplete.h46
3 files changed, 139 insertions, 0 deletions
diff --git a/DevTools/HackStudio/CMakeLists.txt b/DevTools/HackStudio/CMakeLists.txt
index 7b2b28b1f2..684de9fdf5 100644
--- a/DevTools/HackStudio/CMakeLists.txt
+++ b/DevTools/HackStudio/CMakeLists.txt
@@ -1,5 +1,6 @@
set(SOURCES
CodeDocument.cpp
+ CppAutoComplete.cpp
CursorTool.cpp
Debugger/BacktraceModel.cpp
Debugger/DebugInfoWidget.cpp
diff --git a/DevTools/HackStudio/CppAutoComplete.cpp b/DevTools/HackStudio/CppAutoComplete.cpp
new file mode 100644
index 0000000000..1a7762cee2
--- /dev/null
+++ b/DevTools/HackStudio/CppAutoComplete.cpp
@@ -0,0 +1,92 @@
+/*
+ * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "CppAutoComplete.h"
+#include <AK/HashTable.h>
+#include <LibGUI/CppLexer.h>
+
+// #define DEBUG_AUTOCOMPLETE
+
+namespace HackStudio {
+Vector<String> CppAutoComplete::get_suggestions(const String& code, GUI::TextPosition autocomplete_position)
+{
+ auto lines = code.split('\n', true);
+ GUI::CppLexer lexer(code);
+ auto tokens = lexer.lex();
+
+ auto index_of_target_token = token_in_position(tokens, autocomplete_position);
+ if (!index_of_target_token.has_value())
+ return {};
+
+ auto suggestions = identifier_prefixes(lines, tokens, index_of_target_token.value());
+
+#ifdef DEBUG_AUTOCOMPLETE
+ for (auto& suggestion : suggestions) {
+ dbg() << "suggestion: " << suggestion;
+ }
+#endif
+
+ return suggestions;
+}
+
+String CppAutoComplete::text_of_token(const Vector<String> lines, const GUI::CppToken& token)
+{
+ return lines[token.m_start.line].substring(token.m_start.column, token.m_end.column - token.m_start.column + 1);
+}
+
+Optional<size_t> CppAutoComplete::token_in_position(const Vector<GUI::CppToken>& tokens, GUI::TextPosition position)
+{
+ for (size_t token_index = 0; token_index < tokens.size(); ++token_index) {
+ auto& token = tokens[token_index];
+ if (token.m_start.line != position.line())
+ continue;
+ if (token.m_start.column > position.column() || token.m_end.column < position.column())
+ continue;
+ return token_index;
+ }
+ return {};
+}
+
+Vector<String> CppAutoComplete::identifier_prefixes(const Vector<String> lines, const Vector<GUI::CppToken>& tokens, size_t target_token_index)
+{
+ auto partial_input = text_of_token(lines, tokens[target_token_index]);
+ Vector<String> suggestions;
+
+ HashTable<String> suggestions_lookup; // To avoid duplicate results
+
+ for (size_t i = 0; i < target_token_index; ++i) {
+ auto& token = tokens[i];
+ if (token.m_type != GUI::CppToken::Type::Identifier)
+ continue;
+ auto text = text_of_token(lines, token);
+ if (text.starts_with(partial_input) && !suggestions_lookup.contains(text)) {
+ suggestions_lookup.set(text);
+ suggestions.append(text);
+ }
+ }
+ return suggestions;
+}
+};
diff --git a/DevTools/HackStudio/CppAutoComplete.h b/DevTools/HackStudio/CppAutoComplete.h
new file mode 100644
index 0000000000..9c4bbb3233
--- /dev/null
+++ b/DevTools/HackStudio/CppAutoComplete.h
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#include <AK/String.h>
+#include <AK/Vector.h>
+#include <LibGUI/CppLexer.h>
+#include <LibGUI/TextPosition.h>
+
+namespace HackStudio {
+class CppAutoComplete {
+public:
+ CppAutoComplete() = delete;
+
+ static Vector<String> get_suggestions(const String& code, GUI::TextPosition autocomplete_position);
+
+private:
+ static Optional<size_t> token_in_position(const Vector<GUI::CppToken>&, GUI::TextPosition);
+ static String text_of_token(const Vector<String> lines, const GUI::CppToken&);
+ static Vector<String> identifier_prefixes(const Vector<String> lines, const Vector<GUI::CppToken>&, size_t target_token_index);
+};
+};