How do I enable syntax highlighting for my Gemfile in Sublime Text 2? - ruby

I recently started using Sublime Text 2. What an awesome editor. It does a great job of highlighting Ruby code, but it does not highlight my Gemfile.
Is there a way to get it to do that?
I found this Gist but it has no instructions on how to use it.

There are at least three options:
Switch syntax manually (not preferred, but easy; no explanation required)
Add "Gemfile" to the list of Ruby-syntax files
Use the plugin you link to and create a package for it
1. No explanation, but handy trick
You can bind a keystroke to set syntax without moving to the mouse.
I bound syntax changing to Ctrl-Opt-Space by adding the following to my user keybindings:
[
{ "keys": ["ctrl+alt+space"],
"command": "show_overlay",
"args": { "overlay": "command_palette", "text": "Set Syntax: " } }
]
2. Add "Gemfile" to list of Ruby-syntax files
Linux: ~/.config/sublime-text-2/Packages/Ruby/Ruby.tmLanguage
OS X: ~/Library/Application Support/Sublime Text 2/Packages/Ruby/Ruby.tmLanguage
Windows: %APPDATA%/Sublime Text 2/Packages/Ruby/Ruby.tmLanguage
You can also get there by using the menu option Preferences -> Browse Packages and going into the Ruby package. Once you're in the file it'll be obvious: it's the <array> element with Ruby-looking filenames. Add <string>Gemfile</string> and you're all set.
It's possible the setting could get overwritten on an upgrade; I'm not sure how that works with ST2–it may be cleaner to do it through code as in the snippet.
3. Using the snippet you linked to
More work (and the correction of one syntax error). You can either do it manually, by creating a directory in Packages (see above for location) or create an actual package and allow ST2 to install it.
I created a test package called "Syntax" and copied the snippet into it, restarted ST2, and opening a Gemfile worked as expected. The correction required an additional colon (new gist), nutshell:
elif name[-3] == "erb": # Needed a semi-colon here.
set_sintax(view, "HTML (Rails)", "Rails")

If you are here but are using Sublime Text 3 you might not be able able to find the 'list of Ruby-syntax files' in packages.
Most other solutions found online were confusing to me.
I fixed this by manually changing Gemfile to Ruby in the bottom right hand corner file extension menu item when you have opened the file in Sublime Text 3 (which is what I had been doing each time I opened the file up until now).
Once you have selected ruby then go to Preferences -> Settings-More -> Syntax Specific-User
{
"extensions":
[
"Gemfile",
"Gemfile.lock"
]
}
When you navigate to Syntax Specific User it opens a file specific to the language that the file has syntax highlighting for. You may need to change the file back to whatever it is defaulting too (mine was 'Rd (R Documentation).sublime-settings') and removing Gemfile from that Syntax highlighting file.
In Ubuntu these files are stored at
~/.config/sublime-text-3/Packages/User

The DetectSyntax plugin for ST2 provides a more comprehensive solution to highlighting files - It allows file highlighting based on rules. It's smart enough to understand the difference between a Rails file, other files that use .rb as an extension and standard ruby files.
The standard rules include Gemfile, Rakefile, Guardfile and others matched to Ruby for Syntax formatting.
See DetectSyntax on GitHub.

You can achieve this by copying the HTML.tmLanguage file in the User/ folder, this way it won't be overwritten by an update.

Related

Small Basic & syntax highlighting in Visual Studio Code [duplicate]

