void () { String s1 = """ { System.out.println(1); System.out.println(2); System.out.println(3); } { System.out.println(1); System.out.println(2); System.out.println(3); } { System.out.println(1); System.out.println(2); System.out.println(3); }"""; String s2 = """ System.out.println(1); System.out.println(2); System.out.println(3); """; String s3 = """ System.out.println(3); System.out.println(2); System.out.println(1); """; String expectedResult1 = """ { System.out.println(3); System.out.println(2); System.out.println(1); } { System.out.println(3); System.out.println(2); System.out.println(1); } { System.out.println(3); System.out.println(2); System.out.println(1); }"""; options.setToReformatAccordingToStyle(true); assertEquals("three statements replacement", expectedResult1, replace(s1, s2, s3)); options.setToReformatAccordingToStyle(false); String s4 = """ ProgressManager.getInstance().startNonCancelableAction(); try { read(id, READ_PARENT); return myViewport.parent; } finally { ProgressManager.getInstance().finishNonCancelableAction(); }"""; String s5 = """ ProgressManager.getInstance().startNonCancelableAction(); try { '_statement{2,2}; } finally { ProgressManager.getInstance().finishNonCancelableAction(); }"""; String s6 = "$statement$;"; String expectedResult2 = "read(id, READ_PARENT);\n" + " return myViewport.parent;"; assertEquals("extra ;", expectedResult2, replace(s4, s5, s6)); String s7 = """ public class A { void f() { new Runnable() { public void run() { l(); } private void l() { int i = 9; int j = 9; } }; new Runnable() { public void run() { l(); } private void l() { l(); l(); } }; } }"""; String s8 = """ new Runnable() { public void run() { '_l (); } private void '_l () { '_st{2,2}; } };"""; String s9 = """ new My() { public void f() { $st$; } };"""; String expectedResult3 = """ public class A { void f() { new My() { public void f() { int i = 9; int j = 9; } }; new My() { public void f() { l(); l(); } }; } }"""; boolean formatAccordingToStyle = options.isToReformatAccordingToStyle(); options.setToReformatAccordingToStyle(true); assertEquals("extra ; 2", expectedResult3, replace(s7, s8, s9)); String s10 = """ public class A { void f() { new Runnable() { public void run() { l(); l(); } public void run2() { l(); l(); } }; new Runnable() { public void run() { l(); l(); } public void run2() { l(); l(); } }; new Runnable() { public void run() { l(); l(); } public void run2() { l2(); l2(); } }; } private void l() { int i = 9; int j = 9; } } abstract class My { abstract void f(); }"""; String s11 = """ new Runnable() { public void run() { '_l{2,2}; } public void run2() { '_l; } };"""; String s12 = """ new My() { public void f() { $l$; } };"""; String expectedResult4 = """ public class A { void f() { new My() { public void f() { l(); l(); } }; new My() { public void f() { l(); l(); } }; new Runnable() { public void run() { l(); l(); } public void run2() { l2(); l2(); } }; } private void l() { int i = 9; int j = 9; } } abstract class My { abstract void f(); }"""; assertEquals("same multiple occurrences 2 times", expectedResult4, replace(s10, s11, s12)); options.setToReformatAccordingToStyle(formatAccordingToStyle); String s13 = """ PsiLock.LOCK.acquire(); try { return value; } finally { PsiLock.LOCK.release(); }\ """; String s13_2 = """ PsiLock.LOCK.acquire(); try { if (true) { return value; } } finally { PsiLock.LOCK.release(); }\ """; String s13_3 = """ PsiLock.LOCK.acquire(); try { if (true) { return value; } if (true) { return value; } } finally { PsiLock.LOCK.release(); }\ """; String s14 = """ PsiLock.LOCK.acquire(); try { '_T{1,1000}; } finally { PsiLock.LOCK.release(); }\ """; String s15 = """ synchronized(PsiLock.LOCK) { $T$; }"""; String expectedResult5 = """ synchronized (PsiLock.LOCK) { return value; }"""; options.setToReformatAccordingToStyle(true); assertEquals("extra ; over return", expectedResult5, replace(s13, s14, s15)); options.setToReformatAccordingToStyle(false); String expectedResult6 = """ synchronized (PsiLock.LOCK) { if (true) { return value; } }"""; options.setToReformatAccordingToStyle(true); assertEquals("extra ; over if", expectedResult6, replace(s13_2, s14, s15)); options.setToReformatAccordingToStyle(false); String expectedResult7 = """ synchronized (PsiLock.LOCK) { if (true) { return value; } if (true) { return value; } }"""; options.setToReformatAccordingToStyle(true); assertEquals("newlines in matches of several lines", expectedResult7, replace(s13_3, s14, s15)); options.setToReformatAccordingToStyle(false); String s16 = """ public class SSTest { Object lock; public Object getProducts (String[] productNames) { synchronized (lock) { Object o = new Object (); assert o != null; return o; } } }"""; String s16_2 = """ public class SSTest { Object lock; public void getProducts (String[] productNames) { synchronized (lock) { boolean[] v = {true}; } } }"""; String s17 = """ synchronized(lock) { '_Statement*; }"""; String s18 = "$Statement$;"; String expectedResult8 = """ public class SSTest { Object lock; public Object getProducts (String[] productNames) { Object o = new Object (); assert o != null; return o; } }"""; String expectedResult8_2 = """ public class SSTest { Object lock; public void getProducts (String[] productNames) { boolean[] v = {true}; } }"""; assertEquals("extra ;", expectedResult8, replace(s16, s17, s18)); assertEquals("missed ;", expectedResult8_2, replace(s16_2, s17, s18)); }