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 "clang_format.h"
#include <loguru.hpp> #include <loguru.hpp>
@ -44,3 +46,5 @@ std::vector<Replacement> ClangFormat::FormatWholeDocument() {
auto format_result = reformat(*style, document_, ranges_, document_filename_); auto format_result = reformat(*style, document_, ranges_, document_filename_);
return std::vector<Replacement>(format_result.begin(), format_result.end()); 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 <clang/Format/Format.h>
#include <vector> #include <vector>
@ -18,3 +22,5 @@ struct ClangFormat {
std::vector<clang::tooling::Replacement> FormatWholeDocument(); std::vector<clang::tooling::Replacement> FormatWholeDocument();
}; };
#endif

View File

@ -104,6 +104,7 @@ optional<lsDiagnostic> BuildAndDisposeDiagnostic(CXDiagnostic diagnostic,
return ls_diagnostic; return ls_diagnostic;
} }
#if USE_CLANG_CXX
static lsPosition OffsetToRange(llvm::StringRef document, size_t offset) { static lsPosition OffsetToRange(llvm::StringRef document, size_t offset) {
// TODO: Support Windows line endings, etc. // TODO: Support Windows line endings, etc.
llvm::StringRef text_before = document.substr(0, offset); llvm::StringRef text_before = document.substr(0, offset);
@ -125,6 +126,7 @@ std::vector<lsTextEdit> ConvertClangReplacementsIntoTextEdits(
} }
return text_edits_result; return text_edits_result;
} }
#endif
std::string FileName(CXFile file) { std::string FileName(CXFile file) {
CXString cx_name = clang_getFileName(file); CXString cx_name = clang_getFileName(file);

View File

@ -3,7 +3,9 @@
#include "language_server_api.h" #include "language_server_api.h"
#include <clang-c/Index.h> #include <clang-c/Index.h>
#if USE_CLANG_CXX
#include <clang/Format/Format.h> #include <clang/Format/Format.h>
#endif
#include <optional.h> #include <optional.h>
#include <vector> #include <vector>
@ -21,6 +23,8 @@ std::string ToString(CXString cx_string);
std::string ToString(CXCursorKind cursor_kind); std::string ToString(CXCursorKind cursor_kind);
// Converts Clang formatting replacement operations into LSP text edits. // Converts Clang formatting replacement operations into LSP text edits.
#if USE_CLANG_CXX
std::vector<lsTextEdit> ConvertClangReplacementsIntoTextEdits( std::vector<lsTextEdit> ConvertClangReplacementsIntoTextEdits(
llvm::StringRef document, llvm::StringRef document,
const std::vector<clang::tooling::Replacement>& clang_replacements); const std::vector<clang::tooling::Replacement>& clang_replacements);
#endif

View File

@ -3,6 +3,8 @@
#include "queue_manager.h" #include "queue_manager.h"
#include "working_files.h" #include "working_files.h"
#include <loguru.hpp>
namespace { namespace {
struct lsFormattingOptions { struct lsFormattingOptions {
// Size of a tab in spaces. // Size of a tab in spaces.
@ -43,6 +45,9 @@ MAKE_REFLECT_STRUCT(Out_TextDocumentFormatting, jsonrpc, id, result);
struct TextDocumentFormattingHandler struct TextDocumentFormattingHandler
: BaseMessageHandler<Ipc_TextDocumentFormatting> { : BaseMessageHandler<Ipc_TextDocumentFormatting> {
void Run(Ipc_TextDocumentFormatting* request) override { void Run(Ipc_TextDocumentFormatting* request) override {
Out_TextDocumentFormatting response;
response.id = request->id;
#if USE_CLANG_CXX
QueryFile* file; QueryFile* file;
if (!FindFileOrFail(db, project, request->id, if (!FindFileOrFail(db, project, request->id,
request->params.textDocument.uri.GetPath(), &file)) { request->params.textDocument.uri.GetPath(), &file)) {
@ -55,9 +60,6 @@ struct TextDocumentFormattingHandler
int tab_size = request->params.formattingOptions.tabSize; int tab_size = request->params.formattingOptions.tabSize;
bool insert_spaces = request->params.formattingOptions.insertSpaces; bool insert_spaces = request->params.formattingOptions.insertSpaces;
Out_TextDocumentFormatting response;
response.id = request->id;
const auto clang_format = std::unique_ptr<ClangFormat>(new ClangFormat( const auto clang_format = std::unique_ptr<ClangFormat>(new ClangFormat(
working_file->filename, working_file->buffer_content, working_file->filename, working_file->buffer_content,
{clang::tooling::Range(0, working_file->buffer_content.size())}, {clang::tooling::Range(0, working_file->buffer_content.size())},
@ -65,6 +67,12 @@ struct TextDocumentFormattingHandler
const auto replacements = clang_format->FormatWholeDocument(); const auto replacements = clang_format->FormatWholeDocument();
response.result = ConvertClangReplacementsIntoTextEdits( response.result = ConvertClangReplacementsIntoTextEdits(
working_file->buffer_content, replacements); 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); 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']) cc_files += bld.path.ant_glob(['src/clang_cxx/*.cc'])
lib = [] 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'): if sys.platform.startswith('linux'):
lib.append('rt') lib.append('rt')
lib.append('pthread') lib.append('pthread')
@ -266,6 +256,9 @@ def build(bld):
lib.append('clangAST') lib.append('clangAST')
lib.append('clangLex') lib.append('clangLex')
lib.append('clangBasic') lib.append('clangBasic')
lib.append('clangFormat')
lib.append('clangToolingCore')
lib.append('clangRewrite')
# The order is derived from llvm-config --libs core # The order is derived from llvm-config --libs core
lib.append('LLVMCore') lib.append('LLVMCore')