Is there a way to extend the supported languages/grammars in Visual Studio Code?
I'd like to add a custom language syntax, but I've not been able to find any information on how language services are provided.
Can anybody point to any references or even examples of existing language implementations?
It's possible with the new version 0.9.0. There's an official documentation on how to add a custom language: https://github.com/microsoft/vscode-docs/blob/main/release-notes/v0_9_0.md
You need a .tmLanguage file for the language you want to add. You can find existing files e.g. on GitHub or you can define your own language file. Look here to get an idea of how to create one: http://manual.macromates.com/en/language_grammars
After finding a .tmLanguage file you have two ways to create an extension based on it.
Option 1: Using a Yeoman generator
Install node.js (if you haven't already done)
Install yo (if you haven't already done) by executing npm install -g yo
Install the Yo generator for code: npm install -g generator-code
Run yo code and select New language support
Follow the instructions (define the .tmLangauge file, define the plugin name, file extensions etc.)
The generator creates a directory for your extension with the name of the plugin in your current working directory.
Option 2: Create the directory on your own
Create a directory with the name of your plugin (only lowercase letters). Let's say we call it mylang.
Add a subfolder syntaxes and place the .tmlanguage file inside of it
Create a file package.json inside the root of the extension folder with content like this
{
"name": "mylang",
"version": "0.0.1",
"engines": {
"vscode": ">=0.9.0-pre.1"
},
"publisher": "me",
"contributes": {
"languages": [{
"id": "mylang",
"aliases": ["MyLang", "mylang"],
"extensions": [".mylang",".myl"]
}],
"grammars": [{
"language": "mylang",
"scopeName": "source.mylang",
"path": "./syntaxes/mylang.tmLanguage"
}]
}
}
Finally add your extension to Visual Studio Code
Copy the extension folder to the extension directory. This is:
on Windows %USERPROFILE%\.vscode\extensions
on Mac/Linux $HOME/.vscode/extensions
Restart Code. Now your extension will run automatically everytime you open a file with the specified file extension. You can see the name of the used plugin in the down right corner. You can change it by clicking on the name of the extension. If your extension is not the only one registered for a specific file extension then Code may chooses the wrong one.
To extend Wosi's .tmLanguage answer, using a .tmLanguage file is optional. Using a regular .json is a perfectly valid and—in my opinion—better readable alternative.
For an example, see VSCode_SQF: sqf.json
Inside the package.json, you would only need to change the path from ./syntaxes/mylang.tmLanguage to ./syntaxes/mylang.json.
Using reverse engineering you can add a new language to VSCode. You can take a look on how typescript is implemented as a JavaScript plugin and how it communicates with node.exe via pipe. But it's a hard thing since it's coming all without documentation
I'll provide a really short documentation here:
You can define a new plugin in the plugins folder C:\Users\USER\AppData\Local\Code\app-0.3.0\resources\app\plugins.
Copy the typescript plugin folder and rename mentioned file extensions and language names in all files to your new language, so that your new plugin is going to be used when a .mylang file is opened.
In typescriptServiceClient.js you see that a child process is being forked and that its stdout is coupled to a new WireProtocol.Reader. Bind your own mylanguage.exe (you'll probably need to write that exe on your own). VSCode asks that binary to get more language specific information.
In typescriptMain.js you find the feature registration for the language. Delete every call to monaco.Modes.XXXXXXSupport.register except monaco.Modes.DeclarationSupport.register.
Now open a directory in VSCode that contains .mylang files and open one of them via CTRL+P + FileName. Right click on an identifier and select Go to Definition. VSCode sends now a request like this via StdIn to your exe
{"seq":1,"type":"request","command":"definition","arguments":{"file":"d:/Projects/MyProj/Source/MyFile.mylang","line":45,"offset":9}}
VSCode expects an answer like this:
Content-Length: 251
[LINE BREAK]
{ "seq" : 1, "type" : "response", "command" : "definition", "request_seq" : 1, "success" : true, "body" : [{ "file" : "d:/Projects/MyProj/Source/MyOtherFile.mylang", "start" : { "line" : 125, "offset" : 3 }, "end" : { "line" : 145, "offset" : 11} }] }
If everything works VSCode will open MyOtherFile.mylang and set the cursor to line 124 in column 3.
Try it on your own ;-)
Simplest recipe IMHO as of 2021 Q2:
Follow Option 2 in Wosi's answer. You only need two files to get started. Just create the folder structure directly in your extensions directory.
Set "path": "./syntaxes/your_language.plist" in package.json
Use IRO to build your regexes.
Make sure that in the "Scope Information" screen, anything to do with Textmate is green. Don't worry about the other editors.
Save the contents of the "Textmate" tab into the path above, i.e., .syntaxes/your_language.plist
Reload VSCode
That's it. I also save the IRO (left pane) text into my own project.
You can read the source code of the built-in language extensions online:
https://github.com/microsoft/vscode/tree/main/extensions
You can pick an extension that is close to your language's syntax and change it as you wish. (e.g. you can do some modifications to the JavaScript extension and rebrand it for use with jQuery!)
It is important to note that this would be too much work if you choose a language that is so different from your desired language! If you didn't manage to find a language that is similar to your desired language, you may want to create a whole new extension from the ground up - https://stackoverflow.com/a/32996211/14467698 -.

Sublime Text 3: How to configure rubocop.yml path in sublime settings?

I have installed rubocop package for sublime text 3. I am trying to set custom rubocop configuration by providing rubocop.yml path to Rubocop.sublime-settings. Please find my configuration-snippet
"rubocop_config_file": "./.rubocop.yml"
However rubocop does not work when I give this configuration. It only works for
"rubocop_config_file": ""
How can i fix this and provide the path of my rubocop.yml to rubocop?
After some prodding, I found this :
By default, the linter plugin looks for a config file called
.rubocop.yml in the current directory and its parents
- https://github.com/SublimeLinter/SublimeLinter-rubocop
Even though I am not using the linter-plugin, I figured rubocop does the same. So by placing the .rubocop.yml in the current directory or any of its parents, I was able to get my .rubcop.yml file to be picked by rubocop. On a side note, I did not update configuration of the rubocop package, it automatically picks the .rubocop.yml.
Other settings in this Rubocop.sublime-settings seem to use a complete path.
So instead of using the dot to start at the folder where the Rubocop.sublime-settings file is located use a full path like the examples for other Rubocop.sublime-settings configurations.
Hope this helps

Sublime Text: "MarGo: Missing required environment variables: GOPATH"

In order to program with Golang in a more productive way, I just installed GoSublime for Sublime Text 2, as explained on their official GitHub repository:
Installation
Sublime Package Control allows you to easily install or remove
GoSublime (and many other ST2 packages) from within the editor. It
offers automatically updating packages as well so you no longer need
to keep track of changes in GoSublime.
Install Sublime Package Control (if you haven't done so already) from
http://wbond.net/sublime_packages/package_control .
Be sure to restart
ST2 to complete the installation.
Bring up the command palette (default ctrl+shift+p or cmd+shift+p) and
start typing Package Control: Install Package then press return or
click on that option to activate it. You will be presented with a new
Quick Panel with the list of available packages. Type GoSublime and
press return or on its entry to install GoSublime. If there is no
entry for GoSublime, you most likely already have it installed.
Now, whenever I launch Sublime Text, I have the following error message:
MarGo: Missing required environment variables: GOPATH
See the `Quirks` section of USAGE.md for info
And a usage.md file opens automatically, with the following content:
Usage
=====
Note
----
* Unless otherwise mentioned, `super` replaces `ctrl` in key bindings on OS X.
* A mention of a (GO)PATH variable uses a colon(`:`) as the separator.
This is the PATH separator on Linux and OS X, Windows uses a semi-colon(`;`)
Settings
--------
You may customize GoSublime's behaviour by (creating and) customizing the file `Packages/User/GoSublime.sublime-settings`. Default settings are documented in `Packages/GoSublime/GoSublime.sublime-settings`. **WARNING** Do not edit any package file outside of `Packages/User/`, including files inside `Packages/GoSublime/` unless you have a reason to. These files are subject to being overwritten on update of the respective package and/or Sublime Text itself. You may also inadvertently prevent the respective package from being able to update via git etc.
Quirks
------
This section assumes you know what `GOPATH` is and how to set it. If you don't, please see http://golang.org/doc/code.html
In some systems, environment variables are not passed around as expected.
The result of which is that some commands e.g `go build` don't work
as the command cannot be found or `GOPATH` is not set. To get around this
the simplest thing to do is to set these variables in the settings file.
See the documentation for the `env` and/or `shell` setting, both documented in the default
settings file `Packages/User/GoSublime.sublime-settings`
Code Completion
---------------
Completion can be accessed by typing the (default) key combination `CTRL+[SPACE]` inside a Golang file.
Key Bindings
------------
By default, a number of key bindings are provided. They can be viewed by opening the command palette
and typing `GoSublime:` or via the key binding `ctrl+dot`,`ctrl+dot` (or `super+dot`,`super+dot` on OS X).
Wherever I refer to a key binding with `ctrl+` it is by default defined as `super+` on OS X unless stated otherwise.
Useful Key Bindings
-------------------
Often when commenting out a line, the immediate action following this is to move the cursor to the next line either to continue working or comment out the following line.
With this key binding, you can have the line commented out and the cursor automatically moved to the next line.
{ "keys": ["ctrl+/"], "command": "gs_comment_forward", "context": [{ "key": "selector", "operator": "equal", "operand": "source.go" }] },
Package Imports
---------------
Pressing `ctrl+dot`,`ctrl+p` will open the package list from which you can quickly import or delete a package import.
The usage is the same for both operations. If the package is already imported then it will appear near the top
and be marked as a *delete* operation, so in effect it is a toggle. If you want to edit the alias of a package e.g
a database driver: first import the package as normal and then press `ctrl+dot`,`ctrl+i` to quickly jump
the last imported package. Once edited you can return to where you were by pressing `ctrl+dot`,`ctrl+[`
Building, Testing and the Go command
------------------------------------
GoSublime comes with partial command/shell integration `9o`. For more information about 9o, see Packages/GoSublime/9o.md
or from within Sublime Text press `ctrl+9` or `super+9` and type `help`.
To run package tests you have 3 options.
* press `ctrl+dot`,`ctrl+t` to open the testing quick panel. This offers basic/common options such
as running all benchmark functions or running a single test function.
* inside a `_test.go` file, press `ctrl+shift` and left-click on the name of a Test, Benchmark or Example
function e.g. `TestAbc` to execute that function only.
* if the above options are too minimalistic or you would otherwise like to call `go test` with your own options,
open 9o by pressing `ctrl+9` where you have access to the `go` command.
In the case of building a package, 9o provides a replay command (see 9o.md for details) that will execute
the command if the pkg is a command pkg (package main) or run all tests if it's a normal pkg.
The replay command is bound to `ctrl+dot`,`ctrl+r` for easy access.
GoSublime provides an override for the Sublime Text build-system via `ctrl+b`. In the menu `Tools > Build System` it's named `GoSublime`.
`ctrl+b` is automatically handled by Sublime Text, so if you have another `Go` build system chosen, `ctrl+b`
will execute that instead. To access the `GoSublime` build system directly press `ctrl+dot`,`ctrl+b`.
This build system simply opens 9o and expand the last command. i.e. executes the 9o command `^1`.
Per-project Settings & Project-based GOPATH
-------------------------------------------
If you have a settings object called `GoSublime` in your project settings its values will override those
inside the `GoSublime.sublime-settings` file. As a side-effect you may set a specific GOPATH for a single
project.
`my-project.sublime-project`
{
"settings": {
"GoSublime": {
"env": {
"GOPATH": "$HOME/my-project"
}
}
},
"folders": []
}
If the only setting you use this functionality to change is the GOPATH, then you may be able to find
success by adding the string `$GS_GOPATH` to your global `GOPATH` setting e.g.
{
"env": { "GOPATH": "$HOME/go:$GS_GOPATH" }
}
`GS_GOPATH` is a pseudo-environment-variable. It's changed to match a possible GOPATH based on:
* the current working directory, e.g. `~/go/src/pkg` then `$GS_GOPATH` will be `~/go/`
* or the path the current `.go` file (or last activated `.go` file if the current file is not `.go`) e.g. if your file path is `/tmp/go/src/hello/main.go` then it will be `/tmp/go`
If you would like to use `GS_GOPATH` exclusively, you may set the option `use_gs_gopath` to true.
This automatically changes `GOPATH` to the value of `$GS_GOPATH` if `$GS_GOPATH` is not empty.
This allows you to e.g. automatically adapt `GOPATH` to your current project rather than
reverting to your normal `GOPATH`.
Lint/Syntax Check
-----------------
The source is continuously scanned for syntax errors. GoSublime will try to catch some common errors, like
forgetting to call flag.Parse (if this causes false positives, please file a bug report).
Apart from the highlighting in the view using a dot icon in the gutter and usually underlining the
first character of an error region. You are given an entry in the status bar in the form: `GsLint (N)`
where `N` is the number of errors found in that file. You can show the list of errors and navigate to
them by pressing `ctrl+dot`,`ctrl+e`. Errors for the current line are shown in the status bar.
Fmt
---
By default `ctrl+s` and `ctrl+shift+s` are overridden to fmt the the file before saving it. You may also
fmt a file without saving it by pressing `ctrl+dot`,`ctrl+f`
Godoc/Goto Definition
---------------------
To show the source and associated comments(documentation) of a variable press `ctrl+dot`,`ctrl+h` or
using the mouse `ctrl+shift+right-click`. This will show an output panel that presents the full
definition of the variable or function under the (first) cursor along with its comments.
To goto the definition instead, press `ctrl+dot`,`ctrl+g` or alternatively using the mouse `ctrl+shift+left-click`.
Declarations/Code Outline?
--------------------------
A very minimal form of code outline is provided. You can press `ctrl+dot`,`ctrl+d` to list all the declarations
in the current file.
New File
--------
Pressing `ctrl+dot`,`ctrl+n` will create a new Go file with the package declaration filled out.
It will try to be intelligent about it, so if the current directory contains package `mypkg` it will use that as the package name.
Misc. Helper Commands
---------------------
The following commands can be assigned key bindings to further improve your editing experience.
* gs_fmt - this command runs `gofmt` on the current buffer. Also available via the key binding `ctrl+dot`,`ctrl+f`.
* gs_fmt_save, gs_fmt_prompt_save_as - these commands will run the `go_fmt` followed by `save` or `prompt_save_as` - these are bound to `ctrl+s` and `ctrl+shift+s` respectively, by default.
* gs_comment_forward - this command will activate the `ctrl+/` commenting and move the cursor to the next line, allowing you to comment/uncomment multiple lines in sequence without breaking to move the cursor. You can replace the default behaviour by overriding it in your user key bindings (Preferences > Key Bindings - User) with `{ "keys": ["ctrl+/"], "command": "gs_comment_forward", "context": [{ "key": "selector", "operator": "equal", "operand": "source.go" }] }`
Some people seem to have had the same issue:
Idiotproof instructions for MarGo: Missing required environment variables: GOPATH #310
How come USAGE.MD opens everytime I open Sublime? #601
gopath is set, but still show "MarGo: Missing required environment variables: GOPATH" #520
And the root cause of the problem could be that the GOPATH has not been defined.
Mine has been — export GOPATH=$HOME/code/go — and I am able to run Go (and .go files) on my computer.
Any idea of how this can be fixed?
You need to set gopath in the GoSublime settings.
Go to:
Preferences -> Package Settings -> GoSublime -> Settings – User
And paste that code there:
{
"env": {
"GOPATH": "$HOME/code/go"
}
}

Fix Quick Look for TypeScript files

My Mac recognises the .ts extension as MPEG-2 Transport Stream, while I use it more often as TypeScript files. Because of that Quick Look does not show the contents of .ts files.
I found on Stackoverflow that I should be able to assign custom extensions to the text editor, but I'm not sure how.
QLColorCode is a very nice Quick Look plugin which allows you to set syntax highlighting for ANY extension file you want.
1. Install QLColorCode Quick Look plugin
If you have Homebrew, you can install it with:
brew cask install qlcolorcode
Otherwise, you can just copy / paste the QLColorCode.qlgenerator to your ~/Library/QuickLook/ folder from a release.
2. Add .ts and .tsx syntax highlighting support
First get the kMDItemContentType of the extension you want to quick look at with the plugin:
mdls -name kMDItemContentType /path/to/any/file.ts
Add an entry with the string returned by the previous command to the plugin settings list — for a .ts file, this should be public.mpeg-2-transport-stream. To do so, just open the file ~/Library/QuickLook/QLColorCode.qlgenerator/Contents/Info.plist with any text editor (it's not a compiled file) and in the section CFBundleDocumentTypes > Item 0 > LSItemContentTypes (~ 15th line), add a line like this:
<string>public.mpeg-2-transport-stream</string>
You can find more information on how to add language type support to the plugin in the project README.md.
Here is the original post where I found all this useful information.
According this article, there is another solution:
1) Remove filetype definition from: /System/Library/CoreServices/CoreTypes.bundle/Contents/Info.plist
You need to do that in recovery mode.
215 => {
"UTTypeConformsTo" => "public.movie"
"UTTypeDescription" => "MPEG-2 Transport Stream"
"UTTypeIdentifier" => "public.mpeg-2-transport-stream"
"UTTypeTagSpecification" => {
"public.filename-extension" => [
0 => "ts"
]
}
}
2) Then run
/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -kill
I managed to get QuickLook to display .ts files as plain text.
The necessary step was to remove Movie.qlgenerator from the directory /System/Library/QuickLook.
The downside is, that QuickLook can't preview any types of movies any more...
Update 2016-04-12:
In OSX El Capitan it seems not possible to simply remove a file from /System/Library/QuickLook (probably because of System Integrity Protection).
I managed to remove the Movie.qlgenerator with the following steps:
Back up the file first
Boot into Recovery Mode: Restart and press-and-hold Cmd-R
Open a terminal
disable SIP: csrutil disable
cd /Volumes/Macintosh HD/System/Library/QuickLook (your volume name may be different)
rm -rf Movie.qlgenerator
csrutil enable
csrutil status -> Verify SIP is turned on again
Restart
After the above steps I could quick-look my .ts files.
The best part: I put the Movie.qlgenerator I had backed up in the first step into ~/Library/QuickLook and now I have also quick-look previews for movies ...

Make Geany recognize additional file extensions

My default Geany installation on Debian does not recognize some file types out of the box. How can I add extensions, using the same syntax highlighting as other known extensions for simplicity's sake?
In the current case, I'd like Geany to open all .aspx files with the same highlighting as .html files.
Use inside the menu Tools->Configuration files->filetype_extensions.conf. This will allow you to configure the filetypes based on suffix for your user.
Just add new extensions in /usr/local/share/geany/filetype_extensions.conf
I had trouble accomplishing this without help from here.
Go here to read documentation about this topic.
From the Geany menu, go to:
Tools > Configuration Files > filetype_extensions.conf
Go to this line and un-comment it:
#~ [Extensions]
So that it just reads:
[Extensions]
I was needing to add .mjs as an additional extension for javascript. So, for me, I also uncommented this line:
#~ Javascript=*.js;
And changed it to:
Javascript=*.js;*.mjs;
After this, I saved the file.
Now when I opened files having a .mjs file extension, they automatically have Javascript Syntax Highlighting.
I made some suggestions to improve this expeirience here.

Resources