Kaynağa Gözat

LibGUI+HackStudio: Add TokenInfo struct for language-server IPC

The TokenInfo struct contains the token's position and a
"semantic type". The semantic type is a more fine-grained token type
than what's in Cpp::Token::Type.
For example, the semantic token type differentiates between a reference
to a variable and to a function parameter. In the normal Token::Type,
both would be Token::Type::Identifier.
Itamar 3 yıl önce
ebeveyn
işleme
a54d0cc805

+ 27 - 0
Userland/DevTools/HackStudio/AutoCompleteResponse.h

@@ -96,4 +96,31 @@ inline ErrorOr<void> decode(Decoder& decoder, Cpp::Parser::TodoEntry& entry)
     return {};
 }
 
+template<>
+inline bool encode(Encoder& encoder, const GUI::AutocompleteProvider::TokenInfo& location)
+{
+    encoder << (u32)location.type;
+    static_assert(sizeof(location.type) == sizeof(u32));
+    encoder << location.start_line;
+    encoder << location.start_column;
+    encoder << location.end_line;
+    encoder << location.end_column;
+    return true;
+}
+
+template<>
+inline ErrorOr<void> decode(Decoder& decoder, GUI::AutocompleteProvider::TokenInfo& entry)
+{
+    u32 semantic_type { 0 };
+    static_assert(sizeof(semantic_type) == sizeof(entry.type));
+
+    TRY(decoder.decode(semantic_type));
+    entry.type = static_cast<GUI::AutocompleteProvider::TokenInfo::SemanticType>(semantic_type);
+    TRY(decoder.decode(entry.start_line));
+    TRY(decoder.decode(entry.start_column));
+    TRY(decoder.decode(entry.end_line));
+    TRY(decoder.decode(entry.end_column));
+    return {};
+}
+
 }

+ 26 - 0
Userland/Libraries/LibGUI/AutocompleteProvider.h

@@ -69,6 +69,32 @@ public:
 
     virtual void provide_completions(Function<void(Vector<Entry>)>) = 0;
 
+    struct TokenInfo {
+        enum class SemanticType : u32 {
+            Unknown,
+            Regular,
+            Keyword,
+            Type,
+            Identifier,
+            String,
+            Number,
+            IncludePath,
+            PreprocessorStatement,
+            Comment,
+            Whitespace,
+            Function,
+            Variable,
+            CustomType,
+            Namespace,
+            Member,
+            Parameter,
+        } type { SemanticType::Unknown };
+        size_t start_line { 0 };
+        size_t start_column { 0 };
+        size_t end_line { 0 };
+        size_t end_column { 0 };
+    };
+
     void attach(TextEditor& editor)
     {
         VERIFY(!m_editor);