├── ftplugin └── objc.vim ├── after ├── syntax │ └── objc.vim └── indent │ └── objc.vim └── README.markdown /ftplugin/objc.vim: -------------------------------------------------------------------------------- 1 | " Use C++ style comment strings with commentary.vim 2 | setl commentstring=//%s 3 | 4 | " Search for include files inside frameworks (used for gf etc.) 5 | setl includeexpr=substitute(v:fname,'\\([^/]\\+\\)/\\(.\\+\\)','/System/Library/Frameworks/\\1.framework/Headers/\\2','') 6 | 7 | -------------------------------------------------------------------------------- /after/syntax/objc.vim: -------------------------------------------------------------------------------- 1 | " ARC type modifiers 2 | syn keyword objcTypeModifier __bridge __bridge_retained __bridge_transfer __autoreleasing __strong __weak __unsafe_unretained 3 | 4 | " Block modifiers 5 | syn keyword objcTypeModifier __block 6 | 7 | " Remote messaging modifiers 8 | syn keyword objcTypeModifier byref 9 | 10 | " Property keywords - these are only highlighted inside '@property (...)' 11 | syn keyword objcPropertyAttribute contained getter setter readwrite readonly strong weak copy assign retain nonatomic 12 | syn match objcProperty display "^\s*@property\>\s*([^)]*)" contains=objcPropertyAttribute 13 | 14 | " The @property directive must be defined after objcProperty or it won't be 15 | " highlighted 16 | syn match objcDirective "@property\|@synthesize\|@dynamic\|@package" 17 | 18 | " Highlight property attributes as if they were type modifiers 19 | hi def link objcPropertyAttribute objcTypeModifier 20 | -------------------------------------------------------------------------------- /README.markdown: -------------------------------------------------------------------------------- 1 | This repository contains my Vim configuration files for Objective-C editing. 2 | If you have any improvements, please don't hesitate to send them my way. 3 | 4 | 5 | ## Improved indenting 6 | 7 | ### Colon alignment 8 | 9 | The default indent expression has a tendency to align colons even when they 10 | should not be aligned. Here is an example where the default indent fails, but 11 | which my improved indent expression can handle: 12 | 13 | ```objc 14 | - (void)fun 15 | { 16 | // Avoid aligning colons here 17 | if ([obj callSomething:x]) 18 | [obj dontAlign:y] 19 | 20 | // Avoid aligning here, preserve at least one shiftwidth of indent 21 | [obj firstParam:x 22 | doNotAlignThis:z]; 23 | } 24 | ``` 25 | 26 | ### Block alignment 27 | 28 | I have also added better indenting for blocks, for example: 29 | 30 | ```objc 31 | dispatch_async(queue, ^{ 32 | do_stuff(); // indented one shiftwidth 33 | }); // previous indent restored 34 | ``` 35 | 36 | NOTE! Vim highlights curly braces in blocks as errors. To work around this add 37 | the line 38 | 39 | ```viml 40 | let c_no_curly_error = 1 41 | ``` 42 | 43 | to your `~/.vimrc` file. I haven't been able to figure out a way to do this 44 | automatically in this plugin. 45 | 46 | ### Other indenting remarks 47 | 48 | Avoid indenting things like `@interface`, `@end`, and so on. 49 | 50 | 51 | ## Updated syntax highlighting 52 | 53 | Objective-C 2.0 introduced new keywords which should be highlighted but are not 54 | by default. Automatic reference counting (ARC) also adds a few keywords that 55 | are not highlighted. Try to remedy this situation. 56 | 57 | 58 | ## Miscellaneous 59 | 60 | Set `'commentstring'` to use C++ style comments instead of C-style comments 61 | (this is for example used by the 62 | [Commentary plugin](https://github.com/tpope/vim-commentary)). 63 | 64 | Set `'includeexpr'` so that commands like `gf` work on framework imports. For 65 | example, place the cursor inside the angle brackets on a line like `#import 66 | ` and hit `gf` to open up the `OpenGL.h` header. 67 | 68 | 69 | ## Installing 70 | 71 | Assuming you are using the 72 | [Pathogen plugin](https://github.com/tpope/vim-pathogen), 73 | just clone this repository in your `~/.vim/bundle` folder like so: 74 | 75 | ``` 76 | $ cd ~/.vim/bundle 77 | $ git clone https://github.com/b4winckler/vim-objc.git 78 | ``` 79 | 80 | ## License 81 | 82 | Copyright 2011 Björn Winckler. Distributed under the same license as Vim 83 | itself. See `:h license`. 84 | -------------------------------------------------------------------------------- /after/indent/objc.vim: -------------------------------------------------------------------------------- 1 | " Vim indent file 2 | " Language: Objective-C 3 | " Maintainer: Bjorn Winckler 4 | " Last Change: 2012 Jan 01 5 | 6 | " Ensure 'cpo' is set to Vim default values and restore later 7 | let s:save_cpo = &cpo 8 | set cpo&vim 9 | 10 | " Only load this indent file when no other was loaded. 11 | "if exists("b:did_indent") 12 | " finish 13 | "endif 14 | "let b:did_indent = 1 15 | "setlocal cindent 16 | 17 | setl indentkeys=0{,0},:,0#,!^F,o,O,e,<:> 18 | 19 | setlocal indentexpr=GetObjCIndentImproved() 20 | 21 | " Top level statements which should not be indented, and which should not 22 | " cause next (non-blank) line to be indented either. 23 | let s:topLev = '^\s*@\%(class\|end\|implementation\|interface\|protocol\|\)\>' 24 | 25 | function! GetObjCIndentImproved() 26 | " NOTE: Ignore leading white space to avoid having to deal with space vs. 27 | " tab issues. Rely on the indent() function instead. 28 | let thisLine = substitute(getline(v:lnum), '^\s*', '', '') 29 | 30 | if thisLine =~# s:topLev || getline(prevnonblank(v:lnum - 1)) =~# s:topLev 31 | return 0 32 | endif 33 | 34 | " If current line looks like an argument to a message dispatch, then line 35 | " up colon with previous line. This will indent the second line so that 36 | " the colons line up in 37 | " 38 | " [obj firstParameter:value 39 | " paramB:value2]; 40 | " 41 | " but it will not line up colons in 42 | " 43 | " if ([obj something:here]) 44 | " [obj other:here]; 45 | " 46 | let thisColon = match(thisLine, '^\s*\K\k*\zs:') 47 | if thisColon > 0 48 | let prevLine = substitute(getline(v:lnum - 1), '^\s*', '', '') 49 | let prevColon = match(prevLine, ':') 50 | if prevColon > 0 51 | " Try to align colons, always making sure line is indented at least 52 | " one shiftwidth more than the indentation at the beginning of the 53 | " message. Avoids situations like this: 54 | " 55 | " if ([obj a:x 56 | " aLongParameter:y]) 57 | " 58 | let [lnum,lcol] = searchpairpos('\[', '', '\]', 'b', 0, 59 | \ max([1, v:lnum - 10])) 60 | let minInd = &sw + (lnum > 0 ? indent(lnum) : 0) 61 | let alignedInd = indent(v:lnum - 1) + prevColon - thisColon 62 | return alignedInd > minInd ? alignedInd : minInd 63 | endif 64 | endif 65 | 66 | let prevLnum = v:lnum - 1 67 | let ind = indent(prevLnum) 68 | 69 | " Indent one shiftwidth after opening block, e.g.: 70 | " 71 | " call_func_with_block(param, ^{ 72 | " do_stuff(); 73 | " }); 74 | " 75 | let blockPat = '\^\s*\(([^)]*)\)\?\s*{$' 76 | if thisLine =~ '^}' 77 | norm '^%' 78 | if getline(".") =~ blockPat 79 | return indent(".") 80 | endif 81 | endif 82 | 83 | if getline(prevLnum) =~ blockPat 84 | return ind + &sw 85 | endif 86 | 87 | return cindent(v:lnum) 88 | endfunction 89 | 90 | 91 | " Restore 'cpo' options 92 | let &cpo = s:save_cpo 93 | unlet s:save_cpo 94 | --------------------------------------------------------------------------------