Skip to content
Open
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
25 changes: 23 additions & 2 deletions pythonx/jis.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def __init__(self):
self._staticFirst = int(vim.eval("g:JavaImpStaticImportsFirst"))
self._spacesAfterGroups = int(vim.eval("g:JavaImpSpacesAfterGroups"))
self._newLineAtEnd = int(vim.eval("g:JavaImpNewLineAtEnd"))
self._sortLongerSecond = int(vim.eval("g:JavaImpSortLongerSecond"))

# Initialize Import Statement Range
self._rangeStart = -1
Expand Down Expand Up @@ -77,13 +78,31 @@ def _sortImports(self):

return fullySortedImportStatements


# Sort the provided importStatements first by the provided importRegexList, then
# alphanumerically.
def _regexSort(self, importStatements, importRegexList):
regexSortedList = list()

#put longer imports after shorter imports
def sortLongestSecond(first, second):
if second.startswith(first[:-1]):
return -1
if first.startswith(second[:-1]):
return 1
elif second < first:
return 1
elif first < second:
return -1
else:
return 0

# First sort the list alphanumerically.
importStatements.sort()
# Sort with custom comparator
if self._sortLongerSecond:
importStatements.sort(sortLongestSecond)
else:
importStatements.sort()

# If the regex list is non-empty
if len(importRegexList) > 0:
Expand All @@ -104,6 +123,8 @@ def _regexSort(self, importStatements, importRegexList):

return regexSortedList



# Given a list of import statements, divide it into top, static and normal
# imports.
def _separateImports(self, importStatements):
Expand Down Expand Up @@ -265,7 +286,7 @@ def _updateBuffer(self, fullySortedImportStatements):
# Insert Spacing into Middle Import List.
spacedList = self._insertSpacing(fullySortedImportStatements, self._depth)

startLine = self._rangeStart
startLine = self._rangeStart - 1

startLine = self._insertListAtLine(startLine, spacedList)

Expand Down