2017-12-05 07:57:41 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-04-17 01:22:59 +00:00
|
|
|
#include "atomic_object.h"
|
2017-11-11 19:41:09 +00:00
|
|
|
#include "clang_index.h"
|
|
|
|
#include "clang_translation_unit.h"
|
2017-03-26 21:40:34 +00:00
|
|
|
#include "language_server_api.h"
|
2017-12-04 02:23:14 +00:00
|
|
|
#include "lru_cache.h"
|
2017-03-26 21:40:34 +00:00
|
|
|
#include "project.h"
|
2017-05-26 06:40:38 +00:00
|
|
|
#include "threaded_queue.h"
|
2017-03-26 21:40:34 +00:00
|
|
|
#include "working_files.h"
|
|
|
|
|
|
|
|
#include <clang-c/Index.h>
|
|
|
|
|
2017-04-17 01:22:59 +00:00
|
|
|
#include <functional>
|
2017-05-26 06:40:38 +00:00
|
|
|
#include <memory>
|
2017-05-10 04:52:15 +00:00
|
|
|
#include <mutex>
|
2017-05-26 06:40:38 +00:00
|
|
|
#include <string>
|
2017-04-17 01:22:59 +00:00
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
struct CompletionSession
|
|
|
|
: public std::enable_shared_from_this<CompletionSession> {
|
2017-04-20 05:01:36 +00:00
|
|
|
Project::Entry file;
|
2017-05-10 04:52:15 +00:00
|
|
|
WorkingFiles* working_files;
|
2017-11-11 19:41:09 +00:00
|
|
|
ClangIndex index;
|
2017-05-10 04:52:15 +00:00
|
|
|
|
2017-05-26 06:40:38 +00:00
|
|
|
// When |tu| was last parsed.
|
2017-09-22 01:14:57 +00:00
|
|
|
optional<std::chrono::time_point<std::chrono::high_resolution_clock>>
|
|
|
|
tu_last_parsed_at;
|
2017-03-26 21:40:34 +00:00
|
|
|
|
2017-05-26 06:40:38 +00:00
|
|
|
// Acquired when |tu| is being used.
|
|
|
|
std::mutex tu_lock;
|
2017-03-26 21:40:34 +00:00
|
|
|
|
2017-05-26 06:40:38 +00:00
|
|
|
// The active translation unit.
|
2017-11-11 19:41:09 +00:00
|
|
|
std::unique_ptr<ClangTranslationUnit> tu;
|
2017-03-26 21:40:34 +00:00
|
|
|
|
2017-04-26 04:03:22 +00:00
|
|
|
CompletionSession(const Project::Entry& file, WorkingFiles* working_files);
|
2017-03-26 21:40:34 +00:00
|
|
|
~CompletionSession();
|
|
|
|
};
|
|
|
|
|
2017-05-27 04:21:00 +00:00
|
|
|
struct ClangCompleteManager {
|
2017-09-22 01:14:57 +00:00
|
|
|
using OnDiagnostic =
|
|
|
|
std::function<void(std::string path,
|
2017-12-12 05:20:29 +00:00
|
|
|
std::vector<lsDiagnostic> diagnostics)>;
|
2017-11-11 19:41:09 +00:00
|
|
|
using OnIndex = std::function<void(ClangTranslationUnit* tu,
|
2017-09-27 06:03:43 +00:00
|
|
|
const std::vector<CXUnsavedFile>& unsaved,
|
|
|
|
const std::string& path,
|
|
|
|
const std::vector<std::string>& args)>;
|
2017-09-22 01:14:57 +00:00
|
|
|
using OnComplete =
|
2017-12-12 05:20:29 +00:00
|
|
|
std::function<void(const std::vector<lsCompletionItem>& results,
|
2017-09-22 01:14:57 +00:00
|
|
|
bool is_cached_result)>;
|
2018-02-22 07:13:42 +00:00
|
|
|
using OnDropped = std::function<void(lsRequestId request_id)>;
|
2017-06-29 02:50:30 +00:00
|
|
|
|
2017-05-26 06:40:38 +00:00
|
|
|
struct ParseRequest {
|
|
|
|
ParseRequest(const std::string& path);
|
|
|
|
|
|
|
|
std::chrono::time_point<std::chrono::high_resolution_clock> request_time;
|
|
|
|
std::string path;
|
|
|
|
};
|
2017-04-17 01:22:59 +00:00
|
|
|
struct CompletionRequest {
|
2018-02-22 07:13:42 +00:00
|
|
|
CompletionRequest(const lsRequestId& id,
|
|
|
|
const lsTextDocumentIdentifier& document,
|
|
|
|
bool emit_diagnostics);
|
|
|
|
CompletionRequest(const lsRequestId& id,
|
|
|
|
const lsTextDocumentIdentifier& document,
|
2018-02-18 17:15:39 +00:00
|
|
|
const lsPosition& position,
|
|
|
|
const OnComplete& on_complete,
|
2018-02-22 07:13:42 +00:00
|
|
|
bool emit_diagnostics);
|
2018-02-18 17:15:39 +00:00
|
|
|
|
2018-02-22 07:13:42 +00:00
|
|
|
lsRequestId id;
|
2017-09-22 02:25:33 +00:00
|
|
|
lsTextDocumentIdentifier document;
|
|
|
|
optional<lsPosition> position;
|
|
|
|
OnComplete on_complete; // May be null/empty.
|
|
|
|
bool emit_diagnostics = false;
|
2017-04-17 01:22:59 +00:00
|
|
|
};
|
2017-05-10 04:52:15 +00:00
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
ClangCompleteManager(Config* config,
|
|
|
|
Project* project,
|
|
|
|
WorkingFiles* working_files,
|
2017-09-27 06:03:43 +00:00
|
|
|
OnDiagnostic on_diagnostic,
|
2018-02-22 07:13:42 +00:00
|
|
|
OnIndex on_index,
|
|
|
|
OnDropped on_dropped);
|
2017-06-29 02:50:30 +00:00
|
|
|
~ClangCompleteManager();
|
2017-03-26 21:40:34 +00:00
|
|
|
|
2017-04-17 01:22:59 +00:00
|
|
|
// 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-02-22 07:13:42 +00:00
|
|
|
void CodeComplete(const lsRequestId& request_id,
|
|
|
|
const lsTextDocumentPositionParams& completion_location,
|
2017-05-26 06:40:38 +00:00
|
|
|
const OnComplete& on_complete);
|
2017-09-22 02:25:33 +00:00
|
|
|
// Request a diagnostics update.
|
2018-02-22 07:13:42 +00:00
|
|
|
void DiagnosticsUpdate(const lsRequestId& request_id,
|
|
|
|
const lsTextDocumentIdentifier& document);
|
2017-05-26 06:40:38 +00:00
|
|
|
|
|
|
|
// Notify the completion manager that |filename| has been viewed and we
|
|
|
|
// should begin preloading completion data.
|
|
|
|
void NotifyView(const std::string& filename);
|
|
|
|
// Notify the completion manager that |filename| has been edited.
|
|
|
|
void NotifyEdit(const std::string& filename);
|
|
|
|
// Notify the completion manager that |filename| has been saved. This
|
|
|
|
// triggers a reparse.
|
|
|
|
void NotifySave(const std::string& filename);
|
2017-10-17 18:43:33 +00:00
|
|
|
// Notify the completion manager that |filename| has been closed. Any existing
|
|
|
|
// completion session will be dropped.
|
|
|
|
void NotifyClose(const std::string& filename);
|
2017-05-26 06:40:38 +00:00
|
|
|
|
2017-10-23 07:28:21 +00:00
|
|
|
// Ensures there is a completion or preloaded session. Returns true if a new
|
|
|
|
// session was created.
|
|
|
|
bool EnsureCompletionOrCreatePreloadSession(const std::string& filename);
|
2017-10-23 04:49:17 +00:00
|
|
|
// Tries to find an edit session for |filename|. This will move the session
|
|
|
|
// from view to edit.
|
2017-10-23 07:28:21 +00:00
|
|
|
std::shared_ptr<CompletionSession> TryGetSession(const std::string& filename,
|
|
|
|
bool mark_as_completion,
|
|
|
|
bool create_if_needed);
|
2017-05-26 06:40:38 +00:00
|
|
|
|
|
|
|
// TODO: make these configurable.
|
2017-10-23 07:28:21 +00:00
|
|
|
const int kMaxPreloadedSessions = 10;
|
|
|
|
const int kMaxCompletionSessions = 5;
|
2017-05-26 06:40:38 +00:00
|
|
|
|
|
|
|
// Global state.
|
|
|
|
Config* config_;
|
|
|
|
Project* project_;
|
|
|
|
WorkingFiles* working_files_;
|
2017-06-10 04:13:16 +00:00
|
|
|
OnDiagnostic on_diagnostic_;
|
2017-09-27 06:03:43 +00:00
|
|
|
OnIndex on_index_;
|
2018-02-22 07:13:42 +00:00
|
|
|
OnDropped on_dropped_;
|
2017-05-26 06:40:38 +00:00
|
|
|
|
2017-12-04 02:23:14 +00:00
|
|
|
using LruSessionCache = LruCache<std::string, CompletionSession>;
|
|
|
|
|
2017-10-23 07:28:21 +00:00
|
|
|
// 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_;
|
2017-05-26 06:40:38 +00:00
|
|
|
// Mutex which protects |view_sessions_| and |edit_sessions_|.
|
|
|
|
std::mutex sessions_lock_;
|
|
|
|
|
|
|
|
// Request a code completion at the given location.
|
2018-02-18 17:15:39 +00:00
|
|
|
ThreadedQueue<std::unique_ptr<CompletionRequest>> completion_request_;
|
2017-05-26 06:40:38 +00:00
|
|
|
// Parse requests. The path may already be parsed, in which case it should be
|
|
|
|
// reparsed.
|
|
|
|
ThreadedQueue<ParseRequest> parse_requests_;
|
2017-06-29 02:50:30 +00:00
|
|
|
};
|