code style for velocity template files in IntelliJ - intellij-idea

Problem
The automatic indents in .vm files don't behave as I want them to. Below I add the auto formatted file and also the formatting I want.
The following code snippet is auto formatted by IntelliJ:
#styleTitle('sample-title')
##styleDescription()
A short description of the component
#end
##styleComponent('/components/molecules/sampleComponent/sampleComponent', {
"headline": 'A headline title',
"alertText": "Alert text example",
"link" : {
"href": "www.stackoverflow.com",
"text": "Click here!"
}
})#end
What I want:
#styleTitle('sample-title')
##styleDescription()
A short description of the component
#end
##styleComponent('/components/molecules/sampleComponent/sampleComponent', {
"headline": 'A headline title',
"alertText": "Alert text example",
"link" : {
"href": "www.stackoverflow.com",
"text": "Click here!"
}
})#end
Question
I couldn't find any additional configuration under IntelliJ IDEA | Preferences | Editor | Code Style | Velocity to adjust indention for different code blocks.
How can I configure the code style manually for a specific file type in IntelliJ and how does such a configuration file look like?

Related

How to define and use custom variable for VS Code snippets

Is there any way to define custom variables for VS Code snippets?
Particularly, I want to set variable values from settings.
For example, with config setting "my-user-name-test.user.name" in setting.json, I want to define snippets like the below example.
{
"Input User Name": {
"prefix": "user-name",
"body": [
"{",
" \"user_name\": \"${config:my-user-name-test.user.name}\"",
"}"
],
"description": "Test config user name"
}
}

Custom code completion to Visual Studio Code?

I have a definition (below) file for a JavaScript library.
How can I hook it up to Visual Studio Code code completion?
It has the following format:
{
"!name": "SomeLibrary",
"someMethod" :
{
"!doc" : "Description of some method.",
"!type" : "fn(someParameter: someType) -> someReturnType"
},
"SomeClass" :
{
"!doc" : "Description of some class.",
"someOtherMethod" :
{
"!doc" : "Description of some other method.",
"!type" : "fn(someParameter: someType) -> someReturnType"
}
}
}
Bonus question: What is the name of this format at all?
UPDATE: I found out that I should create a Typescript definition file, but I really don't know how to convert it from this file I have. Is this a standard format at all?
What you want to create is called a snippet:
You can define your own snippets, either global snippets or snippets for a specific language. To open up a snippet file for editing, select User Snippets under File > Preferences (Code > Preferences on macOS) and select the language (by language identifier) for which the snippets should appear or create a new global snippet (New Global Snippets file...).
Since snippets themselves are defined as JSON, your example could get a bit messy, but basically it would look like this:
{
"You Interface Definition": {
"prefix": "idef",
"body": [
"{",
"\t\"!name\": \"<LIBRARY_NAME>\",",
"\t\"<METHOD_NAME>\" : ",
"\t\t{",
"\t\t\"!doc\" : \"<DESCRIPTION>\",",
"\t\t\"!type\" : \"fn(<PARAMETER_NAME>: <PARAMETER_TYPE>) -> <RETURN_TYPE>\"",
"\t\t},",
"\t\"<CLASS_NAME>\" : {",
"\t\t\"!doc\" : \"<DESCRIPTION>\",",
"\t\t\"<METHOD_NAME>\" : {",
"\t\t\t\"!doc\" : \"<DESCRIPTION>\",",
"\t\t\t\"!type\" : \"fn(<PARAMETER_NAME>: <PARAMETER_TYPE>) -> <RETURN_TYPE>\"",
"\t\t}",
"\t}",
"}",
]
},
}
Note that I'm using \t rather than spaces, since that will respect a user's indentation settings
Now, once you type the prefix idef and press Tab the snippet will expand. You can add tab-stops with placeholder text, in case you want to change <DESCRIPTION> or <PARAMETER_NAME>. Here's a simplified example:
\"fn(${1:<PARAMETER_NAME>}: ${2:<PARAMETER_TYPE>}) -> ${3:<RETURN_TYPE>}\""
Note those parts enclosed by curly braces. Once your snippet is expanded you can use Tab to jump between those tab-stops and modify the selected text. The same tab-stop can be repeated multiple times, should you have the need to insert the same text at multiple places.

Sublime Text 3: how to bind a shortcut to a specific file extension?

I would like to customize shortcut, but apply them only to a specific extension.
For example,
"jump to matching bracket" -> works in JS files -> customly bound to ctrl+m,
"go to matching tag pair" (emmet) -> works in HTML files -> I would like to ctrl+m also here, but doesn't work (ST3 understand "jump to matching bracket" which doesn't apply here).
I was wondering if specializing a shortcut to a specific extension would do the trick?
Apparently you can try something like this:
[
{ "keys": ["ctrl+x", "ctrl+i"],
"command": "insert_snippet",
"args": {"name": "Packages/User/mysnippet.sublime-snippet"},
"context": [ {"key": "selector", "operator": "equal", "operand": "text.tex.latex"} ]
}
]
Where you'ld replace the first 3 lines by what you want, and text.tex.latex by the scope you want (source.js and text.html.basic in your case).

How to move the cursor to the end of the tag in Sublime Text 2?

When I type the start of a tag, Sublime Text will auto-complete the end of the tag and position the cursor inside the tag.
<code>|</code>
I use | to represent the cursor. So when I finished the contents inside the tag, I want to move the cursor to the end of the tag like this:
<code>blabla</code>|
To do this, now I have to press Right button to move the cursor character by character, which is not efficient. Is there any shortcut to move the cursor to the end of the tag directly?
You could also create a macro. This may be valuable if your tags cover multiple lines. Save the following as something like move_to_end_tag.sublime-macro in Packages/User.
[
{
"args":
{
"to": "tag"
},
"command": "expand_selection"
},
{
"args":
{
"by": "characters",
"forward": true
},
"command": "move"
}
]
You can then create a keybinding for the action.
{
"keys": ["ctrl+shift+alt+right"],
"command": "run_macro_file",
"args": {"file": "res://Packages/User/move_to_end_tag.sublime-macro"}
}
Of course, you can change the keys to whatever you like.
I'm working in Sublime Text 3 rather than 2, but the End button (for me, between the delete and page down) does the trick for me.
I found that annoying as well. Personally, taking the End key binding and applying it to Shift+Space works well enough for my purposes.
If that's easier for you, you can add this line to your user key bindings:
{ "keys": ["shift+space"], "command": "move_to", "args": {"to": "eol", "extend": false} }
Using a macro as skuroda suggested is a good option as well since you end up with more control of the cursor placement.
For curly braces and parentheses you can use Control + M.
Not sure about angle brackets in markup though.

Sublime 3 - Set Key map for function Goto Definition

I want to create an Eclipse style shortcut Ctrl+MouseClick to open the function/method. Sublime Text 3 has already this function called goto_definition but it is bound to F12.
But I'm not sure how to create this binding. I looked here for documentation but it was too complex. Can you one help me out with this simple key binding?
Edit: Following this article I was told to do this: http://webtempest.com/better-definition-navigation-in-sublime-text-3/
[
{
"button": "button1",
"count": 1,
"modifiers": ["super", "shift"],
"press_command": "drag_select",
"command": "goto_definition"
}
]
This doesn't seem to work, ctrl+shift+click executes nothing.
For anyone else who wants to set Eclipse style goto definition, you need to create .sublime-mousemap file in Sublime User folder.
Windows - create Default (Windows).sublime-mousemap in %appdata%\Sublime Text 3\Packages\User
Linux - create Default (Linux).sublime-mousemap in ~/.config/sublime-text-3/Packages/User
Mac - create Default (OSX).sublime-mousemap in ~/Library/Application Support/Sublime Text 3/Packages/User
Now open that file and put the following configuration inside
[
{
"button": "button1",
"count": 1,
"modifiers": ["ctrl"],
"press_command": "drag_select",
"command": "goto_definition"
}
]
You can change modifiers key as you like.
Since Ctrl-button1 on Windows and Linux is used for multiple selections, adding a second modifier key like Alt might be a good idea if you want to use both features:
[
{
"button": "button1",
"count": 1,
"modifiers": ["ctrl", "alt"],
"press_command": "drag_select",
"command": "goto_definition"
}
]
Alternatively, you could use the right mouse button (button2) with Ctrl alone, and not interfere with any built-in functions.
To set go to definition to alt + d. From the Menu Preferences > Key Bindings-User. And then add the following JSON.
[
{ "keys": ["alt+d"], "command": "goto_definition" }
]
If you want to see how to do a proper definition go into Sublime Text->Preferences->Key Bindings - Default and search for the command you want to override.
{ "keys": ["f12"], "command": "goto_definition" },
{ "keys": ["super+alt+down"], "command": "goto_definition" }
Those are two that show in my Default.
On Mac I copied the second to override.
in Sublime Text -> Preferences -> Key Bindings - User I added this
/* Beginning of File */
[
{
"keys": ["super+shift+i"], "command": "goto_definition"
}
]
/* End of File */
This binds it to the Command + Shift + 1 combination on mac.
On a mac you have to set keybinding yourself. Simply go to
Sublime --> Preference --> Key Binding - User
and input the following:
{ "keys": ["shift+command+m"], "command": "goto_definition" }
This will enable keybinding of Shift + Command + M to enable goto definition. You can set the keybinding to anything you would like of course.
ctrl != super on windows and linux machines.
If the F12 version of "Goto Definition" produces results of several files, the "ctrl + shift + click" version might not work well. I found that bug when viewing golang project with GoSublime package.
I'm using Sublime portable version (for Windows) and this (placing the mousemap in SublimeText\Packages\User folder) did not work for me.
I had to place the mousemap file in SublimeText\Data\Packages\User folder to get it to work where SublimeText is the installation directory for my portable version. Data\Packages\User is where I found the keymap file as well.
One should not just configure the goto_definition shortcut -- you would also need a shortcut to go back(and forth) after you jump to the definition.
Hence, consider configuring all three shortcuts: goto_definition, jump_back, and jump_forward as follows in your Key Bindings config file :
// go to the definition
{ "keys": ["ctrl+i"], "command": "goto_definition" },
// go back to the previous location
{ "keys": ["ctrl+h"], "command": "jump_back" },
// go to the next location
{ "keys": ["ctrl+l"], "command": "jump_forward" },
I find these three commands especially useful while trying to read code quickly.