1
0
mirror of https://github.com/chylex/IntelliJ-AceJump.git synced 2025-04-09 17:15:43 +02:00

In Kotlin, helper methods don't need an enclosing class

This commit is contained in:
breandan.considine 2016-07-17 11:58:20 -04:00
parent 098f141e01
commit 596b96a1d3
2 changed files with 135 additions and 136 deletions
.idea
src/main/kotlin/com/johnlindquist/acejump

View File

@ -4,6 +4,7 @@
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>
<option name="createEmptyContentRootDirectories" value="true" />
<option name="disableWrapperSourceDistributionNotification" value="true" />
<option name="distributionType" value="DEFAULT_WRAPPED" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleHome" value="C:/Program Files (x86)/Gradle/gradle-2.9" />

View File

@ -25,151 +25,149 @@ import com.intellij.openapi.editor.VisualPosition
/**
* This is a set of helper methods for working with editors. All line and column values are zero based.
*/
object EditorHelper {
fun getVisualLineAtTopOfScreen(editor: Editor): Int {
val lh = editor.lineHeight
return (editor.scrollingModel.verticalScrollOffset + lh - 1) / lh
fun getVisualLineAtTopOfScreen(editor: Editor): Int {
val lh = editor.lineHeight
return (editor.scrollingModel.verticalScrollOffset + lh - 1) / lh
}
/**
* Gets the number of actual lines in the file
* @param editor The editor
* *
* @return The file line count
*/
fun getLineCount(editor: Editor): Int {
var len = editor.document.lineCount
if (editor.document.textLength > 0 && editor.document.charsSequence[editor.document.textLength - 1] == '\n') {
len--
}
/**
* Gets the number of actual lines in the file
return len
}
* @param editor The editor
* *
* @return The file line count
*/
fun getLineCount(editor: Editor): Int {
var len = editor.document.lineCount
if (editor.document.textLength > 0 && editor.document.charsSequence[editor.document.textLength - 1] == '\n') {
len--
}
/**
* Gets the actual number of characters in the file
return len
}
* @param editor The editor
* *
* @param includeEndNewLine True include newline
* *
* @return The file's character count
*/
@JvmOverloads fun getFileSize(editor: Editor, includeEndNewLine: Boolean = false): Int {
val len = editor.document.textLength
return if (includeEndNewLine || len == 0 || editor.document.charsSequence[len - 1] != '\n')
len
else
len - 1
}
/**
* Gets the actual number of characters in the file
/**
* Gets the number of lines than can be displayed on the screen at one time. This is rounded down to the
* nearest whole line if there is a partial line visible at the bottom of the screen.
* @param editor The editor
* *
* @param includeEndNewLine True include newline
* *
* @return The file's character count
*/
@JvmOverloads fun getFileSize(editor: Editor, includeEndNewLine: Boolean = false): Int {
val len = editor.document.textLength
return if (includeEndNewLine || len == 0 || editor.document.charsSequence[len - 1] != '\n')
len
else
len - 1
}
* @param editor The editor
* *
* @return The number of screen lines
*/
fun getScreenHeight(editor: Editor): Int {
val lh = editor.lineHeight
val height = editor.scrollingModel.visibleArea.y + editor.scrollingModel.visibleArea.height - getVisualLineAtTopOfScreen(editor) * lh
return height / lh
}
/**
* Gets the number of lines than can be displayed on the screen at one time. This is rounded down to the
* nearest whole line if there is a partial line visible at the bottom of the screen.
/**
* Converts a visual line number to a logical line number.
* @param editor The editor
* *
* @return The number of screen lines
*/
fun getScreenHeight(editor: Editor): Int {
val lh = editor.lineHeight
val height = editor.scrollingModel.visibleArea.y + editor.scrollingModel.visibleArea.height - getVisualLineAtTopOfScreen(editor) * lh
return height / lh
}
* @param editor The editor
* *
* @param line The visual line number to convert
* *
* @return The logical line number
*/
fun visualLineToLogicalLine(editor: Editor, line: Int): Int {
val logicalLine = editor.visualToLogicalPosition(VisualPosition(line, 0)).line
return normalizeLine(editor, logicalLine)
}
/**
* Converts a visual line number to a logical line number.
/**
* Returns the offset of the start of the requested line.
* @param editor The editor
* *
* @param line The visual line number to convert
* *
* @return The logical line number
*/
fun visualLineToLogicalLine(editor: Editor, line: Int): Int {
val logicalLine = editor.visualToLogicalPosition(VisualPosition(line, 0)).line
return normalizeLine(editor, logicalLine)
}
/**
* Returns the offset of the start of the requested line.
* @param editor The editor
* *
* @param line The logical line to get the start offset for.
* *
* @return 0 if line is &lt 0, file size of line is bigger than file, else the start offset for the line
*/
fun getLineStartOffset(editor: Editor, line: Int): Int {
if (line < 0) {
return 0
} else if (line >= getLineCount(editor)) {
return getFileSize(editor)
} else {
return editor.document.getLineStartOffset(line)
}
}
/**
* Returns the offset of the end of the requested line.
* @param editor The editor
* *
* @param line The logical line to get the end offset for
* *
* @param allowEnd True include newline
* *
* @return 0 if line is &lt 0, file size of line is bigger than file, else the end offset for the line
*/
fun getLineEndOffset(editor: Editor, line: Int, allowEnd: Boolean): Int {
if (line < 0) {
return 0
} else if (line >= getLineCount(editor)) {
return getFileSize(editor, allowEnd)
} else {
return editor.document.getLineEndOffset(line) - if (allowEnd) 0 else 1
}
}
/**
* Ensures that the supplied logical line is within the range 0 (incl) and the number of logical lines in the file
* (excl).
* @param editor The editor
* *
* @param line The logical line number to normalize
* *
* @return The normalized logical line number
*/
fun normalizeLine(editor: Editor, line: Int): Int {
return Math.max(0, Math.min(line, getLineCount(editor) - 1))
}
/**
* Ensures that the supplied offset for the given logical line is within the range for the line. If allowEnd
* is true, the range will allow for the offset to be one past the last character on the line.
* @param editor The editor
* *
* @param line The logical line number
* *
* @param offset The offset to normalize
* *
* @param allowEnd true if the offset can be one past the last character on the line, false if not
* *
* @return The normalized column number
*/
fun normalizeOffset(editor: Editor,
line: Int,
offset: Int,
allowEnd: Boolean): Int {
if (getFileSize(editor, allowEnd) == 0) {
return 0
}
val min = getLineStartOffset(editor, line)
val max = getLineEndOffset(editor, line, allowEnd)
return Math.max(Math.min(offset, max), min)
* @param editor The editor
* *
* @param line The logical line to get the start offset for.
* *
* @return 0 if line is &lt 0, file size of line is bigger than file, else the start offset for the line
*/
fun getLineStartOffset(editor: Editor, line: Int): Int {
if (line < 0) {
return 0
} else if (line >= getLineCount(editor)) {
return getFileSize(editor)
} else {
return editor.document.getLineStartOffset(line)
}
}
/**
* Returns the offset of the end of the requested line.
* @param editor The editor
* *
* @param line The logical line to get the end offset for
* *
* @param allowEnd True include newline
* *
* @return 0 if line is &lt 0, file size of line is bigger than file, else the end offset for the line
*/
fun getLineEndOffset(editor: Editor, line: Int, allowEnd: Boolean): Int {
if (line < 0) {
return 0
} else if (line >= getLineCount(editor)) {
return getFileSize(editor, allowEnd)
} else {
return editor.document.getLineEndOffset(line) - if (allowEnd) 0 else 1
}
}
/**
* Ensures that the supplied logical line is within the range 0 (incl) and the number of logical lines in the file
* (excl).
* @param editor The editor
* *
* @param line The logical line number to normalize
* *
* @return The normalized logical line number
*/
fun normalizeLine(editor: Editor, line: Int): Int {
return Math.max(0, Math.min(line, getLineCount(editor) - 1))
}
/**
* Ensures that the supplied offset for the given logical line is within the range for the line. If allowEnd
* is true, the range will allow for the offset to be one past the last character on the line.
* @param editor The editor
* *
* @param line The logical line number
* *
* @param offset The offset to normalize
* *
* @param allowEnd true if the offset can be one past the last character on the line, false if not
* *
* @return The normalized column number
*/
fun normalizeOffset(editor: Editor,
line: Int,
offset: Int,
allowEnd: Boolean): Int {
if (getFileSize(editor, allowEnd) == 0) {
return 0
}
val min = getLineStartOffset(editor, line)
val max = getLineEndOffset(editor, line, allowEnd)
return Math.max(Math.min(offset, max), min)
}