summaryrefslogtreecommitdiff
path: root/continuedev/src/continuedev/steps/find_and_replace.py
blob: 690872c03f80ba0ec07cfc9547c6905bd4b73112 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from ..models.filesystem_edit import FileEdit, Range
from ..core.main import Models, Step
from ..core.sdk import ContinueSDK


class FindAndReplaceStep(Step):
    name: str = "Find and replace"
    filepath: str
    pattern: str
    replacement: str

    async def describe(self, models: Models):
        return f"Replaced all instances of `{self.pattern}` with `{self.replacement}` in `{self.filepath}`"

    async def run(self, sdk: ContinueSDK):
        file_content = await sdk.ide.readFile(self.filepath)
        while self.pattern in file_content:
            start_index = file_content.index(self.pattern)
            end_index = start_index + len(self.pattern)
            await sdk.ide.applyFileSystemEdit(FileEdit(
                filepath=self.filepath,
                range=Range.from_indices(
                    file_content, start_index, end_index - 1),
                replacement=self.replacement
            ))
            file_content = file_content[:start_index] + \
                self.replacement + file_content[end_index:]
            await sdk.ide.saveFile(self.filepath)