Search in sources :

Example 1 with JavaCodeFragmentFactory

use of com.intellij.psi.JavaCodeFragmentFactory in project intellij-community by JetBrains.

the class ExtractMethodObject4DebuggerTest method doTest.

private void doTest(String evaluatedText, String expectedCallSite, String expectedClass, boolean codeBlock) throws Exception {
    final String testName = getTestName(false);
    configureByFile("/refactoring/extractMethodObject4Debugger/" + testName + ".java");
    final int offset = getEditor().getCaretModel().getOffset();
    final PsiElement context = getFile().findElementAt(offset);
    final JavaCodeFragmentFactory fragmentFactory = JavaCodeFragmentFactory.getInstance(getProject());
    final JavaCodeFragment fragment = codeBlock ? fragmentFactory.createCodeBlockCodeFragment(evaluatedText, context, false) : fragmentFactory.createExpressionCodeFragment(evaluatedText, context, null, false);
    final ExtractLightMethodObjectHandler.ExtractedData extractedData = ExtractLightMethodObjectHandler.extractLightMethodObject(getProject(), context, fragment, "test");
    assertNotNull(extractedData);
    assertEquals(expectedCallSite, extractedData.getGeneratedCallText());
    final PsiClass innerClass = extractedData.getGeneratedInnerClass();
    assertEquals(expectedClass, innerClass.getText());
}
Also used : PsiClass(com.intellij.psi.PsiClass) ExtractLightMethodObjectHandler(com.intellij.refactoring.extractMethodObject.ExtractLightMethodObjectHandler) JavaCodeFragment(com.intellij.psi.JavaCodeFragment) PsiElement(com.intellij.psi.PsiElement) JavaCodeFragmentFactory(com.intellij.psi.JavaCodeFragmentFactory)

Example 2 with JavaCodeFragmentFactory

use of com.intellij.psi.JavaCodeFragmentFactory in project intellij-community by JetBrains.

the class JavaFormatterTest method testFormatCodeFragment.

public void testFormatCodeFragment() throws Exception {
    final JavaCodeFragmentFactory factory = JavaCodeFragmentFactory.getInstance(getProject());
    final PsiCodeFragment fragment = factory.createCodeBlockCodeFragment("a=1;int b=2;", null, true);
    final PsiElement[] result = new PsiElement[1];
    CommandProcessor.getInstance().executeCommand(getProject(), () -> WriteCommandAction.runWriteCommandAction(null, () -> {
        try {
            result[0] = CodeStyleManager.getInstance(getProject()).reformat(fragment);
        } catch (IncorrectOperationException e) {
            fail(e.getLocalizedMessage());
        }
    }), null, null);
    assertEquals("a = 1;\n" + "int b = 2;", result[0].getText());
}
Also used : PsiCodeFragment(com.intellij.psi.PsiCodeFragment) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PsiElement(com.intellij.psi.PsiElement) JavaCodeFragmentFactory(com.intellij.psi.JavaCodeFragmentFactory)

Aggregations

JavaCodeFragmentFactory (com.intellij.psi.JavaCodeFragmentFactory)2 PsiElement (com.intellij.psi.PsiElement)2 JavaCodeFragment (com.intellij.psi.JavaCodeFragment)1 PsiClass (com.intellij.psi.PsiClass)1 PsiCodeFragment (com.intellij.psi.PsiCodeFragment)1 ExtractLightMethodObjectHandler (com.intellij.refactoring.extractMethodObject.ExtractLightMethodObjectHandler)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1