Skip to content
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

fix rename error if using 'LF' eol in windows #748

Merged
merged 2 commits into from
Feb 17, 2017

Conversation

drzunny
Copy link
Contributor

@drzunny drzunny commented Feb 15, 2017

Hello, DonJayamanne

I found the rename feature goes wrong when i edit a py file with LF eol on Windows(7/8/10).
Bug GIF

but if I convert the file EOL to CRLF, everything is ok.

CRLF is OK

Some editor / IDE will keep both LF and CRLF in one file. So the numbers of character (CR) are not necessarily equal to the line numbers

Here is the effect of this patch:
This patch

@gandhis1
Copy link
Contributor

Very useful - is this related to #579?

@drzunny
Copy link
Contributor Author

drzunny commented Feb 15, 2017

@gandhis1 I'm not sure. If you are using Windows and editing python file with LF eol, try to convert the file to CRLF and use rename(F2) again, if rename correctly, maybe it's related to your issue #579 😄

return offset - position.line;

const winEols = getWindowsLineEndingCount(document.getText(), offset);
return offset - winEols;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@drzunny
You are loading the entire document as a string (document.getText()), contrary to your comments.

// In order to prevent the one-time loading of large files from taking up too much memory

You might want to use the VS Code api to read text from the document.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, i will update my PR later, thanks for your suggestion

return offset - position.line;

const winEols = getWindowsLineEndingCount(document.getText(), offset);
return offset - winEols;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, if this is specific to windows, shouldn't we check the value of IS_WINDOWS and then invoke the functino getWindowsLineEncodingCount?

Copy link
Contributor Author

@drzunny drzunny Feb 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it specific to Windows, but in this method (getOffsetAt), IS_WINDOWS has already checked before invoke the function getWindowsLineEndingCount.

@DonJayamanne
Copy link
Owner

@drzunny please could you point me to the rope code that uses LF instead of CRLF

@drzunny
Copy link
Contributor Author

drzunny commented Feb 16, 2017

@DonJayamanne

i check the source code and find the reason in fscommands.py

def file_data_to_unicode(data, encoding=None):
    result = _decode_data(data, encoding)
    if '\r' in result:    # <-------------------- here
        result = result.replace('\r\n', '\n').replace('\r', '\n')
    return result

The filepath will be read as a File object (rope/base/resources.py), in its read method:

    def read(self):
        data = self.read_bytes()
        try:
            return fscommands.file_data_to_unicode(data)
        except UnicodeDecodeError as e:
            raise exceptions.ModuleDecodeError(self.path, e.reason)

it read binary data, and convert to unicode AND REPLACE THE \r.

@DonJayamanne DonJayamanne merged commit 218bb2d into DonJayamanne:master Feb 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants