当前位置: 首页>>代码示例>>Java>>正文


Java GenerateMembersUtil.positionCaret方法代码示例

本文整理汇总了Java中com.intellij.codeInsight.generation.GenerateMembersUtil.positionCaret方法的典型用法代码示例。如果您正苦于以下问题:Java GenerateMembersUtil.positionCaret方法的具体用法?Java GenerateMembersUtil.positionCaret怎么用?Java GenerateMembersUtil.positionCaret使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.intellij.codeInsight.generation.GenerateMembersUtil的用法示例。


在下文中一共展示了GenerateMembersUtil.positionCaret方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: invoke

import com.intellij.codeInsight.generation.GenerateMembersUtil; //导入方法依赖的package包/类
@Override
public void invoke(@NotNull Project project,
                   @NotNull PsiFile file,
                   @Nullable("is null when called from inspection") Editor editor,
                   @NotNull PsiElement startElement,
                   @NotNull PsiElement endElement) {
  final PsiClass myClass = (PsiClass)startElement;
  if (!FileModificationService.getInstance().prepareFileForWrite(myClass.getContainingFile())) return;
  PsiCodeBlock body;
  if (myClass.isInterface() && (body = myMethodPrototype.getBody()) != null) body.delete();
  for (String exception : myExceptions) {
    PsiUtil.addException(myMethodPrototype, exception);
  }
  PsiMethod method = (PsiMethod)myClass.add(myMethodPrototype);
  method = (PsiMethod)method.replace(reformat(project, method));
  if (editor != null && method.getContainingFile() == file) {
    GenerateMembersUtil.positionCaret(editor, method, true);
  }
}
 
开发者ID:lshain-android-source,项目名称:tools-idea,代码行数:20,代码来源:AddMethodFix.java

示例2: postAddAction

