From cbff50b710c215164b5c9167af48e92d3fe76cc9 Mon Sep 17 00:00:00 2001 From: Dominik Jain Date: Fri, 13 Feb 2026 23:01:43 +0100 Subject: [PATCH] Apply SymbolDictGrouper in FindReferencingSymbolsTool & JetBrainsFindReferencingSymbolsTool Resolves #1021 --- src/serena/symbol.py | 2 +- src/serena/tools/jetbrains_tools.py | 8 ++++++-- src/serena/tools/symbol_tools.py | 11 +++++++++-- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/serena/symbol.py b/src/serena/symbol.py index e1050b46..6223b9d2 100644 --- a/src/serena/symbol.py +++ b/src/serena/symbol.py @@ -405,7 +405,7 @@ class LanguageServerSymbol(Symbol, ToStringMixin): kind: NotRequired[str] children: NotRequired[list["LanguageServerSymbol.OutputDict"]] - OutputDictKey = Literal["name", "name_path", "location", "body_location", "body", "kind", "children"] + OutputDictKey = Literal["name", "name_path", "relative_path", "location", "body_location", "body", "kind", "children"] def to_dict( self, diff --git a/src/serena/tools/jetbrains_tools.py b/src/serena/tools/jetbrains_tools.py index 736fa435..afd8d579 100644 --- a/src/serena/tools/jetbrains_tools.py +++ b/src/serena/tools/jetbrains_tools.py @@ -91,6 +91,8 @@ class JetBrainsFindReferencingSymbolsTool(Tool, ToolMarkerSymbolicRead, ToolMark Finds symbols that reference the given symbol using the JetBrains backend """ + symbol_dict_grouper = JetBrainsSymbolDictGrouper(["relative_path", "type"], ["type"], collapse_singleton=True) + # TODO: (maybe) - add content snippets showing the references like in LS based version? def apply( self, @@ -118,8 +120,10 @@ class JetBrainsFindReferencingSymbolsTool(Tool, ToolMarkerSymbolicRead, ToolMark relative_path=relative_path, include_quick_info=include_info, ) - result = self._to_json(response_dict) - return self._limit_length(result, max_answer_chars) + symbol_dicts = response_dict["symbols"] + result = self.symbol_dict_grouper.group(symbol_dicts) + result_json = self._to_json(result) + return self._limit_length(result_json, max_answer_chars) class JetBrainsGetSymbolsOverviewTool(Tool, ToolMarkerSymbolicRead, ToolMarkerOptional): diff --git a/src/serena/tools/symbol_tools.py b/src/serena/tools/symbol_tools.py index a22c1eef..650a00e5 100644 --- a/src/serena/tools/symbol_tools.py +++ b/src/serena/tools/symbol_tools.py @@ -176,6 +176,8 @@ class FindReferencingSymbolsTool(Tool, ToolMarkerSymbolicRead): Finds symbols that reference the given symbol using the language server backend """ + symbol_dict_grouper = LanguageServerSymbolDictGrouper(["relative_path", "kind"], ["kind"], collapse_singleton=True) + # noinspection PyDefaultArgument def apply( self, @@ -204,6 +206,7 @@ class FindReferencingSymbolsTool(Tool, ToolMarkerSymbolicRead): parsed_include_kinds: Sequence[SymbolKind] | None = [SymbolKind(k) for k in include_kinds] if include_kinds else None parsed_exclude_kinds: Sequence[SymbolKind] | None = [SymbolKind(k) for k in exclude_kinds] if exclude_kinds else None symbol_retriever = self.create_language_server_symbol_retriever() + references_in_symbols = symbol_retriever.find_referencing_symbols( name_path, relative_file_path=relative_path, @@ -211,6 +214,7 @@ class FindReferencingSymbolsTool(Tool, ToolMarkerSymbolicRead): include_kinds=parsed_include_kinds, exclude_kinds=parsed_exclude_kinds, ) + reference_dicts = [] for ref in references_in_symbols: ref_dict_orig = ref.symbol.to_dict( @@ -227,8 +231,11 @@ class FindReferencingSymbolsTool(Tool, ToolMarkerSymbolicRead): ) ref_dict["content_around_reference"] = content_around_ref.to_display_string() reference_dicts.append(ref_dict) - result = self._to_json(reference_dicts) - return self._limit_length(result, max_answer_chars) + + result = self.symbol_dict_grouper.group(reference_dicts) # type: ignore + + result_json = self._to_json(result) + return self._limit_length(result_json, max_answer_chars) class ReplaceSymbolBodyTool(Tool, ToolMarkerSymbolicEdit):