]> granicus.if.org Git - clang/commitdiff
clang-format: Add -assume-filename option for editor integrations.
authorDaniel Jasper <djasper@google.com>
Fri, 13 Sep 2013 13:40:24 +0000 (13:40 +0000)
committerDaniel Jasper <djasper@google.com>
Fri, 13 Sep 2013 13:40:24 +0000 (13:40 +0000)
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.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@190691 91177308-0d34-0410-b5e6-96231b3b80d8

tools/clang-format/ClangFormat.cpp
tools/clang-format/clang-format-sublime.py
tools/clang-format/clang-format.el
tools/clang-format/clang-format.py

index 592d46a530c3363c178cfd7e174a8dcc29d60530..71dcfc5640bf864d348e77b761cf930d12bf0821 100644 (file)
@@ -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, StringRef FileName) {
     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");
index 78c8939c1b0d1f5276c565edf579c7149fba90ed..2099d7a98c6b4a7838b7c6bd5ecdd96321435ecd 100644 (file)
@@ -37,21 +37,21 @@ class ClangFormatCommand(sublime_plugin.TextCommand):
       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)
index 531635e8ecfba7011b3d2c907af0f325313e4236..520a3e250cf5ee2c35882504f20fd5aebc93beaa 100644 (file)
          (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
index d8338ab4dae2e1c23a5ac24cdeaf2a6a288d55b9..c333e4dd142a1a744a4f1937b5e66481bffa9c7a 100644 (file)
@@ -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],
                      stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                      stdin=subprocess.PIPE, startupinfo=startupinfo)
 stdout, stderr = p.communicate(input=text)