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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse YAPF diffs into TextEdits (instead of sending the full doc) #136

Merged
merged 22 commits into from
Jun 6, 2022

Conversation

masad-frost
Copy link
Contributor

Currently formatting with YAPF sends back the full document as a range, it's not great because:

  • Editors that allow edits while formatting is pending cannot reasonably consolidate the format diff with the user's local changes
  • Editors that have to talk to servers now have to send the whole document back and forth (admittedly, for smaller documents the added JSON structures offset that)
  • The person who built the plugin wanted to do it, but couldn't be bothered 馃槅
  • In general, it is the recommended approach by the spec

To support this feature I used YAPF's unified diff output option. I took the output and passed it to whatthepatch to produce a parsed result, the format is outlined in the library's readme. We iterate over lines in the diff and create TextEdits. Left some comments around the code to make it more readable.

To be able to test without strictly matching TextEdits I also added a function to the workspace module called apply_text_edits which takes a Document instance and TextEdits and returns a new source/string with the text edits applied. I wrote tests for this function based on the tests in the vscode language server repo. This function is now only used for testing, but I imagine it being useful for anyone working with text edits

@krassowski
Copy link
Contributor

Thanks for starting this work!

The tests are failing on CI - it looks like they need updating to match the changes in 341732d:

>       assert doc.apply_text_edits(res) == "A = ['h', 'w', 'a']\n\nB = ['h', 'w']\n"
E       AttributeError: 'Document' object has no attribute 'apply_text_edits'

@masad-frost masad-frost force-pushed the fm-yapf-diff branch 2 times, most recently from 6303e3d to b1e3447 Compare March 10, 2022 02:52
@masad-frost
Copy link
Contributor Author

@krassowski the tests pass locally now if you wanna run the workflow.

@masad-frost
Copy link
Contributor Author

Actually fixed now. Ran pytest and pylint

@masad-frost
Copy link
Contributor Author

Weird, I'm not getting these lint issues locally. Will look into it

@masad-frost
Copy link
Contributor Author

Will merge main after #134 is merged

pylsp/workspace.py Outdated Show resolved Hide resolved
@ccordoba12
Copy link
Member

@masad-frost, now that #134 is merged, you can fix the merge conflicts here so we can merge it.

Copy link
Member

@ccordoba12 ccordoba12 left a comment

Choose a reason for hiding this comment

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

Hey @masad-frost, thanks for patience with this! Last comment, then this should be ready.

Also, please fix the merge conflicts that this PR has now.

pylsp/workspace.py Outdated Show resolved Hide resolved
pylsp/_utils.py Outdated Show resolved Hide resolved
return text_edits


def ensure_eof_new_line(document, eol_chars, text_edits):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a bit of a hack, see: google/yapf#1008

@masad-frost
Copy link
Contributor Author

@ccordoba12 lint issues seem to be unrelated from this branch.

image

Copy link
Member

@ccordoba12 ccordoba12 left a comment

Choose a reason for hiding this comment

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

Hey @masad-frost, thanks for the updates! I think you're really close to finish this one.

pylsp/_utils.py Outdated Show resolved Hide resolved
pylsp/_utils.py Outdated Show resolved Hide resolved
pylsp/text_edit.py Show resolved Hide resolved
test/test_text_edit.py Show resolved Hide resolved
test/test_workspace.py Outdated Show resolved Hide resolved
@ccordoba12
Copy link
Member

@ccordoba12 lint issues seem to be unrelated from this branch.

I'll help with that after you solve my comments.

@masad-frost
Copy link
Contributor Author

thanks, quick fixes esp with the suggestions!

@masad-frost
Copy link
Contributor Author

masad-frost commented Jun 6, 2022

Just the lints are left! Let me know if there's anything else I can address. If you have more test cases in mind or something

Copy link
Member

@ccordoba12 ccordoba12 left a comment

Choose a reason for hiding this comment

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

Thanks for your hard on this @masad-frost! I think it's finally ready.

@ccordoba12 ccordoba12 merged commit a9d503c into python-lsp:develop Jun 6, 2022
@masad-frost masad-frost deleted the fm-yapf-diff branch June 6, 2022 18:50
lilyinstarlight added a commit to lilyinstarlight/nixpkgs that referenced this pull request Jul 30, 2022
Changes:
* The `test_numpy_completion` test is currently broken - see davidhalter/jedi#1864
* The test flags were moved from setup.cfg to pyproject.toml - see python-lsp/python-lsp-server#207
* A dependency on `whatthepatch` was added for yapf - see python-lsp/python-lsp-server#136
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.

None yet

3 participants