Skip to main content

WorkspaceRefactor

Struct WorkspaceRefactor 

Source
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: WorkspaceIndex

The workspace index used for symbol lookup and cross-file analysis

Implementations§

Source§

impl WorkspaceRefactor

Source

pub fn new(index: WorkspaceIndex) -> WorkspaceRefactor

Create a new workspace refactoring provider

§Arguments
  • index - A WorkspaceIndex containing indexed symbols and documents
§Returns

A new WorkspaceRefactor instance ready to perform refactoring operations

Source

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 rename
  • Err(RefactorError) - If validation fails or symbol lookup encounters issues
§Errors
  • RefactorError::InvalidInput - If names are empty or identical
  • RefactorError::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());
Source

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 extract
  • start_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 like Foo::Bar are mapped to Foo/Bar.pm.
§Returns
  • Ok(RefactorResult) - Contains edits for both the original file and new module
  • Err(RefactorError) - If validation fails or file operations encounter issues
§Errors
  • RefactorError::InvalidInput - If module name is empty or start_line > end_line
  • RefactorError::DocumentNotIndexed - If the source file is not in the workspace index
  • RefactorError::InvalidPosition - If the line numbers are invalid
  • RefactorError::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"
)?;
Source

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 imports
  • Err(String) - If import analysis encounters issues
Source

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 subroutine
  • to_module - The name of the target module (without .pm extension). Qualified names like Foo::Bar are mapped to Foo/Bar.pm.
§Returns
  • Ok(RefactorResult) - Contains edits for both source and target files
  • Err(RefactorError) - If validation fails or the subroutine cannot be found
§Errors
  • RefactorError::InvalidInput - If names are empty
  • RefactorError::DocumentNotIndexed - If the source file is not indexed
  • RefactorError::SymbolNotFound - If the subroutine is not found in the source file
  • RefactorError::InvalidPosition - If the subroutine’s position is invalid
  • RefactorError::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"
)?;
Source

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 variable
  • Err(RefactorError) - If validation fails or the variable cannot be found
§Errors
  • RefactorError::InvalidInput - If the variable name is empty
  • RefactorError::DocumentNotIndexed - If the file is not indexed
  • RefactorError::SymbolNotFound - If the variable definition is not found
  • RefactorError::ParseError - If the variable has no initializer
  • RefactorError::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))?;
Source

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

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more