Sublime Forum

How to use multiples scopes (color syntax)

#1

Hello!

My wish:
Color all characters between two semi-colons, except characters between brackets (for which I have another color/scope).

file1.csv
EVENT1;In a desperate effort to save [From.GetHerHis] skin, [From.GetName] has decided to abdicate the position of regent to you.;Dans un dernier effort pour sauver sa peau, [From.GetName] a décidé d'abdiquer son titre de Régent en votre faveur.;;;;;;;;;x

My csv.sublime-syntax :

file_extensions:
  - csv
scope: source.csv

contexts:
  main:

    - match: '(^.*?);(.*?);(.*?);'
      captures:
        1: Code.csv
        2: English.csv
        3: French.csv


    # Commands
    - match: '\[[A-Za-z0-9_.]*?\]'
      scope: Commands.csv

I read http://www.sublimetext.com/docs/3/syntax.html
I’m not a programmer and it is not easy for me. I think I need to understand multiples scopes. I tried include, meta_scope, captures, etc, but it doesn’t work as I want.

Thanks for any help.

0 Likes

#2

Doing everything with one regex is not a good idea, because it will only match if you complete the whole line and you are missing the granularity to match something inside.
I would change it to something like this:

%YAML 1.2
---
# See http://www.sublimetext.com/docs/3/syntax.html
file_extensions:
  - csv
scope: source.csv
contexts:
  main:
    - match: '^\s*(?=\S)'
      push: block-code

  block-code:
    - meta_content_scope: meta.block.code.csv
    # pop at line end
    - match: \n
      pop: true
    # match commands
    - include: command
    # change to english at ; but exclude it from the scope
    - match: (?=;)
      set:
        - match: ;
          set: block-english
  block-english:
    - meta_content_scope: meta.block.english.csv
    - match: \n
      pop: true
    - include: command
    - match: (?=;)
      set:
        - match: ;
          set: block-french

  block-french:
    - meta_content_scope: meta.block.french.csv
    - match: \n
      pop: true
    - include: command
    - match: (?=;)
      set:
        - match: ;
          pop: true

  command:
    - match: \[[A-Za-z0-9_.]*?\]
      scope: support.function.command.scv
4 Likes

#3

Thank you, it works fine!

0 Likes