Sublime Text different syntax for files under a specific subdirectory - sublimetext

Is there a way to make Sublime Text use a different syntax hilighting plugin for files inside a specific directory tree?
E.g. By default I want to use the default Javascript file type for .js files, but for files under ~/Unity/ folder I want to use Unity3d-Javascript file type.

Check out the ApplySyntax package. This is a very powerful plugin that allows you to apply arbitrary regexes to several different aspects of a file (file name, contents, first line, suffix, etc.) and then assign a syntax based on them. Once installed, open Preferences -> Package Settings -> ApplySyntax -> Settings-Default to read all the notes and see how the regexes are set up. Then, open ... -> Settings-User and add your custom regexes. I haven't tested this, but something along these lines should work:
{
"name": "Unity3D/UnityJavaScript",
"rules": [
{"file_name": ".*/Unity/.js$"}
]
}
Good luck!

Related

Add file-specific words to Sublime Text spell checking

If I use a certain (made-up) word only in a particular file. Is there a way to add it to the dictionary of just that particular file with Sublime Text?
Updated 23 Jan 23
Unfortunately not. You can use the added_words setting to include your special word in the dictionary:
{
// ...
"added_words": [
"Bandersnatch"
]
}
But this is an application-wide setting, and it's not consulted in the project or user setting scopes.
But if you put that in your user settings, it will apply in all files. To be more precise, you can use Projects. Create a project file, then make sure you open your project using the Projects > Open Project… menu item. In there, you can put the added_words setting in the settings object:
{
"folders":
[
{
"path": "."
}
],
"settings":
{
"added_words": ["Bandersnatch"]
},
}
This will apply the setting to just a single window, but it will be all files in that window. You can tailor the project (using the folders key) so that it only displays the one file, but that may be too restrictive.
Depending on the different file types you edit, it may be possible to tailor the spelling_selector to uniquely identify this one file. In fact, if you wrote a syntax which applied a scope (which you could inherit from the real scope) to any file with the exact name your file has, then you'd be able to apply spelling only to that file using spelling_selector. However, I think this would turn off spelling for all other files, which is probably not what you want either.

How to apply a prediffer to WinMerge such that it runs on all files in the diff?

TL;DR: How can I automatically apply PrediffLineFilter on all diff results, not manually on only the open file?
WinMerge 2.16.4.0 supports using plugin prediffers to, among things, diff while ignoring items that meet conditions specified with a regular expression*. However it appears you must manually apply the prediff after you perform the diff (huh?) and on each and every individual file if it doesn't match a file filter. The help manual indicates the file filter for PrediffLineFilter.sct is *.txt and gives no guide to changing it.
I have thousands of files where the only difference might be a server name which follows a predictable pattern. (Example: server01, server02, etc.) I have figured out the regex for that pattern and the manual application of PrediffLineFilter after a diff works. But I can't be doing that on thousands of files.
How can I automatically apply PrediffLineFilter on all diff results, not manually on only the open file? I only want to see the files where the differences are meaningful.
*Learned this thanks to WinMerge : how to ignore specific words in a comparison?
In your winmerge installation folder, go to MergePlugins then edit the PrediffLineFilter.sct. Then change the return of get_PluginFileFilters to files you want this plugin to work on. For example you want this plugin to work on html and txt files:
Function get_PluginFileFilters()
get_PluginFileFilters = "\.html$;\.txt$"
End Function
After you have made these changes, select all the files on your folder compare window then right click > Plugin Settings > Prediffer Settings > Auto Prediffer, then refresh.
Btw, in the later versions(2.16.10) you can do this in the Plugin Settings window.

Syntax highlighting for .js.erb won't stay as JavaScript [duplicate]

Is there a way to make Sublime Text use a different syntax hilighting plugin for files inside a specific directory tree?
E.g. By default I want to use the default Javascript file type for .js files, but for files under ~/Unity/ folder I want to use Unity3d-Javascript file type.
Check out the ApplySyntax package. This is a very powerful plugin that allows you to apply arbitrary regexes to several different aspects of a file (file name, contents, first line, suffix, etc.) and then assign a syntax based on them. Once installed, open Preferences -> Package Settings -> ApplySyntax -> Settings-Default to read all the notes and see how the regexes are set up. Then, open ... -> Settings-User and add your custom regexes. I haven't tested this, but something along these lines should work:
{
"name": "Unity3D/UnityJavaScript",
"rules": [
{"file_name": ".*/Unity/.js$"}
]
}
Good luck!

What is the option that makes Sublime Text add whitespace at the left of wrapped lines?

I want Sublime to treat my text files like they are source code files and show whitespace at the left of wrapped lines, like so:
beginning of long line blah blah blah, now it wraps
and it keeps going after an automatic indent
I tried opening the console and setting the option manually:
view.settings().set('indent_subsequent_lines', True)
but nothing changes, any ideas?
Well, that was fun. There's no easily accessible setting for this. But as you indicate, ST decides whether to add an extra indent when soft wrapping, from whether the syntax is considered code-like or plain text-like.
Being one or the other is up to the package defining the syntax to specify. So lacking a global setting from ST, you need to change your text packages. As an example, let's take Text. That contains Plain text.tmLanguage. In that you change
<key>scopeName</key>
<string>text.plain</string>
to
<key>scopeName</key>
<string>source.plain</string>
I'm unsure of whether there'll be ill effects from not keeping .plain.
One easy way to do this is to get the PackageResourceViewerpackage.
After install, do:
cmdshiftp
Type: Open Resource
return
Type: Text
return
Type: Plain text.tmLanguage
Make your edit and save.
PackageResourceViewer will save the modified Text package to your Packages directory. And sublime will display files considered to have Plain text-syntax, like they are code.
The caveat is that you need to do this for every text-syntax you want to be considered code.
As #AdamAL said, it's dependent on both the 'indent_subsequent_lines' setting as well as whether it's considered a "source" or a "text" language ... by default, "markup" languages (such as HTML, CSS, etc.) and plain text, etc., are considered "text", and programming languages such as C++, Java, PHP, etc., are considered "source".
"Source" languages will indent subsequent soft-wrapped lines (if the indent_subsequent_lines is true), whereas "text" languages will only indent up to the same level as the current line.
For each one you want to change, you'll need to edit the settings of the given language. #AdamAL's answer provides a great way to do this using the PackageResourceViewer package:
After install, do:
[Ctrl/Cmd]+[shift]+p, "Open Resource"
Find the name of the language you want to change, and find either the .sublime-syntax file or the .tmLanguage file. .sublime-syntax is supported from build 3084 of ST 3 and appears that it may trump values in the .tmLanguage file in supported versions, if present (when editing the definition of TaskPaper files provided by the "PlainTasks" package, my change didn't take when just editing the PlainTasks.tmLanguage, I had to edit the PlainTasks.sublime-syntax before it took).
In .sublime-syntax (which are YAML files) look for the first scope: line, where the main scope name of the language is identified (there will be lots of other scope: entries further down under contexts:).
In .tmLanguage (which are XML .plist files) look for the <string> following the <key>scopeName</key>.
Sublime Text Syntax Definition Documentation Reference:
scopeName
Name of the topmost scope for this syntax definition. Either source.<lang> or text.<lang>. Use source for programming languages and text for markup and everything else.
The <lang> (without brackets) is just an identifier string for the given syntax/language definition.
I noted that it seems that (in ST 3, anyway), no restart of Sublime Text is needed to get the changes to apply, if the edit is in the right place.
And also note that there may be other effects of changing this in more complex packages -- For example, in PlainTasks, the additional keybindings that it defines depended on it looking for a context that included text.todo, which I changed to source.todo in several places. So in order for the keybindings to work properly again, I also had to update my .sublime-keymap for that package. (This could also be because I changed it in a place besides the .sublime-syntax that I didn't need to. Just sayin' -- YMMV.)

How to create this sublime text syntax?

I am trying to create a simple sublime syntax where when the first word of a line is "DONE" the whole line turns to a different color.
For example:
- do this
DONE - do that // this line turns green
- but also do this
My two questions are:
- How does one go about creating a ST syntax
- How would I create the above simple syntax.
The most you could highlight is the region containing text. I don't know if that's what you are looking for. To do this would require defining a tmLanguage file and modifying you tmTheme file. The tmLanguage file describes a set of regular expressions for which a scope is applied to the text within ST. The tmTheme file takes the scopes applied, and applies some coloring. I'm not an expert on writing either of these files, so you may need to do some experimentation on your own.
For information on writing syntax files, see here and here. The theme files are much simpler, in terms of defining them. Those guides recommend using AAAPackageDev, which is not necessary (and not compatible with ST3 last I checked). The language files are all XML, which you can work with, but I find working in JSON or YAML easier. If you feel the same way, you can start with the below snippet and use SerializedDataConverter to convert between XML and PLIST/JSON.
{
"name": "Syntax Name",
"scopeName": "source.syntax_name",
"fileTypes": [""],
"patterns": [
],
"uuid": "ca03e751-04ef-4330-9a6b-9b99aae1c418"
}
I can't recall the location of any references, but I'm sure they're out there. Though, simply viewing some existing theme files may be enough for you. TO view these, you can use PackageResourceViewer. Isn't really necessary if you are working in ST2, but makes things much easier in ST3. To begin working on your own custom theme, I'd copy the contents of the theme file you are currently to a new file, and save it in your Packages/User directory. That way, you always have something to revert to, in case you mess something up.

Resources