Can I have multiple cursor points in live templates in intelliJ IDEA using the $END$? - intellij-idea

I can't seem to find a solution to having multiple cursor points in live templates in intellij IDEA

There is not a way to do have the cursor end at multiple points. But you can repeat the same variable in multiple places in your template, Then as you edit that variable, it changes in all the locations.
For example, this simple example will create three variables of the same type, the type of which I enter/edit after I expand the template. The actual template is below. You can copy it to your clipboard and then paste it as a template in the Live Template settings dialog. In the example, I used a variable named EDIT.
<template name="meex" value="$EDIT$ a;
$EDIT$ b;
$EDIT$ c;" description="Multiple Edit Example" toReformat="true" toShortenFQNames="true">
<variable name="EDIT" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_DECLARATION" value="true" />
</context>
</template>
UPDATE
To answer the OP's question in the comment...
To use the example template:
Copy the XML snippet shown above to your clipboard. Make sure there are no leading or trailing spaces or blank lines
Go to Settings (Ctrl+Alt+S / ⌘,) > Editor > Live Templates
On one of the grouping directories, such as "User", right click and select paste (or just type Ctrl+V).
This will paste the live template into the "User" group. Notice (A) the abbreviation (I chose "meex" for "Multiple Edit EXample") and (B) the "Expand with" setting.
Click OK on the Settings Dialog
Use the template by typing meex (or whatever abbreviation you set in step 4A) and then hitting Tab (or whatever Key was defined in Step 4B).
Start typing to the edit $EDIT$ placeholder variable, and hit Enter when done.
You can learn more about Live Templates in the IntelliJ IDEA help documentation here: https://www.jetbrains.com/help/idea/live-templates.html The best way to learn about them is to look at included templates, make a copy of them (Just right click one, select copy, then paste it elsewhere) and then edit the template and look at how the variables are configured in the "Edit variables" button on the template settings dialog. You probably do not want to edit the included templates directly so you do not corrupt them. And before playing around, you may want to back them up, just in case. Exporting them is the easiest way. See the "Sharing live templates through export/import" section at https://www.jetbrains.com/help/idea/sharing-live-templates.html

Related

Possible to manually trigger automatic source code formatting in STM32CubeIDE?

I use STM32CubeIDE version 1.6.1, and have noticed that CubeMX outputs formatted code (e.g. indented with 2 spaces). I would like to reapply this source code formatting manually (that is, manually trigger the source code formatting on selected files).
I have found this post: https://www.openstm32.org/forumthread6435 , which mentions:
go the preferences dialog and select C/C++ > Code Style > Formatter
click on “Edit...” change the tab size field. close the preferences
Then, select all the project you want to apply it, click right and select “Source > Format”
So, I'm not 100% sure the above quote refers to STM32CubeIDE, however, the first part is still there, in Window/Preferences I can get to this:
However, I cannot see how to trigger this source code formatting manually, on selected files. For instance, https://dvteclipse.com/documentation/e/Format_Multiple_Files.html for Eclipse (that STM32CubeIDE is built upon), mentions that by right-clicking source files, one can call the command "Format Source Files"; however, when I click my main.c in the STM32CubeIDE Project Explorer, I cannot see anything like that:
So, is there a way to manually trigger automatic source code formatting on selected (single or multiple) source code files in STM32CubeIDE?
Got it, finally - when you right-click the Core node, or any of its children, in Project Explorer in STM32CubeIDE, you get a "Source >" entry in the right-click context menu, which has only one child, "Format":

How to make my own Code abbreviation in intellij idea

in IDEA ,I can type soutto represent System.out.println();and I want to know how to make my own code abbreviation.For example, alias System.out.print(); to sounor any other names
This is called a Live Template in IntelliJ.
Go to File > Settings > Editor > Live Templates. From there, select Java and on the right, you'll see a plus sign. By clicking it, you'll get a 1. Live Template and that will get you a new abbreviation.
Name it soun and the text should be
System.out.print($END$);
Once you're done, make sure you've enabled that abbreviation by clicking the check box next to it.
The docs of IntelliJ cover in detail what the syntax is for these templates.
The $END$ syntax indicates the position of the cursor when the code snippet is complete, and you can no longer press Tab to jump to the next variable.
There are more variables to look at as well and other configurations to do!
Edit: there is an answer here for this question, but it looks like it doesn't necessarily answer the question asked on that post, so that's why I've decided to post a dedicated answer (tackling the issue at hand).

