Java Code Examples for com.intellij.codeInsight.template.impl.TemplateState#gotoEnd()

The following examples show how to use com.intellij.codeInsight.template.impl.TemplateState#gotoEnd() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EscapeHandler.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(Editor editor, DataContext dataContext) {
  TemplateState templateState = TemplateManagerImpl.getTemplateState(editor);
  if (templateState != null && !templateState.isFinished()) {
    SelectionModel selectionModel = editor.getSelectionModel();
    LookupImpl lookup = (LookupImpl)LookupManager.getActiveLookup(editor);

    // the idea behind lookup checking is that if there is a preselected value in lookup
    // then user might want just to close lookup but not finish a template.
    // E.g. user wants to move to the next template segment by Tab without completion invocation.
    // If there is no selected value in completion that user definitely wants to finish template
    boolean lookupIsEmpty = lookup == null || lookup.getCurrentItem() == null;
    if (!selectionModel.hasSelection() && lookupIsEmpty) {
      CommandProcessor.getInstance().setCurrentCommandName(CodeInsightBundle.message("finish.template.command"));
      templateState.gotoEnd(true);
      return;
    }
  }

  if (myOriginalHandler.isEnabled(editor, dataContext)) {
    myOriginalHandler.execute(editor, dataContext);
  }
}
 
Example 2
Source File: LSPRenameHandler.java    From lsp4intellij with Apache License 2.0 5 votes vote down vote up
private InplaceRefactoring doRename(PsiElement elementToRename, Editor editor) {
    if (elementToRename instanceof PsiNameIdentifierOwner) {
        RenamePsiElementProcessor processor = RenamePsiElementProcessor.forElement(elementToRename);
        if (processor.isInplaceRenameSupported()) {
            StartMarkAction startMarkAction = StartMarkAction.canStart(elementToRename.getProject());
            if (startMarkAction == null || (processor.substituteElementToRename(elementToRename, editor)
                    == elementToRename)) {
                processor.substituteElementToRename(elementToRename, editor, new Pass<PsiElement>() {
                    @Override
                    public void pass(PsiElement element) {
                        MemberInplaceRenamer renamer = createMemberRenamer(element,
                                (PsiNameIdentifierOwner) elementToRename, editor);
                        boolean startedRename = renamer.performInplaceRename();
                        if (!startedRename) {
                            performDialogRename(editor);
                        }
                    }
                });
                return null;
            }
        } else {
            InplaceRefactoring inplaceRefactoring = editor.getUserData(InplaceRefactoring.INPLACE_RENAMER);
            if ((inplaceRefactoring instanceof MemberInplaceRenamer)) {
                TemplateState templateState = TemplateManagerImpl
                        .getTemplateState(InjectedLanguageUtil.getTopLevelEditor(editor));
                if (templateState != null) {
                    templateState.gotoEnd(true);
                }
            }
        }
    }
    performDialogRename(editor);
    return null;
}
 
Example 3
Source File: InplaceRefactoring.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static void navigateToStarted(final Document oldDocument, final Project project, final int exitCode) {
  final PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(oldDocument);
  if (file != null) {
    final VirtualFile virtualFile = file.getVirtualFile();
    if (virtualFile != null) {
      final FileEditor[] editors = FileEditorManager.getInstance(project).getEditors(virtualFile);
      for (FileEditor editor : editors) {
        if (editor instanceof TextEditor) {
          final Editor textEditor = ((TextEditor)editor).getEditor();
          final TemplateState templateState = TemplateManagerImpl.getTemplateState(textEditor);
          if (templateState != null) {
            if (exitCode == DialogWrapper.OK_EXIT_CODE) {
              final TextRange range = templateState.getVariableRange(PRIMARY_VARIABLE_NAME);
              if (range != null) {
                new OpenFileDescriptor(project, virtualFile, range.getStartOffset()).navigate(true);
                return;
              }
            }
            else if (exitCode > 0){
              templateState.gotoEnd();
              return;
            }
          }
        }
      }
    }
  }
}
 
Example 4
Source File: MemberInplaceRenameHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public InplaceRefactoring doRename(@Nonnull final PsiElement elementToRename, final Editor editor, final DataContext dataContext) {
  if (elementToRename instanceof PsiNameIdentifierOwner) {
    final RenamePsiElementProcessor processor = RenamePsiElementProcessor.forElement(elementToRename);
    if (processor.isInplaceRenameSupported()) {
      final StartMarkAction startMarkAction = StartMarkAction.canStart(elementToRename.getProject());
      if (startMarkAction == null || processor.substituteElementToRename(elementToRename, editor) == elementToRename) {
        processor.substituteElementToRename(elementToRename, editor, new Pass<PsiElement>() {
          @Override
          public void pass(PsiElement element) {
            final MemberInplaceRenamer renamer = createMemberRenamer(element, (PsiNameIdentifierOwner)elementToRename, editor);
            boolean startedRename = renamer.performInplaceRename();
            if (!startedRename) {
              performDialogRename(elementToRename, editor, dataContext);
            }
          }
        });
        return null;
      }
      else {
        final InplaceRefactoring inplaceRefactoring = editor.getUserData(InplaceRefactoring.INPLACE_RENAMER);
        if (inplaceRefactoring != null && inplaceRefactoring.getClass() == MemberInplaceRenamer.class) {
          final TemplateState templateState = TemplateManagerImpl.getTemplateState(InjectedLanguageUtil.getTopLevelEditor(editor));
          if (templateState != null) {
            templateState.gotoEnd(true);
          }
        }
      }
    }
  }
  performDialogRename(elementToRename, editor, dataContext);
  return null;
}
 
Example 5
Source File: AbstractInplaceIntroducer.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void restartInplaceIntroduceTemplate() {
  Runnable restartTemplateRunnable = new Runnable() {
    @Override
    public void run() {
      final TemplateState templateState = TemplateManagerImpl.getTemplateState(myEditor);
      if (templateState != null) {
        myEditor.putUserData(INTRODUCE_RESTART, true);
        try {
          final TextRange range = templateState.getCurrentVariableRange();
          if (range != null && range.isEmpty()) {
            final String[] names = suggestNames(isReplaceAllOccurrences(), getLocalVariable());
            ApplicationManager.getApplication().runWriteAction(new Runnable() {
              @Override
              public void run() {
                myEditor.getDocument().insertString(myEditor.getCaretModel().getOffset(), names[0]);
              }
            });
          }
          templateState.gotoEnd(true);
          try {
            myShouldSelect = false;
            startInplaceIntroduceTemplate();
          }
          finally {
            myShouldSelect = true;
          }
        }
        finally {
          myEditor.putUserData(INTRODUCE_RESTART, false);
        }
      }
      updateTitle(getVariable());
    }
  };
  CommandProcessor.getInstance().executeCommand(myProject, restartTemplateRunnable, getCommandName(), getCommandName());
}
 
Example 6
Source File: AbstractInplaceIntroducer.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void stopIntroduce(Editor editor) {
  final TemplateState templateState = TemplateManagerImpl.getTemplateState(editor);
  if (templateState != null) {
    final Runnable runnable = new Runnable() {
      @Override
      public void run() {
        templateState.gotoEnd(true);
      }
    };
    CommandProcessor.getInstance().executeCommand(myProject, runnable, getCommandName(), getCommandName());
  }
}
 
Example 7
Source File: SmartEnterAction.java    From consulo with Apache License 2.0 4 votes vote down vote up
@RequiredWriteAction
@Override
public void executeWriteAction(Editor editor, Caret caret, DataContext dataContext) {
  Project project = dataContext.getData(CommonDataKeys.PROJECT);
  if (project == null || editor.isOneLineMode()) {
    plainEnter(editor, caret, dataContext);
    return;
  }

  LookupManager.getInstance(project).hideActiveLookup();

  TemplateState state = TemplateManagerImpl.getTemplateState(editor);
  if (state != null) {
    state.gotoEnd();
  }

  final int caretOffset = editor.getCaretModel().getOffset();

  PsiFile psiFile = PsiUtilBase.getPsiFileInEditor(editor, project);
  if (psiFile == null) {
    plainEnter(editor, caret, dataContext);
    return;
  }

  if (EnterAfterUnmatchedBraceHandler.isAfterUnmatchedLBrace(editor, caretOffset, psiFile.getFileType())) {
    EditorActionHandler enterHandler = EditorActionManager.getInstance().getActionHandler(IdeActions.ACTION_EDITOR_ENTER);
    enterHandler.execute(editor, caret, dataContext);
    return;
  }

  final Language language = PsiUtilBase.getLanguageInEditor(editor, project);
  boolean processed = false;
  if (language != null) {
    final List<SmartEnterProcessor> processors = SmartEnterProcessors.INSTANCE.allForLanguage(language);
    if (!processors.isEmpty()) {
      for (SmartEnterProcessor processor : processors) {
        if (processor.process(project, editor, psiFile)) {
          processed = true;
          break;
        }
      }
    }
  }
  if (!processed) {
    plainEnter(editor, caret, dataContext);
  }
}
 
Example 8
Source File: InplaceRefactoring.java    From consulo with Apache License 2.0 4 votes vote down vote up
protected boolean buildTemplateAndStart(final Collection<PsiReference> refs,
                                        final Collection<Pair<PsiElement, TextRange>> stringUsages,
                                        final PsiElement scope,
                                        final PsiFile containingFile) {
  final PsiElement context = InjectedLanguageManager.getInstance(containingFile.getProject()).getInjectionHost(containingFile);
  myScope = context != null ? context.getContainingFile() : scope;
  final TemplateBuilderImpl builder = new TemplateBuilderImpl(myScope);

  PsiElement nameIdentifier = getNameIdentifier();
  int offset = myEditor.getCaretModel().getOffset();
  PsiElement selectedElement = getSelectedInEditorElement(nameIdentifier, refs, stringUsages, offset);

  boolean subrefOnPrimaryElement = false;
  boolean hasReferenceOnNameIdentifier = false;
  for (PsiReference ref : refs) {
    if (isReferenceAtCaret(selectedElement, ref)) {
      builder.replaceElement(ref, PRIMARY_VARIABLE_NAME, createLookupExpression(selectedElement), true);
      subrefOnPrimaryElement = true;
      continue;
    }
    addVariable(ref, selectedElement, builder, offset);
    hasReferenceOnNameIdentifier |= isReferenceAtCaret(nameIdentifier, ref);
  }
  if (nameIdentifier != null) {
    hasReferenceOnNameIdentifier |= selectedElement.getTextRange().contains(nameIdentifier.getTextRange());
    if (!subrefOnPrimaryElement || !hasReferenceOnNameIdentifier){
      addVariable(nameIdentifier, selectedElement, builder);
    }
  }
  for (Pair<PsiElement, TextRange> usage : stringUsages) {
    addVariable(usage.first, usage.second, selectedElement, builder);
  }
  addAdditionalVariables(builder);
  try {
    myMarkAction = startRename();
  }
  catch (final StartMarkAction.AlreadyStartedException e) {
    final Document oldDocument = e.getDocument();
    if (oldDocument != myEditor.getDocument()) {
      final int exitCode = Messages.showYesNoCancelDialog(myProject, e.getMessage(), getCommandName(),
                                                          "Navigate to Started", "Cancel Started", "Cancel", Messages.getErrorIcon());
      if (exitCode == Messages.CANCEL) return true;
      navigateToAlreadyStarted(oldDocument, exitCode);
      return true;
    }
    else {

      if (!ourRenamersStack.isEmpty() && ourRenamersStack.peek() == this) {
        ourRenamersStack.pop();
        if (!ourRenamersStack.empty()) {
          myOldName = ourRenamersStack.peek().myOldName;
        }
      }

      revertState();
      final TemplateState templateState = TemplateManagerImpl.getTemplateState(InjectedLanguageUtil.getTopLevelEditor(myEditor));
      if (templateState != null) {
        templateState.gotoEnd(true);
      }
    }
    return false;
  }

  beforeTemplateStart();

  new WriteCommandAction(myProject, getCommandName()) {
    @Override
    protected void run(com.intellij.openapi.application.Result result) throws Throwable {
      startTemplate(builder);
    }
  }.execute();

  if (myBalloon == null) {
    showBalloon();
  }
  return true;
}