private static void replacePathComponent(@NotNull FileLookup.LookupFile file,
                                           @NotNull DocumentOwner doc,
                                           @NotNull Finder finder,
                                           int caretPos,
                                           int start,
                                           int end) throws BadLocationException {

    doc.removeSelection();

    String name = file.getName();
    boolean toRemoveExistingName;

    if (caretPos >= start) {
      String prefix = doc.getText(start, caretPos - start);
      if (prefix.isEmpty()) {
        prefix = doc.getText(start, end - start);
      }
      if (SystemInfo.isFileSystemCaseSensitive) {
        toRemoveExistingName = name.startsWith(prefix) && !prefix.isEmpty();
      }
      else {
        toRemoveExistingName = StringUtil.toUpperCase(name).startsWith(StringUtil.toUpperCase(prefix)) && !prefix.isEmpty();
      }
    }
    else {
      toRemoveExistingName = true;
    }

    int newPos;
    if (toRemoveExistingName) {
      doc.remove(start, end - start);
      doc.insertString(start, name);
      newPos = start + name.length();
    }
    else {
      doc.insertString(caretPos, name);
      newPos = caretPos + name.length();
    }

    if (file.isDirectory()) {
      if (!finder.getSeparator().equals(doc.getText(newPos, 1))) {
        doc.insertString(newPos, finder.getSeparator());
        newPos++;
      }
    }

    if (newPos < doc.getLength()) {
      if (finder.getSeparator().equals(doc.getText(newPos, 1))) {
        newPos++;
      }
    }
    doc.setCaretPosition(newPos);
  }
private static void replacePathComponent(@NotNull FileLookup.LookupFile file,
                                           @NotNull DocumentOwner doc,
                                           @NotNull Finder finder,
                                           int caretPos,
                                           int start,
                                           int end)