Abbreviations in PhpStorm

IntelliJ comes with 2 abbreviations that I have used a lot in the past, but cannot find in PhpStorm:
iter Iterate (for each..in)
itar Iterate elements of array (traditional loop)
So whenever I typed iter and pressed tab, it generated an enhanced for-loop. How can I achieve this in PhpStorm?
PHPStorm has live template so you can use it to generate several types of code snippets.
Live templates can be used to insert frequently-used constructs into your source code, for example, loops, conditions, various declarations, print statements, tags, and so on. To expand a code snippet, type the corresponding template abbreviation and press Tab. Keep pressing Tab to jump from one variable in the template to the next. Press Shift+Tab to move to a previous variable
I think this link is useful and also suggest that you read this link
for creating a new snippet you can go settings > Editor > Live Template

How can I enter a literal <TAB> character in IntelliJ/IDEA/PyCharm?

My configuration indents with four spaces, and I want to keep that. Occasionally (e.g. in a Makefile) I want to input a literal TAB character.
How can I force the IntelliJ-IDEs to input a tab or space, when it would not do so when I hit tab or space in that instance?
You seem to be asking two questions here:
1. How do I force IntelliJ IDE to input a tab, when it would not do so when I hit tab?
and
2. How do I force IntelliJ IDE to input a space, when it would not do so when I hit space?
I don't understand how the second case can arise. However, I have provided a solution to it as well.
Case 1. Insert a tab character when an IntelliJ IDE wants to replace it with spaces due to configuration
Solution
Use search and replace.
Details
Place the cursor where you want the tab to be
Press the X key
Select the X you just typed
From the main menu, choose Edit | Find | Replace to bring up the search and replace pane
Make sure there is an X in the search field
Enter \t in the replace field
Be sure the option Regex is checked
Be sure the option In Selection is checked
Click the Replace button
Case 2. Insert a space character when an IntelliJ IDE won't just let you type one (???)
Solution
Use search and replace.
Details
Place the cursor where you want the space to be
Press the X key
Select the X you just typed
From the main menu, choose Edit | Find | Replace to bring up the search and replace pane
Make sure there is an X in the search field
Enter a single space into the replace field
Be sure the option In Selection is checked
Click the Replace button
Install the plugin for Makefile support: https://plugins.jetbrains.com/plugin/9333-makefile-support
When I tried it today, this automatically use hard tabs in the editors for Makefile files.
Open another text editor, type a tab, and then copy and paste into the PyCharm editor. In MacOSX this worked for me using both Sublime Text 2 and TextEdit.
I can't think of any "direct" way. Probably the easiest way that I can think of is to write a Live Template to do it. See the help page on Live Templates for more information. When you write it, you may need to copy and paste a tab character in from another application.
However, when I tried it, IDEA saw it as just empty text and would not save it. So I used a variable with the "capitalize" function to capitalize a tab character.
Here's the template I created that you can paste into your templates. Now I type tab, hit the Tab key and I get a tab character. Of course you can change the abbreviation.
<template name="tab" value="$TAB$" description="Enter a tab Character" toReformat="false" toShortenFQNames="true">
<variable name="TAB" expression="capitalize(" ")" defaultValue=" " alwaysStopAt="false" />
<context>
<option name="OTHER" value="true" />
</context>
</template>
You could extend the idea to have multiple ones that enter multiple tabs. For example tab to enter 1 tab, tab2 for 2 tabs, etc.
Screenshot of it after use:
There may also be a way to hack a macro to do it. You could then assign the macro to a keyboard shortcut. I'll see if I can figure something out and update this if I do.
Use the menu option: Edit -> Convert Indents -> To Tabs.
Whenever I edit a makefile I do the "to tabs" conversion before I save the file.
UPDATE: Really sadly, I think the generated character still gets converted to spaces... Am checking...
There is an Action in JetBrains IDEs to enter a Tab character.
Here are the steps to use the Tab character action: (discovered in PyCharm 2020.3)
Double-tap the Shift key OR Help -> Find Action...
Type the 3 characters tab
Click on the action that is called Tab and shows the icon for the Tab character...
It remembers the last action you did, so if you have several tabs to add, you can get into a quick process by doing ShiftShift then Return each time.
Answer based on #Morfic's comment to the question. I think this is the cleanest and most sensible reply here so I figured it deserves a place as an answer (and needless to say, it worked for me).
If they're different types of files you could configure File -> Settings -> Editor -> Code style -> Tabs and Indents for each one to use either space or tab depending on how you want it.
One way to do this is to copy a TAB character from another text editor, then right click in your PyCharm file and use Copy / Paste Special -> Paste as Plain Text (Ctrl+Alt+Maj+V)
I guess this was not available in older PyCharm versions, because no other answer proposed it. Regular paste (Ctrl+V) doesn't work (replaced by spaces), but this one works. The function will also bypass other automatic formatting.
Inspired of Clare's suggestion,
assign the left tab key after finding 'tab' action.
At Actions tab, search by 'tab' and move your up/down arrow key and place there.
Then type Ctrl+1, you will see a popup. Select as follows and click OK.
Then, you might be asked "Do you want to remove other assignments?" if Tab key was already assigned. Click Leave because your usage won't conflict with the existing setup.

