List of usage examples for com.intellij.openapi.editor.actionSystem EditorActionHandler execute
public final void execute(@NotNull Editor editor, @Nullable final Caret contextCaret, final DataContext dataContext)
From source file:com.goide.completion.BracesInsertHandler.java
License:Apache License
@Override public void handleInsert(@Nonnull InsertionContext context, LookupElement item) { Editor editor = context.getEditor(); CharSequence documentText = context.getDocument().getImmutableCharSequence(); int offset = skipWhiteSpaces(editor.getCaretModel().getOffset(), documentText); if (documentText.charAt(offset) != '{') { Project project = context.getProject(); Template template = TemplateManager.getInstance(project).createTemplate("braces", "go", myOneLine ? "{$END$}" : " {\n$END$\n}"); template.setToReformat(true);//w w w. j a va 2 s . c o m TemplateManager.getInstance(project).startTemplate(editor, template); } else { editor.getCaretModel().moveToOffset(offset); ApplicationManager.getApplication().runWriteAction(() -> { EditorActionHandler enterAction = EditorActionManager.getInstance() .getActionHandler(IdeActions.ACTION_EDITOR_START_NEW_LINE); enterAction.execute(editor, editor.getCaretModel().getCurrentCaret(), ((EditorEx) editor).getDataContext()); }); } }
From source file:com.google.idea.blaze.base.lang.buildfile.editor.BuildEnterHandler.java
License:Open Source License
@Override public Result preprocessEnter(PsiFile file, Editor editor, Ref<Integer> caretOffset, Ref<Integer> caretAdvance, DataContext dataContext, EditorActionHandler originalHandler) { int offset = caretOffset.get(); if (editor instanceof EditorWindow) { file = InjectedLanguageManager.getInstance(file.getProject()).getTopLevelFile(file); editor = InjectedLanguageUtil.getTopLevelEditor(editor); offset = editor.getCaretModel().getOffset(); }/*from w w w. j a v a 2 s .c om*/ if (!isApplicable(file, dataContext)) { return Result.Continue; } // Previous enter handler's (e.g. EnterBetweenBracesHandler) can introduce a mismatch // between the editor's caret model and the offset we've been provided with. editor.getCaretModel().moveToOffset(offset); Document doc = editor.getDocument(); PsiDocumentManager.getInstance(file.getProject()).commitDocument(doc); CodeStyleSettings currentSettings = CodeStyleSettingsManager.getSettings(file.getProject()); IndentOptions indentOptions = currentSettings.getIndentOptions(file.getFileType()); Integer indent = determineIndent(file, editor, offset, indentOptions); if (indent == null) { return Result.Continue; } removeTrailingWhitespace(doc, file, offset); originalHandler.execute(editor, editor.getCaretModel().getCurrentCaret(), dataContext); LogicalPosition position = editor.getCaretModel().getLogicalPosition(); if (position.column == indent) { return Result.Stop; } if (position.column > indent) { //default enter handler has added too many spaces -- remove them int excess = position.column - indent; doc.deleteString(editor.getCaretModel().getOffset() - excess, editor.getCaretModel().getOffset()); } else if (position.column < indent) { String spaces = StringUtil.repeatSymbol(' ', indent - position.column); doc.insertString(editor.getCaretModel().getOffset(), spaces); } editor.getCaretModel().moveToLogicalPosition(new LogicalPosition(position.line, indent)); return Result.Stop; }
From source file:com.google.idea.blaze.base.lang.projectview.formatting.ProjectViewEnterHandler.java
License:Open Source License
@Override public Result preprocessEnter(PsiFile file, Editor editor, Ref<Integer> caretOffset, Ref<Integer> caretAdvance, DataContext dataContext, EditorActionHandler originalHandler) { int offset = caretOffset.get(); if (editor instanceof EditorWindow) { file = InjectedLanguageManager.getInstance(file.getProject()).getTopLevelFile(file); editor = InjectedLanguageUtil.getTopLevelEditor(editor); offset = editor.getCaretModel().getOffset(); }//from w w w .ja v a 2s . c o m if (!isApplicable(file, dataContext) || !insertIndent(file, offset)) { return Result.Continue; } int indent = SectionParser.INDENT; editor.getCaretModel().moveToOffset(offset); Document doc = editor.getDocument(); PsiDocumentManager.getInstance(file.getProject()).commitDocument(doc); originalHandler.execute(editor, editor.getCaretModel().getCurrentCaret(), dataContext); LogicalPosition position = editor.getCaretModel().getLogicalPosition(); if (position.column < indent) { String spaces = StringUtil.repeatSymbol(' ', indent - position.column); doc.insertString(editor.getCaretModel().getOffset(), spaces); } editor.getCaretModel().moveToLogicalPosition(new LogicalPosition(position.line, indent)); return Result.Stop; }
From source file:com.intellij.codeInsight.lookup.impl.BackspaceHandler.java
License:Apache License
static void truncatePrefix(final DataContext dataContext, LookupImpl lookup, final EditorActionHandler handler, final int hideOffset, final Caret caret) { final Editor editor = lookup.getEditor(); if (!lookup.performGuardedChange(new Runnable() { @Override/*from w w w .j a v a 2 s. c o m*/ public void run() { handler.execute(editor, caret, dataContext); } })) { return; } final CompletionProgressIndicator process = CompletionServiceImpl.getCompletionService() .getCurrentCompletion(); if (lookup.truncatePrefix(process == null || !process.isAutopopupCompletion())) { return; } if (process != null) { if (hideOffset < editor.getCaretModel().getOffset()) { process.scheduleRestart(); return; } process.prefixUpdated(); } lookup.hide(); }
From source file:com.intellij.lang.jsgraphql.ide.formatter.JSGraphQLEnterHandlerDelegate.java
License:Open Source License
@Override public Result preprocessEnter(@NotNull PsiFile file, @NotNull Editor editor, @NotNull Ref<Integer> caretOffsetRef, @NotNull Ref<Integer> caretAdvance, @NotNull DataContext dataContext, EditorActionHandler originalHandler) { try {//from ww w . ja v a2s . c o m // only process if we're inside a JSFile that has GraphQL language injection on the current psi element boolean doProcess = false; boolean injected = false; if (file instanceof JSFile) { Integer caretOffset = caretOffsetRef.get(); final PsiElement psiAtOffset = PsiUtilCore.getElementAtOffset(file, caretOffset); PsiElement psiToCheck = psiAtOffset; while (psiToCheck != null) { if (JSGraphQLLanguageInjectionUtil.isJSGraphQLLanguageInjectionTarget(psiToCheck)) { doProcess = true; injected = true; break; } psiToCheck = psiToCheck.getParent(); } } if (doProcess) { // based on EnterBetweenBracesHandler Document document = editor.getDocument(); CharSequence text = document.getCharsSequence(); int caretOffset = caretOffsetRef.get(); if (!CodeInsightSettings.getInstance().SMART_INDENT_ON_ENTER) { return Result.Continue; } int prevCharOffset = CharArrayUtil.shiftBackward(text, caretOffset - 1, " \t"); int nextCharOffset = CharArrayUtil.shiftForward(text, caretOffset, " \t"); boolean withinText = prevCharOffset >= 0 && text.length() > nextCharOffset; if (withinText) { final char c1 = text.charAt(prevCharOffset); final char c2 = text.charAt(nextCharOffset); if (isBracePair(c1, c2) || (injected && isBackTickPair(c1, c2))) { originalHandler.execute(editor, editor.getCaretModel().getCurrentCaret(), dataContext); PsiDocumentManager.getInstance(file.getProject()).commitDocument(document); try { CodeStyleManager.getInstance(file.getProject()).adjustLineIndent(file, editor.getCaretModel().getOffset()); } catch (IncorrectOperationException e) { log.error(e); } } } } return Result.Continue; } catch (Throwable e) { log.error("Unable to apply enter action", e); } return Result.Continue; }
From source file:com.intellij.testFramework.LightPlatformCodeInsightTestCase.java
License:Apache License
protected static void executeAction(@NonNls @NotNull final String actionId) { CommandProcessor.getInstance().executeCommand(getProject(), new Runnable() { @Override/*from ww w.j a va 2 s . c o m*/ public void run() { EditorActionManager actionManager = EditorActionManager.getInstance(); EditorActionHandler actionHandler = actionManager.getActionHandler(actionId); actionHandler.execute(getEditor(), null, DataManager.getInstance().getDataContext()); } }, "", null); }
From source file:com.shape.idea.completion.BracesInsertHandler.java
License:Apache License
@Override public void handleInsert(InsertionContext context, LookupElement item) { final Editor editor = context.getEditor(); final CharSequence documentText = context.getDocument().getImmutableCharSequence(); int offset = skipWhiteSpaces(editor.getCaretModel().getOffset(), documentText); if (documentText.charAt(offset) != '{') { Project project = context.getProject(); Template template = TemplateManager.getInstance(project).createTemplate("braces", "shp", myOneLine ? "{$END$}" : " {\n$END$\n}"); template.setToReformat(true);/*w w w . j a v a 2 s.com*/ TemplateManager.getInstance(project).startTemplate(editor, template); } else { editor.getCaretModel().moveToOffset(offset); ApplicationManager.getApplication().runWriteAction(new Runnable() { @Override public void run() { EditorActionHandler enterAction = EditorActionManager.getInstance() .getActionHandler(IdeActions.ACTION_EDITOR_START_NEW_LINE); enterAction.execute(editor, editor.getCaretModel().getCurrentCaret(), ((EditorEx) editor).getDataContext()); } }); } }