]> git.madduck.net Git - etc/vim.git/commitdiff

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Fix indenting of strings, especially after opening paren
authorDaniel Hahler <git@thequod.de>
Fri, 27 Feb 2015 17:17:37 +0000 (18:17 +0100)
committerDaniel Hahler <git@thequod.de>
Wed, 6 May 2015 14:36:41 +0000 (16:36 +0200)
Fixes https://github.com/hynek/vim-python-pep8-indent/issues/36
Closes https://github.com/hynek/vim-python-pep8-indent/pull/37

indent/python.vim
spec/indent/indent_spec.rb

index 0c244cb3a998d91bed2075290660733966c7d65a..69128996b7ca1b1c0963b456d2b1340013b50d31 100644 (file)
@@ -246,6 +246,23 @@ function! s:indent_like_previous_line(lnum)
     return base
 endfunction
 
     return base
 endfunction
 
+" Is the syntax at lnum (and optionally cnum) a python string?
+function! s:is_python_string(lnum, ...)
+    let line = getline(a:lnum)
+    let linelen = len(line)
+    if linelen < 1
+      let linelen = 1
+    endif
+    let cols = a:0 ? type(a:1) != type([]) ? [a:1] : a:1 : range(1, linelen)
+    for cnum in cols
+        if match(map(synstack(a:lnum, cnum),
+                    \ 'synIDattr(v:val,"name")'), 'python\S*String') == -1
+            return 0
+        end
+    endfor
+    return 1
+endfunction
+
 function! GetPythonPEPIndent(lnum)
 
     " First line has indent 0
 function! GetPythonPEPIndent(lnum)
 
     " First line has indent 0
@@ -253,6 +270,24 @@ function! GetPythonPEPIndent(lnum)
         return 0
     endif
 
         return 0
     endif
 
+    " Multilinestrings: continous, docstring or starting.
+    if s:is_python_string(a:lnum)
+        if s:is_python_string(a:lnum-1)
+            " Previous line is (completely) a string.
+            return s:indent_like_previous_line(a:lnum)
+        endif
+
+        if match(getline(a:lnum-1), '^\s*\%("""\|''''''\)') != -1
+            " docstring.
+            return s:indent_like_previous_line(a:lnum)
+        endif
+
+        if s:is_python_string(a:lnum-1, len(getline(a:lnum-1)))
+            " String started in previous line.
+            return 0
+        endif
+    endif
+
     " Parens: If we can find an open parenthesis/bracket/brace, line up with it.
     let indent = s:indent_like_opening_paren(a:lnum)
     if indent >= -1
     " Parens: If we can find an open parenthesis/bracket/brace, line up with it.
     let indent = s:indent_like_opening_paren(a:lnum)
     if indent >= -1
index 1026bd1cb9b664fece414b1551d0e309f9a80582..8ee78a65e83d34174775bac0cc4a7f95bedab9d9 100644 (file)
@@ -129,6 +129,68 @@ shared_examples_for "vim" do
     end
   end
 
     end
   end
 
+  describe "when after an '(' that is followed by an unfinished string" do
+    before { vim.feedkeys 'itest("""' }
+
+    it "it does not indent the next line" do
+      vim.feedkeys '\<CR>'
+      proposed_indent.should == 0
+      indent.should == 0
+    end
+
+    it "with contents it does not indent the next line" do
+      vim.feedkeys 'string_contents\<CR>'
+      proposed_indent.should == 0
+      indent.should == 0
+    end
+  end
+
+  describe "when after assigning an unfinished string" do
+    before { vim.feedkeys 'itest = """' }
+
+    it "it does not indent the next line" do
+      vim.feedkeys '\<CR>'
+      proposed_indent.should == 0
+      indent.should == 0
+    end
+  end
+
+  describe "when after assigning an unfinished string" do
+    before { vim.feedkeys 'i    test = """' }
+
+    it "it does not indent the next line" do
+      vim.feedkeys '\<CR>'
+      proposed_indent.should == 0
+      indent.should == 0
+    end
+  end
+
+  describe "when after assigning a finished string" do
+    before { vim.feedkeys 'i    test = ""' }
+
+    it "it does indent the next line" do
+      vim.feedkeys '\<CR>'
+      proposed_indent.should == 4
+      indent.should == 4
+    end
+
+    it "and writing a new string, it does indent the next line" do
+      vim.feedkeys '\<CR>""'
+      proposed_indent.should == 4
+      indent.should == 4
+    end
+  end
+
+  describe "when after a docstring" do
+    before { vim.feedkeys 'i    """' }
+
+    it "it does indent the next line" do
+      vim.feedkeys '\<CR>'
+      proposed_indent.should == 4
+      indent.should == 4
+    end
+  end
+
   describe "when using simple control structures" do
       it "indents shiftwidth spaces" do
           vim.feedkeys 'iwhile True:\<CR>pass'
   describe "when using simple control structures" do
       it "indents shiftwidth spaces" do
           vim.feedkeys 'iwhile True:\<CR>pass'