Java 类com.intellij.psi.JavaCodeFragmentFactory 实例源码

项目:intellij-ce-playground    文件:JavaFormatterTest.java   
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(), new Runnable() {
    @Override
    public void run() {
      WriteCommandAction.runWriteCommandAction(null, new Runnable() {
        @Override
        public void run() {
          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());
}
项目:intellij-ce-playground    文件:ExtractMethodObject4DebuggerTest.java   
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(), getFile(), fragment, "test");
  assertNotNull(extractedData);
  assertEquals(expectedCallSite, extractedData.getGeneratedCallText());
  final PsiClass innerClass = extractedData.getGeneratedInnerClass();
  assertEquals(expectedClass, innerClass.getText());
}
项目:intellij-ce-playground    文件:GrTableParameterInfo.java   
public GrTableParameterInfo(GrParameter parameter, int position) {
  myPosition = position;
  final Project project = parameter.getProject();
  myName = new GroovyCodeFragment(project, parameter.getName());
  final PsiType type = parameter.getDeclaredType();
  final JavaCodeFragmentFactory factory = JavaCodeFragmentFactory.getInstance(project);
  if (type != null) {
    String typeText = type.getCanonicalText();
    if (typeText == null) typeText = type.getPresentableText();
    myType = factory.createTypeCodeFragment(typeText, parameter, true, JavaCodeFragmentFactory.ALLOW_VOID | JavaCodeFragmentFactory.ALLOW_ELLIPSIS);
  }
  else {
    myType = factory.createTypeCodeFragment("", parameter, true, JavaCodeFragmentFactory.ALLOW_VOID | JavaCodeFragmentFactory.ALLOW_ELLIPSIS);
  }
  final GrExpression defaultInitializer = parameter.getInitializerGroovy();
  if (defaultInitializer != null) {
    myDefaultInitializer = new GroovyCodeFragment(project, defaultInitializer.getText());
  }
  else {
    myDefaultInitializer = new GroovyCodeFragment(project, "");
  }
  myDefaultValue = new GroovyCodeFragment(project, "");
}
项目:tools-idea    文件:GrTableParameterInfo.java   
public GrTableParameterInfo(GrParameter parameter, int position) {
  myPosition = position;
  final Project project = parameter.getProject();
  myName = new GroovyCodeFragment(project, parameter.getName());
  final PsiType type = parameter.getDeclaredType();
  final JavaCodeFragmentFactory factory = JavaCodeFragmentFactory.getInstance(project);
  if (type != null) {
    String typeText = type.getCanonicalText();
    if (typeText == null) typeText = type.getPresentableText();
    myType = factory.createTypeCodeFragment(typeText, parameter, true, JavaCodeFragmentFactory.ALLOW_VOID | JavaCodeFragmentFactory.ALLOW_ELLIPSIS);
  }
  else {
    myType = factory.createTypeCodeFragment("", parameter, true, JavaCodeFragmentFactory.ALLOW_VOID | JavaCodeFragmentFactory.ALLOW_ELLIPSIS);
  }
  final GrExpression defaultInitializer = parameter.getInitializerGroovy();
  if (defaultInitializer != null) {
    myDefaultInitializer = new GroovyCodeFragment(project, defaultInitializer.getText());
  }
  else {
    myDefaultInitializer = new GroovyCodeFragment(project, "");
  }
  myDefaultValue = new GroovyCodeFragment(project, "");
}
项目:intellij-ce-playground    文件:GrTableParameterInfo.java   
public GrTableParameterInfo(Project project, PsiElement context) {
  this.myPosition = -1;
  myName = new GroovyCodeFragment(project, "");
  myDefaultValue = new GroovyCodeFragment(project, "");
  myType = JavaCodeFragmentFactory.getInstance(project).createTypeCodeFragment("", context, true, JavaCodeFragmentFactory.ALLOW_VOID);
  myDefaultInitializer = new GroovyCodeFragment(project, "");
}
项目:intellij-ce-playground    文件:GrTableParameterInfo.java   
public GrTableParameterInfo(Project project,
                            PsiElement context,
                            String name,
                            String type,
                            String defaultValue,
                            String defaultInitializer) {
  this.myPosition = -1;
  myName = new GroovyCodeFragment(project, name);
  myDefaultValue = new GroovyCodeFragment(project, defaultValue);
  myType = JavaCodeFragmentFactory.getInstance(project).createTypeCodeFragment(type, context, true, JavaCodeFragmentFactory.ALLOW_VOID);
  myDefaultInitializer = new GroovyCodeFragment(project, defaultInitializer);
}
项目:tools-idea    文件:GrTableParameterInfo.java   
public GrTableParameterInfo(Project project, PsiElement context) {
  this.myPosition = -1;
  myName = new GroovyCodeFragment(project, "");
  myDefaultValue = new GroovyCodeFragment(project, "");
  myType = JavaCodeFragmentFactory.getInstance(project).createTypeCodeFragment("", context, true, JavaCodeFragmentFactory.ALLOW_VOID);
  myDefaultInitializer = new GroovyCodeFragment(project, "");
}
项目:tools-idea    文件:GrTableParameterInfo.java   
public GrTableParameterInfo(Project project,
                            PsiElement context,
                            String name,
                            String type,
                            String defaultValue,
                            String defaultInitializer) {
  this.myPosition = -1;
  myName = new GroovyCodeFragment(project, name);
  myDefaultValue = new GroovyCodeFragment(project, defaultValue);
  myType = JavaCodeFragmentFactory.getInstance(project).createTypeCodeFragment(type, context, true, JavaCodeFragmentFactory.ALLOW_VOID);
  myDefaultInitializer = new GroovyCodeFragment(project, defaultInitializer);
}
项目:consulo-ui-designer    文件:ComponentItemDialog.java   
private void setEditorText(final String className) {
  final JavaCodeFragmentFactory factory = JavaCodeFragmentFactory.getInstance(myProject);
  PsiJavaPackage defaultPackage = JavaPsiFacade.getInstance(myProject).findPackage("");
  final PsiCodeFragment fragment = factory.createReferenceCodeFragment(className, defaultPackage, true, true);
  myDocument = PsiDocumentManager.getInstance(myProject).getDocument(fragment);
  myEditorTextField.setDocument(myDocument);
  updateOKAction();
}
项目:consulo-ui-designer    文件:ClassToBindProperty.java   
private void setEditorText(final String s) {
  final JavaCodeFragmentFactory factory = JavaCodeFragmentFactory.getInstance(myProject);
  PsiJavaPackage defaultPackage = JavaPsiFacade.getInstance(myProject).findPackage("");
  final PsiCodeFragment fragment = factory.createReferenceCodeFragment(s, defaultPackage, true, true);
  myDocument = PsiDocumentManager.getInstance(myProject).getDocument(fragment);
  myEditorTextField.setDocument(myDocument);
}
项目:consulo-ui-designer    文件:ClassNameInputDialog.java   
private void createUIComponents() {
  myEditorTextField1 = new EditorTextField("", myProject, JavaFileType.INSTANCE);
  final JavaCodeFragmentFactory factory = JavaCodeFragmentFactory.getInstance(myProject);
  PsiJavaPackage defaultPackage = JavaPsiFacade.getInstance(myProject).findPackage("");
  final PsiCodeFragment fragment = factory.createReferenceCodeFragment("", defaultPackage, true, true);
  myEditorTextField1.setDocument(PsiDocumentManager.getInstance(myProject).getDocument(fragment));
}
项目:consulo-java    文件:JavaReferenceEditorUtil.java   
@Nullable
@RequiredReadAction
public static Document createDocument(final String text, Project project, boolean isClassesAccepted) {
  final PsiJavaPackage defaultPackage = JavaPsiFacade.getInstance(project).findPackage("");
  final JavaCodeFragmentFactory factory = JavaCodeFragmentFactory.getInstance(project);
  final JavaCodeFragment fragment = factory.createReferenceCodeFragment(text, defaultPackage, true, isClassesAccepted);
  fragment.setVisibilityChecker(JavaCodeFragment.VisibilityChecker.EVERYTHING_VISIBLE);
  return PsiDocumentManager.getInstance(project).getDocument(fragment);
}
项目:consulo-java    文件:JavaReferenceEditorUtil.java   
@Nullable
@RequiredReadAction
public static Document createTypeDocument(final String text, Project project) {
  final PsiJavaPackage defaultPackage = JavaPsiFacade.getInstance(project).findPackage("");
  final JavaCodeFragmentFactory factory = JavaCodeFragmentFactory.getInstance(project);
  final JavaCodeFragment fragment = factory.createTypeCodeFragment(text, defaultPackage, true);
  fragment.setVisibilityChecker(JavaCodeFragment.VisibilityChecker.EVERYTHING_VISIBLE);
  return PsiDocumentManager.getInstance(project).getDocument(fragment);
}
项目:consulo-java    文件:EditorTextFieldWithBrowseButton.java   
private static Document createDocument(final String text,
                                       Project project,
                                       boolean isClassesAccepted,
                                       JavaCodeFragment.VisibilityChecker visibilityChecker) {
  PsiElement defaultPackage = JavaPsiFacade.getInstance(project).findPackage("");
  final JavaCodeFragmentFactory factory = JavaCodeFragmentFactory.getInstance(project);
  final JavaCodeFragment fragment = factory.createReferenceCodeFragment(text, defaultPackage, true, isClassesAccepted);
  fragment.setVisibilityChecker(visibilityChecker);
  return PsiDocumentManager.getInstance(project).getDocument(fragment);
}
项目:consulo-java    文件:PsiTypeCodeFragmentImpl.java   
public PsiTypeCodeFragmentImpl(final Project project, final boolean isPhysical, @NonNls final String name, final CharSequence text, final int flags, PsiElement context)
{
    super(project, isSet(flags, JavaCodeFragmentFactory.ALLOW_INTERSECTION) ? JavaElementType.TYPE_WITH_CONJUNCTIONS_TEXT : JavaElementType.TYPE_WITH_DISJUNCTIONS_TEXT, isPhysical, name, text,
            context);

    myAllowEllipsis = isSet(flags, JavaCodeFragmentFactory.ALLOW_ELLIPSIS);
    myAllowDisjunction = isSet(flags, JavaCodeFragmentFactory.ALLOW_DISJUNCTION);
    myAllowConjunction = isSet(flags, JavaCodeFragmentFactory.ALLOW_INTERSECTION);
    LOG.assertTrue(!myAllowConjunction || !myAllowDisjunction);

    if(isSet(flags, JavaCodeFragmentFactory.ALLOW_VOID))
    {
        putUserData(PsiUtil.VALID_VOID_TYPE_IN_CODE_FRAGMENT, Boolean.TRUE);
    }
}
项目:consulo-java    文件:ReferenceEditorComboWithBrowseButton.java   
private static Document createDocument(final String text,
                                       Project project,
                                       boolean isClassesAccepted, 
                                       final JavaCodeFragment.VisibilityChecker visibilityChecker) {
  PsiJavaPackage defaultPackage = JavaPsiFacade.getInstance(project).findPackage("");
  final JavaCodeFragment fragment = JavaCodeFragmentFactory.getInstance(project).createReferenceCodeFragment(text, defaultPackage, true, isClassesAccepted);
  fragment.setVisibilityChecker(visibilityChecker);
  return PsiDocumentManager.getInstance(project).getDocument(fragment);
}
项目:consulo-java    文件:JavaFormatterTest.java   
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(), new Runnable()
    {
        @Override
        public void run()
        {
            ApplicationManager.getApplication().runWriteAction(new Runnable()
            {
                @Override
                public void run()
                {
                    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());
}
项目:teavm    文件:TeaVMDebuggerEditorsProvider.java   
@Override
protected PsiFile createExpressionCodeFragment(@NotNull Project project, @NotNull String text,
        @Nullable PsiElement context, boolean isPhysical) {
    return JavaCodeFragmentFactory.getInstance(project).createExpressionCodeFragment(text, context, null,
            isPhysical);
}
项目:consulo-java    文件:EditMigrationEntryDialog.java   
@Override
protected JComponent createNorthPanel()
{
    JPanel panel = new JPanel(new GridBagLayout());
    GridBagConstraints gbConstraints = new GridBagConstraints();
    gbConstraints.insets = JBUI.insets(4);
    gbConstraints.weighty = 0;

    gbConstraints.gridwidth = GridBagConstraints.RELATIVE;
    gbConstraints.fill = GridBagConstraints.BOTH;
    gbConstraints.weightx = 0;
    myRbPackage = new JRadioButton(RefactoringBundle.message("migration.entry.package"));
    panel.add(myRbPackage, gbConstraints);

    gbConstraints.gridwidth = GridBagConstraints.RELATIVE;
    gbConstraints.fill = GridBagConstraints.BOTH;
    gbConstraints.weightx = 0;
    myRbClass = new JRadioButton(RefactoringBundle.message("migration.entry.class"));
    panel.add(myRbClass, gbConstraints);

    gbConstraints.gridwidth = GridBagConstraints.REMAINDER;
    gbConstraints.fill = GridBagConstraints.BOTH;
    gbConstraints.weightx = 1;
    panel.add(new JPanel(), gbConstraints);

    ButtonGroup buttonGroup = new ButtonGroup();
    buttonGroup.add(myRbPackage);
    buttonGroup.add(myRbClass);

    gbConstraints.weightx = 0;
    gbConstraints.gridwidth = GridBagConstraints.RELATIVE;
    gbConstraints.fill = GridBagConstraints.NONE;
    JLabel oldNamePrompt = new JLabel(RefactoringBundle.message("migration.entry.old.name"));
    panel.add(oldNamePrompt, gbConstraints);

    gbConstraints.gridwidth = GridBagConstraints.REMAINDER;
    gbConstraints.fill = GridBagConstraints.HORIZONTAL;
    gbConstraints.weightx = 1;
    final LanguageTextField.DocumentCreator documentCreator = new LanguageTextField.DocumentCreator()
    {
        @Override
        public Document createDocument(String value, @Nullable Language language, Project project)
        {
            PsiPackage defaultPackage = JavaPsiFacade.getInstance(project).findPackage("");
            final JavaCodeFragment fragment = JavaCodeFragmentFactory.getInstance(project).createReferenceCodeFragment("", defaultPackage, true, true);
            return PsiDocumentManager.getInstance(project).getDocument(fragment);
        }
    };
    myOldNameField = new LanguageTextField(JavaLanguage.INSTANCE, myProject, "", documentCreator);
    panel.add(myOldNameField, gbConstraints);

    gbConstraints.weightx = 0;
    gbConstraints.gridwidth = GridBagConstraints.RELATIVE;
    gbConstraints.fill = GridBagConstraints.NONE;
    JLabel newNamePrompt = new JLabel(RefactoringBundle.message("migration.entry.new.name"));
    panel.add(newNamePrompt, gbConstraints);

    gbConstraints.gridwidth = GridBagConstraints.REMAINDER;
    gbConstraints.fill = GridBagConstraints.HORIZONTAL;
    gbConstraints.weightx = 1;
    myNewNameField = new LanguageTextField(JavaLanguage.INSTANCE, myProject, "", documentCreator);
    panel.setPreferredSize(new Dimension(300, panel.getPreferredSize().height));
    panel.add(myNewNameField, gbConstraints);

    final DocumentListener documentAdapter = new DocumentListener()
    {
        @Override
        public void documentChanged(DocumentEvent e)
        {
            validateOKButton();
        }
    };
    myOldNameField.getDocument().addDocumentListener(documentAdapter);
    myNewNameField.getDocument().addDocumentListener(documentAdapter);
    return panel;
}
项目:consulo-java    文件:ChangeClassSignatureDialog.java   
private PsiTypeCodeFragment createValueCodeFragment() {
  final JavaCodeFragmentFactory factory = JavaCodeFragmentFactory.getInstance(myProject);
  return factory.createTypeCodeFragment("", myClass.getLBrace(), true);
}
项目:consulo-java    文件:JavaDebuggerEditorsProvider.java   
@Override
protected PsiFile createExpressionCodeFragment(@NotNull Project project, @NotNull String text, @Nullable PsiElement context, boolean isPhysical)
{
    return JavaCodeFragmentFactory.getInstance(project).createExpressionCodeFragment(text, context, null, isPhysical);
}
项目:consulo-java    文件:DefaultCodeFragmentFactory.java   
@Override
public JavaCodeFragment createCodeFragment(TextWithImports item, PsiElement context, final Project project)
{
    final JavaCodeFragmentFactory factory = JavaCodeFragmentFactory.getInstance(project);
    final String text = item.getText();

    final JavaCodeFragment fragment;
    if(CodeFragmentKind.EXPRESSION == item.getKind())
    {
        final String expressionText = StringUtil.endsWithChar(text, ';') ? text.substring(0, text.length() - 1) : text;
        fragment = factory.createExpressionCodeFragment(expressionText, context, null, true);
    }
    else /*if (CodeFragmentKind.CODE_BLOCK == item.getKind())*/
    {
        fragment = factory.createCodeBlockCodeFragment(text, context, true);
    }

    if(item.getImports().length() > 0)
    {
        fragment.addImportsFromString(item.getImports());
    }
    fragment.setVisibilityChecker(JavaCodeFragment.VisibilityChecker.EVERYTHING_VISIBLE);
    //noinspection HardCodedStringLiteral
    fragment.putUserData(KEY, "DebuggerComboBoxEditor.IS_DEBUGGER_EDITOR");
    fragment.putCopyableUserData(JavaCompletionUtilCore.DYNAMIC_TYPE_EVALUATOR, new PairFunction<PsiExpression, CompletionParameters, PsiType>()
    {
        @Override
        public PsiType fun(PsiExpression expression, CompletionParameters parameters)
        {
            if(!RuntimeTypeEvaluator.isSubtypeable(expression))
            {
                return null;
            }

            if(parameters.getInvocationCount() <= 1 && JavaCompletionUtilCore.mayHaveSideEffects(expression))
            {
                final CompletionService service = CompletionService.getCompletionService();
                if(parameters.getInvocationCount() < 2)
                {
                    service.setAdvertisementText("Invoke completion once more to see runtime type variants");
                }
                return null;
            }

            final DebuggerContextImpl debuggerContext = DebuggerManagerEx.getInstanceEx(project).getContext();
            DebuggerSession debuggerSession = debuggerContext.getDebuggerSession();
            if(debuggerSession != null && debuggerContext.getSuspendContext() != null)
            {
                final Semaphore semaphore = new Semaphore();
                semaphore.down();
                final AtomicReference<PsiType> nameRef = new AtomicReference<PsiType>();
                final RuntimeTypeEvaluator worker = new RuntimeTypeEvaluator(null, expression, debuggerContext, ProgressManager.getInstance().getProgressIndicator())
                {
                    @Override
                    protected void typeCalculationFinished(@Nullable PsiType type)
                    {
                        nameRef.set(type);
                        semaphore.up();
                    }
                };
                debuggerSession.getProcess().getManagerThread().invoke(worker);
                for(int i = 0; i < 50; i++)
                {
                    ProgressManager.checkCanceled();
                    if(semaphore.waitFor(20))
                    {
                        break;
                    }
                }
                return nameRef.get();
            }
            return null;
        }
    });

    return fragment;
}