You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自定义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,进一步加剧了单词拆分的问题。

修复步骤

  1. 修改根scope类型
    将语法文件中的scope: source.scripttest改为scope: text.scripttest,让Sublime将该语法识别为文本类而非代码类。

  2. 修正单词匹配规则
    在default上下文里,把单个字符匹配改为连续单词匹配:

default:
  - match: '\p{P}'
    scope: text.plain
  - match: '\w+'  # 匹配整个连续单词
    scope: text.plain

这样Sublime会把完整单词识别为单个token,自动换行时不会在中间拆分。

  1. 确认自动换行设置
    确保用户设置中开启正确的自动换行参数:
{
  "word_wrap": true,
  "wrap_width": 0,  # 0表示自动适配窗口宽度
  "draw_centered": false  # 保持左对齐,符合文档阅读习惯
}

内容的提问来源于stack exchange,提问作者Tot

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 17:43:13