ccls/src/clang_complete.h

149 lines
5.4 KiB
C
Raw Normal View History

#pragma once
2018-05-28 00:50:02 +00:00
#include "clang_tu.h"
2018-03-20 02:51:42 +00:00
#include "lru_cache.h"
#include "lsp_completion.h"
#include "lsp_diagnostic.h"
#include "project.h"
#include "threaded_queue.h"
#include "working_files.h"
#include <functional>
#include <memory>
#include <mutex>
#include <string>
2017-09-22 01:14:57 +00:00
struct CompletionSession
: public std::enable_shared_from_this<CompletionSession> {
2018-04-14 16:52:17 +00:00
// Translation unit for clang.
struct Tu {
// When |tu| was last parsed.
std::optional<std::chrono::time_point<std::chrono::high_resolution_clock>>
last_parsed_at;
// Acquired when |tu| is being used.
std::mutex lock;
std::unique_ptr<ClangTranslationUnit> tu;
};
2017-04-20 05:01:36 +00:00
Project::Entry file;
2018-08-09 17:08:14 +00:00
WorkingFiles *working_files;
2018-04-14 16:52:17 +00:00
Tu completion;
Tu diagnostics;
2018-08-09 17:08:14 +00:00
CompletionSession(const Project::Entry &file, WorkingFiles *wfiles)
2018-04-14 16:52:17 +00:00
: file(file), working_files(wfiles) {}
};
struct ClangCompleteManager {
2018-08-09 17:08:14 +00:00
using OnDiagnostic = std::function<void(
std::string path, std::vector<lsDiagnostic> diagnostics)>;
using OnComplete = std::function<void(
const std::vector<lsCompletionItem> &results, bool is_cached_result)>;
using OnDropped = std::function<void(lsRequestId request_id)>;
2018-04-14 16:52:17 +00:00
struct PreloadRequest {
2018-08-09 17:08:14 +00:00
PreloadRequest(const std::string &path)
2018-04-14 16:52:17 +00:00
: request_time(std::chrono::high_resolution_clock::now()), path(path) {}
std::chrono::time_point<std::chrono::high_resolution_clock> request_time;
std::string path;
};
struct CompletionRequest {
2018-08-09 17:08:14 +00:00
CompletionRequest(const lsRequestId &id,
const lsTextDocumentIdentifier &document,
const lsPosition &position, const OnComplete &on_complete)
: id(id), document(document), position(position),
2018-04-14 16:52:17 +00:00
on_complete(on_complete) {}
lsRequestId id;
lsTextDocumentIdentifier document;
2018-04-14 16:52:17 +00:00
lsPosition position;
OnComplete on_complete;
};
struct DiagnosticRequest {
lsTextDocumentIdentifier document;
};
2018-08-09 17:08:14 +00:00
ClangCompleteManager(Project *project, WorkingFiles *working_files,
OnDiagnostic on_diagnostic, OnDropped on_dropped);
// Start a code completion at the given location. |on_complete| will run when
// completion results are available. |on_complete| may run on any thread.
2018-08-09 17:08:14 +00:00
void CodeComplete(const lsRequestId &request_id,
const lsTextDocumentPositionParams &completion_location,
const OnComplete &on_complete);
// Request a diagnostics update.
2018-08-09 17:08:14 +00:00
void DiagnosticsUpdate(const lsTextDocumentIdentifier &document);
// Notify the completion manager that |filename| has been viewed and we
// should begin preloading completion data.
2018-08-09 17:08:14 +00:00
void NotifyView(const std::string &filename);
// Notify the completion manager that |filename| has been edited.
2018-08-09 17:08:14 +00:00
void NotifyEdit(const std::string &filename);
// Notify the completion manager that |filename| has been saved. This
// triggers a reparse.
2018-08-09 17:08:14 +00:00
void NotifySave(const std::string &filename);
// Notify the completion manager that |filename| has been closed. Any existing
// completion session will be dropped.
2018-08-09 17:08:14 +00:00
void NotifyClose(const std::string &filename);
// Ensures there is a completion or preloaded session. Returns true if a new
// session was created.
2018-08-09 17:08:14 +00:00
bool EnsureCompletionOrCreatePreloadSession(const std::string &filename);
// Tries to find an edit session for |filename|. This will move the session
// from view to edit.
2018-08-09 17:08:14 +00:00
std::shared_ptr<CompletionSession> TryGetSession(const std::string &filename,
bool mark_as_completion,
bool create_if_needed);
// Flushes all saved sessions with the supplied filename
2018-08-09 17:08:14 +00:00
void FlushSession(const std::string &filename);
// Flushes all saved sessions
void FlushAllSessions(void);
// TODO: make these configurable.
const int kMaxPreloadedSessions = 10;
const int kMaxCompletionSessions = 5;
// Global state.
2018-08-09 17:08:14 +00:00
Project *project_;
WorkingFiles *working_files_;
OnDiagnostic on_diagnostic_;
OnDropped on_dropped_;
using LruSessionCache = LruCache<std::string, CompletionSession>;
// CompletionSession instances which are preloaded, ie, files which the user
// has viewed but not requested code completion for.
LruSessionCache preloaded_sessions_;
// CompletionSession instances which the user has actually performed
// completion on. This is more rare so these instances tend to stay alive
// much longer than the ones in |preloaded_sessions_|.
LruSessionCache completion_sessions_;
// Mutex which protects |view_sessions_| and |edit_sessions_|.
std::mutex sessions_lock_;
// Request a code completion at the given location.
ThreadedQueue<std::unique_ptr<CompletionRequest>> completion_request_;
2018-04-14 16:52:17 +00:00
ThreadedQueue<DiagnosticRequest> diagnostic_request_;
// Parse requests. The path may already be parsed, in which case it should be
// reparsed.
2018-04-14 16:52:17 +00:00
ThreadedQueue<PreloadRequest> preload_requests_;
};
2018-03-31 08:01:32 +00:00
// Cached completion information, so we can give fast completion results when
// the user erases a character. vscode will resend the completion request if
// that happens.
struct CodeCompleteCache {
// NOTE: Make sure to access these variables under |WithLock|.
std::optional<std::string> cached_path_;
std::optional<lsPosition> cached_completion_position_;
std::vector<lsCompletionItem> cached_results_;
std::mutex mutex_;
void WithLock(std::function<void()> action);
bool IsCacheValid(lsTextDocumentPositionParams position);
};