ccls/src/working_files.h

106 lines
4.0 KiB
C
Raw Normal View History

#pragma once
#include "language_server_api.h"
#include "utils.h"
#include <clang-c/Index.h>
#include <optional.h>
#include <mutex>
#include <string>
using std::experimental::nullopt;
2017-09-22 01:14:57 +00:00
using std::experimental::optional;
struct WorkingFile {
int version = 0;
std::string filename;
std::string buffer_content;
// Note: This assumes 0-based lines (1-based lines are normally assumed).
std::vector<std::string> index_lines;
// Note: This assumes 0-based lines (1-based lines are normally assumed).
std::vector<std::string> all_buffer_lines;
// This map goes from disk-line -> indicies+1 in index_lines.
// Note: The items in the value entry are 1-based lines.
std::unordered_map<std::string, std::vector<int>> index_lines_lookup;
// This map goes from buffer-line -> indices+1 in all_buffer_lines.
// Note: The items in the value entry are 1-based liness.
std::unordered_map<std::string, std::vector<int>> all_buffer_lines_lookup;
// A set of diagnostics that have been reported for this file.
2017-09-22 01:14:57 +00:00
// NOTE: _ is appended because it must be accessed under the WorkingFiles
// lock!
std::vector<lsDiagnostic> diagnostics_;
WorkingFile(const std::string& filename, const std::string& buffer_content);
// This should be called when the indexed content has changed.
void SetIndexContent(const std::string& index_content);
// This should be called whenever |buffer_content| has changed.
void OnBufferContentUpdated();
// Find the buffer-line which should be shown for |indexed_line|. This
// accepts and returns 1-based lines.
optional<int> GetBufferLineFromIndexLine(int indexed_line) const;
// Find the indexed-line which should be shown for |buffer_line|. This
// accepts and returns 1-based lines.
optional<int> GetIndexLineFromBufferLine(int buffer_line) const;
2017-09-22 01:14:57 +00:00
optional<std::string> GetBufferLineContentFromIndexLine(
int indexed_line,
optional<int>* out_buffer_line) const;
2017-05-21 04:30:59 +00:00
2017-09-22 01:14:57 +00:00
// TODO: Move FindClosestCallNameInBuffer and FindStableCompletionSource into
// lex_utils.h/cc
2017-06-15 05:32:23 +00:00
// Finds the closest 'callable' name prior to position. This is used for
// signature help to filter code completion results.
//
// |completion_position| will be point to a good code completion location to
// for fetching signatures.
2017-09-22 01:14:57 +00:00
std::string FindClosestCallNameInBuffer(
lsPosition position,
int* active_parameter,
lsPosition* completion_position = nullptr) const;
// Returns a relatively stable completion position (it jumps back until there
// is a non-alphanumeric character).
2017-05-26 07:10:55 +00:00
//
// The out param |is_global_completion| is set to true if this looks like a
// global completion.
// The out param |existing_completion| is set to any existing completion
// content the user has entered.
2017-09-22 01:14:57 +00:00
lsPosition FindStableCompletionSource(lsPosition position,
bool* is_global_completion,
std::string* existing_completion) const;
CXUnsavedFile AsUnsavedFile() const;
};
struct WorkingFiles {
//
// :: IMPORTANT :: All methods in this class are guarded by a single lock.
//
// Find the file with the given filename.
WorkingFile* GetFileByFilename(const std::string& filename);
WorkingFile* GetFileByFilenameNoLock(const std::string& filename);
// Run |action| under the lock.
void DoAction(const std::function<void()>& action);
2017-09-22 01:14:57 +00:00
// Run |action| on the file identified by |filename|. This executes under the
// lock.
void DoActionOnFile(const std::string& filename,
const std::function<void(WorkingFile* file)>& action);
2017-12-06 04:39:44 +00:00
WorkingFile* OnOpen(const lsTextDocumentItem& open);
void OnChange(const lsTextDocumentDidChangeParams& change);
void OnClose(const lsTextDocumentItem& close);
std::vector<CXUnsavedFile> AsUnsavedFiles();
// Use unique_ptrs so we can handout WorkingFile ptrs and not have them
// invalidated if we resize files.
std::vector<std::unique_ptr<WorkingFile>> files;
2017-09-22 01:14:57 +00:00
std::mutex files_mutex; // Protects |files|.
};