2017-05-07 138 views
0

我傾向於在聲明(PHP)函數之後並在關閉它之前留出1行空格。使用崇高文本在函數內部進行間距

function foo($bar) { 
    [empty line] 
    do_the_things(); 
    return $something; 
    [empty line] 
} 

對我來說,看起來更清晰。

但我沒有注意到許多其他人這樣做,我想它爲我的代碼弄壞了其他人。

我不知道是否有辦法讓我的編輯剛剛確定函數聲明「函數foo($條){」,只是視覺上留下該行10px的保證金以下,然後尋找收盤「}」並在上面留下10px的餘量?有點像語法突出顯示,但不是突出顯示它會填充它。

回答

1

從構建3118開始,Sublime Text具有一個名爲Phantoms的功能,可用於將內聯HTML內容插入到緩衝區中。我們可以編寫一個插件來使用此功能來創建您想要的填充。 (有沒有其他辦法做到這一點,因爲配色方案不能更改字體大小等,以及line_padding_top/line_padding_bottom偏好影響的所有行。)

  • 工具菜單 - >開發 - >新的插件.. 。
  • 用以下替換模板:
import sublime 
import sublime_plugin 


class FunctionSpacer(sublime_plugin.ViewEventListener): 
    @classmethod 
    def is_applicable(cls, settings): 
     return settings is not None and '/PHP/' in settings.get('syntax', '') 

    spacing = None 

    def __init__(self, view): 
     super().__init__(view) 
     self.spacing = sublime.PhantomSet(view) 
     self.on_modified_async() 

    def on_modified_async(self): 
     regions = list() 

     # find all function names and braces 
     potential_spacing_locations = self.view.find_by_selector('entity.name.function, punctuation.section.block') 
     depth = -1 
     for region in potential_spacing_locations: 
      if self.view.match_selector(region.begin(), 'entity.name.function'): 
       regions.append(region) 
       depth = 0 
      elif depth != -1: 
       for pos in range(region.begin(), region.end()): 
        if self.view.match_selector(pos, 'punctuation.section.block.begin') and depth != -1: 
         depth += 1 
        elif self.view.match_selector(pos, 'punctuation.section.block.end'): 
         depth -= 1 
         if depth == 0: 
          row, col = self.view.rowcol(region.begin()) 
          regions.append(sublime.Region(self.view.text_point(row - 1, col))) 
          depth = -1 

     phantoms = [sublime.Phantom(region, '<br style="font-size: 10pt" />', sublime.LAYOUT_BELOW) for region in regions] 
     self.spacing.update(phantoms) 
  • 保存ST建議的文件夾中,如function_spacing.py

它是如何工作的:

  • 語法何時被設置爲PHP和緩衝區被修改(或第一次加載插件),它會發現所有的函數定義在文件中,並通過他們一個去由一個:
  • 它增加了該函數定義
  • 它然後通過所有相關大括號前進到確定當函數結束下方的幻影,並增加了一個幻影到關閉功能的大括號上面的行

因爲它適用於語法高亮引擎,所以它非常高效,不必重新實現任何相同的邏輯,如忽略字符串中的括號或註釋等。