Skip to content

Commit

Permalink
Add menu item to generate syntax setting key/value pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
reywood committed Feb 18, 2015
1 parent c8d4f29 commit 6feeeb5
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 3 deletions.
15 changes: 15 additions & 0 deletions Context.sublime-menu
@@ -0,0 +1,15 @@
[
{ "caption": "-", "id": "projectspecificsyntax-sep-top"},
{
"id": "projectspecificsyntax",
"caption": "Project Specific Syntax",
"children":
[
{
"caption": "Copy syntax setting to clipboard",
"command": "project_specific_syntax_to_clipboard"
}
]
},
{ "caption": "-", "id": "projectspecificsyntax-sep-bottom"}
]
6 changes: 6 additions & 0 deletions Default.sublime-commands
@@ -0,0 +1,6 @@
[
{
"caption": "Project Specific Syntax: Copy syntax setting to clipboard",
"command": "project_specific_syntax_to_clipboard"
},
]
6 changes: 4 additions & 2 deletions README.md
Expand Up @@ -10,13 +10,15 @@ In your `.sublime-project` file, you just need to add a `syntax_override` sectio
...

"syntax_override": {
"\\.html$": [ "HTML Underscore Syntax", "HTML (Underscore)" ]
"\\.html$": ["HTML Underscore Syntax", "HTML (Underscore)"]
}
}
```

The `syntax_override` section can contain as many key/value pairs as you like.

The key should be a regular expression that will be matched against the name of the file. Note that the `.` in `.html` has to be escaped to `\.` since it will match any character otherwise. And since this is a JSON string, we need to escape the slash, so we end up with `\\.`.
The easiest way to construct these key/value pairs is to open an existing file in your project, set the syntax you would like to use for that file with the `View > Syntax > ...` menus (or the command palette), right click in the file editor area, and select the `Project Specific Syntax > Copy syntax setting to clipboard` menu item. This menu item is also available via the command palette. Once copied to the clipboard, simply open your project file and paste the new key/value pair into the `syntax_override` section. Be sure to add any necessary commas to separate multiple key/value pairs so your project file is still valid JSON.

If you need more control, you can construct your own key/value pairs. The key should be a regular expression that will be matched against the name of the file. Note that the `.` in `.html` has to be escaped to `\.` since it will match any character otherwise. And since this is a JSON string, we need to escape the slash, so we end up with `\\.`.

The value in the key/value pair should be an array containing two or more strings. All but the last string in this array are the names of the package directories containing the syntax file and the last is the name of the syntax. Root around in Sublime Text's directory structure to find files that end with `.tmLanguage`. The names of these files (minus the `.tmLanguage` extension) are what you would use for the last string. Typically, you will only have two strings, a directory name and the syntax file name (minus `.tmLanguage` file extension), but this is dependent on the package's directory structure.
3 changes: 2 additions & 1 deletion messages.json
@@ -1,3 +1,4 @@
{
"install": "README.md"
"install": "README.md",
"0.0.4": "messages/0.0.4.txt"
}
6 changes: 6 additions & 0 deletions messages/0.0.4.txt
@@ -0,0 +1,6 @@
Project Specific Syntax 0.0.4
-----------------------------

Generating the key/value pairs to specify your custom syntax settings is now much easier!

Open an existing file in your project, set the syntax you would like to use for that file with the `View > Syntax > ...` menus (or the command palette), right click in the file editor area, and select the `Project Specific Syntax > Copy syntax setting to clipboard` menu item. This menu item is also available via the command palette. Once copied to the clipboard, simply open your project file and paste the new key/value pair into the `syntax_override` section. Be sure to add any necessary commas to separate multiple key/value pairs so your project file is still valid JSON.
41 changes: 41 additions & 0 deletions project_syntax.py
@@ -1,3 +1,4 @@
import json
import os.path
import re
import sublime
Expand Down Expand Up @@ -43,3 +44,43 @@ def _resolve_window(self, view):
return window

return sublime.active_window()


class ProjectSpecificSyntaxToClipboardCommand(sublime_plugin.TextCommand):

def __init__(self, window):
super().__init__(window)

def run(self, edit):
syntax_path_parts = self._get_syntax_path_parts()

if not syntax_path_parts:
sublime.set_clipboard("Unable to create syntax setting")
return

syntax_path_json = json.dumps(syntax_path_parts)
file_regex = self._get_example_file_regex()
suggested_setting = '"{0}": {1}'.format(file_regex, syntax_path_json)

sublime.set_clipboard(suggested_setting)

def _get_syntax_path_parts(self):
syntax = self.view.settings().get('syntax')

match = re.search(r'^Packages/(.*)\.tmLanguage$', syntax)

if not match:
print('Syntax does not match expected format: {0}'.format(syntax))
return None

return match.group(1).split('/')

def _get_example_file_regex(self):
file_name = self.view.file_name()

if file_name:
ext = os.path.splitext(file_name)[1]
else:
ext = '.xyz'

return '\\\\{0}$'.format(ext)

1 comment on commit 6feeeb5

@reywood
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addresses issues #3

Please sign in to comment.