pub struct WorkspaceRefactor {
pub _index: WorkspaceIndex,
}Expand description
Workspace-wide refactoring provider
Provides high-level refactoring operations that can operate across multiple files within a workspace. Uses a WorkspaceIndex to understand symbol relationships and dependencies between files.
§Examples
let index = WorkspaceIndex::new();
let refactor = WorkspaceRefactor::new(index);
// Rename a variable across all files
let result = refactor.rename_symbol("$old_name", "$new_name", Path::new("file.pl"), (0, 0));Fields§
§_index: WorkspaceIndexThe workspace index used for symbol lookup and cross-file analysis
Implementations§
Source§impl WorkspaceRefactor
impl WorkspaceRefactor
Sourcepub fn new(index: WorkspaceIndex) -> WorkspaceRefactor
pub fn new(index: WorkspaceIndex) -> WorkspaceRefactor
Sourcepub fn rename_symbol(
&self,
old_name: &str,
new_name: &str,
_file_path: &Path,
_position: (usize, usize),
) -> Result<RefactorResult, RefactorError>
pub fn rename_symbol( &self, old_name: &str, new_name: &str, _file_path: &Path, _position: (usize, usize), ) -> Result<RefactorResult, RefactorError>
Rename a symbol across all files in the workspace
Performs a comprehensive rename of a Perl symbol (variable, subroutine, or package) across all indexed files in the workspace. The operation preserves sigils for variables and handles both indexed symbol lookups and text-based fallback searches.
§Arguments
old_name- The current name of the symbol (e.g., “$variable”, “subroutine”)new_name- The new name for the symbol (e.g., “$new_variable”, “new_subroutine”)_file_path- The file path where the rename was initiated (currently unused)_position- The position in the file where the rename was initiated (currently unused)
§Returns
Ok(RefactorResult)- Contains all file edits needed to complete the renameErr(RefactorError)- If validation fails or symbol lookup encounters issues
§Errors
RefactorError::InvalidInput- If names are empty or identicalRefactorError::UriConversion- If file path/URI conversion fails
§Examples
let index = WorkspaceIndex::new();
let refactor = WorkspaceRefactor::new(index);
let result = refactor.rename_symbol("$old_var", "$new_var", Path::new("file.pl"), (0, 0))?;
println!("Rename will affect {} files", result.file_edits.len());Sourcepub fn extract_module(
&self,
file_path: &Path,
start_line: usize,
end_line: usize,
module_name: &str,
) -> Result<RefactorResult, RefactorError>
pub fn extract_module( &self, file_path: &Path, start_line: usize, end_line: usize, module_name: &str, ) -> Result<RefactorResult, RefactorError>
Extract selected code into a new module
Takes a range of lines from an existing file and moves them into a new
Perl module file, replacing the original code with a use statement.
This is useful for breaking up large files into smaller, more manageable modules.
§Arguments
file_path- The path to the file containing the code to extractstart_line- The first line to extract (1-based line number)end_line- The last line to extract (1-based line number, inclusive)module_name- The name of the new module to create (without .pm extension). Qualified names likeFoo::Barare mapped toFoo/Bar.pm.
§Returns
Ok(RefactorResult)- Contains edits for both the original file and new moduleErr(RefactorError)- If validation fails or file operations encounter issues
§Errors
RefactorError::InvalidInput- If module name is empty or start_line > end_lineRefactorError::DocumentNotIndexed- If the source file is not in the workspace indexRefactorError::InvalidPosition- If the line numbers are invalidRefactorError::UriConversion- If file path/URI conversion fails
§Examples
let index = WorkspaceIndex::new();
let refactor = WorkspaceRefactor::new(index);
let result = refactor.extract_module(
Path::new("large_file.pl"),
50, 100, // Extract lines 50-100
"ExtractedUtils"
)?;Sourcepub fn optimize_imports(&self) -> Result<RefactorResult, String>
pub fn optimize_imports(&self) -> Result<RefactorResult, String>
Optimize imports across the entire workspace
Uses the ImportOptimizer to analyze all files and optimize their import statements by:
- Detecting unused imports with smart bare import analysis
- Removing duplicate imports from the same module
- Sorting imports alphabetically
- Consolidating multiple imports from the same module
- Conservative handling of pragma modules and bare imports
§Returns
Ok(RefactorResult)- Contains all file edits to optimize importsErr(String)- If import analysis encounters issues
Sourcepub fn move_subroutine(
&self,
sub_name: &str,
from_file: &Path,
to_module: &str,
) -> Result<RefactorResult, RefactorError>
pub fn move_subroutine( &self, sub_name: &str, from_file: &Path, to_module: &str, ) -> Result<RefactorResult, RefactorError>
Move a subroutine from one file to another module
Extracts a subroutine definition from one file and moves it to another module file. The subroutine is completely removed from the source file and appended to the target module file. This operation does not update callers or add import statements.
§Arguments
sub_name- The name of the subroutine to move (without ‘sub’ keyword)from_file- The source file containing the subroutineto_module- The name of the target module (without .pm extension). Qualified names likeFoo::Barare mapped toFoo/Bar.pm.
§Returns
Ok(RefactorResult)- Contains edits for both source and target filesErr(RefactorError)- If validation fails or the subroutine cannot be found
§Errors
RefactorError::InvalidInput- If names are emptyRefactorError::DocumentNotIndexed- If the source file is not indexedRefactorError::SymbolNotFound- If the subroutine is not found in the source fileRefactorError::InvalidPosition- If the subroutine’s position is invalidRefactorError::UriConversion- If file path/URI conversion fails
§Examples
let index = WorkspaceIndex::new();
let refactor = WorkspaceRefactor::new(index);
let result = refactor.move_subroutine(
"utility_function",
Path::new("main.pl"),
"Utils"
)?;Sourcepub fn inline_variable(
&self,
var_name: &str,
file_path: &Path,
_position: (usize, usize),
) -> Result<RefactorResult, RefactorError>
pub fn inline_variable( &self, var_name: &str, file_path: &Path, _position: (usize, usize), ) -> Result<RefactorResult, RefactorError>
Inline a variable across its scope
Replaces all occurrences of a variable with its initializer expression and removes the variable declaration. This is useful for eliminating unnecessary intermediate variables that only serve to store simple expressions.
Note: This is a naive implementation that uses simple text matching. It may not handle all scoping rules correctly and should be used with caution.
§Arguments
var_name- The name of the variable to inline (including sigil, e.g., “$temp”)file_path- The file containing the variable to inline_position- The position in the file (currently unused)
§Returns
Ok(RefactorResult)- Contains the file edits to inline the variableErr(RefactorError)- If validation fails or the variable cannot be found
§Errors
RefactorError::InvalidInput- If the variable name is emptyRefactorError::DocumentNotIndexed- If the file is not indexedRefactorError::SymbolNotFound- If the variable definition is not foundRefactorError::ParseError- If the variable has no initializerRefactorError::UriConversion- If file path/URI conversion fails
§Examples
let index = WorkspaceIndex::new();
let refactor = WorkspaceRefactor::new(index);
// Inline a temporary variable like: my $temp = some_function(); print $temp;
let result = refactor.inline_variable("$temp", Path::new("file.pl"), (0, 0))?;Sourcepub fn inline_variable_all(
&self,
var_name: &str,
def_file_path: &Path,
_position: (usize, usize),
) -> Result<RefactorResult, RefactorError>
pub fn inline_variable_all( &self, var_name: &str, def_file_path: &Path, _position: (usize, usize), ) -> Result<RefactorResult, RefactorError>
Inline a variable across all files in the workspace
Replaces all occurrences of a variable with its initializer expression across all files in the workspace and removes the variable declaration.
§Arguments
var_name- The name of the variable to inline (including sigil)def_file_path- The file containing the variable definition_position- The position in the definition file
§Returns
Contains all file edits to inline the variable across workspace