Java tutorial
/* * Copyright (C) 2014 meeniex * https://github.com/meeniex/ * * The MIT License (MIT) * * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package org.startica.meeniex.intellij.plugin.eclipseLikeEnterInStringLiteralHandler.editor.actionSystem; import com.intellij.codeInsight.editorActions.JavaLikeQuoteHandler; import com.intellij.codeInsight.editorActions.QuoteHandler; import com.intellij.codeInsight.editorActions.TypedHandler; import com.intellij.codeInsight.editorActions.enter.EnterHandlerDelegateAdapter; import com.intellij.lang.ASTNode; import com.intellij.openapi.actionSystem.CommonDataKeys; import com.intellij.openapi.actionSystem.DataContext; import com.intellij.openapi.actionSystem.IdeActions; import com.intellij.openapi.actionSystem.ex.ActionManagerEx; import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.actionSystem.EditorActionHandler; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Ref; import com.intellij.openapi.util.TextRange; import com.intellij.psi.PsiDocumentManager; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; import org.jetbrains.annotations.NotNull; /** * At end of the "string literal", move caret to out of that. * Created by meeniex on 2014/12/06 */ public class EclipseLikeEnterInStringLiteralHandler extends EnterHandlerDelegateAdapter { @Override public Result preprocessEnter(@NotNull PsiFile psiFile, @NotNull Editor editor, @NotNull Ref<Integer> caretOffsetRef, @NotNull Ref<Integer> caretAdvanceRef, @NotNull DataContext dataContext, EditorActionHandler originalHandler) { if (isSplitLineAction()) { return Result.Continue; } int caretOffset = caretOffsetRef.get(); PsiElement psiAtOffset = psiFile.findElementAt(caretOffset); if (psiAtOffset != null && psiAtOffset.getTextOffset() < caretOffset) { final Project project = CommonDataKeys.PROJECT.getData(dataContext); final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project); final Document document = editor.getDocument(); ASTNode token = psiAtOffset.getNode(); JavaLikeQuoteHandler quoteHandler = getJavaLikeQuoteHandler(editor, psiAtOffset); if (quoteHandler != null && quoteHandler.getConcatenatableStringTokenTypes() != null && quoteHandler.getConcatenatableStringTokenTypes().contains(token.getElementType())) { TextRange range = token.getTextRange(); if (caretOffset + 1 == range.getEndOffset()) { caretOffset++; caretOffsetRef.set(caretOffset); editor.getCaretModel().moveToOffset(caretOffset); documentManager.commitDocument(document); return Result.Stop; } } } return Result.Continue; } private boolean isSplitLineAction() { ActionManagerEx actionManagerEx = ActionManagerEx.getInstanceEx(); String prevPreformedActionId = actionManagerEx.getPrevPreformedActionId(); String lastPreformedActionId = actionManagerEx.getLastPreformedActionId(); return IdeActions.ACTION_EDITOR_SPLIT.equals(prevPreformedActionId) || IdeActions.ACTION_EDITOR_SPLIT.equals(lastPreformedActionId); } private JavaLikeQuoteHandler getJavaLikeQuoteHandler(Editor editor, PsiElement psiAtOffset) { final QuoteHandler fileTypeQuoteHandler = TypedHandler.getQuoteHandler(psiAtOffset.getContainingFile(), editor); return fileTypeQuoteHandler instanceof JavaLikeQuoteHandler ? (JavaLikeQuoteHandler) fileTypeQuoteHandler : null; } }