Sublime Forum

How to define a function/symbol in a sublime-syntax file?

#1

I have a plain text file that looks like this

=Chapter 1
some text

=Chapter 2
some text

How to tell sublime that the pattern ‘=\w.*’ is a symbol?
So that when I do ‘Go to Symbol’ I can see the list of chapters.

I tried the following standard approach, a sublime-syntax file + a tmPreferences files. It doesn’t work, maybe there is something wrong in those two files.

Plain Text.sublime-syntax

%YAML 1.2
---
# http://www.sublimetext.com/docs/3/syntax.html
name: Plain Text
file_extensions:
  - txt
scope: text.plain
contexts:
  main:
    - match: '=+\s*(\w.*)'
      scope: meta.function.text.plain
      captures:
        1: entity.name.function.text.plain

Plain Text.tmPreferences

<?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>Symbol List</string>
  <key>scope</key>
  <string>text.plain meta.function.text.plain</string>
  <key>settings</key>
  <dict>
    <key>showInSymbolList</key>
    <integer>1</integer>
  </dict>
</dict>
</plist>
0 Likes

#2

I think what you have should work - but you might be better off not calling it “Plain Text” - maybe ST is using the default Plain Text syntax and thus not highlighting your file the way you expect and also not recognizing the symbols. Calling it “Plain Text” is a bit confusing imo.

0 Likes

#3

I think it is working here:

I have gone to Preferences -> Browse Packages... and Created a folder called Plain Text. And inside it the files:

$ tree
.
├── Plain Text.sublime-syntax
└── Plain Text.tmPreferences
1 Like

#4

Thanks kingkeith and addons_zz.

What I was missing is that even if the user-defined syntax is called ‘Plain Text’,
I must select it in the right corner ‘User:Plain Text’.

That’s fine for me, I wasn’t planning on overriding pre-configured syntax.

0 Likes