SRa*_*ack 17 newline eof sublimetext sublimetext3 sublime-text-plugin
我正在使用Sublime Text 3,并希望确保在保存文件末尾只有一个新行.目前,90%的时间我的空白完美运行,使用:
"ensure_newline_at_eof_on_save": true
Run Code Online (Sandbox Code Playgroud)
和
"trim_trailing_white_space_on_save": true
Run Code Online (Sandbox Code Playgroud)
...但是,每隔一段时间文件就会在文件末尾保存两个新行.
当保存前最后一个换行符上有空格,配置设置添加换行符,然后删除空格时,就会出现这种情况.在配置中更改这些设置的顺序不能解决此问题.
我无法找到其他原因,所以它可能是唯一的原因,但理想情况下我想检查是否存在多个换行符.
我正在工作的环境未通过测试,除非文件末尾只有一个新行,所以这有点痛苦.我的问题是,是否有一个插件/方式更加严格保存,确保只有一个尾随新行.
mat*_*tst 13
我已经扩展了插件,我在下面发布了很多,现在可以使用Package Control进行安装.
Single Trailing Newline是一个Sublime Text包,可确保文件末尾只有一个尾随换行符.它的工作原理是删除文件末尾的所有空格和换行符(如果有的话),然后插入一个换行符.
可以将插件设置为每次保存文件时自动运行.默认情况下禁用此功能,但通过更改设置,可以为所有文件或仅针对特定语法的文件启用此设置.
提供命令调色板条目以更改包的设置; 添加/删除将触发插件的语法,并允许或阻止插件使用所有语法运行.
这是一个可以执行此操作的插件.
将以下代码保存在带.py扩展名的文件中,例如EnsureExactlyOneTrailingNewLineAtEndOfFileOnSave.py,将文件复制到packages目录中.保存文件时,它将删除文件末尾的所有尾随换行符和空格,然后添加单个尾随换行符.
#
# A Sublime Text plugin to ensure that exactly one trailing
# newline is at the end of all files when files are saved.
#
# License: MIT License
#
import sublime, sublime_plugin
def is_newline_or_whitespace(char):
if char == '\n' or char == '\t' or char == ' ':
return True
else:
return False
class OneTrailingNewLineAtEndOfFileOnSaveListener(sublime_plugin.EventListener):
def on_pre_save(self, view):
# A sublime_plugin.TextCommand class is needed for an edit object.
view.run_command('one_trailing_new_line_at_end_of_file_on_save')
return None
class OneTrailingNewLineAtEndOfFileOnSaveCommand(sublime_plugin.TextCommand):
def run(self, edit):
# Ignore empty files.
if self.view.size() == 0:
return
# Work backwards from the end of the file looking for the last
# significant char (one that is neither whitespace nor a newline).
pos = self.view.size() - 1
char = self.view.substr(pos)
while pos >= 0 and is_newline_or_whitespace(char):
pos -= 1
char = self.view.substr(pos)
# Delete from the last significant char to the end of
# the file and then add a single trailing newline.
del_region = sublime.Region(pos + 1, self.view.size())
self.view.erase(edit, del_region)
self.view.insert(edit, self.view.size(), "\n")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5075 次 |
| 最近记录: |