Bug 177611

Summary: Undesirable blank line after auto-completed environment
Product: [Applications] kile Reporter: Eric Burgess <ericdb>
Component: generalAssignee: Michel Ludwig <michel.ludwig>
Status: RESOLVED FIXED    
Severity: wishlist CC: dhaumann, elflorican, ericdb, feg, jhnnslschnr, kishore96
Priority: NOR    
Version: unspecified   
Target Milestone: ---   
Platform: Ubuntu   
OS: Linux   
Latest Commit: Version Fixed In:
Attachments: Patch for not inserting blank line after autocompletion
Patch for not inserting blank line after autocompletion [version 2013-11-08]
Patch to make newline after environment completion optional

Description Eric Burgess 2008-12-12 18:48:36 UTC
Version:           2.0.3 and below (using KDE 3.5.10)
OS:                Linux
Installed from:    Ubuntu Packages

With auto-complete environments on, when I type "\begin{env}<enter>", kile adds a "\end{env}" line, but _also_ adds a blank line after that.  

After using kile for six months, I haven't found a single situation where that blank line is desirable.  It is particularly troublesome in a display math environment, because a blank line there results in an error.  So anytime I do a cases, matrix, or other environment inside a math environment, I am _forced_ to remove this blank line, or my document won't even compile.

I think this blank line should not be added, or at the very least, an option should be introduced to suppress it.

Following the directions in this thread ( https://sourceforge.net/forum/message.php?msg_id=5791607 ), I built my own kile without this blank line behavior, and it works great.  Kudos to the developers for being so responsive and helpful.
Comment 1 Thomas Braun 2008-12-13 13:02:26 UTC
I think I found one situation where the old behaviour is wanted.

Consider the following line:
\begin{abstract} asdfsdf
                ^
there we cursor is at the ^ position.

Hitting enter with "automatic environment closing" enabled and the
additional new line:
--------
\begin{abstract}
	
\end{abstract}
asdfsdf	
--------
and without the new line:
--------
\begin{abstract}
	
\end{abstract}asdfsdf	
--------

The latter case I think is not really what you expect.
So my suggestion would be to check if the cursor is at the end of line before inserting "\end{}" and only add a newline after it if the cursors was not at the end of line.
Comment 2 Eric Burgess 2008-12-13 16:55:17 UTC
The \[ \] pair (display math) is another case of this same behavior, not fixed by the code change in the discussion thread.

And I agree that the current behavior is more appropriate in the case where Enter is hit mid-line.

Comment 3 Felix Günther 2010-06-28 14:26:54 UTC
Created attachment 48414 [details]
Patch for not inserting blank line after autocompletion

Patch based on http://sourceforge.net/projects/kile/forums/forum/292014/topic/2669855?message=5791607 for not inserting a blank line after autocompletion; against the current revision r1141173
Comment 4 Felix Günther 2010-06-28 14:34:49 UTC
As this bug is pending in the current revision (r1141173), I attached a patch for this, which is just an update of the one proposed in http://sourceforge.net/projects/kile/forums/forum/292014/topic/2669855?message=5791607.

It now _does_ remove the blank line on \[ \] autocompletion, too.

Regarding the current behavior for mid-line cases I have observed two points:

 1. In many cases the text behind the auto-completed tag is place _in_ the completion, not after it. (e.g. "foo \[ bar" is replaced with "foo \[ bar \n\]" when Enter is hit after \[.

 2. The insertion of just "\n" at the end of the auto-completed text is not correct, it has to be "indentation + \n", as without that, the current indentation is lost. I didn't have the time to figure out, how this could be done.

So I suppose to use the attached patch and maybe add a check for mid-line execution where "indentation + \n" could be appended.
Comment 5 Dominik Haumann 2011-04-06 14:18:47 UTC
The patch looks ok to me, is it ok to commit?
Comment 6 Felix Günther 2013-11-08 10:38:22 UTC
Created attachment 83427 [details]
Patch for not inserting blank line after autocompletion [version 2013-11-08]
Comment 7 Felix Günther 2013-11-08 10:38:48 UTC
Hey,

unfortunately, this issue is still unfixed in the latest version of kile. I'm using my patch provided over 3 years ago since then and never encountered any problems with this. I'd thus be happy if it could be included future releases of kile.

I attached an updated version of the patch against the current git revision of kile (as of 2013-11-08), which I'll also send to the developer mailing list (as requested on http://kile.sourceforge.net/developers.php).

Best Regards,
Felix
Comment 8 Dominik Haumann 2013-11-08 13:30:16 UTC
@Kile devs: This patch is around for 2 years now and was never committed. Is there any specific reason for this? :-) It's always better to simply a reject a patch instead of keeping it open for years.
Comment 9 jhnnslschnr 2015-02-28 19:18:35 UTC
Created attachment 91347 [details]
Patch to make newline after environment completion optional

I'm not sure about the placement of the config item (LaTeX/Environments), because it does not only affect the completion done by the item right above it, but also the completion configured at Kile/Complete.
Comment 10 Zhenya 2017-09-29 06:55:45 UTC
@Kile devs, This behavior is still present and detracts from the work flow. The suggestion to make the new line only be created when the environment is auto completed mid line would be great, but at the very lease disabling it would be nice. This problem has apparently been around for a while, and doesn't appear to have been responded to.

Thanks for the otherwise awesome LaTeX editor!
Comment 11 Bug Janitor Service 2023-10-22 06:38:39 UTC
A possibly relevant merge request was started @ https://invent.kde.org/office/kile/-/merge_requests/51
Comment 12 Michel Ludwig 2023-10-22 12:07:22 UTC
Git commit 3e1c37661855d4845ca0bd54a8e7cda0cd6dccad by Michel Ludwig, on behalf of Kishore Gopalakrishnan.
Committed on 22/10/2023 at 14:07.
Pushed by mludwig into branch 'master'.

Environment completion: only insert newline when necessary

This change makes a newline to be inserted after \end{env} only if there
was text to
the right of the cursor when the user pressed enter. It also makes sure
the newline respects the existing indentation.
The special case of `\[...\]` is also handled.

**Test cases:**

In each case below, the pipe character is meant to denote the position of the cursor, not an actual character. Place the cursor as indicated (in insert mode) and press the Enter key on the keyboard.

**1.**
Before pressing Enter:
```
\begin{equation}|
```
After pressing Enter:
```
\begin{equation}
	|
\end{equation}
```

**2.**
Before pressing Enter:
```
\begin{align}
	\begin{split}|
\end{align}
```
After pressing Enter:
```
\begin{align}
	\begin{split}
		|
	\end{split}
\end{align}
```

**3.**
Before pressing Enter:
```
\begin{equation}|abc123
```
After pressing Enter:
```
\begin{equation}
	|
\end{equation}
abc123
```

**4.**
Before pressing Enter:
```
\begin{align}
	\begin{split}|abc123
\end{align}
```
After pressing Enter:
```
\begin{align}
	\begin{split}
		|
	\end{split}
	abc123
\end{align}
```

**5.**
Before pressing Enter:
```
\[|abcasdf
```
After pressing Enter:
```
\[
	|
\]
abcasdf
```

M  +8    -2    src/editorextension.cpp

https://invent.kde.org/office/kile/-/commit/3e1c37661855d4845ca0bd54a8e7cda0cd6dccad