Can Visual Studio Code be prevented from indenting a level deeper after opening braces? - visual-studio-code

If I enter "foo ()\n\t{\n"
foo ()
{
<- cursor is now here
I'd like it to be at the same level of indent as the line above (4 spaces in this example), not pushed in a level (8 spaces in this example). So:
foo ()
{
<- cursor here and all will be well in the world
I've tried completely turning off all the indent/tab settings I can find. I'm even prepared to hack around with an extension so a pointer to any in-depth documentation around type/tab commands would also be welcome.

Well, erm, there is an Editor Setting in Preferences, where you can adjust the tabs "editor.tabSize": 4, but I don't there is any level scaling, but I've also discovered a little "hack". If you make double slashes after
{//
and hit return it looks like this:
{//
Hello_world!!
}
Kind of silly, I guess.

Related

Make spaces act and look like tabs in VS Code

When writing a source file in VS Code that is styled with spaces of a specific width (maybe determined by the .editorconfig file), how can I force VS Code to treat the spaces like tabs without reformatting the file?
For example, the indent width may be 4 spaces, so rather than displaying 4 spaces in my editor, I'd rather see one tab space character with a width of 4 spaces.
The selection aspect of space-tabulated code is supported with VSCode 1.52 (Nov. 2020) and:
Sticky Tab Stops when indenting with Spaces
If you prefer to indent your code with spaces, there is a new setting called editor.stickyTabStops, which makes VS Code treat cursor movements in leading spaces similar to tabs.
Appearance
You said vscode is:
displaying 4 spaces in my editor
I assume that means you're getting those little Interpunct dot characters coming up like this ยทยทยทยท
If you want those to go away, so they appear more consistent with tabs, go into VScode settings (JSON) and enter the following:
"editor.renderWhitespace": "selection"
Assuming everything else is default, both tabs and spaces should be rendered as regular whitespace. But that alone it doesn't really help, because it doesn't allow you to distinguish nested structures i.e. you can't tell how many levels of indentation you're at.
To fix that, there's 2 things.
(minimum) Set indent guides explicitly in your user settings, this will render vertical lines at each indentation level, no matter if the file is using tabs or spaces:
"editor.renderIndentGuides": true
(optional extra) If you want to take it further, there are a few extensions you can try, but the one i recommend is indent-rainbow. There are lots of options for it, but i have mine config'd so after a certain level of indentation it becomes more obnoxious, because i treat it as a code smell i.e. i like to minimize how much i nest if possible.
The end result of doing all this is that tabs and spaces are rendered exactly the same way, and you can't tell the difference unless you have part of your code highlighted:
Behavior
To make the behavior of indentation more consistent, the following should be in your settings if it's not already applied by default:
"editor.detectIndentation": true,
"editor.insertSpaces": true,
"editor.useTabStops": true
As for this:
source file in VS Code that is styled with spaces of a specific width (maybe determined by the .editorconfig file)
I don't think this is possible, or at least not natively. You may be able to find/write an extension that can do detection based on tabsize since there is in fact a property called:
"editor.tabSize": 4,
Not sure if this will help, but you can do selective setting overrides based on filetype, for example:
"[yaml]": {
"editor.insertSpaces": true,
"editor.tabSize": 2,
"editor.autoIndent": "advanced"
}

How to add custom indentation rules to align multiple lines of code when creating a language extension?

I'm authoring an extension for Racket (lisp dialect) and I'd like to customize the indentation rules further than just "when line ends with X, indentation +1, else indentation -1". However, as far as I understand it, that exactly is the point of increaseIndentPattern and decreaseIndentPattern in language-configuration.json.
In particular, I'd like to specify some rule that goes like this:
(call-foo first-arg
......... second-arg
......... (third-arg-function foo
..............................bar))
Where the dots denote the auto-indentation (spaces) automatically inserted by VS Code upon pressing return on the preceeding lines. Is it possible to do something like this?

Xcode - changing the indentation rules for switch statements

When I write a Swift switch statement it indents the code like this:
switch foo {
case 1:
// stuff happens
default:
// other stuff happens
}
I want it to indent like this:
switch foo {
case 1:
// stuff happens
default:
// other stuff happens
}
Is there any way to do this? All the questions I can find on the topic either point to plugins (which no longer work in the latest version of Xcode) or discuss which way is "correct" rather than offer a way to change it.
Xcode 11 now appears to have a preference for this (checked in beta 5):
Go to File > Preferences... > Text editing > Indentation
Check the Swift checkbox under Indent switch/case labels in:
Party!
As of Xcode 9, there is an indentation settings page in Preferences/Text Editing/Indentation, but this includes no options for advanced indentation for each statement.
In Xcode 9, one may now re-indent code by marking it, pressing Tab or Tab + Shift
With Swift 3, Xcode 8.3.3
The only way I know it is:
Select your code,
right click -> Structure -> Shift Left
right click -> Structure -> Shift Right
Actually there is way to do this, you have to edit your snippets file, write extension for this or use the solution in above answers...
In fact you can hack it, create snippet for switchy (or something else related to switch, hit enter and your custom snippet should be applied)
http://nshipster.com/xcode-snippets/
If you want to indent the switches - Tick the Swift checkbox in the Indentation section of Text Editing.
Note that if you use SwiftLint, having Switches that indent the case will throw a warning as it is generally accepted that the case will align with the switch. (Check box off)

Anonymous function assignement in CoffeeScript

I'm struggling to make the following code work:
class Elements
constructor: ->
loadAll: ->
success = (data) =>
doSomething
doSomethingElse
$.post bla success
It thinks everything after doSomething is not part of the success callback body.
Edit: I didn't realise CS was space/tab sensitive. TextMate doesn't help with it; Is there a better IDE for CS?
TextMate is probably the best editor for CoffeeScript, with a bundle maintained by CoffeeScript creator Jeremy Ashkenas. (The bundle is also compatible with Sublime Text 2, which is a nice cross-platform alternative.) The solution here is to:
Convert all tabs to spaces in your existing files (as Jeremy suggested in his comment), and
Switch to Soft Tabs using this switcher at the bottom of the window:
That way, when you hit the Tab key, the number of specified spaces is inserted. By avoiding the use of actual tab characters, you avoid the inevitable confusion that results from n spaces looking the same to a human as 1 tab, but being ambiguous to the compiler.
The norm in the CoffeeScript community is 2 spaces, but there are plenty of folks using 4 spaces (which is the official standard in Python-land); just pick one and stick with it.
If you do decide to make the switch to Sublime Text 2, you can add these lines to your Base File.sublime-settings preferences file to force soft tabs with the given number of spaces:
"tab_size": 2,
"translate_tabs_to_spaces": true,
In sum: Hard tabs and languages with significant whitespace don't mix. Configure your editor to use soft tabs, and CoffeeScript responsibly. :)

Are there any hidden code formatting settings for Javascript in NetBeans?

I do PHP/Javascript development in NetBeans, and I really like the environment, except for one thing - in Javascript, when I press ENTER after a statement, and type the opening brace, it is indented. Like this:
if ( a == b )
{}
I want the brace to stay on the same level, like this:
if ( a == b )
{}
So that when I press ENTER again, I'd get this:
if ( a == b )
{
}
Can this be done and how?
Sorry, I don't have the answer to your question. I too have searched in vain for somewhere in NetBeans 6 to configure JavaScript formatting.
However, you should note the following:
In languages like Java it's legitimate to choose between opening brace on the same line vs. opening brace on a newline. In JavaScript, however, you should really stick to the former as the latter can give rise to ambiguities that can affect the interpretation of the code. See here. (I know the example you cite relates to the if statement, but presumably you want to be consistent.)
Great news for netbeans devotes here: (netbeans 7.0)
Tools -> Options > Editor > Code Templates: choose Language (Javascript in this case)
Look for "if" Abbreviation:
Change Expanded text definition:
from this:
if (${expr}){
${cursor}
}
to this:
if (${expr})
{
${cursor}
}
Save options.
Now inside a js file type if and press [tab]... and you got it...
Can you imagine all possibilities with these templates?