Protect formatting code inside USE_CLANG_CXX

This commit is contained in:
Daniel Martín 2017-12-31 23:15:03 +01:00
parent 4fdc81ae6d
commit f389d294b7
6 changed files with 30 additions and 13 deletions

View File

@ -1,3 +1,5 @@
#if USE_CLANG_CXX
#include "clang_format.h"
#include <loguru.hpp>
@ -44,3 +46,5 @@ std::vector<Replacement> ClangFormat::FormatWholeDocument() {
auto format_result = reformat(*style, document_, ranges_, document_filename_);
return std::vector<Replacement>(format_result.begin(), format_result.end());
}
#endif

View File

@ -1,3 +1,7 @@
#if USE_CLANG_CXX
#pragma once
#include <clang/Format/Format.h>
#include <vector>
@ -18,3 +22,5 @@ struct ClangFormat {
std::vector<clang::tooling::Replacement> FormatWholeDocument();
};
#endif

View File

@ -104,6 +104,7 @@ optional<lsDiagnostic> 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<lsTextEdit> ConvertClangReplacementsIntoTextEdits(
}
return text_edits_result;
}
#endif
std::string FileName(CXFile file) {
CXString cx_name = clang_getFileName(file);

View File

@ -3,7 +3,9 @@
#include "language_server_api.h"
#include <clang-c/Index.h>
#if USE_CLANG_CXX
#include <clang/Format/Format.h>
#endif
#include <optional.h>
#include <vector>
@ -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<lsTextEdit> ConvertClangReplacementsIntoTextEdits(
llvm::StringRef document,
const std::vector<clang::tooling::Replacement>& clang_replacements);
#endif

View File

@ -3,6 +3,8 @@
#include "queue_manager.h"
#include "working_files.h"
#include <loguru.hpp>
namespace {
struct lsFormattingOptions {
// Size of a tab in spaces.
@ -43,6 +45,9 @@ MAKE_REFLECT_STRUCT(Out_TextDocumentFormatting, jsonrpc, id, result);
struct TextDocumentFormattingHandler
: BaseMessageHandler<Ipc_TextDocumentFormatting> {
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<ClangFormat>(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);
}

13
wscript
View File

@ -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')