String (PsiElement element, String name, int maxWidth, FontMetrics fm) { String presentablePath = PSIRenderingUtils.extractPresentablePath(element); String text = ObjectUtils.chooseNotNull(presentablePath, SymbolPresentationUtil.getSymbolContainerText(element)); if (text == null || text.equals(name)) return null; text = PSIRenderingUtils.normalizePsiElementContainerText(element, text, presentablePath); boolean in = text.startsWith("in "); if (in) text = text.substring(3); String left = in ? "in " : ""; String adjustedText = left + text; if (maxWidth < 0) return adjustedText; int fullWidth = fm.stringWidth(adjustedText); if (fullWidth < maxWidth) return adjustedText; String separator = text.contains("/") ? "/" : SystemInfo.isWindows && text.contains("\\") ? "\\" : text.contains(".") ? "." : text.contains("-") ? "-" : " "; LinkedList<String> parts = new LinkedList<>(StringUtil.split(text, separator)); int index; while (parts.size() > 1) { index = parts.size() / 2 - 1; parts.remove(index); if (fm.stringWidth(left + StringUtil.join(parts, separator) + "...") < maxWidth) { parts.add(index, "..."); return left + StringUtil.join(parts, separator); } } int adjustedWidth = Math.max(adjustedText.length() * maxWidth / fullWidth - 1, left.length() + 3); return StringUtil.trimMiddle(adjustedText, adjustedWidth); }