import com.intellij.codeInsight.generation.GenerateMembersUtil; //导入方法依赖的package包/类
protected void postAddAction(@NotNull PsiFile file,
                             @Nullable("is null when called from inspection") Editor editor,
                             PsiMethod newMethod) {
  if (editor != null && newMethod.getContainingFile() == file) {
    GenerateMembersUtil.positionCaret(editor, newMethod, true);
  }
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:8,代码来源:AddMethodFix.java

示例3: processIntention

import com.intellij.codeInsight.generation.GenerateMembersUtil; //导入方法依赖的package包/类
@Override
protected void processIntention(Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
  final PsiLambdaExpression lambdaExpression = PsiTreeUtil.getParentOfType(element, PsiLambdaExpression.class);
  LOG.assertTrue(lambdaExpression != null);
  final PsiParameter[] paramListCopy = ((PsiParameterList)lambdaExpression.getParameterList().copy()).getParameters();
  final PsiType functionalInterfaceType = lambdaExpression.getFunctionalInterfaceType();
  LOG.assertTrue(functionalInterfaceType != null);
  final PsiMethod method = LambdaUtil.getFunctionalInterfaceMethod(functionalInterfaceType);
  LOG.assertTrue(method != null);

  final String blockText = getBodyText(lambdaExpression);
  if (blockText == null) return;

  final PsiElementFactory psiElementFactory = JavaPsiFacade.getElementFactory(element.getProject());
  PsiCodeBlock blockFromText = psiElementFactory.createCodeBlockFromText(blockText, lambdaExpression);
  qualifyThisExpressions(lambdaExpression, psiElementFactory, blockFromText);
  blockFromText = psiElementFactory.createCodeBlockFromText(blockFromText.getText(), null);
  
  PsiNewExpression newExpression = (PsiNewExpression)psiElementFactory.createExpressionFromText("new " + functionalInterfaceType.getCanonicalText() + "(){}", lambdaExpression);
  newExpression = (PsiNewExpression)JavaCodeStyleManager.getInstance(lambdaExpression.getProject()).shortenClassReferences(lambdaExpression.replace(newExpression));

  final PsiAnonymousClass anonymousClass = newExpression.getAnonymousClass();
  LOG.assertTrue(anonymousClass != null);
  final List<PsiGenerationInfo<PsiMethod>> infos = OverrideImplementUtil.overrideOrImplement(anonymousClass, method);
  if (infos != null && infos.size() == 1) {
    final PsiMethod member = infos.get(0).getPsiMember();
    final PsiParameter[] parameters = member.getParameterList().getParameters();
    if (parameters.length == paramListCopy.length) {
      for (int i = 0; i < parameters.length; i++) {
        final PsiParameter parameter = parameters[i];
        final String lambdaParamName = paramListCopy[i].getName();
        if (lambdaParamName != null) {
          parameter.setName(lambdaParamName);
        }
      }
    }
    PsiCodeBlock codeBlock = member.getBody();
    LOG.assertTrue(codeBlock != null);

    codeBlock = (PsiCodeBlock)codeBlock.replace(blockFromText);
    GenerateMembersUtil.positionCaret(editor, member, true);
  }
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:44,代码来源:ReplaceLambdaWithAnonymousIntention.java

示例4: processIntention

import com.intellij.codeInsight.generation.GenerateMembersUtil; //导入方法依赖的package包/类
@Override
protected void processIntention(Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
  final PsiLambdaExpression lambdaExpression = PsiTreeUtil.getParentOfType(element, PsiLambdaExpression.class);
  LOG.assertTrue(lambdaExpression != null);
  final PsiParameter[] paramListCopy = ((PsiParameterList)lambdaExpression.getParameterList().copy()).getParameters();
  PsiType functionalInterfaceType = lambdaExpression.getFunctionalInterfaceType();
  LOG.assertTrue(functionalInterfaceType != null);
  functionalInterfaceType = GenericsUtil.eliminateWildcards(functionalInterfaceType);
  final PsiMethod method = LambdaUtil.getFunctionalInterfaceMethod(functionalInterfaceType);
  LOG.assertTrue(method != null);

  final String blockText = getBodyText(lambdaExpression);
  if (blockText == null) return;

  final PsiElementFactory psiElementFactory = JavaPsiFacade.getElementFactory(element.getProject());
  PsiCodeBlock blockFromText = psiElementFactory.createCodeBlockFromText(blockText, lambdaExpression);
  ChangeContextUtil.encodeContextInfo(blockFromText, true);
  PsiNewExpression newExpression = (PsiNewExpression)psiElementFactory.createExpressionFromText("new " + functionalInterfaceType.getCanonicalText() + "(){}", lambdaExpression);
  final PsiClass thisClass = PsiTreeUtil.getParentOfType(lambdaExpression, PsiClass.class, true);
  final String thisClassName = thisClass.getName();
  if (thisClassName != null) {
    final PsiThisExpression thisAccessExpr = thisClass instanceof PsiAnonymousClass ? null : RefactoringChangeUtil
      .createThisExpression(lambdaExpression.getManager(), thisClass);
    ChangeContextUtil.decodeContextInfo(blockFromText, thisClass, thisAccessExpr);
    final Map<PsiElement, PsiElement> replacements = new HashMap<PsiElement, PsiElement>();
    blockFromText.accept(new JavaRecursiveElementWalkingVisitor() {
      @Override
      public void visitSuperExpression(PsiSuperExpression expression) {
        super.visitSuperExpression(expression);
        if (expression.getQualifier() == null) {
          replacements.put(expression, psiElementFactory.createExpressionFromText(thisClassName + "." + expression.getText(), expression));
        }
      }

    });
    for (PsiElement psiElement : replacements.keySet()) {
      psiElement.replace(replacements.get(psiElement));
    }
  }
  blockFromText = psiElementFactory.createCodeBlockFromText(blockFromText.getText(), null);
  newExpression = (PsiNewExpression)JavaCodeStyleManager.getInstance(lambdaExpression.getProject()).shortenClassReferences(lambdaExpression.replace(newExpression));

  final PsiAnonymousClass anonymousClass = newExpression.getAnonymousClass();
  LOG.assertTrue(anonymousClass != null);
  final List<PsiGenerationInfo<PsiMethod>> infos = OverrideImplementUtil.overrideOrImplement(anonymousClass, method);
  if (infos != null && infos.size() == 1) {
    final PsiMethod member = infos.get(0).getPsiMember();
    final PsiParameter[] parameters = member.getParameterList().getParameters();
    for (int i = 0; i < parameters.length; i++) {
      final PsiParameter parameter = parameters[i];
      final String lambdaParamName = paramListCopy[i].getName();
      if (lambdaParamName != null) {
        parameter.setName(lambdaParamName);
      }
    }
    PsiCodeBlock codeBlock = member.getBody();
    LOG.assertTrue(codeBlock != null);

    codeBlock = (PsiCodeBlock)codeBlock.replace(blockFromText);
    GenerateMembersUtil.positionCaret(editor, member, true);
  }
}
 
开发者ID:lshain-android-source,项目名称:tools-idea,代码行数:63,代码来源:ReplaceLambdaWithAnonymousIntention.java


注:本文中的com.intellij.codeInsight.generation.GenerateMembersUtil.positionCaret方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。