【问题标题】:Fill line up with special Character before and after Text在文本前后填充特殊字符
【发布时间】:2014-10-02 10:54:45
【问题描述】:

我想创建一个 Sublime Text 2 Snippet,用空格填充我键入的变量前后的空间。

应该是这样的:

===========================my_filename.js===========================

文件名应该居中,因此文本前后的空格数必须匹配。 我还需要这条线的整体列宽保持不变。因此,当我添加 2 个字符时,每边的空格数会减少一个。

我认为这方面的一个示例是:

spacesLeft  = roundDown((columnWidth/2) - (textSize/2))
spacesRight = roundUp((columnWidth/2) - (textSize/2))

但由于 Sublime Snippets 中只有 RegEx 可用,我认为我无法完成此任务。

Vintagmode 能以任何方式帮助我吗?

感谢您的帮助!

【问题讨论】:

    标签: code-snippets sublimetext


    【解决方案1】:

    因为 sn-ps 本质上是静态的,所以在这种情况下它们无法为您提供帮助。但是,您可以创建一个插件来相对轻松地执行此操作。 Sublime Text 使用 python 作为其插件。您可以通过转到工具 > 新插件来创建一个。 ST2 的 API 可以在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 += '='
    

    然后将当前行(self.view.line(sel))替换为signs + sel + signs

    【讨论】:

    • sel = " " + re.search("[^/]+$", self.view.file_name()).group(0) + " " 完成了这项工作:)
    猜你喜欢
    • 1970-01-01
    • 2011-09-23
    • 2020-06-03
    • 1970-01-01
    • 2011-12-20
    • 1970-01-01
    • 2015-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多