我想创建一个Sublime Text 2代码片段,用空格填充我键入的变量前后的空格。
它应该看起来像这样:
===========================my_filename.js===========================文件名应居中,以便文本前后的空格数量必须匹配。另外,我需要这一行的总列宽保持不变。因此,当我添加2个字符时,两边的空格数量就减少了1。
我认为这方面的示例如下:
spacesLeft = roundDown((columnWidth/2) - (textSize/2))
spacesRight = roundUp((columnWidth/2) - (textSize/2))但是因为只有RegEx在Sublime代码片段中可用,所以我不认为我能够完成这项任务。
Vintagmode能以任何方式帮助我吗?
谢谢你的帮忙!
发布于 2012-03-15 05:35:21
因为代码片段本质上是静态的,所以在这种情况下它们不能帮助您。但是,您可以创建一个插件来相对容易地做到这一点。Sublime Text使用python作为插件。您可以转到Tools > New Plugin来创建一个插件。St2的应用程序接口可以在here上找到,并且非常令人印象深刻。实际上,您需要使用以下命令存储当前选择(您的变量
sel_reg = self.view.sel()[0] # the current selection 'Region'
sel = self.view.substr(sel_reg) # the text within the 'Region'然后生成=
signs = ''
each_side = len(80 - len(sel))/2 # 80 is the standard window length,
# change this to what you want.
# Not sure about how to make it dynamic.
for x in xrange(each_side):
signs += '='然后用signs + sel + signs替换当前行(self.view.line(sel))。
https://stackoverflow.com/questions/9699507
复制相似问题