Bug 312309 - Comments influence next line's indentation
Summary: Comments influence next line's indentation
Status: RESOLVED FIXED
Alias: None
Product: kate
Classification: Applications
Component: indentation (show other bugs)
Version: unspecified
Platform: Compiled Sources Linux
: NOR minor
Target Milestone: ---
Assignee: KWrite Developers
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-12-28 16:35 UTC by Gerald Senarclens de Grancy
Modified: 2013-03-17 19:04 UTC (History)
1 user (show)

See Also:
Latest Commit:
Version Fixed In:


Attachments
rewrite of Paul Giannaros' python.js (2.22 KB, application/javascript)
2013-01-25 09:41 UTC, Gerald Senarclens de Grancy
Details
allows manual testing (286 bytes, text/x-python)
2013-01-25 09:46 UTC, Gerald Senarclens de Grancy
Details
allows manual testing (315 bytes, text/x-python)
2013-01-26 17:30 UTC, Gerald Senarclens de Grancy
Details
PEP8 style indentation for Kate (5.09 KB, application/javascript)
2013-01-26 17:35 UTC, Gerald Senarclens de Grancy
Details
PEP8 style indentation for Kate (5.09 KB, application/javascript)
2013-01-28 07:58 UTC, Gerald Senarclens de Grancy
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Gerald Senarclens de Grancy 2012-12-28 16:35:10 UTC
Having a comment at the end of a line influences the next line's indentation behavior.

reproduce:
- in a new Python document, type: "for i in [0, 1]:  # important comment"
- hit [enter]

expected:
- next line is indented one level further

actual:
- next line is at same indentation level

-----------------------
reproduce (alternative case):
- in a new Python document, type: "def t():"
- hit [enter]
- type: "pass  # important comment"
- hit [enter]

expected:
- next line is unindented one level

actual:
- next line is at the same indentation level

Reproducible: Always
Comment 1 Sven Brauch 2012-12-28 16:51:51 UTC
Yes, this is a bug. The functionality is provided by katepart tough, so I'll put it there.
This behaviour is due to the very simple implementation of the python indenter in kate, which just looks at the last non-space character in a line for deciding whether to (un)indent. Ignoring comments would need to be implemented there.
Comment 2 Dominik Haumann 2012-12-30 14:54:20 UTC
Right, it's a bug in the indenter. Best would be if you provide a fix for the file python.js.
Comment 3 Gerald Senarclens de Grancy 2013-01-20 18:12:23 UTC
Dominik: I just had a short look at python.js; the fix is simple, but to make it proper I need to check whether a comment sign is part of a string;
syntax/python.xml provides very proper definitions for StringDetect - hence I'd like to know if the scripting interface exposes information on whether a char is part of a string or not. Thanks for a short answer.
Comment 4 Dominik Haumann 2013-01-20 18:46:04 UTC
@Gerald: The scripting API is documented here:
http://docs.kde.org/stable/en/kde-baseapps/kate/advanced-editing-tools-scripting.html#advanced-editing-tools-scripting-api

Maybe you can use:
-  bool document.isString(int line, int column);
-  bool document.isString(Cursor cursor);
Description: Returns true, if the attribute of the character at the cursor position is dsString, otherwise false.

A fix would be really cool :-)
Comment 5 Gerald Senarclens de Grancy 2013-01-25 09:41:26 UTC
Created attachment 76701 [details]
rewrite of Paul Giannaros' python.js

After looking into Paul's version I decided to do a complete rewrite b/c much of the great indent magic that is implicitly asked by PEP8 would create a mess. I also through out all helper functions as they were essentially present in script/libraries/string.js;
The file is a lot shorter now (I also removed all the debugging statements).
For simplicity I removed a special case for closing strings (there was no explanation on why this was needed and it didn't make sense to me.

To the best of my knowledge, this version of the file does exactly the same as the original, but fixes this bug and indents on closing backslashes (using a closing backslash is not considered pythonic, but some people still use/ need it if all else fails: http://www.python.org/dev/peps/pep-0008/#maximum-line-length). However, please don't commit/ push it yet as I still need to implement a proper unittest and the remaining behavior of PEP8 (as known eg. by emacs' python-mode.el).
Comment 6 Gerald Senarclens de Grancy 2013-01-25 09:46:51 UTC
Created attachment 76702 [details]
allows manual testing

Attached my manual test file if anyone wants to have a look at the current workings. The way I test: simply place the cursor at the end of any line an press enter. The new indent should match the indent of the following line.
Of course, the not yet implemented logic fails atm (unfinished sequences and argument lists) - but those weren't present in the initial version either.
Comment 7 Gerald Senarclens de Grancy 2013-01-26 17:30:48 UTC
Created attachment 76738 [details]
allows manual testing

updated manual TC
Comment 8 Gerald Senarclens de Grancy 2013-01-26 17:35:13 UTC
Created attachment 76739 [details]
PEP8 style indentation for Kate

Just finished the python indentation script implementing automatic indentation and unindentation for sequences as implied by PEP8 (and known from Emacs' python-mode).

Tested against the attached manual test file (hitting enter at the end of each line indents to the next line's level as expected).

I'd be thankful if someone could run a few tests on this before committing. Enjoy :)
Comment 9 Dominik Haumann 2013-01-27 19:23:23 UTC
Gerald: Now there are two javascript attachedments. Which one is the correct "python.js" indenter?

With respect to testing: We have a unit testing framework for indenters.
The data is in kate/testdata/indent/python
To run all indenter unit test, type: ./run.sh ~/kde/kate/build/part/tests/indenttest.shell
To run python unit tests, type: ./run.sh ~/kde/kate/build/part/tests/indenttest.shell python

Can you add a testcase for your case?
Comment 10 Gerald Senarclens de Grancy 2013-01-28 07:37:10 UTC
Comment on attachment 76701 [details]
rewrite of Paul Giannaros' python.js

Dominik: go for "PEP8 style indentation for Kate". Thanks for the hint about the tests for the indentation scripts. Once I get to write such a test - should I post it here?
Comment 11 Gerald Senarclens de Grancy 2013-01-28 07:58:30 UTC
Created attachment 76769 [details]
PEP8 style indentation for Kate

updated required Kate version to 4.10 (b/c of using require)
Comment 12 Dominik Haumann 2013-01-28 09:21:27 UTC
Best is to post a patch on http://reviewboard.kde.org that includes all changes.

And if you want, you can apply for a KDE contributor account. Once the patch is reviewed, you can commit yourself :-)
Comment 13 Gerald Senarclens de Grancy 2013-01-31 10:47:42 UTC
Git commit 7aba1f30cf581bb71ebbc8b31d85016c5981d706 by Gerald Senarclens de Grancy.
Committed on 30/01/2013 at 13:36.
Pushed by geralds into branch 'master'.

fixed and extended python indentation mode

Indentation is now correct even if a comment is at the end of a line.
Added two regression tests.

The updated version of the script also deals with opening and closing
argument lists and sequences as suggested in PEP8.
REVIEW: 108644

M  +119  -79   part/script/data/indentation/python.js
A  +3    -0    testdata/indent/python/dedentComment/expected
A  +3    -0    testdata/indent/python/dedentComment/input.js
A  +2    -0    testdata/indent/python/dedentComment/origin
A  +2    -0    testdata/indent/python/indentComment/expected
A  +3    -0    testdata/indent/python/indentComment/input.js
A  +1    -0    testdata/indent/python/indentComment/origin

http://commits.kde.org/kate/7aba1f30cf581bb71ebbc8b31d85016c5981d706
Comment 14 Gerald Senarclens de Grancy 2013-02-04 09:21:02 UTC
Git commit e6b9c40f083b7f2e52ab15541a9af2993f409985 by Gerald Senarclens de Grancy.
Committed on 04/02/2013 at 10:14.
Pushed by geralds into branch 'master'.

created additional tests for Python indentation

A  +4    -0    testdata/indent/python/dict1/expected
A  +7    -0    testdata/indent/python/dict1/input.js
A  +1    -0    testdata/indent/python/dict1/origin
A  +3    -0    testdata/indent/python/dict2/expected
A  +5    -0    testdata/indent/python/dict2/input.js
A  +1    -0    testdata/indent/python/dict2/origin
A  +3    -0    testdata/indent/python/function_args/expected
A  +5    -0    testdata/indent/python/function_args/input.js
A  +1    -0    testdata/indent/python/function_args/origin
A  +4    -0    testdata/indent/python/list1/expected
A  +7    -0    testdata/indent/python/list1/input.js
A  +1    -0    testdata/indent/python/list1/origin
A  +4    -0    testdata/indent/python/list2/expected
A  +7    -0    testdata/indent/python/list2/input.js
A  +1    -0    testdata/indent/python/list2/origin
A  +4    -0    testdata/indent/python/tuple1/expected
A  +7    -0    testdata/indent/python/tuple1/input.js
A  +1    -0    testdata/indent/python/tuple1/origin
A  +4    -0    testdata/indent/python/tuple2/expected
A  +7    -0    testdata/indent/python/tuple2/input.js
A  +1    -0    testdata/indent/python/tuple2/origin
A  +3    -0    testdata/indent/python/wrapped_line/expected
A  +5    -0    testdata/indent/python/wrapped_line/input.js
A  +1    -0    testdata/indent/python/wrapped_line/origin

http://commits.kde.org/kate/e6b9c40f083b7f2e52ab15541a9af2993f409985
Comment 15 Gerald Senarclens de Grancy 2013-03-17 19:04:40 UTC
Git commit db18ba1a5791240094c975a71d9d09f74e39a07b by Gerald Senarclens de Grancy.
Committed on 30/01/2013 at 13:36.
Pushed by geralds into branch 'KDE/4.10'.

fixed and extended python indentation mode

Indentation is now correct even if a comment is at the end of a line.
Added two regression tests.

The updated version of the script also deals with opening and closing
argument lists and sequences as suggested in PEP8.
REVIEW: 108644

M  +119  -79   part/script/data/indentation/python.js
A  +3    -0    testdata/indent/python/dedentComment/expected
A  +3    -0    testdata/indent/python/dedentComment/input.js
A  +2    -0    testdata/indent/python/dedentComment/origin
A  +2    -0    testdata/indent/python/indentComment/expected
A  +3    -0    testdata/indent/python/indentComment/input.js
A  +1    -0    testdata/indent/python/indentComment/origin

http://commits.kde.org/kate/db18ba1a5791240094c975a71d9d09f74e39a07b
Comment 16 Gerald Senarclens de Grancy 2013-03-17 19:04:40 UTC
Git commit cdc845febdac578b2635571fed287bafd13c4647 by Gerald Senarclens de Grancy.
Committed on 04/02/2013 at 10:14.
Pushed by geralds into branch 'KDE/4.10'.

created additional tests for Python indentation

A  +4    -0    testdata/indent/python/dict1/expected
A  +7    -0    testdata/indent/python/dict1/input.js
A  +1    -0    testdata/indent/python/dict1/origin
A  +3    -0    testdata/indent/python/dict2/expected
A  +5    -0    testdata/indent/python/dict2/input.js
A  +1    -0    testdata/indent/python/dict2/origin
A  +3    -0    testdata/indent/python/function_args/expected
A  +5    -0    testdata/indent/python/function_args/input.js
A  +1    -0    testdata/indent/python/function_args/origin
A  +4    -0    testdata/indent/python/list1/expected
A  +7    -0    testdata/indent/python/list1/input.js
A  +1    -0    testdata/indent/python/list1/origin
A  +4    -0    testdata/indent/python/list2/expected
A  +7    -0    testdata/indent/python/list2/input.js
A  +1    -0    testdata/indent/python/list2/origin
A  +4    -0    testdata/indent/python/tuple1/expected
A  +7    -0    testdata/indent/python/tuple1/input.js
A  +1    -0    testdata/indent/python/tuple1/origin
A  +4    -0    testdata/indent/python/tuple2/expected
A  +7    -0    testdata/indent/python/tuple2/input.js
A  +1    -0    testdata/indent/python/tuple2/origin
A  +3    -0    testdata/indent/python/wrapped_line/expected
A  +5    -0    testdata/indent/python/wrapped_line/input.js
A  +1    -0    testdata/indent/python/wrapped_line/origin

http://commits.kde.org/kate/cdc845febdac578b2635571fed287bafd13c4647