r190691 - clang-format: Add -assume-filename option for editor integrations.
Nico Weber
thakis at chromium.org
Fri Oct 25 13:04:33 PDT 2013
On Fri, Sep 13, 2013 at 6:40 AM, Daniel Jasper <djasper at google.com> wrote:
> Author: djasper
> Date: Fri Sep 13 08:40:24 2013
> New Revision: 190691
>
> URL: http://llvm.org/viewvc/llvm-project?rev=190691&view=rev
> Log:
> clang-format: Add -assume-filename option for editor integrations.
>
> With -style=file, clang-format now starts to search for a .clang-format
> file starting at the file given with -assume-filename if it reads from
> stdin. Otherwise, it would start searching from the current directory,
> which is not helpful for editor integrations.
>
> Also changed vim, emacs and sublime integrations to actually make use of
> this flag.
>
> This fixes llvm.org/PR17072.
>
> Modified:
> cfe/trunk/tools/clang-format/ClangFormat.cpp
> cfe/trunk/tools/clang-format/clang-format-sublime.py
> cfe/trunk/tools/clang-format/clang-format.el
> cfe/trunk/tools/clang-format/clang-format.py
>
> Modified: cfe/trunk/tools/clang-format/ClangFormat.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-format/ClangFormat.cpp?rev=190691&r1=190690&r2=190691&view=diff
>
> ==============================================================================
> --- cfe/trunk/tools/clang-format/ClangFormat.cpp (original)
> +++ cfe/trunk/tools/clang-format/ClangFormat.cpp Fri Sep 13 08:40:24 2013
> @@ -73,6 +73,14 @@ static cl::opt<std::string>
> "parameters, e.g.:\n"
> " -style=\"{BasedOnStyle: llvm, IndentWidth: 8}\""),
> cl::init("file"), cl::cat(ClangFormatCategory));
> +
> +static cl::opt<std::string>
> +AssumeFilename("assume-filename",
> + cl::desc("When reading from stdin, clang-format assumes
> this\n"
> + "filename to look for a style config file (with\n"
> + "-style=file)."),
> + cl::cat(ClangFormatCategory));
> +
> static cl::opt<bool> Inplace("i",
> cl::desc("Inplace edit <file>s, if
> specified."),
> cl::cat(ClangFormatCategory));
> @@ -126,11 +134,15 @@ FormatStyle getStyle(StringRef StyleName
> return Style;
> }
>
> + if (FileName == "-")
> + FileName = AssumeFilename;
> SmallString<128> Path(FileName);
> llvm::sys::fs::make_absolute(Path);
> - for (StringRef Directory = llvm::sys::path::parent_path(Path);
> + for (StringRef Directory = Path;
> !Directory.empty();
> Directory = llvm::sys::path::parent_path(Directory)) {
> + if (!llvm::sys::fs::is_directory(Directory))
> + continue;
> SmallString<128> ConfigFile(Directory);
>
> llvm::sys::path::append(ConfigFile, ".clang-format");
>
> Modified: cfe/trunk/tools/clang-format/clang-format-sublime.py
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-format/clang-format-sublime.py?rev=190691&r1=190690&r2=190691&view=diff
>
> ==============================================================================
> --- cfe/trunk/tools/clang-format/clang-format-sublime.py (original)
> +++ cfe/trunk/tools/clang-format/clang-format-sublime.py Fri Sep 13
> 08:40:24 2013
> @@ -37,21 +37,21 @@ class ClangFormatCommand(sublime_plugin.
> region_offset = min(region.a, region.b)
> region_length = abs(region.b - region.a)
> command.extend(['-offset', str(region_offset),
> - '-length', str(region_length)])
> + '-length', str(region_length),
> + '-assume-filename', str(self.view.file_name())])
> old_viewport_position = self.view.viewport_position()
> buf = self.view.substr(sublime.Region(0, self.view.size()))
> p = subprocess.Popen(command, stdout=subprocess.PIPE,
> stderr=subprocess.PIPE, stdin=subprocess.PIPE)
> output, error = p.communicate(buf.encode(encoding))
> - if not error:
> - self.view.replace(
> - edit, sublime.Region(0, self.view.size()),
> - output.decode(encoding))
> - self.view.sel().clear()
> - for region in regions:
> - self.view.sel().add(region)
> - # FIXME: Without the 10ms delay, the viewport sometimes jumps.
> - sublime.set_timeout(lambda: self.view.set_viewport_position(
> - old_viewport_position, False), 10)
> - else:
> + if error:
> print error
> + self.view.replace(
> + edit, sublime.Region(0, self.view.size()),
> + output.decode(encoding))
> + self.view.sel().clear()
> + for region in regions:
> + self.view.sel().add(region)
> + # FIXME: Without the 10ms delay, the viewport sometimes jumps.
> + sublime.set_timeout(lambda: self.view.set_viewport_position(
> + old_viewport_position, False), 10)
>
> Modified: cfe/trunk/tools/clang-format/clang-format.el
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-format/clang-format.el?rev=190691&r1=190690&r2=190691&view=diff
>
> ==============================================================================
> --- cfe/trunk/tools/clang-format/clang-format.el (original)
> +++ cfe/trunk/tools/clang-format/clang-format.el Fri Sep 13 08:40:24 2013
> @@ -38,10 +38,12 @@
> (orig-point (point))
> (style "file"))
> (unwind-protect
> - (call-process-region (point-min) (point-max) clang-format-binary
> t t nil
> + (call-process-region (point-min) (point-max) clang-format-binary
> + t (list t nil) nil
> "-offset" (number-to-string (1- begin))
> "-length" (number-to-string (- end begin))
> "-cursor" (number-to-string (1- (point)))
> + "-assume-filename" (buffer-file-name)
> "-style" style)
> (goto-char (point-min))
> (let ((json-output (json-read-from-string
>
> Modified: cfe/trunk/tools/clang-format/clang-format.py
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-format/clang-format.py?rev=190691&r1=190690&r2=190691&view=diff
>
> ==============================================================================
> --- cfe/trunk/tools/clang-format/clang-format.py (original)
> +++ cfe/trunk/tools/clang-format/clang-format.py Fri Sep 13 08:40:24 2013
> @@ -49,7 +49,8 @@ if sys.platform.startswith('win32'):
>
> # Call formatter.
> p = subprocess.Popen([binary, '-lines', lines, '-style', style,
> - '-cursor', str(cursor)],
> + '-cursor', str(cursor),
> + '-assume-filename', vim.current.buffer.name],
>
This broke formatting unnamed buffers in vim. I sometimes open a new tab,
paste in some code, and format that to see what clang-format does. This
used to work, now I get something about "execv() arg 2 must contain only
strings", probably due to vim.current.buffer.name being None. This patch
fixes it for me, is it ok to land this?
Index: tools/clang-format/clang-format.py
===================================================================
--- tools/clang-format/clang-format.py (revision 193427)
+++ tools/clang-format/clang-format.py (working copy)
@@ -48,9 +48,10 @@
startupinfo.wShowWindow = subprocess.SW_HIDE
# Call formatter.
-p = subprocess.Popen([binary, '-lines', lines, '-style', style,
- '-cursor', str(cursor),
- '-assume-filename', vim.current.buffer.name],
+command = [binary, '-lines', lines, '-style', style, '-cursor',
str(cursor)]
+if vim.current.buffer.name:
+ command.append(['-assume-filename', vim.current.buffer.name])
+p = subprocess.Popen(command,
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
stdin=subprocess.PIPE, startupinfo=startupinfo)
stdout, stderr = p.communicate(input=text)
> stdout=subprocess.PIPE, stderr=subprocess.PIPE,
> stdin=subprocess.PIPE, startupinfo=startupinfo)
> stdout, stderr = p.communicate(input=text)
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20131025/4e2a4556/attachment.html>
More information about the cfe-commits
mailing list