Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Node/Remove Line Actions #24

Open
gerrymanoim opened this issue Jan 25, 2022 · 1 comment
Open

Remove Node/Remove Line Actions #24

gerrymanoim opened this issue Jan 25, 2022 · 1 comment
Labels

Comments

@gerrymanoim
Copy link
Contributor

Recently in a refactor I ended up creating actions to remove a matching node and the entire line it was on, which I think might be useful to others.

Would you accept these in a PR?

from refactor import Action

class RemoveNodeAction(Action):
    """A action that removes matched segments"""
    def apply(self, context: Context, source: str) -> str:
        """Refactor a source segment in the given string."""
        lines = split_lines(source)
        view = slice(self.node.lineno - 1, self.node.end_lineno)

        target_lines = lines[view]
        lines[view] = [
            target_lines[0][: self.node.col_offset] + target_lines[-1][self.node.end_col_offset :]
        ]

        return lines.join()

class RemoveLineAction(Action):
    """A action that removes matched segments"""
    def apply(self, context: Context, source: str) -> str:
        """Refactor a source segment in the given string."""
        lines = split_lines(source)
        view = slice(self.node.lineno - 1, self.node.end_lineno)

        del lines[view]

        return lines.join()
@isidentical
Copy link
Owner

I was actually thinking about this a while, but never needed it. It definitely fits as a use case, and would love to see it on the core! Shoot the PR 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants