Skip to content

rename now reports correct number of lines.#345

Merged
gatesn merged 4 commits into
palantir:developfrom
abingham:count-lines-in-rename
May 17, 2018
Merged

rename now reports correct number of lines.#345
gatesn merged 4 commits into
palantir:developfrom
abingham:count-lines-in-rename

Conversation

@abingham

@abingham abingham commented May 7, 2018

Copy link
Copy Markdown
Contributor

It was initially reporting sys.maxsize all of the time, and this was both
incorrect and problematic for some clients.

See #218 for more information.

It was initially reporting `sys.maxsize` all of the time, and this was both
incorrect and problematic for some clients.
@palantirtech

Copy link
Copy Markdown
Member

Thanks for your interest in palantir/python-language-server, @abingham! Before we can accept your pull request, you need to sign our contributor license agreement - just visit https://cla.palantir.com/ and follow the instructions. Once you sign, I'll automatically update this pull request.

@abingham abingham changed the title rename now report correct number of lines. rename now reports correct number of lines. May 7, 2018

@gatesn gatesn left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Some style comments

Comment thread pyls/plugins/rope_rename.py Outdated
@@ -1,4 +1,6 @@
# Copyright 2017 Palantir Technologies, Inc.
from functools import reduce

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you just import functools? I (and probably the linter!) don’t like overriding the built-in function.

Comment thread pyls/plugins/rope_rename.py Outdated
log = logging.getLogger(__name__)


def _num_lines(resource):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you move “private” functions to bottom of module

Comment thread pyls/plugins/rope_rename.py Outdated

def _num_lines(resource):
"""Count the number of lines in a `File` resource.
"""

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Closing quotes on the same line if they fit

Comment thread pyls/plugins/rope_rename.py Outdated
def _num_lines(resource):
"""Count the number of lines in a `File` resource.
"""
s = io.StringIO(resource.read())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What does resource.read() return? Seems unnecessary to use reduce given you’re loading the resource into memory anyway. Can’t you just call splitlines and count it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, the reduce call was a bit too cutesy.

@abingham

abingham commented May 9, 2018

Copy link
Copy Markdown
Contributor Author

I think I've made all the changes you asked for. I also added version to the textDocument response; I did this because the emacs client I'm using requires it, and this is how I was able to test the change. It also seems required by LSP, so I figured it was OK.

Comment thread pyls/plugins/rope_rename.py Outdated
'uri': uris.uri_with(
document.uri, path=os.path.join(workspace.root_path, change.resource.path)
),
'version': document.version

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is incorrect, the uri we're returning is derived from the rope resource (line above), however this version number is derived from the document within which the rename was triggered.

If you check the LSP, version is not a required field: https://microsoft.github.io/language-server-protocol/specification#versionedtextdocumentidentifier

However, we can do better than just never setting it. If you use workspace.get_document(doc_uri) and then use that document's version then it will return None (correctly) if the server has never received a didOpen message, or the correct version number if it has.

My concern with doing this is that rope doesn't operate on the in-memory view of the documents. We should probably fix that before making too heavy usage of rope. In particular, the edits returned by rope could be inconsistent with the version of the document.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

So is it incorrect that the client is requiring the version? I think they're only accidentally requiring it, but I don't know for sure.

Regarding rope: yes, it would be nice if we could get it to use the in-memory buffer. In my earlier work with rope I just had the client save the file, but that was the lazy way out.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yup, I think it's incorrect of the client to require it. Though it might be correct if they only require it for documents that the client has sent a didOpen message for

@abingham

Copy link
Copy Markdown
Contributor Author

The lsp-mode emacs client code now behaves correctly in the face of a missing version, and I think this PR has all of the changes you asked for.

@gatesn gatesn merged commit 8a34aa2 into palantir:develop May 17, 2018
@abingham abingham deleted the count-lines-in-rename branch May 17, 2018 15:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants