问题描述
我需要在许多配置文件的末尾添加一个文本行(txt-like 格式)。所有文件的行都相同。
是否有自动命令对所有文件执行一次?我试图在 IntelliJ replace in files (CTRL + SHIFT + R 窗口) 的选项中进行搜索,但是我找不到任何可以自动执行的操作。
如您所见,所有选项(案例、文件类型等)都有选项,但没有适合我的案例。
解决方法
只需使用在文件中替换(编辑→查找→在文件中替换,或者在Windows和Linux中使用Ctrl+Shift+R / ⌘ +Shift+R 在 Mac 中)替换此正则表达式:
import UIKit
/// `AdjustableImageView` is a `UIImageView` which should have a fixed width or height.
/// It will add an `NSLayoutConstraint` such that it's width/height (aspect) ratio matches the
/// `image` width/height ratio.
class AdjustableImageView: UIImageView {
/// `NSLayoutConstraint` constraining `heightAnchor` relative to the `widthAnchor`
/// with the same `multiplier` as the inverse of the `image` aspect ratio,where aspect
/// ratio is defined width/height.
private var aspectRatioConstraint: NSLayoutConstraint?
/// Override `image` setting constraint if necessary on set
override var image: UIImage? {
didSet {
updateAspectRatioConstraint()
}
}
// MARK: - Init
override init(image: UIImage?) {
super.init(image: image)
setup()
}
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
// MARK: - Setup
/// Shared initializer code
private func setup() {
// Set default `contentMode`
contentMode = .scaleAspectFill
// Update constraints
updateAspectRatioConstraint()
}
// MARK: - Resize
/// De-active `aspectRatioConstraint` and re-active if conditions are met
private func updateAspectRatioConstraint() {
// De-active old constraint
aspectRatioConstraint?.isActive = false
// Check that we have an image
guard let image = image else { return }
// `image` dimensions
let imageWidth = image.size.width
let imageHeight = image.size.height
// `image` aspectRatio
guard imageWidth > 0 else { return }
let aspectRatio = imageHeight / imageWidth
guard aspectRatio > 0 else { return }
// Create a new constraint
aspectRatioConstraint = heightAnchor.constraint(
equalTo: widthAnchor,multiplier: aspectRatio
)
// Activate new constraint
aspectRatioConstraint?.isActive = true
}
}
这样:
(.)\Z
在您需要的任何文件中。仅此而已。
说明:
正则表达式 $1\nThis is your line
匹配并捕获整个文件中的最后一个字符:
-
(.)\Z
匹配并捕获任何字符 -
(.)
匹配 EOF(文件尾)
替换使用它在它之后添加您的行:
-
\Z
使用第一个捕获组(即之前匹配的字符,以避免将其删除) -
$1
引入了一个新行 -
\n
是您要添加的行
您可以使用文件掩码选项来选择要更改的文件:
结果:
,我会这样做:
- 从文件菜单中打开
Settings
并选择Editor > General
。向下滚动并找到On Save
部分并勾选Ensure every saved file ends with a line break
。这应该看起来像这样:
-
然后,在您的项目中,转到
Edit > Find > Replace in Path
或Ctrl+Shift+R
(在 Windows 和 Linux 上)或Shift+Command+R
(在 MacOS 上)。 -
在
Replace in Files
窗口中勾选File mask:
并指定您的配置文件。 注意:您可以使用通配符! *例如,我使用了三个文件config_1.txt config_2.txt config_3.txt
,因此掩码将为config_*.txt
。这是配置文件中的内容:
- 然后选择
Search with regex
- 图钉图标下方右侧的那些.*
。此外,从Directory
中选择In Project Module Directory Scope
。请参阅下面的屏幕截图。
对于 5 和 6 正则表达式信用转到 @walen's answer:
-
在第一个搜索字段中输入
(.)\Z
。 -
键入
$1\n
,然后键入要附加文件的行。例如$1\n yet another config line
。 -
点击
Replace All
。
- 应该附加您的配置文件,如下所示:
- 如果您使用的是 Linux,请省去所有麻烦,只需执行以下操作:
echo "your new line" | tee -a config_*.txt
附注。我已经在 IntelliJ Community 和 PyCharm Professional 上测试了这种方法。