Reduce MAKE_REFLECT_STRUCT in lsp.hh

Position -> Pos; lsPosition -> Position
This commit is contained in:
Fangrui Song 2018-11-04 10:30:18 -08:00
parent 50736827ca
commit 6dfe5d1c4f
21 changed files with 152 additions and 225 deletions

View File

@ -497,17 +497,17 @@ void *DiagnosticMain(void *manager_) {
case DiagnosticsEngine::Ignored:
// llvm_unreachable
case DiagnosticsEngine::Remark:
ret.severity = DiagnosticSeverity::Hint;
ret.severity = 4;
break;
case DiagnosticsEngine::Note:
ret.severity = DiagnosticSeverity::Information;
ret.severity = 3;
break;
case DiagnosticsEngine::Warning:
ret.severity = DiagnosticSeverity::Warning;
ret.severity = 2;
break;
case DiagnosticsEngine::Error:
case DiagnosticsEngine::Fatal:
ret.severity = DiagnosticSeverity::Error;
ret.severity = 1;
break;
}
ret.code = d.category;

View File

@ -75,7 +75,7 @@ struct CompletionManager {
struct CompletionRequest {
CompletionRequest(const RequestId &id,
const TextDocumentIdentifier &document,
const lsPosition &position,
const Position &position,
std::unique_ptr<clang::CodeCompleteConsumer> Consumer,
clang::CodeCompleteOptions CCOpts,
const OnComplete &on_complete)
@ -85,7 +85,7 @@ struct CompletionManager {
RequestId id;
TextDocumentIdentifier document;
lsPosition position;
Position position;
std::unique_ptr<clang::CodeCompleteConsumer> Consumer;
clang::CodeCompleteOptions CCOpts;
OnComplete on_complete;
@ -165,7 +165,7 @@ template <typename T>
struct CompleteConsumerCache {
// NOTE: Make sure to access these variables under |WithLock|.
std::optional<std::string> path;
std::optional<lsPosition> position;
std::optional<Position> position;
T result;
std::mutex mutex;
@ -174,7 +174,7 @@ struct CompleteConsumerCache {
std::lock_guard lock(mutex);
action();
}
bool IsCacheValid(const std::string path, lsPosition position) {
bool IsCacheValid(const std::string path, Position position) {
std::lock_guard lock(mutex);
return this->path == path && this->position == position;
}

View File

@ -29,7 +29,7 @@ std::string PathFromFileEntry(const FileEntry &file) {
return ret;
}
static Position Decomposed2LineAndCol(const SourceManager &SM,
static Pos Decomposed2LineAndCol(const SourceManager &SM,
std::pair<FileID, unsigned> I) {
int l = (int)SM.getLineNumber(I.first, I.second) - 1,
c = (int)SM.getColumnNumber(I.first, I.second) - 1;

View File

@ -31,7 +31,32 @@ template <> struct hash<llvm::sys::fs::UniqueID> {
namespace ccls {
using Usr = uint64_t;
enum class LanguageId;
// The order matters. In FindSymbolsAtLocation, we want Var/Func ordered in
// front of others.
enum class Kind : uint8_t { Invalid, File, Type, Func, Var };
MAKE_REFLECT_TYPE_PROXY(Kind);
enum class Role : uint16_t {
None = 0,
Declaration = 1 << 0,
Definition = 1 << 1,
Reference = 1 << 2,
Read = 1 << 3,
Write = 1 << 4,
Call = 1 << 5,
Dynamic = 1 << 6,
Address = 1 << 7,
Implicit = 1 << 8,
All = (1 << 9) - 1,
};
MAKE_REFLECT_TYPE_PROXY(Role);
inline uint16_t operator&(Role lhs, Role rhs) {
return uint16_t(lhs) & uint16_t(rhs);
}
inline Role operator|(Role lhs, Role rhs) {
return Role(uint16_t(lhs) | uint16_t(rhs));
}
struct SymbolIdx {
Usr usr;
@ -44,7 +69,6 @@ struct SymbolIdx {
return usr != o.usr ? usr < o.usr : kind < o.kind;
}
};
MAKE_REFLECT_STRUCT(SymbolIdx, usr, kind);
// |id,kind| refer to the referenced entity.
struct SymbolRef {

View File

@ -125,11 +125,7 @@ std::string DocumentUri::GetPath() const {
return ret;
}
std::string lsPosition::ToString() const {
std::string Position::ToString() const {
return std::to_string(line) + ":" + std::to_string(character);
}
bool TextEdit::operator==(const TextEdit &that) {
return range == that.range && newText == that.newText;
}
} // namespace ccls

View File

@ -48,7 +48,6 @@ enum class ErrorCode {
// Defined by the protocol.
RequestCancelled = -32800,
};
MAKE_REFLECT_TYPE_PROXY(ErrorCode);
struct ResponseError {
// A number indicating the error type that occurred.
@ -61,19 +60,10 @@ struct ResponseError {
// information about the error. Can be omitted.
// std::optional<D> data;
};
MAKE_REFLECT_STRUCT(ResponseError, code, message);
constexpr char ccls_xref[] = "ccls.xref";
constexpr char window_showMessage[] = "window/showMessage";
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
////////////////////////////// PRIMITIVE TYPES //////////////////////////////
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
struct DocumentUri {
static DocumentUri FromPath(const std::string &path);
@ -90,22 +80,21 @@ void Reflect(TVisitor &visitor, DocumentUri &value) {
Reflect(visitor, value.raw_uri);
}
struct lsPosition {
struct Position {
int line = 0;
int character = 0;
bool operator==(const lsPosition &o) const {
bool operator==(const Position &o) const {
return line == o.line && character == o.character;
}
bool operator<(const lsPosition &o) const {
bool operator<(const Position &o) const {
return line != o.line ? line < o.line : character < o.character;
}
std::string ToString() const;
};
MAKE_REFLECT_STRUCT(lsPosition, line, character);
struct lsRange {
lsPosition start;
lsPosition end;
Position start;
Position end;
bool operator==(const lsRange &o) const {
return start == o.start && end == o.end;
}
@ -113,7 +102,6 @@ struct lsRange {
return !(start == o.start) ? start < o.start : end < o.end;
}
};
MAKE_REFLECT_STRUCT(lsRange, start, end);
struct Location {
DocumentUri uri;
@ -126,7 +114,6 @@ struct Location {
: range < o.range;
}
};
MAKE_REFLECT_STRUCT(Location, uri, range);
enum class SymbolKind : uint8_t {
Unknown = 0,
@ -169,7 +156,6 @@ enum class SymbolKind : uint8_t {
StaticMethod = 254,
Macro = 255,
};
MAKE_REFLECT_TYPE_PROXY(SymbolKind);
struct SymbolInformation {
std::string_view name;
@ -181,57 +167,24 @@ struct SymbolInformation {
struct TextDocumentIdentifier {
DocumentUri uri;
};
MAKE_REFLECT_STRUCT(TextDocumentIdentifier, uri);
struct VersionedTextDocumentIdentifier {
DocumentUri uri;
// The version number of this document. number | null
std::optional<int> version;
};
MAKE_REFLECT_STRUCT(VersionedTextDocumentIdentifier, uri, version);
struct TextEdit {
// The range of the text document to be manipulated. To insert
// text into a document create a range where start === end.
lsRange range;
// The string to be inserted. For delete operations use an
// empty string.
std::string newText;
bool operator==(const TextEdit &that);
};
MAKE_REFLECT_STRUCT(TextEdit, range, newText);
struct TextDocumentItem {
// The text document's URI.
DocumentUri uri;
// The text document's language identifier.
std::string languageId;
// The version number of this document (it will strictly increase after each
// change, including undo/redo).
int version;
// The content of the opened text document.
std::string text;
};
MAKE_REFLECT_STRUCT(TextDocumentItem, uri, languageId, version, text);
struct TextDocumentEdit {
// The text document to change.
VersionedTextDocumentIdentifier textDocument;
// The edits to be applied.
std::vector<TextEdit> edits;
};
MAKE_REFLECT_STRUCT(TextDocumentEdit, textDocument, edits);
struct WorkspaceEdit {
std::vector<TextDocumentEdit> documentChanges;
};
MAKE_REFLECT_STRUCT(WorkspaceEdit, documentChanges);
struct TextDocumentContentChangeEvent {
// The range of the document that changed.
@ -251,84 +204,27 @@ struct WorkspaceFolder {
DocumentUri uri;
std::string name;
};
MAKE_REFLECT_STRUCT(WorkspaceFolder, uri, name);
// Show a message to the user.
enum class MessageType : int { Error = 1, Warning = 2, Info = 3, Log = 4 };
MAKE_REFLECT_TYPE_PROXY(MessageType)
enum class DiagnosticSeverity {
// Reports an error.
Error = 1,
// Reports a warning.
Warning = 2,
// Reports an information.
Information = 3,
// Reports a hint.
Hint = 4
};
MAKE_REFLECT_TYPE_PROXY(DiagnosticSeverity);
struct Diagnostic {
// The range at which the message applies.
lsRange range;
// The diagnostic's severity. Can be omitted. If omitted it is up to the
// client to interpret diagnostics as error, warning, info or hint.
std::optional<DiagnosticSeverity> severity;
// The diagnostic's code. Can be omitted.
int severity = 0;
int code = 0;
// A human-readable string describing the source of this
// diagnostic, e.g. 'typescript' or 'super lint'.
std::string source = "ccls";
// The diagnostic's message.
std::string message;
// Non-serialized set of fixits.
std::vector<TextEdit> fixits_;
};
MAKE_REFLECT_STRUCT(Diagnostic, range, severity, source, message);
struct ShowMessageParam {
MessageType type = MessageType::Error;
std::string message;
};
MAKE_REFLECT_STRUCT(ShowMessageParam, type, message);
// Used to identify the language at a file level. The ordering is important, as
// a file previously identified as `C`, will be changed to `Cpp` if it
// encounters a c++ declaration.
enum class LanguageId { Unknown = -1, C = 0, Cpp = 1, ObjC = 2, ObjCpp = 3 };
MAKE_REFLECT_TYPE_PROXY(LanguageId);
// The order matters. In FindSymbolsAtLocation, we want Var/Func ordered in
// front of others.
enum class Kind : uint8_t { Invalid, File, Type, Func, Var };
MAKE_REFLECT_TYPE_PROXY(Kind);
enum class Role : uint16_t {
None = 0,
Declaration = 1 << 0,
Definition = 1 << 1,
Reference = 1 << 2,
Read = 1 << 3,
Write = 1 << 4,
Call = 1 << 5,
Dynamic = 1 << 6,
Address = 1 << 7,
Implicit = 1 << 8,
All = (1 << 9) - 1,
};
MAKE_REFLECT_TYPE_PROXY(Role);
inline uint16_t operator&(Role lhs, Role rhs) {
return uint16_t(lhs) & uint16_t(rhs);
}
inline Role operator|(Role lhs, Role rhs) {
return Role(uint16_t(lhs) | uint16_t(rhs));
}
} // namespace ccls

View File

@ -3,7 +3,7 @@
#include "match.hh"
#include "lsp.hh"
#include "message_handler.hh"
#include "pipeline.hh"
namespace ccls {

View File

@ -18,6 +18,8 @@ using namespace clang;
MAKE_HASHABLE(ccls::SymbolIdx, t.usr, t.kind);
namespace ccls {
MAKE_REFLECT_STRUCT(CodeActionParam::Context, diagnostics);
MAKE_REFLECT_STRUCT(CodeActionParam, textDocument, range, context);
MAKE_REFLECT_STRUCT(EmptyParam, placeholder);
MAKE_REFLECT_STRUCT(TextDocumentParam, textDocument);
MAKE_REFLECT_STRUCT(DidOpenTextDocumentParam, textDocument);
@ -26,10 +28,6 @@ MAKE_REFLECT_STRUCT(TextDocumentDidChangeParam, textDocument, contentChanges);
MAKE_REFLECT_STRUCT(TextDocumentPositionParam, textDocument, position);
MAKE_REFLECT_STRUCT(RenameParam, textDocument, position, newName);
// code*
MAKE_REFLECT_STRUCT(CodeActionParam::Context, diagnostics);
MAKE_REFLECT_STRUCT(CodeActionParam, textDocument, range, context);
// completion
MAKE_REFLECT_TYPE_PROXY(CompletionTriggerKind);
MAKE_REFLECT_STRUCT(CompletionContext, triggerKind, triggerCharacter);
@ -62,24 +60,23 @@ struct CclsSemanticHighlightSymbol {
std::vector<lsRange> lsRanges;
};
struct CclsSemanticHighlightParams {
struct CclsSemanticHighlight {
DocumentUri uri;
std::vector<CclsSemanticHighlightSymbol> symbols;
};
MAKE_REFLECT_STRUCT(CclsSemanticHighlightSymbol, id, parentKind, kind, storage,
ranges, lsRanges);
MAKE_REFLECT_STRUCT(CclsSemanticHighlightParams, uri, symbols);
MAKE_REFLECT_STRUCT(CclsSemanticHighlight, uri, symbols);
struct CclsSetSkippedRangesParams {
struct CclsSetSkippedRanges {
DocumentUri uri;
std::vector<lsRange> skippedRanges;
};
MAKE_REFLECT_STRUCT(CclsSetSkippedRangesParams, uri, skippedRanges);
MAKE_REFLECT_STRUCT(CclsSetSkippedRanges, uri, skippedRanges);
struct ScanLineEvent {
lsPosition pos;
lsPosition end_pos; // Second key when there is a tie for insertion events.
Position pos;
Position end_pos; // Second key when there is a tie for insertion events.
int id;
CclsSemanticHighlightSymbol *symbol;
bool operator<(const ScanLineEvent &other) const {
@ -261,7 +258,7 @@ QueryFile *MessageHandler::FindFile(ReplyOnce &reply,
}
void EmitSkippedRanges(WorkingFile *wfile, QueryFile &file) {
CclsSetSkippedRangesParams params;
CclsSetSkippedRanges params;
params.uri = DocumentUri::FromPath(wfile->filename);
for (Range skipped : file.def->skipped_ranges)
if (auto ls_skipped = GetLsRange(wfile, skipped))
@ -353,8 +350,7 @@ void EmitSemanticHighlight(DB *db, WorkingFile *wfile, QueryFile &file) {
continue; // applies to for loop
}
std::optional<lsRange> loc = GetLsRange(wfile, sym.range);
if (loc) {
if (std::optional<lsRange> loc = GetLsRange(wfile, sym.range)) {
auto it = grouped_symbols.find(sym);
if (it != grouped_symbols.end()) {
it->second.lsRanges.push_back(*loc);
@ -409,7 +405,7 @@ void EmitSemanticHighlight(DB *db, WorkingFile *wfile, QueryFile &file) {
deleted[~events[i].id] = 1;
}
CclsSemanticHighlightParams params;
CclsSemanticHighlight params;
params.uri = DocumentUri::FromPath(wfile->filename);
// Transform lsRange into pair<int, int> (offset pairs)
if (!g_config->highlight.lsRanges) {

View File

@ -36,28 +36,6 @@ void Reply(RequestId id, const std::function<void(Writer &)> &fn);
void ReplyError(RequestId id, const std::function<void(Writer &)> &fn);
}
struct EmptyParam {
bool placeholder;
};
struct TextDocumentParam {
TextDocumentIdentifier textDocument;
};
struct DidOpenTextDocumentParam {
TextDocumentItem textDocument;
};
struct TextDocumentPositionParam {
TextDocumentIdentifier textDocument;
lsPosition position;
};
struct RenameParam {
TextDocumentIdentifier textDocument;
lsPosition position;
std::string newName;
};
// code*
struct CodeActionParam {
TextDocumentIdentifier textDocument;
lsRange range;
@ -65,6 +43,33 @@ struct CodeActionParam {
std::vector<Diagnostic> diagnostics;
} context;
};
struct EmptyParam {
bool placeholder;
};
struct DidOpenTextDocumentParam {
TextDocumentItem textDocument;
};
struct RenameParam {
TextDocumentIdentifier textDocument;
Position position;
std::string newName;
};
struct TextDocumentParam {
TextDocumentIdentifier textDocument;
};
struct TextDocumentPositionParam {
TextDocumentIdentifier textDocument;
Position position;
};
struct TextDocumentEdit {
VersionedTextDocumentIdentifier textDocument;
std::vector<TextEdit> edits;
};
MAKE_REFLECT_STRUCT(TextDocumentEdit, textDocument, edits);
struct WorkspaceEdit {
std::vector<TextDocumentEdit> documentChanges;
};
MAKE_REFLECT_STRUCT(WorkspaceEdit, documentChanges);
// completion
enum class CompletionTriggerKind {
@ -139,7 +144,7 @@ struct DocumentFormattingParam {
};
struct DocumentOnTypeFormattingParam {
TextDocumentIdentifier textDocument;
lsPosition position;
Position position;
std::string ch;
FormattingOptions options;
};
@ -173,6 +178,21 @@ struct WorkspaceSymbolParam {
// ccls extensions
std::vector<std::string> folders;
};
MAKE_REFLECT_STRUCT(WorkspaceFolder, uri, name);
MAKE_REFLECT_TYPE_PROXY(ErrorCode);
MAKE_REFLECT_STRUCT(ResponseError, code, message);
MAKE_REFLECT_STRUCT(Position, line, character);
MAKE_REFLECT_STRUCT(lsRange, start, end);
MAKE_REFLECT_STRUCT(Location, uri, range);
MAKE_REFLECT_TYPE_PROXY(SymbolKind);
MAKE_REFLECT_STRUCT(TextDocumentIdentifier, uri);
MAKE_REFLECT_STRUCT(TextDocumentItem, uri, languageId, version, text);
MAKE_REFLECT_STRUCT(TextEdit, range, newText);
MAKE_REFLECT_STRUCT(VersionedTextDocumentIdentifier, uri, version);
MAKE_REFLECT_STRUCT(Diagnostic, range, severity, code, source, message);
MAKE_REFLECT_STRUCT(ShowMessageParam, type, message);
MAKE_REFLECT_TYPE_PROXY(LanguageId);
// TODO llvm 8 llvm::unique_function
template <typename Res>

View File

@ -20,12 +20,12 @@ namespace ccls {
namespace {
struct Param {
TextDocumentIdentifier textDocument;
lsPosition position;
Position position;
std::string direction;
};
MAKE_REFLECT_STRUCT(Param, textDocument, position, direction);
Maybe<Range> FindParent(QueryFile *file, Position pos) {
Maybe<Range> FindParent(QueryFile *file, Pos pos) {
Maybe<Range> parent;
for (auto [sym, refcnt] : file->symbol2refcnt)
if (refcnt > 0 && sym.extent.Valid() && sym.extent.start <= pos &&
@ -47,12 +47,12 @@ void MessageHandler::ccls_navigate(Reader &reader,
return;
WorkingFile *wfile = wfiles->GetFileByFilename(file->def->path);
lsPosition ls_pos = param.position;
Position ls_pos = param.position;
if (wfile && wfile->index_lines.size())
if (auto line = wfile->GetIndexPosFromBufferPos(ls_pos.line,
&ls_pos.character, false))
ls_pos.line = *line;
Position pos{(int16_t)ls_pos.line, (int16_t)ls_pos.character};
Pos pos{(int16_t)ls_pos.line, (int16_t)ls_pos.character};
Maybe<Range> res;
switch (param.direction[0]) {

View File

@ -83,7 +83,7 @@ ParseIncludeLineResult ParseIncludeLine(const std::string &line) {
// significantly snappier completion experience as vscode is easily overloaded
// when given 1000+ completion items.
void FilterCandidates(CompletionList &result, const std::string &complete_text,
lsPosition begin_pos, lsPosition end_pos,
Position begin_pos, Position end_pos,
const std::string &buffer_line) {
assert(begin_pos.line == end_pos.line);
auto &items = result.items;
@ -114,7 +114,7 @@ void FilterCandidates(CompletionList &result, const std::string &complete_text,
// Order of textEdit and additionalTextEdits is unspecified.
auto &edits = item.additionalTextEdits;
if (edits.size() && edits[0].range.end == begin_pos) {
lsPosition start = edits[0].range.start, end = edits[0].range.end;
Position start = edits[0].range.start, end = edits[0].range.end;
item.textEdit.range.start = start;
item.textEdit.newText = edits[0].newText + item.textEdit.newText;
if (start.line == begin_pos.line && item.filterText) {
@ -470,8 +470,8 @@ void MessageHandler::textDocument_completion(CompletionParam &param,
}
std::string completion_text;
lsPosition end_pos = param.position;
lsPosition begin_pos = file->FindStableCompletionSource(
Position end_pos = param.position;
Position begin_pos = file->FindStableCompletionSource(
param.position, &completion_text, &end_pos);
ParseIncludeLineResult preprocess = ParseIncludeLine(buffer_line);

View File

@ -43,7 +43,7 @@ void MessageHandler::textDocument_definition(TextDocumentPositionParam &param,
std::vector<Location> result;
Maybe<Use> on_def;
WorkingFile *wfile = wfiles->GetFileByFilename(file->def->path);
lsPosition &ls_pos = param.position;
Position &ls_pos = param.position;
for (SymbolRef sym : FindSymbolsAtLocation(wfile, file, ls_pos, true)) {
// Special cases which are handled:
@ -95,7 +95,7 @@ void MessageHandler::textDocument_definition(TextDocumentPositionParam &param,
}
// Find the best match of the identifier at point.
if (!range) {
lsPosition position = param.position;
Position position = param.position;
const std::string &buffer = wfile->buffer_content;
std::string_view query = LexIdentifierAroundPos(position, buffer);
std::string_view short_query = query;

View File

@ -31,14 +31,9 @@ WorkspaceEdit BuildWorkspaceEdit(DB *db, WorkingFiles *wfiles, SymbolRef sym,
path_to_edit[file_id].textDocument.version = working_file->version;
}
TextEdit edit;
TextEdit &edit = path_to_edit[file_id].edits.emplace_back();
edit.range = ls_location->range;
edit.newText = new_text;
// vscode complains if we submit overlapping text edits.
auto &edits = path_to_edit[file_id].edits;
if (std::find(edits.begin(), edits.end(), edit) == edits.end())
edits.push_back(edit);
});
WorkspaceEdit edit;

View File

@ -143,10 +143,10 @@ void MessageHandler::textDocument_signatureHelp(
static CompleteConsumerCache<SignatureHelp> cache;
std::string path = param.textDocument.uri.GetPath();
lsPosition begin_pos = param.position;
Position begin_pos = param.position;
if (WorkingFile *file = wfiles->GetFileByFilename(path)) {
std::string completion_text;
lsPosition end_pos = param.position;
Position end_pos = param.position;
begin_pos = file->FindStableCompletionSource(param.position,
&completion_text, &end_pos);
}

View File

@ -10,7 +10,7 @@
#include <stdlib.h>
namespace ccls {
Position Position::FromString(const std::string &encoded) {
Pos Pos::FromString(const std::string &encoded) {
char *p = const_cast<char *>(encoded.c_str());
int16_t line = int16_t(strtol(p, &p, 10)) - 1;
assert(*p == ':');
@ -19,14 +19,14 @@ Position Position::FromString(const std::string &encoded) {
return {line, column};
}
std::string Position::ToString() {
std::string Pos::ToString() {
char buf[99];
snprintf(buf, sizeof buf, "%d:%d", line + 1, column + 1);
return buf;
}
Range Range::FromString(const std::string &encoded) {
Position start, end;
Pos start, end;
char *p = const_cast<char *>(encoded.c_str());
start.line = int16_t(strtol(p, &p, 10)) - 1;
assert(*p == ':');
@ -45,11 +45,11 @@ Range Range::FromString(const std::string &encoded) {
bool Range::Contains(int line, int column) const {
if (line > INT16_MAX)
return false;
Position p{int16_t(line), int16_t(std::min(column, INT16_MAX))};
Pos p{int16_t(line), int16_t(std::min(column, INT16_MAX))};
return !(p < start) && p < end;
}
Range Range::RemovePrefix(Position position) const {
Range Range::RemovePrefix(Pos position) const {
return {std::min(std::max(position, start), end), end};
}
@ -61,15 +61,15 @@ std::string Range::ToString() {
}
// Position
void Reflect(Reader &visitor, Position &value) {
void Reflect(Reader &visitor, Pos &value) {
if (visitor.Format() == SerializeFormat::Json) {
value = Position::FromString(visitor.GetString());
value = Pos::FromString(visitor.GetString());
} else {
Reflect(visitor, value.line);
Reflect(visitor, value.column);
}
}
void Reflect(Writer &visitor, Position &value) {
void Reflect(Writer &visitor, Pos &value) {
if (visitor.Format() == SerializeFormat::Json) {
std::string output = value.ToString();
visitor.String(output.c_str(), output.size());

View File

@ -10,37 +10,37 @@
#include <string>
namespace ccls {
struct Position {
struct Pos {
int16_t line = -1;
int16_t column = -1;
static Position FromString(const std::string &encoded);
static Pos FromString(const std::string &encoded);
bool Valid() const { return line >= 0; }
std::string ToString();
// Compare two Positions and check if they are equal. Ignores the value of
// |interesting|.
bool operator==(const Position &o) const {
bool operator==(const Pos &o) const {
return line == o.line && column == o.column;
}
bool operator<(const Position &o) const {
bool operator<(const Pos &o) const {
if (line != o.line)
return line < o.line;
return column < o.column;
}
bool operator<=(const Position &o) const { return !(o < *this); }
bool operator<=(const Pos &o) const { return !(o < *this); }
};
struct Range {
Position start;
Position end;
Pos start;
Pos end;
static Range FromString(const std::string &encoded);
bool Valid() const { return start.Valid(); }
bool Contains(int line, int column) const;
Range RemovePrefix(Position position) const;
Range RemovePrefix(Pos position) const;
std::string ToString();
@ -55,8 +55,8 @@ struct Range {
// Reflection
class Reader;
class Writer;
void Reflect(Reader &visitor, Position &value);
void Reflect(Writer &visitor, Position &value);
void Reflect(Reader &visitor, Pos &value);
void Reflect(Writer &visitor, Pos &value);
void Reflect(Reader &visitor, Range &value);
void Reflect(Writer &visitor, Range &value);
} // namespace ccls
@ -75,4 +75,4 @@ template <> struct hash<ccls::Range> {
};
} // namespace std
MAKE_HASHABLE(ccls::Position, t.line, t.column);
MAKE_HASHABLE(ccls::Pos, t.line, t.column);

View File

@ -143,8 +143,8 @@ std::vector<Use> GetUsesForAllDerived(DB *db, QueryFunc &root) {
std::optional<lsRange> GetLsRange(WorkingFile *wfile,
const Range &location) {
if (!wfile || wfile->index_lines.empty())
return lsRange{lsPosition{location.start.line, location.start.column},
lsPosition{location.end.line, location.end.column}};
return lsRange{Position{location.start.line, location.start.column},
Position{location.end.line, location.end.column}};
int start_column = location.start.column, end_column = location.end.column;
std::optional<int> start = wfile->GetBufferPosFromIndexPos(
@ -165,8 +165,7 @@ std::optional<lsRange> GetLsRange(WorkingFile *wfile,
if (*start == *end && start_column > end_column)
end_column = start_column;
return lsRange{lsPosition{*start, start_column},
lsPosition{*end, end_column}};
return lsRange{Position{*start, start_column}, Position{*end, end_column}};
}
DocumentUri GetLsDocumentUri(DB *db, int file_id, std::string *path) {
@ -266,8 +265,7 @@ std::optional<SymbolInformation> GetSymbolInfo(DB *db, SymbolIdx sym,
}
std::vector<SymbolRef> FindSymbolsAtLocation(WorkingFile *wfile,
QueryFile *file,
lsPosition &ls_pos,
QueryFile *file, Position &ls_pos,
bool smallest) {
std::vector<SymbolRef> symbols;
// If multiVersion > 0, index may not exist and thus index_lines is empty.

View File

@ -38,7 +38,7 @@ std::optional<SymbolInformation> GetSymbolInfo(DB *db, SymbolIdx sym,
std::vector<SymbolRef> FindSymbolsAtLocation(WorkingFile *working_file,
QueryFile *file,
lsPosition &ls_pos,
Position &ls_pos,
bool smallest = false);
template <typename Fn> void WithEntity(DB *db, SymbolIdx sym, Fn &&fn) {

View File

@ -6,6 +6,7 @@
#include "filesystem.hh"
#include "indexer.hh"
#include "log.hh"
#include "message_handler.hh"
#include "serializers/binary.hh"
#include "serializers/json.hh"

View File

@ -21,7 +21,7 @@ constexpr int kMaxDiff = 20;
// |kMaxColumnAlignSize|.
constexpr int kMaxColumnAlignSize = 200;
lsPosition GetPositionForOffset(const std::string &content, int offset) {
Position GetPositionForOffset(const std::string &content, int offset) {
if (offset >= content.size())
offset = (int)content.size() - 1;
@ -329,9 +329,10 @@ std::optional<int> WorkingFile::GetIndexPosFromBufferPos(int line, int *column,
index_lines, is_end);
}
std::string WorkingFile::FindClosestCallNameInBuffer(
lsPosition position, int *active_parameter,
lsPosition *completion_position) const {
std::string
WorkingFile::FindClosestCallNameInBuffer(Position position,
int *active_parameter,
Position *completion_position) const {
*active_parameter = 0;
int offset = GetOffsetForPosition(position, buffer_content);
@ -377,10 +378,10 @@ std::string WorkingFile::FindClosestCallNameInBuffer(
return buffer_content.substr(offset, start_offset - offset + 1);
}
lsPosition
WorkingFile::FindStableCompletionSource(lsPosition position,
Position
WorkingFile::FindStableCompletionSource(Position position,
std::string *existing_completion,
lsPosition *replace_end_pos) const {
Position *replace_end_pos) const {
int start_offset = GetOffsetForPosition(position, buffer_content);
int offset = start_offset;
@ -526,7 +527,7 @@ WorkingFiles::AsSnapshot(const std::vector<std::string> &filter_paths) {
// text documents.
// We use a UTF-8 iterator to approximate UTF-16 in the specification (weird).
// This is good enough and fails only for UTF-16 surrogate pairs.
int GetOffsetForPosition(lsPosition pos, std::string_view content) {
int GetOffsetForPosition(Position pos, std::string_view content) {
size_t i = 0;
for (; pos.line > 0 && i < content.size(); i++)
if (content[i] == '\n')
@ -542,7 +543,7 @@ int GetOffsetForPosition(lsPosition pos, std::string_view content) {
return int(i);
}
std::string_view LexIdentifierAroundPos(lsPosition position,
std::string_view LexIdentifierAroundPos(Position position,
std::string_view content) {
int start = GetOffsetForPosition(position, content);
int end = start + 1;

View File

@ -60,8 +60,8 @@ struct WorkingFile {
// |completion_position| will be point to a good code completion location to
// for fetching signatures.
std::string
FindClosestCallNameInBuffer(lsPosition position, int *active_parameter,
lsPosition *completion_position = nullptr) const;
FindClosestCallNameInBuffer(Position position, int *active_parameter,
Position *completion_position = nullptr) const;
// Returns a relatively stable completion position (it jumps back until there
// is a non-alphanumeric character).
@ -70,9 +70,9 @@ struct WorkingFile {
// global completion.
// The out param |existing_completion| is set to any existing completion
// content the user has entered.
lsPosition FindStableCompletionSource(lsPosition position,
Position FindStableCompletionSource(Position position,
std::string *existing_completion,
lsPosition *replace_end_pos) const;
Position *replace_end_pos) const;
private:
// Compute index_to_buffer and buffer_to_index.
@ -120,8 +120,8 @@ struct WorkingFiles {
std::mutex files_mutex; // Protects |files|.
};
int GetOffsetForPosition(lsPosition pos, std::string_view content);
int GetOffsetForPosition(Position pos, std::string_view content);
std::string_view LexIdentifierAroundPos(lsPosition position,
std::string_view LexIdentifierAroundPos(Position position,
std::string_view content);
} // namespace ccls