continue-custom/server/continuedev/plugins/steps/find_and_replace.py
Nate Sesti 279593a848
Strong typing (#533)
* refactor: ♻️ get rid of continuedev.src.continuedev structure

* refactor: ♻️ switching back to server folder

* feat: ✨ make config.py imports shorter

* feat: 🔖 publish as pre-release vscode extension

* refactor: ♻️ refactor and add more completion params to ui

* build: 🏗️ download from preview S3
2023-10-04 23:47:59 -07:00

30 lines
1.1 KiB
Python

from ...core.main import Models, Step
from ...core.sdk import ContinueSDK
from ...models.filesystem_edit import FileEdit, Range
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)