Add ftplugin for gn filetype

The 'includeexpr' setting (and corresponding autoload function) allows
users to jump from one gn file to another with the 'gf' command (as long
as vim's cwd is set to /src/).

The commentstring setting lets vim know what the correct comments are
for gn files (this integrates nicely with plugins like vim-commentary).

BUG=

Review-Url: https://codereview.chromium.org/2654673002
Cr-Original-Commit-Position: refs/heads/master@{#445782}
Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src
Cr-Mirrored-Commit: 23449b77810518956542fb0b65be9da863e8492a
diff --git a/tools/gn/misc/vim/autoload/gn.vim b/tools/gn/misc/vim/autoload/gn.vim
new file mode 100644
index 0000000..c1df012
--- /dev/null
+++ b/tools/gn/misc/vim/autoload/gn.vim
@@ -0,0 +1,17 @@
+" Copyright 2017 The Chromium Authors. All rights reserved.
+" Use of this source code is governed by a BSD-style license that can be
+" found in the LICENSE file.
+
+function! gn#TranslateToBuildFile(name) abort
+  " Strip '//' prefix
+  let l:new_path = substitute(a:name, '\v^//', '', '')
+
+  " Strip the build target name (necessary if 'isfname' contains ':')
+  let l:new_path = substitute(l:new_path, '\v:.*$', '', '')
+
+  " Append 'BUILD.gn', only if this is a directory and not a file
+  if isdirectory(l:new_path)
+    let l:new_path = substitute(l:new_path, '\v/?$', '/BUILD.gn', '')
+  endif
+  return l:new_path
+endfunction
diff --git a/tools/gn/misc/vim/ftplugin/gn.vim b/tools/gn/misc/vim/ftplugin/gn.vim
new file mode 100644
index 0000000..ede251d
--- /dev/null
+++ b/tools/gn/misc/vim/ftplugin/gn.vim
@@ -0,0 +1,12 @@
+" Copyright 2017 The Chromium Authors. All rights reserved.
+" Use of this source code is governed by a BSD-style license that can be
+" found in the LICENSE file.
+
+if exists('b:did_ftplugin')
+  finish
+endif
+let b:did_ftplugin = 1
+
+setlocal includeexpr=gn#TranslateToBuildFile(v:fname)
+
+setlocal commentstring=#\ %s