mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 15:42:44 +00:00 
			
		
		
		
	HackStudio+LanguageServers/Cpp: Show scope of symbols in Locator
This commit is contained in:
		
							parent
							
								
									6054a418e5
								
							
						
					
					
						commit
						84e34d76d8
					
				
					 10 changed files with 52 additions and 13 deletions
				
			
		|  | @ -1,6 +1,8 @@ | |||
| #include "other.h" | ||||
| #include <stdio.h> | ||||
| 
 | ||||
| namespace MyNamespace { | ||||
| 
 | ||||
| int func() | ||||
| { | ||||
|     int x = 1; | ||||
|  | @ -12,3 +14,5 @@ int func() | |||
|     printf("x+y: %d\n", x + y); | ||||
|     return x + y; | ||||
| } | ||||
| 
 | ||||
| } | ||||
|  |  | |||
|  | @ -1,3 +1,6 @@ | |||
| 
 | ||||
| namespace MyNamespace { | ||||
| 
 | ||||
| int func(); | ||||
| 
 | ||||
| #define USE_VAR2 | ||||
|  | @ -12,3 +15,5 @@ struct StructInHeader { | |||
|     int var3; | ||||
| #endif | ||||
| }; | ||||
| 
 | ||||
| } | ||||
|  |  | |||
|  | @ -98,6 +98,7 @@ inline bool encode(Encoder& encoder, const GUI::AutocompleteProvider::Declaratio | |||
|     if (!encode(encoder, declaration.position)) | ||||
|         return false; | ||||
|     encoder << (u32)declaration.type; | ||||
|     encoder << declaration.scope; | ||||
|     return true; | ||||
| } | ||||
| 
 | ||||
|  | @ -109,10 +110,14 @@ inline bool decode(Decoder& decoder, GUI::AutocompleteProvider::Declaration& dec | |||
| 
 | ||||
|     if (!decode(decoder, declaration.position)) | ||||
|         return false; | ||||
| 
 | ||||
|     u32 type; | ||||
|     if (!decoder.decode(type)) | ||||
|         return false; | ||||
| 
 | ||||
|     if (!decoder.decode(declaration.scope)) | ||||
|         return false; | ||||
| 
 | ||||
|     declaration.type = static_cast<GUI::AutocompleteProvider::DeclarationType>(type); | ||||
|     return true; | ||||
| } | ||||
|  |  | |||
|  | @ -294,8 +294,7 @@ NonnullRefPtrVector<Declaration> ParserAutoComplete::get_global_declarations(con | |||
| 
 | ||||
|     for (auto& decl : node.declarations()) { | ||||
|         declarations.append(decl); | ||||
|         if(decl.is_namespace()) | ||||
|         { | ||||
|         if (decl.is_namespace()) { | ||||
|             declarations.append(get_global_declarations(decl)); | ||||
|         } | ||||
|     } | ||||
|  | @ -410,13 +409,12 @@ void ParserAutoComplete::update_declared_symbols(const DocumentData& document) | |||
| { | ||||
|     Vector<GUI::AutocompleteProvider::Declaration> declarations; | ||||
| 
 | ||||
|     for(auto& decl : get_global_declarations(*document.parser().root_node())) | ||||
|     { | ||||
|         declarations.append({ decl.name(), { document.filename(), decl.start().line, decl.start().column }, type_of_declaration(decl) }); | ||||
|     for (auto& decl : get_global_declarations(*document.parser().root_node())) { | ||||
|         declarations.append({ decl.name(), { document.filename(), decl.start().line, decl.start().column }, type_of_declaration(decl), scope_of_declaration(decl) }); | ||||
|     } | ||||
| 
 | ||||
|     for (auto& definition : document.preprocessor().definitions()) { | ||||
|         declarations.append({ definition.key, { document.filename(), definition.value.line, definition.value.column }, GUI::AutocompleteProvider::DeclarationType::PreprocessorDefinition }); | ||||
|         declarations.append({ definition.key, { document.filename(), definition.value.line, definition.value.column }, GUI::AutocompleteProvider::DeclarationType::PreprocessorDefinition, {} }); | ||||
|     } | ||||
| 
 | ||||
|     set_declarations_of_document(document.filename(), move(declarations)); | ||||
|  | @ -460,4 +458,27 @@ OwnPtr<ParserAutoComplete::DocumentData> ParserAutoComplete::create_document_dat | |||
|     return document_data; | ||||
| } | ||||
| 
 | ||||
| String ParserAutoComplete::scope_of_declaration(const Declaration& decl) | ||||
| { | ||||
| 
 | ||||
|     auto parent = decl.parent(); | ||||
|     if (!parent) | ||||
|         return {}; | ||||
| 
 | ||||
|     if (!parent->is_declaration()) | ||||
|         return {}; | ||||
| 
 | ||||
|     auto& parent_decl = static_cast<Declaration&>(*parent); | ||||
| 
 | ||||
|     if (parent_decl.is_namespace()) { | ||||
|         auto& containing_namespace = static_cast<NamespaceDeclaration&>(parent_decl); | ||||
|         auto scope_of_parent = scope_of_declaration(parent_decl); | ||||
|         if (scope_of_parent.is_null()) | ||||
|             return containing_namespace.m_name; | ||||
|         return String::formatted("{}::{}", scope_of_parent, containing_namespace.m_name); | ||||
|     } | ||||
| 
 | ||||
|     return {}; | ||||
| } | ||||
| 
 | ||||
| } | ||||
|  |  | |||
|  | @ -107,6 +107,7 @@ private: | |||
|     String document_path_from_include_path(const StringView& include_path) const; | ||||
|     void update_declared_symbols(const DocumentData&); | ||||
|     GUI::AutocompleteProvider::DeclarationType type_of_declaration(const Declaration&); | ||||
|     String scope_of_declaration(const Declaration&); | ||||
|     Optional<GUI::AutocompleteProvider::ProjectLocation> find_preprocessor_definition(const DocumentData&, const GUI::TextPosition&); | ||||
| 
 | ||||
|     OwnPtr<DocumentData> create_document_data(String&& text, const String& filename); | ||||
|  |  | |||
|  | @ -233,7 +233,7 @@ void AutoComplete::update_declared_symbols(const DocumentData& document) | |||
| 
 | ||||
|                 if (!name.is_empty()) { | ||||
|                     dbgln("Found variable {}", name); | ||||
|                     declarations.append({ move(name), { filename, entry.name->position().start_line.line_number, entry.name->position().start_line.line_column }, GUI::AutocompleteProvider::DeclarationType::Variable }); | ||||
|                     declarations.append({ move(name), { filename, entry.name->position().start_line.line_number, entry.name->position().start_line.line_column }, GUI::AutocompleteProvider::DeclarationType::Variable, {} }); | ||||
|                 } | ||||
|             } | ||||
|             ::Shell::AST::NodeVisitor::visit(node); | ||||
|  | @ -242,7 +242,7 @@ void AutoComplete::update_declared_symbols(const DocumentData& document) | |||
|         void visit(const ::Shell::AST::FunctionDeclaration* node) override | ||||
|         { | ||||
|             dbgln("Found function {}", node->name().name); | ||||
|             declarations.append({ node->name().name, { filename, node->position().start_line.line_number, node->position().start_line.line_column }, GUI::AutocompleteProvider::DeclarationType::Function }); | ||||
|             declarations.append({ node->name().name, { filename, node->position().start_line.line_number, node->position().start_line.line_column }, GUI::AutocompleteProvider::DeclarationType::Function, {} }); | ||||
|         } | ||||
| 
 | ||||
|         const String& filename; | ||||
|  |  | |||
|  | @ -77,8 +77,11 @@ public: | |||
|                 return GUI::FileIconProvider::icon_for_path(suggestion.as_filename.value()); | ||||
|         } | ||||
|         if (suggestion.is_symbol_declaration()) { | ||||
|             if (index.column() == Column::Name) | ||||
|             if (index.column() == Column::Name) { | ||||
|                 if (suggestion.as_symbol_declaration.value().scope.is_null()) | ||||
|                     return suggestion.as_symbol_declaration.value().name; | ||||
|                 return String::formatted("{}::{}", suggestion.as_symbol_declaration.value().scope, suggestion.as_symbol_declaration.value().name); | ||||
|             } | ||||
|             if (index.column() == Column::Filename) | ||||
|                 return suggestion.as_symbol_declaration.value().position.file; | ||||
|             if (index.column() == Column::Icon) { | ||||
|  | @ -225,7 +228,7 @@ void Locator::update_suggestions() | |||
| 
 | ||||
|     for (auto& item : m_document_to_declarations) { | ||||
|         for (auto& decl : item.value) { | ||||
|             if (decl.name.contains(typed_text, CaseSensitivity::CaseInsensitive)) | ||||
|             if (decl.name.contains(typed_text, CaseSensitivity::CaseInsensitive) || decl.scope.contains(typed_text, CaseSensitivity::CaseInsensitive)) | ||||
|                 suggestions.append((LocatorSuggestionModel::Suggestion::create_symbol_declaration(decl))); | ||||
|         } | ||||
|     } | ||||
|  |  | |||
|  | @ -75,6 +75,7 @@ public: | |||
|     virtual bool is_variable_or_parameter_declaration() const { return false; } | ||||
|     virtual bool is_function_call() const { return false; } | ||||
|     virtual bool is_type() const { return false; } | ||||
|     virtual bool is_declaration() const { return false; } | ||||
| 
 | ||||
| protected: | ||||
|     ASTNode(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename) | ||||
|  | @ -113,7 +114,6 @@ public: | |||
|     virtual ~Statement() override = default; | ||||
|     virtual const char* class_name() const override { return "Statement"; } | ||||
| 
 | ||||
|     virtual bool is_declaration() const { return false; } | ||||
|     virtual NonnullRefPtrVector<Declaration> declarations() const override; | ||||
| 
 | ||||
| protected: | ||||
|  |  | |||
|  | @ -121,7 +121,6 @@ private: | |||
|     NonnullRefPtrVector<Declaration> parse_declarations_in_translation_unit(ASTNode& parent); | ||||
|     RefPtr<Declaration> parse_single_declaration_in_translation_unit(ASTNode& parent); | ||||
| 
 | ||||
| 
 | ||||
|     bool match(Token::Type); | ||||
|     Token consume(Token::Type); | ||||
|     Token consume(); | ||||
|  |  | |||
|  | @ -75,6 +75,7 @@ public: | |||
|         String name; | ||||
|         ProjectLocation position; | ||||
|         DeclarationType type; | ||||
|         String scope; | ||||
|     }; | ||||
| 
 | ||||
|     virtual void provide_completions(Function<void(Vector<Entry>)>) = 0; | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Itamar
						Itamar