Get the definition of a given identifier in a source file. Args: word: str - Word whose definition needs to be found relative_path: str - Relative path of the file in which the definition needs to be found. line: int, optional - Line number where
(self, word, relative_path, line=None, offset=0, verbose=False)
| 41 | return result |
| 42 | |
| 43 | def get_definition(self, word, relative_path, line=None, offset=0, verbose=False): |
| 44 | """ |
| 45 | Get the definition of a given identifier in a source file. |
| 46 | Args: |
| 47 | word: str - Word whose definition needs to be found |
| 48 | relative_path: str - Relative path of the file in which the definition needs to be found. |
| 49 | line: int, optional - Line number where the word is located. If not provided, the word's first occurrence from top will be considered |
| 50 | offset: int, optional - The number of characters to be ignored from the beginning of the line. |
| 51 | verbose: bool, optional - If true, gives detailed output showing all symbol information along with the definition |
| 52 | |
| 53 | Returns: |
| 54 | Returns the definition of word if found. If not found, returns an error message. |
| 55 | """ |
| 56 | doc = self.open_file(relative_path) |
| 57 | cursor_pos = word_to_position(doc, word, line=line, offset=offset) |
| 58 | # Verifying if the cursor position exists and then getting the definition. In case cursor position does not exist, then returns error message. |
| 59 | if cursor_pos is not None: |
| 60 | with self.server.start_server(): |
| 61 | output = self.server.request_definition(relative_path, **cursor_pos) |
| 62 | else: |
| 63 | return "The tool cannot find the word in the file" |
| 64 | |
| 65 | # If verbose setting is true, then gives detailed information for first symbol from output. In case the symbol doe not have a location attribute then shows the rang attribute. |
| 66 | if verbose and len(output) > 0: |
| 67 | symbols = self.get_symbols(output[0]["relativePath"], verbose=False) |
| 68 | symbol = matching_symbols(symbols, output[0]) |
| 69 | if symbol is None: |
| 70 | return "Please try again with semantic or code search tool" |
| 71 | symbol_type = matching_kind_symbol(symbol) |
| 72 | definition = "" |
| 73 | if "location" not in symbol: |
| 74 | if symbol["range"]["end"]["line"] - symbol["range"]["start"]["line"] > 100: |
| 75 | definition = "Too long to display, you should use open_file tool to view partial content step by step." |
| 76 | symbol["range"]["end"]["line"] = symbol["range"]["start"]["line"] + 100 |
| 77 | definition += add_num_line(get_text(self.open_file(output[0]["relativePath"]), symbol["range"]), symbol["range"]["start"]["line"]) |
| 78 | else: |
| 79 | if symbol["location"]["range"]["end"]["line"] - symbol["location"]["range"]["start"]["line"] > 100: |
| 80 | definition = "Too long to display, you should use open_file tool to view partial content step by step." |
| 81 | symbol["location"]["range"]["end"]["line"] = symbol["location"]["range"]["start"]["line"] + 100 |
| 82 | definition = add_num_line(get_text(self.open_file(output[0]["relativePath"]), symbol["location"]["range"]), symbol["location"]["range"]["start"]["line"]) |
| 83 | output = "Name: " + str(symbol["name"]) + "\n" + "Type: " + str(symbol_type) + "\n" + "Definition: " + definition |
| 84 | |
| 85 | return output |
| 86 | |
| 87 | def get_symbols(self, file_path: str, preview_size: int = 10, verbose: bool = True) -> list: |
| 88 | """ |
no test coverage detected