use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project intellij-community by JetBrains.
the class GroovyFindJarFix method getFqns.
@Override
protected Collection<String> getFqns(@NotNull GrReferenceElement ref) {
GrImportStatement importStatement = PsiTreeUtil.getParentOfType(ref.getElement(), GrImportStatement.class);
//from static imports
if (importStatement != null) {
GrCodeReferenceElement reference = importStatement.getImportReference();
if (reference != null) {
return Collections.singleton(reference.getText());
}
return Collections.emptyList();
}
if (ref.getQualifier() != null)
return Collections.emptyList();
final String className = ref.getReferenceName();
if (className == null)
return Collections.emptyList();
PsiFile file = ref.getContainingFile().getOriginalFile();
if (!(file instanceof GroovyFile))
return Collections.emptyList();
GrImportStatement[] importList = ((GroovyFile) file).getImportStatements();
for (GrImportStatement imp : importList) {
if (className.equals(imp.getImportedName())) {
GrCodeReferenceElement importReference = imp.getImportReference();
if (importReference == null)
return Collections.emptyList();
return Collections.singleton(importReference.getText());
}
}
return Collections.emptyList();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project intellij-community by JetBrains.
the class GroovyDocCompletionProvider method addCompletions.
@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
final PsiElement position = parameters.getPosition();
GrDocMemberReference reference = PsiTreeUtil.getParentOfType(position, GrDocMemberReference.class);
if (reference == null)
return;
GrDocReferenceElement holder = reference.getReferenceHolder();
PsiElement resolved;
if (holder != null) {
GrCodeReferenceElement referenceElement = holder.getReferenceElement();
resolved = referenceElement != null ? referenceElement.resolve() : null;
} else {
resolved = PsiUtil.getContextClass(reference);
}
if (resolved instanceof PsiClass) {
ResolverProcessor propertyProcessor = CompletionProcessor.createPropertyCompletionProcessor(reference);
resolved.processDeclarations(propertyProcessor, ResolveState.initial(), null, reference);
PsiElement[] propertyCandidates = ResolveUtil.mapToElements(propertyProcessor.getCandidates());
ResolverProcessor methodProcessor = CompletionProcessor.createPropertyCompletionProcessor(reference);
resolved.processDeclarations(methodProcessor, ResolveState.initial(), null, reference);
PsiElement[] methodCandidates = ResolveUtil.mapToElements(methodProcessor.getCandidates());
PsiElement[] elements = ArrayUtil.mergeArrays(propertyCandidates, methodCandidates);
for (PsiElement psiElement : elements) {
LookupElement element = GroovyCompletionUtil.createLookupElement((PsiNamedElement) psiElement);
if (psiElement instanceof PsiMethod) {
element = ((LookupElementBuilder) element).withInsertHandler(new GroovyMethodSignatureInsertHandler());
}
result.addElement(element);
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project intellij-community by JetBrains.
the class GroovyInsertHandler method handleInsert.
@Override
public void handleInsert(InsertionContext context, LookupElement item) {
@NonNls Object obj = item.getObject();
PsiSubstitutor substitutor = PsiSubstitutor.EMPTY;
if (obj instanceof GroovyResolveResult) {
substitutor = ((GroovyResolveResult) obj).getSubstitutor();
obj = ((GroovyResolveResult) obj).getElement();
}
if (obj instanceof PsiMethod) {
final PsiMethod method = (PsiMethod) obj;
PsiParameter[] parameters = method.getParameterList().getParameters();
Editor editor = context.getEditor();
Document document = editor.getDocument();
if (context.getCompletionChar() == Lookup.REPLACE_SELECT_CHAR) {
handleOverwrite(editor.getCaretModel().getOffset(), document);
}
CaretModel caretModel = editor.getCaretModel();
int offset = context.getTailOffset();
PsiFile file = context.getFile();
PsiElement elementAt = file.findElementAt(context.getStartOffset());
assert elementAt != null;
PsiElement parent = elementAt.getParent();
if (parent instanceof GrReferenceExpression && ((GrReferenceExpression) parent).getDotTokenType() == GroovyTokenTypes.mMEMBER_POINTER) {
return;
}
CharSequence charsSequence = document.getCharsSequence();
if (isAnnotationNameValuePair(obj, parent)) {
int endOffset = offset;
if (context.getCompletionChar() == Lookup.REPLACE_SELECT_CHAR) {
endOffset = CharArrayUtil.shiftForward(charsSequence, offset, " \t");
if (charsSequence.length() > endOffset && charsSequence.charAt(endOffset) == '=') {
endOffset++;
endOffset = CharArrayUtil.shiftForward(charsSequence, endOffset, " \t");
}
}
document.replaceString(offset, endOffset, " = ");
caretModel.moveToOffset(offset + 3);
return;
}
if (PsiTreeUtil.getParentOfType(elementAt, GrImportStatement.class) != null)
return;
if (parameters.length == 1) {
if ((context.getCompletionChar() != '(' && context.getCompletionChar() != ' ') && TypesUtil.isClassType(parameters[0].getType(), GroovyCommonClassNames.GROOVY_LANG_CLOSURE)) {
int afterBrace;
final int nonWs = CharArrayUtil.shiftForward(charsSequence, offset, " \t");
if (nonWs < document.getTextLength() && charsSequence.charAt(nonWs) == '{') {
afterBrace = nonWs + 1;
} else {
if (isSpaceBeforeClosure(file)) {
document.insertString(offset, " ");
offset++;
}
if (ClosureCompleter.runClosureCompletion(context, method, substitutor, document, offset, parent))
return;
if (context.getCompletionChar() == Lookup.COMPLETE_STATEMENT_SELECT_CHAR) {
//smart enter invoked
document.insertString(offset, "{\n}");
//position caret before '{' for smart enter
afterBrace = offset + 1;
context.setTailOffset(afterBrace);
} else {
document.insertString(offset, "{}");
afterBrace = offset + 1;
}
}
caretModel.moveToOffset(afterBrace);
return;
}
}
context.commitDocument();
if (context.getCompletionChar() == ' ' && MethodParenthesesHandler.hasParams(item, context.getElements(), true, method)) {
return;
}
CommonCodeStyleSettings settings = context.getCodeStyleSettings();
ParenthesesInsertHandler.getInstance(MethodParenthesesHandler.hasParams(item, context.getElements(), true, method), settings.SPACE_BEFORE_METHOD_CALL_PARENTHESES, settings.SPACE_WITHIN_METHOD_CALL_PARENTHESES, true, true).handleInsert(context, item);
AutoPopupController.getInstance(context.getProject()).autoPopupParameterInfo(editor, method);
return;
}
if (obj instanceof PsiClass) {
final PsiClass clazz = (PsiClass) obj;
Editor editor = context.getEditor();
Document document = editor.getDocument();
PsiFile file = PsiDocumentManager.getInstance(clazz.getProject()).getPsiFile(document);
assert file != null;
PsiElement elementAt = file.findElementAt(context.getStartOffset());
assert elementAt != null;
CaretModel caretModel = editor.getCaretModel();
int offset = context.getStartOffset() + elementAt.getTextLength();
final String text = document.getText();
final PsiElement parent = elementAt.getParent();
if (parent instanceof GrCodeReferenceElement && parent.getParent() instanceof GrNewExpression && (offset == text.length() || !text.substring(offset).trim().startsWith("("))) {
document.insertString(offset, "()");
if (GroovyCompletionUtil.hasConstructorParameters(clazz, parent)) {
caretModel.moveToOffset(offset + 1);
return;
}
caretModel.moveToOffset(offset + 2);
return;
}
}
if (context.getCompletionChar() == '=') {
context.setAddCompletionChar(false);
TailType.EQ.processTail(context.getEditor(), context.getTailOffset());
return;
}
if (obj instanceof PsiPackage) {
AutoPopupController.getInstance(context.getProject()).scheduleAutoPopup(context.getEditor());
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project intellij-community by JetBrains.
the class GroovyCodeStyleManagerImpl method getPackageEntryIdx.
protected static int getPackageEntryIdx(@NotNull PackageEntry[] entries, @NotNull GrImportStatement statement) {
final GrCodeReferenceElement reference = statement.getImportReference();
if (reference == null)
return -1;
final String packageName = StringUtil.getPackageName(reference.getCanonicalText());
final boolean isStatic = statement.isStatic();
int best = -1;
int allOtherStatic = -1;
int allOther = -1;
PackageEntry bestEntry = null;
for (int i = 0, length = entries.length; i < length; i++) {
PackageEntry entry = entries[i];
if (entry.isBetterMatchForPackageThan(bestEntry, packageName, isStatic)) {
best = i;
bestEntry = entry;
} else if (entry == PackageEntry.ALL_OTHER_STATIC_IMPORTS_ENTRY) {
allOtherStatic = i;
} else if (entry == PackageEntry.ALL_OTHER_IMPORTS_ENTRY) {
allOther = i;
}
}
if (best >= 0)
return best;
if (isStatic && allOtherStatic != -1)
return allOtherStatic;
return allOther;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project intellij-community by JetBrains.
the class GroovyShellLanguageConsoleView method importToString.
@Nullable
private static String importToString(@NotNull GrImportStatement anImport) {
StringBuilder buffer = new StringBuilder();
GrCodeReferenceElement reference = anImport.getImportReference();
if (reference == null)
return null;
String qname = reference.getClassNameText();
buffer.append(qname);
if (!anImport.isOnDemand()) {
String importedName = anImport.getImportedName();
buffer.append(":").append(importedName);
}
return buffer.toString();
}
Aggregations