[英]Search and replace in current file with Sublime Text plugin
I am trying to port a Sublime Text build system to a plugin. 我正在尝试将Sublime Text构建系统移植到插件。
The build system would receive the current file and go through it with this code: 构建系统将接收当前文件,并使用以下代码对其进行遍历:
for line in fileinput.input(inplace=1):
sys.stdout.write(makeReplacements(line))
Now, in the plugin syntax I go the fact that the way to get my current file's content is: 现在,在插件语法中,我得出这样一个事实,即获取当前文件内容的方式是:
input = self.view.substr(
sublime.Region(0, self.view.size())
)
But now I'm not sure about what I should do about the next operation. 但是现在我不确定下一步应该怎么做。
for line in input(inplace=1):
How could I make replacements in the file on-the-fly and then save it? 如何即时替换文件,然后保存?
I don't think the Sublime Text plugin API can save the buffer, but you could use the file_name()
method in the sublime.View class and work with the file directly.我认为Sublime Text插件API无法保存缓冲区,但是您可以在sublime.View类中使用 file_name()
方法并直接使用该文件。
As noted by @MattDMo, the file can be saved using view.run_command('save')
. 如@MattDMo所述,可以使用view.run_command('save')
保存文件。
It may be easier to use the file name if your old build file worked with that. 如果您的旧版本文件可以使用该文件名,则使用它可能会更容易。
As stated by @RazerM, the first argument has to be the file path. 如@RazerM所述,第一个参数必须是文件路径。 So for my example, this will work. 因此,对于我的示例,这将起作用。
for line in fileinput.input(self.view.file_name(), inplace=1):
sys.stdout.write(self.makeReplacements(line))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.