Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sorting more configurable. Can now have an arbitrary number of package groups. #149

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 38 additions & 20 deletions doc/scala.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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*

Expand All @@ -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*
Expand Down
42 changes: 26 additions & 16 deletions plugin/scala.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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('$')
Expand All @@ -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)
Expand All @@ -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
Expand Down