Search code examples
sublimetext3sublime-syntax

Custom Sublime syntax line break issue with word wrap


I have made myself a custom Sublime syntax for a specific formatting/highlighting which works as expected. However I found out that this syntax does not behave like others (fi. Markdown) with line breaks and word wrapping.

Word wrapping must be enabled for better readability (as it is not for coding but more like a document).

Here is an example: Sublime syntax differences with line break

In the custom syntax, words are split in line breaks, whereas in the markdown they aren't.

I didn't find any option (like content justifying) for sublime syntax or settings to fix this.

Here is a partial sample of the syntax:

%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

[...]


Solution

  • The problem arises because in your default context, you are matching one character at a time. This causes those characters to be seen by Sublime as separate tokens, and word-wrapping is done on a token basis first, therefore it believes it can safely word-wrap them.

    The solution is to match whole words/as many non-formatting characters as you can in a single match.