From 9d0ffd9b2fc9d714c3ee8083a4e80c0b20100be9 Mon Sep 17 00:00:00 2001 From: David Briscoe Date: Thu, 15 Nov 2018 16:27:25 -0800 Subject: [PATCH 1/4] Pass line number as argument instead of global Get improvement from omnisharp-vim. Retrieved from: https://github.com/OmniSharp/omnisharp-vim/blob/4a6ffe6fdbb09b594fc503d022c1e263dd3edd8e/indent/cs.vim --- indent/cs.vim | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/indent/cs.vim b/indent/cs.vim index fc9cdb9..028e5bd 100644 --- a/indent/cs.vim +++ b/indent/cs.vim @@ -4,31 +4,33 @@ " " Only load this indent file when no other was loaded. -if exists("b:did_indent") +if exists('b:did_indent') finish endif let b:did_indent = 1 -setlocal indentexpr=GetCSIndent() +setlocal indentexpr=GetCSIndent(v:lnum) -function! GetCSIndent() +function! GetCSIndent(lnum) abort - let this_line = getline(v:lnum) - let previous_line = getline(v:lnum - 1) + let this_line = getline(a:lnum) + let previous_line = getline(a:lnum - 1) " Hit the start of the file, use zero indent. - if v:lnum == 0 + if a:lnum == 0 return 0 endif " If previous_line is an attribute line: if previous_line =~? '^\s*\[[A-Za-z]' && previous_line =~? '\]$' - let ind = indent(v:lnum - 1) + let ind = indent(a:lnum - 1) return ind else - return cindent(v:lnum) + return cindent(a:lnum) endif endfunction + +" vim:et:sw=2:sts=2 From daab1b6f9f7df010c87db38d673e854ed4ce629c Mon Sep 17 00:00:00 2001 From: David Briscoe Date: Thu, 15 Nov 2018 16:41:03 -0800 Subject: [PATCH 2/4] Obey cinkeys for #if and other compiler directives Compiler directives should go to the first column if that's what the user configured. Fixes directive when it follows an attribute line: [SerializeField] #if UNITY_EDITOR public #endif string m_PrettyName = ""; However, content inside directive (public) is still indented incorrectly. --- indent/cs.vim | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/indent/cs.vim b/indent/cs.vim index 028e5bd..1cdfead 100644 --- a/indent/cs.vim +++ b/indent/cs.vim @@ -23,8 +23,11 @@ function! GetCSIndent(lnum) abort return 0 endif + " Compiler directives use zero indent if so configured. + let is_first_col_macro = this_line =~? '^\s*#' && stridx(&l:cinkeys, '0#') >= 0 + " If previous_line is an attribute line: - if previous_line =~? '^\s*\[[A-Za-z]' && previous_line =~? '\]$' + if !is_first_col_macro && previous_line =~? '^\s*\[[A-Za-z]' && previous_line =~? '\]$' let ind = indent(a:lnum - 1) return ind else From 462395526b5f1a49cc4967f51c49269cda31f65c Mon Sep 17 00:00:00 2001 From: David Briscoe Date: Fri, 16 Nov 2018 10:47:21 -0800 Subject: [PATCH 3/4] Extract functions for clarity and debuggability Prefer function names over comments. Easier to remove the s: off of these and interactively do: echo IsAttributeLine(getcurpos()[1]) --- indent/cs.vim | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/indent/cs.vim b/indent/cs.vim index 1cdfead..108a790 100644 --- a/indent/cs.vim +++ b/indent/cs.vim @@ -12,6 +12,13 @@ let b:did_indent = 1 setlocal indentexpr=GetCSIndent(v:lnum) +function! s:IsCompilerDirective(line) + return a:line =~? '^\s*#' +endf + +function! s:IsAttributeLine(line) + return a:line =~? '^\s*\[[A-Za-z]' && a:line =~? '\]$' +endf function! GetCSIndent(lnum) abort @@ -24,10 +31,9 @@ function! GetCSIndent(lnum) abort endif " Compiler directives use zero indent if so configured. - let is_first_col_macro = this_line =~? '^\s*#' && stridx(&l:cinkeys, '0#') >= 0 + let is_first_col_macro = s:IsCompilerDirective(this_line) && stridx(&l:cinkeys, '0#') >= 0 - " If previous_line is an attribute line: - if !is_first_col_macro && previous_line =~? '^\s*\[[A-Za-z]' && previous_line =~? '\]$' + if !is_first_col_macro && s:IsAttributeLine(previous_code_line) let ind = indent(a:lnum - 1) return ind else From 0d0b521cd27e503f3e0bb86ac870e818db46dd9b Mon Sep 17 00:00:00 2001 From: David Briscoe Date: Tue, 20 Nov 2018 16:49:23 -0800 Subject: [PATCH 4/4] Fix text preceded by attributes and directive Now content inside directive (public) is indented correctly (matches attribute): [SerializeField] #if UNITY_EDITOR public #endif string m_PrettyName = ""; --- indent/cs.vim | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/indent/cs.vim b/indent/cs.vim index 108a790..a42f4da 100644 --- a/indent/cs.vim +++ b/indent/cs.vim @@ -20,21 +20,36 @@ function! s:IsAttributeLine(line) return a:line =~? '^\s*\[[A-Za-z]' && a:line =~? '\]$' endf -function! GetCSIndent(lnum) abort - - let this_line = getline(a:lnum) - let previous_line = getline(a:lnum - 1) +function! s:FindPreviousNonCompilerDirectiveLine(start_lnum) + for delta in range(0, a:start_lnum) + let lnum = a:start_lnum - delta + let line = getline(lnum) + let is_directive = s:IsCompilerDirective(line) + if !is_directive + return lnum + endif + endfor + return 0 +endf +function! GetCSIndent(lnum) abort " Hit the start of the file, use zero indent. if a:lnum == 0 return 0 endif + let this_line = getline(a:lnum) + " Compiler directives use zero indent if so configured. let is_first_col_macro = s:IsCompilerDirective(this_line) && stridx(&l:cinkeys, '0#') >= 0 + if is_first_col_macro + return cindent(a:lnum) + endif - if !is_first_col_macro && s:IsAttributeLine(previous_code_line) - let ind = indent(a:lnum - 1) + let lnum = s:FindPreviousNonCompilerDirectiveLine(a:lnum - 1) + let previous_code_line = getline(lnum) + if s:IsAttributeLine(previous_code_line) + let ind = indent(lnum) return ind else return cindent(a:lnum)