2017-03-26 21:40:34 +00:00
|
|
|
#include "working_files.h"
|
|
|
|
|
2017-04-16 08:09:12 +00:00
|
|
|
#include "position.h"
|
2017-04-09 19:38:52 +00:00
|
|
|
|
2017-05-15 07:28:53 +00:00
|
|
|
#include <doctest/doctest.h>
|
|
|
|
|
2017-04-17 20:40:50 +00:00
|
|
|
#include <climits>
|
|
|
|
|
2017-04-09 19:38:52 +00:00
|
|
|
namespace {
|
|
|
|
|
2017-05-15 07:28:53 +00:00
|
|
|
lsPosition GetPositionForOffset(const std::string& content, int offset) {
|
|
|
|
if (offset >= content.size())
|
|
|
|
offset = content.size() - 1;
|
|
|
|
|
|
|
|
lsPosition result;
|
|
|
|
int i = 0;
|
|
|
|
while (i < offset) {
|
|
|
|
if (content[i] == '\n') {
|
|
|
|
result.line += 1;
|
|
|
|
result.character = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result.character += 1;
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-04-09 19:38:52 +00:00
|
|
|
int GetOffsetForPosition(lsPosition position, const std::string& content) {
|
|
|
|
int offset = 0;
|
|
|
|
|
|
|
|
int remaining_lines = position.line;
|
|
|
|
while (remaining_lines > 0) {
|
|
|
|
if (content[offset] == '\n')
|
|
|
|
--remaining_lines;
|
|
|
|
++offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
return offset + position.character;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2017-04-16 08:09:12 +00:00
|
|
|
WorkingFile::WorkingFile(const std::string& filename, const std::string& buffer_content)
|
|
|
|
: filename(filename), buffer_content(buffer_content) {
|
|
|
|
OnBufferContentUpdated();
|
2017-04-21 06:32:18 +00:00
|
|
|
|
|
|
|
// SetIndexContent gets called when the file is opened.
|
2017-03-26 21:40:34 +00:00
|
|
|
}
|
|
|
|
|
2017-04-16 08:09:12 +00:00
|
|
|
void WorkingFile::SetIndexContent(const std::string& index_content) {
|
|
|
|
index_lines = ToLines(index_content, true /*trim_whitespace*/);
|
2017-04-19 07:52:48 +00:00
|
|
|
|
|
|
|
// Build lookup buffer.
|
|
|
|
index_lines_lookup.clear();
|
|
|
|
index_lines_lookup.reserve(index_lines.size());
|
|
|
|
for (int i = 0; i < index_lines.size(); ++i) {
|
|
|
|
const std::string& index_line = index_lines[i];
|
|
|
|
|
|
|
|
auto it = index_lines_lookup.find(index_line);
|
|
|
|
if (it == index_lines_lookup.end())
|
|
|
|
index_lines_lookup[index_line] = { i + 1 };
|
|
|
|
else
|
|
|
|
it->second.push_back(i + 1);
|
|
|
|
}
|
2017-03-26 21:40:34 +00:00
|
|
|
}
|
|
|
|
|
2017-04-16 08:09:12 +00:00
|
|
|
void WorkingFile::OnBufferContentUpdated() {
|
|
|
|
all_buffer_lines = ToLines(buffer_content, true /*trim_whitespace*/);
|
2017-04-09 19:38:52 +00:00
|
|
|
|
2017-04-16 08:09:12 +00:00
|
|
|
// Build lookup buffer.
|
2017-04-19 07:52:48 +00:00
|
|
|
all_buffer_lines_lookup.clear();
|
|
|
|
all_buffer_lines_lookup.reserve(all_buffer_lines.size());
|
2017-04-16 08:09:12 +00:00
|
|
|
for (int i = 0; i < all_buffer_lines.size(); ++i) {
|
|
|
|
const std::string& buffer_line = all_buffer_lines[i];
|
2017-04-09 19:38:52 +00:00
|
|
|
|
2017-04-19 07:52:48 +00:00
|
|
|
auto it = all_buffer_lines_lookup.find(buffer_line);
|
|
|
|
if (it == all_buffer_lines_lookup.end())
|
|
|
|
all_buffer_lines_lookup[buffer_line] = { i + 1 };
|
2017-04-16 08:09:12 +00:00
|
|
|
else
|
|
|
|
it->second.push_back(i + 1);
|
2017-04-09 19:38:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 07:52:48 +00:00
|
|
|
optional<int> WorkingFile::GetBufferLineFromIndexLine(int index_line) const {
|
2017-04-16 08:09:12 +00:00
|
|
|
// The implementation is simple but works pretty well for most cases. We
|
|
|
|
// lookup the line contents in the indexed file contents, and try to find the
|
|
|
|
// most similar line in the current buffer file.
|
|
|
|
//
|
|
|
|
// Previously, this was implemented by tracking edits and by running myers
|
|
|
|
// diff algorithm. They were complex implementations that did not work as
|
|
|
|
// well.
|
|
|
|
|
|
|
|
// Note: |index_line| and |buffer_line| are 1-based.
|
|
|
|
|
2017-04-18 23:49:55 +00:00
|
|
|
// TODO: reenable this assert once we are using the real indexed file.
|
2017-05-07 05:34:43 +00:00
|
|
|
//assert(index_line >= 1 && index_line <= index_lines.size());
|
|
|
|
if (index_line < 1 || index_line > index_lines.size()) {
|
|
|
|
std::cerr << "!! Bad index_line (got " << index_line << ", expected [1, " << index_lines.size() << "])" << std::endl;
|
|
|
|
return nullopt;
|
|
|
|
}
|
2017-04-16 08:09:12 +00:00
|
|
|
|
|
|
|
// Find the line in the cached index file. We'll try to find the most similar line
|
|
|
|
// in the buffer and return the index for that.
|
|
|
|
std::string index = index_lines[index_line - 1];
|
2017-04-19 07:52:48 +00:00
|
|
|
auto buffer_it = all_buffer_lines_lookup.find(index);
|
|
|
|
if (buffer_it == all_buffer_lines_lookup.end()) {
|
2017-04-16 08:09:12 +00:00
|
|
|
// TODO: Use levenshtein distance to find the best match (but only to an
|
|
|
|
// extent)
|
|
|
|
return nullopt;
|
2017-04-09 19:38:52 +00:00
|
|
|
}
|
|
|
|
|
2017-04-16 08:09:12 +00:00
|
|
|
// From all the identical lines, return the one which is closest to
|
|
|
|
// |index_line|. There will usually only be one identical line.
|
|
|
|
assert(!buffer_it->second.empty());
|
|
|
|
int closest_dist = INT_MAX;
|
|
|
|
int closest_buffer_line = INT_MIN;
|
|
|
|
for (int buffer_line : buffer_it->second) {
|
|
|
|
int dist = std::abs(buffer_line - index_line);
|
|
|
|
if (dist <= closest_dist) {
|
|
|
|
closest_dist = dist;
|
|
|
|
closest_buffer_line = buffer_line;
|
2017-04-09 19:38:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-16 08:09:12 +00:00
|
|
|
return closest_buffer_line;
|
2017-04-09 19:38:52 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 07:52:48 +00:00
|
|
|
optional<int> WorkingFile::GetIndexLineFromBufferLine(int buffer_line) const {
|
|
|
|
// See GetBufferLineFromIndexLine for additional comments.
|
|
|
|
|
|
|
|
// Note: |index_line| and |buffer_line| are 1-based.
|
2017-05-07 05:34:43 +00:00
|
|
|
//assert(buffer_line >= 1 && buffer_line < all_buffer_lines.size());
|
|
|
|
if (buffer_line < 1 || buffer_line > all_buffer_lines.size()) {
|
|
|
|
std::cerr << "!! Bad buffer_line (got " << buffer_line << ", expected [1, " << all_buffer_lines.size() << "])" << std::endl;
|
|
|
|
return nullopt;
|
|
|
|
}
|
2017-04-19 07:52:48 +00:00
|
|
|
|
|
|
|
// Find the line in the index file. We'll try to find the most similar line
|
|
|
|
// in the index file and return the index for that.
|
|
|
|
std::string buffer = all_buffer_lines[buffer_line - 1];
|
|
|
|
auto index_it = index_lines_lookup.find(buffer);
|
|
|
|
if (index_it == index_lines_lookup.end()) {
|
|
|
|
// TODO: Use levenshtein distance to find the best match (but only to an
|
|
|
|
// extent)
|
|
|
|
return nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
// From all the identical lines, return the one which is closest to
|
|
|
|
// |index_line|. There will usually only be one identical line.
|
|
|
|
assert(!index_it->second.empty());
|
|
|
|
int closest_dist = INT_MAX;
|
|
|
|
int closest_index_line = INT_MIN;
|
|
|
|
for (int index_line : index_it->second) {
|
|
|
|
int dist = std::abs(buffer_line - index_line);
|
|
|
|
if (dist <= closest_dist) {
|
|
|
|
closest_dist = dist;
|
|
|
|
closest_index_line = index_line;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return closest_index_line;
|
|
|
|
}
|
|
|
|
|
2017-05-15 07:28:53 +00:00
|
|
|
std::string WorkingFile::FindClosestCallNameInBuffer(lsPosition position, int* active_parameter, lsPosition* completion_position) const {
|
|
|
|
*active_parameter = 0;
|
|
|
|
|
|
|
|
int offset = GetOffsetForPosition(position, buffer_content);
|
|
|
|
|
|
|
|
// If vscode auto-inserts closing ')' we will begin on ')' token in foo()
|
|
|
|
// which will make the below algorithm think it's a nested call.
|
|
|
|
if (offset > 0 && buffer_content[offset] == ')')
|
|
|
|
--offset;
|
|
|
|
|
|
|
|
// Scan back out of call context.
|
|
|
|
int balance = 0;
|
|
|
|
while (offset > 0) {
|
|
|
|
char c = buffer_content[offset];
|
|
|
|
if (c == ')') ++balance;
|
|
|
|
else if (c == '(') --balance;
|
|
|
|
|
|
|
|
if (balance == 0 && c == ',')
|
|
|
|
*active_parameter += 1;
|
|
|
|
|
|
|
|
--offset;
|
|
|
|
|
|
|
|
if (balance == -1)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (offset < 0)
|
|
|
|
return "";
|
|
|
|
|
|
|
|
// Scan back entire identifier.
|
|
|
|
int start_offset = offset;
|
|
|
|
while (offset > 0) {
|
|
|
|
char c = buffer_content[offset - 1];
|
|
|
|
if (isalnum(c) == false && c != '_')
|
|
|
|
break;
|
|
|
|
--offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (completion_position)
|
|
|
|
*completion_position = GetPositionForOffset(buffer_content, offset);
|
|
|
|
|
|
|
|
return buffer_content.substr(offset, start_offset - offset + 1);
|
|
|
|
}
|
|
|
|
|
2017-04-16 08:09:12 +00:00
|
|
|
CXUnsavedFile WorkingFile::AsUnsavedFile() const {
|
|
|
|
CXUnsavedFile result;
|
|
|
|
result.Filename = filename.c_str();
|
|
|
|
result.Contents = buffer_content.c_str();
|
|
|
|
result.Length = (unsigned long)buffer_content.size();
|
|
|
|
return result;
|
2017-04-09 19:38:52 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 21:40:34 +00:00
|
|
|
WorkingFile* WorkingFiles::GetFileByFilename(const std::string& filename) {
|
2017-05-10 04:52:15 +00:00
|
|
|
std::lock_guard<std::mutex> lock(files_mutex);
|
|
|
|
return GetFileByFilenameNoLock(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkingFile* WorkingFiles::GetFileByFilenameNoLock(const std::string& filename) {
|
2017-03-26 21:40:34 +00:00
|
|
|
for (auto& file : files) {
|
|
|
|
if (file->filename == filename)
|
|
|
|
return file.get();
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-04-21 06:32:18 +00:00
|
|
|
WorkingFile* WorkingFiles::OnOpen(const Ipc_TextDocumentDidOpen::Params& open) {
|
2017-05-10 04:52:15 +00:00
|
|
|
std::lock_guard<std::mutex> lock(files_mutex);
|
|
|
|
|
2017-03-26 21:40:34 +00:00
|
|
|
std::string filename = open.textDocument.uri.GetPath();
|
|
|
|
std::string content = open.textDocument.text;
|
|
|
|
|
|
|
|
// The file may already be open.
|
2017-05-10 04:52:15 +00:00
|
|
|
if (WorkingFile* file = GetFileByFilenameNoLock(filename)) {
|
2017-04-14 08:21:03 +00:00
|
|
|
file->version = open.textDocument.version;
|
2017-04-16 08:09:12 +00:00
|
|
|
file->buffer_content = content;
|
|
|
|
file->OnBufferContentUpdated();
|
2017-04-21 06:32:18 +00:00
|
|
|
return file;
|
2017-03-26 21:40:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
files.push_back(MakeUnique<WorkingFile>(filename, content));
|
2017-04-21 06:32:18 +00:00
|
|
|
return files[files.size() - 1].get();
|
2017-03-26 21:40:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WorkingFiles::OnChange(const Ipc_TextDocumentDidChange::Params& change) {
|
2017-05-10 04:52:15 +00:00
|
|
|
std::lock_guard<std::mutex> lock(files_mutex);
|
|
|
|
|
2017-03-26 21:40:34 +00:00
|
|
|
std::string filename = change.textDocument.uri.GetPath();
|
2017-05-10 04:52:15 +00:00
|
|
|
WorkingFile* file = GetFileByFilenameNoLock(filename);
|
2017-03-26 21:40:34 +00:00
|
|
|
if (!file) {
|
|
|
|
std::cerr << "Could not change " << filename << " because it was not open" << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-14 08:21:03 +00:00
|
|
|
file->version = change.textDocument.version;
|
2017-04-09 22:16:06 +00:00
|
|
|
//std::cerr << "VERSION " << change.textDocument.version << std::endl;
|
2017-03-26 21:40:34 +00:00
|
|
|
|
|
|
|
for (const Ipc_TextDocumentDidChange::lsTextDocumentContentChangeEvent& diff : change.contentChanges) {
|
2017-04-09 19:38:52 +00:00
|
|
|
//std::cerr << "Applying rangeLength=" << diff.rangeLength;
|
|
|
|
|
2017-03-26 21:40:34 +00:00
|
|
|
// If range or rangeLength are emitted we replace everything, per the spec.
|
|
|
|
if (diff.rangeLength == -1) {
|
2017-04-16 08:09:12 +00:00
|
|
|
file->buffer_content = diff.text;
|
|
|
|
file->OnBufferContentUpdated();
|
2017-03-26 21:40:34 +00:00
|
|
|
}
|
|
|
|
else {
|
2017-04-16 08:09:12 +00:00
|
|
|
int start_offset = GetOffsetForPosition(diff.range.start, file->buffer_content);
|
|
|
|
file->buffer_content.replace(file->buffer_content.begin() + start_offset,
|
|
|
|
file->buffer_content.begin() + start_offset + diff.rangeLength,
|
2017-04-09 19:38:52 +00:00
|
|
|
diff.text);
|
2017-04-16 08:09:12 +00:00
|
|
|
file->OnBufferContentUpdated();
|
2017-03-26 21:40:34 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-09 19:38:52 +00:00
|
|
|
//std::cerr << std::endl << std::endl << "--------" << file->content << "--------" << std::endl << std::endl;
|
2017-03-26 21:40:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WorkingFiles::OnClose(const Ipc_TextDocumentDidClose::Params& close) {
|
2017-05-10 04:52:15 +00:00
|
|
|
std::lock_guard<std::mutex> lock(files_mutex);
|
|
|
|
|
2017-03-26 21:40:34 +00:00
|
|
|
std::string filename = close.textDocument.uri.GetPath();
|
|
|
|
|
|
|
|
for (int i = 0; i < files.size(); ++i) {
|
|
|
|
if (files[i]->filename == filename) {
|
|
|
|
files.erase(files.begin() + i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cerr << "Could not close " << filename << " because it was not open" << std::endl;
|
|
|
|
}
|
|
|
|
|
2017-05-10 04:52:15 +00:00
|
|
|
std::vector<CXUnsavedFile> WorkingFiles::AsUnsavedFiles() {
|
|
|
|
std::lock_guard<std::mutex> lock(files_mutex);
|
|
|
|
|
2017-03-26 21:40:34 +00:00
|
|
|
std::vector<CXUnsavedFile> result;
|
|
|
|
result.reserve(files.size());
|
|
|
|
for (auto& file : files)
|
|
|
|
result.push_back(file->AsUnsavedFile());
|
|
|
|
return result;
|
2017-04-17 20:40:50 +00:00
|
|
|
}
|
2017-05-15 07:28:53 +00:00
|
|
|
|
|
|
|
TEST_SUITE("WorkingFile");
|
|
|
|
|
|
|
|
lsPosition CharPos(const WorkingFile& file, char character, int character_offset = 0) {
|
|
|
|
const std::string& search = file.buffer_content;
|
|
|
|
|
|
|
|
lsPosition result;
|
|
|
|
int index = 0;
|
|
|
|
while (index < search.size()) {
|
|
|
|
char c = search[index];
|
|
|
|
if (c == character)
|
|
|
|
break;
|
|
|
|
if (c == '\n') {
|
|
|
|
result.line += 1;
|
|
|
|
result.character = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result.character += 1;
|
|
|
|
}
|
|
|
|
++index;
|
|
|
|
}
|
|
|
|
REQUIRE(index < search.size());
|
|
|
|
result.character += character_offset;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("simple call") {
|
|
|
|
WorkingFile f("foo.cc", "abcd(1, 2");
|
|
|
|
int active_param = 0;
|
|
|
|
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, '('), &active_param) == "abcd");
|
|
|
|
REQUIRE(active_param == 0);
|
|
|
|
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, '1'), &active_param) == "abcd");
|
|
|
|
REQUIRE(active_param == 0);
|
|
|
|
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, ','), &active_param) == "abcd");
|
|
|
|
REQUIRE(active_param == 1);
|
|
|
|
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, ' '), &active_param) == "abcd");
|
|
|
|
REQUIRE(active_param == 1);
|
|
|
|
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, '2'), &active_param) == "abcd");
|
|
|
|
REQUIRE(active_param == 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("nested call") {
|
|
|
|
WorkingFile f("foo.cc", "abcd(efg(), 2");
|
|
|
|
int active_param = 0;
|
|
|
|
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, '('), &active_param) == "abcd");
|
|
|
|
REQUIRE(active_param == 0);
|
|
|
|
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, 'e'), &active_param) == "abcd");
|
|
|
|
REQUIRE(active_param == 0);
|
|
|
|
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, 'f'), &active_param) == "abcd");
|
|
|
|
REQUIRE(active_param == 0);
|
|
|
|
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, 'g'), &active_param) == "abcd");
|
|
|
|
REQUIRE(active_param == 0);
|
|
|
|
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, 'g', 1), &active_param) == "efg");
|
|
|
|
REQUIRE(active_param == 0);
|
|
|
|
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, 'g', 2), &active_param) == "efg");
|
|
|
|
REQUIRE(active_param == 0);
|
|
|
|
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, ','), &active_param) == "abcd");
|
|
|
|
REQUIRE(active_param == 1);
|
|
|
|
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, ' '), &active_param) == "abcd");
|
|
|
|
REQUIRE(active_param == 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("auto-insert )") {
|
|
|
|
WorkingFile f("foo.cc", "abc()");
|
|
|
|
int active_param = 0;
|
|
|
|
REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, ')'), &active_param) == "abc");
|
|
|
|
REQUIRE(active_param == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_SUITE_END();
|