ioeric created this revision. ioeric added reviewers: bkramer, hokein. ioeric added a subscriber: cfe-commits.
[clang-include-fixer] Added Vim integration for clang-include-fixer. http://reviews.llvm.org/D20329 Files: include-fixer/tool/ClangIncludeFixer.cpp include-fixer/tool/clang-include-fixer.py
Index: include-fixer/tool/clang-include-fixer.py =================================================================== --- /dev/null +++ include-fixer/tool/clang-include-fixer.py @@ -0,0 +1,51 @@ +# This file is a minimal clang-include-fixer vim-integration. To install: +# - Change 'binary' if clang-include-fixer is not on the path (see below). +# - Add to your .vimrc: +# +# map ,cf :pyf path/to/llvm/source/tools/clang/tools/extra/include-fixer/tool/clang-include-fixer.py<cr> +# +# This enables clang-include-fixer for NORMAL and VISUAL mode. +# +# To set up clang-include-fixer, see http://clang.llvm.org/extra/include-fixer.html +# +# With this integration you can press the bound key and clang-include-fixer will +# be run on the current buffer. +# +# It operates on the current, potentially unsaved buffer and does not create +# or save any files. To revert a fix, just undo. +# +# FIXME: in the current version, each invocation only fixes #include for one +# unidentified symbol, and it simply inserts #include directive to the first +# line. + +import subprocess +import sys +import vim + +# set g:clang_include_fixer_path to the path to clang-include-fixer if it is not +# on the path. +# Change this to the full path if clang-include-fixer is not on the path. +binary = 'clang-include-fixer' +if vim.eval('exists("g:clang_include_fixer_path")') == "1": + binary = vim.eval('g:clang_include_fixer_path') + +def main(): + # Get the current text. + buf = vim.current.buffer + text = '\n'.join(buf) + + # Call clang-include-fixer. + command = [binary, "-db=yaml", "-vim", vim.current.buffer.name] + p = subprocess.Popen(command, + stdout=subprocess.PIPE, stderr=subprocess.PIPE, + stdin=subprocess.PIPE) + stdout, stderr = p.communicate(input=text) + + # If successful, replace buffer contents. + if stderr: + print stderr + + if stdout: + vim.current.buffer[0:0] = [stdout] + +main() Index: include-fixer/tool/ClangIncludeFixer.cpp =================================================================== --- include-fixer/tool/ClangIncludeFixer.cpp +++ include-fixer/tool/ClangIncludeFixer.cpp @@ -47,11 +47,36 @@ cl::opt<bool> Quiet("q", cl::desc("Reduce terminal output"), cl::init(false), cl::cat(IncludeFixerCategory)); +cl::opt<bool> + VimMode("vim", + cl::desc("Run the tool on a potentially unsaved buffer from Vim"), + cl::init(false), cl::cat(IncludeFixerCategory)); + int includeFixerMain(int argc, const char **argv) { tooling::CommonOptionsParser options(argc, argv, IncludeFixerCategory); tooling::ClangTool tool(options.getCompilations(), options.getSourcePathList()); + // In VimMode, we override the file content with the vim buffer from <stdin>. + // Since `tool.mapVirtualFile` takes `StringRef`, we define `Code` outside of + // the if-block so that `Code` is not released after the if-block. + std::unique_ptr<llvm::MemoryBuffer> Code; + if (VimMode) { + assert(options.getSourcePathList().size() == 1 && + "Expect exactly one file path from Vim."); + llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> CodeOrErr = + MemoryBuffer::getSTDIN(); + if (std::error_code EC = CodeOrErr.getError()) { + errs() << EC.message() << "\n"; + return 1; + } + Code = std::move(CodeOrErr.get()); + if (Code->getBufferSize() == 0) + return 0; // Skip empty files. + + tool.mapVirtualFile(options.getSourcePathList().front(), Code->getBuffer()); + } + // Set up data source. auto SymbolIndexMgr = llvm::make_unique<include_fixer::SymbolIndexManager>(); switch (DatabaseFormat) { @@ -114,6 +139,12 @@ for (const tooling::Replacement &Replacement : Replacements) llvm::errs() << "Added " << Replacement.getReplacementText(); + if (VimMode) { + for (const tooling::Replacement &Replacement : Replacements) + llvm::outs() << Replacement.getReplacementText(); + return 0; + } + // Set up a new source manager for applying the resulting replacements. IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions); DiagnosticsEngine Diagnostics(new DiagnosticIDs, &*DiagOpts);
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits