Fix completion result sorting in VSCode (#210)

Fix #207
This commit is contained in:
Riatre Foo 2019-01-30 23:21:40 +08:00 committed by Fangrui Song
parent 9bc762961a
commit d4de474be1

View File

@ -134,6 +134,23 @@ void FilterCandidates(CompletionList &result, const std::string &complete_text,
result.isIncomplete = true; result.isIncomplete = true;
} }
int overwrite_len = 0;
for (auto &item : items) {
auto &edits = item.additionalTextEdits;
if (edits.size() && edits[0].range.end == begin_pos) {
Position start = edits[0].range.start, end = edits[0].range.end;
if (start.line == begin_pos.line) {
overwrite_len =
std::max(overwrite_len, end.character - start.character);
} else {
overwrite_len = -1;
break;
}
}
}
Position overwrite_begin = {begin_pos.line,
begin_pos.character - overwrite_len};
std::string sort(4, ' '); std::string sort(4, ' ');
for (auto &item : items) { for (auto &item : items) {
item.textEdit.range = lsRange{begin_pos, end_pos}; item.textEdit.range = lsRange{begin_pos, end_pos};
@ -142,17 +159,20 @@ void FilterCandidates(CompletionList &result, const std::string &complete_text,
// https://github.com/Microsoft/language-server-protocol/issues/543 // https://github.com/Microsoft/language-server-protocol/issues/543
// Order of textEdit and additionalTextEdits is unspecified. // Order of textEdit and additionalTextEdits is unspecified.
auto &edits = item.additionalTextEdits; auto &edits = item.additionalTextEdits;
if (edits.size() && edits[0].range.end == begin_pos) { if (overwrite_len > 0) {
Position start = edits[0].range.start, end = edits[0].range.end; item.textEdit.range.start = overwrite_begin;
item.textEdit.range.start = start; std::string orig = buffer_line.substr(overwrite_begin.character, overwrite_len);
item.textEdit.newText = edits[0].newText + item.textEdit.newText; if (edits.size() && edits[0].range.end == begin_pos &&
if (start.line == begin_pos.line) { edits[0].range.start.line == begin_pos.line) {
item.filterText = int cur_edit_len = edits[0].range.end.character - edits[0].range.start.character;
buffer_line.substr(start.character, item.textEdit.newText =
end.character - start.character) + buffer_line.substr(overwrite_begin.character, overwrite_len - cur_edit_len) +
item.filterText; edits[0].newText + item.textEdit.newText;
}
edits.erase(edits.begin()); edits.erase(edits.begin());
} else {
item.textEdit.newText = orig + item.textEdit.newText;
}
item.filterText = orig + item.filterText;
} }
if (item.filterText == item.label) if (item.filterText == item.label)
item.filterText.clear(); item.filterText.clear();