Skip to main content
Glama
lin2000wl
by lin2000wl

replace_symbol_body

Update symbol definitions in code by replacing the body with a new one. Specify the symbol's name path, relative file path, and the new body content for precise edits in your codebase.

Instructions

Replaces the body of the symbol with the given name_path.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
bodyYesThe new symbol body. Important: Begin directly with the symbol definition and provide no leading indentation for the first line (but do indent the rest of the body according to the context).
name_pathYesFor finding the symbol to replace, same logic as in the `find_symbol` tool.
relative_pathYesThe relative path to the file containing the symbol.

Implementation Reference

  • Core logic for replacing the body of a symbol identified by name_path in a specific file. Finds the symbol using the language server, then delegates to replace_body_at_location. This is the execution handler for the replace_symbol_body tool.
    def replace_body(self, name_path: str, relative_file_path: str, body: str, *, use_same_indentation: bool = True) -> None: """ Replace the body of the symbol with the given name_path in the given file. :param name_path: the name path of the symbol to replace. :param relative_file_path: the relative path of the file in which the symbol is defined. :param body: the new body :param use_same_indentation: whether to use the same indentation as the original body. This means that the user doesn't have to provide the correct indentation, but can just write the body. """ symbol_candidates = self.find_by_name(name_path, within_relative_path=relative_file_path) if len(symbol_candidates) == 0: raise ValueError(f"No symbol with name {name_path} found in file {relative_file_path}") if len(symbol_candidates) > 1: raise ValueError( f"Found multiple {len(symbol_candidates)} symbols with name {name_path} in file {relative_file_path}. " "Will not replace the body of any of them, but you can use `replace_body_at_location`, the replace lines tool or other editing " "tools to perform your edits. Their locations are: \n " + json.dumps([s.location.to_dict() for s in symbol_candidates], indent=2) ) symbol = symbol_candidates[0] return self.replace_body_at_location(symbol.location, body, use_same_indentation=use_same_indentation)
  • Low-level handler that performs the actual deletion and insertion of the symbol body text using the language server. Adjusts indentation if requested.
    def replace_body_at_location(self, location: SymbolLocation, body: str, *, use_same_indentation: bool = True) -> None: """ Replace the body of the symbol at the given location with the given body :param location: the location of the symbol to replace. :param body: the new body :param use_same_indentation: whether to use the same indentation as the original body. This means that the user doesn't have to provide the correct indentation, but can just write the body. """ with self._edited_symbol_location(location) as symbol: assert location.relative_path is not None start_pos = symbol.body_start_position end_pos = symbol.body_end_position if start_pos is None or end_pos is None: raise ValueError(f"Symbol at {location} does not have a defined body range.") start_line, start_col = start_pos["line"], start_pos["character"] if use_same_indentation: indent = " " * start_col body_lines = body.splitlines() body = body_lines[0] + "\n" + "\n".join(indent + line for line in body_lines[1:]) # make sure the replacement adds no additional newlines (before or after) - all newlines # and whitespace before/after should remain the same, so we strip it entirely body = body.strip() self._lang_server.delete_text_between_positions(location.relative_path, start_pos, end_pos) self._lang_server.insert_text_at_position(location.relative_path, start_line, start_col, body) @staticmethod

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/lin2000wl/Serena-cursor-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server