From 6b10da9c868fc15a4feb65883c400fca5eb6e5bd Mon Sep 17 00:00:00 2001 From: David Hegland Date: Fri, 31 Jul 2020 11:24:41 -0500 Subject: [PATCH 1/3] Handle case with colon at the end of the line of a comment, but also still handle if the colon is at the end of a line with a comment after. Also cleanup whitespace in script. --- indent/python.vim | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/indent/python.vim b/indent/python.vim index 32c773c..6d7777d 100644 --- a/indent/python.vim +++ b/indent/python.vim @@ -1,7 +1,7 @@ " Python indent file " Language: Python " Maintainer: Eric Mc Sween -" Original Author: David Bustos +" Original Author: David Bustos " Last Change: 2004 Jun 07 " Only load this indent file when no other was loaded. @@ -22,7 +22,7 @@ let s:maxoff = 50 function! s:SearchParensPair() let line = line('.') let col = col('.') - + " Skip strings and comments and don't look too far let skip = "line('.') < " . (line - s:maxoff) . " ? dummy :" . \ 'synIDattr(synID(line("."), col("."), 0), "name") =~? ' . @@ -51,7 +51,7 @@ function! s:SearchParensPair() if par3lnum > parlnum || (par3lnum == parlnum && par3col > parcol) let parlnum = par3lnum let parcol = par3col - endif + endif " Put the cursor on the match if parlnum > 0 @@ -87,7 +87,7 @@ function! s:BlockStarter(lnum, block_start_re) if indent(lnum) < maxindent if getline(lnum) =~ a:block_start_re return lnum - else + else let maxindent = indent(lnum) " It's not worth going further if we reached the top level if maxindent == 0 @@ -98,14 +98,14 @@ function! s:BlockStarter(lnum, block_start_re) endwhile return -1 endfunction - + function! GetPythonIndent(lnum) " First line has indent 0 if a:lnum == 1 return 0 endif - + " If we can find an open parenthesis/bracket/brace, line up with it. call cursor(a:lnum, 1) let parlnum = s:SearchParensPair() @@ -126,7 +126,7 @@ function! GetPythonIndent(lnum) endif endif endif - + " Examine this line let thisline = getline(a:lnum) let thisindent = indent(a:lnum) @@ -140,7 +140,7 @@ function! GetPythonIndent(lnum) return -1 endif endif - + " If the line starts with 'except' or 'finally', line up with 'try' " or 'except' if thisline =~ '^\s*\(except\|finally\)\>' @@ -151,19 +151,19 @@ function! GetPythonIndent(lnum) return -1 endif endif - + " Examine previous line let plnum = a:lnum - 1 let pline = getline(plnum) let sslnum = s:StatementStart(plnum) - + " If the previous line is blank, keep the same indentation if pline =~ '^\s*$' return -1 endif - + " If this line is explicitly joined, try to find an indentation that looks - " good. + " good. if pline =~ '\\$' let compound_statement = '^\s*\(if\|while\|for\s.*\sin\|except\)\s*' let maybe_indent = matchend(getline(sslnum), compound_statement) @@ -173,10 +173,13 @@ function! GetPythonIndent(lnum) return indent(sslnum) + &sw * 2 endif endif - + " If the previous line ended with a colon, indent relative to - " statement start. - if pline =~ ':\s*$' + " statement start. Unless the colon was after the start of a + " comment. + " FIXME: Need to handle case where line ends with a colon but + " also includes a comment that also ends in a colon + if pline =~ ':\s*\(#.*\)$' && pline !~ '#.*:\s*$' return indent(sslnum) + &sw endif From 0b7fc38c6f79ffa0cccc3366d19b8154987fab5d Mon Sep 17 00:00:00 2001 From: David Hegland Date: Fri, 31 Jul 2020 12:18:17 -0500 Subject: [PATCH 2/3] Fix indentation issue with colon and no comments --- indent/python.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/indent/python.vim b/indent/python.vim index 6d7777d..a31f7e6 100644 --- a/indent/python.vim +++ b/indent/python.vim @@ -179,7 +179,7 @@ function! GetPythonIndent(lnum) " comment. " FIXME: Need to handle case where line ends with a colon but " also includes a comment that also ends in a colon - if pline =~ ':\s*\(#.*\)$' && pline !~ '#.*:\s*$' + if pline =~ ':\s*\(#*.*\)$' && pline !~ '#.*:\s*$' return indent(sslnum) + &sw endif From 735af45ab83da22a34edb02f7299ba0f7c635f2e Mon Sep 17 00:00:00 2001 From: raven42 Date: Fri, 7 Aug 2020 12:18:45 -0500 Subject: [PATCH 3/3] Fix indentation with colons --- indent/python.vim | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/indent/python.vim b/indent/python.vim index a31f7e6..3901e09 100644 --- a/indent/python.vim +++ b/indent/python.vim @@ -177,12 +177,14 @@ function! GetPythonIndent(lnum) " If the previous line ended with a colon, indent relative to " statement start. Unless the colon was after the start of a " comment. - " FIXME: Need to handle case where line ends with a colon but - " also includes a comment that also ends in a colon - if pline =~ ':\s*\(#*.*\)$' && pline !~ '#.*:\s*$' + if pline =~ ':\s*$' && pline !~ '#.*:\s*$' return indent(sslnum) + &sw endif + if pline =~ ':\s*#' + return indent(sslnum) + &sw + endif + " If the previous line was a stop-execution statement or a pass if getline(sslnum) =~ '^\s*\(break\|continue\|raise\|return\|pass\)\>' " See if the user has already dedented