IDEA-108141 reused code of FixDocCommentAction

This commit is contained in:
Dmitry Batkovich
2014-06-03 15:06:11 +04:00
parent d03289c9e5
commit 6a73a7151e
15 changed files with 213 additions and 5 deletions
@@ -0,0 +1,57 @@
/*
* Copyright 2000-2014 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.intention.impl;
import com.intellij.codeInsight.editorActions.FixDocCommentAction;
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
/**
* @author Dmitry Batkovich
*/
public class AddJavadocIntention extends PsiElementBaseIntentionAction {
@Override
public void invoke(@NotNull final Project project, final Editor editor, @NotNull final PsiElement element) throws IncorrectOperationException {
final PsiDocCommentOwner docCommentOwner = (PsiDocCommentOwner)element.getParent();
FixDocCommentAction.generateOrFixComment(docCommentOwner, project, editor);
}
@Override
public boolean isAvailable(@NotNull final Project project, final Editor editor, @NotNull final PsiElement element) {
if (!(element instanceof PsiIdentifier)) {
return false;
}
final PsiElement docCommentOwner = element.getParent();
return docCommentOwner instanceof PsiDocCommentOwner &&
!(docCommentOwner instanceof PsiAnonymousClass) && ((PsiDocCommentOwner)docCommentOwner).getDocComment() == null;
}
@NotNull
@Override
public String getFamilyName() {
return "Add Javadoc";
}
@NotNull
@Override
public String getText() {
return getFamilyName();
}
}
@@ -0,0 +1,8 @@
// "Add Javadoc" "true"
/**
*
*/
class Foo {
}
@@ -0,0 +1,10 @@
// "Add Javadoc" "true"
class A {
/**
*
*/
private int myFoo;
}
@@ -0,0 +1,10 @@
// "Add Javadoc" "true"
class A {
/**
*
*/
public void foo() {
}
}
@@ -0,0 +1,12 @@
// "Add Javadoc" "true"
class A {
/**
* @param s
* @return
* @throws Exception
*/
public int foo(final String s) throws Exception {
}
}
@@ -0,0 +1,5 @@
// "Add Javadoc" "true"
class Fo<caret>o {
}
@@ -0,0 +1,13 @@
// "Add Javadoc" "false"
class A {
void m() {
Object o<caret> = new Object() {
}
}
}
@@ -0,0 +1,7 @@
// "Add Javadoc" "true"
class A {
private int myFo<caret>o;
}
@@ -0,0 +1,7 @@
// "Add Javadoc" "true"
class A {
public void f<caret>oo() {
}
}
@@ -0,0 +1,7 @@
// "Add Javadoc" "true"
class A {
public int f<caret>oo(final String s) throws Exception {
}
}
@@ -0,0 +1,31 @@
/*
* Copyright 2000-2014 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.intention;
import com.intellij.codeInsight.daemon.LightIntentionActionTestCase;
/**
* @author Dmitry Batkovich
*/
public class AddJavadocIntentionTest extends LightIntentionActionTestCase {
public void test() throws Exception { doAllTests(); }
@Override
protected String getBasePath() {
return "/codeInsight/addJavadoc";
}
}
@@ -33,7 +33,6 @@ import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.util.PsiUtilCore;
import com.intellij.util.text.CharArrayUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
@@ -71,15 +70,25 @@ public class FixDocCommentAction extends EditorAction {
process(psiFile, editor, project, editor.getCaretModel().getOffset());
}
}
}
private static void process(@NotNull final PsiFile file, @NotNull final Editor editor, @NotNull final Project project, int offset) {
PsiElement elementAtOffset = file.findElementAt(offset);
if (elementAtOffset == null) {
return;
}
generateOrFixComment(elementAtOffset, project, editor);
}
Language language = PsiUtilCore.getLanguageAtOffset(file, offset);
/**
* Generates comment if it's not exist or try to fix if exists
*
* @param element target element for which a comment should be generated
* @param project current project
* @param editor target editor
*/
public static void generateOrFixComment(@NotNull final PsiElement element, @NotNull final Project project, @NotNull final Editor editor) {
Language language = element.getLanguage();
final CodeDocumentationProvider docProvider;
final DocumentationProvider langDocumentationProvider = LanguageDocumentation.INSTANCE.forLanguage(language);
if (langDocumentationProvider instanceof CompositeDocumentationProvider) {
@@ -95,7 +104,7 @@ public class FixDocCommentAction extends EditorAction {
return;
}
final Pair<PsiElement, PsiComment> pair = docProvider.parseContext(elementAtOffset);
final Pair<PsiElement, PsiComment> pair = docProvider.parseContext(element);
if (pair == null) {
return;
}
@@ -0,0 +1,8 @@
public class X {
/**
* @param j
*/
public void m(int j) {
}
}
@@ -0,0 +1,20 @@
<!--
~ Copyright 2000-2014 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.
-->
<html>
<body>
Creates documentation comment for class, method or field if it's not created yet.
</body>
</html>
+4
View File
@@ -875,6 +875,10 @@
<category>Other</category>
</intentionAction>
<intentionAction>
<className>com.intellij.codeInsight.intention.impl.AddJavadocIntention</className>
<category>Other</category>
</intentionAction>