Author Topic: Syntax Highlighting .list Files with Sublime Text  (Read 2448 times)

0 Members and 1 Guest are viewing this topic.

Offline cvoight

  • Newbie
  • *
  • Posts: 18
  • Last Login:June 18, 2020, 02:31:07 pm
Syntax Highlighting .list Files with Sublime Text
« on: January 03, 2020, 09:46:45 pm »
Changelog (refer to linked posts for newest files)

I use (the fantastic) Sublime Text as my text editor, and got a little tired of staring at white text in my .list file. So, I cobbled together a little .sublime-syntax file that provides some rudimentary syntax highlighting for TM .list files (mostly by mangling the scope naming conventions, but it's a nice shortcut to adjusting a color scheme file). I do not know whether it works with Sublime Merge. I hope someone finds it useful :)

Here's a (very scaled down) picture:



Features:
  • Comments (lines beginning with '#' but not '#--' or '#{xx}') appear as normally in syntax highlighted languages, gray. Two 'special' comment lines which I personally use are defined below.
  • Entries are matched in their entirety -- a partially completed entry does not get syntax highlighting and appears plain white while a complete entry is highlighted. A complete entry is defined (per my reading) by 4 sets of non-whitespace characters each separated by a single whitespace character. Region is colored like a function, Highway is colored like a keyword, and Waypoints are colored like strings.
  • '#{xx}' (where xx is anything except whitespace, including nothing -- i.e. '#{}') is highlighted like a Markdown heading. With the Monokai color scheme, this means the '#{xx}' prefix is bold orange while the rest of the line is bold white. I use this as a searchable section heading.
  • '#--' is highlighted like a language constant (darkish blue in Monokai). I use this as a searchable subsection heading.
You don't have to use the 'special' comments or can delete them from the YAML file below.

To get syntax highlighting, download the attached files and place them in %APPDATA%\Sublime Text 3\Packages\User\ (or an equivalent for OS X or Linux). The .tmPreferences file is required if you want to toggle comments using Ctrl + /. I have also placed the content of the files in code blocks so they are accessible if someone isn't logged in to the forums.

Code: (List.sublime-syntax) [Select]
%YAML 1.2
---
# See http://www.sublimetext.com/docs/3/syntax.html
name: List (TravelMapping)
file_extensions:
  - list
scope: text.list
comment: |-
  this syntax document makes TM .list files prettier

contexts:
  # The prototype context is prepended to all contexts but those setting
  # meta_include_prototype: false.
  prototype:
    - include: comments

  main:
    # \S matches non-whitespace, \s matches whitespace.
    - match: '^(\S+)\s(\S+)\s(\S+)\s(\S+)'
      captures:
        1: support.function     # Region.     could create entity.name.region
        2: keyword              # Highway.    could create entity.name.highway
        3: string               # Waypoint 1. could create entity.name.waypoint.one
        4: string               # Waypoint 2. could create entity.name.waypoint.two

  comments:
    # Comments begin with a '#' and finish at the end of the line.
    # Heading 1
    - match: '#{\S*}'
      scope: punctuation.definition.heading
      push:
        - meta_scope: markup.heading
        - match: \n
          pop: true
    # Heading 2
    - match: '#--'
      scope: punctuation.definition.heading
      push:
        - meta_scope: constant.language
        - match: \n
          pop: true
    # Normal comments
    - match: '#'
      scope: punctuation.definition.comment
      push:
        - meta_scope: comment.line.number-sign
        - match: \n
          pop: true

Code: (List.tmPreferences) [Select]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>name</key>
  <string>Comments</string>
  <key>scope</key>
  <string>text.list</string>
  <key>settings</key>
  <dict>
    <key>shellVariables</key>
    <array>
      <dict>
        <key>name</key>
        <string>TM_COMMENT_START</string>
        <key>value</key>
        <string># </string>
      </dict>
    </array>
  </dict>
</dict>
</plist>
« Last Edit: January 10, 2020, 04:28:55 pm by cvoight »

Offline cvoight

  • Newbie
  • *
  • Posts: 18
  • Last Login:June 18, 2020, 02:31:07 pm
Re: Syntax Highlighting with Sublime Text
« Reply #1 on: January 10, 2020, 04:21:34 pm »
I have overhauled the .sublime-syntax file to provide some rudimentary error-checking (via syntax highlighting) for US Interstate and US Route highway & waypoint fields. Namely, US Interstates have a hyphen between the interstate prefix I and the interstate number while US Routes do not have any characters between the route prefix US and the route number. This formatting is applied only to lines that start with a region that's a US state or territory. As a result of this change, entries in a list file are no longer highlighted in their entirety and are matched as they are typed. No changes have been made to comments. An example of what this looks like:



As previously, this file is placed in %APPDATA%\Sublime Text 3\Packages\User\ (or an equivalent for OS X or Linux). No changes were made to the .tmPreferences file.

Code: (List.sublime-syntax) [Select]
%YAML 1.2
---
# Last modified: 2020-01-10
# See http://www.sublimetext.com/docs/3/syntax.html
name: List (TravelMapping)
file_extensions:
  - list
scope: text.list
comment: |-
  this syntax document makes TM .list files prettier

contexts:
  # The prototype context is prepended to all contexts but those setting
  # meta_include_prototype: false.
  prototype:
    - include: comments

  main:
    - match: '^(?=A[KLRSZ]\b|C[AOT]\b|D[CE]\b|F[L]\b|G[AU]\b|H[I]\b|I[ADLN]\b|K[SY]\b|L[A]\b|M[ADEINOPST]\b|N[CDEHJMVY]\b|O[HKR]\b|P[AR]\b|R[I]\b|S[CD]\b|T[NX]\b|U[T]\b|V[AIT]\b|W[AIVY]\b)'
      push: region_usa
    - match: '^'
      push: region

  region:
    - meta_scope: support.function
    - match: '\s'
      set: highway
    - match: '$'
      pop: true

  highway:
    - meta_scope: keyword
    - match: '\s'
      set: waypoint_one
    - match: '$'
      pop: true

  waypoint_one:
    - meta_scope: string
    - match: '\s'
      set: waypoint_two
    - match: '$'
      pop: true

  waypoint_two:
    - meta_scope: string
    - match: '\s'
      pop: true
    - match: '$'
      pop: true

  region_usa:
    - meta_scope: support.function
    - match: '\s'
      set: highway_usa
    - match: '$'
      pop: true

  highway_usa:
    - meta_scope: keyword
    - match: 'US'
      push: check_us_route
    - match: 'I'
      push: check_interstate
    - match: '\s'
      set: waypoint_one_usa
    - match: '$'
      pop: true

  waypoint_one_usa:
    - meta_scope: string
    - match: 'US'
      push: check_us_route
    - match: 'I'
      push: check_interstate
    - match: '\s'
      set: waypoint_two_usa
    - match: '$'
      pop: true

  waypoint_two_usa:
    - meta_scope: string
    - match: 'US'
      push: check_us_route
    - match: 'I'
      push: check_interstate
    - match: '\s'
      pop: true
    - match: '$'
      pop: true

  check_us_route:
    - match: '-'
      scope: invalid.illegal
    - match: '.'
      pop: true
    - match: '$'
      pop: true

  check_interstate:
    - match: '[0-9]'
      scope: invalid.illegal
    - match: '.'
      pop: true
    - match: '$'
      pop: true

  comments:
    # Comments begin with a '#' and finish at the end of the line.
    # Heading 1
    - match: '#{\S*}'
      scope: punctuation.definition.heading
      push:
        - meta_scope: markup.heading
        - match: \n
          pop: true
    # Heading 2
    - match: '#--'
      scope: punctuation.definition.heading
      push:
        - meta_scope: constant.language
        - match: \n
          pop: true
    # Normal comments
    - match: '#'
      scope: punctuation.definition.comment
      push:
        - meta_scope: comment.line.number-sign
        - match: \n
          pop: true
« Last Edit: January 10, 2020, 04:24:01 pm by cvoight »

Offline yakra

  • TM Collaborator
  • Hero Member
  • *****
  • Posts: 2849
  • Last Login:Today at 12:55:55 pm
Re: Syntax Highlighting .list Files with Sublime Text
« Reply #2 on: March 15, 2020, 06:17:31 pm »
Nifty stuff here. Belated welcome to the forum!
I wasn't aware of Sublime Merge before; I'll have to give it a try.