带有 Sublime Text 的函数内部的间距
Spacing inside a function with Sublime Text
我倾向于在 (PHP) 函数声明之后和关闭函数之前留下 1 行 space。
function foo($bar) {
[empty line]
do_the_things();
return $something;
[empty line]
}
对我来说,阅读起来似乎更清晰。
但我没有注意到很多其他人这样做,我猜这让我的代码对其他人来说很丑陋。
我想知道是否有办法让我的编辑器只识别函数声明 "function foo($bar) {" 并在视觉上在该行下方留出 10px 的边距,然后寻找关闭 "}" 并在其上方留出 10px 的边距?有点像语法高亮,但不是高亮而是填充它。
从 build 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并且缓冲区被修改(或插件首次加载)时,它会找到文件中的所有函数定义并逐一遍历:
- 它在函数定义下面添加了一个 Phantom
- 然后它遍历所有相关的花括号以确定函数的结束位置,并在关闭函数的花括号上方的行中添加一个 Phantom
因为它在语法高亮引擎上工作,所以非常高效,并且不必重新实现任何相同的逻辑,例如忽略字符串或注释中的大括号等。
我倾向于在 (PHP) 函数声明之后和关闭函数之前留下 1 行 space。
function foo($bar) {
[empty line]
do_the_things();
return $something;
[empty line]
}
对我来说,阅读起来似乎更清晰。
但我没有注意到很多其他人这样做,我猜这让我的代码对其他人来说很丑陋。
我想知道是否有办法让我的编辑器只识别函数声明 "function foo($bar) {" 并在视觉上在该行下方留出 10px 的边距,然后寻找关闭 "}" 并在其上方留出 10px 的边距?有点像语法高亮,但不是高亮而是填充它。
从 build 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并且缓冲区被修改(或插件首次加载)时,它会找到文件中的所有函数定义并逐一遍历:
- 它在函数定义下面添加了一个 Phantom
- 然后它遍历所有相关的花括号以确定函数的结束位置,并在关闭函数的花括号上方的行中添加一个 Phantom
因为它在语法高亮引擎上工作,所以非常高效,并且不必重新实现任何相同的逻辑,例如忽略字符串或注释中的大括号等。