Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3e72b4e

Browse files
Merge pull request #71 from sfiera/gn
Add builtin gn formatter.
2 parents 2486dc1 + 6613abe commit 3e72b4e

File tree

6 files changed

+144
-0
lines changed

6 files changed

+144
-0
lines changed

‎autoload/codefmt.vim‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,76 @@ function! codefmt#GetYAPFFormatter() abort
445445
endfunction
446446

447447

448+
""
449+
" @private
450+
" Formatter: gn
451+
function! codefmt#GetGnFormatter() abort
452+
let l:url = 'https://www.chromium.org/developers/how-tos/install-depot-tools'
453+
let l:formatter = {
454+
\ 'name': 'gn',
455+
\ 'setup_instructions': 'install Chromium depot_tools (' . l:url . ')'}
456+
457+
function l:formatter.IsAvailable() abort
458+
return executable(s:plugin.Flag('gn_executable'))
459+
endfunction
460+
461+
function l:formatter.AppliesToBuffer() abort
462+
return &filetype is# 'gn'
463+
endfunction
464+
465+
""
466+
" Run `gn format` to format the whole file.
467+
"
468+
" We implement Format(), and not FormatRange{,s}(), because gn doesn't
469+
" provide a hook for formatting a range, and all gn files are supposed
470+
" to be fully formatted anyway.
471+
function l:formatter.Format() abort
472+
let l:executable = s:plugin.Flag('gn_executable')
473+
let l:cmd = [ l:executable, 'format', '--stdin' ]
474+
let l:input = join(getline(1, line('$')), "\n")
475+
476+
" gn itself prints errors to stdout, but if the error comes from the
477+
" gn.py wrapper script, it is printed to stderr. Use stdout as the
478+
" error message if stderr is empty.
479+
let l:result = maktaba#syscall#Create(l:cmd).WithStdin(l:input).Call(0)
480+
if !empty(l:result.stdout)
481+
let l:output = l:result.stdout
482+
else
483+
let l:output = l:result.stderr
484+
endif
485+
486+
" Other formatters generally catch failure as an exception, but
487+
" v:exception contains stderr in that case, and gn prints errors to
488+
" stdout, so we need to check for a shell error ourselves.
489+
if !v:shell_error
490+
let l:formatted = split(l:output, "\n")
491+
call maktaba#buffer#Overwrite(1, line('$'), l:formatted)
492+
else
493+
let l:errors = []
494+
for line in split(l:output, "\n")
495+
let l:tokens = matchlist(line, '\C\v^ERROR at :(\d+):(\d+):\s*(.*)')
496+
if !empty(l:tokens)
497+
call add(l:errors, {
498+
\ "filename": @%,
499+
\ "lnum": l:tokens[1],
500+
\ "col": l:tokens[2],
501+
\ "text": l:tokens[3]})
502+
endif
503+
endfor
504+
505+
if empty(l:errors)
506+
" Couldn't parse errors; display the whole error message.
507+
call maktaba#error#Shout('Error formatting file: %s', l:output)
508+
else
509+
call setqflist(l:errors, 'r')
510+
cc 1
511+
endif
512+
endif
513+
endfunction
514+
515+
return l:formatter
516+
endfunction
517+
448518
""
449519
" Checks whether {formatter} is available.
450520
" NOTE: If IsAvailable checks are disabled via

‎doc/codefmt.txt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ Default: 'js-beautify' `
5252
The path to the yapf executable.
5353
Default: 'yapf' `
5454

55+
*codefmt:gn_executable*
56+
The path to the gn executable.
57+
Default: 'gn' `
58+
5559
*codefmt:plugin[autocmds]*
5660
Configures whether plugin/autocmds.vim should be loaded.
5761
Default: 1 `
@@ -94,6 +98,7 @@ The current list of defaults by filetype is:
9498
* c, cpp, proto, javascript: clang-format
9599
* go: gofmt
96100
* python: autopep8, yapf
101+
* gn: gn
97102

98103
==============================================================================
99104
DICTIONARIES *codefmt-dicts*

‎instant/flags.vim‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,7 @@ call s:plugin.Flag('js_beautify_executable', 'js-beautify')
7575
""
7676
" The path to the yapf executable.
7777
call s:plugin.Flag('yapf_executable', 'yapf')
78+
79+
""
80+
" The path to the gn executable.
81+
call s:plugin.Flag('gn_executable', 'gn')

‎plugin/register.vim‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ call s:registry.AddExtension(codefmt#GetClangFormatFormatter())
2626
call s:registry.AddExtension(codefmt#GetGofmtFormatter())
2727
call s:registry.AddExtension(codefmt#GetYAPFFormatter())
2828
call s:registry.AddExtension(codefmt#GetAutopep8Formatter())
29+
call s:registry.AddExtension(codefmt#GetGnFormatter())

‎vroom/gn.vroom‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
The built-in gn formatter knows how to format gn build files.
2+
If you aren't familiar with basic codefmt usage yet, see main.vroom first.
3+
4+
We'll set up codefmt and configure the vroom environment, then jump into some
5+
examples.
6+
7+
:source $VROOMDIR/setupvroom.vim
8+
9+
:let g:repeat_calls = []
10+
:function FakeRepeat(...)<CR>
11+
| call add(g:repeat_calls, a:000)<CR>
12+
:endfunction
13+
:call maktaba#test#Override('repeat#set', 'FakeRepeat')
14+
15+
:call codefmt#SetWhetherToPerformIsAvailableChecksForTesting(0)
16+
17+
18+
The gn formatter expects the gn executable to be installed on your system.
19+
20+
% buildconfig = "//build/config/BUILDCONFIG.gn"
21+
:FormatCode gn
22+
! gn .*
23+
$ buildconfig = "//build/config/BUILDCONFIG.gn"
24+
25+
The name or path of the gn executable can be configured via the
26+
gn_executable flag if the default of "gn" doesn't work.
27+
28+
:Glaive codefmt gn_executable='mygn'
29+
:FormatCode gn
30+
! mygn .*
31+
$ buildconfig = "//build/config/BUILDCONFIG.gn"
32+
:Glaive codefmt gn_executable='gn'
33+
34+
35+
You can format any buffer with gn specifying the formatter explicitly.
36+
37+
@clear
38+
% executable("hello") { sources = ["hello.c"] }
39+
:FormatCode gn
40+
! gn format --stdin .*2>.*
41+
$ executable("hello") {
42+
$ sources = [
43+
$ "hello.c",
44+
$ ]
45+
$ }
46+
executable("hello") {
47+
sources = [
48+
"hello.c",
49+
]
50+
}
51+
@end
52+
53+
The gn filetype will use the gn formatter by default.
54+
55+
@clear
56+
% buildconfig = "//build/config/BUILDCONFIG.gn"
57+
58+
:set filetype=gn
59+
:FormatCode
60+
! gn .*
61+
$ buildconfig = "//build/config/BUILDCONFIG.gn"
62+
63+
:set filetype=

‎vroom/main.vroom‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ directory cover various topics of codefmt usage:
1313
* clangformat.vroom - Configuring and using the built-in clang-format formatter
1414
* gofmt.vroom - Configuring and using the built-in gofmt formatter
1515
* yapf.vroom - Configuring and using the built-in yapf formatter
16+
* gn.vroom - Configuring and using the built-in gn formatter
1617
* autocmd.vroom - Automatic hooks like format-on-save
1718
* extensions.vroom - Adding additional formatters for codefmt to use
1819

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /