use of com.intellij.psi.javadoc.PsiDocTagValue in project intellij-community by JetBrains.
the class GroovyMethodSignatureInsertHandler method handleInsert.
@Override
public void handleInsert(InsertionContext context, LookupElement item) {
if (!(item.getObject() instanceof PsiMethod)) {
return;
}
PsiDocumentManager.getInstance(context.getProject()).commitDocument(context.getEditor().getDocument());
final Editor editor = context.getEditor();
final PsiMethod method = (PsiMethod) item.getObject();
final PsiParameter[] parameters = method.getParameterList().getParameters();
final StringBuilder buffer = new StringBuilder();
final CharSequence chars = editor.getDocument().getCharsSequence();
int endOffset = editor.getCaretModel().getOffset();
final Project project = context.getProject();
int afterSharp = CharArrayUtil.shiftBackwardUntil(chars, endOffset - 1, "#") + 1;
int signatureOffset = afterSharp;
PsiElement element = context.getFile().findElementAt(signatureOffset - 1);
final CodeStyleSettings styleSettings = CodeStyleSettingsManager.getSettings(element.getProject());
PsiDocTag tag = PsiTreeUtil.getParentOfType(element, PsiDocTag.class);
if (context.getCompletionChar() == Lookup.REPLACE_SELECT_CHAR) {
final PsiDocTagValue value = tag.getValueElement();
endOffset = value.getTextRange().getEndOffset();
}
editor.getDocument().deleteString(afterSharp, endOffset);
editor.getCaretModel().moveToOffset(signatureOffset);
editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
editor.getSelectionModel().removeSelection();
buffer.append(method.getName()).append("(");
final int afterParenth = afterSharp + buffer.length();
for (int i = 0; i < parameters.length; i++) {
final PsiType type = TypeConversionUtil.erasure(parameters[i].getType());
buffer.append(type.getCanonicalText());
if (i < parameters.length - 1) {
buffer.append(",");
if (styleSettings.SPACE_AFTER_COMMA)
buffer.append(" ");
}
}
buffer.append(")");
if (!(tag instanceof PsiInlineDocTag)) {
buffer.append(" ");
} else {
final int currentOffset = editor.getCaretModel().getOffset();
if (chars.charAt(currentOffset) == '}') {
afterSharp++;
} else {
buffer.append("} ");
}
}
String insertString = buffer.toString();
EditorModificationUtil.insertStringAtCaret(editor, insertString);
editor.getCaretModel().moveToOffset(afterSharp + buffer.length());
editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
shortenReferences(project, editor, context, afterParenth);
}
use of com.intellij.psi.javadoc.PsiDocTagValue in project intellij-community by JetBrains.
the class ConvertToInstanceMethodProcessor method findUsages.
@NotNull
protected UsageInfo[] findUsages() {
LOG.assertTrue(myTargetParameter.getDeclarationScope() == myMethod);
final Project project = myMethod.getProject();
final PsiReference[] methodReferences = ReferencesSearch.search(myMethod, GlobalSearchScope.projectScope(project), false).toArray(PsiReference.EMPTY_ARRAY);
List<UsageInfo> result = new ArrayList<>();
for (final PsiReference ref : methodReferences) {
final PsiElement element = ref.getElement();
if (element instanceof PsiReferenceExpression) {
if (element.getParent() instanceof PsiMethodCallExpression) {
result.add(new MethodCallUsageInfo((PsiMethodCallExpression) element.getParent()));
}
} else if (element instanceof PsiDocTagValue) {
//TODO:!!!
result.add(new JavaDocUsageInfo(ref));
}
}
for (final PsiReference ref : ReferencesSearch.search(myTargetParameter, new LocalSearchScope(myMethod), false)) {
final PsiElement element = ref.getElement();
if (element instanceof PsiReferenceExpression || element instanceof PsiDocParamRef) {
result.add(new ParameterUsageInfo(ref));
}
}
if (myTargetClass.isInterface()) {
PsiClass[] implementingClasses = RefactoringHierarchyUtil.findImplementingClasses(myTargetClass);
for (final PsiClass implementingClass : implementingClasses) {
result.add(new ImplementingClassUsageInfo(implementingClass));
}
}
return result.toArray(new UsageInfo[result.size()]);
}
use of com.intellij.psi.javadoc.PsiDocTagValue in project intellij-community by JetBrains.
the class DocValuesIterator method advanceToNext.
// since doctag value may be inside doc comment we specially skip that nodes from list
static PsiElement advanceToNext(PsiElement e) {
PsiElement nextSibling = e.getNextSibling();
if (nextSibling instanceof PsiDocTagValue)
e = nextSibling;
nextSibling = e.getNextSibling();
if (nextSibling instanceof PsiDocToken && ((PsiDocToken) nextSibling).getTokenType() == tokenType) {
e = nextSibling;
}
return e;
}
use of com.intellij.psi.javadoc.PsiDocTagValue in project intellij-community by JetBrains.
the class RefactoringUtil method fixJavadocsForParams.
public static void fixJavadocsForParams(PsiMethod method, Set<PsiParameter> newParameters, Condition<Pair<PsiParameter, String>> eqCondition, Condition<String> matchedToOldParam) throws IncorrectOperationException {
final PsiDocComment docComment = method.getDocComment();
if (docComment == null)
return;
final PsiParameter[] parameters = method.getParameterList().getParameters();
final PsiDocTag[] paramTags = docComment.findTagsByName("param");
if (parameters.length > 0 && newParameters.size() < parameters.length && paramTags.length == 0)
return;
Map<PsiParameter, PsiDocTag> tagForParam = new HashMap<>();
for (PsiParameter parameter : parameters) {
boolean found = false;
for (PsiDocTag paramTag : paramTags) {
if (parameter.getName().equals(getNameOfReferencedParameter(paramTag))) {
tagForParam.put(parameter, paramTag);
found = true;
break;
}
}
if (!found) {
for (PsiDocTag paramTag : paramTags) {
final String paramName = getNameOfReferencedParameter(paramTag);
if (eqCondition.value(Pair.create(parameter, paramName))) {
tagForParam.put(parameter, paramTag);
found = true;
break;
}
}
}
if (!found && !newParameters.contains(parameter)) {
tagForParam.put(parameter, null);
}
}
List<PsiDocTag> newTags = new ArrayList<>();
for (PsiDocTag paramTag : paramTags) {
final String paramName = getNameOfReferencedParameter(paramTag);
if (!tagForParam.containsValue(paramTag) && !matchedToOldParam.value(paramName)) {
newTags.add((PsiDocTag) paramTag.copy());
}
}
for (PsiParameter parameter : parameters) {
if (tagForParam.containsKey(parameter)) {
final PsiDocTag psiDocTag = tagForParam.get(parameter);
if (psiDocTag != null) {
final PsiDocTag copy = (PsiDocTag) psiDocTag.copy();
final PsiDocTagValue valueElement = copy.getValueElement();
if (valueElement != null) {
valueElement.replace(createParamTag(parameter).getValueElement());
}
newTags.add(copy);
}
} else {
newTags.add(createParamTag(parameter));
}
}
PsiElement anchor = paramTags.length > 0 ? paramTags[0].getPrevSibling() : null;
for (PsiDocTag paramTag : paramTags) {
paramTag.delete();
}
for (PsiDocTag psiDocTag : newTags) {
anchor = anchor != null && anchor.isValid() ? docComment.addAfter(psiDocTag, anchor) : docComment.add(psiDocTag);
}
}
use of com.intellij.psi.javadoc.PsiDocTagValue in project intellij-community by JetBrains.
the class JavaDocCommentFixer method locateCaret.
private static void locateCaret(@NotNull PsiDocComment comment, @NotNull Editor editor, @NotNull PsiFile file) {
Document document = editor.getDocument();
int lineToNavigate = -1;
for (PsiDocTag tag : comment.getTags()) {
PsiElement nameElement = tag.getNameElement();
if (nameElement == null || !CARET_ANCHOR_TAGS.contains(nameElement.getText())) {
continue;
}
boolean good = false;
PsiElement[] dataElements = tag.getDataElements();
if (dataElements != null) {
PsiDocTagValue valueElement = tag.getValueElement();
for (PsiElement element : dataElements) {
if (element == valueElement) {
continue;
}
if (!StringUtil.isEmptyOrSpaces(element.getText())) {
good = true;
break;
}
}
}
if (!good) {
int offset = tag.getTextRange().getEndOffset();
CharSequence text = document.getCharsSequence();
int i = CharArrayUtil.shiftBackward(text, offset - 1, " \t*");
if (i > 0 && text.charAt(i) == '\n') {
offset = i - 1;
}
lineToNavigate = document.getLineNumber(offset);
break;
}
}
if (lineToNavigate >= 0) {
editor.getCaretModel().moveToOffset(document.getLineEndOffset(lineToNavigate));
JavadocNavigationDelegate.navigateToLineEnd(editor, file);
}
}
Aggregations