MCPcopy Index your code
hub / github.com/FSoft-AI4Code/HyperAgent / get_references

Method get_references

src/hyperagent/llm_multilspy.py:142–203  ·  view source on GitHub ↗

This function is used to get references of a particular identifier in a codebase. It can also provide detailed output if verbose argument is set to true. Args: search_word (str): The identifier to be searched in the codebase. file_path (str): Path of

(
        self,
        search_word: str,
        file_path: str,
        line_number: int = None,
        offset_value: int = 0,
        verbose: bool = False,
        context_limit: int = 10
    )

Source from the content-addressed store, hash-verified

140 return file_symbols
141
142 def get_references(
143 self,
144 search_word: str,
145 file_path: str,
146 line_number: int = None,
147 offset_value: int = 0,
148 verbose: bool = False,
149 context_limit: int = 10
150 ) -> str:
151 """
152 This function is used to get references of a particular identifier in a codebase. It can also provide detailed
153 output if verbose argument is set to true.
154
155 Args:
156 search_word (str): The identifier to be searched in the codebase.
157 file_path (str): Path of the file in which to search the identifier.
158 line_number (int, optional): Line number to start the search from. Defaults to None.
159 offset_value (int, optional): The number of positions to ignore from the start of the line. Defaults to 0.
160 verbose (bool, optional): If set to True, detailed output will be returned. Defaults to False.
161 context_limit (int, optional): Defines the number of lines to print before and after the matched line in verbose mode. Defaults to 10.
162
163 Returns:
164 str: This function returns a string consisting of locations of the search identifier in the document.
165 In verbose mode, this string contains additional information at each location such as implementation code.
166 """
167 document_contents = self.open_file(file_path)
168
169 if line_number is None:
170 # Handle cases where the line number is either incorrect or not known
171 cursor_position = word_to_position(document_contents, search_word, line=None, offset=offset_value)
172 else:
173 try:
174 cursor_position = word_to_position(document_contents, search_word, line=line_number, offset=offset_value)
175 except:
176 cursor_position = word_to_position(document_contents, search_word, line=None, offset=offset_value)
177
178 if cursor_position is None:
179 return "The tool cannot find the word in the file"
180
181 with self.server.start_server():
182 references_output = self.server.request_references(file_path, **cursor_position)
183
184 if verbose:
185 detailed_output = []
186 for reference_item in references_output:
187 document_item = self.open_file(reference_item["relativePath"])
188 reference_item["range"]["start"]["line"] = max(0, reference_item["range"]["start"]["line"] - context_limit)
189 reference_item["range"]["end"]["line"] = min(len(document_item.splitlines(True)), reference_item["range"]["end"]["line"] + context_limit)
190 reference_item["range"]["start"]["character"] = 0
191 reference_item["range"]["end"]["character"] = len(document_item.splitlines(True)[reference_item["range"]["end"]["line"]-1])
192 reference_code = get_text(document_item, reference_item["range"])
193 formatted_results = []
194 for index, code_line in enumerate(reference_code.split("\n")):
195 code_line = str(index + reference_item["range"]["start"]["line"]) + " " + code_line
196 formatted_results.append(code_line)
197
198 reference_code = "\n".join(formatted_results)
199

Callers 1

_runMethod · 0.80

Calls 6

open_fileMethod · 0.95
word_to_positionFunction · 0.90
get_textFunction · 0.90
appendMethod · 0.80
start_serverMethod · 0.45
request_referencesMethod · 0.45

Tested by

no test coverage detected