OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 # | 4 # |
5 # Based on clang-format.py. | 5 # Based on clang-format.py. |
6 # | 6 # |
7 # This file is a minimal gn format vim-integration. To install: | 7 # This file is a minimal gn format vim-integration. To install: |
8 # - Change 'binary' if gn is not on the path (see below). | 8 # - Change 'binary' if gn is not on the path (see below). |
9 # - Add to your .vimrc: | 9 # - Add to your .vimrc: |
10 # | 10 # |
(...skipping 20 matching lines...) Expand all Loading... |
31 startupinfo = None | 31 startupinfo = None |
32 if sys.platform.startswith('win32'): | 32 if sys.platform.startswith('win32'): |
33 startupinfo = subprocess.STARTUPINFO() | 33 startupinfo = subprocess.STARTUPINFO() |
34 startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW | 34 startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW |
35 startupinfo.wShowWindow = subprocess.SW_HIDE | 35 startupinfo.wShowWindow = subprocess.SW_HIDE |
36 | 36 |
37 # Call formatter. | 37 # Call formatter. |
38 p = subprocess.Popen([binary, 'format', '--stdin'], | 38 p = subprocess.Popen([binary, 'format', '--stdin'], |
39 stdout=subprocess.PIPE, stderr=subprocess.PIPE, | 39 stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
40 stdin=subprocess.PIPE, startupinfo=startupinfo, | 40 stdin=subprocess.PIPE, startupinfo=startupinfo, |
41 universal_newlines=True) | 41 shell=True, universal_newlines=True) |
42 stdout, stderr = p.communicate(input=text) | 42 stdout, stderr = p.communicate(input=text) |
43 if p.returncode != 0: | 43 if p.returncode != 0: |
44 print 'Formatting failed, please report to gn-dev@chromium.org.' | 44 print 'Formatting failed, please report to gn-dev@chromium.org.' |
45 print stdout, stderr | 45 print stdout, stderr |
46 else: | 46 else: |
47 # Otherwise, replace current buffer. | 47 # Otherwise, replace current buffer. |
48 lines = stdout.split('\n') | 48 lines = stdout.split('\n') |
49 # Last line should have trailing \n, but we don't want to insert a blank | 49 # Last line should have trailing \n, but we don't want to insert a blank |
50 # line at the end of the buffer, so remove that. | 50 # line at the end of the buffer, so remove that. |
51 if lines[-1] == '': | 51 if lines[-1] == '': |
52 lines = lines[:-1] | 52 lines = lines[:-1] |
53 sequence = difflib.SequenceMatcher(None, vim.current.buffer, lines) | 53 sequence = difflib.SequenceMatcher(None, vim.current.buffer, lines) |
54 for op in reversed(sequence.get_opcodes()): | 54 for op in reversed(sequence.get_opcodes()): |
55 if op[0] is not 'equal': | 55 if op[0] is not 'equal': |
56 vim.current.buffer[op[1]:op[2]] = lines[op[3]:op[4]] | 56 vim.current.buffer[op[1]:op[2]] = lines[op[3]:op[4]] |
57 | 57 |
58 main() | 58 main() |
OLD | NEW |