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

Tags now occupy vertically adjacent whitespace (if available)

This commit is contained in:
breandan 2016-10-20 18:24:36 -02:00
parent 658a29175a
commit dc3db730f0
3 changed files with 109 additions and 31 deletions
src/main/kotlin/com/johnlindquist/acejump

View File

@ -28,6 +28,19 @@ fun getVisibleRange(editor: Editor): Pair<Int, Int> {
return Pair(startOffset, endOffset)
}
fun getPreviousLineLength(editor: Editor, offset: Int): Int {
val pos = editor.offsetToVisualPosition(offset)
if (pos.line - 1 > editor.offsetToVisualPosition(getVisibleRange(editor).first).line)
return getVisualLineLength(editor, pos.line - 1)
return getVisualLineLength(editor, pos.line)
}
fun getNextLineLength(editor: Editor, offset: Int): Int {
val pos = editor.offsetToVisualPosition(offset)
if (pos.line + 1 < editor.offsetToVisualPosition(getVisibleRange(editor).second).line)
return getVisualLineLength(editor, pos.line + 1)
return getVisualLineLength(editor, pos.line)
}
/*
* IdeaVim - A Vim emulator plugin for IntelliJ Idea
@ -193,3 +206,69 @@ fun normalizeOffset(editor: Editor, line: Int, offset: Int, allowEnd: Boolean):
val max = getLineEndOffset(editor, line, allowEnd)
return max(min(offset, max), min)
}
/**
* Gets the number of characters on the specified visual line. This will be different than the number of visual
* characters if there are "real" tabs in the line.
* @param editor The editor
* *
* @param line The visual line within the file
* *
* @return The number of characters in the specified line
*/
fun getVisualLineLength(editor: Editor, line: Int): Int {
return getLineLength(editor, visualLineToLogicalLine(editor, line))
}
/**
* Gets the number of characters on the specified logical line. This will be different than the number of visual
* characters if there are "real" tabs in the line.
* @param editor The editor
* *
* @param line The logical line within the file
* *
* @return The number of characters in the specified line
*/
fun getLineLength(editor: Editor, line: Int): Int {
if (getLineCount(editor) === 0) {
return 0
} else {
return Math.max(0, editor.offsetToLogicalPosition(editor.document.getLineEndOffset(line)).column)
}
}
fun getLengthFromStartToOffset(editor: Editor, offset: Int): Int {
if (getLineCount(editor) === 0) {
return 0
} else {
return Math.max(0, editor.offsetToLogicalPosition(offset).column)
}
}
fun getLeadingCharacterOffset(editor: Editor, line: Int): Int {
return getLeadingCharacterOffset(editor, line, 0)
}
fun getLeadingCharacterOffset(editor: Editor, line: Int, col: Int): Int {
val start = getLineStartOffset(editor, line) + col
val end = getLineEndOffset(editor, line, true)
val chars = editor.document.charsSequence
var pos = end
for (offset in start..end - 1) {
if (offset >= chars.length) {
break
}
if (!Character.isWhitespace(chars[offset])) {
pos = offset
break
}
}
return pos
}

View File

@ -27,7 +27,7 @@ class AceCanvas(val editor: EditorImpl) : JComponent() {
val doubleRectMarginWidth = rectMarginWidth * 2
val fontSpacing = fontHeight * lineSpacing
val rectHOffset = fontSpacing - fontHeight
val rectWidth = fontWidth + doubleRectMarginWidth
val rectWidth = doubleRectMarginWidth
val hOffset = fontHeight - fontSpacing
}

View File

@ -1,7 +1,8 @@
package com.johnlindquist.acejump.ui
import com.intellij.ide.util.EditorHelper
import com.intellij.openapi.editor.impl.EditorImpl
import com.johnlindquist.acejump.search.getPointFromVisualPosition
import com.johnlindquist.acejump.search.*
import java.awt.AlphaComposite
import java.awt.Color
import java.awt.Color.*
@ -13,8 +14,10 @@ class JumpInfo(private val tag: String, var search: String, val index: Int, val
val source: String = window.substring(index, index + tag.length).toLowerCase()
var result: String = window.substring(index, index + search.length)
var offset = index
val line = editor.offsetToVisualPosition(offset).line
var originOffset = editor.offsetToVisualPosition(offset)
var tagOffset = editor.offsetToVisualPosition(offset + search.length)
var trueOffset = offset + search.length
var tagOffset = editor.offsetToVisualPosition(trueOffset)
var tagPoint = getPointFromVisualPosition(editor, originOffset).originalPoint
var srcPoint = getPointFromVisualPosition(editor, originOffset).originalPoint
@ -36,44 +39,40 @@ class JumpInfo(private val tag: String, var search: String, val index: Int, val
val text = renderTag()
val origin = srcPoint
val original = tagPoint
val backgroundColor = yellow//if (text[0] == ' ') Color.YELLOW else colors.first
val foregroundColor = yellow//if (text[0] == ' ') Color.YELLOW else colors.second
original.translate(0, -fbm.hOffset.toInt())
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)
g2d.composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.35.toFloat())
g2d.color = green
if (search.isNotEmpty()) {
g2d.drawRect(origin.x - fbm.rectMarginWidth - 1,
origin.y - fbm.rectHOffset.toInt() - 1,
g2d.fillRect(origin.x - 1,
original.y - fbm.rectHOffset.toInt() - 1,
search.length * fbm.fontWidth, fbm.lineHeight.toInt() + 1)
}
//a slight border for "pop" against the background
g2d.color = backgroundColor
if (text.length == 2) {
g2d.drawRect(original.x - fbm.rectMarginWidth - 1,
original.y - fbm.rectHOffset.toInt() - 1,
fbm.rectWidth + fbm.fontWidth, fbm.lineHeight.toInt() + 1)
} else {
g2d.drawRect(original.x - fbm.rectMarginWidth - 1,
original.y - fbm.rectHOffset.toInt() - 1,
fbm.rectWidth, fbm.lineHeight.toInt() + 1)
var y = original.y
var x = original.x
if (search.isNotEmpty()) {
val lineOffset = getLengthFromStartToOffset(editor, offset + search.length)
val startOfNextLine = getLeadingCharacterOffset(editor, line + 1)
val startOfPrevLine = getLeadingCharacterOffset(editor, line - 1)
val pLineOffset = getLengthFromStartToOffset(editor, startOfPrevLine)
val nLineOffset = getLengthFromStartToOffset(editor, startOfNextLine)
if (getNextLineLength(editor, offset) < lineOffset || nLineOffset > lineOffset) {
y += fbm.lineHeight.toInt()
x -= fbm.fontWidth
} else if (getPreviousLineLength(editor, offset) < lineOffset || pLineOffset > lineOffset) {
y -= fbm.lineHeight.toInt()
x -= fbm.fontWidth
}
}
//the background rectangle
g2d.color = foregroundColor
if (text.length == 2) {
g2d.fillRect(original.x - fbm.rectMarginWidth,
original.y - fbm.rectHOffset.toInt(),
fbm.rectWidth + fbm.fontWidth, fbm.lineHeight.toInt())
} else {
g2d.fillRect(original.x - fbm.rectMarginWidth,
original.y - fbm.rectHOffset.toInt(),
fbm.rectWidth, fbm.lineHeight.toInt())
}
g2d.color = yellow
x += fbm.fontWidth
g2d.fillRect(x - fbm.rectMarginWidth, y - fbm.rectHOffset.toInt(),
fbm.rectWidth + text.length * fbm.fontWidth, fbm.lineHeight.toInt())
//just a touch of alpha
g2d.composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
@ -82,6 +81,6 @@ class JumpInfo(private val tag: String, var search: String, val index: Int, val
//the foreground text
g2d.font = fbm.font
g2d.color = BLACK
g2d.drawString(text.toUpperCase(), original.x, original.y + fbm.fontHeight)
g2d.drawString(text.toUpperCase(), x, y + fbm.fontHeight)
}
}