This class serves as a natural language interface for LLM to interact with Language Server Protocol. It provides functionalities like opening files, accessing text, finding definitions and finding symbols related to words in specified documents. root_path: str - the
| 5 | from hyperagent.multilspy.lsp_protocol_handler.lsp_types import SymbolKind |
| 6 | |
| 7 | class LSPToolKit: |
| 8 | """ |
| 9 | This class serves as a natural language interface for LLM to interact with Language Server Protocol. |
| 10 | It provides functionalities like opening files, accessing text, finding definitions and finding symbols |
| 11 | related to words in specified documents. |
| 12 | |
| 13 | root_path: str - the root path of your codebase, |
| 14 | language: str, optional - the language of your code (default is 'python') |
| 15 | """ |
| 16 | def __init__(self, root_path, language="python"): |
| 17 | """ |
| 18 | Creating a language server with root path and language configuration provided by user |
| 19 | |
| 20 | MultilspyConfig: A class that helps to configure Language Server for multiple languages |
| 21 | MultilspyLogger: A class that helps to Log LSP operations and Debugging |
| 22 | """ |
| 23 | self.root_path = root_path |
| 24 | self.language = language |
| 25 | self.server = SyncLanguageServer.create(MultilspyConfig(code_language=language), MultilspyLogger(), root_path) |
| 26 | |
| 27 | def open_file(self, relative_path): |
| 28 | """ |
| 29 | Open a file using the file's relative path to the root path of code base |
| 30 | |
| 31 | Args: |
| 32 | relative_path: str - relative path of file to codebase root path. |
| 33 | |
| 34 | Returns: |
| 35 | the file text if successful, Else returns an error message string. |
| 36 | """ |
| 37 | with self.server.start_server(): |
| 38 | with self.server.open_file(relative_path): |
| 39 | result = self.server.get_open_file_text(relative_path) |
| 40 | |
| 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 |