[bitbake-devel,v2,7/9] bitbake: contrib/vim: indenting for assignments; tweak Python indenting

Submitted by Nicolas Cornu via bitbake-devel on Oct. 17, 2019, 4:36 p.m. | Patch ID: 165899

Details

Message ID 1246d20a022e82ab47b38b1a92e75c35d63ea754.1571329975.git.chris.laplante@agilent.com
State New
Headers show

Commit Message

Nicolas Cornu via bitbake-devel Oct. 17, 2019, 4:36 p.m.
Signed-off-by: Chris Laplante <chris.laplante@agilent.com>
---
 bitbake/contrib/vim/indent/bitbake.vim | 56 ++++++++++++++++++++++++++++++++--
 1 file changed, 53 insertions(+), 3 deletions(-)

Patch hide | download patch | download mbox

diff --git a/bitbake/contrib/vim/indent/bitbake.vim b/bitbake/contrib/vim/indent/bitbake.vim
index 76f84e5..1d0a6e8 100644
--- a/bitbake/contrib/vim/indent/bitbake.vim
+++ b/bitbake/contrib/vim/indent/bitbake.vim
@@ -110,7 +110,12 @@  function GetPythonIndent(lnum)
             \ . " =~ '\\(Comment\\|Todo\\|String\\)$'",
             \ searchpair_stopline, searchpair_timeout)
     if p > 0
-        if s:is_python_func_def(p)
+        if s:is_bb_python_func_def(p)
+          " Handle first non-empty line inside a BB Python task
+          if p == plnum
+              return shiftwidth()
+          endif
+
           " Handle the user actually trying to close a BitBake Python task
           let line = getline(a:lnum)
           if line =~ '^\s*}'
@@ -242,6 +247,7 @@  unlet s:keepcpo
 
 
 let b:did_indent = 1
+setlocal indentkeys+=0\"
 
 
 function BitbakeIndent(lnum)
@@ -249,13 +255,57 @@  function BitbakeIndent(lnum)
         return -1
     endif
 
-    let stack = synstack(a:lnum, col("."))
+    let stack = synstack(a:lnum, 1)
     if len(stack) == 0
         return -1
     endif
 
     let name = synIDattr(stack[0], "name")
-    "echo name
+
+    " TODO: support different styles of indentation for assignments. For now,
+    " we only support like this:
+    " VAR = " \
+    "     value1 \
+    "     value2 \
+    " "
+    "
+    " i.e. each value indented by shiftwidth(), with the final quote " completely unindented.
+    if name == "bbVarValue"
+        " Quote handling is tricky. kernel.bbclass has this line for instance:
+        "     EXTRA_OEMAKE = " HOSTCC="${BUILD_CC} ${BUILD_CFLAGS} ${BUILD_LDFLAGS}" " HOSTCPP="${BUILD_CPP}""
+        " Instead of trying to handle crazy cases like that, just assume that a
+        " double-quote on a line by itself (following an assignment) means the
+        " user is closing the assignment, and de-dent.
+        if getline(a:lnum) =~ '^\s*"$'
+            return 0
+        endif
+
+        let prevstack = synstack(a:lnum - 1, 1)
+        if len(prevstack) == 0
+            return -1
+        endif
+
+        let prevname = synIDattr(prevstack[0], "name")
+
+        " Only indent if there was actually a continuation character on
+        " the previous line, to avoid misleading indentation.
+        let prevlinelastchar = synIDattr(synID(a:lnum - 1, col([a:lnum - 1, "$"]) - 1, 1), "name")
+        let prev_continued = prevlinelastchar == "bbContinue"
+
+        " Did the previous line introduce an assignment?
+        if index(["bbVarDef", "bbVarFlagDef"], prevname) != -1
+            if prev_continued
+                return shiftwidth()
+            endif
+        endif
+
+        if !prev_continued
+            return 0
+        endif
+
+        " Autoindent can take it from here
+        return -1
+    endif
 
     if index(["bbPyDefRegion", "bbPyFuncRegion"], name) != -1
         let ret = GetPythonIndent(a:lnum)