PsiElement (Editor editor, PsiFile file, TextRange toDuplicate) { int nonWs = CharArrayUtil.shiftForward(editor.getDocument().getCharsSequence(), toDuplicate.getStartOffset(), "\n\t "); PsiElement psi = file.findElementAt(nonWs); PsiElement named = null; while (psi != null) { TextRange range = psi.getTextRange(); if (range == null || psi instanceof PsiFile || !toDuplicate.contains(psi.getTextRange())) { break; } if (psi instanceof PsiNameIdentifierOwner) { named = ((PsiNameIdentifierOwner)psi).getNameIdentifier(); } psi = psi.getParent(); } return named; }