Travel Mapping

User Discussions => How To? => Topic started by: cvoight on January 03, 2020, 09:46:45 pm

Title: Syntax Highlighting .list Files with Sublime Text
Post by: cvoight on January 03, 2020, 09:46:45 pm
Changelog (refer to linked posts for newest files)

I use (the fantastic) Sublime Text (https://www.sublimetext.com/) 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 (https://www.sublimetext.com/docs/3/scope_naming.html), but it's a nice shortcut to adjusting a color scheme file). I do not know whether it works with Sublime Merge (https://www.sublimemerge.com/). I hope someone finds it useful :)

Here's a (very scaled down) picture:

(https://i.imgur.com/xJjIjTa.png)

Features:
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>
Title: Re: Syntax Highlighting with Sublime Text
Post by: cvoight 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:

(https://i.imgur.com/Ab7CwAV.png)

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
Title: Re: Syntax Highlighting .list Files with Sublime Text
Post by: yakra 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.