Idea Intellij include non-menu actions by default

Is there a way to ensure the Include non-menu actions checkbox in the Run actions window is always turned on by default?
I'm quite tired of having to press Сtrl+Shift+A twice to run a maven build (a non-menu action)!
There is not. Your best best would be to record a simple macro (Edit > Macros) and record the Ctrl+Shift+A twice. Give it a name and then map a different shortcut to it in Settings > Keymap
If you wanted to move the Ctrl+Shift+A mapping to the macro, In theory you should be to record the "Action:FindMacro" step followed by Alt+N to select the checkbox using its n accelerator. However a bug surfaced when I did that. When the macro runs, the "Open Tasks" dialog opens which is mapped to Alt+Shift+N. It appears that the when the macro interpreter sees the uppercase N, it adds a shift key to the playback. I went in and manually edited the 'N' to 'n' and that resolved the issue. (I reported the bug via IDEA-124492).
After working through that, another issue surfaced. The Alt+N sequence was getting sent too quickly. So I needed a pause between the ''Open Find Action Dialog''. Since I am unaware of a way to add a pause in a recorded macro (I opened feature request IDEA-124493 to allow such) I instead inserted a few Ctrl+A sequences as a hacked pause. Since the default behavior when opening the Find Actions dialog is for the previous search string to be selected, this does not end up changing any behavior.
If you want to use this macro, close IntelliJ IDEA. Open the file config/options/macro.xml. (See this document to learn where the IntelliJ IDEA config directory is.) If the file is not present, create one with the structure:
<?xml version="1.0" encoding="UTF-8"?>
<application>
<component name="ActionMacroManager">
</component>
</application>
Then in the <component> element, insert the macro:
<macro name="Find non-menu Action">
<action id="GotoAction" />
<shortuct text="control a" />
<shortuct text="control a" />
<shortuct text="control a" />
<shortuct text="control a" />
<shortuct text="alt n" />
</macro>
Save and restart IntelliJ IDEA. You can then remap the Ctrl+Shift+A mapping to it via Settings > [IDE Settings] > Keymap > Macros.
I found on my system I needed four of the "pauses" via the Ctrl+A commands. Three was intermittently successful. So it was right on the edge of the pause needed. You may need to increase those.
Just as a side note in case it helps you, you can assign a shortcut to a maven goal.