-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
246 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package org.acejump.action | ||
|
||
import com.intellij.openapi.editor.* | ||
import org.acejump.* | ||
import org.acejump.search.SearchProcessor | ||
|
||
internal class TagScroller(private val editor: Editor, private val searchProcessor: SearchProcessor) { | ||
fun scroll(forward: Boolean = true): Boolean { | ||
val position = if (forward) findNextPosition() else findPreviousPosition() | ||
return if (position != null) true.also { scrollTo(position) } else false | ||
} | ||
|
||
private fun scrollTo(position: LogicalPosition) = editor.run { | ||
scrollingModel.disableAnimation() | ||
scrollingModel.scrollTo(position, ScrollType.CENTER) | ||
|
||
val firstInView = textMatches.first { it in editor.getView() } | ||
val horizontalOffset = offsetToLogicalPosition(firstInView).column | ||
if (horizontalOffset > scrollingModel.visibleArea.width) | ||
scrollingModel.scrollHorizontally(horizontalOffset) | ||
} | ||
|
||
val textMatches by lazy { searchProcessor.results[editor]!! } | ||
|
||
private fun findPreviousPosition(): LogicalPosition? { | ||
val prevIndex = textMatches.toList().dropLastWhile { it > editor.getView().first } | ||
.lastOrNull() ?: textMatches.lastOrNull() ?: return null | ||
|
||
val prevLineNum = editor.offsetToLogicalPosition(prevIndex).line | ||
|
||
// Try to capture as many previous results as will fit in a screenful | ||
fun maximizeCoverageOfPreviousOccurrence(): LogicalPosition { | ||
val minVisibleLine = prevLineNum - editor.getScreenHeight() | ||
val firstVisibleIndex = editor.getLineStartOffset(minVisibleLine) | ||
val firstIndex = textMatches.dropWhile { it < firstVisibleIndex }.first() | ||
return editor.offsetCenter(firstIndex, prevIndex) | ||
} | ||
|
||
return maximizeCoverageOfPreviousOccurrence() | ||
} | ||
|
||
/** | ||
* Returns the center of the next set of results that will fit in the editor. | ||
* [textMatches] must be sorted prior to using Scroller. If [textMatches] have | ||
* not previously been sorted, the result of calling this method is undefined. | ||
*/ | ||
|
||
private fun findNextPosition(): LogicalPosition? { | ||
val nextIndex = textMatches.dropWhile { it <= editor.getView().last } | ||
.firstOrNull() ?: textMatches.firstOrNull() ?: return null | ||
|
||
val nextLineNum = editor.offsetToLogicalPosition(nextIndex).line | ||
|
||
// Try to capture as many subsequent results as will fit in a screenful | ||
fun maximizeCoverageOfNextOccurrence(): LogicalPosition { | ||
val maxVisibleLine = nextLineNum + editor.getScreenHeight() | ||
val lastVisibleIndex = editor.getLineEndOffset(maxVisibleLine, true) | ||
val lastIndex = textMatches.toList().dropLastWhile { it > lastVisibleIndex }.last() | ||
return editor.offsetCenter(nextIndex, lastIndex) | ||
} | ||
|
||
return maximizeCoverageOfNextOccurrence() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters