summaryrefslogtreecommitdiffstats
path: root/indent/swift.vim
diff options
context:
space:
mode:
authorDan Reif <dan.reif@collectivehealth.com>2018-04-30 12:00:42 -0700
committerDan Reif <dan.reif@collectivehealth.com>2018-04-30 12:00:42 -0700
commit3e0c887365bc1ebd55b91dd69ab73f2dee1f00ed (patch)
treece76003a08274cb805df101a44be8d37af1a4c4b /indent/swift.vim
parentb4d7993e7ea554153025c5072749f415e69e0323 (diff)
downloadvim-polyglot-3e0c887365bc1ebd55b91dd69ab73f2dee1f00ed.tar.gz
vim-polyglot-3e0c887365bc1ebd55b91dd69ab73f2dee1f00ed.zip
Update (periodic rebuild)
I originally meant to run this before adding haproxy, but accidentally pushed that into my branch. If you'd like to see that content, it's at https://github.com/CH-DanReif/vim-polyglot/commit/414ad25c3ad1ab9c4b6a99fe4f08d6c30b7e0f57.
Diffstat (limited to 'indent/swift.vim')
-rw-r--r--indent/swift.vim80
1 files changed, 68 insertions, 12 deletions
diff --git a/indent/swift.vim b/indent/swift.vim
index 92df0fc8..14e851ee 100644
--- a/indent/swift.vim
+++ b/indent/swift.vim
@@ -42,11 +42,15 @@ endfunction
function! s:IsExcludedFromIndentAtPosition(line, column)
let name = s:SyntaxNameAtPosition(a:line, a:column)
- return name ==# "swiftComment" || name ==# "swiftString"
+ return s:IsSyntaxNameExcludedFromIndent(name)
endfunction
function! s:IsExcludedFromIndent()
- return s:SyntaxName() ==# "swiftComment" || s:SyntaxName() ==# "swiftString"
+ return s:IsSyntaxNameExcludedFromIndent(s:SyntaxName())
+endfunction
+
+function! s:IsSyntaxNameExcludedFromIndent(name)
+ return a:name ==# "swiftComment" || a:name ==# "swiftString" || a:name ==# "swiftInterpolatedWrapper" || a:name ==# "swiftMultilineInterpolatedWrapper" || a:name ==# "swiftMultilineString"
endfunction
function! s:IsCommentLine(lnum)
@@ -103,10 +107,10 @@ function! SwiftIndent(...)
return indent(openingSquare) + shiftwidth()
endif
- if line =~ ":$"
+ if line =~ ":$" && (line =~ '^\s*case\W' || line =~ '^\s*default\W')
let switch = search("switch", "bWn")
return indent(switch)
- elseif previous =~ ":$"
+ elseif previous =~ ":$" && (previous =~ '^\s*case\W' || previous =~ '^\s*default\W')
return previousIndent + shiftwidth()
endif
@@ -133,12 +137,26 @@ function! SwiftIndent(...)
return previousIndent + shiftwidth()
elseif line =~ "}.*{"
let openingBracket = searchpair("{", "", "}", "bWn", "s:IsExcludedFromIndent()")
+
+ let bracketLine = getline(openingBracket)
+ let numOpenParensBracketLine = s:NumberOfMatches("(", bracketLine, openingBracket)
+ let numCloseParensBracketLine = s:NumberOfMatches(")", bracketLine, openingBracket)
+ if numOpenParensBracketLine > numCloseParensBracketLine
+ let line = line(".")
+ let column = col(".")
+ call cursor(openingParen, column)
+ let openingParenCol = searchpairpos("(", "", ")", "bWn", "s:IsExcludedFromIndent()")[1]
+ call cursor(line, column)
+ return openingParenCol
+ endif
+
return indent(openingBracket)
elseif currentCloseBrackets > currentOpenBrackets
let column = col(".")
- call cursor(line("."), 1)
+ let line = line(".")
+ call cursor(line, 1)
let openingBracket = searchpair("{", "", "}", "bWn", "s:IsExcludedFromIndent()")
- call cursor(line("."), column)
+ call cursor(line, column)
let bracketLine = getline(openingBracket)
@@ -151,8 +169,23 @@ function! SwiftIndent(...)
let openingParen = searchpair("(", "", ")", "bWn", "s:IsExcludedFromIndent()")
call cursor(line, column)
return indent(openingParen)
+ elseif numOpenParensBracketLine > numCloseParensBracketLine
+ let line = line(".")
+ let column = col(".")
+ call cursor(openingParen, column)
+ let openingParenCol = searchpairpos("(", "", ")", "bWn", "s:IsExcludedFromIndent()")[1]
+ call cursor(line, column)
+ return openingParenCol
endif
+
return indent(openingBracket)
+ elseif line =~ '^\s*)$'
+ let line = line(".")
+ let column = col(".")
+ call cursor(line, 1)
+ let openingParen = searchpair("(", "", ")", "bWn", "s:IsExcludedFromIndent()")
+ call cursor(line, column)
+ return indent(openingParen)
else
" - Current line is blank, and the user presses 'o'
return previousIndent
@@ -196,8 +229,19 @@ function! SwiftIndent(...)
return previousIndent + shiftwidth()
endif
- let previousParen = match(previous, "(")
- return indent(previousParen) + shiftwidth()
+ let previousParen = match(previous, '\v\($')
+ if previousParen != -1
+ return previousIndent + shiftwidth()
+ endif
+
+ let line = line(".")
+ let column = col(".")
+ call cursor(previousNum, col([previousNum, "$"]))
+ let previousParen = searchpairpos("(", "", ")", "cbWn", "s:IsExcludedFromIndent()")
+ call cursor(line, column)
+
+ " Match the last non escaped paren on the previous line
+ return previousParen[1]
endif
if numOpenBrackets > numCloseBrackets
@@ -206,7 +250,7 @@ function! SwiftIndent(...)
call cursor(previousNum, column)
let openingParen = searchpair("(", "", ")", "bWn", "s:IsExcludedFromIndent()")
call cursor(line, column)
- return indent(openingParen) + shiftwidth()
+ return openingParen + 1
endif
" - Previous line has close then open braces, indent previous + 1 'sw'
@@ -226,11 +270,23 @@ function! SwiftIndent(...)
" - Line above has (unmatched) open paren, next line needs indent
if numOpenParens > 0
let savePosition = getcurpos()
+ let lastColumnOfPreviousLine = col([previousNum, "$"]) - 1
" Must be at EOL because open paren has to be above (left of) the cursor
- call cursor(previousNum, [previousNum, col("$")])
- let previousParen = searchpair("(", "", ")", "cbWn", "s:IsExcludedFromIndent()")
+ call cursor(previousNum, lastColumnOfPreviousLine)
+ let previousParen = searchpairpos("(", "", ")", "cbWn", "s:IsExcludedFromIndent()")[1]
+ " If the paren on the last line is the last character, indent the contents
+ " at shiftwidth + previous indent
+ if previousParen == lastColumnOfPreviousLine
+ return previousIndent + shiftwidth()
+ endif
+
+ " The previous line opens a closure and doesn't close it
+ if numOpenBrackets > numCloseBrackets
+ return previousParen + shiftwidth()
+ endif
+
call setpos(".", savePosition)
- return indent(previousParen) + shiftwidth()
+ return previousParen
endif
return cindent