gclient_eval: Add more support when adding new variables.

Now we respect comments before the first variable.

Bug: 760633
Change-Id: Ibe60d719429c033415bfb1c99942c9d04601d967
Reviewed-on: https://chromium-review.googlesource.com/998683
Commit-Queue: Edward Lesmes <[email protected]>
Reviewed-by: Michael Moss <[email protected]>
Reviewed-by: Aaron Gable <[email protected]>
diff --git a/gclient_eval.py b/gclient_eval.py
index 7037875..93fd2a8 100644
--- a/gclient_eval.py
+++ b/gclient_eval.py
@@ -542,13 +542,15 @@
   if not gclient_dict['vars']:
     raise ValueError('vars dict is empty. This is not yet supported.')
 
-  # We will attempt to add the var right before the first var.
-  node = gclient_dict.GetNode('vars').keys[0]
+  # We will attempt to add the var right after 'vars = {'.
+  node = gclient_dict.GetNode('vars')
   if node is None:
     raise ValueError(
         "The vars dict has no formatting information." % var_name)
-  line = node.lineno
-  col = node.col_offset
+  line = node.lineno + 1
+
+  # We will try to match the new var's indentation to the next variable.
+  col = node.keys[0].col_offset
 
   # We use a minimal Python dictionary, so that ast can parse it.
   var_content = '{\n%s"%s": "%s",\n}' % (' ' * col, var_name, value)