diff --git a/src/clang_format.cc b/src/clang_format.cc index b460bb2a..659d38f7 100644 --- a/src/clang_format.cc +++ b/src/clang_format.cc @@ -1,3 +1,5 @@ +#if USE_CLANG_CXX + #include "clang_format.h" #include @@ -44,3 +46,5 @@ std::vector ClangFormat::FormatWholeDocument() { auto format_result = reformat(*style, document_, ranges_, document_filename_); return std::vector(format_result.begin(), format_result.end()); } + +#endif diff --git a/src/clang_format.h b/src/clang_format.h index f2a39569..dc7b4565 100644 --- a/src/clang_format.h +++ b/src/clang_format.h @@ -1,3 +1,7 @@ +#if USE_CLANG_CXX + +#pragma once + #include #include @@ -18,3 +22,5 @@ struct ClangFormat { std::vector FormatWholeDocument(); }; + +#endif diff --git a/src/clang_utils.cc b/src/clang_utils.cc index 60ee39da..278e6d22 100644 --- a/src/clang_utils.cc +++ b/src/clang_utils.cc @@ -104,6 +104,7 @@ optional BuildAndDisposeDiagnostic(CXDiagnostic diagnostic, return ls_diagnostic; } +#if USE_CLANG_CXX static lsPosition OffsetToRange(llvm::StringRef document, size_t offset) { // TODO: Support Windows line endings, etc. llvm::StringRef text_before = document.substr(0, offset); @@ -125,6 +126,7 @@ std::vector ConvertClangReplacementsIntoTextEdits( } return text_edits_result; } +#endif std::string FileName(CXFile file) { CXString cx_name = clang_getFileName(file); diff --git a/src/clang_utils.h b/src/clang_utils.h index 0922643a..9932d2eb 100644 --- a/src/clang_utils.h +++ b/src/clang_utils.h @@ -3,7 +3,9 @@ #include "language_server_api.h" #include +#if USE_CLANG_CXX #include +#endif #include #include @@ -21,6 +23,8 @@ std::string ToString(CXString cx_string); std::string ToString(CXCursorKind cursor_kind); // Converts Clang formatting replacement operations into LSP text edits. +#if USE_CLANG_CXX std::vector ConvertClangReplacementsIntoTextEdits( llvm::StringRef document, const std::vector& clang_replacements); +#endif diff --git a/src/messages/text_document_formatting.cc b/src/messages/text_document_formatting.cc index e8e0e5c5..8b114cf3 100644 --- a/src/messages/text_document_formatting.cc +++ b/src/messages/text_document_formatting.cc @@ -3,6 +3,8 @@ #include "queue_manager.h" #include "working_files.h" +#include + namespace { struct lsFormattingOptions { // Size of a tab in spaces. @@ -43,6 +45,9 @@ MAKE_REFLECT_STRUCT(Out_TextDocumentFormatting, jsonrpc, id, result); struct TextDocumentFormattingHandler : BaseMessageHandler { void Run(Ipc_TextDocumentFormatting* request) override { + Out_TextDocumentFormatting response; + response.id = request->id; +#if USE_CLANG_CXX QueryFile* file; if (!FindFileOrFail(db, project, request->id, request->params.textDocument.uri.GetPath(), &file)) { @@ -55,9 +60,6 @@ struct TextDocumentFormattingHandler int tab_size = request->params.formattingOptions.tabSize; bool insert_spaces = request->params.formattingOptions.insertSpaces; - Out_TextDocumentFormatting response; - response.id = request->id; - const auto clang_format = std::unique_ptr(new ClangFormat( working_file->filename, working_file->buffer_content, {clang::tooling::Range(0, working_file->buffer_content.size())}, @@ -65,6 +67,12 @@ struct TextDocumentFormattingHandler const auto replacements = clang_format->FormatWholeDocument(); response.result = ConvertClangReplacementsIntoTextEdits( working_file->buffer_content, replacements); +#else + LOG_S(WARNING) << "You must compile cquery with --use-clang-cxx to use " + "document formatting."; + // TODO: Fallback to execute the clang-format binary? + response.result = {}; +#endif QueueManager::WriteStdout(IpcId::TextDocumentFormatting, response); } diff --git a/wscript b/wscript index 44ed66e3..23ff2852 100644 --- a/wscript +++ b/wscript @@ -234,16 +234,6 @@ def build(bld): cc_files += bld.path.ant_glob(['src/clang_cxx/*.cc']) lib = [] - # clang-format support - lib.append('clangBasic') - lib.append('clangFormat') - lib.append('clangLex') - lib.append('clangRewrite') - lib.append('clangToolingCore') - lib.append('LLVMCore') - lib.append('LLVMDemangle') - lib.append('LLVMSupport') - lib.append('ncurses') if sys.platform.startswith('linux'): lib.append('rt') lib.append('pthread') @@ -266,6 +256,9 @@ def build(bld): lib.append('clangAST') lib.append('clangLex') lib.append('clangBasic') + lib.append('clangFormat') + lib.append('clangToolingCore') + lib.append('clangRewrite') # The order is derived from llvm-config --libs core lib.append('LLVMCore')