mirror of
				https://github.com/MaskRay/ccls.git
				synced 2025-11-03 22:04:24 +00:00 
			
		
		
		
	Change line/column to 0-based and only use 1-based when (de,)serialized to JSON
This commit is contained in:
		
							parent
							
								
									f78d87ded3
								
							
						
					
					
						commit
						54ac72115e
					
				@ -17,8 +17,8 @@ Range ResolveCXSourceRange(const CXSourceRange& range, CXFile* cx_file) {
 | 
				
			|||||||
  unsigned int end_line, end_column;
 | 
					  unsigned int end_line, end_column;
 | 
				
			||||||
  clang_getSpellingLocation(end, nullptr, &end_line, &end_column, nullptr);
 | 
					  clang_getSpellingLocation(end, nullptr, &end_line, &end_column, nullptr);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  return Range(Position((int16_t)start_line, (int16_t)start_column) /*start*/,
 | 
					  return Range(Position((int16_t)start_line - 1, (int16_t)start_column - 1),
 | 
				
			||||||
               Position((int16_t)end_line, (int16_t)end_column) /*end*/);
 | 
					               Position((int16_t)end_line - 1, (int16_t)end_column - 1));
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// TODO Place this global variable into config
 | 
					// TODO Place this global variable into config
 | 
				
			||||||
 | 
				
			|||||||
@ -12,9 +12,9 @@ FileContents::FileContents(const std::string& path, const std::string& content)
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
optional<int> FileContents::ToOffset(Position p) const {
 | 
					optional<int> FileContents::ToOffset(Position p) const {
 | 
				
			||||||
  if (0 < p.line && size_t(p.line) <= line_offsets_.size()) {
 | 
					  if (0 <= p.line && size_t(p.line) < line_offsets_.size()) {
 | 
				
			||||||
    int ret = line_offsets_[p.line - 1] + p.column - 1;
 | 
					    int ret = line_offsets_[p.line] + p.column;
 | 
				
			||||||
    if (size_t(ret) <= content.size())
 | 
					    if (size_t(ret) < content.size())
 | 
				
			||||||
      return ret;
 | 
					      return ret;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  return nullopt;
 | 
					  return nullopt;
 | 
				
			||||||
 | 
				
			|||||||
@ -141,12 +141,12 @@ void EmitSemanticHighlighting(QueryDatabase* db,
 | 
				
			|||||||
        // shrinking hack, the contained keywords and primitive types will be
 | 
					        // shrinking hack, the contained keywords and primitive types will be
 | 
				
			||||||
        // highlighted undesiredly.
 | 
					        // highlighted undesiredly.
 | 
				
			||||||
        auto concise_name = detailed_name.substr(0, detailed_name.find('<'));
 | 
					        auto concise_name = detailed_name.substr(0, detailed_name.find('<'));
 | 
				
			||||||
        if (0 < sym.loc.range.start.line &&
 | 
					        if (0 <= sym.loc.range.start.line &&
 | 
				
			||||||
            sym.loc.range.start.line <= working_file->index_lines.size()) {
 | 
					            sym.loc.range.start.line < working_file->index_lines.size()) {
 | 
				
			||||||
          const std::string& line =
 | 
					          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;
 | 
					          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)
 | 
					          if (line.compare(col, concise_name.size(), concise_name) == 0)
 | 
				
			||||||
            sym.loc.range.end.column =
 | 
					            sym.loc.range.end.column =
 | 
				
			||||||
                sym.loc.range.start.column + concise_name.size();
 | 
					                sym.loc.range.start.column + concise_name.size();
 | 
				
			||||||
 | 
				
			|||||||
@ -64,8 +64,8 @@ struct TextDocumentDefinitionHandler
 | 
				
			|||||||
    Out_TextDocumentDefinition out;
 | 
					    Out_TextDocumentDefinition out;
 | 
				
			||||||
    out.id = request->id;
 | 
					    out.id = request->id;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    int target_line = request->params.position.line + 1;
 | 
					    int target_line = request->params.position.line;
 | 
				
			||||||
    int target_column = request->params.position.character + 1;
 | 
					    int target_column = request->params.position.character;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for (const SymbolRef& ref :
 | 
					    for (const SymbolRef& ref :
 | 
				
			||||||
         FindSymbolsAtLocation(working_file, file, request->params.position)) {
 | 
					         FindSymbolsAtLocation(working_file, file, request->params.position)) {
 | 
				
			||||||
 | 
				
			|||||||
@ -69,7 +69,7 @@ struct TextDocumentDocumentLinkHandler
 | 
				
			|||||||
        // Subtract 1 from line because querydb stores 1-based lines but
 | 
					        // Subtract 1 from line because querydb stores 1-based lines but
 | 
				
			||||||
        // vscode expects 0-based lines.
 | 
					        // vscode expects 0-based lines.
 | 
				
			||||||
        optional<lsRange> between_quotes = ExtractQuotedRange(
 | 
					        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)
 | 
					        if (!between_quotes)
 | 
				
			||||||
          continue;
 | 
					          continue;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -11,17 +11,17 @@ const char* SkipAfter(const char* input, char skip_after) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
}  // namespace
 | 
					}  // namespace
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Position::Position() {}
 | 
					Position::Position() : line(0), column(0) {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Position::Position(int16_t line, int16_t column) : line(line), column(column) {}
 | 
					Position::Position(int16_t line, int16_t column) : line(line), column(column) {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Position::Position(const char* encoded) {
 | 
					Position::Position(const char* encoded) {
 | 
				
			||||||
  assert(encoded);
 | 
					  assert(encoded);
 | 
				
			||||||
  line = (int16_t)atoi(encoded);
 | 
					  line = (int16_t)atoi(encoded) - 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  encoded = SkipAfter(encoded, ':');
 | 
					  encoded = SkipAfter(encoded, ':');
 | 
				
			||||||
  assert(encoded);
 | 
					  assert(encoded);
 | 
				
			||||||
  column = (int16_t)atoi(encoded);
 | 
					  column = (int16_t)atoi(encoded) - 1;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
std::string Position::ToString() {
 | 
					std::string Position::ToString() {
 | 
				
			||||||
@ -33,9 +33,9 @@ std::string Position::ToString() {
 | 
				
			|||||||
  // 2 => column
 | 
					  // 2 => column
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  std::string result;
 | 
					  std::string result;
 | 
				
			||||||
  result += std::to_string(line);
 | 
					  result += std::to_string(line + 1);
 | 
				
			||||||
  result += ':';
 | 
					  result += ':';
 | 
				
			||||||
  result += std::to_string(column);
 | 
					  result += std::to_string(column + 1);
 | 
				
			||||||
  return result;
 | 
					  return result;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -51,9 +51,9 @@ std::string Position::ToPrettyString(const std::string& filename) {
 | 
				
			|||||||
  std::string result;
 | 
					  std::string result;
 | 
				
			||||||
  result += filename;
 | 
					  result += filename;
 | 
				
			||||||
  result += ':';
 | 
					  result += ':';
 | 
				
			||||||
  result += std::to_string(line);
 | 
					  result += std::to_string(line + 1);
 | 
				
			||||||
  result += ':';
 | 
					  result += ':';
 | 
				
			||||||
  result += std::to_string(column);
 | 
					  result += std::to_string(column + 1);
 | 
				
			||||||
  return result;
 | 
					  return result;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -79,17 +79,17 @@ Range::Range(Position start, Position end) : start(start), end(end) {}
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
Range::Range(const char* encoded) {
 | 
					Range::Range(const char* encoded) {
 | 
				
			||||||
  char* p = const_cast<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 == ':');
 | 
					  assert(*p == ':');
 | 
				
			||||||
  p++;
 | 
					  p++;
 | 
				
			||||||
  start.column = int16_t(strtol(p, &p, 10));
 | 
					  start.column = int16_t(strtol(p, &p, 10)) - 1;
 | 
				
			||||||
  assert(*p == '-');
 | 
					  assert(*p == '-');
 | 
				
			||||||
  p++;
 | 
					  p++;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  end.line = int16_t(strtol(p, &p, 10));
 | 
					  end.line = int16_t(strtol(p, &p, 10)) - 1;
 | 
				
			||||||
  assert(*p == ':');
 | 
					  assert(*p == ':');
 | 
				
			||||||
  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 {
 | 
					bool Range::Contains(int line, int column) const {
 | 
				
			||||||
@ -117,13 +117,13 @@ std::string Range::ToString() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  std::string output;
 | 
					  std::string output;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  output += std::to_string(start.line);
 | 
					  output += std::to_string(start.line + 1);
 | 
				
			||||||
  output += ':';
 | 
					  output += ':';
 | 
				
			||||||
  output += std::to_string(start.column);
 | 
					  output += std::to_string(start.column + 1);
 | 
				
			||||||
  output += '-';
 | 
					  output += '-';
 | 
				
			||||||
  output += std::to_string(end.line);
 | 
					  output += std::to_string(end.line + 1);
 | 
				
			||||||
  output += ':';
 | 
					  output += ':';
 | 
				
			||||||
  output += std::to_string(end.column);
 | 
					  output += std::to_string(end.column + 1);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  return output;
 | 
					  return output;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -8,8 +8,8 @@
 | 
				
			|||||||
#include "utils.h"
 | 
					#include "utils.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct Position {
 | 
					struct Position {
 | 
				
			||||||
  int16_t line = -1;
 | 
					  int16_t line;
 | 
				
			||||||
  int16_t column = -1;
 | 
					  int16_t column;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  Position();
 | 
					  Position();
 | 
				
			||||||
  Position(int16_t line, int16_t column);
 | 
					  Position(int16_t line, int16_t column);
 | 
				
			||||||
 | 
				
			|||||||
@ -247,7 +247,7 @@ QueryFile::Def BuildFileDef(const IdMap& id_map, const IndexFile& indexed) {
 | 
				
			|||||||
      // 42;` will take you to the constructor.
 | 
					      // 42;` will take you to the constructor.
 | 
				
			||||||
      Range range = caller.loc;
 | 
					      Range range = caller.loc;
 | 
				
			||||||
      if (caller.is_implicit) {
 | 
					      if (caller.is_implicit) {
 | 
				
			||||||
        if (range.start.column > 1)
 | 
					        if (range.start.column > 0)
 | 
				
			||||||
          range.start.column--;
 | 
					          range.start.column--;
 | 
				
			||||||
        range.end.column++;
 | 
					        range.end.column++;
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
				
			|||||||
@ -316,20 +316,20 @@ std::vector<QueryFuncRef> GetCallersForAllDerivedFunctions(QueryDatabase* db,
 | 
				
			|||||||
optional<lsPosition> GetLsPosition(WorkingFile* working_file,
 | 
					optional<lsPosition> GetLsPosition(WorkingFile* working_file,
 | 
				
			||||||
                                   const Position& position) {
 | 
					                                   const Position& position) {
 | 
				
			||||||
  if (!working_file)
 | 
					  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);
 | 
					  optional<int> start = working_file->GetBufferLineFromIndexLine(position.line);
 | 
				
			||||||
  if (!start)
 | 
					  if (!start)
 | 
				
			||||||
    return nullopt;
 | 
					    return nullopt;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  return lsPosition(*start - 1, position.column - 1);
 | 
					  return lsPosition(*start, position.column);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
optional<lsRange> GetLsRange(WorkingFile* working_file, const Range& location) {
 | 
					optional<lsRange> GetLsRange(WorkingFile* working_file, const Range& location) {
 | 
				
			||||||
  if (!working_file) {
 | 
					  if (!working_file) {
 | 
				
			||||||
    return lsRange(
 | 
					    return lsRange(
 | 
				
			||||||
        lsPosition(location.start.line - 1, location.start.column - 1),
 | 
					        lsPosition(location.start.line, location.start.column),
 | 
				
			||||||
        lsPosition(location.end.line - 1, location.end.column - 1));
 | 
					        lsPosition(location.end.line, location.end.column));
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  optional<int> start =
 | 
					  optional<int> start =
 | 
				
			||||||
@ -348,8 +348,8 @@ optional<lsRange> GetLsRange(WorkingFile* working_file, const Range& location) {
 | 
				
			|||||||
  if (*end < *start)
 | 
					  if (*end < *start)
 | 
				
			||||||
    *end = *start + (location.end.line - location.start.line);
 | 
					    *end = *start + (location.end.line - location.start.line);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  return lsRange(lsPosition(*start - 1, location.start.column - 1),
 | 
					  return lsRange(lsPosition(*start, location.start.column),
 | 
				
			||||||
                 lsPosition(*end - 1, location.end.column - 1));
 | 
					                 lsPosition(*end, location.end.column));
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
lsDocumentUri GetLsDocumentUri(QueryDatabase* db,
 | 
					lsDocumentUri GetLsDocumentUri(QueryDatabase* db,
 | 
				
			||||||
@ -478,8 +478,8 @@ std::vector<SymbolRef> FindSymbolsAtLocation(WorkingFile* working_file,
 | 
				
			|||||||
  std::vector<SymbolRef> symbols;
 | 
					  std::vector<SymbolRef> symbols;
 | 
				
			||||||
  symbols.reserve(1);
 | 
					  symbols.reserve(1);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  int target_line = position.line + 1;
 | 
					  int target_line = position.line;
 | 
				
			||||||
  int target_column = position.character + 1;
 | 
					  int target_column = position.character;
 | 
				
			||||||
  if (working_file) {
 | 
					  if (working_file) {
 | 
				
			||||||
    optional<int> index_line =
 | 
					    optional<int> index_line =
 | 
				
			||||||
        working_file->GetIndexLineFromBufferLine(target_line);
 | 
					        working_file->GetIndexLineFromBufferLine(target_line);
 | 
				
			||||||
 | 
				
			|||||||
@ -69,9 +69,8 @@ optional<int> FindMatchingLine(const std::vector<std::string>& index_lines,
 | 
				
			|||||||
                               const std::vector<int>& index_to_buffer,
 | 
					                               const std::vector<int>& index_to_buffer,
 | 
				
			||||||
                               int line,
 | 
					                               int line,
 | 
				
			||||||
                               const std::vector<std::string>& buffer_lines) {
 | 
					                               const std::vector<std::string>& buffer_lines) {
 | 
				
			||||||
  line--;
 | 
					 | 
				
			||||||
  if (index_to_buffer[line] >= 0)
 | 
					  if (index_to_buffer[line] >= 0)
 | 
				
			||||||
    return index_to_buffer[line] + 1;
 | 
					    return index_to_buffer[line];
 | 
				
			||||||
  int up = line, down = line;
 | 
					  int up = line, down = line;
 | 
				
			||||||
  while (--up >= 0 && index_to_buffer[up] < 0) {}
 | 
					  while (--up >= 0 && index_to_buffer[up] < 0) {}
 | 
				
			||||||
  while (++down < int(index_to_buffer.size()) && index_to_buffer[down] < 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;
 | 
					      best = i;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  return best + 1;
 | 
					  return best;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}  // namespace
 | 
					}  // namespace
 | 
				
			||||||
@ -223,10 +222,10 @@ optional<int> WorkingFile::GetBufferLineFromIndexLine(int line) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  // TODO: reenable this assert once we are using the real indexed file.
 | 
					  // TODO: reenable this assert once we are using the real indexed file.
 | 
				
			||||||
  // assert(index_line >= 1 && index_line <= index_lines.size());
 | 
					  // 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();
 | 
					    loguru::Text stack = loguru::stacktrace();
 | 
				
			||||||
    LOG_S(WARNING) << "Bad index_line (got " << line << ", expected [1, "
 | 
					    LOG_S(WARNING) << "Bad index_line (got " << line << ", expected [0, "
 | 
				
			||||||
                   << index_lines.size() << "]) in " << filename
 | 
					                   << index_lines.size() << ")) in " << filename
 | 
				
			||||||
                   << stack.c_str();
 | 
					                   << stack.c_str();
 | 
				
			||||||
    return nullopt;
 | 
					    return nullopt;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
@ -240,7 +239,7 @@ optional<int> WorkingFile::GetIndexLineFromBufferLine(int line) {
 | 
				
			|||||||
  // See GetBufferLineFromIndexLine for additional comments.
 | 
					  // See GetBufferLineFromIndexLine for additional comments.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // Note: |index_line| and |buffer_line| are 1-based.
 | 
					  // 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();
 | 
					    loguru::Text stack = loguru::stacktrace();
 | 
				
			||||||
    LOG_S(WARNING) << "Bad buffer_line (got " << line
 | 
					    LOG_S(WARNING) << "Bad buffer_line (got " << line
 | 
				
			||||||
                   << ", expected [1, " << buffer_lines.size() << "]) in "
 | 
					                   << ", expected [1, " << buffer_lines.size() << "]) in "
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user