Change line/column to 0-based and only use 1-based when (de,)serialized to JSON

This commit is contained in:
Fangrui Song 2018-01-14 11:39:29 -08:00
parent f78d87ded3
commit 54ac72115e
10 changed files with 45 additions and 46 deletions

View File

@ -17,8 +17,8 @@ Range ResolveCXSourceRange(const CXSourceRange& range, CXFile* cx_file) {
unsigned int end_line, end_column;
clang_getSpellingLocation(end, nullptr, &end_line, &end_column, nullptr);
return Range(Position((int16_t)start_line, (int16_t)start_column) /*start*/,
Position((int16_t)end_line, (int16_t)end_column) /*end*/);
return Range(Position((int16_t)start_line - 1, (int16_t)start_column - 1),
Position((int16_t)end_line - 1, (int16_t)end_column - 1));
}
// TODO Place this global variable into config

View File

@ -12,9 +12,9 @@ FileContents::FileContents(const std::string& path, const std::string& content)
}
optional<int> FileContents::ToOffset(Position p) const {
if (0 < p.line && size_t(p.line) <= line_offsets_.size()) {
int ret = line_offsets_[p.line - 1] + p.column - 1;
if (size_t(ret) <= content.size())
if (0 <= p.line && size_t(p.line) < line_offsets_.size()) {
int ret = line_offsets_[p.line] + p.column;
if (size_t(ret) < content.size())
return ret;
}
return nullopt;
@ -26,4 +26,4 @@ optional<std::string> FileContents::ContentsInRange(Range range) const {
if (start_offset && end_offset && *start_offset < *end_offset)
return content.substr(*start_offset, *end_offset - *start_offset);
return nullopt;
}
}

View File

@ -141,12 +141,12 @@ void EmitSemanticHighlighting(QueryDatabase* db,
// shrinking hack, the contained keywords and primitive types will be
// highlighted undesiredly.
auto concise_name = detailed_name.substr(0, detailed_name.find('<'));
if (0 < sym.loc.range.start.line &&
sym.loc.range.start.line <= working_file->index_lines.size()) {
if (0 <= sym.loc.range.start.line &&
sym.loc.range.start.line < working_file->index_lines.size()) {
const std::string& line =
working_file->index_lines[sym.loc.range.start.line - 1];
working_file->index_lines[sym.loc.range.start.line];
sym.loc.range.end.line = sym.loc.range.start.line;
int col = sym.loc.range.start.column - 1;
int col = sym.loc.range.start.column;
if (line.compare(col, concise_name.size(), concise_name) == 0)
sym.loc.range.end.column =
sym.loc.range.start.column + concise_name.size();

View File

@ -64,8 +64,8 @@ struct TextDocumentDefinitionHandler
Out_TextDocumentDefinition out;
out.id = request->id;
int target_line = request->params.position.line + 1;
int target_column = request->params.position.character + 1;
int target_line = request->params.position.line;
int target_column = request->params.position.character;
for (const SymbolRef& ref :
FindSymbolsAtLocation(working_file, file, request->params.position)) {

View File

@ -69,7 +69,7 @@ struct TextDocumentDocumentLinkHandler
// Subtract 1 from line because querydb stores 1-based lines but
// vscode expects 0-based lines.
optional<lsRange> between_quotes = ExtractQuotedRange(
*buffer_line - 1, working_file->buffer_lines[*buffer_line - 1]);
*buffer_line, working_file->buffer_lines[*buffer_line]);
if (!between_quotes)
continue;

View File

@ -11,17 +11,17 @@ const char* SkipAfter(const char* input, char skip_after) {
}
} // namespace
Position::Position() {}
Position::Position() : line(0), column(0) {}
Position::Position(int16_t line, int16_t column) : line(line), column(column) {}
Position::Position(const char* encoded) {
assert(encoded);
line = (int16_t)atoi(encoded);
line = (int16_t)atoi(encoded) - 1;
encoded = SkipAfter(encoded, ':');
assert(encoded);
column = (int16_t)atoi(encoded);
column = (int16_t)atoi(encoded) - 1;
}
std::string Position::ToString() {
@ -33,9 +33,9 @@ std::string Position::ToString() {
// 2 => column
std::string result;
result += std::to_string(line);
result += std::to_string(line + 1);
result += ':';
result += std::to_string(column);
result += std::to_string(column + 1);
return result;
}
@ -51,9 +51,9 @@ std::string Position::ToPrettyString(const std::string& filename) {
std::string result;
result += filename;
result += ':';
result += std::to_string(line);
result += std::to_string(line + 1);
result += ':';
result += std::to_string(column);
result += std::to_string(column + 1);
return result;
}
@ -79,17 +79,17 @@ Range::Range(Position start, Position end) : start(start), end(end) {}
Range::Range(const char* encoded) {
char* p = const_cast<char*>(encoded);
start.line = int16_t(strtol(p, &p, 10));
start.line = int16_t(strtol(p, &p, 10)) - 1;
assert(*p == ':');
p++;
start.column = int16_t(strtol(p, &p, 10));
start.column = int16_t(strtol(p, &p, 10)) - 1;
assert(*p == '-');
p++;
end.line = int16_t(strtol(p, &p, 10));
end.line = int16_t(strtol(p, &p, 10)) - 1;
assert(*p == ':');
p++;
end.column = int16_t(strtol(p, nullptr, 10));
end.column = int16_t(strtol(p, nullptr, 10)) - 1;
}
bool Range::Contains(int line, int column) const {
@ -117,13 +117,13 @@ std::string Range::ToString() {
std::string output;
output += std::to_string(start.line);
output += std::to_string(start.line + 1);
output += ':';
output += std::to_string(start.column);
output += std::to_string(start.column + 1);
output += '-';
output += std::to_string(end.line);
output += std::to_string(end.line + 1);
output += ':';
output += std::to_string(end.column);
output += std::to_string(end.column + 1);
return output;
}

View File

@ -8,8 +8,8 @@
#include "utils.h"
struct Position {
int16_t line = -1;
int16_t column = -1;
int16_t line;
int16_t column;
Position();
Position(int16_t line, int16_t column);

View File

@ -247,7 +247,7 @@ QueryFile::Def BuildFileDef(const IdMap& id_map, const IndexFile& indexed) {
// 42;` will take you to the constructor.
Range range = caller.loc;
if (caller.is_implicit) {
if (range.start.column > 1)
if (range.start.column > 0)
range.start.column--;
range.end.column++;
}

View File

@ -316,20 +316,20 @@ std::vector<QueryFuncRef> GetCallersForAllDerivedFunctions(QueryDatabase* db,
optional<lsPosition> GetLsPosition(WorkingFile* working_file,
const Position& position) {
if (!working_file)
return lsPosition(position.line - 1, position.column - 1);
return lsPosition(position.line, position.column);
optional<int> start = working_file->GetBufferLineFromIndexLine(position.line);
if (!start)
return nullopt;
return lsPosition(*start - 1, position.column - 1);
return lsPosition(*start, position.column);
}
optional<lsRange> GetLsRange(WorkingFile* working_file, const Range& location) {
if (!working_file) {
return lsRange(
lsPosition(location.start.line - 1, location.start.column - 1),
lsPosition(location.end.line - 1, location.end.column - 1));
lsPosition(location.start.line, location.start.column),
lsPosition(location.end.line, location.end.column));
}
optional<int> start =
@ -348,8 +348,8 @@ optional<lsRange> GetLsRange(WorkingFile* working_file, const Range& location) {
if (*end < *start)
*end = *start + (location.end.line - location.start.line);
return lsRange(lsPosition(*start - 1, location.start.column - 1),
lsPosition(*end - 1, location.end.column - 1));
return lsRange(lsPosition(*start, location.start.column),
lsPosition(*end, location.end.column));
}
lsDocumentUri GetLsDocumentUri(QueryDatabase* db,
@ -478,8 +478,8 @@ std::vector<SymbolRef> FindSymbolsAtLocation(WorkingFile* working_file,
std::vector<SymbolRef> symbols;
symbols.reserve(1);
int target_line = position.line + 1;
int target_column = position.character + 1;
int target_line = position.line;
int target_column = position.character;
if (working_file) {
optional<int> index_line =
working_file->GetIndexLineFromBufferLine(target_line);

View File

@ -69,9 +69,8 @@ optional<int> FindMatchingLine(const std::vector<std::string>& index_lines,
const std::vector<int>& index_to_buffer,
int line,
const std::vector<std::string>& buffer_lines) {
line--;
if (index_to_buffer[line] >= 0)
return index_to_buffer[line] + 1;
return index_to_buffer[line];
int up = line, down = line;
while (--up >= 0 && index_to_buffer[up] < 0) {}
while (++down < int(index_to_buffer.size()) && index_to_buffer[down] < 0) {}
@ -89,7 +88,7 @@ optional<int> FindMatchingLine(const std::vector<std::string>& index_lines,
best = i;
}
}
return best + 1;
return best;
}
} // namespace
@ -223,10 +222,10 @@ optional<int> WorkingFile::GetBufferLineFromIndexLine(int line) {
// TODO: reenable this assert once we are using the real indexed file.
// assert(index_line >= 1 && index_line <= index_lines.size());
if (line < 1 || line > index_lines.size()) {
if (line < 0 || line >= (int)index_lines.size()) {
loguru::Text stack = loguru::stacktrace();
LOG_S(WARNING) << "Bad index_line (got " << line << ", expected [1, "
<< index_lines.size() << "]) in " << filename
LOG_S(WARNING) << "Bad index_line (got " << line << ", expected [0, "
<< index_lines.size() << ")) in " << filename
<< stack.c_str();
return nullopt;
}
@ -240,7 +239,7 @@ optional<int> WorkingFile::GetIndexLineFromBufferLine(int line) {
// See GetBufferLineFromIndexLine for additional comments.
// Note: |index_line| and |buffer_line| are 1-based.
if (line < 1 || line > buffer_lines.size()) {
if (line < 0 || line >= (int)buffer_lines.size()) {
loguru::Text stack = loguru::stacktrace();
LOG_S(WARNING) << "Bad buffer_line (got " << line
<< ", expected [1, " << buffer_lines.size() << "]) in "