com.intellij.codeInsight.CodeInsightUtilBase Java Examples

The following examples show how to use com.intellij.codeInsight.CodeInsightUtilBase. 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: FunctionBodyQuickfix.java    From BashSupport with Apache License 2.0 6 votes vote down vote up
@Override
public void invoke(@NotNull Project project, @NotNull PsiFile file, Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
    if (!CodeInsightUtilBase.prepareEditorForWrite(editor)) {
        return;
    }

    BashFunctionDef functionDef = (BashFunctionDef) startElement;

    final BashBlock block = functionDef.functionBody();
    if (block != null) {
        StringBuilder builder = new StringBuilder();
        final BashBlock body = functionDef.functionBody();
        builder.append("{ ").append(body.getText()).append(" }");

        int startOffset = body.getTextOffset();
        int endOffset = startOffset + body.getTextLength();

        Document document = PsiDocumentManager.getInstance(project).getDocument(file);
        document.replaceString(startOffset, endOffset, builder);

        PsiDocumentManager.getInstance(project).commitDocument(document);
    }
}
 
Example #2
Source File: CSharpDeclarationMover.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@RequiredReadAction
@Override
public void beforeMove(@Nonnull final Editor editor, @Nonnull final MoveInfo info, final boolean down)
{
	super.beforeMove(editor, info, down);

	if(myEnumToInsertSemicolonAfter != null)
	{
		TreeElement semicolon = Factory.createSingleLeafElement(CSharpTokens.SEMICOLON, ";", 0, 1, null, myEnumToInsertSemicolonAfter.getManager());

		try
		{
			PsiElement inserted = myEnumToInsertSemicolonAfter.getParent().addAfter(semicolon.getPsi(), myEnumToInsertSemicolonAfter);
			inserted = CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(inserted);
			final LogicalPosition position = editor.offsetToLogicalPosition(inserted.getTextRange().getEndOffset());

			info.toMove2 = new LineRange(position.line + 1, position.line + 1);
		}
		catch(IncorrectOperationException e)
		{
			LOG.error(e);
		}
		finally
		{
			myEnumToInsertSemicolonAfter = null;
		}
	}
}
 
Example #3
Source File: SurroundWithHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void invoke(@Nonnull Project project, @Nonnull Editor editor, @Nonnull PsiFile file, Surrounder surrounder) {
  if (!CodeInsightUtilBase.prepareEditorForWrite(editor)) return;
  if (file instanceof PsiCompiledElement) {
    HintManager.getInstance().showErrorHint(editor, "Can't modify decompiled code");
    return;
  }

  List<AnAction> applicable = buildSurroundActions(project, editor, file, surrounder);
  if (applicable != null) {
    showPopup(editor, applicable);
  }
  else if (!ApplicationManager.getApplication().isUnitTestMode()) {
    HintManager.getInstance().showErrorHint(editor, "Couldn't find Surround With variants applicable to the current context");
  }
}
 
Example #4
Source File: ImplementMethodsHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@RequiredUIAccess
@Override
public final void invoke(@Nonnull final Project project, @Nonnull final Editor editor, @Nonnull PsiFile file) {
  if (!CodeInsightUtilBase.prepareEditorForWrite(editor)) return;
  if (!FileDocumentManager.getInstance().requestWriting(editor.getDocument(), project)){
    return;
  }

  Language language = PsiUtilCore.getLanguageAtOffset(file, editor.getCaretModel().getOffset());
  final LanguageCodeInsightActionHandler codeInsightActionHandler = CodeInsightActions.IMPLEMENT_METHOD.forLanguage(language);
  if (codeInsightActionHandler != null) {
    codeInsightActionHandler.invoke(project, editor, file);
  }
}
 
Example #5
Source File: DelegateMethodsHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@RequiredUIAccess
@Override
public final void invoke(@Nonnull final Project project, @Nonnull final Editor editor, @Nonnull PsiFile file) {
  if (!CodeInsightUtilBase.prepareEditorForWrite(editor)) return;
  if (!FileDocumentManager.getInstance().requestWriting(editor.getDocument(), project)){
    return;
  }

  Language language = PsiUtilCore.getLanguageAtOffset(file, editor.getCaretModel().getOffset());
  final LanguageCodeInsightActionHandler codeInsightActionHandler = CodeInsightActions.DELEGATE_METHODS.forLanguage(language);
  if (codeInsightActionHandler != null) {
    codeInsightActionHandler.invoke(project, editor, file);
  }
}
 
Example #6
Source File: OverrideMethodsHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@RequiredUIAccess
@Override
public final void invoke(@Nonnull final Project project, @Nonnull final Editor editor, @Nonnull PsiFile file) {
  if (!CodeInsightUtilBase.prepareEditorForWrite(editor)) return;
  if (!FileDocumentManager.getInstance().requestWriting(editor.getDocument(), project)){
    return;
  }

  Language language = PsiUtilCore.getLanguageAtOffset(file, editor.getCaretModel().getOffset());
  final LanguageCodeInsightActionHandler codeInsightActionHandler = CodeInsightActions.OVERRIDE_METHOD.forLanguage(language);
  if (codeInsightActionHandler != null) {
    codeInsightActionHandler.invoke(project, editor, file);
  }
}
 
Example #7
Source File: UnwrapHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@RequiredUIAccess
@Override
public void invoke(@Nonnull Project project, @Nonnull Editor editor, @Nonnull PsiFile file) {
  if (!CodeInsightUtilBase.prepareEditorForWrite(editor)) return;
  List<AnAction> options = collectOptions(project, editor, file);
  selectOption(options, editor, file);
}
 
Example #8
Source File: EmacsStyleIndentAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
@RequiredUIAccess
@Override
public void invoke(@Nonnull final Project project, @Nonnull final Editor editor, @Nonnull final PsiFile file) {
  if (!CodeInsightUtilBase.prepareEditorForWrite(editor)) return;
  PsiDocumentManager.getInstance(project).commitAllDocuments();

  if (!FileDocumentManager.getInstance().requestWriting(editor.getDocument(), project)) {
    return;
  }

  EmacsProcessingHandler emacsProcessingHandler = LanguageEmacsExtension.INSTANCE.forLanguage(file.getLanguage());
  if (emacsProcessingHandler != null) {
    EmacsProcessingHandler.Result result = emacsProcessingHandler.changeIndent(project, editor, file);
    if (result == EmacsProcessingHandler.Result.STOP) {
      return;
    }
  }

  final Document document = editor.getDocument();
  final int startOffset = editor.getCaretModel().getOffset();
  final int line = editor.offsetToLogicalPosition(startOffset).line;
  final int col = editor.getCaretModel().getLogicalPosition().column;
  final int lineStart = document.getLineStartOffset(line);
  final int initLineEnd = document.getLineEndOffset(line);
  try{
    final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
    final int newPos = codeStyleManager.adjustLineIndent(file, lineStart);
    final int newCol = newPos - lineStart;
    final int lineInc = document.getLineEndOffset(line) - initLineEnd;
    if (newCol >= col + lineInc && newCol >= 0) {
      final LogicalPosition pos = new LogicalPosition(line, newCol);
      editor.getCaretModel().moveToLogicalPosition(pos);
      editor.getSelectionModel().removeSelection();
      editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
    }
  }
  catch(IncorrectOperationException e){
    LOG.error(e);
  }
}
 
Example #9
Source File: ListTemplatesHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@RequiredUIAccess
@Override
public void invoke(@Nonnull final Project project, @Nonnull final Editor editor, @Nonnull PsiFile file) {
  if (!CodeInsightUtilBase.prepareEditorForWrite(editor)) return;
  if (!FileDocumentManager.getInstance().requestWriting(editor.getDocument(), project)) {
    return;
  }
  EditorUtil.fillVirtualSpaceUntilCaret(editor);

  PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
  int offset = editor.getCaretModel().getOffset();
  List<TemplateImpl> applicableTemplates = TemplateManagerImpl.listApplicableTemplateWithInsertingDummyIdentifier(editor, file, false);

  Map<TemplateImpl, String> matchingTemplates = filterTemplatesByPrefix(applicableTemplates, editor, offset, false, true);
  MultiMap<String, CustomLiveTemplateLookupElement> customTemplatesLookupElements = getCustomTemplatesLookupItems(editor, file, offset);

  if (matchingTemplates.isEmpty()) {
    for (TemplateImpl template : applicableTemplates) {
      matchingTemplates.put(template, null);
    }
  }

  if (matchingTemplates.isEmpty() && customTemplatesLookupElements.isEmpty()) {
    HintManager.getInstance().showErrorHint(editor, CodeInsightBundle.message("templates.no.defined"));
    return;
  }

  showTemplatesLookup(project, editor, file, matchingTemplates, customTemplatesLookupElements);
}
 
Example #10
Source File: SurroundWithTemplateHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@RequiredUIAccess
@Override
public void invoke(@Nonnull final Project project, @Nonnull final Editor editor, @Nonnull PsiFile file) {
  if (!CodeInsightUtilBase.prepareEditorForWrite(editor)) return;
  DefaultActionGroup group = createActionGroup(project, editor, file);
  if (group == null) return;

  final ListPopup popup = JBPopupFactory.getInstance().createActionGroupPopup(CodeInsightBundle.message("templates.select.template.chooser.title"), group,
                                                                              DataManager.getInstance().getDataContext(editor.getContentComponent()),
                                                                              JBPopupFactory.ActionSelectionAid.MNEMONICS, false);

  popup.showInBestPositionFor(editor);
}
 
Example #11
Source File: InnerBuilderHandler.java    From innerbuilder with Apache License 2.0 5 votes vote down vote up
@Override
public void invoke(@NotNull final Project project, @NotNull final Editor editor, @NotNull final PsiFile file) {
    final PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project);
    final Document currentDocument = psiDocumentManager.getDocument(file);
    if (currentDocument == null) {
        return;
    }

    psiDocumentManager.commitDocument(currentDocument);

    if (!CodeInsightUtilBase.prepareEditorForWrite(editor)) {
        return;
    }

    if (!FileDocumentManager.getInstance().requestWriting(editor.getDocument(), project)) {
        return;
    }

    final List<PsiFieldMember> existingFields = collectFields(file, editor);
    if (existingFields != null) {
        final List<PsiFieldMember> selectedFields = selectFieldsAndOptions(existingFields, project);

        if (selectedFields == null || selectedFields.isEmpty()) {
            return;
        }

        InnerBuilderGenerator.generate(project, editor, file, selectedFields);
    }
}
 
Example #12
Source File: AutoIndentLinesHandler.java    From consulo with Apache License 2.0 4 votes vote down vote up
@RequiredWriteAction
@Override
public void invokeInWriteAction(@Nonnull Project project, @Nonnull Editor editor, @Nonnull PsiFile file) {
  if (!CodeInsightUtilBase.prepareEditorForWrite(editor)) return;
  PsiDocumentManager.getInstance(project).commitAllDocuments();

  if (!FileDocumentManager.getInstance().requestWriting(editor.getDocument(), project)){
    return;
  }

  Document document = editor.getDocument();
  int startOffset;
  int endOffset;
  RangeMarker selectionEndMarker = null;
  if (editor.getSelectionModel().hasSelection()){
    startOffset = editor.getSelectionModel().getSelectionStart();
    endOffset = editor.getSelectionModel().getSelectionEnd();
    selectionEndMarker = document.createRangeMarker(endOffset, endOffset);
    endOffset -= 1;
  }
  else{
    startOffset = endOffset = editor.getCaretModel().getOffset();
  }
  int line1 = editor.offsetToLogicalPosition(startOffset).line;
  int col = editor.getCaretModel().getLogicalPosition().column;

  try{
    adjustLineIndent(file, document, startOffset, endOffset, line1, project);
  }
  catch(IncorrectOperationException e){
    LOG.error(e);
  }

  if (selectionEndMarker == null){
    if (line1 < document.getLineCount() - 1){
      if (document.getLineStartOffset(line1 + 1) + col >= document.getTextLength()) {
        col = document.getLineEndOffset(line1 + 1) - document.getLineStartOffset(line1 + 1);
      }
      LogicalPosition pos = new LogicalPosition(line1 + 1, col);
      editor.getCaretModel().moveToLogicalPosition(pos);
      editor.getSelectionModel().removeSelection();
      editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
    }
  }
  else{
    if (!selectionEndMarker.isValid()) return;
    endOffset = selectionEndMarker.getEndOffset();
    editor.getSelectionModel().setSelection(startOffset, endOffset);
  }
}
 
Example #13
Source File: PasteHandler.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void execute(final Editor editor, final DataContext dataContext, @Nullable final Producer<Transferable> producer) {
  final Transferable transferable = EditorModificationUtil.getContentsToPasteToEditor(producer);
  if (transferable == null) return;

  if (!CodeInsightUtilBase.prepareEditorForWrite(editor)) return;

  final Document document = editor.getDocument();
  if (!FileDocumentManager.getInstance().requestWriting(document, dataContext.getData(CommonDataKeys.PROJECT))) {
    return;
  }

  DataContext context = new DataContext() {
    @Override
    public Object getData(@NonNls Key dataId) {
      return PasteAction.TRANSFERABLE_PROVIDER == dataId ? new Producer<Transferable>() {
        @Nullable
        @Override
        public Transferable produce() {
          return transferable;
        }
      } : dataContext.getData(dataId);
    }
  };

  final Project project = editor.getProject();
  if (project == null || editor.isColumnMode() || editor.getCaretModel().getCaretCount() > 1) {
    if (myOriginalHandler != null) {
      myOriginalHandler.execute(editor, null, context);
    }
    return;
  }

  final PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(document);
  if (file == null) {
    if (myOriginalHandler != null) {
      myOriginalHandler.execute(editor, null, context);
    }
    return;
  }

  DumbService.getInstance(project).setAlternativeResolveEnabled(true);
  document.startGuardedBlockChecking();
  try {
    for (PasteProvider provider : Extensions.getExtensions(EP_NAME)) {
      if (provider.isPasteEnabled(context)) {
        provider.performPaste(context);
        return;
      }
    }
    doPaste(editor, project, file, document, transferable);
  }
  catch (ReadOnlyFragmentModificationException e) {
    EditorActionManager.getInstance().getReadonlyFragmentModificationHandler(document).handle(e);
  }
  finally {
    document.stopGuardedBlockChecking();
    DumbService.getInstance(project).setAlternativeResolveEnabled(false);
  }
}