From 54ac72115ea1fe7ae24f1ddfccdcc5291d7302d9 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Sun, 14 Jan 2018 11:39:29 -0800 Subject: [PATCH] Change line/column to 0-based and only use 1-based when (de,)serialized to JSON --- src/clang_cursor.cc | 4 +-- src/file_contents.cc | 8 +++--- src/message_handler.cc | 8 +++--- src/messages/text_document_definition.cc | 4 +-- src/messages/text_document_document_link.cc | 2 +- src/position.cc | 30 ++++++++++----------- src/position.h | 4 +-- src/query.cc | 2 +- src/query_utils.cc | 16 +++++------ src/working_files.cc | 13 +++++---- 10 files changed, 45 insertions(+), 46 deletions(-) diff --git a/src/clang_cursor.cc b/src/clang_cursor.cc index 5c6cea3f..d1d38b57 100644 --- a/src/clang_cursor.cc +++ b/src/clang_cursor.cc @@ -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 diff --git a/src/file_contents.cc b/src/file_contents.cc index 09ec8c75..4e716e65 100644 --- a/src/file_contents.cc +++ b/src/file_contents.cc @@ -12,9 +12,9 @@ FileContents::FileContents(const std::string& path, const std::string& content) } optional 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 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; -} \ No newline at end of file +} diff --git a/src/message_handler.cc b/src/message_handler.cc index 31c37590..6fa94184 100644 --- a/src/message_handler.cc +++ b/src/message_handler.cc @@ -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(); diff --git a/src/messages/text_document_definition.cc b/src/messages/text_document_definition.cc index ec6e4817..ccee686f 100644 --- a/src/messages/text_document_definition.cc +++ b/src/messages/text_document_definition.cc @@ -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)) { diff --git a/src/messages/text_document_document_link.cc b/src/messages/text_document_document_link.cc index c4ba69b0..341d8d85 100644 --- a/src/messages/text_document_document_link.cc +++ b/src/messages/text_document_document_link.cc @@ -69,7 +69,7 @@ struct TextDocumentDocumentLinkHandler // Subtract 1 from line because querydb stores 1-based lines but // vscode expects 0-based lines. optional 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; diff --git a/src/position.cc b/src/position.cc index f3514f25..cda9a564 100644 --- a/src/position.cc +++ b/src/position.cc @@ -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(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; } diff --git a/src/position.h b/src/position.h index 205bb8ed..cc41e65e 100644 --- a/src/position.h +++ b/src/position.h @@ -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); diff --git a/src/query.cc b/src/query.cc index 921b24bb..1fc9884e 100644 --- a/src/query.cc +++ b/src/query.cc @@ -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++; } diff --git a/src/query_utils.cc b/src/query_utils.cc index eff1f271..8679b80c 100644 --- a/src/query_utils.cc +++ b/src/query_utils.cc @@ -316,20 +316,20 @@ std::vector GetCallersForAllDerivedFunctions(QueryDatabase* db, optional 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 start = working_file->GetBufferLineFromIndexLine(position.line); if (!start) return nullopt; - return lsPosition(*start - 1, position.column - 1); + return lsPosition(*start, position.column); } optional 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 start = @@ -348,8 +348,8 @@ optional 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 FindSymbolsAtLocation(WorkingFile* working_file, std::vector 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 index_line = working_file->GetIndexLineFromBufferLine(target_line); diff --git a/src/working_files.cc b/src/working_files.cc index 27ebd605..f86cc03d 100644 --- a/src/working_files.cc +++ b/src/working_files.cc @@ -69,9 +69,8 @@ optional FindMatchingLine(const std::vector& index_lines, const std::vector& index_to_buffer, int line, const std::vector& 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 FindMatchingLine(const std::vector& index_lines, best = i; } } - return best + 1; + return best; } } // namespace @@ -223,10 +222,10 @@ optional 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 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 "