-
Notifications
You must be signed in to change notification settings - Fork 186
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
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
9b45612
Add apply_text_edits method to workspace document
masad-frost c7623f6
Sendback diffs instead of the whole document with yapf formatting
masad-frost 613b173
Remove apply_text_edits from document method
masad-frost fe0980d
oops
masad-frost 0565667
Fix the test
masad-frost fa394ad
Fix not passing doc to apply_text_edit
masad-frost 2dbf392
Lint-y lint
masad-frost 26e977e
Merge develop
masad-frost 8847a5d
Make a separate module for text_edit
masad-frost 2f9a509
handle EOF newline and an extra test
masad-frost c1f5d2a
lint
masad-frost 55f6eab
Make pylint happier
masad-frost 89f6211
Match new lines first
masad-frost a8cd387
Hopefully last lint
masad-frost 1849f7d
Update setup.cfg
masad-frost 06f5e34
Undo change to EOL_CHARS
masad-frost 1fb8ff2
Undo changes to get_eol_chars
masad-frost c1daaf8
Add back new line
masad-frost 5196079
Add copyright headers
masad-frost ea6fc90
Fix linting issues
ccordoba12 c404a51
More linting fixes
ccordoba12 53c1caa
Fix pycodestyle issue
ccordoba12 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# Copyright 2017-2020 Palantir Technologies, Inc. | ||
# Copyright 2021- Python Language Server Contributors. | ||
|
||
def get_well_formatted_range(lsp_range): | ||
masad-frost marked this conversation as resolved.
Show resolved
Hide resolved
|
||
start = lsp_range['start'] | ||
end = lsp_range['end'] | ||
|
||
if start['line'] > end['line'] or (start['line'] == end['line'] and start['character'] > end['character']): | ||
return {'start': end, 'end': start} | ||
|
||
return lsp_range | ||
|
||
|
||
def get_well_formatted_edit(text_edit): | ||
lsp_range = get_well_formatted_range(text_edit['range']) | ||
if lsp_range != text_edit['range']: | ||
return {'newText': text_edit['newText'], 'range': lsp_range} | ||
|
||
return text_edit | ||
|
||
|
||
def compare_text_edits(a, b): | ||
diff = a['range']['start']['line'] - b['range']['start']['line'] | ||
if diff == 0: | ||
return a['range']['start']['character'] - b['range']['start']['character'] | ||
|
||
return diff | ||
|
||
|
||
def merge_sort_text_edits(text_edits): | ||
if len(text_edits) <= 1: | ||
return text_edits | ||
|
||
p = len(text_edits) // 2 | ||
left = text_edits[:p] | ||
right = text_edits[p:] | ||
|
||
merge_sort_text_edits(left) | ||
merge_sort_text_edits(right) | ||
|
||
left_idx = 0 | ||
right_idx = 0 | ||
i = 0 | ||
while left_idx < len(left) and right_idx < len(right): | ||
ret = compare_text_edits(left[left_idx], right[right_idx]) | ||
if ret <= 0: | ||
# smaller_equal -> take left to preserve order | ||
text_edits[i] = left[left_idx] | ||
i += 1 | ||
left_idx += 1 | ||
else: | ||
# greater -> take right | ||
text_edits[i] = right[right_idx] | ||
i += 1 | ||
right_idx += 1 | ||
while left_idx < len(left): | ||
text_edits[i] = left[left_idx] | ||
i += 1 | ||
left_idx += 1 | ||
while right_idx < len(right): | ||
text_edits[i] = right[right_idx] | ||
i += 1 | ||
right_idx += 1 | ||
return text_edits | ||
|
||
|
||
class OverLappingTextEditException(Exception): | ||
""" | ||
Text edits are expected to be sorted | ||
and compressed instead of overlapping. | ||
This error is raised when two edits | ||
are overlapping. | ||
""" | ||
|
||
|
||
def apply_text_edits(doc, text_edits): | ||
text = doc.source | ||
sorted_edits = merge_sort_text_edits(list(map(get_well_formatted_edit, text_edits))) | ||
last_modified_offset = 0 | ||
spans = [] | ||
for e in sorted_edits: | ||
start_offset = doc.offset_at_position(e['range']['start']) | ||
if start_offset < last_modified_offset: | ||
raise OverLappingTextEditException('overlapping edit') | ||
|
||
if start_offset > last_modified_offset: | ||
spans.append(text[last_modified_offset:start_offset]) | ||
|
||
if len(e['newText']): | ||
spans.append(e['newText']) | ||
last_modified_offset = doc.offset_at_position(e['range']['end']) | ||
|
||
spans.append(text[last_modified_offset:]) | ||
return ''.join(spans) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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