ctrl+shift+p filters: :st2 :st3 :win :osx :linux
Browse

Copy Paste Killer

by kyamaguchi ST3

Sublime text 3 plugin to help to refactor duplicated code

Details

Installs

  • Total 222
  • Win 136
  • Mac 67
  • Linux 19
Oct 7 Oct 6 Oct 5 Oct 4 Oct 3 Oct 2 Oct 1 Sep 30 Sep 29 Sep 28 Sep 27 Sep 26 Sep 25 Sep 24 Sep 23 Sep 22 Sep 21 Sep 20 Sep 19 Sep 18 Sep 17 Sep 16 Sep 15 Sep 14 Sep 13 Sep 12 Sep 11 Sep 10 Sep 9 Sep 8 Sep 7 Sep 6 Sep 5 Sep 4 Sep 3 Sep 2 Sep 1 Aug 31 Aug 30 Aug 29 Aug 28 Aug 27 Aug 26 Aug 25 Aug 24
Windows 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Mac 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Linux 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Readme

Source
raw.​githubusercontent.​com

CopyPasteKiller Plugin

Sublime text 3 plugin to help to refactor duplicated code.

This command sets snippets for “Find and Replace” with regular expression.
You can find any code blocks without caring about indentation and some differences like variable names.
You can replace those matches with refactored code by changing the given 'replace_string' and hitting “Replace”.

0x_git_diff

Usage:

You can declare shortcut like:

{ "keys": ["ctrl+shift+i"], "command": "copy_paste_killer" }

Guide

Select lines

01_select_lines

Run “copy_paste_killer” command

The snippets for “Find” and “Replace” are set

You could configure whether to open panels or not with settings keep_panels_open

02_run_copy_paste_killer

Change the snippet for “Find”

Find parts which have variations(e.g. variable name) in the snippet for “Find” and select them

03_edit_find_string

Replace variables with Regex

Replace variable name parts with Regex in the snippet for “Find”
You can name regex groups as you want

The typical format to name regex group is (?<name>.*)

Copy the snippet into “Find:”
“Select All ⌘A” -> “Use Selection for Find ⌘E”

04_change_variable_to_regex

Hit “Find” and confirm matches

You can find duplicated codes even if they have different indents and different variable names

05_confirm_find

Change the snippet for “Replace”

Change the snippet for “Replace” using regex groups named in “Find”
The format is $+{name}

Copy the snippet into “Replace:”
“Select All ⌘A” -> “Use Selection for Replace ⇧⌘E”

06_edit_replace_string

Hit “Replace” and confirm changes

Hit “Replace” and confirm if changes are expected

07_replace_matches

Check changes

Check changes with git diff

Reset(Undo) changes with git checkout etc if replacements were wrong

08_git_diff

Settings

keep_panels_open (default true) - Configure whether to open panels for snippets or not

Test

UnitTesting plugin

Use UnitTesting plugin

  • Open tests/test_copy_paste_killer_selection_converter.py
  • Open “Command Palette…” (⇧⌘P)
  • Run “UnitTesting: Test Current File”

Test scenarios

  • “tests/data/*_source.txt” -> Expected snippet for selection
  • “tests/data/*_find.txt” -> Expected snippet for find_string
  • “tests/data/*_replace.txt” -> Expected snippet for replace_string

Manual testing

Any selection shouldn't change with “copy_paste_killer” command and “Find” and “Replace” when replace_string isn't changed.