diff --git a/doc/scala.txt b/doc/scala.txt index ebc2e68..39d9ef6 100644 --- a/doc/scala.txt +++ b/doc/scala.txt @@ -49,13 +49,13 @@ standard Javadoc format. */ Set this option to enable the indentation standard as recommended for Scaladoc comments. - /** This is a Scaladoc comment using + /** This is a Scaladoc comment using * the recommended indentation. */ > let g:scala_scaladoc_indent = 1 < - + ============================================================================== COMMANDS *scala-commands* @@ -73,26 +73,44 @@ COMMANDS *scala-commands* This makes this command include all imports in the sorting regardless of blank lines in between them and - puts them in three predefined groups instead. - The three groups in which the imports can fall are: + puts them in to predefined groups instead. + The two groups in which the imports fall by default + are: 1. Scala and Java core - 2. Third party libraries - 3. First party code (ie. your own) - - Java and Scala core imports are identified by the - java(x) and scala namespaces. - Everything else that isn't a first party namespace - will be a third party import. - You can define a regex that matches first party - namespaces by setting - - g:scala_first_party_namespaces - - For example in a standard Play app this would be - set to - g:scala_first_party_namespaces= - \ '\(controllers\|views\|models\)' + 2. All others + + By default, Java and Scala imports are identified by + the java(x) and scala namespaces. + + You can modify this behavior and provide your own + import groups by setting + + g:scala_import_sort_groups. + + Any imports not caught in the patterns will be put at + the end of the imports list, in their own group. + + For example, a standard Play app would have the + following sort groups: + + g:scala_import_sort_groups = [ + \ '\(java\(x\)\?\|scala\)\.', + \ '\(controllers\|views\|models\)' + \] + + If you were to want packages that are specific to your + project to be placed at the end of the imports list, + you can get this behavior using a negative lookahead. + Building on the example above, that would look like + + g:scala_import_sort_groups = [ + \ '\(java\(x\)\?\|scala\)\.', + \ '\(controllers\|views\|models\)', + \ '\(com.my.project\)\@!' + \] + + ============================================================================== MAPPINGS *scala-mappings* diff --git a/plugin/scala.vim b/plugin/scala.vim index 49cdf3c..005d2b1 100644 --- a/plugin/scala.vim +++ b/plugin/scala.vim @@ -40,9 +40,20 @@ function! s:sortAcrossGroups() let first_line = -1 let last_line = -1 let trailing_newlines = 0 - let java_scala_imports = [] - let first_party_imports = [] - let third_party_imports = [] + + if exists('g:scala_import_sort_groups') + let sort_group_patterns = copy(g:scala_import_sort_groups) + else + let sort_group_patterns = ['\(java\(x\)\?\|scala\)\.'] + endif + + " A catch all pattern for imports which didn't match the other cases. + call add(sort_group_patterns, '.*') + + let import_groups = [] + for x in sort_group_patterns + call add(import_groups, []) + endfor " loop over lines in buffer while curr <= line('$') @@ -54,18 +65,17 @@ function! s:sortAcrossGroups() let first_line = curr endif - if line =~ '^import \(java\(x\)\?\|scala\)\.' - call add(java_scala_imports, line) - elseif exists('g:scala_first_party_namespaces') - let regex = '^import '.g:scala_first_party_namespaces + let iterator = 0 + for sort_group_pattern in sort_group_patterns + let regex = '^import '.sort_group_pattern if line =~ regex - call add(first_party_imports, line) - else - call add(third_party_imports, line) + call add(import_groups[iterator], line) + let iterator += 1 + break endif - else - call add(third_party_imports, line) - endif + + let iterator += 1 + endfor let trailing_newlines = 0 elseif empty(line) @@ -86,9 +96,9 @@ function! s:sortAcrossGroups() execute 'd'to_delete endif - call s:sortAndPrint(first_party_imports) - call s:sortAndPrint(third_party_imports) - call s:sortAndPrint(java_scala_imports) + for lines in reverse(import_groups) + call s:sortAndPrint(lines) + endfor if first_line != -1 " remove extra blank line at top