自定义Sublime语法换行与自动换行异常问题求助
自定义Sublime语法自动换行拆分单词的解决方法
我为特定格式高亮需求制作了自定义Sublime语法(ScriptTest),功能正常,但自动换行时会拆分单词,而Markdown语法不会。由于是文档类内容,必须开启自动换行提升可读性,未找到相关设置修复,以下是部分语法代码:
%YAML 1.2 --- # http://www.sublimetext.com/docs/3/syntax.html name: ScriptTest file_extensions: - scripttest scope: source.scripttest contexts: main: - include: comment - include: string - include: title - include: bold - include: key - include: number - include: punct - include: mark - include: tag - include: default default: - match: '\p{P}' scope: text.plain - match: '\w' scope: text.plain punct: - match: '(\+ |- |= |> |< |-> |<- |=> |<= )' scope: punctuation tag: - match: '@[\w]+' scope: meta.tag - match: '\#[\w]+' scope: meta.tag.hash [...]
问题核心
Sublime的自动换行逻辑和语法根scope类型直接绑定:
source.xxx类scope默认按代码逻辑换行,允许拆分单词;text.xxx类scope按文本段落逻辑换行,会保留单词完整性。
同时你当前的default规则单个匹配\w(单个单词字符),导致Sublime把每个字符视为独立token,进一步加剧了单词拆分的问题。
修复步骤
修改根scope类型
将语法文件中的scope: source.scripttest改为scope: text.scripttest,让Sublime将该语法识别为文本类而非代码类。修正单词匹配规则
在default上下文里,把单个字符匹配改为连续单词匹配:
default: - match: '\p{P}' scope: text.plain - match: '\w+' # 匹配整个连续单词 scope: text.plain
这样Sublime会把完整单词识别为单个token,自动换行时不会在中间拆分。
- 确认自动换行设置
确保用户设置中开启正确的自动换行参数:
{ "word_wrap": true, "wrap_width": 0, # 0表示自动适配窗口宽度 "draw_centered": false # 保持左对齐,符合文档阅读习惯 }
内容的提问来源于stack exchange,提问作者Tot
相关产品推荐
相关产品推荐

