-
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
Spike on auto indentation issues #6943
Comments
Seems like removing the vscode-python/src/client/language/languageConfiguration.ts Lines 37 to 40 in 9c61d0e
This also means that we should probably expand on the old Issues reported by users: Moving lines up and down:
# move the next line to the bottom (alt + down arrow)
move_to_end
class Foo:
def test(self):
pass
class Bar:
pass Pressing return
def foo():
pass
# press return here
def bar():
pass
class Foo:
pass
# set the cursor at the beginning of the line and press return Copy-pasting:
if 1 == 1:
print('This line belongs to the if')
newline = 'not part of if' # set the cursor before 'newline' and paste something there
if 1 == 1:
print('This line belongs to the if')
newline = 'not part of if' # highlight 'newline' and paste something on it Another possible broken case: if True: print('Of course!')
#<--cursor should be here |
TODO:
Outcome:
|
Re: Adapting Ruby indentation behavior for Python Please take a step back and carefully consider how much we can really borrow from Ruby. Crucially, Ruby has an Please see my comment on a related issue for an illustration of how the proper dedent level for If you want to look at existing work to possibly incorporate into this extension, perhaps it should be the highly regarded Python Indent extension. |
FTR:
Docs:
|
Here are the two regexes from the default VSCode config, expanded verbosely: increaseIndentPattern^
\\s*
(
(
begin |
class |
(
(
private |
protected
)
\\s+
def
) |
def |
else |
elsif |
ensure |
for |
if |
module |
rescue |
unless |
until |
when |
while |
case
) |
(
[^#]*
\\s
do
\\b
) |
(
[^#]*
=
\\s*
(
case |
if |
unless
)
)
)
\\b
(
(
[^#\\{;]
) |
(
(
\" |
' |
\/
)
.*
\\4
)
)*
( # .* )?
$ decreaseIndentPattern^
\\s*
(
[}\\]]
(
(
[,)]?
\\s*
(
# |
$
)
) |
(
\\.
[a-zA-Z_]
\\w*
\\b
)
)|
(
end|rescue|ensure|else|elsif|when
)\\b
) Here are the two regexes from the actual Ruby extension (more recent), expanded verbosely: decreaseIndentPattern^
(
(
\\s*
(
module |
class |
(
(
(
private |
protected
)
\\s+
)?
def
) |
unless |
if |
else |
elsif |
case |
when |
begin |
rescue |
ensure |
for |
while |
until |
(
(?=
.*? \\b
(
do |
begin |
case |
if |
unless
)
\\b
)
(
(
"
(
\\\\. |
[^\\"]
)*
"
) |
(
'
(
\\\\. |
[^\\']
)*
'
) |
(
[^#"']
)
)*
(
(
\\s
(
do |
begin |
case
)
) |
(
[-+=&|*/~%^<>~]
\\s*
(
if |
unless
)
)
)
)
)
\\b
(?!
[^;]* ;
.*? \\b
end \\b
)
) |
(
(
(
"
(
\\\\. |
[^\\"]
)*
"
) |
(
'
(
\\\\. |
[^\\']
)*
'
) |
(
[^#"']
)
)*
(
(
\(
(?!
[^\)]*
\)
)
) |
(
\{
(?!
[^\}]*
\}
)
) |
(
\[
(?!
[^\]]*
\]
)
)
)
)
).*
$ decreaseIndentPattern^
\\s*
(
[}\\])]
(
(
,? \\s*
(
# |
$
)
) |
(
\\.
[a-zA-Z_]
\\w*
\\b
)
) |
(
end |
rescue |
ensure |
else |
elsif |
when
)
\\b
) |
For our "else"/"elif"/"except"/"finally" needs, the focus is on the decreaseIndentPattern. |
One question I have is if I do: if a:
pass
if b:
pass
else:
pass
| # With the cursor here ... what must I do to get dedenting to work for an |
@jkyeung we can potentially borrow from Ruby thanks to how And the Python Indent extension approach can't be used by us as it overrides the the Enter key which is way to intrusive for us to do. |
Well, I guess you could say it's "similar", but then it's just as similar as Ruby's # Ruby
if a
puts 'a'
if b
puts 'b'
elsif c # must go with b
puts 'c'
end
end # Ruby
if a
puts 'a'
if b
puts 'b'
end
elsif c # must go with a
puts 'c'
end Python does not have this property. (Of course, the following code, as-is, isn't even valid, but the point is we can't know for sure which of two valid indentation options is the right one, and if we pick the wrong one, the meaning of the program changes.) # Python
if a:
print('a')
if b:
print('b')
elif c: # no way to know
print('c')
I get that. Without understanding those Ruby-handling regexes, I was preemptively guarding against "oh, it works for Ruby, so let's just slap it in for Python!". For all I know, it may be that those regexes happen to be doing what the Python Indent extension says it is doing for I'll hold further comments until more concrete details are presented. |
FWIW, I tried applying just the dedent rules that we reverted and had the exact same problems with inappropriate indent. Presumably the mere presence of |
Given everything I've looked at in the sprint, I don't think it's worth it to pursue this further at the moment. From what I can tell, VS Code make some assumptions about the existence of an end-of-block syntax (which Python almost uniquely does not have for "suites"). So at this point I'm not convinced that any of the existing options provided by VS Code will meet our needs. However, it will be worth bringing this up with the VS Code folks as soon as they have interest/time. |
If we redo our |
To address #6886 and #6927
At this point only the following things still have to be resolved:
We should look at how Ruby does it in their extension...
[spike conclusion]: #6943 (comment)
The text was updated successfully, but these errors were encountered: