在 PHPStorm 中重命名文件而不重构

发布于 2024-12-10 13:50:28 字数 96 浏览 0 评论 0原文

如何在 PHPStorm 中不重构快速重命名文件名?即使在我刚刚创建文件之后,PHPStorm 也会花费很长时间来搜索我的文件以了解其使用情况。

How do I quickly rename a file name without refactoring in PHPStorm? Even after I've just created the file, PHPStorm takes a long time to search through my files for its usage.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

这个俗人 2024-12-17 13:50:28

我快速重命名文件的方法是单击文件名并按 Shift-F6。将打开一个对话框,您可以立即键入文件的新名称,然后按回车/回车键完成重命名。

我发现这种技术很直观,因为您不必将鼠标移动到对话框窗口、键入新名称或单击任何内容进行确认。只要您所做的只是一个您知道是安全的重命名,您只需输入它即可。

我也喜欢这个功能,因为您仍然可以选择在进行更改之前快速预览更改。在这些情况下,当您重构可能对应用程序其余部分产生影响的内容时,您仍然可以启用“搜索引用”和/或“在注释和字符串中搜索”来获取整个辣酱玉米饼馅。 (我默认禁用这些选项。)

我同意您的观点,右键单击文件名,向下滚动到“重构”,选择“重命名”,然后输入新名称很麻烦,特别是在启用了安全检查的情况下。我发现 PhpStorm 开发人员似乎理解这些类型的问题,并且通常也会想到一种“更快的方法”来完成任务。

My technique for renaming files quickly is to single-click the file name and press Shift-F6. A dialog opens and you can immediately type the new name of the file and hit return/enter to complete the rename.

I find this technique intuitive because you don't have to move your mouse to the dialog window, type the new name, or click anything to confirm. As long as all you're doing is a rename that you know is safe, you just type it and go.

I also like this feature because you still have the option of quickly previewing the changes before they're made. And on those occasions when you are refactoring something with possible implications to the rest of your app, you can still enable "Search for references" and/or "Search in comments and strings" to get the whole enchilada. (I have those options disabled by default.)

I agree with you that right-clicking the file name, scrolling down to Refactor, selecting Rename, and then entering the new name is cumbersome, especially if the safe checks are enabled. I find that PhpStorm developers seem to understand these types of issues and usually have thought of a "faster way" to do things as well.

将军与妓 2024-12-17 13:50:28

尽管 Refactor | 不重构就无法做到这一点。 重命名通常非常快,您可以禁用搜索参考的选项。

You can't do it without refactoring, though Refactor | Rename is usually very quick and you can disable options to search for references.

半枫 2024-12-17 13:50:28

默认为Shift-F6。如果不重构,请禁用弹出窗口中的复选框。

我想像 Windows 默认那样使用 F2 进行文件重命名。

您可以创建F2的快捷方式。转到:

  1. 文件
  2. 设置
  3. 键盘
  4. 映射搜索“重命名”
  5. 双击“重构->重命名”
  6. 单击“键盘快捷键”
  7. 按F2
  8. 确定

编辑

如果它不能立即工作,请重新启动PHPStorm。

Default is Shift-F6. For not refactoring, disable the checkboxes in the popup.

I wanted to use F2 for file renaming like Windows default.

You can make a shortcut for F2. Go to:

  1. File
  2. Settings
  3. Keymap
  4. Search for "rename"
  5. Doubleclick "Refactor -> Rename"
  6. Click "Keyboard Shortcut"
  7. Press F2
  8. Ok

EDIT

If it does not work immediately, restart PHPStorm.

被翻牌 2024-12-17 13:50:28

还有一个可用选项右键单击文件选择“在资源管理器中显示”,然后在 Windows 资源管理器中按 F2 重命名文件。它不会重构或影响项目中的任何其他文件。

There is also an option available Right-click on file select "Show In Explorer" and then in windows explorer rename file by F2. It will not refactor or effect any other file in your project.

不顾 2024-12-17 13:50:28

这适用于 Linux 用户。

它取决于zenity,因此首先检查它是否安装在您的系统上,
在控制台中运行:zenity --version
如果它输出类似 3.18.1.1 的内容 - 那么就可以了。

创建包含以下内容的文件 idea-rename.sh

#!/usr/bin/env bash

FileDir="$1"
FileName="$2"

NewFileName="$(zenity --entry --title "Rename ${FileName}" --text "Please enter new name" --entry-text "${FileName}" --ok-label="Rename" 2>/dev/null)"

if [[ "${NewFileName}" != "" ]]; then
    if [[ -f "${FileDir}/${FileName}" ]]; then
        path="${FileDir}"
        name="${NewFileName}"

        if [[ "${NewFileName}" == *"/"* ]]; then
            IFS='/' read -ra newDirs <<< "$NewFileName"
            IFS='/' read -ra dirs <<< "$FileDir"

            last=$((${#newDirs[@]} - 1))
            path=""
            name="${newDirs[$last]}"
            unset 'newDirs[last]'

            if [[ "${NewFileName}" == *"./"* ]]; then
                for i in "${!newDirs[@]}"; do
                    dir="${newDirs[i]}"

                    if [[ "$dir" == ".." ]]; then
                        unset 'newDirs[i]'
                        j=$i

                        while [ $j -ge 0 ]; do
                            j=$(($j-1))

                            if [ $j -lt 0 ]; then
                                for (( k=${#dirs[@]}-1 ; k>=0 ; k-- )); do
                                    if [[ "${dirs[k]}" ]]; then
                                        echo 'dirs[k]'
                                        unset 'dirs[k]'
                                        break
                                    fi
                                done
                            elif [[ "${newDirs[j]}" ]]; then
                                echo 'newDirs[j]'
                                unset 'newDirs[j]'
                                break
                            fi
                        done

                    elif [[ "$dir" == "." ]]; then
                        unset 'newDirs[i]'
                    fi
                done
            fi

            for dir in "${dirs[@]}"; do
                if [[ "$dir" ]]; then
                    path="${path}/${dir}"

                    if [[ ! -d "$path" ]]; then
                        mkdir -m 0777 "$path"
                    fi
                fi
            done

            for dir in "${newDirs[@]}"; do
                if [[ "$dir" ]]; then
                    path="${path}/${dir}"

                    if [[ ! -d "$path" ]]; then
                        mkdir -m 0777 "$path"
                    fi
                fi
            done
        fi

        if [[ ! -f "${FileDir}/${NewFileName}" ]]; then
            mv "${FileDir}/${FileName}" "${path}/${name}"
        fi
    fi
fi

打开您的 Settings > 工具 > 外部工具,点击 添加按钮
并填写如下表格

添加工具

名称:重命名
组:文件
程序:/bin/bash
参数:/path/to/idea-rename.sh $FileDir$ $FileName$
工作目录:$ProjectFileDir$

单击确定应用
之后,您可以在文件菜单中看到新项目

Menu

当您单击重命名时,将打开对话框

重命名对话框

重命名后在某些情况下 phpstorm 不会同步文件,因此在路径中文件所在位置需要单击文件菜单“同步'路径'”

同步路径菜单

This will work for linux users.

It depends on zenity, so first check if it installed on your system,
run in console: zenity --version
If it output something like 3.18.1.1 - then it's ok.

Create file idea-rename.sh with content below:

#!/usr/bin/env bash

FileDir="$1"
FileName="$2"

NewFileName="$(zenity --entry --title "Rename ${FileName}" --text "Please enter new name" --entry-text "${FileName}" --ok-label="Rename" 2>/dev/null)"

if [[ "${NewFileName}" != "" ]]; then
    if [[ -f "${FileDir}/${FileName}" ]]; then
        path="${FileDir}"
        name="${NewFileName}"

        if [[ "${NewFileName}" == *"/"* ]]; then
            IFS='/' read -ra newDirs <<< "$NewFileName"
            IFS='/' read -ra dirs <<< "$FileDir"

            last=$((${#newDirs[@]} - 1))
            path=""
            name="${newDirs[$last]}"
            unset 'newDirs[last]'

            if [[ "${NewFileName}" == *"./"* ]]; then
                for i in "${!newDirs[@]}"; do
                    dir="${newDirs[i]}"

                    if [[ "$dir" == ".." ]]; then
                        unset 'newDirs[i]'
                        j=$i

                        while [ $j -ge 0 ]; do
                            j=$(($j-1))

                            if [ $j -lt 0 ]; then
                                for (( k=${#dirs[@]}-1 ; k>=0 ; k-- )); do
                                    if [[ "${dirs[k]}" ]]; then
                                        echo 'dirs[k]'
                                        unset 'dirs[k]'
                                        break
                                    fi
                                done
                            elif [[ "${newDirs[j]}" ]]; then
                                echo 'newDirs[j]'
                                unset 'newDirs[j]'
                                break
                            fi
                        done

                    elif [[ "$dir" == "." ]]; then
                        unset 'newDirs[i]'
                    fi
                done
            fi

            for dir in "${dirs[@]}"; do
                if [[ "$dir" ]]; then
                    path="${path}/${dir}"

                    if [[ ! -d "$path" ]]; then
                        mkdir -m 0777 "$path"
                    fi
                fi
            done

            for dir in "${newDirs[@]}"; do
                if [[ "$dir" ]]; then
                    path="${path}/${dir}"

                    if [[ ! -d "$path" ]]; then
                        mkdir -m 0777 "$path"
                    fi
                fi
            done
        fi

        if [[ ! -f "${FileDir}/${NewFileName}" ]]; then
            mv "${FileDir}/${FileName}" "${path}/${name}"
        fi
    fi
fi

Open your Settings > Tools > External Tools, click on add button
and fill form as below

Adding tool

Name: Rename
Group: File
Program: /bin/bash
Parameters: /path/to/idea-rename.sh $FileDir$ $FileName$
Working Directory: $ProjectFileDir$

Click Ok, Apply
After that you can see new item in your file menu

Menu

When you click on rename will opens dialog

Rename dialog

After rename in some cases phpstorm not sync files, so in path where located your file need to click in file menu "Synchronize 'path'"

Synchronize path menu

独留℉清风醉 2024-12-17 13:50:28

我喜欢 Andrey Izman 的 答案,但 bash 脚本的复杂性让我感到惊讶。
所以我又做了一个:

#!/usr/bin/env bash

FILEPATH="$1"

if [ ! "$FILEPATH" ]; then
  zenity --warning --text "I do require path to the file (or directory) to be renamed, as a first script argument"
  exit 1
fi

if [[ ! "$FILEPATH" == /* ]]; then
   # relative path, prefix with current directory
   FILEPATH="$PWD/$FILEPATH"
fi

if [ ! -e "$FILEPATH" ]; then
  zenity --warning --text "File (or a directory) '$FILEPATH' does not exists!"
  exit 1
fi

FILENAME=$(basename "$FILEPATH")
DIRNAME=$(dirname "$FILEPATH")

NEWFILENAME="$(zenity --entry --title "Rename $FILENAME" --text "Please enter new name for $FILENAME" --entry-text "$FILENAME" --ok-label="Accept & rename" 2>/dev/null)"

if [ ! "$NEWFILENAME" ] || [ "$FILENAME" == "$NEWFILENAME" ]; then
  # Escape hit, "Cancel" button pressed, new file name is the same
  # as the current one or new filename not given. Quit. Just quit.
  exit 0
fi

if [ -e "$DIRNAME/$NEWFILENAME" ]; then
  zenity --warning --text "Will not rename $FILENAME into $NEWFILENAME as file/directory '$NEWFILENAME' already exists!"
  exit 1
fi

# Finally, the checks are over and the rename can be performed:
mv "$FILEPATH" "$DIRNAME/$NEWFILENAME"

绑定到 IDE 略有不同:
JetBrains 外部工具设置模板

请注意 Arguments 设置中的差异:引号中的单个变量。

另请注意,该脚本也可以独立使用(即不在 JetBrains IDE 中)。

I like Andrey Izman's answer, but the bash script surprised me with its complexity.
So I made another one:

#!/usr/bin/env bash

FILEPATH="$1"

if [ ! "$FILEPATH" ]; then
  zenity --warning --text "I do require path to the file (or directory) to be renamed, as a first script argument"
  exit 1
fi

if [[ ! "$FILEPATH" == /* ]]; then
   # relative path, prefix with current directory
   FILEPATH="$PWD/$FILEPATH"
fi

if [ ! -e "$FILEPATH" ]; then
  zenity --warning --text "File (or a directory) '$FILEPATH' does not exists!"
  exit 1
fi

FILENAME=$(basename "$FILEPATH")
DIRNAME=$(dirname "$FILEPATH")

NEWFILENAME="$(zenity --entry --title "Rename $FILENAME" --text "Please enter new name for $FILENAME" --entry-text "$FILENAME" --ok-label="Accept & rename" 2>/dev/null)"

if [ ! "$NEWFILENAME" ] || [ "$FILENAME" == "$NEWFILENAME" ]; then
  # Escape hit, "Cancel" button pressed, new file name is the same
  # as the current one or new filename not given. Quit. Just quit.
  exit 0
fi

if [ -e "$DIRNAME/$NEWFILENAME" ]; then
  zenity --warning --text "Will not rename $FILENAME into $NEWFILENAME as file/directory '$NEWFILENAME' already exists!"
  exit 1
fi

# Finally, the checks are over and the rename can be performed:
mv "$FILEPATH" "$DIRNAME/$NEWFILENAME"

Binding into IDE is slightly different though:
JetBrains External tools settings template

Note the difference in Arguments setting: single variable in quotes.

Note also that this script can also be used standalone (i.e. not within the JetBrains IDE).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文