void (InsertionContext context, PsiClass aClass) { if (aClass.getQualifiedName() == null) return; PsiFile file = context.getFile(); int startOffset = context.getStartOffset(); int tail = context.getTailOffset(); PsiJavaCodeReferenceElement ref = PsiTreeUtil.findElementOfClassAtOffset(file, tail - 1, PsiJavaCodeReferenceElement.class, false); boolean goneDeeper = false; while (ref != null) { PsiElement qualifier = ref.getQualifier(); PsiClass outer = aClass.getContainingClass(); if (!Objects.equals(aClass.getName(), ref.getReferenceName())) { if (!JavaPsiFacade.getInstance(context.getProject()).getResolveHelper().isAccessible(aClass, ref, outer)) { // An inner class of non-public superclass is accessed via public subclass: do not rationalize qualifier in this case return; } break; } if (!(qualifier instanceof PsiJavaCodeReferenceElement) || outer == null) break; goneDeeper = true; ref = (PsiJavaCodeReferenceElement)qualifier; aClass = outer; } int newTail = JavaCompletionUtil.insertClassReference(aClass, file, goneDeeper ? ref.getTextRange().getStartOffset() : startOffset, goneDeeper ? ref.getTextRange().getEndOffset() : tail); if (newTail > context.getDocument().getTextLength() || newTail < 0) { LOG.error("Invalid offset after insertion\n" + "offset=" + newTail + "\n" + "start=" + startOffset + "\n" + "tail=" + tail + "\n" + "file.length=" + file.getTextLength() + "\n" + "document=" + context.getDocument() + "\n" + new Throwable(), CoreAttachmentFactory.createAttachment(context.getDocument())); return; } if (!goneDeeper) { context.setTailOffset(newTail); } }