SublimeText2 Custom Scope - sublimetext2

I have a number of different types of projects that use similar code, and my goal is to make snippets and key bindings in SublimeText2 that only activate on a given project.
So, for instance, I have jsg1.html through jsg40.html, and I also have kwa1.html through kwa40.html. Each of these two groups has code unique to them, and I want to create different snippets and key bindings for each.
This has led me to want to create custom file-types, like .kwa and .jsg, so that I can create the snippets/key-bindings with a scope set to be those file types.
However, I can't make it work. This is what I've tried:
a.sublime-snippet
<snippet>
<content><![CDATA[
$2
]]></content>
<tabTrigger>aj</tabTrigger>
<scope>source.jsg</scope>
</snippet>
jsg.tmLanguage
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>fileTypes</key>
<array>
<string>jsg</string>
</array>
<key>name</key>
<string>jsg</string>
<key>scopeName</key>
<string>source.jsg</string>
</dict>
</plist>
Both of these files are in my Packages/User folder. However, I have a file, test.jsg, and the snippet won't activate in it.
So the questions are:
What's the best way to create custom scopes for groups of HTML files which will use the same key bindings and snippets? Is it creating a custom scope by creating a custom syntax for a new filetype? Or is there a better way? If this is the best way, why doesn't my snippet work in my .jsg files?

So I figured this out. To generate your own key-bindings and snippets, you'll need to create a custom scope. To display the current scope of your document, you can use the hotkey:
Ctrl + ⇧ + Alt + P
If you open an html file and run this command, you should see that it displays text.html. The goal is to make a filetype that highlights like html syntax, yet displays the scope of text.we where .we is our custom filetype (which is to say, it can be anything we want). We can then use that scope in our key-bindings and snippets files.
First things first: we need to make sure our custom filetype isn't already associated with html. This happens if you set the setting under View - Syntax - Open all with current extension as..., which you might initially do in an effort to make your filetype have the same highlighting as html. Don't worry, we'll still do that, but we have to go about it another way.
If you've already done it, locate the JSON file ../Path/to/Sublime Text 2/Packages/Users/HTML.sublime-settings and delete the extension you associated from the attribute extensions. If you're having trouble finding the Packages folder on Windows, it's typically hidden away in User/AppData/Roaming.
So now that we've disassociated our files, we must first find the settings file of the language that uses the highlighting we want. In my case, it was html, so I went to ../Path/to/Sublime Text 2/Packages/HTML. Copy the files html.tmLanguage and Comments.tmPreferences and put them anywhere in Packages. For organization, I put them in Packages/Users/xxx where xxx is my custom filetype. When we make snippets later, I'll also suggest you keep them there so that everything related to your filetype is in one place.
Anyway, rename the files accordingly, based on your filetype. Within the file, you'll need to change three attributes: fileTypes (it's at the beginning), name, and scope (those last two are at the very end). Replace the content of those accordingly. Be sure the scopeName takes the form text.___.
Change up the scope of the Comments.tmPreferences file and you'll be good to go with your custom filetype. It'll now display the highlighting of html files and allow you to use it as a scope in snippets and key-bindings.
Also, if your custom files are HTML files, browsers won't have any problem displaying them when you open them up.
Mission accomplished!

Related

Custom code snippet for WebStorm and PhpStorm

I use custom code snippet for Sublime Text like custom comments, function, reusable block code, CDN and more.. but I could not do that in WebStorm and PhpStorm IDE.
Here is my building block code snippet (comments) for Sublime Text:
/*============================
comments
============================*/
and this code blocks for HTML5 comments
<!-----------------------
comments
----------------------->
Moreover I'm new user for JetBrains software. Can I use custom code snippet above in JetBrains software ?
It's called Live Templates in JetBrains IDEs.
Available at Settings/Preferences | Editor | Live Templates.
You can use existing Live Templates as is, alter them to your needs or create your own.
Creating own is better be done in own group -- they will be stored in separate config file so easier to share, no possible conflicts with built-in ones (easier to update between versions etc.). It also makes perfect sense to use separate group per language -- the same abbreviation can be used for different languages/context but abbreviation within the same group must be unique.
BTW -- I'd say -- do not edit built-ins at all -- just disable specific built-in template and create your own version of it in separate group. This way you can always see what fix/change devs have made in new IDE version etc.
Full official tutorial/how-to is available here: https://confluence.jetbrains.com/display/PhpStorm/Live+Templates+%28Snippets%29+in+PhpStorm
You may also be interested in other articles:
https://confluence.jetbrains.com/display/PhpStorm/Tutorials
in particular (since you have used Sublime in the past): https://confluence.jetbrains.com/display/PhpStorm/PhpStorm+for+Users+of+Text+Editors
Finally I get a tips ! PhpStorm allows you to create your own live templates (code snippets) to optimise your workflows.
Open the settings dialog and head into Editor | Live Templates, you can see the available live templates grouped by language. To add a new template click the + (plus) button and select Live Template. Specify the abbreviation (the short bit of text you type that will be expanded to the full code snippet) and a description.
Then provide the full code snippet in the Template text field. You can include variables in the template in the format $<variable name>$, which will allow you to provide values when the template is expanded. PhpStorm recognises $END$ as a special variable indicating the final position of the cursor after the template has been expanded and values have been provided for all variables.
Next click the Define warning text to specify which language the template is for and optionally the context it is available in.
Now the template is ready to be used. Open a file and type the abbreviation that was specified earlier, then hit Tab to expand the template. The cursor will be positioned on the first variable, provide a value then hit Tab to keep moving through all available variables. The final position of the cursor will be the location of the $END$ variable.
Further Reading
Creating & Editing Live Templates
I don't think you can do this, however you can add custom tags in Settings > Editor > TODO.
//TODO & //FIXME are already implemented.
But this custom tags are not working for HTML.
Maybe you can find an extension to do that in Settings > Plugins.

Modifiying Sublime Text Syntax Highlighting for HTML Tags

I've tried numerous colour schemes for Sublime Text 2 and I've found a number that look great for javascript and php, but they also have terrible distinction between text and tags in HTML.
What I'm looking to do is make HTML tags the same colour as the beginning and end tag (<>). Right now the left and right chevrons appear the same colour as plain text, which can be dizzying to read.
Colour scheme Frontier
Realizing it's not the colour scheme but the syntax definitions for HTML, I looked in ~/Library/Application Support/Sublime Text 2/Packages/HTML/HTML.tmLanguage to make the modification.
Problem is, the XML file is littered with > and < and no documentation for the file itself other than the unofficial documentation for syntax definitions. Anyone know how I could merge the beginning and end tag definitions to be the same as the tags themselves?
The other option, of course, it to edit each theme I like and make sure the tags and the chrevrons are coloured the same, but I figured a global edit would make more sense in my case.
Colour scheme Phoenix Dark Blue
For example, Phoenix Dark Blue solves this problem by using the following definition,
<dict>
<key>name</key>
<string>Variable, String Link, Tag Name</string>
<key>scope</key>
<string>variable, support.other.variable, string.other.link, entity.name.tag, entity.other.attribute-name, meta.tag, declaration.tag</string>
<key>settings</key>
<dict>
<key>foreground</key>
<string>#787878</string>
</dict>
</dict>
In case of further .tmLanguage-related emergencies, take note of the TextMate Manual's Language Grammars section. Their examples are in JSON: a format infinitely more readable than PLIST but not a thing that Sublime Text can properly utilize. ST2 and ST3 users can use PackageDev to edit grammars in YAML.
As you've mentioned, and as confirmed by my regex search &(g|l)t;, there are way too many < or > tag delimiters within the Sublime Text 2 (build 2221) HTML.tmLanguage grammar file. Some HTML tags and wacky regex tag contexts are so special that they warrant a unique denotation, which unfortunately for us includes re-specifying the scope and context of the tag delimiters, < and >, rather than hooking in to some universal inequality symbol scope selector.
Luckily, every instance of < and > is given a scope name of punctuation.definition.tag.???.html, where ??? could be begin, end, or nothing. Likewise, each HTML tag (html in <html>) is named quite a lot like entity.name.tag.html. Fortune be praised, it's time for regular expressions!
In your HTML.tmLanguage file – I'd actually suggest cloning a new one into Packages/User for safety's sake, although you'll have to rename the file and the grammar – perform a regex search for all offending scope names with:
punctuation\.definition\.tag.*(?=</string>)
…and replace every result (this should be as simple as one Ctrl+V) with the same name as the tags themselves, entity.name.tag.html. In my testing I also added another sub-scope past tag in case I needed to find the replacements later, so it looked like this:
entity.name.tag.delimiter.html
I've tested this with ST2's default color schemes, and it should work with other well-written themes. Here it is with Dayle Rees' Frontier:
It even works in Sublime Text 3!
ST2 is no longer supported (or running, as far as I know). Here is a quick ST3 solution.
To customize colors of 'tag' angle brackets, you must create a dict scope for tag activity, with foreground key and color string. Here is the correct syntax.
<dict>
<key>name</key>
<string>tag</string>
<key>scope</key>
<string>entity.name.tag, punctuation.definition.tag.begin, punctuation.definition.tag.end</string>
<key>settings</key>
<dict>
<key>foreground</key>
<string>#CCCCAA</string> <!-- custom tag color here -->
</dict>
</dict>
This scope is explained here.
More scopes to explore here.

Sublime Text 2/3 class navigation to include class name in methods/fields

I'm using Sublime Text 3 on a large codebase, the entire LLVM + Clang.
One thing I'm seriously missing is the ability to goto a function belonging to a specific class.
To explain, let's say I'm in a file with 10+ classes, all having a method called getSourceRange().
The current goto symbol functionality only indexes the method names, so when I search for this method, I get a bunch of identical results that I have to go through manually until I find the right one. Using the project-wide Goto Symbol is worse as there are even more clashes.
What I'd like is for the indexed symbol name to always include the class name, e.g. VarDecl::getSourceRange instead of just getSourceRange. I can enable this for implementations outside-of-class by removing C++/Symbol Index.tmPreferences which essentially was stripping the CLASS:: from the indexed name. However I don't yet know how to inject the class name into definitions that are inside a class' scope.
The information is all there in the TextMate scopes, so I could technically add a rule for the following scope:
source.c++ meta.class-struct-block.c++ entity.name.function.c
But I don't know how to access the class' name as a string (stored in meta.class-struct-block.c++ » entity.name.type.c++ as saved in C++.tmLanguage), let alone format the function's indexed symbol name to include it.
Anyone know how to do this, if possible? TextMate's page explaining these mechanics doesn't mention this being possible, for what it's worth.
My attempt, stuck at how to insert a syntax name into the symbol(Index)?Transformation:
<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
<key>scope</key>
<string>source.c++ meta.class-struct-block.c++ entity.name.function.c</string>
<key>settings</key>
<dict>
<key>symbolIndexTransformation</key>
<string>/^/${entity.name.type.c++}::/;</string>
<key>symbolTransformation</key>
<string>/^/${entity.name.type.c++}::/;</string> <!-- just adds it literally :( -->
</dict>
</dict>
</plist>

How best open xml, parse with xslt and show result in browser

I am currently studying ways to present transformed xml files in browsers. My experience with this is minimal, so a number of questions pop up.
I have a transformation test.xslt which transforms input xml to html, and an input file test.xml containing
<?xml version="1.0" standalone="yes"?>
<?xml-stylesheet type="text/xsl" href="test.xslt" ?>
<root>...</root>
which, when opened in IE9, neatly displays the transformed xml contained above in the root element.
Question 1
Is there a processing instruction or similar available to include the source xml into the xml to be opened, somewhat like the following:
<?xml version="1.0" standalone="yes"?>
<?xml-stylesheet type="text/xsl" href="test.xslt" ?>
<... instruction to include source file data.xml>
Question 2
The file opened has extension xml. Is there a way to change file contents so it is valid html, allowing the file to be saved with extension html, so that when opened, the default browser will be selected (simply changing extension to html obviously does not have the desired effect so some structural change is necessary) ?
Question 3
My goal is to query a db to get the data to be parsed by the xslt code. What is the best way to do this (no problem if this includes javascript)?
Question 4
Standard db utilities may export query results in attribute-centered fashion (column names and values being represented as attribute names and values). This may involve pre-parsing the xml from db in order to convert it to parent-child fashion (columns as children instead of attributes). What is the best way to do this pre-parsing (note: I already have the xslt for this; I wonder about the data flow and when/how to run two xslt's in sequence) and then apply test.xslt (preferably without saving intermediate xml result files on the server)?
Question 5
When I open above xml in IE9, this works fine as said. But opening it in Firefox errors (RTF issue, apparently I need to use Firefox's node-set function but I still have to discover which namespace that has), and Opera/Chrome/Safari do not show any content. What exactly are the prerequisites for the various browsers where can I find more information on this?
Q1 If you start by serving an html file which then accesses the xml and xslt via javascript it naturally has access to both the input and the output of the xslt. If you are serving the xml and initiating the transformation using xml-stylesheet pi, then perhaps the best thing to do (depending on what you want to do) is to stuff the original source into the output, then javascript in the generated page can access it if needed, eg
<xsl:template matcj="whatever">
<html>
<head>
<script id="source" type="x-xml-spurce">
<xsl:copy-of select="/"/>
</script>
.... whatever you were going to do
then if you need to access the source in response to a user action on the page, a script can retrieve the script with id source and do whatever is needed. (If there is a possibility of the the source including the string you have to code it a bit more defensively).
Q2 If you want to use the xml-stylesheet API then you have to serve it as xml. However you can instead just serve html and then access the xml and xslt from within a script in the html page using the browsers javascip xslt api. as noted above that is more flexible than the xml-stylesheet mechanism.
Q3 pass
Q4 If you are accessing the xslt from javascript then it is easy to chain the output of one to the input of another without writing back to the server as you just have access to the result as a DOM node (or string, depending)
Answer to question 5: Firefox/Mozilla, Opera, Safari, Chrome all support the EXSLT node-set extension function in the namespace http://exslt.org/common, for IE and MSXML you can use script (imported) inside the XSLT stylesheet to allow it to support that namespace too, see http://dpcarlisle.blogspot.de/2007/05/exslt-node-set-function.html. That way inside the main stylesheet where you need to use the node-set function you don't need to write different code to cater for the different namespaces.

Eclipse - how to extend HTML editor to add custom tags?

I write an application and inside of HTML code I have custom tags (of course these tags are parsed on server side and end user gets them as valid HTML code). Example of custom tag usage:
<html>
<body>
...
<Gallery type="grid" title="My Gallery" />
...
</body>
</html>
1.) How can I have eclipse recognize my custom tags inside of HTML code and add syntax highlighting to them?
2.) How can I add auto-suggestions to my custom tags? For example if I type "<Gallery " press "Ctrl+Space" - in the list of available attributes it shows me "type" and "title" and if I type "<Gallery type=" press "Ctrl+Space" I would see list of available values only for tag "Gallery" and its attribute "type".
Thanks in advance!
Not really what you want, but maybe it helps you:
You can try the Aptana Plug-in for Eclipse. It allows to write your own regular expression for HTML validation, so a custom tag would be ignored by the validator.
E.g.:
.gallery.
Eclipse allows you to add simple auto-suggestions via Templates. On
Eclipse 3.7.1 (Indigo) + PHP Dev Tools (PDT) 3.0.0: Window > Preferences > Web > HTML Files > Editor > Templates
Sadly, there is no easy way: you have to roll your own parser for this, and then add both your extra elements and the base grammar (HTML) to it.
If you have your parser, you could use it to do syntax highlighting (strictly speaking, for that simple lexing is enough); and a good parser can support content assist (auto-suggestions in your terminology).
Caveats:
Creating a parser for HTML is not an easy task. Maybe by aiming at a more often used subset is feasible.
If a parser exists, the editor parts are still hard to get well.
Some help on the other hand: you could use some text editor generators to ease your work:
Eclipse IMP http://www.eclipse.org/imp/ can in theory handle any type of parser, but currently it is most optimized for LPG. The documentation is scarce, but the developers are helpful in the forums.
Xtext http://www.eclipse.org/Xtext/ got quite a hype for creating text editors for DSLs. The generated editors are quite nice out of the box, but is not the best solution for large files. Has a really helpful developer community.
EMFText http://www.emftext.org/index.php/EMFText is a lesser known entity - I don't know it in details, but I guess, it is similar to Xtext.
I know its been a long time since this Q was asked,
but I hope this might help others like myself that reach this in search of a solution.
So, When using Eclipse (Mars.1 Release (4.5.1) - and possibly earlier - I did not check).
Go to Window - Prefrences
Then in the dialog that opens go to Web - HTML Files - Editor - Validation.
On the right side:
under Ignore specified element names in validation and enter the list of custom elements you use. (e.g. Gallery,tab,tabset,my-element-directives-*)
you might also like to go under Ignore specified attribute names in validation do the same for your custom attributes.(e.g. ng-*,my-attr-directives-*)
Two things to note:
After letting eclipse do a full validation you must also close the file and reopen it to have the warnings removed from the source code.
Using this method would ignore those attributes under any element. I don't think there is a simple way to tell it to ignore some-attribute only if its a child of some-element.
I find templates are an ok alternative but let's see if we can encourage a more robust solution; please take a moment and vote for this: https://bugs.eclipse.org/bugs/show_bug.cgi?id=422584
You need to add a new HTML template.To add a new template, complete the following steps:
1) From the Window menu, select Preferences.
2) In the Preferences page, select Web and XML > HTML Files > HTML Templates.
3) Click New.
4) Enter the new template name and a brief description of the template.
5) Using the Context drop-down list, specify the context in which the template is available.
6) In the Pattern field, enter the appropriate tags, attributes, or attribute values (the content of the template) to be inserted by content assist.
7) If you want to insert a variable, click the Variable button and select the variable to be inserted. For example, the word_selection variable indicates the word that is selected at the beginning of template insertion, and the cursor variable determines where the cursor will be after the template is inserted in the HTML document.
8) Click OK to save the new template.
You can edit, remove, import, or export a template by using the same Preferences page.
Reference : http://help.eclipse.org/kepler/index.jsp?topic=%2Forg.eclipse.wst.sse.doc.user%2Ftopics%2Ftsrcedt024.html