diff --git a/java/java-impl/src/com/intellij/codeInsight/editorActions/JavadocLineStartHandler.java b/java/java-impl/src/com/intellij/codeInsight/editorActions/JavadocLineStartHandler.java new file mode 100644 index 000000000000..d8d366322fa2 --- /dev/null +++ b/java/java-impl/src/com/intellij/codeInsight/editorActions/JavadocLineStartHandler.java @@ -0,0 +1,74 @@ +/* + * Copyright 2000-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.codeInsight.editorActions; + +import com.intellij.openapi.actionSystem.DataContext; +import com.intellij.openapi.editor.Caret; +import com.intellij.openapi.editor.Document; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.editor.EditorModificationUtil; +import com.intellij.openapi.editor.actionSystem.EditorActionHandler; +import com.intellij.openapi.editor.actionSystem.EditorWriteActionHandler; +import com.intellij.openapi.editor.ex.EditorSettingsExternalizable; +import com.intellij.openapi.project.Project; +import com.intellij.psi.*; +import com.intellij.psi.javadoc.PsiDocToken; +import com.intellij.psi.tree.IElementType; +import com.intellij.util.text.CharArrayUtil; +import org.jetbrains.annotations.Nullable; + +public class JavadocLineStartHandler extends EditorWriteActionHandler { + private static final String WHITESPACE = " \t"; + + private final EditorActionHandler myOriginalHandler; + + public JavadocLineStartHandler(EditorActionHandler originalHandler) { + super(true); + myOriginalHandler = originalHandler; + } + + @Override + public void executeWriteAction(Editor editor, @Nullable Caret caret, DataContext dataContext) { + assert caret != null; + Project project = editor.getProject(); + if (project != null && EditorSettingsExternalizable.getInstance().isSmartHome()) { + Document document = editor.getDocument(); + CharSequence text = document.getImmutableCharSequence(); + int lineStartOffset = document.getLineStartOffset(caret.getLogicalPosition().line); + int nonWsStartOffset = CharArrayUtil.shiftForward(text, lineStartOffset, WHITESPACE); + if (CharArrayUtil.regionMatches(text, nonWsStartOffset, "/**") || CharArrayUtil.regionMatches(text, nonWsStartOffset, "*")) { + PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project); + PsiFile file = psiDocumentManager.getPsiFile(document); + if (file instanceof PsiJavaFile) { + psiDocumentManager.commitDocument(document); + PsiElement startElement = file.findElementAt(nonWsStartOffset); + if (startElement instanceof PsiDocToken) { + IElementType type = ((PsiDocToken)startElement).getTokenType(); + if (type == JavaDocTokenType.DOC_COMMENT_START || type == JavaDocTokenType.DOC_COMMENT_LEADING_ASTERISKS) { + int targetOffset = CharArrayUtil.shiftForward(text, startElement.getTextRange().getEndOffset(), WHITESPACE); + if (caret.getOffset() == targetOffset) targetOffset = lineStartOffset; + caret.moveToOffset(targetOffset); + caret.removeSelection(); + EditorModificationUtil.scrollToCaret(editor); + return; + } + } + } + } + } + myOriginalHandler.execute(editor, caret, dataContext); + } +} diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/editorActions/JavaEditingTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/editorActions/JavaEditingTest.java new file mode 100644 index 000000000000..2297c52e7f8e --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/codeInsight/editorActions/JavaEditingTest.java @@ -0,0 +1,34 @@ +/* + * Copyright 2000-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.codeInsight.editorActions; + +import com.intellij.openapi.editor.impl.AbstractEditorTest; +import com.intellij.testFramework.TestFileType; + +public class JavaEditingTest extends AbstractEditorTest { + public void testSmartHomeInJavadoc() throws Exception { + init("/**\n" + + " * some text\n" + + " */\n" + + "class C {}", + TestFileType.JAVA); + home(); + checkResultByText("/**\n" + + " * some text\n" + + " */\n" + + "class C {}"); + } +} diff --git a/resources/src/META-INF/IdeaPlugin.xml b/resources/src/META-INF/IdeaPlugin.xml index 8975072427de..e69552ea4176 100644 --- a/resources/src/META-INF/IdeaPlugin.xml +++ b/resources/src/META-INF/IdeaPlugin.xml @@ -1365,6 +1365,8 @@ + +