Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
490 changes: 45 additions & 445 deletions src/server/compiler/compiler.cpp

Large diffs are not rendered by default.

68 changes: 30 additions & 38 deletions src/server/compiler/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
#include <vector>

#include "command/command.h"
#include "server/service/session.h"
#include "server/session/session.h"
#include "server/worker/worker_pool.h"
#include "server/workspace/workspace.h"
#include "support/signal.h"
#include "syntax/completion.h"

#include "kota/async/async.h"
#include "kota/codec/json/json.h"
#include "kota/ipc/codec/json.h"
#include "kota/ipc/lsp/protocol.h"
#include "kota/ipc/peer.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
Expand All @@ -26,13 +26,15 @@ namespace clice {

namespace protocol = kota::ipc::protocol;

class ContextResolver;

/// Convert a file:// URI to a local file path.
std::string uri_to_path(const std::string& uri);

/// Where the compile command for a file came from. Anything other than
/// CDBExact means the command was guessed to some degree, which is why
/// diagnostics produced with it may deserve a guidance note (see
/// Compiler::publish_diagnostics).
/// format_diagnostics).
enum class CommandSource : std::uint8_t {
/// Direct compilation database entry for the file.
CDBExact,
Expand Down Expand Up @@ -61,15 +63,14 @@ enum class CommandSource : std::uint8_t {
///
/// NOT responsible for:
/// - Document lifecycle (didOpen/didChange/didClose) — handled by MasterServer
/// - Index queries — handled by Indexer
/// - Background indexing scheduling — handled by Indexer
/// - Index queries — handled by IndexQuery
/// - Background indexing scheduling — handled by BackgroundIndexer
class Compiler {
public:
Compiler(kota::event_loop& loop, Workspace& workspace, WorkerPool& pool);

void set_peer(kota::ipc::JsonPeer* p) {
peer = p;
}
Compiler(kota::event_loop& loop,
Workspace& workspace,
ContextResolver& contexts,
WorkerPool& pool);

~Compiler();

Expand Down Expand Up @@ -121,8 +122,11 @@ class Compiler {
RawResult handle_completion(const protocol::Position& position,
std::shared_ptr<Session> session);

/// Send an empty diagnostics notification to clear stale markers in the editor.
void clear_diagnostics(const std::string& uri);
/// Emitted after a compile round materializes its publishable products
/// into the session's `output` field (both success and the failure/
/// clear path). Subscribers read the output from the session; with no
/// subscriber connected the output simply stays there.
Signal<std::shared_ptr<Session>> on_output;

/// Callback invoked when indexing should be scheduled.
std::function<void()> on_indexing_needed;
Expand All @@ -149,38 +153,26 @@ class Compiler {
bool is_stale(const Session& session);
void record_deps(Session& session, llvm::ArrayRef<std::string> deps);

/// Deserialize worker-produced diagnostics and publish them. When the
/// compile command was not an exact CDB match and the diagnostics contain
/// file-not-found class errors, a file-top guidance diagnostic explaining
/// the inferred command is merged into the same publish.
static void append_suffix_include(const Session& session, std::string& text);

void publish_inactive_regions(const std::string& uri,
const Session& session,
llvm::ArrayRef<std::uint32_t> regions);

void publish_diagnostics(const std::string& uri,
int version,
const kota::codec::RawValue& diags,
CommandSource source,
std::optional<std::uint32_t> line_limit = std::nullopt);

std::optional<HeaderContext> resolve_header_context(std::uint32_t header_path_id,
Session* session,
bool synthesize);

bool fill_header_context_args(llvm::StringRef path,
std::uint32_t path_id,
std::string& directory,
std::vector<std::string>& arguments,
Session* session);

private:
kota::event_loop& loop;
kota::ipc::JsonPeer* peer = nullptr;
Workspace& workspace;
ContextResolver& contexts;
WorkerPool& pool;
kota::task_group<> compile_tasks{loop};
};

/// Format a materialized compile output into publishable diagnostics:
/// deserialize the worker's raw diagnostics, drop phantom suffix-include
/// lines, and — when the compile command was not an exact CDB match and
/// the diagnostics contain file-not-found class errors — merge a file-top
/// guidance diagnostic explaining the inferred command. Formatting is
/// compile semantics; sending the result is the transport's job.
std::vector<protocol::Diagnostic> format_diagnostics(const CompileOutput& output);

/// Convert a compile output's inactive-region byte offsets into LSP ranges
/// using the session's line map.
std::vector<protocol::Range> format_inactive_regions(const Session& session,
const CompileOutput& output);

} // namespace clice
Loading