-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve auto-indentation behaviour #481
Comments
Added case 3. |
Case 3 actually has been reported in #385. |
Case 3 is fixed by #385
Case 2 requires AST analysis and extension currently has no facilities for this. |
Agreed. Guess this is why VS has options to configure how these things get formatted. |
Really? That's surprising to me. I'm so used to such indentation in vim and I find it very natural to have. Think about it: you're inside parenthesis, typing function arguments. Why would you not want to have an indentation, at least any of the two? It's a continuation of a function call and we always indent them, aren't we?
Could you please direct me to such configuration? I looked through the VSCode settings filtering them by "indent" and "format" and only found this relevant option:
Are there any plans to add such facilities? Maybe this case could be moved out into a separate issue so that you could keep it on your backlog if it's not possible to implement it with the current facilities? |
May be i was wrong, I just assumed this was one of the settings. Could be something in Reshparer. Either way I could be mistaken. |
@balta2ar - it depends on the language and I meant big VS IDE. C# simple preserves user indentation of arguments. By default it indents one level deeper and not by ( or first argument In R there is setting Yes, there are plans to have AST analysis, but this is pretty big work item. Working on it :-) |
That's great! Thanks for giving a heads up! Oh, and speaking of AST. There is no doubt you know how to do it right, but I noticed this comment today: atom/atom#13877 (comment)
I haven't tried it in practice myself yet, but it sounds promising from the description. Just spreading the word :-) |
I can ensure you we have lots of parsers here at MS :-). |
I agree with case 1. It makes sense to have the default indentation behavior comply with PEP8 (https://www.python.org/dev/peps/pep-0008/#indentation). |
Well, isn't this lib about Python? It has PEP8 which has rules about indentation. There is some room left for matter of taste on where to add linebreaks in the case of long lines, but in this case the user makes a choice about that linebreak. |
@tino PEP 8 is technically the style guide for Python's standard library, it just so happens people often adopt it as their own style guide. That isn't saying that we shouldn't aim to follow PEP 8, just that we also have to be flexible for those that choose to not follow PEP 8. |
Fair enough. But this also isn't a formatter or a linter, it's just a default indentation. You can always remove the spaces, as you now have to insert them. And then defaulting to the default Python style guide doesn't seem like a bad idea to me :D. |
Atom has a nice plugin that works perfectly: https://atom.io/packages/python-indent IIRC the author tried to implement the same functionality for VS code, but concluded that it wasn't possible 😞. |
I think it's still not there :/ |
Hello, I'm the person who wrote the parser for the python-indent package linked above, and I'm trying to replicate the behavior in VSCode. I believe it can be done with OnTypeFormattingEditProvider but I'm running into trouble using it (I have next to 0 javascript experience). Using the extension code below, I never see the 'use strict';
import * as vscode from 'vscode';
// const PY_MODE: vscode.DocumentFilter = { language: 'python', scheme: 'file' };
const PY_MODE: vscode.DocumentFilter = {};
class PythonIndent implements vscode.OnTypeFormattingEditProvider{
public provideOnTypeFormattingEdits(
document: vscode.TextDocument, position: vscode.Position, ch: string,
options: vscode.FormattingOptions, token: vscode.CancellationToken):
vscode.TextEdit[] {
console.log("hi!");
let pos = new vscode.Position(0, 0);
let x = new vscode.TextEdit(new vscode.Range(pos, pos), "");
return [x];
}
}
export function activate(ctx: vscode.ExtensionContext): void {
ctx.subscriptions.push(
vscode.languages.registerOnTypeFormattingEditProvider(
PY_MODE, new PythonIndent(), "a"));
} |
I don't have experience in Javascript either, but maybe @DSpeckhals can comment? |
I assume Dustin is still quite happy with Atom. The only reason I'm looking to do anything with VSCode is b/c I routinely need to open large text files which Atom still struggles real hard with. |
I started going down the path of implementing similar indentation as the Atom package into a VS Code extension, but as of several months ago, came up short. If I remember correctly, there wasn't (and perhaps still isn't) a good hook to capture the end of line event, then insert the correct indentation characters on the newline event. If there's something that will work in VS Code that I'm unaware of, I'd love to do it here too. I just haven't invested the time to dig recently. If there were good API's, I'd probably separate the core indentation logic that was written by @kbrose into a separate repository and npm package, then write the implementation details for both Atom and VS Code. |
Thanks for looking at this @DSpeckhals! I was looking at the API, and there is certainly a lot less exposed than w/ Atom. No sort of newline event to hook onto. I feel like I may bang my head against it more this weekend and see if I can make something work. |
Can folks please try Pylance and let us know if they still reproduce these issues? Add:
to |
Specifically, see the instructions and announcement at https://devblogs.microsoft.com/python/python-in-visual-studio-code-december-2022-release/#auto-indentation-with-pylance for Pylance to do more intelligent indenting. |
I added the Repro: def f(a,<newline> jumps to the beginning of the next line, instead of to the opening parenthesis indentation. Thank you for working on this :) |
@amueller Thanks for checking! Could you please file this as an issue on https://github.com/microsoft/pylance-release? |
@amueller how about syntactic indent/dedent? E.g. typing |
I commented on the existing issue. Having syntactic correct indent would be great. Ideally the stylistic ones would also work, though; I think that's basic pep8 that people that don't even know about pep8 instinctively follow. |
One thing at a time. 😉 Ultimately it's up to the Pylance team to decide how far they want to take this, but I'm personally supportive of them do simple, reasonable stylistic formatting as long as people realize they will very likely not get knobs to tweak anything (I would argue you should be using a formatter in that case). |
FYI if you are not getting automatic indentation with Pylance by turning on |
I switched to the pre-release (v2023.1.11), I have: {
"[python]": {
"editor.formatOnType": true
},
"python.languageServer": "Pylance"
} in my I'm still seeing the |
ok open and closed settings again and it was gone again (this time without the comma for some reason) |
It seems we had a bug in the Python extension that wasn't automatically opting people into the feature when |
The trailing comma makes it invalid JSON, so there should have been a red squiggle and a warning that your settings were invalid. |
There was a red squiggle but I didn't see a warning. Not sure if my description was clear: I copy & pasted the setting, saved it, closed VSCode, opened VSCode, open the settings, and now my settings were clearly invalid JSON. |
@amueller sorry -- would you mind confirming if you still see this using v2023.1.10111008 of the Python extension? I was able to reproduce this with an older version, but it does no longer happen for me with the newest ones: We merged #20480 n the Python extension a couple of days ago, which we believe has the fix for the problem you're seeing. |
This was part of the bug that was fixed. We are pushing a point release today that should fix this in stable while it was fixed in our pre-release a few days ago. |
After switching to the insider version I was able to install |
We have enabled format on type for all Python files by default when using Pylance. For those who want to disable this behaviour, you can add the following setting to your settings file (View > Command Palette... > Preferences: Open User Settings (JSON)):
If you have any problems or feedback, please open a separate issue at the Pylance repo detailing the behaviour you'd like to see 😊 |
Environment data
VS Code version: Version 1.19.0
Commit 816be6780ca8bd0ab80314e11478c48c70d09383
Date 2017-12-14T09:56:48.842Z
Shell 1.7.9
Renderer 58.0.3029.110
Node 7.9.0
Architecture x64
Python Extension version: 0.9.1
Python Version: Python 3.6.3
OS and version: ArchLinux
Case 1
The cursor is at
|
position (about to add second argument toprint
), and I pressEnter
:Actual
It becomes:
Expected 1
OR Expected 2 (this one is preferred)
Case 2
Actual
The cursor is right after the closing parenthesis and I press
Enter
.It becomes:
Expected
Case 3
Actual
The cursor after the comment. Press Enter:
It becomes:
Expected
Same with comments after for loops:
after pressing Enter becomes
Expected:
Also look at #3284
The text was updated successfully, but these errors were encountered: