Skip to content

Commit d375eac

Browse files
committed
New GraphQL input type should not be inserted between current type and its documentation (#15)
1 parent 207915c commit d375eac

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/main/com/intellij/lang/jsgraphql/endpoint/doc/psi/JSGraphQLEndpointDocPsiUtil.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,20 @@ public static boolean isDocumentationComment(PsiElement element) {
103103
return false;
104104
}
105105

106+
/**
107+
* Gets the PSI comment that starts the documentation for the specified element, or <code>null</code> if no documentation is available
108+
*/
109+
public static PsiComment getDocumentationStartElement(PsiElement element) {
110+
final PsiComment comment = PsiTreeUtil.getPrevSiblingOfType(element, PsiComment.class);
111+
if(isDocumentationComment(comment)) {
112+
final List<PsiComment> siblings = Lists.newArrayList(comment);
113+
getDocumentationCommentSiblings(comment, siblings, PsiElement::getPrevSibling);
114+
Collections.reverse(siblings);
115+
return siblings.get(0);
116+
}
117+
return null;
118+
}
119+
106120
/**
107121
* Gets the text of the continuous comments placed directly above the specified element
108122
* @param element element whose previous siblings are enumerated and included if they're documentation comments

src/main/com/intellij/lang/jsgraphql/endpoint/ide/intentions/JSGraphQLEndpointCreateDefinitionIntention.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction;
1212
import com.intellij.ide.impl.DataManagerImpl;
1313
import com.intellij.lang.jsgraphql.endpoint.JSGraphQLEndpointTokenTypes;
14+
import com.intellij.lang.jsgraphql.endpoint.doc.psi.JSGraphQLEndpointDocPsiUtil;
1415
import com.intellij.lang.jsgraphql.endpoint.psi.*;
1516
import com.intellij.openapi.actionSystem.ActionManager;
1617
import com.intellij.openapi.actionSystem.ActionPlaces;
@@ -20,7 +21,7 @@
2021
import com.intellij.openapi.editor.Editor;
2122
import com.intellij.openapi.project.Project;
2223
import com.intellij.openapi.util.Ref;
23-
import com.intellij.openapi.util.TextRange;
24+
import com.intellij.psi.PsiComment;
2425
import com.intellij.psi.PsiDocumentManager;
2526
import com.intellij.psi.PsiElement;
2627
import com.intellij.psi.PsiReference;
@@ -77,8 +78,17 @@ public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement
7778
indent = true;
7879
}
7980
final Document document = editor.getDocument();
80-
final TextRange definitionTextRange = definition.getTextRange();
81-
final int insertOffset = insertBefore ? definitionTextRange.getStartOffset() : definitionTextRange.getEndOffset();
81+
final int insertOffset;
82+
if(insertBefore) {
83+
final PsiComment documentationStartElement = JSGraphQLEndpointDocPsiUtil.getDocumentationStartElement(definition);
84+
if(documentationStartElement != null) {
85+
insertOffset = documentationStartElement.getTextRange().getStartOffset();
86+
} else {
87+
insertOffset = definition.getTextRange().getStartOffset();
88+
}
89+
} else {
90+
insertOffset = definition.getTextRange().getEndOffset();
91+
}
8292
document.insertString(insertOffset, definitionText);
8393
if (caretOffsetAfterInsert.get() != null) {
8494
// move caret to new position

0 commit comments

Comments
 (0)