Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

Auto indent on paste behaves oddly with already indented lines. #5473

Closed
NatalieWolfe opened this issue Feb 10, 2015 · 29 comments
Closed

Auto indent on paste behaves oddly with already indented lines. #5473

NatalieWolfe opened this issue Feb 10, 2015 · 29 comments

Comments

@NatalieWolfe
Copy link

When copying a segment of code that starts with a new-line followed by tabbed-in code while "Auto Indent On Paste" is enabled, consecutive pastes will each be an additional tab deeper. I tested this with C++, Java, and JavaScript which makes me believe it is not a problem with the language specs.

Here is an example in the form of an animated gif:
autoindent-2

It isn't just comments that this happens with, but it does require the previous line to be selected. Specifically, this happens when the copy-buffer matches a regex like /^\n\s+[^\s]/. That is, it starts with a newline, followed by one or more whitespace characters and then a non-whitespace character. And the previous line must be a blank line.

@winstliu
Copy link
Contributor

What version of Atom is this on?

/cc @maxbrunsfeld

@NatalieWolfe
Copy link
Author

Oops, should have included that. I'm using Mac 10.10.2 with Atom v0.177.0, but I've been experiencing this bug for a few versions now (not entirely sure when it first presented).

Interesting, just tried to reproduce this on my Windows build box too. It has v0.175.0 installed. On that machine the behavior is reversed. When Auto Indent On Paste is enabled then pasting behaves as expected (each consecutive line is pasted at the same depth). However when that option is disabled then the first paste behaves as expected, but the 2nd through nth pastes are all one level deeper than the last. Not sure how to update the Windows version to see if it persists in 0.177.0.

@izuzak
Copy link
Contributor

izuzak commented Feb 13, 2015

@NatalieWolfe I'm having trouble reproducing this in the latest version of Atom:

$ atom -v
0.180.0-2dac7fd

Here's what I'm doing:

paste

Is there something I'm doing differently than you are?

Can you try reproducing this again and record a gif in which the invisible characters are shown as well as the keybinding resolver (cmd+.)? Also, can you record the gif so that includes the point at which you select and copy text into the clipboard for the first time (it's not shown in the gif above)?

Atom should update to the latest version on its own, but you can download the latest version here as well:

https://github.com/atom/atom/releases/latest

@NatalieWolfe
Copy link
Author

@izuzak: The link you provided goes to 0.179.0, which is what I have installed on my Mac at the moment.

Here is an updated gif, starting from a completely new window. Auto Indent on Paste is enabled during this demonstration.

autoindent-3

If you would like me to make another where I type slower (so you have more time to see the keybinding resolver updates) let me know.

@NatalieWolfe
Copy link
Author

I notice on your gif, @izuzak, that you are using a plain text file. I don't think Auto Indent on Paste works in plaintext files (there is no structure to the text for it to use).

@izuzak
Copy link
Contributor

izuzak commented Feb 13, 2015

I notice on your gif, @izuzak, that you are using a plain text file. I don't think Auto Indent on Paste works in plaintext files (there is no structure to the text for it to use).

Ahhhh, that was it, thanks @NatalieWolfe! Seeing it now as well.

@karai17
Copy link

karai17 commented Apr 30, 2015

I am running the current Atom (194) on Windows 7 and this issue persists.

@muppetjones
Copy link

There are many indentation problems, and I'm trying to link all of them together in a single issue: here

@jesseleite
Copy link

I'm finding that Auto Indent works differently than Auto Indent On Paste setting in Atom. There's a consistency issue between the two...

If I take this PHP method:

        public function name()
    {
if ($something) {
    return $something;
}
            }

And paste it into a PHP class, I get:

    public function name()
{
if ($something) {
return $something;
}
        }

So Auto Indent On Paste is doing something, but it's not doing it correctly. However, if I now run Auto Indent from the Edit > Lines menu, I get this:

    public function name()
    {
        if ($something) {
            return $something;
        }
    }

Beautiful, now it's properly intended for my PHP class. Auto indentation on paste is something PHPStorm does without flaw (see http://g.recordit.co/Rj6unPomlW.gif). It's weird that Atom can manually Auto Indent fine, but Auto Indent On Paste seems useless sometimes.

(For the record, I'm on Yosemite with Atom 1.0.9 in --safe mode.)

@jesseleite
Copy link

...Oh I see what's going on here. Let me follow up on my last comment.

Auto Indent On Paste is trying to auto-indent the block as a whole, based on first line's indentation:
example1

Whereas PHPStorm is also fixing indentation within the block, making sure each line auto-indents correctly:
example2

IMHO, maybe there should be more consistency between manually running Auto Indent, and Auto Indent On Paste, since running Auto Indent manually is smart enough to fix the mess.

Thoughts?

@maxbrunsfeld
Copy link
Contributor

@jesseleite we used to do it the way you're suggesting: auto indent every line of the pasted text, but for many languages, our auto-indent is not smart enough to do it correctly, and people found it surprising that they code that they pasted was being altered like that.

@karai17
Copy link

karai17 commented Sep 1, 2015

Would it be possible to read indenting rules from the current grammar?


Landon Manning
[email protected]

On 1 September 2015 at 13:55, Max Brunsfeld [email protected]
wrote:

@jesseleite https://github.com/JesseLeite we used to do it the way
you're suggesting: auto indent every line of the pasted text, but for many
languages, our auto-indent is not smart enough to do it correctly, and
people found it surprising that they code that they pasted was being
altered like that.


Reply to this email directly or view it on GitHub
#5473 (comment).

@jeancroy
Copy link
Contributor

jeancroy commented Sep 1, 2015

One thing for sure is that first line is problematic. If I had to copy JesseLeite public function(){ i'd probably start the selection at p up to the closing } thus loosing indent information. Empty line as shown in first example is also a case where first line does not contain proper indent information.

I'd however agree that full blown aot indent is probably not what we want on paste, because we have no garantee user will only copy/paste properly matched block scope.

So maybe block indent with overall indent information a bit smarter ?

@jesseleite
Copy link

@maxbrunsfeld I see what you are saying. I would agree, that definitely is an issue.

However, based on terminology, it's a very confusing inconsistency for the end user to understand:
Paste + Auto Indent != Auto Indent On Paste

See what I'm getting at?

@timkelty
Copy link

Related comment: #4272 (comment)

I think the desired behavior matches Sublime's paste_and_indent.
With that setting, the content's leading whitespace is removed when pasting, which prevents the indentation cascade.

@brentshermana
Copy link

I've created a solution to the problem with this pull request. To paraphrase, when text is copied starting with a newline character, a metadata member 'indentBasis' can be incorrect. More detail if you follow the link.

My pull request needs testing on Windows! Can you guys help me out?

@TimvdLippe
Copy link
Contributor

A new PR is up with a fix: #12793. This bug has been bugging me for a long time now and would love to get it fixed.

@patstew
Copy link

patstew commented May 3, 2017

I've had another go at fixing this - see this package: https://atom.io/packages/fix-indent-on-paste
It would be useful to get feedback on whether that package works as people expect - if so I'll update PR #12793 with the same code

@larslue
Copy link

larslue commented May 9, 2017

Thanks @patstew. I had the feeling that this bug still persists even after a full year of development. Your package fixes atom which is now working as expected on my machine. Thanks!

@markiragoldberg
Copy link

Thanks @patstew, your work made Atom much more usable for me.

@clusteratlas
Copy link

This is fucking awesome, thank you very much.

@CodeKiwi
Copy link

CodeKiwi commented Sep 6, 2017

@patstew Awesome, this has been bugging me for a while. I did notice one unexpected thing. When pasting over the existing block it removes the indent when it shouldn't. Not that this is a particularly realistic use case (why would you paste over a block with the same content). Just thought you might like to know. See screenshots (first is copy, second is paste).

screen shot 2017-09-06 at 7 49 42 am

screen shot 2017-09-06 at 7 49 50 am

@lennartq
Copy link

@CodeKiwi I actually duplicate a selection by Ctrl-C, Ctrl-V, Ctrl-V, pasting over the original. Not sure how common that is.

@TimvdLippe
Copy link
Contributor

This might be fixed with #15976.

@stale
Copy link

stale bot commented Oct 29, 2018

Thanks for your contribution!

This issue has been automatically marked as stale because it has not had recent activity. Because the Atom team treats their issues as their backlog, stale issues are closed. If you would like this issue to remain open:

  1. Verify that you can still reproduce the issue in the latest version of Atom
  2. Comment that the issue is still reproducible and include:
    • What version of Atom you reproduced the issue on
    • What OS and version you reproduced the issue on
    • What steps you followed to reproduce the issue

Issues that are labeled as triaged will not be automatically marked as stale.

@stale stale bot added stale and removed stale labels Oct 29, 2018
@stale
Copy link

stale bot commented Oct 29, 2019

Thanks for your contribution!

This issue has been automatically marked as stale because it has not had recent activity. Because the Atom team treats their issues as their backlog, stale issues are closed. If you would like this issue to remain open:

  1. Verify that you can still reproduce the issue in the latest version of Atom
  2. Comment that the issue is still reproducible and include:
    • What version of Atom you reproduced the issue on
    • What OS and version you reproduced the issue on
    • What steps you followed to reproduce the issue

Issues that are labeled as triaged will not be automatically marked as stale.

@lock
Copy link

lock bot commented May 20, 2020

This issue has been automatically locked since there has not been any recent activity after it was closed. If you can still reproduce this issue in Safe Mode then please open a new issue and fill out the entire issue template to ensure that we have enough information to address your issue. Thanks!

2 similar comments
@lock
Copy link

lock bot commented May 20, 2020

This issue has been automatically locked since there has not been any recent activity after it was closed. If you can still reproduce this issue in Safe Mode then please open a new issue and fill out the entire issue template to ensure that we have enough information to address your issue. Thanks!

@lock
Copy link

lock bot commented May 20, 2020

This issue has been automatically locked since there has not been any recent activity after it was closed. If you can still reproduce this issue in Safe Mode then please open a new issue and fill out the entire issue template to ensure that we have enough information to address your issue. Thanks!

@lock lock bot locked as resolved and limited conversation to collaborators May 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests