Odd RubyMine indenting when enter is pressed - rubymine

When I press Enter this happens:
def spend_cash
self.totals[order.type] = if order.cash > 0
// cursor is here
order.cash / order.price
end
end
How do I fix something like this?
I want it inline with order.cash

This area might be the one to experiment with:
Preferences | Editor | Code Style | Ruby
... then try the settings on the Tabs and indents tab to see which works best for you.
It's probably the checkbox Use indents relative to expression start

Related

Is there a way to disable the default behaviour of ctrl+enter in find and replace in vscode?

VScode has an annoying feature where it adds a new line into the text box during find and replace when I press ctrl+enter. I would like to use the keyboard shortcut ctrl+enter to replace all, but when I configure it that way in keyboard shortcuts, it is still inserting a newline before replacing, adding an extra newline to every match that is replaced. Example:
Find: "foo"
Replace: "bar"
Before:
foofoofoo
foo
Expected result:
barbarbar
bar
Actual result:
bar
bar
bar
bar
This is highly infuriating since I've already become accustomed to this keyboard shortcut and it is disrupting my normal workflow. Does anyone know how to fix this?
Use CTRL+K+S , you will get list of all defaults.
Change whatever you want from the first option in the line of edit.
This issue is now fixed in at least 1.67, probably a version before that.

VSCode: keeping indents on empty lines

Is it possible to make VSCode keep indents on empty lines?
Could not find such a setting neither natively nor in "Beautify" extension.
Example of desired behavior:
UPDATE: eventually I've just switched to Prettier - and never had to think about code style again as it's just being formatted automatically for me.
Go to File > Preferences > Settings. On the right side, add the line:
,"editor.trimAutoWhitespace": false
It worked for me perfectly.
Bit of an old question, but I found that a combination of the settings:
"editor.trimAutoWhitespace": false,
"editor.renderWhitespace": "all"
...worked for me.
NOTE: (19/03/2021)
It appears that this issue, registered on GitHub, is still an open case.
As of: 2022-02-01
Here is the solution that worked for me even with VSCODE formatting(linting) on:
There are two native VSCODE settings that format your code automatically:
"editor.formatOnSave": true,
"editor.formatOnType": true,
These settings affect your text editor only if
"editor.defaultFormatter" is not null
Here are the problems
The "editor.trimAutoWhitespace" VSCODE setting solves the problem but only works on the "editor.formatOnType" autoformat, when you save the document this setting is ignored.
You may also want to trimAutoWhitespaces on all cases but the ones where you are leaving an indent on an empty row. (this was my case).
In my case, while coding, I test my code in the terminal chunck by chunck. The problem with, in my case, python is that the end of a for loops and a conditional is defined by indentation. In this case since the terminal run the code line by line, and not in chuncks, with the removal of the indentation, the terminal thinks that the for and if commands are finished before they actually are.
Here is the solution:
Never define an edit.defaultFormatter in your VSCODE settings define it per language.
How do you do that?
Change VSCODE "editor.defaultFormatter" to null as in:
"editor.defaultFormatter": null,
Change the language formatter to your prefered one. In my case:
python.formatting.provider": "black"
VSCODE gives you the possibility of choosing a formatting (linting) provider for any language. These providers' rules are way more detailed than the VSCode native rules. In VS code press CRTL+SHIFT+P and search for "preferences: Configure Language Specific Settings...". Then define your formatting provider for said language.
Every provider (in my case "black") provides the option of ignoring some sort of formatting rule. Set the ignore argument for this provider.
In my case I only ignore 2 rules and my setting is:
"python.formatting.blackArgs": [ "--ignore=E501,W293" ],
Rule E501 = Row with more than 80 character.
Rule W293 = Empty Row containing only WhiteSpaces.
Ignoring W293 allows the Auto Trim White Spaces to keep working for every kind of trailing White Spaces except for the case referred to in W293.
I had the same issue but after removing :
"editor.autoIndent": "none"
I was good to go.
Timestamp:
2021, using VS Code v1.56.2
Use Case:
You use editor.action.trimTrailingWhitespace. Later you wish to write new code in a function. You navigate to a blank line within the function, but discover this line isn't indented and is completely empty. In this scenario, you have four choices:
Hit Tab multiple times until your cursor is at the proper indentation.
Navigate up to a line with proper indentation, press End, then press Enter.
Click your mouse to the right of a properly indented line, then press Enter.
Press Ctrl + ] repeatedly to indent the empty line.[See Footnote 1]
This answer automates this workflow by inserting the indentation for you (when you navigate to an empty line surrounded by indented lines).
Background:
Spent 3 hours trying all of the solutions on this page (and many different extensions). Couldn't find anything else which worked.
Solution via Extension
implicit-indent Extension by jemc:
https://marketplace.visualstudio.com/items?itemName=jemc.vscode-implicit-indent
Automatically indents an empty line when navigated to (if its surrounding lines are indented).
Functionality
From experimenting, here's how implicit-indent seems to work:
Checks and matches either tab or space indentation automatically.
Determines spaces or tabs based on indentation of surrounding text, regardless of editor settings.[See Footnote 2]
Clicking on an empty line automatically indents to match surroundings.
If pressing the up ↑ or down ↓ arrow key navigates the caret (a.k.a. text cursor) to an empty line, implicit-indent will automatically indent the line and place the cursor to the right of the indentation (making it feel the same as if the line were already indented).
-> Before ↑ or ↓ is pressed, it doesn't matter what column the caret is on, this extension will trigger regardless.
 
Navigating to an empty line matches nested indentation.
Assume you have the following:
Where |n| represents a line
and c represents the caret / text cursor
and only line |2| is empty.
|1| indented line
|2| (empty line)
|3| c indented line
^ assume caret is on line |3|
(caret can be at any column)
Pressing the up ↑ arrow key results in the following:
|1| indented line
|2| c
|3| indented line
^ Line |2| (previously empty)
is automatically indented to
match nested indentation
of line |3|
and the caret is automatically moved
to the end of the inserted whitespace
(as if the line were already indented).
VS Code
As of 2021 May, the following GitHub issues are still unresolved:
https://github.com/microsoft/vscode/issues/49661
https://github.com/microsoft/vscode/issues/54210
https://github.com/OmniSharp/omnisharp-vscode/issues/1980
https://github.com/OmniSharp/omnisharp-vscode/issues/1680
Using implicit-indent is the only workaround I've discovered so far.
I'll amend this answer when automatic indentation is added to VS Code's native settings.
[Footnote 1]
As an alternative to this extension, you can use Tab or the following indent/unindent functionality built-in to VS Code to manually indent an empty line. Slightly out of scope, but it's a good hotkey to know about.
Default built-in hotkeys are:
Hotkey: CTRL + ]
Command: editor.action.indentLines
What it does:
Indent current line (if no selection)
or indent all selected or partially-selected lines.
Hotkey: CTRL + [
Command: editor.action.outdentLines
What it does:
"Outdent" (a.k.a "unindent" a.k.a dedent)
current line (if no selection)
or outdent all selected or partially-selected lines.
[Footnote 2]
Note: regardless of whether you use this extension or not, you can quickly replace your current document's indentation (to either leading tabs or leading spaces) by using the following VS Code commands:
editor.action.indentationToTabs
editor.action.indentationToSpaces
That is probably eslint (and/or beautify) doing that. Look at
"no-trailing-spaces": ["error", { "skipBlankLines": false }],
I have that in my eslintrc.json file and so I get errors on blank lines with spaces or tabs on them. Setting "skipBlankLines" to true might work for you.
Answer edited as for VSC version 1.56.2 and newer.
The command editor.action.insertLineAfter makes a new line and moves the cursor there preserving the indentation.
To bind that command to Enter key, go to your keyboard shortcuts (press ctrl + k ctrl + s) then press the page with the pivot arrow icon on the far right top.
add the following inside keybindings.json to look like so:
// Place your key bindings in this file to override the defaults
[
{
"key": "enter",
"command": "editor.action.insertLineAfter",
"when": "editorTextFocus && !editorReadonly"
},
]
It worked for my without re-opening of VSC
I was having this issue with writing Flutter code.
Only solution for me was to change my default formatter to Flutter.
UPDATE: I also needed to disable Dart: Enable SDK Formatter

PhpStorm new tag instead of new line

I've got a problem with php storm. Every time I press Enter after a word, PhpStorm makes up a tag instead of a new line as you can see here:
Do you know where I can disable this function?
Please check your Emmet settings (expand key) -- you may have Enter as expand key -- use more traditional Tab or something else instead.
Settings/Preferences | Editor | Emmet

Eclipse-like comment formatting in IntelliJ IDEA?

In Eclipse, I can format comments by selecting them and pressing Shift + Ctrl + F. For example, when I select a method comment like this:
/**
* This method
* does some stuff.
*/
and press Shift + Ctrl + F, Eclipse automatically wraps it:
/**
* This method does some stuff.
*/
Is there anything comparable to this in IDEA?
EDIT: To clarify, I'm looking for comment formatting that also breaks lines that are too long into multiple lines.
The closest thing that you can get is Edit | Join Lines (Ctrl+Shift+J). You have to select the lines you want to join first.
To wrap long comments enable Settings | Code Style | JavaDoc | Wrap at right margin.
For Javadoc comments, you want to make sure the "Wrap at right margin" setting is checked. See Code Style > JavaDoc, under "Other". However, this setting only seems to take effect when you reformat the whole file, since a reformat of just the Javadoc (i.e., select the Javadoc, then do a Code (menu) > Reformat Code... or CtrlAltL) that exceeds the right margin doesn't force it to wrap. If I reformat the entire file, then it wraps at the margin as expected.
This seems like a bug (though one that doesn't seem to have been reported), since if you have to set the "Ensure right margin is not exceeded" checked, then selecting the Javadoc text and doing a reformat code does indeed wrap the lines. This setting is in Settings > Code Style > Wrapping and Braces. You can also do a search in the Settings dialog for "ensure right margin".
You'll still have to manually join the lines using CtrlShiftJ
This might be worthy of an improvement request to JetBrains.
Existing comment will be reformatted when you do "Reformat Code" (⌥⌘L in Mac).
#kghastie uncovered the key.
Steps:
Set the Code Style > Java > JavaDoc > Wrap at right margin setting.
Select the full lines of the entire JavaDoc comment.
Reformat Code (Ctrl-Alt-L or ⌥⌘L).
Lesser alternative:
Set the Code Style > Java > JavaDoc > Wrap at right margin setting and the Code Style > Java > Wrapping and Braces > Ensure right margin is not exceeded setting.
Select some text within a JavaDoc comment.
Join Lines (Ctrl-Shift-J) followed by reformat Code (Ctrl-Alt-L or ⌥⌘L).
Beware: This will leave all the selected lines joined even where you had paragraph breaks (<p/> or \n\n).
The JetBrains plugin Wrap to Column is made for this:
From the overview:
Wraps text to the specified column width. Similar to the Emacs command 'Fill Paragraph' and Vim's gq (format lines) command. This is a replacement for the native Intellij Fill Paragraph command, which doesn't work quite how I need it to.
This plugin provies two IDE actions:
Wrap Line to Column: Wraps selected text or the current line if no text is selected. This is useful for IdeaVim users who wish to pair the command with motions like vip (select current paragraph).
Wrap Paragraph to Column: Wraps the paragraph (multiple lines) in which the cursor appears. No selection is needed, and will be ignored.
I'm using IntelliJ 14 on a Mac, which has a Fill Paragraph command. Access it via the awesome universal Command-Shift-A action search feature. Works like a charm!
This is a hack, not a really good solution, but if you have a block of code that you want formatted like this and it's in serious need of auto format, because it's going over the 80 line max, or it's just unreadable...
You can just put if ("foo" == "bar") { on top of whatever you want formatted, and then and the} at the bottom of the if statement, to close it, and voila, your code should auto-indent, auto format, etc... Then take it out, highlight all of what you just formatted and press SHIFT+TAB to move it back 4 spaces and remove the dummy if statement

Configure eclipse so that backspace unindents

Having eclipse configured to use space instead of tabs, is it possible to configure eclipse so that the backspace key unindents like the tab key indents?
A demonstration to clarify what I mean with backspace unindent (the vertical bar stands for cursor position and the dots for spaces):
if(bar == 0) {
|foo = 0;
}
Pressing tab will indent 4 spaces:
if(bar == 0) {
....|foo = 0;
}
Pressing backspace only goes back 1 space:
if(bar == 0) {
...|foo = 0;
}
What I want is that it goes back 4 spaces:
if(bar == 0) {
|foo = 0;
}
You know that "shift+tab" will outdent a line already, correct?
Hmmm, I just took a look at preferences->general->keys. I only see indent line there, no outdent (or anything useful under "dent").
Personally, I would say that backspace is the key I hit second most often, after space. Rebinding it would drive me bonkers.
According to these very similar questions:
In Eclipse, how can spaces behave as tabs?
Is there a way to get Eclipse to treat 4 spaces exactly as it treats a tab?
it seems to be not possible :(
The feature will finally be available for next version of Eclipse 4.14 planned for 2019-12
https://www.eclipse.org/eclipse/news/4.14/platform.php#delete-spaces-as-tabs
Backspace/delete can treat spaces as tabs If you use the Insert spaces for tabs option, now you can also change the backspace and delete keys behavior to remove multiple spaces at once, as if they were a tab. The new setting is called Remove multiple spaces on backspace/delete and is found on the General > Editors > Text Editors preference page.
Hard way: set Eclipse to format indention using the tab character. Then backspace will remove the tab char
Easy way: start using the formatting feature of Eclipse to fix the formatting. Ctrl-Shift-F will format the selected lines or the whole file if there is nothing selected, using the formatting rules you configure.
Alternative: select the lines of text you want unindented and use shift-tab, it will unindent the selected lines.