Refactoring Guru Refactoring Examples
repository·main·Indexed 19 days ago
https://github.com/refactoringguru/refactoring-examplesA collection of interactive programming examples demonstrating refactoring techniques in Java, C#, PHP, and TypeScript. Includes documentation on defining interactive scenarios, using popover tooltips, selecting code elements, and simulating compilation results through a specialized command set including Print, Replace, and Remove.
What's inside refactoring-examples
- This repository provides a collection of simple and interactive refactoring examples. These examples are implemented across multiple programming languages, including C#, Java, PHP, and TypeScript, to demonstrate various refactoring techniques in practice.
Simulate compilation results
mainSince compilation is simulated, use a two-step popover sequence to show the process:
- Start Compilation: Use
#Cfollowed by a message explaining the compilation attempt. - Show Result:
- Use
#Sfor a successful compilation with a success message. - Use
#Ffor a failed compilation with an error message. If failing, you can follow up with aSelectcommand to highlight the error location.
- Use
Example:
#C Let's compile this baby. #S Everything works fine! #C Let's compile this baby. #F Error! Unknown variable <code>b</code> in method "someMethod" Select "b" in "someMethod"#C Let's compile this baby. #S Everything works fine! #C Let's compile this baby. #F Error! Unknown variable <code>b</code> in method "someMethod" Select "b" in "someMethod"- Start Compilation: Use
Define an interactive example scenario
mainAn interactive scenario is a script that guides a user through a refactoring process. It is composed of 5 distinct sections separated by
###delimiters:- Scenario ID and language: The identifier and target language (e.g.,
extract-method:java). - Steps: A numbered list of steps (one per line).
- Starting code: The initial code state, wrapped in triple backticks.
- Resulting code: The final code state after refactoring, wrapped in triple backticks.
- Actions: A list of commands that drive the interactive experience.
Example structure:
extract-method:java ### 1. Some step. 2. Another step. ### ```java class Example { }class Example { public int field; }Set step 1
First popover
extract-method:java
Some step.
Another step.
class Example { }class Example { public int field; }Set step 1
Here's the first popover
- Scenario ID and language: The identifier and target language (e.g.,
Select text and code elements
mainThe
Selectaction highlights text to draw attention or prepare for replacement.Key capabilities:
- Targeting specific items: Use
Select Nth "text"to pick a specific match from multiple results. - Contextual selection: Use
into narrow down the search area (e.g.,Select "field" in "ExampleClass"). - Structural selection: Target parts of code like
parameters of,body of,name of,visibility of,type of, orwhole ofa method/class. - Multiline selection: Use a block syntax:
line 1 line 2Select: - Sub-selection: Use
|||to select specific parts of a larger selection (e.g.,Select "Multiple |||lines||| go here"). - Additive selection: Prefix a command with
+to add to the current selection instead of replacing it (e.g.,+ Select "something else").
Select "private int field;" Select 3rd "getSomething()" Select "private int field;" in "ExampleClass" Select "interval" in parameters of "doSomething" Select "interval" in body of "doSomething" in "ExampleClass" Select:Multiple lines go here
Select:Multiple |||lines||| go here. I also want to select |||this|||.
+ Select "something else"- Targeting specific items: Use
Modify code with Print, Replace, and Remove
mainUse these commands to change the code content:
- Print (or Type): Inserts text at the cursor or replaces selected text. Supports multiline blocks.
- Replace: Identical to
Print, but includes a slight delay to allow the user to see the selection before the change occurs. - Remove selected: Deletes the currently selected text. Includes a slight delay so the user sees what is being deleted.
- Indent / Deindent: Adjusts indentation. Use
Indent N timesorDeindent N timeswhereNis the number of tab stops.
Print "some text" Print:Multiline text goes here.
Replace "old text" Remove selected Indent 2 times DeindentManage scenario flow and timing
mainControl the progression and pacing of the interactive scenario:
- Set step: Advances the scenario to a specific step number (e.g.,
Set step 1) or to the end (Set final step). - Wait: Pauses execution for a specified duration (e.g.,
Wait 500ms) to allow the user to process changes.
Set step 1 Wait 500ms Set final step- Set step: Advances the scenario to a specific step number (e.g.,
Use Popovers (tooltips) to guide users
mainPopovers are tooltips that attach to the selected text or the cursor. Use the
#character to create them. You can append options to the#character to control behavior:- Direction: Use arrow characters (
^,V,<,>) to point the popover. Default is down (V). - Delay: Append a number (milliseconds) to automatically proceed after a delay (e.g.,
#<2000). - Remain visible: Append
+to keep the popover visible after the next action (use#=to hide it later). - Attachment to steps: Use
Qto attach the popover to the step list (useful for final messages). - Close all: Use
#=to close all visible popovers and show a new one.
Example syntax:
# Simple popover #<2000+ Popover to the right, fires in 2s, remains visible. #= Close all visible popovers. #Q Show final popover attached to steps.#<2000+ Popover to the right of selected text, which will fire next action in 2 seconds, but will remain visible. #= Close all visible popovers and show new one. #Q Show final popover, attached to steps.- Direction: Use arrow characters (
Move the cursor with Go to
mainThe
Go tocommand moves the cursor to a specific location. This is often used to set a target for aPrintaction. Note thatGo todeselects any current selection.Usage patterns:
- Marker-based: Use
|||to specify the exact insertion point (e.g.,Go to "private |||int field;"). - Named targets:
Go to the end of fileGo to start of "methodName"Go to end of "methodName"Go to before "methodName" in "ClassName"Go to after "methodName"Go to parameters of "methodName"Go to the end of parameters of "methodName" in "ClassName"
Note: The cursor is inserted after opening braces
{or at the end of the previous line before a closing brace}.Go to "private |||int field;" Go to:class Example { private |||int field; }
Go to start of "someMethod" Go to parameters of "someMethod"- Marker-based: Use