mirror of
https://github.com/chylex/IntelliJ-AceJump.git
synced 2025-09-15 14:32:08 +02:00
Compare commits
97 Commits
customized
...
vim
Author | SHA1 | Date | |
---|---|---|---|
568128f474
|
|||
018d29007c
|
|||
cb814e0999
|
|||
1fc06e8120
|
|||
841f2fd125
|
|||
6c8f19e311
|
|||
ce4f3b5f03
|
|||
46f42c88eb
|
|||
43dfec940e
|
|||
abe06ec7be
|
|||
3fc3cbc7f8
|
|||
5979579042
|
|||
ea61d49aa6
|
|||
dbc6db108d
|
|||
01c38df82a
|
|||
a3a86cf447
|
|||
59fbd4e19c
|
|||
6e08d56cdf
|
|||
9a14fb87e3
|
|||
d22bcc220e
|
|||
1f76a8bd25
|
|||
2e31ddd504
|
|||
47a34f6f14
|
|||
575283b2be
|
|||
8bb34d7f75
|
|||
8d1ef031d2
|
|||
a9375ec414
|
|||
b42112cc9e
|
|||
8e42c82821
|
|||
48bcf9f284
|
|||
2681d9901f
|
|||
b13d629046
|
|||
92dcd033fb
|
|||
fa3505b850
|
|||
dacac684f0
|
|||
e627db3a24
|
|||
084d729baa
|
|||
e01edccb5b
|
|||
655ccde60e
|
|||
eb2ea55fb8
|
|||
a6381a2a34
|
|||
e005983d4c
|
|||
8f4d9748ad
|
|||
76c6458ef4
|
|||
2f53e9da6d
|
|||
100001ffca
|
|||
184896a6cb
|
|||
8563400946
|
|||
a07c61a384
|
|||
e072003c5c
|
|||
8062cf5cab
|
|||
9151ee376c
|
|||
19ce1c69fd
|
|||
f2a053505c
|
|||
647cfb14f1
|
|||
c31ba60909
|
|||
9c60a8a4ba
|
|||
1e8b7d7963
|
|||
9157ce19b0
|
|||
9a435feccc
|
|||
c1feb891e4
|
|||
0b6ba62cda
|
|||
dfd5b122a0
|
|||
f2400d134d
|
|||
98997b4d86
|
|||
1e172e9c49
|
|||
01b37c878e | |||
1630c706a9 | |||
041a130c5f | |||
7561bfde36 | |||
724e469f21 | |||
bce9a5f636 | |||
bfe0aa536e | |||
2ca21b8423 | |||
430bcf6883 | |||
092650af81 | |||
2009123114 | |||
de19c0e0b8 | |||
5021a07fb9 | |||
1180547b06 | |||
d2ae335de1 | |||
324296b55a | |||
a4ebeffe05 | |||
7716242e55 | |||
eb70ef5097 | |||
4d5a0b3e6a | |||
eb1bbb2e03 | |||
74a65a6510 | |||
2c08494a71 | |||
b61abee04d | |||
![]() |
89af38422a | ||
![]() |
80f25c39b2 | ||
![]() |
8e09ab83d7 | ||
![]() |
a9df9b7970 | ||
![]() |
b1d69bf251 | ||
![]() |
e37e1d92b3 | ||
![]() |
c5008ab26e |
@@ -1,7 +1,7 @@
|
|||||||
[*]
|
[*]
|
||||||
charset=utf-8
|
charset = utf-8
|
||||||
end_of_line=lf
|
end_of_line = lf
|
||||||
insert_final_newline=false
|
insert_final_newline = true
|
||||||
indent_style=space
|
indent_style = space
|
||||||
indent_size=2
|
indent_size = 2
|
||||||
max_line_length=80
|
max_line_length = 140
|
1
.gitattributes
vendored
Normal file
1
.gitattributes
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
* text=auto eol=lf
|
24
.gitignore
vendored
24
.gitignore
vendored
@@ -1,21 +1,5 @@
|
|||||||
### JetBrains template
|
/.idea/*
|
||||||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio
|
!/.idea/runConfigurations
|
||||||
|
|
||||||
## Directory-based project format:
|
/.gradle/
|
||||||
.idea
|
/build/
|
||||||
*.iml
|
|
||||||
|
|
||||||
## Plugin-specific files:
|
|
||||||
# IntelliJ
|
|
||||||
/out/
|
|
||||||
|
|
||||||
### Gradle template
|
|
||||||
.gradle
|
|
||||||
build/
|
|
||||||
|
|
||||||
# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
|
|
||||||
!gradle-wrapper.jar
|
|
||||||
gradle.properties
|
|
||||||
|
|
||||||
# Mac OS
|
|
||||||
.DS_Store
|
|
||||||
|
15
CHANGES.md
15
CHANGES.md
@@ -1,8 +1,17 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
### 3.6.4
|
### 3.7
|
||||||
|
|
||||||
- Improvements to tag latency. Thanks to @chylex for [the PR](https://github.com/acejump/AceJump/pull/339)!
|
- Improvements to tag latency
|
||||||
|
- Redesign settings panel
|
||||||
|
- Add missing configuration for definition mode color
|
||||||
|
- Adds option to switch between straight and rounded tag corners
|
||||||
|
- Adds option to only consider visible area
|
||||||
|
- Add customizable jump mode cycling
|
||||||
|
- Jump-to-End mode jumps to the end of a word
|
||||||
|
- Fixes toggle keys not resetting mode when pressed twice
|
||||||
|
- Increase limit for what is considered a large file
|
||||||
|
- Thanks to @chylex for [all the PRs](https://github.com/acejump/AceJump/pulls?q=is%3Apr+author%3Achylex)!
|
||||||
|
|
||||||
### 3.6.3
|
### 3.6.3
|
||||||
|
|
||||||
@@ -304,4 +313,4 @@
|
|||||||
|
|
||||||
### 1.0.0
|
### 1.0.0
|
||||||
|
|
||||||
- Cleaned up code base for release
|
- Cleaned up code base for release
|
||||||
|
@@ -171,7 +171,7 @@ The following individuals have significantly improved AceJump through their cont
|
|||||||
* [John Lindquist](https://github.com/johnlindquist) for creating AceJump and supporting it for many years.
|
* [John Lindquist](https://github.com/johnlindquist) for creating AceJump and supporting it for many years.
|
||||||
* [Breandan Considine](https://github.com/breandan) for maintaining the project and adding some new features.
|
* [Breandan Considine](https://github.com/breandan) for maintaining the project and adding some new features.
|
||||||
* [Alex Plate](https://github.com/AlexPl292) for submitting [several PRs](https://github.com/acejump/AceJump/pulls?q=is%3Apr+author%3AAlexPl292).
|
* [Alex Plate](https://github.com/AlexPl292) for submitting [several PRs](https://github.com/acejump/AceJump/pulls?q=is%3Apr+author%3AAlexPl292).
|
||||||
* [Daniel Chýlek](https://github.com/chylex) for several [performance optimizations](https://github.com/acejump/AceJump/pull/339).
|
* [Daniel Chýlek](https://github.com/chylex) for several [performance optimizations](https://github.com/acejump/AceJump/pulls?q=is%3Apr+author%3Achylex).
|
||||||
* [Sven Speckmaier](https://github.com/svensp) for [improving](https://github.com/acejump/AceJump/pull/214) search latency.
|
* [Sven Speckmaier](https://github.com/svensp) for [improving](https://github.com/acejump/AceJump/pull/214) search latency.
|
||||||
* [Stefan Monnier](https://www.iro.umontreal.ca/~monnier/) for algorithmic advice and maintaining Emacs for several years.
|
* [Stefan Monnier](https://www.iro.umontreal.ca/~monnier/) for algorithmic advice and maintaining Emacs for several years.
|
||||||
* [Fool's Mate](https://www.fools-mate.de/) for the [icon](https://github.com/acejump/AceJump/issues/313) and graphic design.
|
* [Fool's Mate](https://www.fools-mate.de/) for the [icon](https://github.com/acejump/AceJump/issues/313) and graphic design.
|
||||||
|
108
build.gradle.kts
108
build.gradle.kts
@@ -1,66 +1,54 @@
|
|||||||
import org.jetbrains.changelog.closure
|
@file:Suppress("ConvertLambdaToReference")
|
||||||
import org.jetbrains.intellij.tasks.*
|
|
||||||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
||||||
import org.jetbrains.intellij.tasks.PatchPluginXmlTask
|
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
idea apply true
|
kotlin("jvm") version "1.9.10"
|
||||||
kotlin("jvm") version "1.3.72"
|
id("org.jetbrains.intellij") version "1.17.3"
|
||||||
id("org.jetbrains.intellij") version "0.6.4"
|
|
||||||
id("org.jetbrains.changelog") version "0.6.2"
|
|
||||||
id("com.github.ben-manes.versions") version "0.36.0"
|
|
||||||
}
|
|
||||||
|
|
||||||
tasks {
|
|
||||||
withType<KotlinCompile> {
|
|
||||||
kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8.toString()
|
|
||||||
kotlinOptions.freeCompilerArgs += "-progressive"
|
|
||||||
}
|
|
||||||
|
|
||||||
named<Zip>("buildPlugin") {
|
|
||||||
dependsOn("test")
|
|
||||||
archiveFileName.set("AceJump.zip")
|
|
||||||
}
|
|
||||||
|
|
||||||
withType<RunIdeTask> {
|
|
||||||
dependsOn("test")
|
|
||||||
findProperty("luginDev")?.let { args = listOf(projectDir.absolutePath) }
|
|
||||||
}
|
|
||||||
|
|
||||||
withType<PublishTask> {
|
|
||||||
val intellijPublishToken: String? by project
|
|
||||||
token(intellijPublishToken)
|
|
||||||
}
|
|
||||||
|
|
||||||
withType<PatchPluginXmlTask> {
|
|
||||||
sinceBuild("201.6668.0")
|
|
||||||
changeNotes({ changelog.getLatest().toHTML() })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
changelog {
|
|
||||||
path = "${project.projectDir}/CHANGES.md"
|
|
||||||
header = closure { "${project.version}" }
|
|
||||||
}
|
|
||||||
|
|
||||||
dependencies {
|
|
||||||
// gradle-intellij-plugin doesn't attach sources properly for Kotlin :(
|
|
||||||
compileOnly(kotlin("stdlib-jdk8"))
|
|
||||||
// https://github.com/promeG/TinyPinyin
|
|
||||||
implementation("com.github.promeg:tinypinyin:2.0.3")
|
|
||||||
}
|
|
||||||
|
|
||||||
repositories {
|
|
||||||
mavenCentral()
|
|
||||||
jcenter()
|
|
||||||
}
|
|
||||||
|
|
||||||
intellij {
|
|
||||||
version = "2020.2"
|
|
||||||
pluginName = "AceJump"
|
|
||||||
updateSinceUntilBuild = false
|
|
||||||
setPlugins("java")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
group = "org.acejump"
|
group = "org.acejump"
|
||||||
version = "3.6.4"
|
version = "chylex-26"
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
|
intellij {
|
||||||
|
version.set("2024.2")
|
||||||
|
updateSinceUntilBuild.set(false)
|
||||||
|
|
||||||
|
plugins.add("IdeaVIM:chylex-41")
|
||||||
|
plugins.add("com.intellij.classic.ui:242.20224.159")
|
||||||
|
|
||||||
|
pluginsRepositories {
|
||||||
|
custom("https://intellij.chylex.com")
|
||||||
|
marketplace()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
kotlin {
|
||||||
|
jvmToolchain(17)
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
testImplementation("org.junit.jupiter:junit-jupiter:5.9.2")
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.patchPluginXml {
|
||||||
|
sinceBuild.set("242")
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.buildSearchableOptions {
|
||||||
|
enabled = false
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.test {
|
||||||
|
useJUnitPlatform()
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.withType<KotlinCompile> {
|
||||||
|
kotlinOptions.freeCompilerArgs = listOf(
|
||||||
|
"-Xjvm-default=all"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
1
gradle.properties
Normal file
1
gradle.properties
Normal file
@@ -0,0 +1 @@
|
|||||||
|
kotlin.stdlib.default.dependency=false
|
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
Binary file not shown.
4
gradle/wrapper/gradle-wrapper.properties
vendored
4
gradle/wrapper/gradle-wrapper.properties
vendored
@@ -1,5 +1,7 @@
|
|||||||
distributionBase=GRADLE_USER_HOME
|
distributionBase=GRADLE_USER_HOME
|
||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip
|
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
|
||||||
|
networkTimeout=10000
|
||||||
|
validateDistributionUrl=true
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
zipStorePath=wrapper/dists
|
zipStorePath=wrapper/dists
|
||||||
|
294
gradlew
vendored
294
gradlew
vendored
@@ -1,7 +1,7 @@
|
|||||||
#!/usr/bin/env sh
|
#!/bin/sh
|
||||||
|
|
||||||
#
|
#
|
||||||
# Copyright 2015 the original author or authors.
|
# Copyright © 2015-2021 the original authors.
|
||||||
#
|
#
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
# you may not use this file except in compliance with the License.
|
# you may not use this file except in compliance with the License.
|
||||||
@@ -17,78 +17,111 @@
|
|||||||
#
|
#
|
||||||
|
|
||||||
##############################################################################
|
##############################################################################
|
||||||
##
|
#
|
||||||
## Gradle start up script for UN*X
|
# Gradle start up script for POSIX generated by Gradle.
|
||||||
##
|
#
|
||||||
|
# Important for running:
|
||||||
|
#
|
||||||
|
# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
|
||||||
|
# noncompliant, but you have some other compliant shell such as ksh or
|
||||||
|
# bash, then to run this script, type that shell name before the whole
|
||||||
|
# command line, like:
|
||||||
|
#
|
||||||
|
# ksh Gradle
|
||||||
|
#
|
||||||
|
# Busybox and similar reduced shells will NOT work, because this script
|
||||||
|
# requires all of these POSIX shell features:
|
||||||
|
# * functions;
|
||||||
|
# * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
|
||||||
|
# «${var#prefix}», «${var%suffix}», and «$( cmd )»;
|
||||||
|
# * compound commands having a testable exit status, especially «case»;
|
||||||
|
# * various built-in commands including «command», «set», and «ulimit».
|
||||||
|
#
|
||||||
|
# Important for patching:
|
||||||
|
#
|
||||||
|
# (2) This script targets any POSIX shell, so it avoids extensions provided
|
||||||
|
# by Bash, Ksh, etc; in particular arrays are avoided.
|
||||||
|
#
|
||||||
|
# The "traditional" practice of packing multiple parameters into a
|
||||||
|
# space-separated string is a well documented source of bugs and security
|
||||||
|
# problems, so this is (mostly) avoided, by progressively accumulating
|
||||||
|
# options in "$@", and eventually passing that to Java.
|
||||||
|
#
|
||||||
|
# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
|
||||||
|
# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
|
||||||
|
# see the in-line comments for details.
|
||||||
|
#
|
||||||
|
# There are tweaks for specific operating systems such as AIX, CygWin,
|
||||||
|
# Darwin, MinGW, and NonStop.
|
||||||
|
#
|
||||||
|
# (3) This script is generated from the Groovy template
|
||||||
|
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
|
||||||
|
# within the Gradle project.
|
||||||
|
#
|
||||||
|
# You can find Gradle at https://github.com/gradle/gradle/.
|
||||||
|
#
|
||||||
##############################################################################
|
##############################################################################
|
||||||
|
|
||||||
# Attempt to set APP_HOME
|
# Attempt to set APP_HOME
|
||||||
|
|
||||||
# Resolve links: $0 may be a link
|
# Resolve links: $0 may be a link
|
||||||
PRG="$0"
|
app_path=$0
|
||||||
# Need this for relative symlinks.
|
|
||||||
while [ -h "$PRG" ] ; do
|
# Need this for daisy-chained symlinks.
|
||||||
ls=`ls -ld "$PRG"`
|
while
|
||||||
link=`expr "$ls" : '.*-> \(.*\)$'`
|
APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
|
||||||
if expr "$link" : '/.*' > /dev/null; then
|
[ -h "$app_path" ]
|
||||||
PRG="$link"
|
do
|
||||||
else
|
ls=$( ls -ld "$app_path" )
|
||||||
PRG=`dirname "$PRG"`"/$link"
|
link=${ls#*' -> '}
|
||||||
fi
|
case $link in #(
|
||||||
|
/*) app_path=$link ;; #(
|
||||||
|
*) app_path=$APP_HOME$link ;;
|
||||||
|
esac
|
||||||
done
|
done
|
||||||
SAVED="`pwd`"
|
|
||||||
cd "`dirname \"$PRG\"`/" >/dev/null
|
|
||||||
APP_HOME="`pwd -P`"
|
|
||||||
cd "$SAVED" >/dev/null
|
|
||||||
|
|
||||||
APP_NAME="Gradle"
|
# This is normally unused
|
||||||
APP_BASE_NAME=`basename "$0"`
|
# shellcheck disable=SC2034
|
||||||
|
APP_BASE_NAME=${0##*/}
|
||||||
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
|
||||||
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit
|
||||||
|
|
||||||
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||||
MAX_FD="maximum"
|
MAX_FD=maximum
|
||||||
|
|
||||||
warn () {
|
warn () {
|
||||||
echo "$*"
|
echo "$*"
|
||||||
}
|
} >&2
|
||||||
|
|
||||||
die () {
|
die () {
|
||||||
echo
|
echo
|
||||||
echo "$*"
|
echo "$*"
|
||||||
echo
|
echo
|
||||||
exit 1
|
exit 1
|
||||||
}
|
} >&2
|
||||||
|
|
||||||
# OS specific support (must be 'true' or 'false').
|
# OS specific support (must be 'true' or 'false').
|
||||||
cygwin=false
|
cygwin=false
|
||||||
msys=false
|
msys=false
|
||||||
darwin=false
|
darwin=false
|
||||||
nonstop=false
|
nonstop=false
|
||||||
case "`uname`" in
|
case "$( uname )" in #(
|
||||||
CYGWIN* )
|
CYGWIN* ) cygwin=true ;; #(
|
||||||
cygwin=true
|
Darwin* ) darwin=true ;; #(
|
||||||
;;
|
MSYS* | MINGW* ) msys=true ;; #(
|
||||||
Darwin* )
|
NONSTOP* ) nonstop=true ;;
|
||||||
darwin=true
|
|
||||||
;;
|
|
||||||
MINGW* )
|
|
||||||
msys=true
|
|
||||||
;;
|
|
||||||
NONSTOP* )
|
|
||||||
nonstop=true
|
|
||||||
;;
|
|
||||||
esac
|
esac
|
||||||
|
|
||||||
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
|
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
|
||||||
|
|
||||||
|
|
||||||
# Determine the Java command to use to start the JVM.
|
# Determine the Java command to use to start the JVM.
|
||||||
if [ -n "$JAVA_HOME" ] ; then
|
if [ -n "$JAVA_HOME" ] ; then
|
||||||
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
|
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
|
||||||
# IBM's JDK on AIX uses strange locations for the executables
|
# IBM's JDK on AIX uses strange locations for the executables
|
||||||
JAVACMD="$JAVA_HOME/jre/sh/java"
|
JAVACMD=$JAVA_HOME/jre/sh/java
|
||||||
else
|
else
|
||||||
JAVACMD="$JAVA_HOME/bin/java"
|
JAVACMD=$JAVA_HOME/bin/java
|
||||||
fi
|
fi
|
||||||
if [ ! -x "$JAVACMD" ] ; then
|
if [ ! -x "$JAVACMD" ] ; then
|
||||||
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
|
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
|
||||||
@@ -97,87 +130,120 @@ Please set the JAVA_HOME variable in your environment to match the
|
|||||||
location of your Java installation."
|
location of your Java installation."
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
JAVACMD="java"
|
JAVACMD=java
|
||||||
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
if ! command -v java >/dev/null 2>&1
|
||||||
|
then
|
||||||
|
die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||||
|
|
||||||
Please set the JAVA_HOME variable in your environment to match the
|
Please set the JAVA_HOME variable in your environment to match the
|
||||||
location of your Java installation."
|
location of your Java installation."
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Increase the maximum file descriptors if we can.
|
# Increase the maximum file descriptors if we can.
|
||||||
if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
|
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
|
||||||
MAX_FD_LIMIT=`ulimit -H -n`
|
case $MAX_FD in #(
|
||||||
if [ $? -eq 0 ] ; then
|
max*)
|
||||||
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
|
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
|
||||||
MAX_FD="$MAX_FD_LIMIT"
|
# shellcheck disable=SC2039,SC3045
|
||||||
fi
|
MAX_FD=$( ulimit -H -n ) ||
|
||||||
ulimit -n $MAX_FD
|
warn "Could not query maximum file descriptor limit"
|
||||||
if [ $? -ne 0 ] ; then
|
esac
|
||||||
warn "Could not set maximum file descriptor limit: $MAX_FD"
|
case $MAX_FD in #(
|
||||||
fi
|
'' | soft) :;; #(
|
||||||
else
|
*)
|
||||||
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
|
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
|
||||||
fi
|
# shellcheck disable=SC2039,SC3045
|
||||||
fi
|
ulimit -n "$MAX_FD" ||
|
||||||
|
warn "Could not set maximum file descriptor limit to $MAX_FD"
|
||||||
# For Darwin, add options to specify how the application appears in the dock
|
|
||||||
if $darwin; then
|
|
||||||
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
|
|
||||||
fi
|
|
||||||
|
|
||||||
# For Cygwin or MSYS, switch paths to Windows format before running java
|
|
||||||
if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
|
|
||||||
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
|
|
||||||
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
|
|
||||||
JAVACMD=`cygpath --unix "$JAVACMD"`
|
|
||||||
|
|
||||||
# We build the pattern for arguments to be converted via cygpath
|
|
||||||
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
|
|
||||||
SEP=""
|
|
||||||
for dir in $ROOTDIRSRAW ; do
|
|
||||||
ROOTDIRS="$ROOTDIRS$SEP$dir"
|
|
||||||
SEP="|"
|
|
||||||
done
|
|
||||||
OURCYGPATTERN="(^($ROOTDIRS))"
|
|
||||||
# Add a user-defined pattern to the cygpath arguments
|
|
||||||
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
|
|
||||||
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
|
|
||||||
fi
|
|
||||||
# Now convert the arguments - kludge to limit ourselves to /bin/sh
|
|
||||||
i=0
|
|
||||||
for arg in "$@" ; do
|
|
||||||
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
|
|
||||||
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
|
|
||||||
|
|
||||||
if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
|
|
||||||
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
|
|
||||||
else
|
|
||||||
eval `echo args$i`="\"$arg\""
|
|
||||||
fi
|
|
||||||
i=`expr $i + 1`
|
|
||||||
done
|
|
||||||
case $i in
|
|
||||||
0) set -- ;;
|
|
||||||
1) set -- "$args0" ;;
|
|
||||||
2) set -- "$args0" "$args1" ;;
|
|
||||||
3) set -- "$args0" "$args1" "$args2" ;;
|
|
||||||
4) set -- "$args0" "$args1" "$args2" "$args3" ;;
|
|
||||||
5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
|
|
||||||
6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
|
|
||||||
7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
|
|
||||||
8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
|
|
||||||
9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
|
|
||||||
esac
|
esac
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Escape application args
|
# Collect all arguments for the java command, stacking in reverse order:
|
||||||
save () {
|
# * args from the command line
|
||||||
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
|
# * the main class name
|
||||||
echo " "
|
# * -classpath
|
||||||
}
|
# * -D...appname settings
|
||||||
APP_ARGS=`save "$@"`
|
# * --module-path (only if needed)
|
||||||
|
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
|
||||||
|
|
||||||
# Collect all arguments for the java command, following the shell quoting and substitution rules
|
# For Cygwin or MSYS, switch paths to Windows format before running java
|
||||||
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
|
if "$cygwin" || "$msys" ; then
|
||||||
|
APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
|
||||||
|
CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
|
||||||
|
|
||||||
|
JAVACMD=$( cygpath --unix "$JAVACMD" )
|
||||||
|
|
||||||
|
# Now convert the arguments - kludge to limit ourselves to /bin/sh
|
||||||
|
for arg do
|
||||||
|
if
|
||||||
|
case $arg in #(
|
||||||
|
-*) false ;; # don't mess with options #(
|
||||||
|
/?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
|
||||||
|
[ -e "$t" ] ;; #(
|
||||||
|
*) false ;;
|
||||||
|
esac
|
||||||
|
then
|
||||||
|
arg=$( cygpath --path --ignore --mixed "$arg" )
|
||||||
|
fi
|
||||||
|
# Roll the args list around exactly as many times as the number of
|
||||||
|
# args, so each arg winds up back in the position where it started, but
|
||||||
|
# possibly modified.
|
||||||
|
#
|
||||||
|
# NB: a `for` loop captures its iteration list before it begins, so
|
||||||
|
# changing the positional parameters here affects neither the number of
|
||||||
|
# iterations, nor the values presented in `arg`.
|
||||||
|
shift # remove old arg
|
||||||
|
set -- "$@" "$arg" # push replacement arg
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||||
|
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
||||||
|
|
||||||
|
# Collect all arguments for the java command:
|
||||||
|
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
|
||||||
|
# and any embedded shellness will be escaped.
|
||||||
|
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
|
||||||
|
# treated as '${Hostname}' itself on the command line.
|
||||||
|
|
||||||
|
set -- \
|
||||||
|
"-Dorg.gradle.appname=$APP_BASE_NAME" \
|
||||||
|
-classpath "$CLASSPATH" \
|
||||||
|
org.gradle.wrapper.GradleWrapperMain \
|
||||||
|
"$@"
|
||||||
|
|
||||||
|
# Stop when "xargs" is not available.
|
||||||
|
if ! command -v xargs >/dev/null 2>&1
|
||||||
|
then
|
||||||
|
die "xargs is not available"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Use "xargs" to parse quoted args.
|
||||||
|
#
|
||||||
|
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
|
||||||
|
#
|
||||||
|
# In Bash we could simply go:
|
||||||
|
#
|
||||||
|
# readarray ARGS < <( xargs -n1 <<<"$var" ) &&
|
||||||
|
# set -- "${ARGS[@]}" "$@"
|
||||||
|
#
|
||||||
|
# but POSIX shell has neither arrays nor command substitution, so instead we
|
||||||
|
# post-process each arg (as a line of input to sed) to backslash-escape any
|
||||||
|
# character that might be a shell metacharacter, then use eval to reverse
|
||||||
|
# that process (while maintaining the separation between arguments), and wrap
|
||||||
|
# the whole thing up as a single "set" statement.
|
||||||
|
#
|
||||||
|
# This will of course break if any of these variables contains a newline or
|
||||||
|
# an unmatched quote.
|
||||||
|
#
|
||||||
|
|
||||||
|
eval "set -- $(
|
||||||
|
printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
|
||||||
|
xargs -n1 |
|
||||||
|
sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
|
||||||
|
tr '\n' ' '
|
||||||
|
)" '"$@"'
|
||||||
|
|
||||||
exec "$JAVACMD" "$@"
|
exec "$JAVACMD" "$@"
|
||||||
|
35
gradlew.bat
vendored
35
gradlew.bat
vendored
@@ -14,7 +14,7 @@
|
|||||||
@rem limitations under the License.
|
@rem limitations under the License.
|
||||||
@rem
|
@rem
|
||||||
|
|
||||||
@if "%DEBUG%" == "" @echo off
|
@if "%DEBUG%"=="" @echo off
|
||||||
@rem ##########################################################################
|
@rem ##########################################################################
|
||||||
@rem
|
@rem
|
||||||
@rem Gradle startup script for Windows
|
@rem Gradle startup script for Windows
|
||||||
@@ -25,7 +25,8 @@
|
|||||||
if "%OS%"=="Windows_NT" setlocal
|
if "%OS%"=="Windows_NT" setlocal
|
||||||
|
|
||||||
set DIRNAME=%~dp0
|
set DIRNAME=%~dp0
|
||||||
if "%DIRNAME%" == "" set DIRNAME=.
|
if "%DIRNAME%"=="" set DIRNAME=.
|
||||||
|
@rem This is normally unused
|
||||||
set APP_BASE_NAME=%~n0
|
set APP_BASE_NAME=%~n0
|
||||||
set APP_HOME=%DIRNAME%
|
set APP_HOME=%DIRNAME%
|
||||||
|
|
||||||
@@ -40,7 +41,7 @@ if defined JAVA_HOME goto findJavaFromJavaHome
|
|||||||
|
|
||||||
set JAVA_EXE=java.exe
|
set JAVA_EXE=java.exe
|
||||||
%JAVA_EXE% -version >NUL 2>&1
|
%JAVA_EXE% -version >NUL 2>&1
|
||||||
if "%ERRORLEVEL%" == "0" goto init
|
if %ERRORLEVEL% equ 0 goto execute
|
||||||
|
|
||||||
echo.
|
echo.
|
||||||
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||||
@@ -54,7 +55,7 @@ goto fail
|
|||||||
set JAVA_HOME=%JAVA_HOME:"=%
|
set JAVA_HOME=%JAVA_HOME:"=%
|
||||||
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
||||||
|
|
||||||
if exist "%JAVA_EXE%" goto init
|
if exist "%JAVA_EXE%" goto execute
|
||||||
|
|
||||||
echo.
|
echo.
|
||||||
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
|
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
|
||||||
@@ -64,38 +65,26 @@ echo location of your Java installation.
|
|||||||
|
|
||||||
goto fail
|
goto fail
|
||||||
|
|
||||||
:init
|
|
||||||
@rem Get command-line arguments, handling Windows variants
|
|
||||||
|
|
||||||
if not "%OS%" == "Windows_NT" goto win9xME_args
|
|
||||||
|
|
||||||
:win9xME_args
|
|
||||||
@rem Slurp the command line arguments.
|
|
||||||
set CMD_LINE_ARGS=
|
|
||||||
set _SKIP=2
|
|
||||||
|
|
||||||
:win9xME_args_slurp
|
|
||||||
if "x%~1" == "x" goto execute
|
|
||||||
|
|
||||||
set CMD_LINE_ARGS=%*
|
|
||||||
|
|
||||||
:execute
|
:execute
|
||||||
@rem Setup the command line
|
@rem Setup the command line
|
||||||
|
|
||||||
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
|
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
|
||||||
|
|
||||||
|
|
||||||
@rem Execute Gradle
|
@rem Execute Gradle
|
||||||
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
|
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
|
||||||
|
|
||||||
:end
|
:end
|
||||||
@rem End local scope for the variables with windows NT shell
|
@rem End local scope for the variables with windows NT shell
|
||||||
if "%ERRORLEVEL%"=="0" goto mainEnd
|
if %ERRORLEVEL% equ 0 goto mainEnd
|
||||||
|
|
||||||
:fail
|
:fail
|
||||||
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
|
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
|
||||||
rem the _cmd.exe /c_ return code!
|
rem the _cmd.exe /c_ return code!
|
||||||
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
|
set EXIT_CODE=%ERRORLEVEL%
|
||||||
exit /b 1
|
if %EXIT_CODE% equ 0 set EXIT_CODE=1
|
||||||
|
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
|
||||||
|
exit /b %EXIT_CODE%
|
||||||
|
|
||||||
:mainEnd
|
:mainEnd
|
||||||
if "%OS%"=="Windows_NT" endlocal
|
if "%OS%"=="Windows_NT" endlocal
|
||||||
|
13
src/main/java/org/acejump/action/AceVimUtil.java
Normal file
13
src/main/java/org/acejump/action/AceVimUtil.java
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package org.acejump.action;
|
||||||
|
|
||||||
|
import com.maddyhome.idea.vim.VimPlugin;
|
||||||
|
import com.maddyhome.idea.vim.api.VimEditor;
|
||||||
|
import com.maddyhome.idea.vim.state.mode.SelectionType;
|
||||||
|
|
||||||
|
final class AceVimUtil {
|
||||||
|
private AceVimUtil() {}
|
||||||
|
|
||||||
|
public static void enterVisualMode(final VimEditor vim, final SelectionType mode) {
|
||||||
|
VimPlugin.getVisualMotion().enterVisualMode(vim, mode);
|
||||||
|
}
|
||||||
|
}
|
61
src/main/kotlin/org/acejump/AceUtil.kt
Normal file
61
src/main/kotlin/org/acejump/AceUtil.kt
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
package org.acejump
|
||||||
|
|
||||||
|
import com.intellij.openapi.editor.Editor
|
||||||
|
import com.intellij.openapi.fileEditor.TextEditor
|
||||||
|
import com.intellij.openapi.fileEditor.ex.FileEditorManagerEx
|
||||||
|
import com.intellij.openapi.project.Project
|
||||||
|
import com.intellij.util.IncorrectOperationException
|
||||||
|
import it.unimi.dsi.fastutil.ints.IntArrayList
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an immutable version of the currently edited document.
|
||||||
|
*/
|
||||||
|
val Editor.immutableText
|
||||||
|
get() = this.document.immutableCharSequence
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns all open editors in the project.
|
||||||
|
*/
|
||||||
|
val Project.openEditors: List<Editor>
|
||||||
|
get() {
|
||||||
|
return try {
|
||||||
|
FileEditorManagerEx.getInstanceEx(this)
|
||||||
|
.splitters
|
||||||
|
.getSelectedEditors()
|
||||||
|
.mapNotNull { (it as? TextEditor)?.editor }
|
||||||
|
} catch (e: IncorrectOperationException) {
|
||||||
|
emptyList()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if [this] contains [otherText] at the specified offset.
|
||||||
|
*/
|
||||||
|
fun CharSequence.matchesAt(selfOffset: Int, otherText: String, ignoreCase: Boolean): Boolean {
|
||||||
|
return this.regionMatches(selfOffset, otherText, 0, otherText.length, ignoreCase)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates the length of a common prefix in [this] starting at index [selfOffset], and [otherText] starting at index 0.
|
||||||
|
*/
|
||||||
|
fun CharSequence.countMatchingCharacters(selfOffset: Int, otherText: String): Int {
|
||||||
|
var i = 0
|
||||||
|
var o = selfOffset + i
|
||||||
|
|
||||||
|
while (i < otherText.length && o < this.length && otherText[i].equals(this[o], ignoreCase = true)) {
|
||||||
|
i++
|
||||||
|
o++
|
||||||
|
}
|
||||||
|
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
|
||||||
|
fun MutableMap<Editor, IntArrayList>.clone(): MutableMap<Editor, IntArrayList> {
|
||||||
|
val clone = HashMap<Editor, IntArrayList>(size)
|
||||||
|
|
||||||
|
for ((editor, offsets) in this) {
|
||||||
|
clone[editor] = offsets.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
return clone
|
||||||
|
}
|
44
src/main/kotlin/org/acejump/action/AceEditorAction.kt
Normal file
44
src/main/kotlin/org/acejump/action/AceEditorAction.kt
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
package org.acejump.action
|
||||||
|
|
||||||
|
import com.intellij.openapi.actionSystem.DataContext
|
||||||
|
import com.intellij.openapi.editor.Caret
|
||||||
|
import com.intellij.openapi.editor.Editor
|
||||||
|
import com.intellij.openapi.editor.actionSystem.EditorActionHandler
|
||||||
|
import org.acejump.session.Session
|
||||||
|
import org.acejump.session.SessionManager
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base class for keyboard-activated overrides of existing editor actions, that have a different meaning during an AceJump [Session].
|
||||||
|
*/
|
||||||
|
abstract class AceEditorAction(private val originalHandler: EditorActionHandler) : EditorActionHandler() {
|
||||||
|
final override fun isEnabledForCaret(editor: Editor, caret: Caret, dataContext: DataContext?): Boolean {
|
||||||
|
return SessionManager[editor] != null || originalHandler.isEnabled(editor, caret, dataContext)
|
||||||
|
}
|
||||||
|
|
||||||
|
final override fun doExecute(editor: Editor, caret: Caret?, dataContext: DataContext?) {
|
||||||
|
val session = SessionManager[editor]
|
||||||
|
|
||||||
|
if (session != null) {
|
||||||
|
run(session)
|
||||||
|
}
|
||||||
|
else if (originalHandler.isEnabled(editor, caret, dataContext)) {
|
||||||
|
originalHandler.execute(editor, caret, dataContext)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract fun run(session: Session)
|
||||||
|
|
||||||
|
// Actions
|
||||||
|
|
||||||
|
class Reset(originalHandler: EditorActionHandler) : AceEditorAction(originalHandler) {
|
||||||
|
override fun run(session: Session) = session.end()
|
||||||
|
}
|
||||||
|
|
||||||
|
class ClearSearch(originalHandler: EditorActionHandler) : AceEditorAction(originalHandler) {
|
||||||
|
override fun run(session: Session) = session.restart()
|
||||||
|
}
|
||||||
|
|
||||||
|
class TagImmediately(originalHandler: EditorActionHandler) : AceEditorAction(originalHandler) {
|
||||||
|
override fun run(session: Session) = session.tagImmediately()
|
||||||
|
}
|
||||||
|
}
|
99
src/main/kotlin/org/acejump/action/AceTagAction.kt
Normal file
99
src/main/kotlin/org/acejump/action/AceTagAction.kt
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
package org.acejump.action
|
||||||
|
|
||||||
|
import com.intellij.openapi.actionSystem.ActionManager
|
||||||
|
import com.intellij.openapi.actionSystem.IdeActions
|
||||||
|
import com.intellij.openapi.application.ApplicationManager
|
||||||
|
import com.intellij.openapi.command.CommandProcessor
|
||||||
|
import com.intellij.openapi.command.UndoConfirmationPolicy
|
||||||
|
import com.intellij.openapi.editor.Document
|
||||||
|
import com.intellij.openapi.editor.Editor
|
||||||
|
import com.intellij.openapi.editor.actionSystem.DocCommandGroupId
|
||||||
|
import com.intellij.openapi.fileEditor.TextEditor
|
||||||
|
import com.intellij.openapi.fileEditor.ex.FileEditorManagerEx
|
||||||
|
import com.intellij.openapi.fileEditor.ex.IdeDocumentHistory
|
||||||
|
import com.intellij.openapi.project.Project
|
||||||
|
import com.intellij.openapi.ui.playback.commands.ActionCommand
|
||||||
|
import org.acejump.search.Tag
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base class for actions available after typing a tag.
|
||||||
|
*/
|
||||||
|
sealed class AceTagAction {
|
||||||
|
abstract operator fun invoke(tag: Tag, shiftMode: Boolean, isFinal: Boolean)
|
||||||
|
|
||||||
|
abstract class BaseJumpAction : AceTagAction() {
|
||||||
|
override fun invoke(tag: Tag, shiftMode: Boolean, isFinal: Boolean) {
|
||||||
|
val editor = tag.editor
|
||||||
|
val caretModel = editor.caretModel
|
||||||
|
val oldCarets = if (shiftMode) caretModel.caretsAndSelections else emptyList()
|
||||||
|
|
||||||
|
editor.project?.let { addCurrentPositionToHistory(it, editor.document) }
|
||||||
|
|
||||||
|
if (isFinal) {
|
||||||
|
ensureEditorFocused(editor)
|
||||||
|
}
|
||||||
|
|
||||||
|
moveCaretTo(editor, tag.offset)
|
||||||
|
|
||||||
|
if (shiftMode) {
|
||||||
|
caretModel.caretsAndSelections = oldCarets + caretModel.caretsAndSelections
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private companion object {
|
||||||
|
fun moveCaretTo(editor: Editor, offset: Int) = with(editor) {
|
||||||
|
selectionModel.removeSelection(true)
|
||||||
|
caretModel.removeSecondaryCarets()
|
||||||
|
caretModel.moveToOffset(offset)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun performAction(actionName: String) {
|
||||||
|
val actionManager = ActionManager.getInstance()
|
||||||
|
val action = actionManager.getAction(actionName)
|
||||||
|
if (action != null) {
|
||||||
|
actionManager.tryToExecute(action, ActionCommand.getInputEvent(null), null, null, true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun ensureEditorFocused(editor: Editor) {
|
||||||
|
val project = editor.project ?: return
|
||||||
|
val fem = FileEditorManagerEx.getInstanceEx(project)
|
||||||
|
|
||||||
|
val window = fem.windows.firstOrNull { (it.selectedComposite?.selectedWithProvider?.fileEditor as? TextEditor)?.editor === editor }
|
||||||
|
if (window != null && window !== fem.currentWindow) {
|
||||||
|
fem.currentWindow = window
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun addCurrentPositionToHistory(project: Project, document: Document) {
|
||||||
|
CommandProcessor.getInstance().executeCommand(project, {
|
||||||
|
with(IdeDocumentHistory.getInstance(project)) {
|
||||||
|
setCurrentCommandHasMoves()
|
||||||
|
includeCurrentCommandAsNavigation()
|
||||||
|
includeCurrentPlaceAsChangePlace()
|
||||||
|
}
|
||||||
|
}, "AceJumpHistoryAppender", DocCommandGroupId.noneGroupId(document), UndoConfirmationPolicy.DO_NOT_REQUEST_CONFIRMATION, document)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Actions
|
||||||
|
|
||||||
|
/**
|
||||||
|
* On default action, places the caret at the first character of the search query.
|
||||||
|
* On shift action, adds the new caret to existing carets.
|
||||||
|
*/
|
||||||
|
object JumpToSearchStart : BaseJumpAction()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* On default action, performs the Go To Declaration action, available via `Navigate | Declaration or Usages`.
|
||||||
|
* On shift action, performs the Go To Type Declaration action, available via `Navigate | Type Declaration`.
|
||||||
|
* Always places the caret at the start of the word.
|
||||||
|
*/
|
||||||
|
object GoToDeclaration : AceTagAction() {
|
||||||
|
override fun invoke(tag: Tag, shiftMode: Boolean, isFinal: Boolean) {
|
||||||
|
JumpToSearchStart.invoke(tag, shiftMode = false, isFinal = isFinal)
|
||||||
|
ApplicationManager.getApplication().invokeLater { performAction(if (shiftMode) IdeActions.ACTION_GOTO_TYPE_DECLARATION else IdeActions.ACTION_GOTO_DECLARATION) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
189
src/main/kotlin/org/acejump/action/AceVimAction.kt
Normal file
189
src/main/kotlin/org/acejump/action/AceVimAction.kt
Normal file
@@ -0,0 +1,189 @@
|
|||||||
|
package org.acejump.action
|
||||||
|
|
||||||
|
import com.intellij.openapi.actionSystem.ActionUpdateThread
|
||||||
|
import com.intellij.openapi.actionSystem.AnActionEvent
|
||||||
|
import com.intellij.openapi.actionSystem.CommonDataKeys
|
||||||
|
import com.intellij.openapi.application.ApplicationManager
|
||||||
|
import com.intellij.openapi.application.WriteAction
|
||||||
|
import com.intellij.openapi.project.DumbAwareAction
|
||||||
|
import com.maddyhome.idea.vim.KeyHandler
|
||||||
|
import com.maddyhome.idea.vim.action.change.change.ChangeVisualAction
|
||||||
|
import com.maddyhome.idea.vim.action.change.delete.DeleteVisualAction
|
||||||
|
import com.maddyhome.idea.vim.action.copy.YankVisualAction
|
||||||
|
import com.maddyhome.idea.vim.api.injector
|
||||||
|
import com.maddyhome.idea.vim.command.OperatorArguments
|
||||||
|
import com.maddyhome.idea.vim.group.visual.vimSetSelection
|
||||||
|
import com.maddyhome.idea.vim.helper.inVisualMode
|
||||||
|
import com.maddyhome.idea.vim.helper.vimSelectionStart
|
||||||
|
import com.maddyhome.idea.vim.newapi.vim
|
||||||
|
import com.maddyhome.idea.vim.state.mode.Mode.OP_PENDING
|
||||||
|
import com.maddyhome.idea.vim.state.mode.SelectionType
|
||||||
|
import org.acejump.boundaries.StandardBoundaries.AFTER_CARET
|
||||||
|
import org.acejump.boundaries.StandardBoundaries.BEFORE_CARET
|
||||||
|
import org.acejump.boundaries.StandardBoundaries.CARET_LINE
|
||||||
|
import org.acejump.boundaries.StandardBoundaries.VISIBLE_ON_SCREEN
|
||||||
|
import org.acejump.modes.JumpMode
|
||||||
|
import org.acejump.search.Pattern
|
||||||
|
import org.acejump.search.Tag
|
||||||
|
import org.acejump.session.SessionManager
|
||||||
|
import org.acejump.session.SessionState
|
||||||
|
|
||||||
|
sealed class AceVimAction : DumbAwareAction() {
|
||||||
|
protected abstract val mode: AceVimMode
|
||||||
|
|
||||||
|
final override fun actionPerformed(e: AnActionEvent) {
|
||||||
|
val editor = e.getData(CommonDataKeys.EDITOR) ?: return
|
||||||
|
val context = e.dataContext.vim
|
||||||
|
|
||||||
|
val caret = editor.caretModel.currentCaret
|
||||||
|
val initialOffset = caret.offset
|
||||||
|
val selectionStart = if (editor.inVisualMode) caret.vimSelectionStart else null
|
||||||
|
|
||||||
|
val session = SessionManager.start(editor, mode.getJumpEditors(editor))
|
||||||
|
|
||||||
|
session.defaultBoundary = mode.boundaries
|
||||||
|
session.startJumpMode {
|
||||||
|
object : JumpMode() {
|
||||||
|
override fun accept(state: SessionState, acceptedTag: Tag) {
|
||||||
|
AceTagAction.JumpToSearchStart.invoke(acceptedTag, shiftMode = wasUpperCase, isFinal = true)
|
||||||
|
|
||||||
|
if (selectionStart != null) {
|
||||||
|
caret.vim.vimSetSelection(selectionStart, caret.offset, moveCaretToSelectionEnd = true)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
val vim = editor.vim
|
||||||
|
if (vim.mode is OP_PENDING) {
|
||||||
|
val keyHandler = KeyHandler.getInstance()
|
||||||
|
val key = keyHandler.keyHandlerState.commandBuilder.keys.singleOrNull()?.keyChar
|
||||||
|
|
||||||
|
keyHandler.fullReset(vim)
|
||||||
|
|
||||||
|
AceVimUtil.enterVisualMode(vim, SelectionType.CHARACTER_WISE)
|
||||||
|
caret.vim.vimSetSelection(caret.offset, initialOffset, moveCaretToSelectionEnd = true)
|
||||||
|
|
||||||
|
val action = when (key) {
|
||||||
|
'd' -> DeleteVisualAction()
|
||||||
|
'c' -> ChangeVisualAction()
|
||||||
|
'y' -> YankVisualAction()
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
|
||||||
|
if (action != null) {
|
||||||
|
ApplicationManager.getApplication().invokeLater {
|
||||||
|
WriteAction.run<Nothing> {
|
||||||
|
keyHandler.keyHandlerState.commandBuilder.addAction(action)
|
||||||
|
|
||||||
|
val cmd = keyHandler.keyHandlerState.commandBuilder.buildCommand()
|
||||||
|
val operatorArguments = OperatorArguments(vim.mode is OP_PENDING, cmd.rawCount, injector.vimState.mode)
|
||||||
|
|
||||||
|
injector.vimState.executingCommand = cmd
|
||||||
|
injector.actionExecutor.executeVimAction(vim, action, context, operatorArguments)
|
||||||
|
}
|
||||||
|
|
||||||
|
keyHandler.reset(vim)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
injector.scroll.scrollCaretIntoView(editor.vim)
|
||||||
|
mode.finishSession(editor, session)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mode.setupSession(editor, session)
|
||||||
|
}
|
||||||
|
|
||||||
|
class JumpAllEditors : AceVimAction() {
|
||||||
|
override val mode = AceVimMode.JumpAllEditors
|
||||||
|
}
|
||||||
|
|
||||||
|
class JumpForward : AceVimAction() {
|
||||||
|
override val mode = AceVimMode.Jump(AFTER_CARET.intersection(VISIBLE_ON_SCREEN))
|
||||||
|
}
|
||||||
|
|
||||||
|
class JumpBackward : AceVimAction() {
|
||||||
|
override val mode = AceVimMode.Jump(BEFORE_CARET.intersection(VISIBLE_ON_SCREEN))
|
||||||
|
}
|
||||||
|
|
||||||
|
class JumpTillForward : AceVimAction() {
|
||||||
|
override val mode = AceVimMode.JumpTillForward(AFTER_CARET.intersection(VISIBLE_ON_SCREEN))
|
||||||
|
}
|
||||||
|
|
||||||
|
class JumpTillBackward : AceVimAction() {
|
||||||
|
override val mode = AceVimMode.JumpTillBackward(BEFORE_CARET.intersection(VISIBLE_ON_SCREEN))
|
||||||
|
}
|
||||||
|
|
||||||
|
class JumpOnLineForward : AceVimAction() {
|
||||||
|
override val mode = AceVimMode.Jump(AFTER_CARET.intersection(CARET_LINE))
|
||||||
|
}
|
||||||
|
|
||||||
|
class JumpOnLineBackward : AceVimAction() {
|
||||||
|
override val mode = AceVimMode.Jump(BEFORE_CARET.intersection(CARET_LINE))
|
||||||
|
}
|
||||||
|
|
||||||
|
class JumpLineIndentsForward : AceVimAction() {
|
||||||
|
override val mode = AceVimMode.JumpToPattern(Pattern.LINE_INDENTS, AFTER_CARET.intersection(VISIBLE_ON_SCREEN))
|
||||||
|
}
|
||||||
|
|
||||||
|
class JumpLineIndentsBackward : AceVimAction() {
|
||||||
|
override val mode = AceVimMode.JumpToPattern(Pattern.LINE_INDENTS, BEFORE_CARET.intersection(VISIBLE_ON_SCREEN))
|
||||||
|
}
|
||||||
|
|
||||||
|
class JumpLWordForward : AceVimAction() {
|
||||||
|
override val mode = AceVimMode.JumpToPattern(Pattern.VIM_LWORD, AFTER_CARET.intersection(VISIBLE_ON_SCREEN))
|
||||||
|
}
|
||||||
|
|
||||||
|
class JumpUWordForward : AceVimAction() {
|
||||||
|
override val mode = AceVimMode.JumpToPattern(Pattern.VIM_UWORD, AFTER_CARET.intersection(VISIBLE_ON_SCREEN))
|
||||||
|
}
|
||||||
|
|
||||||
|
class JumpLWordBackward : AceVimAction() {
|
||||||
|
override val mode = AceVimMode.JumpToPattern(Pattern.VIM_LWORD, BEFORE_CARET.intersection(VISIBLE_ON_SCREEN))
|
||||||
|
}
|
||||||
|
|
||||||
|
class JumpUWordBackward : AceVimAction() {
|
||||||
|
override val mode = AceVimMode.JumpToPattern(Pattern.VIM_UWORD, BEFORE_CARET.intersection(VISIBLE_ON_SCREEN))
|
||||||
|
}
|
||||||
|
|
||||||
|
class JumpLWordEndForward : AceVimAction() {
|
||||||
|
override val mode = AceVimMode.JumpToPattern(Pattern.VIM_LWORD_END, AFTER_CARET.intersection(VISIBLE_ON_SCREEN))
|
||||||
|
}
|
||||||
|
|
||||||
|
class JumpUWordEndForward : AceVimAction() {
|
||||||
|
override val mode = AceVimMode.JumpToPattern(Pattern.VIM_UWORD_END, AFTER_CARET.intersection(VISIBLE_ON_SCREEN))
|
||||||
|
}
|
||||||
|
|
||||||
|
class JumpLWordEndBackward : AceVimAction() {
|
||||||
|
override val mode = AceVimMode.JumpToPattern(Pattern.VIM_LWORD_END, BEFORE_CARET.intersection(VISIBLE_ON_SCREEN))
|
||||||
|
}
|
||||||
|
|
||||||
|
class JumpUWordEndBackward : AceVimAction() {
|
||||||
|
override val mode = AceVimMode.JumpToPattern(Pattern.VIM_UWORD_END, BEFORE_CARET.intersection(VISIBLE_ON_SCREEN))
|
||||||
|
}
|
||||||
|
|
||||||
|
class JumpAllEditorsGoToDeclaration : DumbAwareAction() {
|
||||||
|
override fun update(action: AnActionEvent) {
|
||||||
|
action.presentation.isEnabled = action.getData(CommonDataKeys.EDITOR) != null
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getActionUpdateThread(): ActionUpdateThread {
|
||||||
|
return ActionUpdateThread.BGT
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun actionPerformed(e: AnActionEvent) {
|
||||||
|
val editor = e.getData(CommonDataKeys.EDITOR) ?: return
|
||||||
|
val session = SessionManager.start(editor, AceVimMode.JumpAllEditors.getJumpEditors(editor))
|
||||||
|
|
||||||
|
session.defaultBoundary = VISIBLE_ON_SCREEN
|
||||||
|
session.startJumpMode {
|
||||||
|
object : JumpMode() {
|
||||||
|
override fun accept(state: SessionState, acceptedTag: Tag) {
|
||||||
|
AceTagAction.GoToDeclaration.invoke(acceptedTag, shiftMode = wasUpperCase, isFinal = true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
64
src/main/kotlin/org/acejump/action/AceVimMode.kt
Normal file
64
src/main/kotlin/org/acejump/action/AceVimMode.kt
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
package org.acejump.action
|
||||||
|
|
||||||
|
import com.intellij.openapi.editor.Editor
|
||||||
|
import org.acejump.boundaries.Boundaries
|
||||||
|
import org.acejump.boundaries.StandardBoundaries
|
||||||
|
import org.acejump.openEditors
|
||||||
|
import org.acejump.search.Pattern
|
||||||
|
import org.acejump.session.Session
|
||||||
|
|
||||||
|
sealed class AceVimMode {
|
||||||
|
abstract val boundaries: Boundaries
|
||||||
|
|
||||||
|
open fun getJumpEditors(mainEditor: Editor): List<Editor> {
|
||||||
|
return listOf(mainEditor)
|
||||||
|
}
|
||||||
|
|
||||||
|
open fun setupSession(editor: Editor, session: Session) {}
|
||||||
|
open fun finishSession(editor: Editor, session: Session) {}
|
||||||
|
|
||||||
|
class Jump(override val boundaries: Boundaries) : AceVimMode()
|
||||||
|
|
||||||
|
object JumpAllEditors : AceVimMode() {
|
||||||
|
override val boundaries = StandardBoundaries.VISIBLE_ON_SCREEN
|
||||||
|
override fun getJumpEditors(mainEditor: Editor): List<Editor> {
|
||||||
|
val project = mainEditor.project ?: return super.getJumpEditors(mainEditor)
|
||||||
|
|
||||||
|
return project.openEditors
|
||||||
|
.sortedBy { if (it === mainEditor) 0 else 1 }
|
||||||
|
.ifEmpty { listOf(mainEditor) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class JumpTillForward(override val boundaries: Boundaries) : AceVimMode() {
|
||||||
|
override fun finishSession(editor: Editor, session: Session) {
|
||||||
|
val document = editor.document
|
||||||
|
|
||||||
|
for (caret in editor.caretModel.allCarets) {
|
||||||
|
val offset = caret.offset
|
||||||
|
if (offset > document.getLineStartOffset(document.getLineNumber(offset))) {
|
||||||
|
caret.moveToOffset(offset - 1, false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class JumpTillBackward(override val boundaries: Boundaries) : AceVimMode() {
|
||||||
|
override fun finishSession(editor: Editor, session: Session) {
|
||||||
|
val document = editor.document
|
||||||
|
|
||||||
|
for (caret in editor.caretModel.allCarets) {
|
||||||
|
val offset = caret.offset
|
||||||
|
if (offset < document.getLineEndOffset(document.getLineNumber(offset))) {
|
||||||
|
caret.moveToOffset(offset + 1, false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class JumpToPattern(private val pattern: Pattern, override val boundaries: Boundaries) : AceVimMode() {
|
||||||
|
override fun setupSession(editor: Editor, session: Session) {
|
||||||
|
session.startRegexSearch(pattern)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
44
src/main/kotlin/org/acejump/boundaries/Boundaries.kt
Normal file
44
src/main/kotlin/org/acejump/boundaries/Boundaries.kt
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
package org.acejump.boundaries
|
||||||
|
|
||||||
|
import com.intellij.openapi.editor.Editor
|
||||||
|
import kotlin.math.max
|
||||||
|
import kotlin.math.min
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defines a (possibly) disjoint set of editor offsets that partitions the whole editor into two groups - offsets inside the range, and
|
||||||
|
* offsets outside the range.
|
||||||
|
*/
|
||||||
|
interface Boundaries {
|
||||||
|
/**
|
||||||
|
* Returns a range of editor offsets, starting at the first offset in the boundary, and ending at the last offset in the boundary.
|
||||||
|
* May include offsets outside the boundary, for ex. when the boundary is rectangular and the file has long lines which are only
|
||||||
|
* partially visible.
|
||||||
|
*/
|
||||||
|
fun getOffsetRange(editor: Editor, cache: EditorOffsetCache = EditorOffsetCache.Uncached): IntRange
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns whether the editor offset is included within the boundary.
|
||||||
|
*/
|
||||||
|
fun isOffsetInside(editor: Editor, offset: Int, cache: EditorOffsetCache = EditorOffsetCache.Uncached): Boolean
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a boundary so that an offset/range is within the boundary iff it is within both original boundaries.
|
||||||
|
*/
|
||||||
|
fun intersection(other: Boundaries): Boundaries {
|
||||||
|
if (this === other) {
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
|
return object : Boundaries {
|
||||||
|
override fun getOffsetRange(editor: Editor, cache: EditorOffsetCache): IntRange {
|
||||||
|
val b1 = this@Boundaries.getOffsetRange(editor, cache)
|
||||||
|
val b2 = other.getOffsetRange(editor, cache)
|
||||||
|
return max(b1.first, b2.first)..min(b1.last, b2.last)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun isOffsetInside(editor: Editor, offset: Int, cache: EditorOffsetCache): Boolean {
|
||||||
|
return this@Boundaries.isOffsetInside(editor, offset, cache) && other.isOffsetInside(editor, offset, cache)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
122
src/main/kotlin/org/acejump/boundaries/EditorOffsetCache.kt
Normal file
122
src/main/kotlin/org/acejump/boundaries/EditorOffsetCache.kt
Normal file
@@ -0,0 +1,122 @@
|
|||||||
|
package org.acejump.boundaries
|
||||||
|
|
||||||
|
import com.intellij.openapi.editor.Editor
|
||||||
|
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap
|
||||||
|
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap
|
||||||
|
import java.awt.Point
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Optionally caches slow operations of (1) retrieving the currently visible editor area, and (2) converting between editor offsets and
|
||||||
|
* pixel coordinates.
|
||||||
|
*
|
||||||
|
* To avoid unnecessary overhead, there is no automatic detection of when the editor, its contents, or its visible area has changed, so the
|
||||||
|
* cache must only be used for a single rendered frame of a single [Editor].
|
||||||
|
*/
|
||||||
|
sealed class EditorOffsetCache {
|
||||||
|
/**
|
||||||
|
* Returns the top left and bottom right points of the visible area rectangle.
|
||||||
|
*/
|
||||||
|
abstract fun visibleArea(editor: Editor): Pair<Point, Point>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns whether the offset is in the visible area rectangle.
|
||||||
|
*/
|
||||||
|
abstract fun isVisible(editor: Editor, offset: Int): Boolean
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the editor offset at the provided pixel coordinate.
|
||||||
|
*/
|
||||||
|
abstract fun xyToOffset(editor: Editor, pos: Point): Int
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the top left pixel coordinate of the character at the provided editor offset.
|
||||||
|
*/
|
||||||
|
abstract fun offsetToXY(editor: Editor, offset: Int): Point
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
fun new(): EditorOffsetCache {
|
||||||
|
return Cache()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class Cache : EditorOffsetCache() {
|
||||||
|
private var visibleArea: Pair<Point, Point>? = null
|
||||||
|
private val lineToVisibleOffsetRange = Int2ObjectOpenHashMap<IntRange>()
|
||||||
|
private val pointToOffset = Object2IntOpenHashMap<Point>().apply { defaultReturnValue(-1) }
|
||||||
|
private val offsetToPoint = Int2ObjectOpenHashMap<Point>()
|
||||||
|
|
||||||
|
override fun visibleArea(editor: Editor): Pair<Point, Point> {
|
||||||
|
return visibleArea ?: Uncached.visibleArea(editor).also { visibleArea = it }
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun isVisible(editor: Editor, offset: Int): Boolean {
|
||||||
|
val visualLine = editor.offsetToVisualLine(offset, false)
|
||||||
|
|
||||||
|
var visibleRange = lineToVisibleOffsetRange.get(visualLine)
|
||||||
|
if (visibleRange == null) {
|
||||||
|
val (topLeft, bottomRight) = visibleArea(editor)
|
||||||
|
val lineY = editor.visualLineToY(visualLine)
|
||||||
|
|
||||||
|
val firstVisibleOffset = xyToOffset(editor, Point(topLeft.x, lineY))
|
||||||
|
val lastVisibleOffset = xyToOffset(editor, Point(bottomRight.x, lineY))
|
||||||
|
|
||||||
|
visibleRange = firstVisibleOffset..lastVisibleOffset
|
||||||
|
lineToVisibleOffsetRange.put(visualLine, visibleRange)
|
||||||
|
}
|
||||||
|
|
||||||
|
return offset in visibleRange
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun xyToOffset(editor: Editor, pos: Point): Int {
|
||||||
|
val offset = pointToOffset.getInt(pos)
|
||||||
|
|
||||||
|
if (offset != -1) {
|
||||||
|
return offset
|
||||||
|
}
|
||||||
|
|
||||||
|
return Uncached.xyToOffset(editor, pos).also {
|
||||||
|
pointToOffset.put(pos, it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun offsetToXY(editor: Editor, offset: Int): Point {
|
||||||
|
val pos = offsetToPoint.get(offset)
|
||||||
|
|
||||||
|
if (pos != null) {
|
||||||
|
return pos
|
||||||
|
}
|
||||||
|
|
||||||
|
return Uncached.offsetToXY(editor, offset).also {
|
||||||
|
offsetToPoint.put(offset, it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
object Uncached : EditorOffsetCache() {
|
||||||
|
override fun visibleArea(editor: Editor): Pair<Point, Point> {
|
||||||
|
val visibleRect = editor.scrollingModel.visibleArea
|
||||||
|
|
||||||
|
return Pair(
|
||||||
|
visibleRect.location,
|
||||||
|
visibleRect.location.apply { translate(visibleRect.width, visibleRect.height) }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun isVisible(editor: Editor, offset: Int): Boolean {
|
||||||
|
val (topLeft, bottomRight) = visibleArea(editor)
|
||||||
|
val pos = offsetToXY(editor, offset)
|
||||||
|
val x = pos.x
|
||||||
|
val y = pos.y
|
||||||
|
|
||||||
|
return x >= topLeft.x && y >= topLeft.y && x <= bottomRight.x && y <= bottomRight.y
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun xyToOffset(editor: Editor, pos: Point): Int {
|
||||||
|
return editor.logicalPositionToOffset(editor.xyToLogicalPosition(pos))
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun offsetToXY(editor: Editor, offset: Int): Point {
|
||||||
|
return editor.offsetToXY(offset, true, false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
52
src/main/kotlin/org/acejump/boundaries/StandardBoundaries.kt
Normal file
52
src/main/kotlin/org/acejump/boundaries/StandardBoundaries.kt
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
package org.acejump.boundaries
|
||||||
|
|
||||||
|
import com.intellij.openapi.editor.Editor
|
||||||
|
|
||||||
|
enum class StandardBoundaries : Boundaries {
|
||||||
|
VISIBLE_ON_SCREEN {
|
||||||
|
override fun getOffsetRange(editor: Editor, cache: EditorOffsetCache): IntRange {
|
||||||
|
val (topLeft, bottomRight) = cache.visibleArea(editor)
|
||||||
|
val startOffset = cache.xyToOffset(editor, topLeft)
|
||||||
|
val endOffset = cache.xyToOffset(editor, bottomRight)
|
||||||
|
|
||||||
|
return startOffset..endOffset
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun isOffsetInside(editor: Editor, offset: Int, cache: EditorOffsetCache): Boolean {
|
||||||
|
return cache.isVisible(editor, offset)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
BEFORE_CARET {
|
||||||
|
override fun getOffsetRange(editor: Editor, cache: EditorOffsetCache): IntRange {
|
||||||
|
return 0 until editor.caretModel.offset
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun isOffsetInside(editor: Editor, offset: Int, cache: EditorOffsetCache): Boolean {
|
||||||
|
return offset < editor.caretModel.offset
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
AFTER_CARET {
|
||||||
|
override fun getOffsetRange(editor: Editor, cache: EditorOffsetCache): IntRange {
|
||||||
|
return (editor.caretModel.offset + 1) until editor.document.textLength
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun isOffsetInside(editor: Editor, offset: Int, cache: EditorOffsetCache): Boolean {
|
||||||
|
return offset > editor.caretModel.offset
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
CARET_LINE {
|
||||||
|
override fun getOffsetRange(editor: Editor, cache: EditorOffsetCache): IntRange {
|
||||||
|
val document = editor.document
|
||||||
|
val offset = editor.caretModel.offset
|
||||||
|
val line = document.getLineNumber(offset)
|
||||||
|
return document.getLineStartOffset(line)..document.getLineEndOffset(line)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun isOffsetInside(editor: Editor, offset: Int, cache: EditorOffsetCache): Boolean {
|
||||||
|
return offset in getOffsetRange(editor, cache)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -4,112 +4,36 @@ import com.intellij.openapi.components.PersistentStateComponent
|
|||||||
import com.intellij.openapi.components.ServiceManager
|
import com.intellij.openapi.components.ServiceManager
|
||||||
import com.intellij.openapi.components.State
|
import com.intellij.openapi.components.State
|
||||||
import com.intellij.openapi.components.Storage
|
import com.intellij.openapi.components.Storage
|
||||||
import com.intellij.openapi.diagnostic.Logger
|
import org.acejump.input.KeyLayoutCache
|
||||||
import org.acejump.label.Pattern
|
|
||||||
import org.acejump.label.Pattern.Companion.KeyLayout
|
|
||||||
import org.acejump.label.mapIndices
|
|
||||||
import org.acejump.search.JumpMode
|
|
||||||
import java.awt.Color
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Ensures consistiency between [AceSettings] and [AceSettingsPanel].
|
* Ensures consistiency between [AceSettings] and [AceSettingsPanel]. Persists the state of the AceJump IDE settings across IDE restarts.
|
||||||
* Persists the state of the AceJump IDE settings across IDE restarts.
|
* [https://www.jetbrains.org/intellij/sdk/docs/basics/persisting_state_of_components.html]
|
||||||
* https://www.jetbrains.org/intellij/sdk/docs/basics/persisting_state_of_components.html
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@State(name = "AceConfig", storages = [(Storage("\$APP_CONFIG\$/AceJump.xml"))])
|
@State(name = "AceConfig", storages = [(Storage("\$APP_CONFIG\$/AceJump.xml"))])
|
||||||
class AceConfig: PersistentStateComponent<AceSettings> {
|
class AceConfig : PersistentStateComponent<AceSettings> {
|
||||||
private val logger = Logger.getInstance(AceConfig::class.java)
|
private var aceSettings = AceSettings()
|
||||||
|
|
||||||
internal var aceSettings = AceSettings()
|
|
||||||
set(value) {
|
|
||||||
allPossibleTags = value.allowedChars.bigrams(defaultTagOrder(value.layout))
|
|
||||||
field = value
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
val settings: AceSettings
|
val settings
|
||||||
get() = ServiceManager.getService(AceConfig::class.java).aceSettings
|
get() = ServiceManager.getService(AceConfig::class.java).aceSettings
|
||||||
val allowedChars: String get() = settings.allowedChars
|
|
||||||
val layout: KeyLayout get() = settings.layout
|
val layout get() = settings.layout
|
||||||
val cycleMode1: JumpMode get() = settings.cycleMode1
|
val minQueryLength get() = settings.minQueryLength
|
||||||
val cycleMode2: JumpMode get() = settings.cycleMode2
|
val invertUppercaseMode get() = settings.invertUppercaseMode
|
||||||
val cycleMode3: JumpMode get() = settings.cycleMode3
|
val editorFadeOpacity get() = settings.editorFadeOpacity
|
||||||
val cycleMode4: JumpMode get() = settings.cycleMode4
|
val jumpModeColor get() = settings.jumpModeColor
|
||||||
val jumpModeColor: Color get() = settings.jumpModeColor
|
val tagForegroundColor1 get() = settings.tagForegroundColor1
|
||||||
val jumpEndModeColor: Color get() = settings.jumpEndModeColor
|
val tagForegroundColor2 get() = settings.tagForegroundColor2
|
||||||
val targetModeColor: Color get() = settings.targetModeColor
|
val searchHighlightColor get() = settings.searchHighlightColor
|
||||||
val definitionModeColor: Color get() = settings.definitionModeColor
|
|
||||||
val textHighlightColor: Color get() = settings.textHighlightColor
|
|
||||||
val tagForegroundColor: Color get() = settings.tagForegroundColor
|
|
||||||
val tagBackgroundColor: Color get() = settings.tagBackgroundColor
|
|
||||||
val roundedTagCorners: Boolean get() = settings.roundedTagCorners
|
|
||||||
val searchWholeFile: Boolean get() = settings.searchWholeFile
|
|
||||||
val supportPinyin: Boolean get() = settings.supportPinyin
|
|
||||||
|
|
||||||
private val nearby: Map<Char, Map<Char, Int>> = mapOf(
|
|
||||||
// Values are QWERTY keys sorted by physical proximity to the map key
|
|
||||||
'j' to "jikmnhuolbgypvftcdrxsezawq8796054321",
|
|
||||||
'f' to "ftgvcdryhbxseujnzawqikmolp5463728190",
|
|
||||||
'k' to "kolmjipnhubgyvftcdrxsezawq9807654321",
|
|
||||||
'd' to "drfcxsetgvzawyhbqujnikmolp4352617890",
|
|
||||||
'l' to "lkopmjinhubgyvftcdrxsezawq0987654321",
|
|
||||||
's' to "sedxzawrfcqtgvyhbujnikmolp3241567890",
|
|
||||||
'a' to "aqwszedxrfctgvyhbujnikmolp1234567890",
|
|
||||||
'h' to "hujnbgyikmvftolcdrpxsezawq6758493021",
|
|
||||||
'g' to "gyhbvftujncdrikmxseolzawpq5647382910",
|
|
||||||
'y' to "yuhgtijnbvfrokmcdeplxswzaq6758493021",
|
|
||||||
't' to "tygfruhbvcdeijnxswokmzaqpl5647382910",
|
|
||||||
'u' to "uijhyokmnbgtplvfrcdexswzaq7869504321",
|
|
||||||
'r' to "rtfdeygvcxswuhbzaqijnokmpl4536271890",
|
|
||||||
'n' to "nbhjmvgyuiklocftpxdrzseawq7685940321",
|
|
||||||
'v' to "vcfgbxdrtyhnzseujmawikqolp5463728190",
|
|
||||||
'm' to "mnjkbhuilvgyopcftxdrzseawq8970654321",
|
|
||||||
'c' to "cxdfvzsertgbawyhnqujmikolp4352617890",
|
|
||||||
'b' to "bvghncftyujmxdrikzseolawqp6574839201",
|
|
||||||
'i' to "iokjuplmnhybgtvfrcdexswzaq8970654321",
|
|
||||||
'e' to "erdswtfcxzaqygvuhbijnokmpl3425167890",
|
|
||||||
'x' to "xzsdcawerfvqtgbyhnujmikolp3241567890",
|
|
||||||
'z' to "zasxqwedcrfvtgbyhnujmikolp1234567890",
|
|
||||||
'o' to "oplkimjunhybgtvfrcdexswzaq9087654321",
|
|
||||||
'w' to "wesaqrdxztfcygvuhbijnokmpl2314567890",
|
|
||||||
'p' to "plokimjunhybgtvfrcdexswzaq0987654321",
|
|
||||||
'q' to "qwaeszrdxtfcygvuhbijnokmpl1234567890",
|
|
||||||
'1' to "1234567890qawzsexdrcftvgybhunjimkolp",
|
|
||||||
'2' to "2134567890qwasezxdrcftvgybhunjimkolp",
|
|
||||||
'3' to "3241567890weqasdrzxcftvgybhunjimkolp",
|
|
||||||
'4' to "4352617890erwsdftqazxcvgybhunjimkolp",
|
|
||||||
'5' to "5463728190rtedfgywsxcvbhuqaznjimkolp",
|
|
||||||
'6' to "6574839201tyrfghuedcvbnjiwsxmkoqazlp",
|
|
||||||
'7' to "7685940321yutghjirfvbnmkoedclpwsxqaz",
|
|
||||||
'8' to "8796054321uiyhjkotgbnmlprfvedcwsxqaz",
|
|
||||||
'9' to "9807654321ioujklpyhnmtgbrfvedcwsxqaz",
|
|
||||||
'0' to "0987654321opiklujmyhntgbrfvedcwsxqaz")
|
|
||||||
.mapValues { (_, v) -> v.mapIndices() }
|
|
||||||
|
|
||||||
private fun distance(fromKey: Char, toKey: Char) = nearby[fromKey]!![toKey]
|
|
||||||
|
|
||||||
val defaultTagOrder = defaultTagOrder(this.layout)
|
|
||||||
|
|
||||||
private fun defaultTagOrder(layout: KeyLayout) = compareBy(
|
|
||||||
{ it[0].isDigit() || it[1].isDigit() },
|
|
||||||
{ distance(it[0], it.last()) },
|
|
||||||
layout.priority { it[0] })
|
|
||||||
|
|
||||||
internal var allPossibleTags: Set<String> = settings.allowedChars.bigrams()
|
|
||||||
|
|
||||||
internal fun String.bigrams(comparator: Comparator<String> = defaultTagOrder): Set<String> {
|
|
||||||
return run { flatMap { e -> map { c -> "$e$c" } } }.sortedWith(comparator).toSet()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getCompatibleTags(query: String, matching: (String) -> Boolean) =
|
|
||||||
Pattern.filter(allPossibleTags, query).filter(matching).toSet()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getState() = aceSettings
|
override fun getState(): AceSettings {
|
||||||
|
return aceSettings
|
||||||
|
}
|
||||||
|
|
||||||
override fun loadState(state: AceSettings) {
|
override fun loadState(state: AceSettings) {
|
||||||
logger.info("Loaded AceConfig settings: $aceSettings")
|
|
||||||
aceSettings = state
|
aceSettings = state
|
||||||
|
KeyLayoutCache.reset(state)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,65 +1,39 @@
|
|||||||
package org.acejump.config
|
package org.acejump.config
|
||||||
|
|
||||||
import com.intellij.openapi.diagnostic.Logger
|
|
||||||
import com.intellij.openapi.options.Configurable
|
import com.intellij.openapi.options.Configurable
|
||||||
import org.acejump.config.AceConfig.Companion.bigrams
|
|
||||||
import org.acejump.config.AceConfig.Companion.settings
|
import org.acejump.config.AceConfig.Companion.settings
|
||||||
|
import org.acejump.input.KeyLayoutCache
|
||||||
|
|
||||||
class AceConfigurable: Configurable {
|
class AceConfigurable : Configurable {
|
||||||
private val logger = Logger.getInstance(AceConfigurable::class.java)
|
private val panel by lazy(::AceSettingsPanel)
|
||||||
|
|
||||||
private val panel by lazy { AceSettingsPanel() }
|
|
||||||
|
|
||||||
override fun getDisplayName() = "AceJump"
|
override fun getDisplayName() = "AceJump"
|
||||||
|
|
||||||
override fun createComponent() = panel.rootPanel
|
override fun createComponent() = panel.rootPanel
|
||||||
|
|
||||||
override fun isModified() =
|
override fun isModified() =
|
||||||
panel.allowedChars != settings.allowedChars ||
|
panel.allowedChars != settings.allowedChars ||
|
||||||
panel.keyboardLayout != settings.layout ||
|
panel.keyboardLayout != settings.layout ||
|
||||||
panel.cycleMode1 != settings.cycleMode1 ||
|
panel.minQueryLengthInt != settings.minQueryLength ||
|
||||||
panel.cycleMode2 != settings.cycleMode2 ||
|
panel.invertUppercaseMode != settings.invertUppercaseMode ||
|
||||||
panel.cycleMode3 != settings.cycleMode3 ||
|
panel.editorFadeOpacityPercent != settings.editorFadeOpacity ||
|
||||||
panel.cycleMode4 != settings.cycleMode4 ||
|
|
||||||
panel.jumpModeColor != settings.jumpModeColor ||
|
panel.jumpModeColor != settings.jumpModeColor ||
|
||||||
panel.jumpEndModeColor != settings.jumpEndModeColor ||
|
panel.tagForegroundColor1 != settings.tagForegroundColor1 ||
|
||||||
panel.targetModeColor != settings.targetModeColor ||
|
panel.tagForegroundColor2 != settings.tagForegroundColor2 ||
|
||||||
panel.definitionModeColor != settings.definitionModeColor ||
|
panel.searchHighlightColor != settings.searchHighlightColor
|
||||||
panel.textHighlightColor != settings.textHighlightColor ||
|
|
||||||
panel.tagForegroundColor != settings.tagForegroundColor ||
|
|
||||||
panel.tagBackgroundColor != settings.tagBackgroundColor ||
|
|
||||||
panel.roundedTagCorners != settings.roundedTagCorners ||
|
|
||||||
panel.searchWholeFile != settings.searchWholeFile ||
|
|
||||||
panel.supportPinyin != settings.supportPinyin
|
|
||||||
|
|
||||||
private fun String.distinctAlphanumerics() =
|
|
||||||
if (isEmpty()) settings.layout.text
|
|
||||||
else toList().distinct().filter(Char::isLetterOrDigit).joinToString("")
|
|
||||||
|
|
||||||
override fun apply() {
|
override fun apply() {
|
||||||
panel.allowedChars.distinctAlphanumerics().let {
|
settings.allowedChars = panel.allowedChars
|
||||||
settings.allowedChars = it
|
|
||||||
AceConfig.allPossibleTags = it.bigrams()
|
|
||||||
}
|
|
||||||
|
|
||||||
settings.layout = panel.keyboardLayout
|
settings.layout = panel.keyboardLayout
|
||||||
settings.cycleMode1 = panel.cycleMode1
|
settings.minQueryLength = panel.minQueryLengthInt ?: settings.minQueryLength
|
||||||
settings.cycleMode2 = panel.cycleMode2
|
settings.invertUppercaseMode = panel.invertUppercaseMode
|
||||||
settings.cycleMode3 = panel.cycleMode3
|
settings.editorFadeOpacity = panel.editorFadeOpacityPercent
|
||||||
settings.cycleMode4 = panel.cycleMode4
|
panel.jumpModeColor?.let { settings.jumpModeColor = it }
|
||||||
panel.jumpModeColor ?.let { settings.jumpModeColor = it }
|
panel.tagForegroundColor1?.let { settings.tagForegroundColor1 = it }
|
||||||
panel.jumpEndModeColor?.let { settings.jumpEndModeColor = it }
|
panel.tagForegroundColor2?.let { settings.tagForegroundColor2 = it }
|
||||||
panel.targetModeColor ?.let { settings.targetModeColor = it }
|
panel.searchHighlightColor?.let { settings.searchHighlightColor = it }
|
||||||
panel.definitionModeColor ?.let { settings.definitionModeColor = it }
|
KeyLayoutCache.reset(settings)
|
||||||
panel.textHighlightColor ?.let { settings.textHighlightColor = it }
|
|
||||||
panel.tagForegroundColor ?.let { settings.tagForegroundColor = it }
|
|
||||||
panel.tagBackgroundColor ?.let { settings.tagBackgroundColor = it }
|
|
||||||
settings.roundedTagCorners = panel.roundedTagCorners
|
|
||||||
settings.searchWholeFile = panel.searchWholeFile
|
|
||||||
settings.supportPinyin = panel.supportPinyin
|
|
||||||
|
|
||||||
logger.info("User applied new settings: $settings")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun reset() = panel.reset(settings)
|
override fun reset() = panel.reset(settings)
|
||||||
}
|
}
|
||||||
|
@@ -1,47 +1,26 @@
|
|||||||
package org.acejump.config
|
package org.acejump.config
|
||||||
|
|
||||||
import com.intellij.util.xmlb.annotations.OptionTag
|
import com.intellij.util.xmlb.annotations.OptionTag
|
||||||
import org.acejump.label.Pattern.Companion.KeyLayout
|
import org.acejump.input.KeyLayout
|
||||||
import org.acejump.label.Pattern.Companion.KeyLayout.QWERTY
|
import org.acejump.input.KeyLayout.QWERTY
|
||||||
import org.acejump.search.JumpMode
|
|
||||||
import java.awt.Color
|
import java.awt.Color
|
||||||
|
|
||||||
/**
|
|
||||||
* Settings model located for [AceSettingsPanel].
|
|
||||||
*/
|
|
||||||
|
|
||||||
// TODO: https://github.com/acejump/AceJump/issues/215
|
|
||||||
data class AceSettings(
|
data class AceSettings(
|
||||||
var layout: KeyLayout = QWERTY,
|
var layout: KeyLayout = QWERTY,
|
||||||
var allowedChars: String = layout.text,
|
var allowedChars: String = layout.allChars,
|
||||||
var cycleMode1: JumpMode = JumpMode.JUMP,
|
var invertUppercaseMode: Boolean = false,
|
||||||
var cycleMode2: JumpMode = JumpMode.DEFINE,
|
var minQueryLength: Int = 1,
|
||||||
var cycleMode3: JumpMode = JumpMode.TARGET,
|
var editorFadeOpacity: Int = 70,
|
||||||
var cycleMode4: JumpMode = JumpMode.JUMP_END,
|
|
||||||
|
|
||||||
@OptionTag("jumpModeRGB", converter = ColorConverter::class)
|
@OptionTag("jumpModeRGB", converter = ColorConverter::class)
|
||||||
var jumpModeColor: Color = Color.BLUE,
|
var jumpModeColor: Color = Color(0xFFFFFF),
|
||||||
|
|
||||||
@OptionTag("jumpEndModeRGB", converter = ColorConverter::class)
|
|
||||||
var jumpEndModeColor: Color = Color.CYAN,
|
|
||||||
|
|
||||||
@OptionTag("targetModeRGB", converter = ColorConverter::class)
|
|
||||||
var targetModeColor: Color = Color.RED,
|
|
||||||
|
|
||||||
@OptionTag("definitionModeRGB", converter = ColorConverter::class)
|
|
||||||
var definitionModeColor: Color = Color.MAGENTA,
|
|
||||||
|
|
||||||
@OptionTag("textHighlightRGB", converter = ColorConverter::class)
|
|
||||||
var textHighlightColor: Color = Color.GREEN,
|
|
||||||
|
|
||||||
@OptionTag("tagForegroundRGB", converter = ColorConverter::class)
|
@OptionTag("tagForegroundRGB", converter = ColorConverter::class)
|
||||||
var tagForegroundColor: Color = Color.BLACK,
|
var tagForegroundColor1: Color = Color(0xFFFFFF),
|
||||||
|
|
||||||
@OptionTag("tagBackgroundRGB", converter = ColorConverter::class)
|
@OptionTag("tagForeground2RGB", converter = ColorConverter::class)
|
||||||
var tagBackgroundColor: Color = Color.YELLOW,
|
var tagForegroundColor2: Color = Color(0xFFFFFF),
|
||||||
|
|
||||||
var displayQuery: Boolean = false,
|
@OptionTag("searchHighlightRGB", converter = ColorConverter::class)
|
||||||
var roundedTagCorners: Boolean = true,
|
var searchHighlightColor: Color = Color(0x008299),
|
||||||
var searchWholeFile: Boolean = true,
|
)
|
||||||
var supportPinyin: Boolean = false
|
|
||||||
)
|
|
||||||
|
@@ -3,174 +3,135 @@ package org.acejump.config
|
|||||||
import com.intellij.openapi.ui.ComboBox
|
import com.intellij.openapi.ui.ComboBox
|
||||||
import com.intellij.ui.ColorPanel
|
import com.intellij.ui.ColorPanel
|
||||||
import com.intellij.ui.components.JBCheckBox
|
import com.intellij.ui.components.JBCheckBox
|
||||||
|
import com.intellij.ui.components.JBSlider
|
||||||
import com.intellij.ui.components.JBTextArea
|
import com.intellij.ui.components.JBTextArea
|
||||||
import com.intellij.ui.components.JBTextField
|
import com.intellij.ui.components.JBTextField
|
||||||
import com.intellij.ui.layout.Cell
|
import com.intellij.ui.dsl.builder.COLUMNS_LARGE
|
||||||
import com.intellij.ui.layout.GrowPolicy.MEDIUM_TEXT
|
import com.intellij.ui.dsl.builder.COLUMNS_SHORT
|
||||||
import com.intellij.ui.layout.GrowPolicy.SHORT_TEXT
|
import com.intellij.ui.dsl.builder.columns
|
||||||
import com.intellij.ui.layout.panel
|
import com.intellij.ui.dsl.builder.panel
|
||||||
import org.acejump.label.Pattern.Companion.KeyLayout
|
import org.acejump.input.KeyLayout
|
||||||
import org.acejump.search.JumpMode
|
|
||||||
import org.acejump.search.aceString
|
|
||||||
import java.awt.Color
|
import java.awt.Color
|
||||||
|
import java.awt.Dimension
|
||||||
import java.awt.Font
|
import java.awt.Font
|
||||||
|
import java.util.Hashtable
|
||||||
import javax.swing.JCheckBox
|
import javax.swing.JCheckBox
|
||||||
import javax.swing.JComponent
|
import javax.swing.JLabel
|
||||||
import javax.swing.JPanel
|
import javax.swing.JPanel
|
||||||
|
import javax.swing.JSlider
|
||||||
import javax.swing.text.JTextComponent
|
import javax.swing.text.JTextComponent
|
||||||
import kotlin.reflect.KProperty
|
import kotlin.reflect.KProperty
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Settings view located in File | Settings | Tools | AceJump.
|
* Settings view located in File | Settings | Tools | AceJump.
|
||||||
*/
|
*/
|
||||||
|
@Suppress("UsePropertyAccessSyntax")
|
||||||
internal class AceSettingsPanel {
|
internal class AceSettingsPanel {
|
||||||
private val tagCharsField = JBTextField()
|
private val tagAllowedCharsField = JBTextField()
|
||||||
private val keyboardLayoutCombo = ComboBox<KeyLayout>()
|
private val keyboardLayoutCombo = ComboBox<KeyLayout>()
|
||||||
private val keyboardLayoutArea = JBTextArea()
|
private val keyboardLayoutArea = JBTextArea().apply { isEditable = false }
|
||||||
private val cycleModeCombo1 = ComboBox<JumpMode>()
|
private val minQueryLengthField = JBTextField()
|
||||||
private val cycleModeCombo2 = ComboBox<JumpMode>()
|
private val invertUppercaseModeCheckBox = JBCheckBox("Invert uppercase mode")
|
||||||
private val cycleModeCombo3 = ComboBox<JumpMode>()
|
private val editorFadeOpacitySlider = JBSlider(0, 10).apply {
|
||||||
private val cycleModeCombo4 = ComboBox<JumpMode>()
|
labelTable = Hashtable((0..10).associateWith { JLabel("${it * 10}") })
|
||||||
private val jumpModeColorWheel = ColorPanel()
|
paintTrack = true
|
||||||
private val jumpEndModeColorWheel = ColorPanel()
|
paintLabels = true
|
||||||
private val targetModeColorWheel = ColorPanel()
|
paintTicks = true
|
||||||
private val definitionModeColorWheel = ColorPanel()
|
minorTickSpacing = 1
|
||||||
private val textHighlightColorWheel = ColorPanel()
|
majorTickSpacing = 1
|
||||||
private val tagForegroundColorWheel = ColorPanel()
|
minimumSize = Dimension(275, minimumSize.height)
|
||||||
private val tagBackgroundColorWheel = ColorPanel()
|
|
||||||
private val displayQueryCheckBox = JBCheckBox().apply { isEnabled = false }
|
|
||||||
private val roundedTagCornersCheckBox = JBCheckBox()
|
|
||||||
private val searchWholeFileCheckBox = JBCheckBox()
|
|
||||||
private val supportPinyinCheckBox = JBCheckBox()
|
|
||||||
|
|
||||||
init {
|
|
||||||
tagCharsField.apply { font = Font("monospaced", font.style, font.size) }
|
|
||||||
keyboardLayoutArea.apply {
|
|
||||||
font = Font("monospaced", font.style, font.size)
|
|
||||||
isEditable = false
|
|
||||||
}
|
|
||||||
|
|
||||||
keyboardLayoutCombo.run {
|
|
||||||
KeyLayout.values().forEach { addItem(it) }
|
|
||||||
addActionListener { keyChars = (selectedItem as KeyLayout).joinBy("\n") }
|
|
||||||
}
|
|
||||||
|
|
||||||
cycleModeCombo1.run {
|
|
||||||
JumpMode.values().forEach { addItem(it) }
|
|
||||||
addActionListener { cycleMode1 = selectedItem as JumpMode }
|
|
||||||
}
|
|
||||||
|
|
||||||
cycleModeCombo2.run {
|
|
||||||
JumpMode.values().forEach { addItem(it) }
|
|
||||||
addActionListener { cycleMode2 = selectedItem as JumpMode }
|
|
||||||
}
|
|
||||||
|
|
||||||
cycleModeCombo3.run {
|
|
||||||
JumpMode.values().forEach { addItem(it) }
|
|
||||||
addActionListener { cycleMode3 = selectedItem as JumpMode }
|
|
||||||
}
|
|
||||||
|
|
||||||
cycleModeCombo4.run {
|
|
||||||
JumpMode.values().forEach { addItem(it) }
|
|
||||||
addActionListener { cycleMode4 = selectedItem as JumpMode }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
private val jumpModeColorWheel = ColorPanel()
|
||||||
// https://github.com/JetBrains/intellij-community/blob/master/platform/platform-impl/src/com/intellij/ui/layout/readme.md
|
private val tagForeground1ColorWheel = ColorPanel()
|
||||||
|
private val tagForeground2ColorWheel = ColorPanel()
|
||||||
|
private val searchHighlightColorWheel = ColorPanel()
|
||||||
|
|
||||||
|
init {
|
||||||
|
tagAllowedCharsField.apply { font = Font("monospaced", font.style, font.size) }
|
||||||
|
keyboardLayoutArea.apply { font = Font("monospaced", font.style, font.size) }
|
||||||
|
keyboardLayoutCombo.setupEnumItems { keyChars = it.rows.joinToString("\n") }
|
||||||
|
}
|
||||||
|
|
||||||
internal val rootPanel: JPanel = panel {
|
internal val rootPanel: JPanel = panel {
|
||||||
fun Cell.short(component: JComponent) = component(growPolicy = SHORT_TEXT)
|
group("Characters and Layout") {
|
||||||
fun Cell.medium(component: JComponent) = component(growPolicy = MEDIUM_TEXT)
|
row("Allowed characters in tags:") { cell(tagAllowedCharsField).columns(COLUMNS_LARGE) }
|
||||||
|
row("Keyboard layout:") { cell(keyboardLayoutCombo).columns(COLUMNS_SHORT) }
|
||||||
titledRow(aceString("charactersAndLayoutHeading")) {
|
row("Keyboard design:") { cell(keyboardLayoutArea).columns(COLUMNS_SHORT) }
|
||||||
row(aceString("tagCharsToBeUsedLabel")) { medium(tagCharsField) }
|
|
||||||
row(aceString("keyboardLayoutLabel")) { short(keyboardLayoutCombo) }
|
|
||||||
row(aceString("keyboardDesignLabel")) { short(keyboardLayoutArea) }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
titledRow(aceString("modesHeading")) {
|
group("Behavior") {
|
||||||
row(aceString("cycleModeOrderLabel")) {
|
row("Minimum typed characters (1-10):") { cell(minQueryLengthField).columns(COLUMNS_SHORT) }
|
||||||
cell(isVerticalFlow = false, isFullWidth = false) {
|
row { cell(invertUppercaseModeCheckBox) }
|
||||||
cycleModeCombo1()
|
}
|
||||||
cycleModeCombo2()
|
|
||||||
cycleModeCombo3()
|
group("Colors") {
|
||||||
cycleModeCombo4()
|
row("Caret background:") {
|
||||||
}
|
cell(jumpModeColorWheel)
|
||||||
|
}
|
||||||
|
row("Tag foreground:") {
|
||||||
|
cell(tagForeground1ColorWheel)
|
||||||
|
cell(tagForeground2ColorWheel)
|
||||||
|
}
|
||||||
|
row("Search highlight:") {
|
||||||
|
cell(searchHighlightColorWheel)
|
||||||
|
}
|
||||||
|
row("Editor fade opacity (%):") {
|
||||||
|
cell(editorFadeOpacitySlider)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
titledRow(aceString("colorsHeading")) {
|
|
||||||
row(aceString("jumpModeColorLabel")) { short(jumpModeColorWheel) }
|
|
||||||
row(aceString("jumpEndModeColorLabel")) { short(jumpEndModeColorWheel) }
|
|
||||||
row(aceString("targetModeColorLabel")) { short(targetModeColorWheel) }
|
|
||||||
row(aceString("definitionModeColorLabel")) { short(definitionModeColorWheel) }
|
|
||||||
row(aceString("textHighlightColorLabel")) { short(textHighlightColorWheel) }
|
|
||||||
row(aceString("tagForegroundColorLabel")) { short(tagForegroundColorWheel) }
|
|
||||||
row(aceString("tagBackgroundColorLabel")) { short(tagBackgroundColorWheel) }
|
|
||||||
}
|
|
||||||
|
|
||||||
titledRow(aceString("appearanceHeading")) {
|
|
||||||
row { short(displayQueryCheckBox.apply { text = aceString("displayQueryLabel") }) }
|
|
||||||
row { short(roundedTagCornersCheckBox.apply { text = aceString("roundedTagCornersLabel") }) }
|
|
||||||
}
|
|
||||||
|
|
||||||
titledRow(aceString("behaviorHeading")) {
|
|
||||||
row { short(searchWholeFileCheckBox.apply { text = aceString("searchWholeFileLabel") }) }
|
|
||||||
row { short(supportPinyinCheckBox.apply { text = aceString("supportPinyin") }) }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Property-to-property delegation: https://stackoverflow.com/q/45074596/1772342
|
// Property-to-property delegation: https://stackoverflow.com/q/45074596/1772342
|
||||||
internal var allowedChars by tagCharsField
|
internal var allowedChars by tagAllowedCharsField
|
||||||
internal var keyboardLayout by keyboardLayoutCombo
|
internal var keyboardLayout by keyboardLayoutCombo
|
||||||
internal var keyChars by keyboardLayoutArea
|
internal var keyChars by keyboardLayoutArea
|
||||||
internal var cycleMode1 by cycleModeCombo1
|
internal var minQueryLength by minQueryLengthField
|
||||||
internal var cycleMode2 by cycleModeCombo2
|
internal var invertUppercaseMode by invertUppercaseModeCheckBox
|
||||||
internal var cycleMode3 by cycleModeCombo3
|
internal var editorFadeOpacity by editorFadeOpacitySlider
|
||||||
internal var cycleMode4 by cycleModeCombo4
|
|
||||||
internal var jumpModeColor by jumpModeColorWheel
|
internal var jumpModeColor by jumpModeColorWheel
|
||||||
internal var jumpEndModeColor by jumpEndModeColorWheel
|
internal var tagForegroundColor1 by tagForeground1ColorWheel
|
||||||
internal var targetModeColor by targetModeColorWheel
|
internal var tagForegroundColor2 by tagForeground2ColorWheel
|
||||||
internal var definitionModeColor by definitionModeColorWheel
|
internal var searchHighlightColor by searchHighlightColorWheel
|
||||||
internal var textHighlightColor by textHighlightColorWheel
|
|
||||||
internal var tagForegroundColor by tagForegroundColorWheel
|
internal var minQueryLengthInt
|
||||||
internal var tagBackgroundColor by tagBackgroundColorWheel
|
get() = minQueryLength.toIntOrNull()?.coerceIn(1, 10)
|
||||||
internal var displayQuery by displayQueryCheckBox
|
set(value) { minQueryLength = value.toString() }
|
||||||
internal var roundedTagCorners by roundedTagCornersCheckBox
|
|
||||||
internal var searchWholeFile by searchWholeFileCheckBox
|
internal var editorFadeOpacityPercent
|
||||||
internal var supportPinyin by supportPinyinCheckBox
|
get() = editorFadeOpacity * 10
|
||||||
|
set(value) { editorFadeOpacity = value / 10 }
|
||||||
|
|
||||||
fun reset(settings: AceSettings) {
|
fun reset(settings: AceSettings) {
|
||||||
allowedChars = settings.allowedChars
|
allowedChars = settings.allowedChars
|
||||||
keyboardLayout = settings.layout
|
keyboardLayout = settings.layout
|
||||||
cycleMode1 = settings.cycleMode1
|
minQueryLength = settings.minQueryLength.toString()
|
||||||
cycleMode2 = settings.cycleMode2
|
invertUppercaseMode = settings.invertUppercaseMode
|
||||||
cycleMode3 = settings.cycleMode3
|
editorFadeOpacityPercent = settings.editorFadeOpacity
|
||||||
cycleMode4 = settings.cycleMode4
|
|
||||||
jumpModeColor = settings.jumpModeColor
|
jumpModeColor = settings.jumpModeColor
|
||||||
jumpEndModeColor = settings.jumpEndModeColor
|
tagForegroundColor1 = settings.tagForegroundColor1
|
||||||
targetModeColor = settings.targetModeColor
|
tagForegroundColor2 = settings.tagForegroundColor2
|
||||||
definitionModeColor = settings.definitionModeColor
|
searchHighlightColor = settings.searchHighlightColor
|
||||||
textHighlightColor = settings.textHighlightColor
|
|
||||||
tagForegroundColor = settings.tagForegroundColor
|
|
||||||
tagBackgroundColor = settings.tagBackgroundColor
|
|
||||||
displayQuery = settings.displayQuery
|
|
||||||
roundedTagCorners = settings.roundedTagCorners
|
|
||||||
searchWholeFile = settings.searchWholeFile
|
|
||||||
supportPinyin = settings.supportPinyin
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Removal pending support for https://youtrack.jetbrains.com/issue/KT-8575
|
// Removal pending support for https://youtrack.jetbrains.com/issue/KT-8575
|
||||||
private operator fun JTextComponent.getValue(a: AceSettingsPanel, p: KProperty<*>) = text.toLowerCase()
|
|
||||||
|
private operator fun JTextComponent.getValue(a: AceSettingsPanel, p: KProperty<*>) = text.lowercase()
|
||||||
private operator fun JTextComponent.setValue(a: AceSettingsPanel, p: KProperty<*>, s: String) = setText(s)
|
private operator fun JTextComponent.setValue(a: AceSettingsPanel, p: KProperty<*>, s: String) = setText(s)
|
||||||
|
|
||||||
private operator fun ColorPanel.getValue(a: AceSettingsPanel, p: KProperty<*>) = selectedColor
|
private operator fun ColorPanel.getValue(a: AceSettingsPanel, p: KProperty<*>) = selectedColor
|
||||||
private operator fun ColorPanel.setValue(a: AceSettingsPanel, p: KProperty<*>, c: Color?) = setSelectedColor(c)
|
private operator fun ColorPanel.setValue(a: AceSettingsPanel, p: KProperty<*>, c: Color?) = setSelectedColor(c)
|
||||||
|
|
||||||
private operator fun JCheckBox.getValue(a: AceSettingsPanel, p: KProperty<*>) = isSelected
|
private operator fun JCheckBox.getValue(a: AceSettingsPanel, p: KProperty<*>) = isSelected
|
||||||
private operator fun JCheckBox.setValue(a: AceSettingsPanel, p: KProperty<*>, selected: Boolean) = setSelected(selected)
|
private operator fun JCheckBox.setValue(a: AceSettingsPanel, p: KProperty<*>, selected: Boolean) = setSelected(selected)
|
||||||
|
|
||||||
|
private operator fun JSlider.getValue(a: AceSettingsPanel, p: KProperty<*>) = value
|
||||||
|
private operator fun JSlider.setValue(a: AceSettingsPanel, p: KProperty<*>, value: Int) = setValue(value)
|
||||||
|
|
||||||
private operator fun <T> ComboBox<T>.getValue(a: AceSettingsPanel, p: KProperty<*>) = selectedItem as T
|
private operator fun <T> ComboBox<T>.getValue(a: AceSettingsPanel, p: KProperty<*>) = selectedItem as T
|
||||||
private operator fun <T> ComboBox<T>.setValue(a: AceSettingsPanel, p: KProperty<*>, item: T) = setSelectedItem(item)
|
private operator fun <T> ComboBox<T>.setValue(a: AceSettingsPanel, p: KProperty<*>, item: T) = setSelectedItem(item)
|
||||||
}
|
|
||||||
|
private inline fun <reified T : Enum<T>> ComboBox<T>.setupEnumItems(crossinline onChanged: (T) -> Unit) {
|
||||||
|
T::class.java.enumConstants.forEach(this::addItem)
|
||||||
|
addActionListener { onChanged(selectedItem as T) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@@ -3,12 +3,12 @@ package org.acejump.config
|
|||||||
import com.intellij.util.xmlb.Converter
|
import com.intellij.util.xmlb.Converter
|
||||||
import java.awt.Color
|
import java.awt.Color
|
||||||
|
|
||||||
class ColorConverter : Converter<Color>() {
|
internal class ColorConverter : Converter<Color>() {
|
||||||
override fun toString(value: Color): String {
|
override fun toString(value: Color): String {
|
||||||
return value.rgb.toString()
|
return value.rgb.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun fromString(value: String): Color? {
|
override fun fromString(value: String): Color? {
|
||||||
return value.toIntOrNull()?.let(::Color)
|
return value.toIntOrNull()?.let(::Color)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,132 +0,0 @@
|
|||||||
package org.acejump.control
|
|
||||||
|
|
||||||
import com.intellij.codeInsight.editorActions.SelectWordUtil
|
|
||||||
import com.intellij.openapi.actionSystem.AnActionEvent
|
|
||||||
import com.intellij.openapi.actionSystem.CommonDataKeys.EDITOR
|
|
||||||
import com.intellij.openapi.diagnostic.Logger
|
|
||||||
import com.intellij.openapi.diagnostic.debug
|
|
||||||
import com.intellij.openapi.editor.CaretState
|
|
||||||
import com.intellij.openapi.editor.ScrollType
|
|
||||||
import com.intellij.openapi.fileEditor.FileDocumentManager
|
|
||||||
import com.intellij.openapi.project.DumbAwareAction
|
|
||||||
import com.intellij.openapi.util.TextRange
|
|
||||||
import org.acejump.control.Handler.regexSearch
|
|
||||||
import org.acejump.label.Pattern
|
|
||||||
import org.acejump.label.Pattern.ALL_WORDS
|
|
||||||
import org.acejump.search.Finder
|
|
||||||
import org.acejump.search.JumpMode
|
|
||||||
import org.acejump.search.Jumper
|
|
||||||
import org.acejump.view.Boundary.*
|
|
||||||
import org.acejump.view.Model
|
|
||||||
import org.acejump.view.Model.boundaries
|
|
||||||
import org.acejump.view.Model.defaultBoundary
|
|
||||||
import org.acejump.view.Model.editor
|
|
||||||
import org.acejump.view.Model.viewBounds
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Entry point for all actions. The IntelliJ Platform calls AceJump here.
|
|
||||||
*/
|
|
||||||
|
|
||||||
sealed class AceAction: DumbAwareAction() {
|
|
||||||
val logger = Logger.getInstance(javaClass)
|
|
||||||
|
|
||||||
final override fun update(action: AnActionEvent) {
|
|
||||||
action.presentation.isEnabled = action.getData(EDITOR) != null
|
|
||||||
}
|
|
||||||
|
|
||||||
final override fun actionPerformed(e: AnActionEvent) {
|
|
||||||
editor = e.getData(EDITOR) ?: return
|
|
||||||
boundaries = defaultBoundary
|
|
||||||
logger.debug { "Invoked on ${FileDocumentManager.getInstance().getFile(editor.document)?.presentableName} (${editor.document.textLength})" }
|
|
||||||
Handler.activate()
|
|
||||||
invoke()
|
|
||||||
}
|
|
||||||
|
|
||||||
abstract fun invoke()
|
|
||||||
|
|
||||||
object ActivateOrCycleMode : AceAction() {
|
|
||||||
override fun invoke() = Jumper.cycleMode()
|
|
||||||
}
|
|
||||||
|
|
||||||
object ToggleJumpMode: AceAction() {
|
|
||||||
override fun invoke() = Jumper.toggleMode(JumpMode.JUMP)
|
|
||||||
}
|
|
||||||
|
|
||||||
object ToggleJumpEndMode: AceAction() {
|
|
||||||
override fun invoke() = Jumper.toggleMode(JumpMode.JUMP_END)
|
|
||||||
}
|
|
||||||
|
|
||||||
object ToggleSelectWordMode: AceAction() {
|
|
||||||
override fun invoke() = Jumper.toggleMode(JumpMode.TARGET)
|
|
||||||
}
|
|
||||||
|
|
||||||
object ToggleDefinitionMode: AceAction() {
|
|
||||||
override fun invoke() = Jumper.toggleMode(JumpMode.DEFINE)
|
|
||||||
}
|
|
||||||
|
|
||||||
object ToggleAllLinesMode: AceAction() {
|
|
||||||
override fun invoke() = regexSearch(Pattern.LINE_MARK)
|
|
||||||
}
|
|
||||||
|
|
||||||
object ToggleAllWordsMode: AceAction() {
|
|
||||||
override fun invoke() = regexSearch(ALL_WORDS, SCREEN_BOUNDARY)
|
|
||||||
}
|
|
||||||
|
|
||||||
object ToggleAllWordsForwardMode: AceAction() {
|
|
||||||
override fun invoke() = regexSearch(ALL_WORDS, AFTER_CARET_BOUNDARY)
|
|
||||||
}
|
|
||||||
|
|
||||||
object ToggleAllWordsBackwardsMode: AceAction() {
|
|
||||||
override fun invoke() = regexSearch(ALL_WORDS, BEFORE_CARET_BOUNDARY)
|
|
||||||
}
|
|
||||||
|
|
||||||
object ActOnHighlightedWords: AceAction() {
|
|
||||||
override fun invoke() = when(JumpMode.mode) {
|
|
||||||
JumpMode.DISABLED -> {}
|
|
||||||
JumpMode.JUMP -> if (editor.caretModel.supportsMultipleCarets()) jumpToAll() else Unit
|
|
||||||
JumpMode.JUMP_END -> if (editor.caretModel.supportsMultipleCarets()) jumpToWordEnds() else Unit
|
|
||||||
JumpMode.TARGET -> if (editor.caretModel.supportsMultipleCarets()) selectAllWords() else Unit
|
|
||||||
JumpMode.DEFINE -> {}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun jumpToAll() {
|
|
||||||
val carets = Finder.allResults().map {
|
|
||||||
CaretState(editor.offsetToLogicalPosition(it), null, null)
|
|
||||||
}
|
|
||||||
if (carets.isEmpty()) return
|
|
||||||
Handler.reset()
|
|
||||||
editor.caretModel.caretsAndSelections = carets
|
|
||||||
editor.scrollingModel.scrollToCaret(ScrollType.MAKE_VISIBLE)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun jumpToWordEnds() {
|
|
||||||
val ranges = ArrayList<TextRange>()
|
|
||||||
for (offset in Finder.allResults()) {
|
|
||||||
SelectWordUtil.addWordSelection(editor.settings.isCamelWords, Model.editorText, offset, ranges)
|
|
||||||
}
|
|
||||||
if (ranges.isEmpty()) return
|
|
||||||
|
|
||||||
Handler.reset()
|
|
||||||
editor.caretModel.caretsAndSelections = ranges.map {
|
|
||||||
CaretState(editor.offsetToLogicalPosition(it.endOffset), null, null)
|
|
||||||
}
|
|
||||||
editor.scrollingModel.scrollToCaret(ScrollType.MAKE_VISIBLE)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun selectAllWords() {
|
|
||||||
val ranges = ArrayList<TextRange>()
|
|
||||||
for (offset in Finder.allResults()) {
|
|
||||||
SelectWordUtil.addWordSelection(editor.settings.isCamelWords, Model.editorText, offset, ranges)
|
|
||||||
}
|
|
||||||
if (ranges.isEmpty()) return
|
|
||||||
|
|
||||||
Handler.reset()
|
|
||||||
editor.caretModel.caretsAndSelections = ranges.map {
|
|
||||||
val start = editor.offsetToLogicalPosition(it.startOffset)
|
|
||||||
val end = editor.offsetToLogicalPosition(it.endOffset)
|
|
||||||
CaretState(end, start, end)
|
|
||||||
}
|
|
||||||
editor.scrollingModel.scrollToCaret(ScrollType.MAKE_VISIBLE)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,22 +0,0 @@
|
|||||||
package org.acejump.control
|
|
||||||
|
|
||||||
import com.intellij.find.EditorSearchSession
|
|
||||||
import com.intellij.find.impl.livePreview.SearchResults
|
|
||||||
import com.intellij.openapi.actionSystem.AnAction
|
|
||||||
import com.intellij.openapi.editor.Editor
|
|
||||||
import org.acejump.search.AceFindModel
|
|
||||||
|
|
||||||
class AceSearchSession(editor: Editor, findModel: AceFindModel):
|
|
||||||
EditorSearchSession(editor, editor.project, findModel) {
|
|
||||||
init {
|
|
||||||
editor.headerComponent = component
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun searchResultsUpdated(sr: SearchResults) {
|
|
||||||
super.searchResultsUpdated(sr)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun createPrimarySearchActions(): Array<AnAction> {
|
|
||||||
return super.createPrimarySearchActions()
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,207 +0,0 @@
|
|||||||
package org.acejump.control
|
|
||||||
|
|
||||||
import com.intellij.openapi.actionSystem.DataContext
|
|
||||||
import com.intellij.openapi.actionSystem.IdeActions.*
|
|
||||||
import com.intellij.openapi.diagnostic.Logger
|
|
||||||
import com.intellij.openapi.editor.Caret
|
|
||||||
import com.intellij.openapi.editor.Editor
|
|
||||||
import com.intellij.openapi.editor.actionSystem.EditorActionHandler
|
|
||||||
import com.intellij.openapi.editor.actionSystem.EditorActionManager
|
|
||||||
import com.intellij.openapi.editor.actionSystem.TypedAction
|
|
||||||
import com.intellij.openapi.editor.actionSystem.TypedActionHandler
|
|
||||||
import com.intellij.openapi.editor.colors.EditorColors.CARET_COLOR
|
|
||||||
import com.intellij.openapi.editor.colors.EditorColors.TEXT_SEARCH_RESULT_ATTRIBUTES
|
|
||||||
import com.intellij.openapi.editor.colors.EditorColorsManager
|
|
||||||
import com.intellij.util.containers.ContainerUtil
|
|
||||||
import org.acejump.control.Scroller.restoreScroll
|
|
||||||
import org.acejump.control.Scroller.saveScroll
|
|
||||||
import org.acejump.label.Pattern
|
|
||||||
import org.acejump.label.Pattern.*
|
|
||||||
import org.acejump.label.Tagger
|
|
||||||
import org.acejump.search.*
|
|
||||||
import org.acejump.view.Boundary
|
|
||||||
import org.acejump.view.Boundary.FULL_FILE_BOUNDARY
|
|
||||||
import org.acejump.view.Canvas
|
|
||||||
import org.acejump.view.Canvas.bindCanvas
|
|
||||||
import org.acejump.view.Model
|
|
||||||
import org.acejump.view.Model.editor
|
|
||||||
import org.acejump.view.Model.setupCaret
|
|
||||||
import java.awt.Color
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handles all incoming keystrokes, IDE notifications, and UI updates.
|
|
||||||
*/
|
|
||||||
|
|
||||||
object Handler : TypedActionHandler, Resettable {
|
|
||||||
private val listeners: MutableList<AceJumpListener> = ContainerUtil.createLockFreeCopyOnWriteList()
|
|
||||||
private val logger = Logger.getInstance(Handler::class.java)
|
|
||||||
private var enabled = false
|
|
||||||
private val typingAction = TypedAction.getInstance()
|
|
||||||
private val oldHandler = typingAction.rawHandler
|
|
||||||
|
|
||||||
private val editorActionMap = mutableMapOf<String, EditorActionHandler>(
|
|
||||||
ACTION_EDITOR_BACKSPACE to makeHandler { clear() },
|
|
||||||
ACTION_EDITOR_START_NEW_LINE to makeHandler { Selector.select(false) },
|
|
||||||
ACTION_EDITOR_ENTER to makeHandler { Selector.select() },
|
|
||||||
ACTION_EDITOR_TAB to makeHandler { Scroller.scroll(true) },
|
|
||||||
ACTION_EDITOR_PREV_PARAMETER to makeHandler { Scroller.scroll(false) },
|
|
||||||
ACTION_EDITOR_MOVE_CARET_UP to makeHandler { regexSearch(CODE_INDENTS) },
|
|
||||||
ACTION_EDITOR_MOVE_CARET_LEFT to makeHandler { regexSearch(START_OF_LINE) },
|
|
||||||
ACTION_EDITOR_MOVE_CARET_RIGHT to makeHandler { regexSearch(END_OF_LINE) },
|
|
||||||
ACTION_EDITOR_MOVE_LINE_START to makeHandler { regexSearch(START_OF_LINE) },
|
|
||||||
ACTION_EDITOR_MOVE_LINE_END to makeHandler { regexSearch(END_OF_LINE) },
|
|
||||||
ACTION_EDITOR_ESCAPE to makeHandler { reset() }
|
|
||||||
)
|
|
||||||
|
|
||||||
private fun makeHandler(handle: () -> Unit) = object : EditorActionHandler() {
|
|
||||||
override fun doExecute(e: Editor, c: Caret?, dc: DataContext?) = handle()
|
|
||||||
}
|
|
||||||
|
|
||||||
@ExternalUsage
|
|
||||||
fun regexSearch(regex: Pattern, bounds: Boundary = FULL_FILE_BOUNDARY) =
|
|
||||||
Canvas.reset().also { Finder.search(regex, bounds) }
|
|
||||||
|
|
||||||
@ExternalUsage
|
|
||||||
fun customRegexSearch(regex: String, bounds: Boundary = FULL_FILE_BOUNDARY) =
|
|
||||||
Canvas.reset().also { Finder.search(regex, bounds) }
|
|
||||||
|
|
||||||
fun activate() = if (!enabled) configureEditor() else { }
|
|
||||||
|
|
||||||
private fun clear() {
|
|
||||||
applyTo(Tagger, Jumper, Finder, Canvas) { reset() }
|
|
||||||
repaintTagMarkers()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun installKeyHandler() = typingAction.setupRawHandler(this)
|
|
||||||
private fun uninstallKeyHandler() = typingAction.setupRawHandler(oldHandler)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* TODO: Integrate query highlighting with [AceSearchSession]
|
|
||||||
*/
|
|
||||||
|
|
||||||
var session: AceSearchSession? = null
|
|
||||||
override fun execute(editor: Editor, key: Char, dataContext: DataContext) {
|
|
||||||
logger.info("Intercepted keystroke: $key")
|
|
||||||
Finder.query += key // This will trigger an update
|
|
||||||
|
|
||||||
// if (session == null)
|
|
||||||
// session = AceSearchSession(editor, AceFindModel(key.toString()))
|
|
||||||
// else session?.findModel!!.stringToFind += key
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun configureEditor() =
|
|
||||||
editor.run {
|
|
||||||
enabled = true
|
|
||||||
saveScroll()
|
|
||||||
saveCaret()
|
|
||||||
saveColors()
|
|
||||||
runNow {
|
|
||||||
setupCaret()
|
|
||||||
bindCanvas()
|
|
||||||
swapActionHandler()
|
|
||||||
installKeyHandler()
|
|
||||||
Listener.enable()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun swapActionHandler() = EditorActionManager.getInstance().run {
|
|
||||||
editorActionMap.forEach { (actionId, handler) ->
|
|
||||||
editorActionMap[actionId] = getActionHandler(actionId)
|
|
||||||
setActionHandler(actionId, handler)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun repaintTagMarkers() {
|
|
||||||
if (Tagger.tagSelected) reset() else Canvas.jumpLocations = Tagger.markers
|
|
||||||
}
|
|
||||||
|
|
||||||
fun redoFind() {
|
|
||||||
runNow {
|
|
||||||
editor.run {
|
|
||||||
restoreCanvas()
|
|
||||||
bindCanvas()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Finder.query.isNotEmpty() || Tagger.regex) {
|
|
||||||
Finder.search()
|
|
||||||
repaintTagMarkers()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun reset() {
|
|
||||||
if (enabled) Listener.disable()
|
|
||||||
session = null
|
|
||||||
|
|
||||||
// In order to get Finder.query value, listeners should
|
|
||||||
// be placed before cleanup
|
|
||||||
listeners.forEach(AceJumpListener::finished)
|
|
||||||
clear()
|
|
||||||
editor.restoreSettings()
|
|
||||||
}
|
|
||||||
|
|
||||||
@ExternalUsage
|
|
||||||
fun addAceJumpListener(listener: AceJumpListener) {
|
|
||||||
listeners += listener
|
|
||||||
}
|
|
||||||
|
|
||||||
@ExternalUsage
|
|
||||||
fun removeAceJumpListener(listener: AceJumpListener) {
|
|
||||||
listeners -= listener
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun Editor.restoreSettings() = runNow {
|
|
||||||
enabled = false
|
|
||||||
swapActionHandler()
|
|
||||||
uninstallKeyHandler()
|
|
||||||
if(!isDisposed) {
|
|
||||||
restoreScroll()
|
|
||||||
restoreCanvas()
|
|
||||||
restoreCaret()
|
|
||||||
restoreColors()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun Editor.restoreCanvas() =
|
|
||||||
contentComponent.run {
|
|
||||||
remove(Canvas)
|
|
||||||
repaint()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun saveCaret() {
|
|
||||||
editor.settings.run {
|
|
||||||
Model.naturalBlock = isBlockCursor
|
|
||||||
Model.naturalBlink = isBlinkCaret
|
|
||||||
}
|
|
||||||
|
|
||||||
editor.caretModel.primaryCaret.visualAttributes.run {
|
|
||||||
Model.naturalCaretColor = color
|
|
||||||
?: EditorColorsManager.getInstance().globalScheme.getColor(CARET_COLOR)
|
|
||||||
?: Color.BLACK
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun saveColors() =
|
|
||||||
EditorColorsManager.getInstance().globalScheme
|
|
||||||
.getAttributes(TEXT_SEARCH_RESULT_ATTRIBUTES)
|
|
||||||
?.backgroundColor.let { Model.naturalHighlight = it }
|
|
||||||
|
|
||||||
private fun Editor.restoreCaret() =
|
|
||||||
runNow {
|
|
||||||
settings.isBlinkCaret = Model.naturalBlink
|
|
||||||
settings.isBlockCursor = Model.naturalBlock
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun Editor.restoreColors() =
|
|
||||||
runNow {
|
|
||||||
colorsScheme.run {
|
|
||||||
setAttributes(TEXT_SEARCH_RESULT_ATTRIBUTES,
|
|
||||||
getAttributes(TEXT_SEARCH_RESULT_ATTRIBUTES)
|
|
||||||
.apply { backgroundColor = Model.naturalHighlight })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
interface AceJumpListener {
|
|
||||||
fun finished() {}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,92 +0,0 @@
|
|||||||
package org.acejump.control
|
|
||||||
|
|
||||||
import com.intellij.openapi.diagnostic.Logger
|
|
||||||
import com.intellij.openapi.editor.event.VisibleAreaEvent
|
|
||||||
import com.intellij.openapi.editor.event.VisibleAreaListener
|
|
||||||
import org.acejump.control.Handler.redoFind
|
|
||||||
import org.acejump.label.Tagger
|
|
||||||
import org.acejump.search.getView
|
|
||||||
import org.acejump.view.Model.editor
|
|
||||||
import org.acejump.view.Model.viewBounds
|
|
||||||
import java.awt.event.FocusEvent
|
|
||||||
import java.awt.event.FocusListener
|
|
||||||
import javax.swing.event.AncestorEvent
|
|
||||||
import javax.swing.event.AncestorListener
|
|
||||||
import kotlin.system.measureTimeMillis
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Callback for GUI updates (e.g. resize, scroll). Ensures tags are painted to
|
|
||||||
* the screen whenever the view changes. Since visible tags are prioritized,
|
|
||||||
* there may be tags off-screen which have not yet been painted.
|
|
||||||
*/
|
|
||||||
|
|
||||||
internal object Listener: FocusListener, AncestorListener, VisibleAreaListener {
|
|
||||||
private val logger = Logger.getInstance(Listener::class.java)
|
|
||||||
private val redoFindTrigger = Trigger()
|
|
||||||
|
|
||||||
fun enable() =
|
|
||||||
// TODO: Do we really need `synchronized` here?
|
|
||||||
synchronized(this) {
|
|
||||||
editor.run {
|
|
||||||
component.addFocusListener(Listener)
|
|
||||||
component.addAncestorListener(Listener)
|
|
||||||
scrollingModel.addVisibleAreaListener(Listener)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun disable() =
|
|
||||||
// TODO: Do we really need `synchronized` here?
|
|
||||||
synchronized(this) {
|
|
||||||
editor.run {
|
|
||||||
component.removeFocusListener(Listener)
|
|
||||||
component.removeAncestorListener(Listener)
|
|
||||||
scrollingModel.removeVisibleAreaListener(Listener)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This callback is very jittery. We need to delay repainting tags by a short
|
|
||||||
* duration [Trigger] in order to prevent flashing tag syndrome.
|
|
||||||
*/
|
|
||||||
|
|
||||||
override fun visibleAreaChanged(e: VisibleAreaEvent) {
|
|
||||||
var elapsed = measureTimeMillis {
|
|
||||||
if (canTagsSurviveViewResize()) {
|
|
||||||
viewBounds = editor.getView()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
elapsed = (350L - elapsed).coerceAtLeast(0L)
|
|
||||||
redoFindTrigger(withDelay = elapsed) {
|
|
||||||
logger.info("Visible area changed")
|
|
||||||
redoFind()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun canTagsSurviveViewResize() =
|
|
||||||
editor.getView().run {
|
|
||||||
if (first in viewBounds && last in viewBounds) return true
|
|
||||||
else if (Tagger.full) return true
|
|
||||||
else if (Tagger.regex) return false
|
|
||||||
else !Tagger.hasMatchBetweenOldAndNewView(viewBounds, this)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun ancestorMoved(ancestorEvent: AncestorEvent?) {
|
|
||||||
if (!canTagsSurviveViewResize()) {
|
|
||||||
logger.info("Ancestor moved: $ancestorEvent")
|
|
||||||
Handler.reset()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun ancestorAdded(ancestorEvent: AncestorEvent?) =
|
|
||||||
logger.info("Ancestor added: $ancestorEvent").also { Handler.reset() }
|
|
||||||
|
|
||||||
override fun ancestorRemoved(ancestorEvent: AncestorEvent?) =
|
|
||||||
logger.info("Ancestor removed: $ancestorEvent").also { Handler.reset() }
|
|
||||||
|
|
||||||
override fun focusLost(focusEvent: FocusEvent?) =
|
|
||||||
logger.info("Focus lost: $focusEvent").also { Handler.reset() }
|
|
||||||
|
|
||||||
override fun focusGained(focusEvent: FocusEvent?) =
|
|
||||||
logger.info("Focus gained: $focusEvent").also { Handler.reset() }
|
|
||||||
}
|
|
@@ -1,101 +0,0 @@
|
|||||||
package org.acejump.control
|
|
||||||
|
|
||||||
import com.intellij.openapi.diagnostic.Logger
|
|
||||||
import com.intellij.openapi.editor.Editor
|
|
||||||
import com.intellij.openapi.editor.LogicalPosition
|
|
||||||
import com.intellij.openapi.editor.ScrollType.CENTER
|
|
||||||
import com.intellij.openapi.editor.ScrollType.MAKE_VISIBLE
|
|
||||||
import org.acejump.label.Tagger.textMatches
|
|
||||||
import org.acejump.search.*
|
|
||||||
import org.acejump.view.Model.editor
|
|
||||||
import org.acejump.view.Model.viewBounds
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Updates the editor's vertical scroll position to make search results visible.
|
|
||||||
* This will occur when the user presses TAB OR searches for text which does not
|
|
||||||
* currently appear on the screen. Once scrolling is complete, the [Listener]
|
|
||||||
* will [Trigger] an update that re-paint tags to the screen.
|
|
||||||
*/
|
|
||||||
|
|
||||||
object Scroller {
|
|
||||||
private val logger = Logger.getInstance(Scroller::class.java)
|
|
||||||
private var scrollX = 0
|
|
||||||
private var scrollY = 0
|
|
||||||
|
|
||||||
fun scroll(forward: Boolean = true): Boolean {
|
|
||||||
logger.info("Scrolling to ${if (forward) "next" else "previous"} match")
|
|
||||||
val position = if (forward) findNextPosition() else findPreviousPosition()
|
|
||||||
return if (position != null) true.also { scrollTo(position) }
|
|
||||||
else false.also { logger.info("No result found") }
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun scrollTo(position: LogicalPosition) = editor.run {
|
|
||||||
scrollingModel.disableAnimation()
|
|
||||||
scrollingModel.scrollTo(position, CENTER)
|
|
||||||
|
|
||||||
val firstInView = textMatches.first { it in getView() }
|
|
||||||
val horizontalOffset = offsetToLogicalPosition(firstInView).column
|
|
||||||
if (horizontalOffset > scrollingModel.visibleArea.width)
|
|
||||||
scrollingModel.scrollHorizontally(horizontalOffset)
|
|
||||||
|
|
||||||
viewBounds = getView()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun ensureCaretVisible() = editor.scrollingModel.run {
|
|
||||||
val initialArea = visibleArea
|
|
||||||
scrollToCaret(MAKE_VISIBLE)
|
|
||||||
visibleArea == initialArea
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun findPreviousPosition(): LogicalPosition? {
|
|
||||||
val prevIndex = textMatches.toList().dropLastWhile { it > viewBounds.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 <= viewBounds.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()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun Editor.saveScroll() {
|
|
||||||
scrollX = scrollingModel.horizontalScrollOffset
|
|
||||||
scrollY = scrollingModel.verticalScrollOffset
|
|
||||||
}
|
|
||||||
|
|
||||||
fun Editor.restoreScroll() {
|
|
||||||
if (caretModel.offset !in getView()) {
|
|
||||||
scrollingModel.scrollVertically(scrollY)
|
|
||||||
scrollingModel.scrollHorizontally(scrollX)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,31 +0,0 @@
|
|||||||
package org.acejump.control
|
|
||||||
|
|
||||||
import org.acejump.search.Finder
|
|
||||||
import org.acejump.search.Jumper
|
|
||||||
import org.acejump.view.Model.caretOffset
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Supports cyclical selection of tags using the ENTER/SHIFT+ENTER keys.
|
|
||||||
*
|
|
||||||
* @see [Handler.editorActionMap]
|
|
||||||
*/
|
|
||||||
|
|
||||||
object Selector {
|
|
||||||
fun select(forward: Boolean = true) {
|
|
||||||
val matches = nearestVisibleMatches(forward)
|
|
||||||
matches.ifEmpty { return }
|
|
||||||
Jumper.jumpTo(matches.first(), false)
|
|
||||||
val wasAlreadyVisible = Scroller.ensureCaretVisible()
|
|
||||||
if (matches.size == 1 && wasAlreadyVisible) Handler.reset()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun nearestVisibleMatches(forward: Boolean = true): List<Int> {
|
|
||||||
val caretOffset = caretOffset
|
|
||||||
|
|
||||||
return Finder.visibleResults().sortedWith(compareBy(
|
|
||||||
{ it == caretOffset },
|
|
||||||
{ (it <= caretOffset) == forward },
|
|
||||||
{ if (forward) it - caretOffset else caretOffset - it }
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,41 +0,0 @@
|
|||||||
package org.acejump.control
|
|
||||||
|
|
||||||
import com.intellij.openapi.diagnostic.Logger
|
|
||||||
import org.acejump.search.runLater
|
|
||||||
import java.util.concurrent.Executors
|
|
||||||
import java.util.concurrent.Future
|
|
||||||
import java.util.concurrent.TimeUnit
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Timer for triggering events with a designated delay.
|
|
||||||
*/
|
|
||||||
|
|
||||||
class Trigger {
|
|
||||||
private companion object {
|
|
||||||
private val executor = Executors.newSingleThreadScheduledExecutor()
|
|
||||||
}
|
|
||||||
|
|
||||||
private val logger = Logger.getInstance(Trigger::class.java)
|
|
||||||
private var task: Future<*>? = null
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Can be called multiple times inside [delay], but doing so will reset the
|
|
||||||
* timer, delaying the [event] from occurring by [withDelay] milliseconds.
|
|
||||||
*/
|
|
||||||
|
|
||||||
@Synchronized
|
|
||||||
operator fun invoke(withDelay: Long, event: () -> Unit = {}) {
|
|
||||||
task?.cancel(true)
|
|
||||||
task = executor.schedule({
|
|
||||||
runLater {
|
|
||||||
try {
|
|
||||||
event()
|
|
||||||
} catch (e: Exception) {
|
|
||||||
logger.error("Exception occurred while triggering event!", e)
|
|
||||||
} finally {
|
|
||||||
task = null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, withDelay, TimeUnit.MILLISECONDS)
|
|
||||||
}
|
|
||||||
}
|
|
38
src/main/kotlin/org/acejump/input/EditorKeyListener.kt
Normal file
38
src/main/kotlin/org/acejump/input/EditorKeyListener.kt
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package org.acejump.input
|
||||||
|
|
||||||
|
import com.intellij.openapi.actionSystem.DataContext
|
||||||
|
import com.intellij.openapi.editor.Editor
|
||||||
|
import com.intellij.openapi.editor.actionSystem.TypedAction
|
||||||
|
import com.intellij.openapi.editor.actionSystem.TypedActionHandler
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If at least one session exists, this listener redirects all characters, typed in [Editor]s with attached sessions, to the appropriate
|
||||||
|
* sessions' own handlers.
|
||||||
|
*/
|
||||||
|
internal object EditorKeyListener : TypedActionHandler {
|
||||||
|
private val attached = mutableMapOf<Editor, TypedActionHandler>()
|
||||||
|
private var originalHandler: TypedActionHandler? = null
|
||||||
|
|
||||||
|
override fun execute(editor: Editor, charTyped: Char, dataContext: DataContext) {
|
||||||
|
(attached[editor] ?: originalHandler ?: return).execute(editor, charTyped, dataContext)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun attach(editor: Editor, callback: TypedActionHandler) {
|
||||||
|
if (attached.isEmpty()) {
|
||||||
|
val typedAction = TypedAction.getInstance()
|
||||||
|
originalHandler = typedAction.rawHandler
|
||||||
|
typedAction.setupRawHandler(this)
|
||||||
|
}
|
||||||
|
|
||||||
|
attached[editor] = callback
|
||||||
|
}
|
||||||
|
|
||||||
|
fun detach(editor: Editor) {
|
||||||
|
attached.remove(editor)
|
||||||
|
|
||||||
|
if (attached.isEmpty()) {
|
||||||
|
originalHandler?.let(TypedAction.getInstance()::setupRawHandler)
|
||||||
|
originalHandler = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
52
src/main/kotlin/org/acejump/input/KeyLayout.kt
Normal file
52
src/main/kotlin/org/acejump/input/KeyLayout.kt
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
package org.acejump.input
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defines common keyboard layouts. Each layout has a key priority order, based on each key's distance from the home row and how
|
||||||
|
* ergonomically difficult they are to press.
|
||||||
|
*/
|
||||||
|
@Suppress("unused", "SpellCheckingInspection")
|
||||||
|
enum class KeyLayout(
|
||||||
|
internal val rows: Array<String>,
|
||||||
|
priority: String,
|
||||||
|
private val characterSides: Pair<Set<Char>, Set<Char>> = Pair(emptySet(), emptySet()),
|
||||||
|
internal val characterRemapping: Map<Char, Char> = emptyMap(),
|
||||||
|
) {
|
||||||
|
COLEMK(arrayOf("1234567890", "qwfpgjluy", "arstdhneio", "zxcvbkm"), priority = "tndhseriaovkcmbxzgjplfuwyq5849673210"),
|
||||||
|
WORKMN(arrayOf("1234567890", "qdrwbjfup", "ashtgyneoi", "zxmcvkl"), priority = "tnhegysoaiclvkmxzwfrubjdpq5849673210"),
|
||||||
|
DVORAK(arrayOf("1234567890", "pyfgcrl", "aoeuidhtns", "qjkxbmwvz"), priority = "uhetidonasxkbjmqwvzgfycprl5849673210"),
|
||||||
|
QWERTY(arrayOf("1234567890", "qwertyuiop", "asdfghjkl", "zxcvbnm"), priority = "fjghdkslavncmbxzrutyeiwoqp5849673210", characterSides = sides(listOf("123456", "qwert", "asdfg", "zxcvb"), listOf("7890", "yuiop", "hjkl", "nm"))),
|
||||||
|
QWERTZ(arrayOf("1234567890", "qwertzuiop", "asdfghjkl", "yxcvbnm"), priority = "fjghdkslavncmbxyrutzeiwoqp5849673210", characterSides = sides(listOf("123456", "qwert", "asdfg", "yxcvb"), listOf("7890", "zuiop", "hjkl", "nm"))),
|
||||||
|
QWERTZ_CZ(arrayOf("1234567890", "qwertzuiop", "asdfghjkl", "yxcvbnm"), priority = "fjghdkslavncmbxyrutzeiwoqp5849673210", characterSides = sides(listOf("123456", "qwert", "asdfg", "yxcvb"), listOf("7890", "zuiop", "hjkl", "nm")), characterRemapping = mapOf(
|
||||||
|
'+' to '1',
|
||||||
|
'ě' to '2',
|
||||||
|
'š' to '3',
|
||||||
|
'č' to '4',
|
||||||
|
'ř' to '5',
|
||||||
|
'ž' to '6',
|
||||||
|
'ý' to '7',
|
||||||
|
'á' to '8',
|
||||||
|
'í' to '9',
|
||||||
|
'é' to '0'
|
||||||
|
)),
|
||||||
|
QGMLWY(arrayOf("1234567890", "qgmlwyfub", "dstnriaeoh", "zxcvjkp"), priority = "naterisodhvkcpjxzlfmuwygbq5849673210"),
|
||||||
|
QGMLWB(arrayOf("1234567890", "qgmlwbyuv", "dstnriaeoh", "zxcfjkp"), priority = "naterisodhfkcpjxzlymuwbgvq5849673210"),
|
||||||
|
NORMAN(arrayOf("1234567890", "qwdfkjurl", "asetgynioh", "zxcvbpm"), priority = "tneigysoahbvpcmxzjkufrdlwq5849673210");
|
||||||
|
|
||||||
|
internal val allChars = rows.joinToString("").toCharArray().apply(CharArray::sort).joinToString("")
|
||||||
|
private val allPriorities = priority.mapIndexed { index, char -> char to index }.toMap()
|
||||||
|
|
||||||
|
fun priority(char: Char): Int {
|
||||||
|
return allPriorities[char] ?: allChars.length
|
||||||
|
}
|
||||||
|
|
||||||
|
fun areOnSameSide(c1: Char, c2: Char): Boolean {
|
||||||
|
return (c1 in characterSides.first && c2 in characterSides.first) || (c1 in characterSides.second && c2 in characterSides.second)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun sides(left: List<String>, right: List<String>): Pair<Set<Char>, Set<Char>> {
|
||||||
|
return Pair(
|
||||||
|
left.flatMapTo(mutableSetOf()) { it.toCharArray().toSet() },
|
||||||
|
right.flatMapTo(mutableSetOf()) { it.toCharArray().toSet() }
|
||||||
|
)
|
||||||
|
}
|
62
src/main/kotlin/org/acejump/input/KeyLayoutCache.kt
Normal file
62
src/main/kotlin/org/acejump/input/KeyLayoutCache.kt
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
package org.acejump.input
|
||||||
|
|
||||||
|
import org.acejump.config.AceSettings
|
||||||
|
import kotlin.math.pow
|
||||||
|
import kotlin.math.roundToInt
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stores data specific to the selected keyboard layout. We want to assign tags with easily reachable keys first, and ideally have tags
|
||||||
|
* with repeated keys (ex. FF, JJ) or adjacent keys (ex. GH, UJ).
|
||||||
|
*/
|
||||||
|
internal object KeyLayoutCache {
|
||||||
|
lateinit var allowedTagsSorted: List<String>
|
||||||
|
private set
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called before any lazily initialized properties are used, to ensure that they are initialized even if the settings are missing.
|
||||||
|
*/
|
||||||
|
fun ensureInitialized(settings: AceSettings) {
|
||||||
|
if (!::allowedTagsSorted.isInitialized) {
|
||||||
|
reset(settings)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Re-initializes cached data according to updated settings.
|
||||||
|
*/
|
||||||
|
fun reset(settings: AceSettings) {
|
||||||
|
val allowedChars = processCharList(settings.allowedChars).ifEmpty { processCharList(settings.layout.allChars) }
|
||||||
|
val allowedTags = mutableSetOf<String>()
|
||||||
|
|
||||||
|
for (c1 in allowedChars) {
|
||||||
|
allowedTags.add("$c1")
|
||||||
|
|
||||||
|
for (c2 in allowedChars) {
|
||||||
|
if (c1 != c2) {
|
||||||
|
allowedTags.add("$c1$c2")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
allowedTagsSorted = allowedTags.sortedBy { rankPriority(settings.layout, it) }
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun processCharList(charList: String): List<Char> {
|
||||||
|
return charList.toCharArray().map(Char::lowercaseChar).distinct()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun rankPriority(layout: KeyLayout, tag: String): Int {
|
||||||
|
val c1 = tag.first()
|
||||||
|
val p1 = (1.0 + layout.priority(c1)).pow(3)
|
||||||
|
|
||||||
|
if (tag.length == 1) {
|
||||||
|
return p1.roundToInt()
|
||||||
|
}
|
||||||
|
|
||||||
|
val c2 = tag.last()
|
||||||
|
val p2 = (1.0 + layout.priority(c2)).pow(3)
|
||||||
|
|
||||||
|
val multiplier = if (layout.areOnSameSide(c1, c2)) 2 else 1
|
||||||
|
return (((p1 * 50) + p2 + 1000) * multiplier).roundToInt()
|
||||||
|
}
|
||||||
|
}
|
@@ -1,72 +0,0 @@
|
|||||||
package org.acejump.label
|
|
||||||
|
|
||||||
import org.acejump.config.AceConfig
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Patterns related to key priority, separation, and regexps for line mode.
|
|
||||||
*/
|
|
||||||
|
|
||||||
enum class Pattern(val string: String) {
|
|
||||||
END_OF_LINE("\\n|\\Z"),
|
|
||||||
START_OF_LINE("^.|^\\n"),
|
|
||||||
CODE_INDENTS("[^\\s].*|^\\n"),
|
|
||||||
// START_OF_LINE("^[^\\n]{2,}|^\\n"),
|
|
||||||
// CODE_INDENTS("[^\\s][^\\n]{2,}|^\\n"),
|
|
||||||
LINE_MARK(END_OF_LINE.string + "|" +
|
|
||||||
START_OF_LINE.string + "|" +
|
|
||||||
CODE_INDENTS.string),
|
|
||||||
ALL_WORDS("(?<=[^a-zA-Z0-9_]|\\A)[a-zA-Z0-9_]");
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
val NUM_TAGS: Int
|
|
||||||
get() = NUM_CHARS * NUM_CHARS
|
|
||||||
|
|
||||||
val NUM_CHARS: Int
|
|
||||||
get() = AceConfig.allowedChars.length
|
|
||||||
|
|
||||||
fun filter(bigrams: Set<String>, query: String) =
|
|
||||||
bigrams.filter { !query.endsWith(it[0]) }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sorts available tags by key distance. Tags which are ergonomically easier
|
|
||||||
* to reach will be assigned first. We would prefer to use tags that contain
|
|
||||||
* repeated keys (ex. FF, JJ), and use tags that contain physically adjacent
|
|
||||||
* keys (ex. 12, 21) to keys that are located further apart on the keyboard.
|
|
||||||
*/
|
|
||||||
|
|
||||||
enum class KeyLayout(vararg val rows: String) {
|
|
||||||
COLEMK("1234567890", "qwfpgjluy", "arstdhneio", "zxcvbkm"),
|
|
||||||
WORKMN("1234567890", "qdrwbjfup", "ashtgyneoi", "zxmcvkl"),
|
|
||||||
DVORAK("1234567890", "pyfgcrl", "aoeuidhtns", "qjkxbmwvz"),
|
|
||||||
QWERTY("1234567890", "qwertyuiop", "asdfghjkl", "zxcvbnm"),
|
|
||||||
QWERTZ("1234567890", "qwertzuiop", "asdfghjkl", "yxcvbnm"),
|
|
||||||
QGMLWY("1234567890", "qgmlwyfub", "dstnriaeoh", "zxcvjkp"),
|
|
||||||
QGMLWB("1234567890", "qgmlwbyuv", "dstnriaeoh", "zxcfjkp"),
|
|
||||||
NORMAN("1234567890", "qwdfkjurl", "asetgynioh", "zxcvbpm");
|
|
||||||
|
|
||||||
private val priority by lazy {
|
|
||||||
when (this) {
|
|
||||||
QWERTY -> "fjghdkslavncmbxzrutyeiwoqp5849673210"
|
|
||||||
QWERTZ -> "fjghdkslavncmbxyrutzeiwoqp5849673210"
|
|
||||||
COLEMK -> "tndhseriaovkcmbxzgjplfuwyq5849673210"
|
|
||||||
DVORAK -> "uhetidonasxkbjmqwvzgfycprl5849673210"
|
|
||||||
NORMAN -> "tneigysoahbvpcmxzjkufrdlwq5849673210"
|
|
||||||
QGMLWY -> "naterisodhvkcpjxzlfmuwygbq5849673210"
|
|
||||||
QGMLWB -> "naterisodhfkcpjxzlymuwbgvq5849673210"
|
|
||||||
WORKMN -> "tnhegysoaiclvkmxzwfrubjdpq5849673210"
|
|
||||||
}.mapIndices()
|
|
||||||
}
|
|
||||||
|
|
||||||
val text by lazy {
|
|
||||||
joinBy("").toCharArray().sortedBy { priority[it] }.joinToString("")
|
|
||||||
}
|
|
||||||
|
|
||||||
fun priority(tagToChar: (String) -> Char): (String) -> Int? =
|
|
||||||
{ priority[tagToChar(it)] }
|
|
||||||
|
|
||||||
fun joinBy(separator: CharSequence) = rows.joinToString(separator)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun String.mapIndices() = mapIndexed { i, c -> c to i }.toMap()
|
|
@@ -1,210 +0,0 @@
|
|||||||
package org.acejump.label
|
|
||||||
|
|
||||||
import com.intellij.openapi.diagnostic.Logger
|
|
||||||
import it.unimi.dsi.fastutil.ints.*
|
|
||||||
import it.unimi.dsi.fastutil.objects.Object2IntMap
|
|
||||||
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap
|
|
||||||
import org.acejump.config.AceConfig
|
|
||||||
import org.acejump.search.wordBoundsPlus
|
|
||||||
import kotlin.math.max
|
|
||||||
import kotlin.system.measureTimeMillis
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Solves the Tag Assignment Problem. The tag assignment problem can be stated
|
|
||||||
* thusly: Given a set of indices I in document d, and a set of tags T, find a
|
|
||||||
* bijection f: T*⊂T → I*⊂I s.t. d[i..k] + t ∉ d[i'..(k + |t|)], ∀ i' ∈ I\{i},
|
|
||||||
* ∀ k ∈ (i, |d|-|t|], where t ∈ T, i ∈ I. Maximize |I*|. This can be relaxed
|
|
||||||
* to t=t[0] and ∀ k ∈ (i, i+K] for some fixed K, in most natural documents.
|
|
||||||
*
|
|
||||||
* More concretely, tags are typically two-character strings containing alpha-
|
|
||||||
* numeric symbols. Documents are plaintext files. Indices are produced by a
|
|
||||||
* search query of length N, i.e. the preceding N characters of every index i in
|
|
||||||
* document d are identical. For characters proceeding d[i], all bets are off.
|
|
||||||
* We can assume that P(d[i]|d[i-1]) has some structure for d~D. Ultimately, we
|
|
||||||
* want a fast algorithm which maximizes the number of tagged document indices.
|
|
||||||
*
|
|
||||||
* Tags are used by the typist to select indices within a document. To select an
|
|
||||||
* index, the typist starts by activating AceJump and searching for a character.
|
|
||||||
* As soon as the first character is received, we begin to scan the document for
|
|
||||||
* matching locations and assign as many valid tags as possible. When subsequent
|
|
||||||
* characters are received, we refine the search results to match either:
|
|
||||||
*
|
|
||||||
* 1.) The plaintext query alone, or
|
|
||||||
* 2.) The concatenation of plaintext query and partial tag
|
|
||||||
*
|
|
||||||
* The constraint in paragraph no. 1 tries to impose the following criteria:
|
|
||||||
*
|
|
||||||
* 1.) All valid key sequences will lead to a unique location in the document
|
|
||||||
* 2.) All indices in the document will be reachable by a short key sequence
|
|
||||||
*
|
|
||||||
* If there is an insufficient number of two-character tags to cover every index
|
|
||||||
* (which typically occurs when the user searches for a common character within
|
|
||||||
* a long document), then we attempt to maximize the number of tags assigned to
|
|
||||||
* document indices. The key is, all tags must be assigned as soon as possible,
|
|
||||||
* i.e. as soon as the first character is received or whenever the user ceases
|
|
||||||
* typing (at the very latest). Once assigned, a visible tag must never change
|
|
||||||
* at any time during the selection process, so as not to confuse the user.
|
|
||||||
*/
|
|
||||||
|
|
||||||
class Solver(val text: String,
|
|
||||||
val query: String,
|
|
||||||
val results: Collection<Int>,
|
|
||||||
val availableTags: Set<String>,
|
|
||||||
val viewBounds: IntRange = 0..text.length) {
|
|
||||||
private val logger = Logger.getInstance(Solver::class.java)
|
|
||||||
private var newTags: Object2IntMap<String> =
|
|
||||||
Object2IntOpenHashMap(Pattern.NUM_TAGS)
|
|
||||||
private val newTagIndices: IntSet = IntOpenHashSet()
|
|
||||||
private var strings: Set<String> =
|
|
||||||
HashSet(results.flatMap { getWordFragments(it) })
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Iterates through remaining available tags, until we find one matching our
|
|
||||||
* criteria, i.e. does not collide with an existing tag or plaintext string.
|
|
||||||
*
|
|
||||||
* @param tag the tag string which is to be assigned
|
|
||||||
* @param sites potential indices where a tag may be assigned
|
|
||||||
*/
|
|
||||||
|
|
||||||
private fun tryToAssignTag(tag: String, sites: IntArray): Boolean {
|
|
||||||
if (newTags.containsKey(tag)) return false
|
|
||||||
val index = sites.firstOrNull { it !in newTagIndices } ?: return false
|
|
||||||
@Suppress("ReplacePutWithAssignment")
|
|
||||||
newTags.put(tag, index)
|
|
||||||
newTagIndices.add(index)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Ensures tag conservation. Most tags prefer to occupy certain sites during
|
|
||||||
* assignment, since not all tags may be assigned to all sites. Therefore, we
|
|
||||||
* must spend our tag "budget" wisely, in order to cover the most sites with
|
|
||||||
* the tags we have at our disposal. We should consider the "most restrictive"
|
|
||||||
* tags first, since they have the least chance of being available as further
|
|
||||||
* sites are assigned.
|
|
||||||
*
|
|
||||||
* Tags which are compatible with the fewest sites should have preference for
|
|
||||||
* first assignment. Here we ensure that scarce tags are prioritized for their
|
|
||||||
* subsequent binding to available sites.
|
|
||||||
*
|
|
||||||
* @see isCompatibleWithTagChar This defines how tags may be assigned to sites
|
|
||||||
*/
|
|
||||||
|
|
||||||
private val tagOrder = AceConfig.defaultTagOrder
|
|
||||||
.thenComparingInt { eligibleSitesByTag.getValue(it).size }
|
|
||||||
.thenBy(AceConfig.layout.priority { it.last() })
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sorts jump targets to determine which positions get first choice for tags,
|
|
||||||
* by taking into account the structure of the surrounding text. For example,
|
|
||||||
* if the jump target is the first letter in a word, it is advantageous to
|
|
||||||
* prioritize this location (in case we run out of tags), since the typist is
|
|
||||||
* more likely to target words by their leading character.
|
|
||||||
*/
|
|
||||||
|
|
||||||
private val siteOrder: IntComparator = IntComparator { a, b ->
|
|
||||||
val aInBounds = a in viewBounds
|
|
||||||
val bInBounds = b in viewBounds
|
|
||||||
|
|
||||||
if (aInBounds != bInBounds) {
|
|
||||||
// Sites in immediate view should come first
|
|
||||||
return@IntComparator if (aInBounds) -1 else 1
|
|
||||||
}
|
|
||||||
|
|
||||||
val aIsNotFirstLetter = text[max(0, a - 1)].isLetterOrDigit()
|
|
||||||
val bIsNotFirstLetter = text[max(0, b - 1)].isLetterOrDigit()
|
|
||||||
|
|
||||||
if (aIsNotFirstLetter != bIsNotFirstLetter) {
|
|
||||||
// Ensure that the first letter of a word is prioritized for tagging
|
|
||||||
return@IntComparator if (bIsNotFirstLetter) -1 else 1
|
|
||||||
}
|
|
||||||
|
|
||||||
when {
|
|
||||||
a < b -> -1
|
|
||||||
a > b -> 1
|
|
||||||
else -> 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private val eligibleSitesByTag: MutableMap<String, IntList> = HashMap(100)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Maps tags to search results according to the following constraints.
|
|
||||||
*
|
|
||||||
* 1. A tag's first letter must not match any letters of the covered word.
|
|
||||||
* 2. Once assigned, a tag must never change until it has been selected. *A.
|
|
||||||
*
|
|
||||||
* Tags *should* have the following properties:
|
|
||||||
*
|
|
||||||
* A. Should be as short as possible. A tag may be "compacted" later.
|
|
||||||
* B. Should prefer keys that are physically closer on a QWERTY keyboard.
|
|
||||||
*
|
|
||||||
* @return A list of all tags and their corresponding indices
|
|
||||||
*/
|
|
||||||
|
|
||||||
fun map(): Map<String, Int> {
|
|
||||||
var totalAssigned = 0
|
|
||||||
var timeAssigned = 0L
|
|
||||||
val timeElapsed = measureTimeMillis {
|
|
||||||
val tagsByFirstLetter = availableTags.groupBy { it[0] }
|
|
||||||
for (site in results) {
|
|
||||||
for (tag in tagsByFirstLetter.getTagsCompatibleWith(site)) {
|
|
||||||
eligibleSitesByTag.getOrPut(tag){ IntArrayList(10) }.add(site)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val sortedTags = eligibleSitesByTag.keys.toMutableList().apply {
|
|
||||||
sortWith(tagOrder)
|
|
||||||
}
|
|
||||||
|
|
||||||
val matchingSites = HashMap<IntList, IntArray>()
|
|
||||||
val matchingSitesAsArrays = HashMap<String, IntArray>()
|
|
||||||
|
|
||||||
for ((key, value) in eligibleSitesByTag.entries) {
|
|
||||||
matchingSitesAsArrays[key] = matchingSites.getOrPut(value){
|
|
||||||
value.toIntArray().apply { IntArrays.mergeSort(this, siteOrder) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
timeAssigned = measureTimeMillis {
|
|
||||||
for (tag in sortedTags) {
|
|
||||||
if (totalAssigned == results.size) break
|
|
||||||
if (tryToAssignTag(tag, matchingSitesAsArrays.getValue(tag)))
|
|
||||||
totalAssigned++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.run {
|
|
||||||
info("results size: ${results.size}")
|
|
||||||
info("newTags size: ${newTags.size}")
|
|
||||||
info("Time elapsed: $timeElapsed ms")
|
|
||||||
info("Total assign: $totalAssigned")
|
|
||||||
info("Completed in: $timeAssigned ms")
|
|
||||||
}
|
|
||||||
|
|
||||||
return newTags
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun Map<Char, List<String>>.getTagsCompatibleWith(site: Int) =
|
|
||||||
entries.flatMap { (firstLetter, tags) ->
|
|
||||||
if (site isCompatibleWithTagChar firstLetter) tags else emptyList()
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns true IFF the tag, when inserted at any position in the word, could
|
|
||||||
* match an existing substring elsewhere in the editor text. We should never
|
|
||||||
* assign a tag which can be partly completed by typing plaintext.
|
|
||||||
*/
|
|
||||||
|
|
||||||
private infix fun Int.isCompatibleWithTagChar(char: Char) =
|
|
||||||
getWordFragments(this).map { it + char }.none { it in strings }
|
|
||||||
|
|
||||||
private fun getWordFragments(site: Int): List<String> {
|
|
||||||
val left = max(0, site + query.length - 1)
|
|
||||||
val right = text.wordBoundsPlus(site).second
|
|
||||||
val lowercase = text.substring(left, right).toLowerCase()
|
|
||||||
|
|
||||||
return (0..(right - left)).map { lowercase.substring(0, it) }
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,319 +0,0 @@
|
|||||||
package org.acejump.label
|
|
||||||
|
|
||||||
import com.google.common.collect.BiMap
|
|
||||||
import com.google.common.collect.HashBiMap
|
|
||||||
import com.intellij.openapi.diagnostic.Logger
|
|
||||||
import org.acejump.config.AceConfig
|
|
||||||
import org.acejump.control.Scroller
|
|
||||||
import org.acejump.search.AceFindModel
|
|
||||||
import org.acejump.search.Finder
|
|
||||||
import org.acejump.search.Jumper
|
|
||||||
import org.acejump.search.Resettable
|
|
||||||
import org.acejump.search.canIndicesBeSimultaneouslyVisible
|
|
||||||
import org.acejump.search.getFeasibleRegion
|
|
||||||
import org.acejump.search.runNow
|
|
||||||
import org.acejump.view.Marker
|
|
||||||
import org.acejump.view.Model.editor
|
|
||||||
import org.acejump.view.Model.editorText
|
|
||||||
import org.acejump.view.Model.viewBounds
|
|
||||||
import java.util.*
|
|
||||||
import kotlin.collections.Iterable
|
|
||||||
import kotlin.collections.List
|
|
||||||
import kotlin.collections.Map
|
|
||||||
import kotlin.collections.Set
|
|
||||||
import kotlin.collections.all
|
|
||||||
import kotlin.collections.any
|
|
||||||
import kotlin.collections.component1
|
|
||||||
import kotlin.collections.component2
|
|
||||||
import kotlin.collections.contains
|
|
||||||
import kotlin.collections.emptyList
|
|
||||||
import kotlin.collections.filter
|
|
||||||
import kotlin.collections.firstOrNull
|
|
||||||
import kotlin.collections.isNotEmpty
|
|
||||||
import kotlin.collections.iterator
|
|
||||||
import kotlin.collections.joinToString
|
|
||||||
import kotlin.collections.lastOrNull
|
|
||||||
import kotlin.collections.map
|
|
||||||
import kotlin.collections.mapKeysTo
|
|
||||||
import kotlin.collections.none
|
|
||||||
import kotlin.collections.partition
|
|
||||||
import kotlin.collections.plus
|
|
||||||
import kotlin.collections.sortedSetOf
|
|
||||||
import kotlin.collections.sortedWith
|
|
||||||
import kotlin.collections.toMap
|
|
||||||
import kotlin.collections.toSortedSet
|
|
||||||
import kotlin.collections.zip
|
|
||||||
import kotlin.streams.toList
|
|
||||||
import kotlin.system.measureTimeMillis
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The [Tagger] works with [Finder] to assign selectable tags to search results
|
|
||||||
* in the editor. These tags may be selected by typing their label at any point
|
|
||||||
* during the search. Since there is no explicit signal to begin selecting a tag
|
|
||||||
* when AceJump is active, we must infer when a tag is being selected. We do so
|
|
||||||
* by carefully assigning tags to search results so that every search result can
|
|
||||||
* be expanded indefinitely and selected unambiguously any time the user wishes.
|
|
||||||
*
|
|
||||||
* To do so, we must solve a tag assignment problem, where each search result is
|
|
||||||
* assigned an available tag. The Tagger (1) identifies available tags (2) uses
|
|
||||||
* the [Solver] to assign them, and (3) determines when a previously assigned
|
|
||||||
* tag has been selected, then (4) calls [Jumper] to reposition the caret.
|
|
||||||
*/
|
|
||||||
|
|
||||||
object Tagger : Resettable {
|
|
||||||
var markers: List<Marker> = emptyList()
|
|
||||||
private set
|
|
||||||
var regex = false
|
|
||||||
private set
|
|
||||||
var query = ""
|
|
||||||
private set
|
|
||||||
|
|
||||||
@Volatile
|
|
||||||
var full = false // Tracks whether all search results were successfully tagged
|
|
||||||
var textMatches: SortedSet<Int> = sortedSetOf()
|
|
||||||
@Volatile
|
|
||||||
var tagMap: BiMap<String, Int> = HashBiMap.create()
|
|
||||||
private val logger = Logger.getInstance(Tagger::class.java)
|
|
||||||
|
|
||||||
@Volatile
|
|
||||||
var tagSelected = false
|
|
||||||
|
|
||||||
private fun Iterable<Int>.noneInView() = none { it in viewBounds }
|
|
||||||
|
|
||||||
fun markOrJump(model: AceFindModel, results: SortedSet<Int>) {
|
|
||||||
model.run {
|
|
||||||
if (!regex) regex = isRegularExpressions
|
|
||||||
query = if (regex) " $stringToFind" else stringToFind.mapIndexed { i, c ->
|
|
||||||
if (i == 0) c else c.toLowerCase()
|
|
||||||
}.joinToString("")
|
|
||||||
logger.info("Received query: \"$query\"")
|
|
||||||
}
|
|
||||||
|
|
||||||
val availableTags = AceConfig.getCompatibleTags(query) { it !in tagMap }
|
|
||||||
|
|
||||||
measureTimeMillis { textMatches = refineSearchResults(results) }
|
|
||||||
.let { if (!regex) logger.info("Refined search results in $it ms") }
|
|
||||||
|
|
||||||
giveJumpOpportunity()
|
|
||||||
if (!tagSelected && query.isNotEmpty()) mark(textMatches, availableTags)
|
|
||||||
|
|
||||||
if (1 < query.length && tagMap.values.noneInView())
|
|
||||||
runNow { Scroller.scroll() }
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Narrows down results that need to be tagged. For example, "eee" need not be
|
|
||||||
* tagged three times. Furthermore, we will not be able to tag every location
|
|
||||||
* in a very large document.
|
|
||||||
*/
|
|
||||||
|
|
||||||
private fun refineSearchResults(results: SortedSet<Int>): SortedSet<Int> {
|
|
||||||
if (regex) return results
|
|
||||||
|
|
||||||
val admittance: (t: Int) -> Boolean = { editorText.admitsTagAtLocation(it) }
|
|
||||||
val sites = if (results.size < 500) results.filter(admittance)
|
|
||||||
else results.parallelStream().filter(admittance).toList()
|
|
||||||
|
|
||||||
val discards = results.size - sites.size
|
|
||||||
discards.let { if (it > 0) logger.info("Discarded $it unsuitable results") }
|
|
||||||
|
|
||||||
return sites.toSortedSet()
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns whether a given index inside a String can be tagged with a two-
|
|
||||||
* character tag (either to the left or right) without visually overlapping
|
|
||||||
* any nearby tags.
|
|
||||||
*/
|
|
||||||
|
|
||||||
private fun String.admitsTagAtLocation(loc: Int) = when {
|
|
||||||
1 < query.length -> true
|
|
||||||
loc - 1 < 0 -> true
|
|
||||||
loc + 1 >= length -> true
|
|
||||||
this[loc] isUnlike this[loc - 1] -> true
|
|
||||||
this[loc] isUnlike this[loc + 1] -> true
|
|
||||||
this[loc] != this[loc - 1] -> true
|
|
||||||
this[loc] != this[loc + 1] -> true
|
|
||||||
this[loc + 1] == '\r' || this[loc + 1] == '\n' -> true
|
|
||||||
this[loc - 1] == this[loc] && this[loc] == this[loc + 1] -> false
|
|
||||||
this[loc + 1].isWhitespace() && this[(loc + 2)
|
|
||||||
.coerceAtMost(length - 1)].isWhitespace() -> true
|
|
||||||
else -> false
|
|
||||||
}
|
|
||||||
|
|
||||||
private infix fun Char.isUnlike(other: Char) =
|
|
||||||
this.isLetterOrDigit() xor other.isLetterOrDigit() ||
|
|
||||||
this.isWhitespace() xor other.isWhitespace()
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks whether a visible tag has been selected, and if so, jumps to it.
|
|
||||||
*/
|
|
||||||
|
|
||||||
private fun giveJumpOpportunity() =
|
|
||||||
tagMap.entries.firstOrNull { it.value in viewBounds && it solves query }
|
|
||||||
?.run {
|
|
||||||
logger.info("User selected tag: ${key.toUpperCase()}")
|
|
||||||
tagSelected = true
|
|
||||||
Jumper.jumpTo(value)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns true if and only if a tag location is unambiguously completed by a
|
|
||||||
* given query. This can only happen if the query matches the underlying text,
|
|
||||||
* AND ends with the tag in question. Tags are case-insensitive.
|
|
||||||
*/
|
|
||||||
|
|
||||||
private infix fun Map.Entry<String, Int>.solves(query: String) =
|
|
||||||
query.endsWith(key, true) && isCompatibleWithQuery(query)
|
|
||||||
|
|
||||||
private fun Map.Entry<String, Int>.isCompatibleWithQuery(query: String) =
|
|
||||||
query.getPlaintextPortion(key).let { text ->
|
|
||||||
regex || editorText.regionMatches(
|
|
||||||
thisOffset = value,
|
|
||||||
other = text,
|
|
||||||
otherOffset = 0,
|
|
||||||
length = text.length,
|
|
||||||
ignoreCase = true
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun mark(results: SortedSet<Int>, availableTags: Set<String>) =
|
|
||||||
assignMissingTags(results, availableTags).compact().apply {
|
|
||||||
tagMap = this
|
|
||||||
markers = if(results.isEmpty()) // Last query char must be a tag char
|
|
||||||
tagMap.map { (tag, index) -> Marker(query, tag, index) }
|
|
||||||
else results.map { Marker(query, tagMap.inverse()[it], it) }
|
|
||||||
full = markers.size == tagMap.size
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Shortens previously assigned tags. Two-character tags may be shortened to
|
|
||||||
* one-character tags if and only if:
|
|
||||||
*
|
|
||||||
* 1. The shortened tag is unique among the set of visible tags.
|
|
||||||
* 3. The query does not end with the shortened tag, in whole or part.
|
|
||||||
*/
|
|
||||||
|
|
||||||
private fun Map<String, Int>.compact(): HashBiMap<String, Int> {
|
|
||||||
var timeElapsed = System.currentTimeMillis()
|
|
||||||
var totalCompacted = 0
|
|
||||||
val compacted = mapKeysTo(HashBiMap.create(size)) { e ->
|
|
||||||
val tag = e.key
|
|
||||||
if (e.value !in viewBounds) return@mapKeysTo tag
|
|
||||||
// Avoid matching query - will trigger a jump. TODO: lift this constraint.
|
|
||||||
val queryEndsWith = query.endsWith(tag[0]) || query.endsWith(tag)
|
|
||||||
return@mapKeysTo if (!queryEndsWith && tag.canBeShortened(this)) {
|
|
||||||
totalCompacted++
|
|
||||||
tag[0].toString()
|
|
||||||
} else tag
|
|
||||||
}
|
|
||||||
|
|
||||||
timeElapsed = System.currentTimeMillis() - timeElapsed
|
|
||||||
logger.info("Compacted $totalCompacted visible tags in $timeElapsed ms")
|
|
||||||
return compacted
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun String.canBeShortened(tagMap: Map<String, Int>): Boolean {
|
|
||||||
var i = 0
|
|
||||||
var canBeShortened = true
|
|
||||||
|
|
||||||
runNow {
|
|
||||||
for (tag in tagMap) {
|
|
||||||
if (tag.key[0] == this[0] &&
|
|
||||||
editor.canIndicesBeSimultaneouslyVisible(tagMap[this]!!, tag.value))
|
|
||||||
i++
|
|
||||||
if (1 < i) {
|
|
||||||
canBeShortened = false; break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return canBeShortened
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Assigns [availableTags] to [results]. Initially, all results are vacant.
|
|
||||||
* If there are any untagged results visible, assign as many tags as possible.
|
|
||||||
* Assuming all visible tags have been assigned, there is nothing left to do.
|
|
||||||
*/
|
|
||||||
|
|
||||||
private fun assignMissingTags(results: Set<Int>,
|
|
||||||
availableTags: Set<String>): Map<String, Int> {
|
|
||||||
var timeElapsed = System.currentTimeMillis()
|
|
||||||
val oldTags = transferExistingTagsCompatibleWithQuery()
|
|
||||||
// Ongoing queries with results in view do not need further tag assignment
|
|
||||||
oldTags.run {
|
|
||||||
if (regex && isNotEmpty() && values.all { it in viewBounds }) return this
|
|
||||||
else if (hasTagSuffixInView(query)) return this
|
|
||||||
}
|
|
||||||
|
|
||||||
val remainder = getFeasibleSites(results)
|
|
||||||
val (onScreen, offScreen) = remainder.partition { it in viewBounds }
|
|
||||||
val completeResultSet = onScreen + offScreen
|
|
||||||
// Some results are untagged. Let's assign some tags!
|
|
||||||
val vacantResults = completeResultSet.filter { it !in oldTags.values }
|
|
||||||
|
|
||||||
logger.run {
|
|
||||||
timeElapsed = System.currentTimeMillis() - timeElapsed
|
|
||||||
info("Results on screen: ${onScreen.size}, off screen: ${offScreen.size}")
|
|
||||||
info("Vacant Results: ${vacantResults.size}")
|
|
||||||
info("Available Tags: ${availableTags.size}")
|
|
||||||
info("Time elapsed: $timeElapsed ms")
|
|
||||||
}
|
|
||||||
|
|
||||||
return if (regex) solveRegex(vacantResults, availableTags) else oldTags +
|
|
||||||
Solver(editorText, query, vacantResults, availableTags, viewBounds).map()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getFeasibleSites(results: Set<Int>): List<Int> {
|
|
||||||
val feasibleRegion = getFeasibleRegion(results)
|
|
||||||
val remainder = results.partition { it in feasibleRegion }
|
|
||||||
remainder.second.size.let { if (it > 0) logger.info("Discarded $it OOBs") }
|
|
||||||
return remainder.first
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun solveRegex(vacantResults: List<Int>, availableTags: Set<String>) =
|
|
||||||
availableTags.sortedWith(AceConfig.defaultTagOrder).zip(vacantResults).toMap()
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Adds pre-existing tags where search string and tag overlap. For example,
|
|
||||||
* tags starting with the last character of the query will be included. Tags
|
|
||||||
* that no longer match the query will be discarded.
|
|
||||||
*/
|
|
||||||
|
|
||||||
private fun transferExistingTagsCompatibleWithQuery() =
|
|
||||||
tagMap.filter { it.isCompatibleWithQuery(query) || it.value in textMatches }
|
|
||||||
|
|
||||||
override fun reset() {
|
|
||||||
regex = false
|
|
||||||
full = false
|
|
||||||
textMatches = sortedSetOf()
|
|
||||||
tagMap = HashBiMap.create()
|
|
||||||
query = ""
|
|
||||||
markers = emptyList()
|
|
||||||
tagSelected = false
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun String.getPlaintextPortion(tag: String) = when {
|
|
||||||
endsWith(tag, true) -> dropLast(tag.length)
|
|
||||||
endsWith(tag.first(), true) -> dropLast(1)
|
|
||||||
else -> this
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns true if the Tagger contains a match in the new view, that is not
|
|
||||||
* contained (visible) in the old view. This method assumes that [textMatches]
|
|
||||||
* are in ascending order by index.
|
|
||||||
*
|
|
||||||
* @return true if there is a match in the new range not in the old range
|
|
||||||
*/
|
|
||||||
|
|
||||||
fun hasMatchBetweenOldAndNewView(old: IntRange, new: IntRange) =
|
|
||||||
textMatches.lastOrNull { it < old.first } ?: -1 >= new.first ||
|
|
||||||
textMatches.firstOrNull { it > old.last } ?: new.last < new.last
|
|
||||||
|
|
||||||
fun hasTagSuffixInView(query: String) =
|
|
||||||
tagMap.any { it.value in viewBounds && it.isCompatibleWithQuery(query) }
|
|
||||||
|
|
||||||
infix fun canDiscard(i: Int) = !(Finder.skim || i in tagMap.values || i == -1)
|
|
||||||
}
|
|
24
src/main/kotlin/org/acejump/modes/JumpMode.kt
Normal file
24
src/main/kotlin/org/acejump/modes/JumpMode.kt
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package org.acejump.modes
|
||||||
|
|
||||||
|
import org.acejump.action.AceTagAction
|
||||||
|
import org.acejump.config.AceConfig
|
||||||
|
import org.acejump.search.Tag
|
||||||
|
import org.acejump.session.SessionState
|
||||||
|
import org.acejump.session.TypeResult
|
||||||
|
|
||||||
|
open class JumpMode : SessionMode {
|
||||||
|
override val caretColor
|
||||||
|
get() = AceConfig.jumpModeColor
|
||||||
|
|
||||||
|
protected var wasUpperCase = false
|
||||||
|
private set
|
||||||
|
|
||||||
|
override fun type(state: SessionState, charTyped: Char, acceptedTag: Tag?): TypeResult {
|
||||||
|
wasUpperCase = charTyped.isUpperCase()
|
||||||
|
return state.type(charTyped)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun accept(state: SessionState, acceptedTag: Tag) {
|
||||||
|
AceTagAction.JumpToSearchStart.invoke(acceptedTag, shiftMode = wasUpperCase, isFinal = true)
|
||||||
|
}
|
||||||
|
}
|
13
src/main/kotlin/org/acejump/modes/SessionMode.kt
Normal file
13
src/main/kotlin/org/acejump/modes/SessionMode.kt
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package org.acejump.modes
|
||||||
|
|
||||||
|
import org.acejump.search.Tag
|
||||||
|
import org.acejump.session.SessionState
|
||||||
|
import org.acejump.session.TypeResult
|
||||||
|
import java.awt.Color
|
||||||
|
|
||||||
|
interface SessionMode {
|
||||||
|
val caretColor: Color
|
||||||
|
|
||||||
|
fun type(state: SessionState, charTyped: Char, acceptedTag: Tag?): TypeResult
|
||||||
|
fun accept(state: SessionState, acceptedTag: Tag)
|
||||||
|
}
|
@@ -1,26 +0,0 @@
|
|||||||
package org.acejump.search
|
|
||||||
|
|
||||||
import com.intellij.find.FindModel
|
|
||||||
import kotlin.text.RegexOption.IGNORE_CASE
|
|
||||||
import kotlin.text.RegexOption.MULTILINE
|
|
||||||
|
|
||||||
class AceFindModel: FindModel {
|
|
||||||
internal constructor(key: String, isRegex: Boolean = false): super() {
|
|
||||||
isCaseSensitive = false
|
|
||||||
stringToFind = key
|
|
||||||
isRegularExpressions = isRegex
|
|
||||||
isReplaceState = false
|
|
||||||
}
|
|
||||||
|
|
||||||
fun toRegex(): Regex {
|
|
||||||
var regex = stringToFind
|
|
||||||
val options = mutableSetOf(MULTILINE)
|
|
||||||
|
|
||||||
if (!isCaseSensitive && stringToFind.first().isLowerCase())
|
|
||||||
options.add(IGNORE_CASE)
|
|
||||||
if (!isRegularExpressions)
|
|
||||||
regex = Regex.escape(stringToFind)
|
|
||||||
|
|
||||||
return Regex(regex, options)
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,294 +0,0 @@
|
|||||||
package org.acejump.search
|
|
||||||
|
|
||||||
import com.intellij.openapi.application.ApplicationManager
|
|
||||||
import com.intellij.openapi.application.ReadAction
|
|
||||||
import com.intellij.openapi.editor.*
|
|
||||||
import com.intellij.openapi.fileEditor.FileDocumentManager
|
|
||||||
import com.intellij.openapi.fileEditor.FileEditorManager
|
|
||||||
import com.intellij.openapi.project.ProjectManager
|
|
||||||
import com.intellij.ui.awt.RelativePoint
|
|
||||||
import com.intellij.util.IntPair
|
|
||||||
import org.acejump.view.Model.MAX_TAG_RESULTS
|
|
||||||
import org.acejump.view.Model.viewBounds
|
|
||||||
import java.awt.Point
|
|
||||||
import java.util.*
|
|
||||||
import javax.swing.JComponent
|
|
||||||
import kotlin.math.*
|
|
||||||
|
|
||||||
interface Resettable {
|
|
||||||
fun reset()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun aceString(s: String): String =
|
|
||||||
ResourceBundle.getBundle("AceResources").getString(s)
|
|
||||||
|
|
||||||
fun <P> applyTo(vararg ps: P, fx: P.() -> Unit) = ps.forEach { it.fx() }
|
|
||||||
|
|
||||||
operator fun Point.component1() = x
|
|
||||||
operator fun Point.component2() = y
|
|
||||||
|
|
||||||
operator fun CharSequence.get(i: Int, j: Int) = substring(i, j).toCharArray()
|
|
||||||
|
|
||||||
fun String.hasSpaceRight(i: Int) = length <= i + 1 || this[i + 1].isWhitespace()
|
|
||||||
|
|
||||||
/**
|
|
||||||
* TODO: This is mostly an antipattern and can be replaced with [ReadAction.run]
|
|
||||||
*
|
|
||||||
* Further details: https://www.jetbrains.org/intellij/sdk/docs/basics/architectural_overview/general_threading_rules.html#readwrite-lock
|
|
||||||
*/
|
|
||||||
|
|
||||||
@Deprecated("This is applied too broadly. Narrow down usages where necessary.")
|
|
||||||
fun runNow(t: () -> Unit) = ApplicationManager.getApplication().invokeAndWait(t)
|
|
||||||
|
|
||||||
@Deprecated("This is applied too broadly. Narrow down usages where necessary.")
|
|
||||||
fun runLater(t: () -> Unit) = ApplicationManager.getApplication().invokeLater(t)
|
|
||||||
|
|
||||||
fun Editor.offsetCenter(first: Int, second: Int): LogicalPosition {
|
|
||||||
val firstIndexLine = offsetToLogicalPosition(first).line
|
|
||||||
val lastIndexLine = offsetToLogicalPosition(second).line
|
|
||||||
val center = (firstIndexLine + lastIndexLine) / 2
|
|
||||||
return offsetToLogicalPosition(getLineStartOffset(center))
|
|
||||||
}
|
|
||||||
|
|
||||||
fun Editor.getNameOfFileInEditor() =
|
|
||||||
FileDocumentManager.getInstance().getFile(document)?.presentableName
|
|
||||||
|
|
||||||
fun Editor.isNotFolded(offset: Int) = !foldingModel.isOffsetCollapsed(offset)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Identifies the bounds of a word, defined as a contiguous group of letters
|
|
||||||
* and digits, by expanding the provided index until a non-matching character
|
|
||||||
* is seen on either side.
|
|
||||||
*/
|
|
||||||
|
|
||||||
fun String.wordBounds(index: Int): IntPair {
|
|
||||||
var first = index
|
|
||||||
var last = index
|
|
||||||
while (0 < first && get(first - 1).isJavaIdentifierPart()) first--
|
|
||||||
while (last < length && get(last).isJavaIdentifierPart()) last++
|
|
||||||
return IntPair(first, last)
|
|
||||||
}
|
|
||||||
|
|
||||||
operator fun IntPair.component1() = this.first
|
|
||||||
operator fun IntPair.component2() = this.second
|
|
||||||
|
|
||||||
fun String.wordBoundsPlus(index: Int): IntPair {
|
|
||||||
var (left, right) = wordBounds(index)
|
|
||||||
|
|
||||||
for (it in (right..(right + 3).coerceAtMost(length - 1))) {
|
|
||||||
if (get(it) == '\n' || get(it) == '\r') {
|
|
||||||
break
|
|
||||||
} else {
|
|
||||||
right = it
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return IntPair(left, right)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun defaultEditor(): Editor =
|
|
||||||
FileEditorManager.getInstance(ProjectManager.getInstance()
|
|
||||||
.run { openProjects.firstOrNull() ?: defaultProject })
|
|
||||||
.run {
|
|
||||||
selectedTextEditor ?: allEditors.firstOrNull { it is Editor } as? Editor
|
|
||||||
?: EditorFactory.getInstance().run { createEditor(createDocument("")) }
|
|
||||||
}
|
|
||||||
|
|
||||||
fun Editor.getPoint(idx: Int) = visualPositionToXY(offsetToVisualPosition(idx))
|
|
||||||
|
|
||||||
fun Editor.getPointRelative(absolute: Point, relativeToComponent: JComponent) =
|
|
||||||
RelativePoint(relativeToComponent, absolute).originalPoint
|
|
||||||
|
|
||||||
fun Editor.isFirstCharacterOfLine(index: Int) =
|
|
||||||
index == getLineStartOffset(offsetToLogicalPosition(index).line)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns up to [MAX_TAG_RESULTS] by accumulating results before and after the
|
|
||||||
* view boundaries, (approximately centered around the middle of the screen).
|
|
||||||
*/
|
|
||||||
|
|
||||||
fun getFeasibleRegion(results: Set<Int>, takeAtMost: Int = MAX_TAG_RESULTS) =
|
|
||||||
(viewBounds.run { first + last } / 2).let { middleOfScreen ->
|
|
||||||
results.sortedBy { abs(middleOfScreen - it) }
|
|
||||||
.take(min(results.size, takeAtMost))
|
|
||||||
}.sorted().let { if (it.isNotEmpty()) it.first()..it.last() else viewBounds }
|
|
||||||
|
|
||||||
fun Editor.getView(): IntRange {
|
|
||||||
val firstVisibleLine = max(0, getVisualLineAtTopOfScreen() - 1)
|
|
||||||
val firstLine = visualLineToLogicalLine(firstVisibleLine)
|
|
||||||
val startOffset = getLineStartOffset(firstLine)
|
|
||||||
|
|
||||||
val height = getScreenHeight() + 2
|
|
||||||
val lastLine = visualLineToLogicalLine(firstVisibleLine + height)
|
|
||||||
var endOffset = getLineEndOffset(lastLine, true)
|
|
||||||
endOffset = normalizeOffset(lastLine, endOffset)
|
|
||||||
endOffset = min(max(0, document.textLength - 1), endOffset + 1)
|
|
||||||
|
|
||||||
return startOffset..endOffset
|
|
||||||
}
|
|
||||||
|
|
||||||
fun Editor.selectRange(fromOffset: Int, toOffset: Int) = runNow {
|
|
||||||
selectionModel.removeSelection()
|
|
||||||
selectionModel.setSelection(fromOffset, toOffset)
|
|
||||||
caretModel.moveToOffset(toOffset)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns whether two indices can be simultaneously visible on screen
|
|
||||||
*/
|
|
||||||
|
|
||||||
fun Editor.canIndicesBeSimultaneouslyVisible(idx0: Int, idx1: Int): Boolean {
|
|
||||||
// Thread must must have read access
|
|
||||||
val line1 = offsetToLogicalPosition(idx0).line
|
|
||||||
val line2 = offsetToLogicalPosition(idx1).line
|
|
||||||
|
|
||||||
return abs(line1 - line2) < getScreenHeight()
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* IdeaVim - A Vim emulator plugin for IntelliJ Idea
|
|
||||||
* Copyright (C) 2003-2005 Rick Maddy
|
|
||||||
*
|
|
||||||
* This program is free software; you can redistribute it and/or
|
|
||||||
* modify it under the terms of the GNU General Public License
|
|
||||||
* as published by the Free Software Foundation; either version 2
|
|
||||||
* of the License, or (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program; if not, write to the Free Software
|
|
||||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This is a set of helper methods for working with editors.
|
|
||||||
* All line and column values are zero based.
|
|
||||||
*/
|
|
||||||
|
|
||||||
fun Editor.getVisualLineAtTopOfScreen() =
|
|
||||||
(scrollingModel.verticalScrollOffset + lineHeight - 1) / lineHeight
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the number of actual lines in the file
|
|
||||||
*
|
|
||||||
* @return The file line count
|
|
||||||
*/
|
|
||||||
|
|
||||||
fun Editor.getLineCount() = document.run {
|
|
||||||
lineCount - if (textLength > 0 && text[textLength - 1] == '\n') 1 else 0
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the actual number of characters in the file
|
|
||||||
*
|
|
||||||
* @param countNewLines True include newline
|
|
||||||
*
|
|
||||||
* @return The file's character count
|
|
||||||
*/
|
|
||||||
|
|
||||||
fun Editor.getFileSize(countNewLines: Boolean = false): Int {
|
|
||||||
val len = document.textLength
|
|
||||||
val doc = document.charsSequence
|
|
||||||
return if (countNewLines || len == 0 || doc[len - 1] != '\n') len else len - 1
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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.
|
|
||||||
*
|
|
||||||
* @return The number of screen lines
|
|
||||||
*/
|
|
||||||
|
|
||||||
fun Editor.getScreenHeight() =
|
|
||||||
(scrollingModel.visibleArea.y + scrollingModel.visibleArea.height -
|
|
||||||
getVisualLineAtTopOfScreen() * lineHeight) / lineHeight
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Converts a visual line number to a logical line number.
|
|
||||||
*
|
|
||||||
* @param line The visual line number to convert
|
|
||||||
*
|
|
||||||
* @return The logical line number
|
|
||||||
*/
|
|
||||||
|
|
||||||
fun Editor.visualLineToLogicalLine(line: Int) =
|
|
||||||
normalizeLine(visualToLogicalPosition(
|
|
||||||
VisualPosition(line.coerceAtLeast(0), 0)).line)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the offset of the start of the requested line.
|
|
||||||
*
|
|
||||||
* @param line The logical line to get the start offset for.
|
|
||||||
*
|
|
||||||
* @return 0 if line is < 0, file size of line is bigger than file, else the
|
|
||||||
* start offset for the line
|
|
||||||
*/
|
|
||||||
|
|
||||||
fun Editor.getLineStartOffset(line: Int) =
|
|
||||||
when {
|
|
||||||
line < 0 -> 0
|
|
||||||
line >= getLineCount() -> getFileSize()
|
|
||||||
else -> document.getLineStartOffset(line)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the offset of the end of the requested line.
|
|
||||||
*
|
|
||||||
* @param line The logical line to get the end offset for
|
|
||||||
*
|
|
||||||
* @param allowEnd True include newline
|
|
||||||
*
|
|
||||||
* @return 0 if line is < 0, file size of line is bigger than file, else the
|
|
||||||
* end offset for the line
|
|
||||||
*/
|
|
||||||
|
|
||||||
fun Editor.getLineEndOffset(line: Int, allowEnd: Boolean = true) =
|
|
||||||
when {
|
|
||||||
line < 0 -> 0
|
|
||||||
line >= getLineCount() -> getFileSize(allowEnd)
|
|
||||||
else -> 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 line The logical line number to normalize
|
|
||||||
*
|
|
||||||
* @return The normalized logical line number
|
|
||||||
*/
|
|
||||||
|
|
||||||
fun Editor.normalizeLine(line: Int) = max(0, min(line, getLineCount() - 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 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 Editor.normalizeOffset(line: Int, offset: Int, allowEnd: Boolean = true) =
|
|
||||||
if (getFileSize(allowEnd) == 0) 0 else
|
|
||||||
max(min(offset, getLineEndOffset(line, allowEnd)), getLineStartOffset(line))
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This annotation is a marker which means that the annotated function is
|
|
||||||
* used in external plugins.
|
|
||||||
*/
|
|
||||||
|
|
||||||
@Retention(AnnotationRetention.SOURCE)
|
|
||||||
@Target(AnnotationTarget.FUNCTION)
|
|
||||||
annotation class ExternalUsage
|
|
@@ -1,231 +0,0 @@
|
|||||||
package org.acejump.search
|
|
||||||
|
|
||||||
import com.intellij.openapi.diagnostic.Logger
|
|
||||||
import com.intellij.openapi.editor.markup.HighlighterLayer
|
|
||||||
import com.intellij.openapi.editor.markup.HighlighterTargetArea.EXACT_RANGE
|
|
||||||
import com.intellij.openapi.editor.markup.RangeHighlighter
|
|
||||||
import org.acejump.config.AceConfig
|
|
||||||
import org.acejump.control.Handler
|
|
||||||
import org.acejump.control.Trigger
|
|
||||||
import org.acejump.label.Pattern
|
|
||||||
import org.acejump.label.Tagger
|
|
||||||
import org.acejump.view.Boundary
|
|
||||||
import org.acejump.view.Marker
|
|
||||||
import org.acejump.view.Model.LONG_DOCUMENT
|
|
||||||
import org.acejump.view.Model.boundaries
|
|
||||||
import org.acejump.view.Model.editor
|
|
||||||
import org.acejump.view.Model.editorText
|
|
||||||
import org.acejump.view.Model.markup
|
|
||||||
import org.acejump.view.Model.viewBounds
|
|
||||||
import java.util.*
|
|
||||||
import kotlin.math.max
|
|
||||||
import kotlin.math.min
|
|
||||||
import kotlin.system.measureTimeMillis
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Singleton that searches for text in editor and highlights matching results.
|
|
||||||
*/
|
|
||||||
|
|
||||||
object Finder : Resettable {
|
|
||||||
@Volatile
|
|
||||||
private var results: SortedSet<Int> = sortedSetOf()
|
|
||||||
@Volatile
|
|
||||||
private var textHighlights = listOf<RangeHighlighter>()
|
|
||||||
private var HIGHLIGHT_LAYER = HighlighterLayer.LAST + 1
|
|
||||||
private val logger = Logger.getInstance(Finder::class.java)
|
|
||||||
private val skimTrigger = Trigger()
|
|
||||||
var isShiftSelectEnabled = false
|
|
||||||
|
|
||||||
var skim = false
|
|
||||||
private set
|
|
||||||
|
|
||||||
@Volatile
|
|
||||||
var query: String = ""
|
|
||||||
set(value) {
|
|
||||||
field = value
|
|
||||||
if (query.isNotEmpty()) logger.info("Received query: \"$value\"")
|
|
||||||
isShiftSelectEnabled = value.lastOrNull()?.isUpperCase() == true
|
|
||||||
|
|
||||||
when {
|
|
||||||
value.isEmpty() -> return
|
|
||||||
Tagger.regex -> search()
|
|
||||||
value.length == 1 -> skimThenSearch()
|
|
||||||
value.isValidQuery() -> skimThenSearch()
|
|
||||||
else -> {
|
|
||||||
logger.info("Invalid query \"$field\", dropping: ${field.last()}")
|
|
||||||
field = field.dropLast(1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A user has two possible intentions when launching an AceJump search.
|
|
||||||
*
|
|
||||||
* 1. To locate the position of a known string in the document (a.k.a. Find)
|
|
||||||
* 2. To reposition the caret to a known location (i.e. staring at location)
|
|
||||||
*
|
|
||||||
* Since we cannot know why the user initiated any query a priori, here we
|
|
||||||
* attempt to satisfy both goals. First, we highlight all matches on (or off)
|
|
||||||
* the screen. This operation has very low latency. As soon as the user types
|
|
||||||
* a single character, we highlight all matches immediately. If we should
|
|
||||||
* receive no further characters after a short delay (indicating a pause in
|
|
||||||
* typing cadence), then we apply tags.
|
|
||||||
*
|
|
||||||
* Typically when a user searches for a known string, they will type several
|
|
||||||
* characters in rapid succession. We can avoid unnecessary work by only
|
|
||||||
* applying tags once we have received a "chunk" of search text.
|
|
||||||
*/
|
|
||||||
|
|
||||||
private fun skimThenSearch() {
|
|
||||||
if (results.size == 0 && LONG_DOCUMENT && AceConfig.searchWholeFile) {
|
|
||||||
logger.info("Skimming document for matches of: $query")
|
|
||||||
skim = true
|
|
||||||
search()
|
|
||||||
skimTrigger(400L) { skim = false; search() }
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
search()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun search(pattern: Pattern, bounds: Boundary) {
|
|
||||||
logger.info("Searching for regular expression: ${pattern.name} in $bounds")
|
|
||||||
search(pattern.string, bounds)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun search(pattern: String, bounds: Boundary) {
|
|
||||||
boundaries = bounds
|
|
||||||
// TODO: Fix this broken reset
|
|
||||||
reset()
|
|
||||||
Tagger.reset()
|
|
||||||
search(AceFindModel(pattern, true))
|
|
||||||
}
|
|
||||||
|
|
||||||
fun search(model: AceFindModel = AceFindModel(query)) {
|
|
||||||
val time = measureTimeMillis {
|
|
||||||
results = Scanner.find(model, calculateSearchBoundaries(), results)
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.info("Found ${results.size} matching sites in $time ms")
|
|
||||||
|
|
||||||
markResults(results, model)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun calculateSearchBoundaries(): IntRange {
|
|
||||||
if (AceConfig.searchWholeFile) {
|
|
||||||
return boundaries.intRange()
|
|
||||||
}
|
|
||||||
|
|
||||||
val bounds1 = boundaries
|
|
||||||
val bounds2 = Boundary.SCREEN_BOUNDARY
|
|
||||||
return max(bounds1.start, bounds2.start)..min(bounds1.endInclusive, bounds2.endInclusive)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method is used by IdeaVim integration plugin and must not be inlined.
|
|
||||||
*
|
|
||||||
* By default, when this function is called externally, [results] are already
|
|
||||||
* collected and [AceFindModel] should be empty. Additionally, if the flag
|
|
||||||
* [AceFindModel.isRegularExpressions] is true only one symbol is highlighted.
|
|
||||||
*/
|
|
||||||
|
|
||||||
@ExternalUsage
|
|
||||||
fun markResults(results: SortedSet<Int>,
|
|
||||||
model: AceFindModel = AceFindModel("", true)
|
|
||||||
) {
|
|
||||||
markup(results, model.isRegularExpressions)
|
|
||||||
if (!skim) tag(model, results)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Paints text highlights beneath each query result to the editor using the
|
|
||||||
* [com.intellij.openapi.editor.markup.MarkupModel].
|
|
||||||
*/
|
|
||||||
|
|
||||||
fun markup(markers: Set<Int> = results, isRegexQuery: Boolean = false) {
|
|
||||||
if (markers.isEmpty()) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
runLater {
|
|
||||||
val highlightLen = if (isRegexQuery) 1 else query.length
|
|
||||||
|
|
||||||
editor.document.isInBulkUpdate = true
|
|
||||||
textHighlights.forEach { markup.removeHighlighter(it) }
|
|
||||||
|
|
||||||
textHighlights = markers.map {
|
|
||||||
val start = it - if (it == editorText.length) 1 else 0
|
|
||||||
val end = start + highlightLen
|
|
||||||
createTextHighlight(max(start, 0), min(end, editorText.length - 1))
|
|
||||||
}
|
|
||||||
editor.document.isInBulkUpdate = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun createTextHighlight(start: Int, end: Int) =
|
|
||||||
markup.addRangeHighlighter(start, end, HIGHLIGHT_LAYER, null, EXACT_RANGE)
|
|
||||||
.apply { customRenderer = Marker.Companion }
|
|
||||||
|
|
||||||
private fun tag(model: AceFindModel, results: SortedSet<Int>) {
|
|
||||||
synchronized(this) { Tagger.markOrJump(model, results) }
|
|
||||||
val (ivb, ovb) = textHighlights.partition { it.startOffset in viewBounds }
|
|
||||||
|
|
||||||
ivb.cull()
|
|
||||||
runLater { ovb.cull() }
|
|
||||||
|
|
||||||
if (model.stringToFind == query || model.isRegularExpressions)
|
|
||||||
Handler.repaintTagMarkers()
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Erases highlights which are no longer compatible with the current query.
|
|
||||||
*/
|
|
||||||
|
|
||||||
private fun List<RangeHighlighter>.cull() =
|
|
||||||
eraseIf { Tagger canDiscard startOffset }
|
|
||||||
.also { newHighlights ->
|
|
||||||
val numDiscarded = size - newHighlights.size
|
|
||||||
if (numDiscarded != 0) logger.info("Discarded $numDiscarded highlights")
|
|
||||||
}
|
|
||||||
|
|
||||||
fun List<RangeHighlighter>.eraseIf(cond: RangeHighlighter.() -> Boolean): List<RangeHighlighter> {
|
|
||||||
val (erased, kept) = partition(cond)
|
|
||||||
|
|
||||||
if (erased.isNotEmpty()) {
|
|
||||||
runLater {
|
|
||||||
editor.document.isInBulkUpdate = true
|
|
||||||
erased.forEach { markup.removeHighlighter(it) }
|
|
||||||
editor.document.isInBulkUpdate = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return kept
|
|
||||||
}
|
|
||||||
|
|
||||||
fun allResults() = results
|
|
||||||
|
|
||||||
fun visibleResults() = results.filter { it in viewBounds }
|
|
||||||
|
|
||||||
private fun String.isValidQuery() =
|
|
||||||
Tagger.hasTagSuffixInView(query) ||
|
|
||||||
results.any {
|
|
||||||
editorText.regionMatches(
|
|
||||||
thisOffset = it,
|
|
||||||
other = this,
|
|
||||||
otherOffset = 0,
|
|
||||||
length = length,
|
|
||||||
ignoreCase = true
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun reset() {
|
|
||||||
runLater {
|
|
||||||
editor.document.isInBulkUpdate = true
|
|
||||||
markup.removeAllHighlighters()
|
|
||||||
editor.document.isInBulkUpdate = false
|
|
||||||
}
|
|
||||||
query = ""
|
|
||||||
skim = false
|
|
||||||
results = sortedSetOf()
|
|
||||||
textHighlights = listOf()
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,90 +0,0 @@
|
|||||||
package org.acejump.search
|
|
||||||
|
|
||||||
import com.intellij.openapi.editor.colors.EditorColors.CARET_COLOR
|
|
||||||
import org.acejump.config.AceConfig
|
|
||||||
import org.acejump.control.Handler
|
|
||||||
import org.acejump.view.Canvas
|
|
||||||
import org.acejump.view.Model
|
|
||||||
import org.acejump.view.Model.editor
|
|
||||||
import java.awt.Color
|
|
||||||
|
|
||||||
enum class JumpMode {
|
|
||||||
DISABLED, JUMP, JUMP_END, TARGET, DEFINE;
|
|
||||||
|
|
||||||
companion object: Resettable {
|
|
||||||
private var modeIndex = 0
|
|
||||||
|
|
||||||
var mode: JumpMode = DISABLED
|
|
||||||
private set(value) {
|
|
||||||
field = value
|
|
||||||
setCaretColor(when (field) {
|
|
||||||
JUMP -> AceConfig.jumpModeColor
|
|
||||||
JUMP_END -> AceConfig.jumpEndModeColor
|
|
||||||
DEFINE -> AceConfig.definitionModeColor
|
|
||||||
TARGET -> AceConfig.targetModeColor
|
|
||||||
DISABLED -> Model.naturalCaretColor
|
|
||||||
})
|
|
||||||
|
|
||||||
Finder.markup()
|
|
||||||
Canvas.repaint()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun setCaretColor(color: Color) =
|
|
||||||
editor.colorsScheme.setColor(CARET_COLOR, color)
|
|
||||||
|
|
||||||
fun toggle(newMode: JumpMode): JumpMode {
|
|
||||||
if (mode == newMode) {
|
|
||||||
mode = DISABLED
|
|
||||||
modeIndex = 0
|
|
||||||
Handler.reset()
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
mode = newMode
|
|
||||||
modeIndex = cycleSettings.indexOfFirst { it == newMode } + 1
|
|
||||||
}
|
|
||||||
return mode
|
|
||||||
}
|
|
||||||
|
|
||||||
private val cycleSettings
|
|
||||||
get() = arrayOf(
|
|
||||||
AceConfig.cycleMode1,
|
|
||||||
AceConfig.cycleMode2,
|
|
||||||
AceConfig.cycleMode3,
|
|
||||||
AceConfig.cycleMode4
|
|
||||||
)
|
|
||||||
|
|
||||||
fun cycle(): JumpMode {
|
|
||||||
val cycleSettings = cycleSettings
|
|
||||||
|
|
||||||
for (testModeIndex in (modeIndex + 1)..(cycleSettings.size)) {
|
|
||||||
if (cycleSettings[testModeIndex - 1] != DISABLED) {
|
|
||||||
mode = cycleSettings[testModeIndex - 1]
|
|
||||||
modeIndex = testModeIndex
|
|
||||||
return mode
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
mode = DISABLED
|
|
||||||
modeIndex = 0
|
|
||||||
Handler.reset()
|
|
||||||
return mode
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun reset() {
|
|
||||||
mode = DISABLED
|
|
||||||
modeIndex = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun equals(other: Any?) =
|
|
||||||
if (other is JumpMode) mode == other else super.equals(other)
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun toString() = when(this) {
|
|
||||||
DISABLED -> aceString("jumpModeDisabled")
|
|
||||||
JUMP -> aceString("jumpModeJump")
|
|
||||||
JUMP_END -> aceString("jumpModeJumpEnd")
|
|
||||||
TARGET -> aceString("jumpModeTarget")
|
|
||||||
DEFINE -> aceString("jumpModeDefine")
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,132 +0,0 @@
|
|||||||
package org.acejump.search
|
|
||||||
|
|
||||||
import com.intellij.codeInsight.editorActions.SelectWordUtil.addWordSelection
|
|
||||||
import com.intellij.codeInsight.navigation.actions.GotoDeclarationAction
|
|
||||||
import com.intellij.openapi.actionSystem.ActionManager
|
|
||||||
import com.intellij.openapi.command.CommandProcessor
|
|
||||||
import com.intellij.openapi.diagnostic.Logger
|
|
||||||
import com.intellij.openapi.editor.Editor
|
|
||||||
import com.intellij.openapi.editor.actionSystem.DocCommandGroupId
|
|
||||||
import com.intellij.openapi.fileEditor.ex.IdeDocumentHistory
|
|
||||||
import com.intellij.openapi.fileEditor.impl.IdeDocumentHistoryImpl
|
|
||||||
import com.intellij.openapi.ui.playback.commands.ActionCommand
|
|
||||||
import com.intellij.openapi.util.TextRange
|
|
||||||
import org.acejump.label.Tagger
|
|
||||||
import org.acejump.search.JumpMode.*
|
|
||||||
import org.acejump.view.Model.editor
|
|
||||||
import org.acejump.view.Model.editorText
|
|
||||||
import org.acejump.view.Model.project
|
|
||||||
import kotlin.math.max
|
|
||||||
import kotlin.math.min
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Updates the [com.intellij.openapi.editor.CaretModel] after a tag is selected.
|
|
||||||
*/
|
|
||||||
|
|
||||||
object Jumper: Resettable {
|
|
||||||
private val logger = Logger.getInstance(Jumper::class.java)
|
|
||||||
|
|
||||||
fun cycleMode() =
|
|
||||||
logger.info("Entering ${JumpMode.cycle()} mode")
|
|
||||||
|
|
||||||
fun toggleMode(mode: JumpMode) =
|
|
||||||
logger.info("Entering ${JumpMode.toggle(mode)} mode")
|
|
||||||
|
|
||||||
fun jumpTo(newOffset: Int, done: Boolean = true) =
|
|
||||||
editor.run {
|
|
||||||
val logPos = offsetToLogicalPosition(newOffset)
|
|
||||||
logger.debug("Jumping to line ${logPos.line}, column ${logPos.column}...")
|
|
||||||
|
|
||||||
val oldOffset = caretModel.offset
|
|
||||||
|
|
||||||
when {
|
|
||||||
JumpMode.equals(JUMP_END) ->
|
|
||||||
moveCaretToEnd(newOffset + countMatchingCharacters(newOffset, Tagger.query))
|
|
||||||
|
|
||||||
else ->
|
|
||||||
moveCaretTo(newOffset)
|
|
||||||
}
|
|
||||||
|
|
||||||
when {
|
|
||||||
Finder.isShiftSelectEnabled && done -> selectRange(oldOffset, newOffset)
|
|
||||||
JumpMode.equals(TARGET) -> selectWordAtOffset(newOffset)
|
|
||||||
JumpMode.equals(DEFINE) && done -> gotoSymbolAction()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private val aceJumpHistoryAppender = {
|
|
||||||
with(IdeDocumentHistory.getInstance(project) as IdeDocumentHistoryImpl) {
|
|
||||||
onSelectionChanged()
|
|
||||||
includeCurrentCommandAsNavigation()
|
|
||||||
includeCurrentPlaceAsChangePlace()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Ensures each jump destination is appended to [IdeDocumentHistory] so that
|
|
||||||
* users can navigate forward and backward using the IDE actions.
|
|
||||||
*/
|
|
||||||
|
|
||||||
private fun Editor.appendCaretPositionToEditorNavigationHistory() =
|
|
||||||
CommandProcessor.getInstance().executeCommand(project,
|
|
||||||
aceJumpHistoryAppender, "AceJumpHistoryAppender",
|
|
||||||
DocCommandGroupId.noneGroupId(document), document)
|
|
||||||
|
|
||||||
private fun Editor.moveCaretTo(offset: Int) {
|
|
||||||
appendCaretPositionToEditorNavigationHistory()
|
|
||||||
selectionModel.removeSelection()
|
|
||||||
caretModel.moveToOffset(offset)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun Editor.moveCaretToEnd(offset: Int) {
|
|
||||||
val ranges = ArrayList<TextRange>()
|
|
||||||
addWordSelection(settings.isCamelWords, editorText, offset, ranges)
|
|
||||||
|
|
||||||
if (ranges.isEmpty()) {
|
|
||||||
moveCaretTo(offset)
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
moveCaretTo(min(ranges[0].endOffset, editorText.length))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun countMatchingCharacters(offset: Int, query: String): Int {
|
|
||||||
var count = 0
|
|
||||||
while (offset + count < editorText.length && count < query.length && editorText[offset + count] == query[count]) {
|
|
||||||
count++
|
|
||||||
}
|
|
||||||
return count
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Selects a sequence of contiguous characters adjacent to the target offset
|
|
||||||
* matching [Character.isJavaIdentifierPart], or nothing at all.
|
|
||||||
*
|
|
||||||
* TODO: Make this language agnostic.
|
|
||||||
*/
|
|
||||||
|
|
||||||
private fun Editor.selectWordAtOffset(offset: Int = caretModel.offset) {
|
|
||||||
val ranges = ArrayList<TextRange>()
|
|
||||||
addWordSelection(settings.isCamelWords, editorText, offset, ranges)
|
|
||||||
|
|
||||||
ranges.ifEmpty { return }
|
|
||||||
|
|
||||||
val firstRange = ranges[0]
|
|
||||||
val startOfWordOffset = max(0, firstRange.startOffset)
|
|
||||||
val endOfWordOffset = min(firstRange.endOffset, editorText.length)
|
|
||||||
|
|
||||||
selectRange(startOfWordOffset, endOfWordOffset)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Navigates to the target symbol's declaration, using [GotoDeclarationAction]
|
|
||||||
*/
|
|
||||||
|
|
||||||
private fun gotoSymbolAction() =
|
|
||||||
runNow {
|
|
||||||
ActionManager.getInstance().tryToExecute(GotoDeclarationAction(),
|
|
||||||
ActionCommand.getInputEvent("NewFromTemplate"), null, null, true)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun reset() = JumpMode.reset()
|
|
||||||
}
|
|
12
src/main/kotlin/org/acejump/search/Pattern.kt
Normal file
12
src/main/kotlin/org/acejump/search/Pattern.kt
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package org.acejump.search
|
||||||
|
|
||||||
|
enum class Pattern(val regex: String) {
|
||||||
|
LINE_STARTS("^.|^\\n"),
|
||||||
|
LINE_ENDS("\\n|\\Z"),
|
||||||
|
LINE_INDENTS("[^\\s].*|^\\n"),
|
||||||
|
ALL_WORDS("(?<=[^a-zA-Z0-9_]|\\A)[a-zA-Z0-9_]"),
|
||||||
|
VIM_LWORD("(?<=[^a-zA-Z0-9_]|\\A)[a-zA-Z0-9_]"),
|
||||||
|
VIM_UWORD("(?<=\\s|\\A)[^\\s]"),
|
||||||
|
VIM_LWORD_END("[a-zA-Z0-9_](?=[^a-zA-Z0-9_]|\\Z)"),
|
||||||
|
VIM_UWORD_END("[^\\s](?=\\s|\\Z)")
|
||||||
|
}
|
@@ -1,76 +0,0 @@
|
|||||||
package org.acejump.search
|
|
||||||
|
|
||||||
import com.intellij.openapi.diagnostic.Logger
|
|
||||||
import org.acejump.view.Model.LONG_DOCUMENT_LENGTH
|
|
||||||
import org.acejump.view.Model.editorText
|
|
||||||
import java.util.*
|
|
||||||
import kotlin.streams.toList
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a set of indices indicating query matches, within the given range.
|
|
||||||
* These are full indices, i.e. are not offset to the beginning of the range.
|
|
||||||
*/
|
|
||||||
|
|
||||||
internal object Scanner {
|
|
||||||
val cores = Runtime.getRuntime().availableProcessors() - 1
|
|
||||||
private val logger = Logger.getInstance(Scanner::class.java)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns [SortedSet] of indices matching the [model]. Providing a [cache]
|
|
||||||
* will filter prior results instead of searching the editor contents.
|
|
||||||
*/
|
|
||||||
|
|
||||||
fun find(model: AceFindModel, boundaries: IntRange, cache: Set<Int> = emptySet()): SortedSet<Int> =
|
|
||||||
if (cache.isNotEmpty() || (boundaries.last - boundaries.first) < LONG_DOCUMENT_LENGTH)
|
|
||||||
editorText.search(model, cache, boundaries).toSortedSet()
|
|
||||||
else editorText.chunk().parallelStream().map { chunk ->
|
|
||||||
editorText.search(model, cache, chunk)
|
|
||||||
}.toList().flatten().toSortedSet()
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Divides lines of text into equally-sized chunks for parallelized search.
|
|
||||||
*/
|
|
||||||
|
|
||||||
private fun String.chunk(): List<IntRange> {
|
|
||||||
val lines = splitToSequence("\n", "\r").toList()
|
|
||||||
val chunkSize = lines.size / cores + 1
|
|
||||||
logger.info("Parallelizing ${lines.size}-line search across $cores cores")
|
|
||||||
var offset = 0
|
|
||||||
return lines.chunked(chunkSize).map {
|
|
||||||
val len = it.joinToString("\n").length
|
|
||||||
(offset..(offset + len)).also { offset += len + 1 }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Searches the [cache] (if it is populated), or else the whole document.
|
|
||||||
*/
|
|
||||||
|
|
||||||
fun String.search(model: AceFindModel, cache: Set<Int>, chunk: IntRange) =
|
|
||||||
run {
|
|
||||||
val query = model.stringToFind
|
|
||||||
if (isEmpty() || query.isEmpty()) sortedSetOf<Int>()
|
|
||||||
else if (cache.isNotEmpty()) filterCache(cache, query)
|
|
||||||
else findAll(model.toRegex(), chunk)
|
|
||||||
}.toList()
|
|
||||||
|
|
||||||
private fun String.filterCache(cache: Set<Int>, query: String) =
|
|
||||||
cache.asSequence().filter { index ->
|
|
||||||
regionMatches(
|
|
||||||
thisOffset = index + query.length - 1,
|
|
||||||
other = query.last().toString(),
|
|
||||||
otherOffset = 0,
|
|
||||||
length = 1,
|
|
||||||
ignoreCase = query.last().isLowerCase()
|
|
||||||
)
|
|
||||||
}.toList()
|
|
||||||
|
|
||||||
fun CharSequence.findAll(regex: Regex, chunk: IntRange) =
|
|
||||||
generateSequence(
|
|
||||||
seedFunction = { regex.find(this, chunk.first) },
|
|
||||||
nextFunction = { result -> filterNext(result, chunk) }
|
|
||||||
).map { it.range.first }.toList()
|
|
||||||
|
|
||||||
fun filterNext(result: MatchResult, chunk: IntRange): MatchResult? =
|
|
||||||
result.next()?.let { if(it.range.first !in chunk) null else it }
|
|
||||||
}
|
|
85
src/main/kotlin/org/acejump/search/SearchProcessor.kt
Normal file
85
src/main/kotlin/org/acejump/search/SearchProcessor.kt
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
package org.acejump.search
|
||||||
|
|
||||||
|
import com.intellij.openapi.editor.Editor
|
||||||
|
import it.unimi.dsi.fastutil.ints.IntArrayList
|
||||||
|
import org.acejump.boundaries.Boundaries
|
||||||
|
import org.acejump.boundaries.EditorOffsetCache
|
||||||
|
import org.acejump.clone
|
||||||
|
import org.acejump.config.AceConfig
|
||||||
|
import org.acejump.immutableText
|
||||||
|
import org.acejump.matchesAt
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Searches editor text for matches of a [SearchQuery], and updates previous results when the user [refineQuery]s a character.
|
||||||
|
*/
|
||||||
|
class SearchProcessor private constructor(query: SearchQuery, val boundaries: Boundaries, val invertUppercaseMode: Boolean, private val results: MutableMap<Editor, IntArrayList>) {
|
||||||
|
internal constructor(editors: List<Editor>, query: SearchQuery, boundaries: Boundaries, invertUppercaseMode: Boolean) : this(query, boundaries, invertUppercaseMode, mutableMapOf()) {
|
||||||
|
val regex = query.toRegex(invertUppercaseMode)
|
||||||
|
|
||||||
|
if (regex != null) {
|
||||||
|
for (editor in editors) {
|
||||||
|
val cache = EditorOffsetCache.new()
|
||||||
|
val offsets = IntArrayList()
|
||||||
|
|
||||||
|
val offsetRange = boundaries.getOffsetRange(editor, cache)
|
||||||
|
var result = regex.find(editor.immutableText, offsetRange.first)
|
||||||
|
|
||||||
|
while (result != null) {
|
||||||
|
val index = result.range.first // For some reason regex matches can be out of bounds, but boundary check prevents an exception.
|
||||||
|
val highlightEnd = index + query.getHighlightLength("", index)
|
||||||
|
|
||||||
|
if (highlightEnd > offsetRange.last) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
else if (boundaries.isOffsetInside(editor, index, cache) && !editor.foldingModel.isOffsetCollapsed(index)) {
|
||||||
|
offsets.add(index)
|
||||||
|
}
|
||||||
|
|
||||||
|
result = result.next()
|
||||||
|
}
|
||||||
|
|
||||||
|
results[editor] = offsets
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal var query = query
|
||||||
|
private set
|
||||||
|
|
||||||
|
val resultsCopy
|
||||||
|
get() = results.clone()
|
||||||
|
|
||||||
|
val isQueryFinished
|
||||||
|
get() = query.rawText.length >= AceConfig.minQueryLength
|
||||||
|
|
||||||
|
fun refineQuery(char: Char): Boolean {
|
||||||
|
if (char == '\n') {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
query = query.refine(char)
|
||||||
|
removeObsoleteResults()
|
||||||
|
return isQueryFinished
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* After updating the query, removes all results that no longer match the search query.
|
||||||
|
*/
|
||||||
|
private fun removeObsoleteResults() {
|
||||||
|
val query = query.rawText
|
||||||
|
|
||||||
|
for (entry in results) {
|
||||||
|
val editor = entry.key
|
||||||
|
val offsetIter = entry.value.iterator()
|
||||||
|
|
||||||
|
while (offsetIter.hasNext()) {
|
||||||
|
val offset = offsetIter.nextInt()
|
||||||
|
|
||||||
|
if (!editor.immutableText.matchesAt(offset, query, ignoreCase = true)) {
|
||||||
|
offsetIter.remove()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
79
src/main/kotlin/org/acejump/search/SearchQuery.kt
Normal file
79
src/main/kotlin/org/acejump/search/SearchQuery.kt
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
package org.acejump.search
|
||||||
|
|
||||||
|
import org.acejump.countMatchingCharacters
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defines the current search query for a session.
|
||||||
|
*/
|
||||||
|
internal sealed class SearchQuery {
|
||||||
|
abstract val rawText: String
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a new query with the given character appended.
|
||||||
|
*/
|
||||||
|
abstract fun refine(char: Char): SearchQuery
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns how many characters the search occurrence highlight should cover.
|
||||||
|
*/
|
||||||
|
abstract fun getHighlightLength(text: CharSequence, offset: Int): Int
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts the query into a regular expression to find the initial matches.
|
||||||
|
*/
|
||||||
|
abstract fun toRegex(invertUppercaseMode: Boolean): Regex?
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Searches for all occurrences of a literal text query.
|
||||||
|
* If the first character of the query is lowercase, then the entire query will be case-insensitive,
|
||||||
|
* and only beginnings of words and camel humps will be matched.
|
||||||
|
*/
|
||||||
|
class Literal(override val rawText: String) : SearchQuery() {
|
||||||
|
init {
|
||||||
|
require(rawText.isNotEmpty())
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun refine(char: Char): SearchQuery {
|
||||||
|
return Literal(rawText + char)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getHighlightLength(text: CharSequence, offset: Int): Int {
|
||||||
|
return text.countMatchingCharacters(offset, rawText)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun toRegex(invertUppercaseMode: Boolean): Regex {
|
||||||
|
val firstChar = rawText.first()
|
||||||
|
val pattern = if (firstChar.isLowerCase() xor invertUppercaseMode) {
|
||||||
|
val fullPattern = Regex.escape(rawText)
|
||||||
|
"(?i)$fullPattern"
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
val firstCharUppercasePattern = Regex.escape(firstChar.uppercase())
|
||||||
|
val firstCharLowercasePattern = Regex.escape(firstChar.lowercase())
|
||||||
|
val remainingPattern = if (rawText.length > 1) Regex.escape(rawText.drop(1)) else ""
|
||||||
|
"(?:$firstCharUppercasePattern|(?<![a-zA-Z])$firstCharLowercasePattern)$remainingPattern"
|
||||||
|
}
|
||||||
|
|
||||||
|
return Regex(pattern, setOf(RegexOption.MULTILINE))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Searches for all matches of a regular expression.
|
||||||
|
*/
|
||||||
|
class RegularExpression(private val pattern: String) : SearchQuery() {
|
||||||
|
override val rawText = ""
|
||||||
|
|
||||||
|
override fun refine(char: Char): SearchQuery {
|
||||||
|
return Literal(char.toString())
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getHighlightLength(text: CharSequence, offset: Int): Int {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun toRegex(invertUppercaseMode: Boolean): Regex {
|
||||||
|
return Regex(pattern, setOf(RegexOption.MULTILINE, RegexOption.IGNORE_CASE))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
13
src/main/kotlin/org/acejump/search/Tag.kt
Normal file
13
src/main/kotlin/org/acejump/search/Tag.kt
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package org.acejump.search
|
||||||
|
|
||||||
|
import com.intellij.openapi.editor.Editor
|
||||||
|
|
||||||
|
data class Tag(val editor: Editor, val offset: Int) {
|
||||||
|
override fun equals(other: Any?): Boolean {
|
||||||
|
return other is Tag && other.offset == offset && other.editor === editor
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun hashCode(): Int {
|
||||||
|
return (offset * 31) + editor.hashCode()
|
||||||
|
}
|
||||||
|
}
|
174
src/main/kotlin/org/acejump/search/Tagger.kt
Normal file
174
src/main/kotlin/org/acejump/search/Tagger.kt
Normal file
@@ -0,0 +1,174 @@
|
|||||||
|
package org.acejump.search
|
||||||
|
|
||||||
|
import com.google.common.collect.ArrayListMultimap
|
||||||
|
import com.intellij.openapi.editor.Editor
|
||||||
|
import it.unimi.dsi.fastutil.ints.IntList
|
||||||
|
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap
|
||||||
|
import org.acejump.boundaries.EditorOffsetCache
|
||||||
|
import org.acejump.boundaries.StandardBoundaries.VISIBLE_ON_SCREEN
|
||||||
|
import org.acejump.input.KeyLayoutCache
|
||||||
|
import org.acejump.view.TagMarker
|
||||||
|
import kotlin.collections.component1
|
||||||
|
import kotlin.collections.component2
|
||||||
|
import kotlin.math.min
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assigns tags to search occurrences.
|
||||||
|
* The ordering of [editors] may be used to prioritize tagging editors earlier in the list in case of conflicts.
|
||||||
|
*/
|
||||||
|
class Tagger(private val editors: List<Editor>, results: Map<Editor, IntList>) {
|
||||||
|
private var tagMap: Map<String, Tag>
|
||||||
|
private var typedTag = ""
|
||||||
|
|
||||||
|
internal val markers: Map<Editor, Collection<TagMarker>>
|
||||||
|
get() {
|
||||||
|
val markers = ArrayListMultimap.create<Editor, TagMarker>(editors.size, min(tagMap.values.size, 40))
|
||||||
|
|
||||||
|
for ((mark, tag) in tagMap) {
|
||||||
|
val marker = TagMarker.create(mark, tag.offset, typedTag)
|
||||||
|
markers.put(tag.editor, marker)
|
||||||
|
}
|
||||||
|
|
||||||
|
return markers.asMap()
|
||||||
|
}
|
||||||
|
|
||||||
|
init {
|
||||||
|
val caches = results.keys.associateWith { EditorOffsetCache.new() }
|
||||||
|
|
||||||
|
sortResults(results, caches)
|
||||||
|
|
||||||
|
val tagSites = results
|
||||||
|
.flatMap { (editor, sites) -> sites.map { site -> Tag(editor, site) } }
|
||||||
|
.sortedWith(siteOrder(editors, caches))
|
||||||
|
|
||||||
|
tagMap = generateTags(tagSites.size).zip(tagSites).toMap()
|
||||||
|
}
|
||||||
|
|
||||||
|
internal fun type(char: Char): TaggingResult {
|
||||||
|
val newTypedTag = typedTag + char.lowercaseChar()
|
||||||
|
val matchingTag = tagMap[newTypedTag]
|
||||||
|
if (matchingTag != null) {
|
||||||
|
return TaggingResult.Accept(matchingTag)
|
||||||
|
}
|
||||||
|
|
||||||
|
val newTagMap = tagMap.filter { it.key.startsWith(newTypedTag) }
|
||||||
|
if (newTagMap.isEmpty()) {
|
||||||
|
return TaggingResult.Nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
typedTag = newTypedTag
|
||||||
|
tagMap = newTagMap
|
||||||
|
return TaggingResult.Mark(markers)
|
||||||
|
}
|
||||||
|
|
||||||
|
private companion object {
|
||||||
|
private fun generateTags(tagCount: Int): List<String> {
|
||||||
|
val allowedTagsSorted = KeyLayoutCache.allowedTagsSorted
|
||||||
|
val tags = mutableListOf<String>()
|
||||||
|
|
||||||
|
val containedSingleCharTags = mutableSetOf<Char>()
|
||||||
|
val blockedSingleCharTags = mutableSetOf<Char>()
|
||||||
|
val doubleCharTagCountsByFirstChar = Object2IntOpenHashMap<Char>()
|
||||||
|
|
||||||
|
for (tag in allowedTagsSorted) {
|
||||||
|
val firstChar = tag.first()
|
||||||
|
|
||||||
|
if (tag.length == 1) {
|
||||||
|
if (firstChar in blockedSingleCharTags) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
containedSingleCharTags.add(firstChar)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (containedSingleCharTags.remove(firstChar)) {
|
||||||
|
tags.remove(firstChar.toString())
|
||||||
|
}
|
||||||
|
|
||||||
|
blockedSingleCharTags.add(firstChar)
|
||||||
|
doubleCharTagCountsByFirstChar.addTo(firstChar, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
tags.add(tag)
|
||||||
|
|
||||||
|
if (tags.size >= tagCount) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// In rare cases, the final tag list may contain a double character tag that is the only tag starting with its first character,
|
||||||
|
// so we replace it with the single character tag.
|
||||||
|
for (entry in doubleCharTagCountsByFirstChar.object2IntEntrySet()) {
|
||||||
|
if (entry.intValue != 1) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
tags.removeAt(tags.indexOfFirst { it.first() == entry.key })
|
||||||
|
|
||||||
|
val tag = entry.key.toString()
|
||||||
|
var previousTagIndex = -1
|
||||||
|
|
||||||
|
// The implementation of searching where to place the single character tag is theoretically slow,
|
||||||
|
// but getting here is so rare it doesn't matter.
|
||||||
|
for (i in allowedTagsSorted.indexOf(tag) - 1 downTo 0) {
|
||||||
|
previousTagIndex = tags.indexOf(allowedTagsSorted[i])
|
||||||
|
|
||||||
|
if (previousTagIndex != -1) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tags.add(previousTagIndex + 1, tag)
|
||||||
|
}
|
||||||
|
|
||||||
|
return tags
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun sortResults(results: Map<Editor, IntList>, caches: Map<Editor, EditorOffsetCache>) {
|
||||||
|
for ((editor, offsets) in results) {
|
||||||
|
val cache = caches.getValue(editor)
|
||||||
|
|
||||||
|
offsets.sort { a, b ->
|
||||||
|
val aIsVisible = VISIBLE_ON_SCREEN.isOffsetInside(editor, a, cache)
|
||||||
|
val bIsVisible = VISIBLE_ON_SCREEN.isOffsetInside(editor, b, cache)
|
||||||
|
|
||||||
|
when {
|
||||||
|
aIsVisible && !bIsVisible -> -1
|
||||||
|
bIsVisible && !aIsVisible -> 1
|
||||||
|
else -> 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun siteOrder(editorPriority: List<Editor>, caches: Map<Editor, EditorOffsetCache>) = Comparator<Tag> { a, b ->
|
||||||
|
val aEditor = a.editor
|
||||||
|
val bEditor = b.editor
|
||||||
|
|
||||||
|
if (aEditor !== bEditor) {
|
||||||
|
val aEditorIndex = editorPriority.indexOf(aEditor)
|
||||||
|
val bEditorIndex = editorPriority.indexOf(bEditor)
|
||||||
|
// For multiple editors, prioritize them based on the provided order.
|
||||||
|
return@Comparator if (aEditorIndex < bEditorIndex) -1 else 1
|
||||||
|
}
|
||||||
|
|
||||||
|
val aCaches = caches.getValue(aEditor)
|
||||||
|
val bCaches = caches.getValue(bEditor)
|
||||||
|
|
||||||
|
val aIsVisible = VISIBLE_ON_SCREEN.isOffsetInside(aEditor, a.offset, aCaches)
|
||||||
|
val bIsVisible = VISIBLE_ON_SCREEN.isOffsetInside(bEditor, b.offset, bCaches)
|
||||||
|
if (aIsVisible != bIsVisible) {
|
||||||
|
// Sites in immediate view should come first.
|
||||||
|
return@Comparator if (aIsVisible) -1 else 1
|
||||||
|
}
|
||||||
|
|
||||||
|
val firstEditor = editorPriority[0]
|
||||||
|
val caretPosition = caches.getValue(firstEditor).offsetToXY(firstEditor, firstEditor.caretModel.offset)
|
||||||
|
|
||||||
|
val aDistance = aCaches.offsetToXY(aEditor, a.offset).distanceSq(caretPosition)
|
||||||
|
val bDistance = bCaches.offsetToXY(bEditor, b.offset).distanceSq(caretPosition)
|
||||||
|
|
||||||
|
return@Comparator aDistance.compareTo(bDistance)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
10
src/main/kotlin/org/acejump/search/TaggingResult.kt
Normal file
10
src/main/kotlin/org/acejump/search/TaggingResult.kt
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package org.acejump.search
|
||||||
|
|
||||||
|
import com.intellij.openapi.editor.Editor
|
||||||
|
import org.acejump.view.TagMarker
|
||||||
|
|
||||||
|
internal sealed class TaggingResult {
|
||||||
|
object Nothing : TaggingResult()
|
||||||
|
class Accept(val tag: Tag) : TaggingResult()
|
||||||
|
class Mark(val markers: Map<Editor, Collection<TagMarker>>) : TaggingResult()
|
||||||
|
}
|
53
src/main/kotlin/org/acejump/session/EditorSettings.kt
Normal file
53
src/main/kotlin/org/acejump/session/EditorSettings.kt
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
package org.acejump.session
|
||||||
|
|
||||||
|
import com.intellij.openapi.editor.Editor
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Holds [Editor] caret settings. The settings are saved the moment a [Session] starts, modified to indicate AceJump states, and restored
|
||||||
|
* once the [Session] ends.
|
||||||
|
*/
|
||||||
|
internal data class EditorSettings(private val isBlockCursor: Boolean, private val isBlinkCaret: Boolean, private val isReadOnly: Boolean) {
|
||||||
|
companion object {
|
||||||
|
fun setup(editor: Editor): EditorSettings {
|
||||||
|
val settings = editor.settings
|
||||||
|
val document = editor.document
|
||||||
|
|
||||||
|
val original = EditorSettings(
|
||||||
|
isBlockCursor = settings.isBlockCursor,
|
||||||
|
isBlinkCaret = settings.isBlinkCaret,
|
||||||
|
isReadOnly = !document.isWritable
|
||||||
|
)
|
||||||
|
|
||||||
|
settings.isBlockCursor = true
|
||||||
|
settings.isBlinkCaret = false
|
||||||
|
document.setReadOnly(true)
|
||||||
|
|
||||||
|
return original
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun startEditing(editor: Editor) {
|
||||||
|
editor.document.setReadOnly(isReadOnly)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun stopEditing(editor: Editor) {
|
||||||
|
editor.document.setReadOnly(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onTagAccepted(editor: Editor) = editor.let {
|
||||||
|
it.settings.isBlockCursor = isBlockCursor
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onTagUnaccepted(editor: Editor) = editor.let {
|
||||||
|
it.settings.isBlockCursor = true
|
||||||
|
}
|
||||||
|
|
||||||
|
fun restore(editor: Editor) {
|
||||||
|
val settings = editor.settings
|
||||||
|
val document = editor.document
|
||||||
|
|
||||||
|
settings.isBlockCursor = isBlockCursor
|
||||||
|
settings.isBlinkCaret = isBlinkCaret
|
||||||
|
document.setReadOnly(isReadOnly)
|
||||||
|
}
|
||||||
|
}
|
180
src/main/kotlin/org/acejump/session/Session.kt
Normal file
180
src/main/kotlin/org/acejump/session/Session.kt
Normal file
@@ -0,0 +1,180 @@
|
|||||||
|
package org.acejump.session
|
||||||
|
|
||||||
|
import com.intellij.codeInsight.hint.HintManagerImpl
|
||||||
|
import com.intellij.openapi.editor.Editor
|
||||||
|
import com.intellij.openapi.editor.ScrollType
|
||||||
|
import com.intellij.openapi.editor.colors.EditorColors
|
||||||
|
import com.intellij.openapi.editor.colors.impl.AbstractColorsScheme
|
||||||
|
import it.unimi.dsi.fastutil.ints.IntList
|
||||||
|
import org.acejump.boundaries.Boundaries
|
||||||
|
import org.acejump.boundaries.StandardBoundaries
|
||||||
|
import org.acejump.config.AceConfig
|
||||||
|
import org.acejump.input.EditorKeyListener
|
||||||
|
import org.acejump.input.KeyLayoutCache
|
||||||
|
import org.acejump.modes.JumpMode
|
||||||
|
import org.acejump.modes.SessionMode
|
||||||
|
import org.acejump.search.Pattern
|
||||||
|
import org.acejump.search.SearchProcessor
|
||||||
|
import org.acejump.search.SearchQuery
|
||||||
|
import org.acejump.search.Tag
|
||||||
|
import org.acejump.session.TypeResult.AcceptTag
|
||||||
|
import org.acejump.session.TypeResult.ChangeMode
|
||||||
|
import org.acejump.session.TypeResult.ChangeState
|
||||||
|
import org.acejump.session.TypeResult.EndSession
|
||||||
|
import org.acejump.session.TypeResult.Nothing
|
||||||
|
import org.acejump.view.TagCanvas
|
||||||
|
import org.acejump.view.TagMarker
|
||||||
|
import org.acejump.view.TextHighlighter
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Manages an AceJump session for a single [Editor].
|
||||||
|
*/
|
||||||
|
class Session(private val mainEditor: Editor, private val jumpEditors: List<Editor>) {
|
||||||
|
private val editorSettings = EditorSettings.setup(mainEditor)
|
||||||
|
private lateinit var mode: SessionMode
|
||||||
|
|
||||||
|
private var state: SessionState? = null
|
||||||
|
|
||||||
|
private var acceptedTag: Tag? = null
|
||||||
|
set(value) {
|
||||||
|
field = value
|
||||||
|
|
||||||
|
if (value != null) {
|
||||||
|
tagCanvases.values.forEach(TagCanvas::removeMarkers)
|
||||||
|
editorSettings.onTagAccepted(mainEditor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private val textHighlighter = TextHighlighter()
|
||||||
|
private val tagCanvases = jumpEditors.associateWith(::TagCanvas)
|
||||||
|
|
||||||
|
var defaultBoundary: Boundaries = StandardBoundaries.VISIBLE_ON_SCREEN
|
||||||
|
|
||||||
|
private val actions = object : SessionActions {
|
||||||
|
override fun showHighlights(results: Map<Editor, IntList>, query: SearchQuery) {
|
||||||
|
textHighlighter.renderOccurrences(results, query)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun hideHighlights() {
|
||||||
|
textHighlighter.reset()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun setTagMarkers(markers: Map<Editor, Collection<TagMarker>>) {
|
||||||
|
for ((editor, canvas) in tagCanvases) {
|
||||||
|
canvas.setMarkers(markers[editor].orEmpty())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
init {
|
||||||
|
KeyLayoutCache.ensureInitialized(AceConfig.settings)
|
||||||
|
EditorKeyListener.attach(mainEditor) { editor, charTyped, _ -> typeCharacter(editor, charTyped) }
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun typeCharacter(editor: Editor, charTyped: Char) {
|
||||||
|
val state = state ?: return
|
||||||
|
|
||||||
|
editorSettings.startEditing(editor)
|
||||||
|
val result = mode.type(state, charTyped, acceptedTag)
|
||||||
|
editorSettings.stopEditing(editor)
|
||||||
|
|
||||||
|
when (result) {
|
||||||
|
Nothing -> return
|
||||||
|
is ChangeState -> this.state = result.state
|
||||||
|
is ChangeMode -> setMode(result.mode)
|
||||||
|
|
||||||
|
is AcceptTag -> {
|
||||||
|
acceptedTag = result.tag
|
||||||
|
mode.accept(state, result.tag)
|
||||||
|
end()
|
||||||
|
}
|
||||||
|
|
||||||
|
EndSession -> end()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun setMode(mode: SessionMode) {
|
||||||
|
this.mode = mode
|
||||||
|
mainEditor.colorsScheme.setColor(EditorColors.CARET_COLOR, mode.caretColor)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun startJumpMode() {
|
||||||
|
startJumpMode(::JumpMode)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun startJumpMode(mode: () -> JumpMode) {
|
||||||
|
if (this::mode.isInitialized && mode is JumpMode) {
|
||||||
|
end()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this::mode.isInitialized) {
|
||||||
|
restart()
|
||||||
|
}
|
||||||
|
|
||||||
|
setMode(mode())
|
||||||
|
state = SessionState.WaitForKey(actions, jumpEditors, defaultBoundary, AceConfig.invertUppercaseMode)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Starts a regular expression search. If a search was already active, it will be reset alongside its tags and highlights.
|
||||||
|
*/
|
||||||
|
fun startRegexSearch(pattern: Pattern) {
|
||||||
|
if (!this::mode.isInitialized) {
|
||||||
|
setMode(JumpMode())
|
||||||
|
}
|
||||||
|
|
||||||
|
for (canvas in tagCanvases.values) {
|
||||||
|
canvas.setMarkers(emptyList())
|
||||||
|
}
|
||||||
|
|
||||||
|
val processor = SearchProcessor(jumpEditors, SearchQuery.RegularExpression(pattern.regex), defaultBoundary, AceConfig.invertUppercaseMode)
|
||||||
|
textHighlighter.renderOccurrences(processor.resultsCopy, processor.query)
|
||||||
|
|
||||||
|
state = SessionState.SelectTag(actions, jumpEditors, processor)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun tagImmediately() {
|
||||||
|
typeCharacter(mainEditor, '\n')
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ends this session.
|
||||||
|
*/
|
||||||
|
fun end() {
|
||||||
|
SessionManager.end(mainEditor)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clears any currently active search, tags, and highlights.
|
||||||
|
*/
|
||||||
|
fun restart() {
|
||||||
|
state = null
|
||||||
|
acceptedTag = null
|
||||||
|
tagCanvases.values.forEach(TagCanvas::removeMarkers)
|
||||||
|
textHighlighter.reset()
|
||||||
|
|
||||||
|
HintManagerImpl.getInstanceImpl().hideAllHints()
|
||||||
|
editorSettings.onTagUnaccepted(mainEditor)
|
||||||
|
mainEditor.colorsScheme.setColor(EditorColors.CARET_COLOR, mode.caretColor)
|
||||||
|
jumpEditors.forEach { it.contentComponent.repaint() }
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Should only be used from [SessionManager] to dispose a successfully ended session.
|
||||||
|
*/
|
||||||
|
internal fun dispose() {
|
||||||
|
tagCanvases.values.forEach(TagCanvas::unbind)
|
||||||
|
textHighlighter.reset()
|
||||||
|
EditorKeyListener.detach(mainEditor)
|
||||||
|
|
||||||
|
if (!mainEditor.isDisposed) {
|
||||||
|
HintManagerImpl.getInstanceImpl().hideAllHints()
|
||||||
|
editorSettings.restore(mainEditor)
|
||||||
|
mainEditor.colorsScheme.setColor(EditorColors.CARET_COLOR, AbstractColorsScheme.INHERITED_COLOR_MARKER)
|
||||||
|
|
||||||
|
val focusedEditor = acceptedTag?.editor ?: mainEditor
|
||||||
|
focusedEditor.scrollingModel.scrollToCaret(ScrollType.MAKE_VISIBLE)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
13
src/main/kotlin/org/acejump/session/SessionActions.kt
Normal file
13
src/main/kotlin/org/acejump/session/SessionActions.kt
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package org.acejump.session
|
||||||
|
|
||||||
|
import com.intellij.openapi.editor.Editor
|
||||||
|
import it.unimi.dsi.fastutil.ints.IntList
|
||||||
|
import org.acejump.search.SearchQuery
|
||||||
|
import org.acejump.view.TagMarker
|
||||||
|
|
||||||
|
internal interface SessionActions {
|
||||||
|
fun showHighlights(results: Map<Editor, IntList>, query: SearchQuery)
|
||||||
|
fun hideHighlights()
|
||||||
|
|
||||||
|
fun setTagMarkers(markers: Map<Editor, Collection<TagMarker>>)
|
||||||
|
}
|
50
src/main/kotlin/org/acejump/session/SessionManager.kt
Normal file
50
src/main/kotlin/org/acejump/session/SessionManager.kt
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
package org.acejump.session
|
||||||
|
|
||||||
|
import com.intellij.openapi.editor.Editor
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Manages active [Session]s in [Editor]s. There may only be one [Session] per [Editor], but multiple [Session]s across multiple [Editor]s
|
||||||
|
* may be active at once.
|
||||||
|
*
|
||||||
|
* It is possible for an [Editor] to be disposed with an active [Session]. In such case, the reference to both will remain until a new
|
||||||
|
* [Session] starts, at which point the [SessionManager.cleanup] method will purge disposed [Editor]s.
|
||||||
|
*/
|
||||||
|
object SessionManager {
|
||||||
|
private val sessions = HashMap<Editor, Session>(4)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Starts a new [Session], or returns an existing [Session] if the specified [Editor] already has one.
|
||||||
|
*/
|
||||||
|
fun start(editor: Editor): Session {
|
||||||
|
return start(editor, listOf(editor))
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Starts a new multi-editor [Session], or returns an existing [Session] if the specified main [Editor] already has one.
|
||||||
|
* The [mainEditor] is used for typing the search query and tag.
|
||||||
|
* The [jumpEditors] are all editors that will be searched and tagged.
|
||||||
|
*/
|
||||||
|
fun start(mainEditor: Editor, jumpEditors: List<Editor>): Session {
|
||||||
|
return sessions.getOrPut(mainEditor) { cleanup(); Session(mainEditor, jumpEditors) }
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the active [Session] in the specified [Editor], or null if the [Editor] has no active session.
|
||||||
|
*/
|
||||||
|
operator fun get(editor: Editor): Session? {
|
||||||
|
return sessions[editor]
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ends the active [Session] in the specified [Editor], or does nothing if the [Editor] has no active session.
|
||||||
|
*/
|
||||||
|
fun end(editor: Editor) {
|
||||||
|
sessions.remove(editor)?.dispose()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun cleanup() {
|
||||||
|
for (disposedEditor in sessions.keys.filter { it.isDisposed }) {
|
||||||
|
sessions.remove(disposedEditor)?.dispose()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
95
src/main/kotlin/org/acejump/session/SessionState.kt
Normal file
95
src/main/kotlin/org/acejump/session/SessionState.kt
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
package org.acejump.session
|
||||||
|
|
||||||
|
import com.intellij.openapi.editor.Editor
|
||||||
|
import org.acejump.boundaries.Boundaries
|
||||||
|
import org.acejump.boundaries.StandardBoundaries
|
||||||
|
import org.acejump.config.AceConfig
|
||||||
|
import org.acejump.search.SearchProcessor
|
||||||
|
import org.acejump.search.SearchQuery
|
||||||
|
import org.acejump.search.Tagger
|
||||||
|
import org.acejump.search.TaggingResult
|
||||||
|
|
||||||
|
sealed interface SessionState {
|
||||||
|
fun type(char: Char): TypeResult
|
||||||
|
|
||||||
|
class WaitForKey internal constructor(
|
||||||
|
private val actions: SessionActions,
|
||||||
|
private val jumpEditors: List<Editor>,
|
||||||
|
private val defaultBoundary: Boundaries,
|
||||||
|
private val invertUppercaseMode: Boolean,
|
||||||
|
) : SessionState {
|
||||||
|
override fun type(char: Char): TypeResult {
|
||||||
|
val searchProcessor = SearchProcessor(jumpEditors, SearchQuery.Literal(char.toString()), defaultBoundary, invertUppercaseMode)
|
||||||
|
|
||||||
|
return if (searchProcessor.isQueryFinished) {
|
||||||
|
TypeResult.ChangeState(SelectTag(actions, jumpEditors, searchProcessor))
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
TypeResult.ChangeState(RefineSearchQuery(actions, jumpEditors, searchProcessor))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class RefineSearchQuery internal constructor(
|
||||||
|
private val actions: SessionActions,
|
||||||
|
private val jumpEditors: List<Editor>,
|
||||||
|
private val searchProcessor: SearchProcessor,
|
||||||
|
) : SessionState {
|
||||||
|
init {
|
||||||
|
actions.showHighlights(searchProcessor.resultsCopy, searchProcessor.query)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun type(char: Char): TypeResult {
|
||||||
|
return if (searchProcessor.refineQuery(char)) {
|
||||||
|
actions.hideHighlights()
|
||||||
|
TypeResult.ChangeState(SelectTag(actions, jumpEditors, searchProcessor))
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
actions.showHighlights(searchProcessor.resultsCopy, searchProcessor.query)
|
||||||
|
TypeResult.Nothing
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class SelectTag internal constructor(
|
||||||
|
private val actions: SessionActions,
|
||||||
|
private val jumpEditors: List<Editor>,
|
||||||
|
private val searchProcessor: SearchProcessor,
|
||||||
|
) : SessionState {
|
||||||
|
private val tagger = Tagger(jumpEditors, searchProcessor.resultsCopy)
|
||||||
|
|
||||||
|
init {
|
||||||
|
actions.setTagMarkers(tagger.markers)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun type(char: Char): TypeResult {
|
||||||
|
if (char == ' ') {
|
||||||
|
val query = searchProcessor.query
|
||||||
|
if (query is SearchQuery.Literal) {
|
||||||
|
val newBoundaries = when (searchProcessor.boundaries) {
|
||||||
|
StandardBoundaries.VISIBLE_ON_SCREEN -> StandardBoundaries.AFTER_CARET
|
||||||
|
StandardBoundaries.AFTER_CARET -> StandardBoundaries.BEFORE_CARET
|
||||||
|
StandardBoundaries.BEFORE_CARET -> StandardBoundaries.VISIBLE_ON_SCREEN
|
||||||
|
else -> searchProcessor.boundaries
|
||||||
|
}
|
||||||
|
|
||||||
|
val newSearchProcessor = SearchProcessor(jumpEditors, query, newBoundaries, searchProcessor.invertUppercaseMode)
|
||||||
|
return TypeResult.ChangeState(SelectTag(actions, jumpEditors, newSearchProcessor))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (char == '\n') {
|
||||||
|
val newSearchProcessor = SearchProcessor(jumpEditors, searchProcessor.query, searchProcessor.boundaries, !searchProcessor.invertUppercaseMode)
|
||||||
|
return TypeResult.ChangeState(SelectTag(actions, jumpEditors, newSearchProcessor))
|
||||||
|
}
|
||||||
|
|
||||||
|
return when (val result = tagger.type(AceConfig.layout.characterRemapping.getOrDefault(char, char))) {
|
||||||
|
is TaggingResult.Nothing -> TypeResult.Nothing
|
||||||
|
is TaggingResult.Accept -> TypeResult.AcceptTag(result.tag)
|
||||||
|
is TaggingResult.Mark -> {
|
||||||
|
actions.setTagMarkers(result.markers)
|
||||||
|
TypeResult.Nothing
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
12
src/main/kotlin/org/acejump/session/TypeResult.kt
Normal file
12
src/main/kotlin/org/acejump/session/TypeResult.kt
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package org.acejump.session
|
||||||
|
|
||||||
|
import org.acejump.modes.SessionMode
|
||||||
|
import org.acejump.search.Tag
|
||||||
|
|
||||||
|
sealed class TypeResult {
|
||||||
|
object Nothing : TypeResult()
|
||||||
|
class ChangeState(val state: SessionState) : TypeResult()
|
||||||
|
class ChangeMode(val mode: SessionMode) : TypeResult()
|
||||||
|
class AcceptTag(val tag: Tag) : TypeResult()
|
||||||
|
object EndSession : TypeResult()
|
||||||
|
}
|
@@ -1,69 +0,0 @@
|
|||||||
package org.acejump.view
|
|
||||||
|
|
||||||
import org.acejump.search.getLineEndOffset
|
|
||||||
import org.acejump.search.getLineStartOffset
|
|
||||||
import org.acejump.view.Model.editor
|
|
||||||
import org.acejump.view.Model.editorText
|
|
||||||
import org.acejump.view.Model.viewBounds
|
|
||||||
import kotlin.math.max
|
|
||||||
import kotlin.math.min
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Interface which defines the boundary inside the file to be searched
|
|
||||||
*/
|
|
||||||
|
|
||||||
enum class Boundary: ClosedRange<Int> {
|
|
||||||
// Search the complete file
|
|
||||||
FULL_FILE_BOUNDARY {
|
|
||||||
override val start: Int
|
|
||||||
get() = 0
|
|
||||||
override val endInclusive: Int
|
|
||||||
get() = editorText.length
|
|
||||||
},
|
|
||||||
// Search only on the screen
|
|
||||||
SCREEN_BOUNDARY {
|
|
||||||
override val start: Int
|
|
||||||
get() = Model.viewBounds.first
|
|
||||||
override val endInclusive: Int
|
|
||||||
get() = Model.viewBounds.last
|
|
||||||
},
|
|
||||||
// Search from the start of the screen to the caret
|
|
||||||
BEFORE_CARET_BOUNDARY {
|
|
||||||
override val start: Int
|
|
||||||
get() = max(0, viewBounds.first)
|
|
||||||
override val endInclusive: Int
|
|
||||||
get() = min(editor.caretModel.offset, viewBounds.last)
|
|
||||||
},
|
|
||||||
// Search from the caret to the end of the screen
|
|
||||||
AFTER_CARET_BOUNDARY {
|
|
||||||
override val start: Int
|
|
||||||
get() = max(editor.caretModel.offset, viewBounds.first)
|
|
||||||
override val endInclusive: Int
|
|
||||||
get() = viewBounds.last
|
|
||||||
},
|
|
||||||
// Search on the current line
|
|
||||||
CURRENT_LINE_BOUNDARY {
|
|
||||||
override val start: Int
|
|
||||||
get() = editor.getLineStartOffset(editor.caretModel.logicalPosition.line)
|
|
||||||
override val endInclusive: Int
|
|
||||||
get() = editor.getLineEndOffset(editor.caretModel.logicalPosition.line)
|
|
||||||
},
|
|
||||||
// Search after caret within line
|
|
||||||
CURRENT_LINE_AFTER_CARET_BOUNDARY {
|
|
||||||
override val start: Int
|
|
||||||
get() = max(editor.caretModel.offset, viewBounds.first)
|
|
||||||
override val endInclusive: Int
|
|
||||||
get() = editor.getLineEndOffset(editor.caretModel.logicalPosition.line)
|
|
||||||
},
|
|
||||||
// Search before caret within line
|
|
||||||
CURRENT_LINE_BEFORE_CARET_BOUNDARY {
|
|
||||||
override val start: Int
|
|
||||||
get() = editor.getLineStartOffset(editor.caretModel.logicalPosition.line)
|
|
||||||
override val endInclusive: Int
|
|
||||||
get() = min(editor.caretModel.offset, viewBounds.last)
|
|
||||||
};
|
|
||||||
|
|
||||||
fun intRange() = IntRange(start, endInclusive)
|
|
||||||
|
|
||||||
override fun toString() = super.toString() + " (${intRange()}) "
|
|
||||||
}
|
|
@@ -1,67 +0,0 @@
|
|||||||
package org.acejump.view
|
|
||||||
|
|
||||||
import com.intellij.openapi.application.ApplicationInfo
|
|
||||||
import com.intellij.openapi.diagnostic.Logger
|
|
||||||
import com.intellij.openapi.editor.Editor
|
|
||||||
import org.acejump.search.*
|
|
||||||
import org.acejump.view.Model.fontWidth
|
|
||||||
import org.acejump.view.Model.viewBounds
|
|
||||||
import java.awt.Graphics
|
|
||||||
import java.awt.Point
|
|
||||||
import javax.swing.JComponent
|
|
||||||
import javax.swing.SwingUtilities.convertPoint
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Overlay composed of all tag [Marker]s. Maintains a registry of tags' visual
|
|
||||||
* positions once assigned, so that we do not paint two tags to the same space.
|
|
||||||
*/
|
|
||||||
|
|
||||||
object Canvas: JComponent(), Resettable {
|
|
||||||
private val logger = Logger.getInstance(Canvas::class.java)
|
|
||||||
private val occupied = hashSetOf<Point>()
|
|
||||||
@Volatile
|
|
||||||
var jumpLocations: Collection<Marker> = emptyList()
|
|
||||||
set(value) {
|
|
||||||
field = value
|
|
||||||
runLater { repaint() }
|
|
||||||
}
|
|
||||||
|
|
||||||
fun Editor.bindCanvas() {
|
|
||||||
reset()
|
|
||||||
storeBounds()
|
|
||||||
contentComponent.add(Canvas)
|
|
||||||
setBounds(0, 0, contentComponent.width, contentComponent.height)
|
|
||||||
|
|
||||||
if (ApplicationInfo.getInstance().build.components.first() < 173) {
|
|
||||||
val loc = convertPoint(Canvas, location, component.rootPane)
|
|
||||||
setLocation(-loc.x, -loc.y)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun Editor.storeBounds() {
|
|
||||||
viewBounds = getView()
|
|
||||||
// TODO: Fix reference, cf. https://github.com/acejump/AceJump/issues/200
|
|
||||||
this::offsetToLogicalPosition.let {
|
|
||||||
logger.info("View bounds: $viewBounds (lines " +
|
|
||||||
"${it(viewBounds.first).line}..${it(viewBounds.last).line})")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun paint(graphics: Graphics) {
|
|
||||||
jumpLocations.ifEmpty { return }
|
|
||||||
|
|
||||||
super.paint(graphics)
|
|
||||||
occupied.clear()
|
|
||||||
jumpLocations.forEach { it.paintMe(graphics) }
|
|
||||||
}
|
|
||||||
|
|
||||||
fun registerTag(p: Point, tag: String) =
|
|
||||||
(-1..tag.length).forEach { occupied.add(Point(p.x + it * fontWidth, p.y)) }
|
|
||||||
|
|
||||||
fun isFree(point: Point) = point !in occupied
|
|
||||||
|
|
||||||
override fun reset() {
|
|
||||||
jumpLocations = emptyList()
|
|
||||||
occupied.clear()
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,230 +0,0 @@
|
|||||||
package org.acejump.view
|
|
||||||
|
|
||||||
import com.intellij.openapi.editor.Editor
|
|
||||||
import com.intellij.openapi.editor.markup.CustomHighlighterRenderer
|
|
||||||
import com.intellij.openapi.editor.markup.RangeHighlighter
|
|
||||||
import org.acejump.config.AceConfig
|
|
||||||
import org.acejump.control.Selector
|
|
||||||
import org.acejump.label.Tagger.regex
|
|
||||||
import org.acejump.search.*
|
|
||||||
import org.acejump.search.JumpMode.TARGET
|
|
||||||
import org.acejump.view.Marker.Alignment.*
|
|
||||||
import org.acejump.view.Model.arcD
|
|
||||||
import org.acejump.view.Model.editor
|
|
||||||
import org.acejump.view.Model.fontHeight
|
|
||||||
import org.acejump.view.Model.fontWidth
|
|
||||||
import org.acejump.view.Model.naturalCaretColor
|
|
||||||
import org.acejump.view.Model.rectHeight
|
|
||||||
import org.acejump.view.Model.rectVOffset
|
|
||||||
import java.awt.*
|
|
||||||
import java.awt.AlphaComposite.SRC_OVER
|
|
||||||
import java.awt.AlphaComposite.getInstance
|
|
||||||
import java.awt.RenderingHints.KEY_ANTIALIASING
|
|
||||||
import java.awt.RenderingHints.VALUE_ANTIALIAS_ON
|
|
||||||
import org.acejump.view.Model.editorText as text
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents the visual component of a tag, which gets painted to the screen.
|
|
||||||
* All functionality related to tag highlighting including visual appearance,
|
|
||||||
* alignment and painting should be handled here. Tags are "captioned" with two
|
|
||||||
* or fewer characters. To select a tag, a user will type the tag's assigned
|
|
||||||
* "caption", which will move the caret to a known index in the document.
|
|
||||||
*
|
|
||||||
* TODO: Clean up this class.
|
|
||||||
*/
|
|
||||||
|
|
||||||
class Marker {
|
|
||||||
val index: Int
|
|
||||||
private val query: String
|
|
||||||
val tag: String?
|
|
||||||
private val tagUpperCase: String?
|
|
||||||
private var srcPoint: Point
|
|
||||||
private var queryLength: Int
|
|
||||||
private var trueOffset: Int
|
|
||||||
private val searchWidth: Int
|
|
||||||
private val tgIdx: Int
|
|
||||||
private val start: Point
|
|
||||||
private val startY: Int
|
|
||||||
private var tagPoint: Point
|
|
||||||
private var yPos: Int
|
|
||||||
private var alignment: Alignment
|
|
||||||
private val endsWith: Boolean
|
|
||||||
|
|
||||||
constructor(query: String, tag: String?, index: Int) {
|
|
||||||
this.query = query
|
|
||||||
this.tag = tag
|
|
||||||
this.tagUpperCase = tag?.toUpperCase()
|
|
||||||
this.index = index
|
|
||||||
val lastQueryChar = query.last()
|
|
||||||
endsWith = tag != null && lastQueryChar == tag[0]
|
|
||||||
queryLength = query.length - if(endsWith) 1 else 0
|
|
||||||
trueOffset = query.length - 1
|
|
||||||
|
|
||||||
searchWidth = if (regex) 0 else queryLength * fontWidth
|
|
||||||
|
|
||||||
var i = 1
|
|
||||||
while (i < query.length && index + i < text.length &&
|
|
||||||
query[i].equals(text[index + i], ignoreCase = true)) i++
|
|
||||||
trueOffset = i - 1
|
|
||||||
queryLength = i
|
|
||||||
|
|
||||||
tgIdx = index + trueOffset
|
|
||||||
|
|
||||||
editor.run {
|
|
||||||
start = getPoint(index)
|
|
||||||
srcPoint = getPointRelative(start, contentComponent)
|
|
||||||
tagPoint =
|
|
||||||
if (index == tgIdx) srcPoint
|
|
||||||
else getPointRelative(getPoint(tgIdx), contentComponent)
|
|
||||||
}
|
|
||||||
|
|
||||||
startY = start.y + rectVOffset
|
|
||||||
yPos = tagPoint.y + rectVOffset
|
|
||||||
alignment = RIGHT
|
|
||||||
}
|
|
||||||
|
|
||||||
enum class Alignment { /*TOP, BOTTOM,*/ LEFT, RIGHT, NONE }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Called by AceJump as a renderable element of [Canvas]. Paints [tag]s.
|
|
||||||
*/
|
|
||||||
|
|
||||||
fun paintMe(graphics: Graphics) = (graphics as Graphics2D).run {
|
|
||||||
setRenderingHint(KEY_ANTIALIASING, VALUE_ANTIALIAS_ON)
|
|
||||||
|
|
||||||
if (!Finder.skim)
|
|
||||||
tag?.alignTag(Canvas)
|
|
||||||
?.apply { Canvas.registerTag(this, tag) }
|
|
||||||
?.let {
|
|
||||||
if (alignment == NONE) return
|
|
||||||
highlightTag(it); drawTagForeground(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Called by IntelliJ as a [CustomHighlighterRenderer]. Paints [highlight]s.
|
|
||||||
*/
|
|
||||||
|
|
||||||
companion object: CustomHighlighterRenderer {
|
|
||||||
override fun paint(editor: Editor, highlighter: RangeHighlighter, g: Graphics) =
|
|
||||||
(g as Graphics2D).run {
|
|
||||||
setRenderingHint(KEY_ANTIALIASING, VALUE_ANTIALIAS_ON)
|
|
||||||
color = AceConfig.textHighlightColor
|
|
||||||
val start = editor.getPoint(highlighter.startOffset)
|
|
||||||
val startY = start.y + rectVOffset
|
|
||||||
|
|
||||||
if (regex) {
|
|
||||||
highlightRegex(start.x, startY)
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
val queryLength = highlighter.endOffset - highlighter.startOffset
|
|
||||||
val searchWidth = queryLength * fontWidth
|
|
||||||
fillRoundRect(start.x, startY, searchWidth, rectHeight, arcD, arcD)
|
|
||||||
if (JumpMode.equals(TARGET)) {
|
|
||||||
surroundTargetWord(highlighter.startOffset, startY)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO this is a lag-fest, results should be cached before this gets turned back on
|
|
||||||
//if (highlighter.startOffset == Selector.nearestVisibleMatches().firstOrNull()) {
|
|
||||||
// color = naturalCaretColor
|
|
||||||
// drawLine(start.x, startY, start.x, startY + rectHeight)
|
|
||||||
//}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun Graphics2D.highlightRegex(x: Int, y: Int) {
|
|
||||||
composite = getInstance(SRC_OVER, 0.40.toFloat())
|
|
||||||
fillRoundRect(x, y, fontWidth, rectHeight, arcD, arcD)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun Graphics2D.surroundTargetWord(index: Int, startY: Int) {
|
|
||||||
if (!text[index].isLetterOrDigit()) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
color = AceConfig.targetModeColor
|
|
||||||
val (wordStart, wordEnd) = text.wordBounds(index)
|
|
||||||
|
|
||||||
val xPos = editor.getPoint(wordStart).x
|
|
||||||
val wordWidth = (wordEnd - wordStart) * fontWidth
|
|
||||||
|
|
||||||
drawRoundRect(xPos, startY, wordWidth, rectHeight, arcD, arcD)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun Graphics2D.drawTagForeground(tagPosition: Point) {
|
|
||||||
font = Model.font
|
|
||||||
color = AceConfig.tagForegroundColor
|
|
||||||
composite = getInstance(SRC_OVER, 1.toFloat())
|
|
||||||
|
|
||||||
drawString(tagUpperCase!!, tagPosition.x, tagPosition.y + fontHeight)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Fix tag alignment and visibility issues
|
|
||||||
// https://github.com/acejump/AceJump/issues/233
|
|
||||||
// https://github.com/acejump/AceJump/issues/228
|
|
||||||
private fun String.alignTag(canvas: Canvas): Point {
|
|
||||||
val xPos = tagPoint.x + fontWidth
|
|
||||||
// val top = Point(x - fontWidth, y - fontHeight)
|
|
||||||
// val bottom = Point(x - fontWidth, y + fontHeight)
|
|
||||||
val left = Point(srcPoint.x - fontWidth * length, yPos)
|
|
||||||
val right = Point(xPos, yPos)
|
|
||||||
|
|
||||||
val nextCharIsWhiteSpace = text.length <= index + 1 ||
|
|
||||||
text[index + 1].isWhitespace()
|
|
||||||
|
|
||||||
val canAlignRight = canvas.isFree(right)
|
|
||||||
val isFirstCharacterOfLine = editor.isFirstCharacterOfLine(index)
|
|
||||||
val canAlignLeft = !isFirstCharacterOfLine && canvas.isFree(left)
|
|
||||||
|
|
||||||
alignment = when {
|
|
||||||
nextCharIsWhiteSpace -> RIGHT
|
|
||||||
isFirstCharacterOfLine -> RIGHT
|
|
||||||
canAlignLeft -> LEFT
|
|
||||||
canAlignRight -> RIGHT
|
|
||||||
else -> NONE
|
|
||||||
}
|
|
||||||
|
|
||||||
return when (alignment) {
|
|
||||||
// TOP -> top
|
|
||||||
LEFT -> left
|
|
||||||
RIGHT -> right
|
|
||||||
// BOTTOM -> bottom
|
|
||||||
NONE -> Point(-1, -1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun Graphics2D.highlightTag(point: Point?) {
|
|
||||||
if (query.isEmpty() || alignment == NONE) return
|
|
||||||
|
|
||||||
var tagX = point!!.x
|
|
||||||
var tagWidth = tag?.length?.times(fontWidth) ?: 0
|
|
||||||
val charIndex = index + query.length - 1
|
|
||||||
val beforeEnd = charIndex < text.length
|
|
||||||
val textChar = if (beforeEnd) text[charIndex].toLowerCase() else 0.toChar()
|
|
||||||
|
|
||||||
fun highlightFirst() {
|
|
||||||
composite = getInstance(SRC_OVER, 0.40.toFloat())
|
|
||||||
color = AceConfig.textHighlightColor
|
|
||||||
|
|
||||||
if (endsWith && query.last() != textChar) {
|
|
||||||
fillRoundRect(tagX, yPos, fontWidth, rectHeight, arcD, arcD)
|
|
||||||
tagX += fontWidth
|
|
||||||
tagWidth -= fontWidth
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun highlightLast() {
|
|
||||||
color = AceConfig.tagBackgroundColor
|
|
||||||
if (alignment != RIGHT || text.hasSpaceRight(index) || regex)
|
|
||||||
composite = getInstance(SRC_OVER, 1.toFloat())
|
|
||||||
|
|
||||||
fillRoundRect(tagX, yPos, tagWidth, rectHeight, arcD, arcD)
|
|
||||||
}
|
|
||||||
|
|
||||||
highlightFirst()
|
|
||||||
highlightLast()
|
|
||||||
}
|
|
||||||
|
|
||||||
infix fun inside(viewBounds: IntRange) = index in viewBounds
|
|
||||||
}
|
|
@@ -1,73 +0,0 @@
|
|||||||
package org.acejump.view
|
|
||||||
|
|
||||||
import com.github.promeg.pinyinhelper.Pinyin
|
|
||||||
import com.intellij.openapi.editor.Editor
|
|
||||||
import com.intellij.openapi.editor.colors.EditorColors.CARET_COLOR
|
|
||||||
import com.intellij.openapi.editor.impl.EditorImpl
|
|
||||||
import com.intellij.openapi.project.ProjectManager
|
|
||||||
import org.acejump.config.AceConfig
|
|
||||||
import org.acejump.search.defaultEditor
|
|
||||||
import org.acejump.view.Boundary.FULL_FILE_BOUNDARY
|
|
||||||
import java.awt.Color.BLACK
|
|
||||||
import java.awt.Color.YELLOW
|
|
||||||
import java.awt.Font
|
|
||||||
import java.awt.Font.BOLD
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Data holder for all settings and IDE components needed by AceJump.
|
|
||||||
*
|
|
||||||
* TODO: Integrate this class with [org.acejump.config.AceSettings]
|
|
||||||
*/
|
|
||||||
|
|
||||||
object Model {
|
|
||||||
var editor: Editor = defaultEditor()
|
|
||||||
|
|
||||||
val markup
|
|
||||||
get() = editor.markupModel
|
|
||||||
val project
|
|
||||||
get() = editor.project ?: ProjectManager.getInstance().defaultProject
|
|
||||||
val caretOffset
|
|
||||||
get() = editor.caretModel.offset
|
|
||||||
val editorText: String
|
|
||||||
get() = editor.document.text
|
|
||||||
.let { if (AceConfig.supportPinyin) it.mapToPinyin() else it }
|
|
||||||
|
|
||||||
private fun String.mapToPinyin() =
|
|
||||||
map { Pinyin.toPinyin(it).first() }.joinToString("")
|
|
||||||
|
|
||||||
var naturalBlock = false
|
|
||||||
var naturalBlink = true
|
|
||||||
var naturalCaretColor = BLACK
|
|
||||||
var naturalHighlight = YELLOW
|
|
||||||
|
|
||||||
val scheme
|
|
||||||
get() = editor.colorsScheme
|
|
||||||
val font
|
|
||||||
get() = Font(scheme.editorFontName, BOLD, scheme.editorFontSize)
|
|
||||||
val fontWidth
|
|
||||||
get() = editor.component.getFontMetrics(font).stringWidth("w")
|
|
||||||
val fontHeight
|
|
||||||
get() = editor.colorsScheme.editorFontSize
|
|
||||||
val lineHeight
|
|
||||||
get() = editor.lineHeight
|
|
||||||
val rectHeight
|
|
||||||
get() = fontHeight + 3
|
|
||||||
val rectVOffset
|
|
||||||
get() = lineHeight - (editor as EditorImpl).descent - fontHeight
|
|
||||||
val arcD
|
|
||||||
get() = if (AceConfig.roundedTagCorners) rectHeight - 6 else 1
|
|
||||||
var viewBounds = 0..0
|
|
||||||
const val LONG_DOCUMENT_LENGTH = 100000
|
|
||||||
val LONG_DOCUMENT
|
|
||||||
get() = LONG_DOCUMENT_LENGTH < editorText.length
|
|
||||||
const val MAX_TAG_RESULTS = 300
|
|
||||||
|
|
||||||
val defaultBoundary = FULL_FILE_BOUNDARY
|
|
||||||
var boundaries: Boundary = defaultBoundary
|
|
||||||
|
|
||||||
fun Editor.setupCaret() {
|
|
||||||
settings.isBlockCursor = true
|
|
||||||
settings.isBlinkCaret = false
|
|
||||||
colorsScheme.setColor(CARET_COLOR, AceConfig.jumpModeColor)
|
|
||||||
}
|
|
||||||
}
|
|
78
src/main/kotlin/org/acejump/view/TagCanvas.kt
Normal file
78
src/main/kotlin/org/acejump/view/TagCanvas.kt
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
package org.acejump.view
|
||||||
|
|
||||||
|
import com.intellij.openapi.application.ApplicationInfo
|
||||||
|
import com.intellij.openapi.editor.Editor
|
||||||
|
import com.intellij.ui.ColorUtil
|
||||||
|
import org.acejump.boundaries.EditorOffsetCache
|
||||||
|
import org.acejump.boundaries.StandardBoundaries
|
||||||
|
import org.acejump.config.AceConfig
|
||||||
|
import java.awt.Graphics
|
||||||
|
import java.awt.Graphics2D
|
||||||
|
import java.awt.Rectangle
|
||||||
|
import java.awt.RenderingHints
|
||||||
|
import javax.swing.JComponent
|
||||||
|
import javax.swing.SwingUtilities
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Holds all active tag markers and renders them on top of the editor.
|
||||||
|
*/
|
||||||
|
internal class TagCanvas(private val editor: Editor) : JComponent() {
|
||||||
|
private var markers: Collection<TagMarker> = emptyList()
|
||||||
|
private var visible = false
|
||||||
|
|
||||||
|
init {
|
||||||
|
val contentComponent = editor.contentComponent
|
||||||
|
contentComponent.add(this)
|
||||||
|
setBounds(0, 0, contentComponent.width, contentComponent.height)
|
||||||
|
|
||||||
|
if (ApplicationInfo.getInstance().build.components.first() < 173) {
|
||||||
|
SwingUtilities.convertPoint(this, location, editor.component.rootPane).let { setLocation(-it.x, -it.y) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun unbind() {
|
||||||
|
editor.contentComponent.remove(this)
|
||||||
|
editor.contentComponent.repaint()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun setMarkers(markers: Collection<TagMarker>) {
|
||||||
|
this.markers = markers
|
||||||
|
this.visible = true
|
||||||
|
repaint()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun removeMarkers() {
|
||||||
|
this.markers = emptyList()
|
||||||
|
this.visible = false
|
||||||
|
repaint()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun paintChildren(g: Graphics) {
|
||||||
|
super.paintChildren(g)
|
||||||
|
|
||||||
|
if (!visible) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
g.color = ColorUtil.withAlpha(editor.colorsScheme.defaultBackground, (AceConfig.editorFadeOpacity * 0.01).coerceIn(0.0, 1.0))
|
||||||
|
g.fillRect(0, 0, width - 1, height - 1)
|
||||||
|
|
||||||
|
if (markers.isEmpty()) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
(g as Graphics2D).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)
|
||||||
|
|
||||||
|
val font = TagFont(editor)
|
||||||
|
|
||||||
|
val cache = EditorOffsetCache.new()
|
||||||
|
val viewRange = StandardBoundaries.VISIBLE_ON_SCREEN.getOffsetRange(editor, cache)
|
||||||
|
val occupied = mutableListOf<Rectangle>()
|
||||||
|
|
||||||
|
for (marker in markers) {
|
||||||
|
if (marker.offset in viewRange) {
|
||||||
|
marker.paint(g, editor, cache, font, occupied)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
20
src/main/kotlin/org/acejump/view/TagFont.kt
Normal file
20
src/main/kotlin/org/acejump/view/TagFont.kt
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package org.acejump.view
|
||||||
|
|
||||||
|
import com.intellij.openapi.editor.Editor
|
||||||
|
import com.intellij.openapi.editor.colors.EditorFontType
|
||||||
|
import org.acejump.config.AceConfig
|
||||||
|
import java.awt.Font
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stores font metrics for aligning and rendering [TagMarker]s.
|
||||||
|
*/
|
||||||
|
internal class TagFont(editor: Editor) {
|
||||||
|
val tagFont: Font = editor.colorsScheme.getFont(EditorFontType.PLAIN)
|
||||||
|
val tagCharWidth = editor.component.getFontMetrics(tagFont).charWidth('W')
|
||||||
|
|
||||||
|
val foregroundColor1 = AceConfig.tagForegroundColor1
|
||||||
|
val foregroundColor2 = AceConfig.tagForegroundColor2
|
||||||
|
|
||||||
|
val lineHeight = editor.lineHeight
|
||||||
|
val baselineDistance = editor.ascent
|
||||||
|
}
|
84
src/main/kotlin/org/acejump/view/TagMarker.kt
Normal file
84
src/main/kotlin/org/acejump/view/TagMarker.kt
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
package org.acejump.view
|
||||||
|
|
||||||
|
import com.intellij.openapi.editor.Editor
|
||||||
|
import com.intellij.openapi.util.SystemInfo
|
||||||
|
import org.acejump.boundaries.EditorOffsetCache
|
||||||
|
import java.awt.Color
|
||||||
|
import java.awt.Graphics2D
|
||||||
|
import java.awt.Point
|
||||||
|
import java.awt.Rectangle
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Describes a 1 or 2 character shortcut that points to a specific character in the editor.
|
||||||
|
*/
|
||||||
|
internal class TagMarker(
|
||||||
|
private val firstChar: String,
|
||||||
|
private val secondChar: String,
|
||||||
|
val offset: Int
|
||||||
|
) {
|
||||||
|
private constructor(tag: String, offset: Int) : this(tag.first().toString(), tag.drop(1), offset)
|
||||||
|
|
||||||
|
private val length = firstChar.length + secondChar.length
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
/**
|
||||||
|
* TODO This might be due to DPI settings.
|
||||||
|
*/
|
||||||
|
private val HIGHLIGHT_OFFSET = if (SystemInfo.isMac) -0.5 else 0.0
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new tag, precomputing some information about the nearby characters to reduce rendering overhead. If the last typed
|
||||||
|
* character ([typedTag]) matches the first [tag] character, only the second [tag] character is displayed.
|
||||||
|
*/
|
||||||
|
fun create(tag: String, offset: Int, typedTag: String): TagMarker {
|
||||||
|
return TagMarker(tag.drop(typedTag.length), offset)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Paints the tag, taking into consideration visual space around characters in the editor, as well as all other previously painted tags.
|
||||||
|
* Returns a rectangle indicating the area where the tag was rendered, or null if the tag could not be rendered due to overlap.
|
||||||
|
*/
|
||||||
|
fun paint(g: Graphics2D, editor: Editor, cache: EditorOffsetCache, font: TagFont, occupied: MutableList<Rectangle>): Rectangle? {
|
||||||
|
val rect = alignTag(editor, cache, font, occupied) ?: return null
|
||||||
|
|
||||||
|
drawHighlight(g, rect, editor.colorsScheme.defaultBackground)
|
||||||
|
drawForeground(g, font, rect.location)
|
||||||
|
|
||||||
|
occupied.add(rect)
|
||||||
|
return rect
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Renders the tag background.
|
||||||
|
*/
|
||||||
|
private fun drawHighlight(g: Graphics2D, rect: Rectangle, color: Color) {
|
||||||
|
g.color = color
|
||||||
|
g.translate(0.0, HIGHLIGHT_OFFSET)
|
||||||
|
g.fillRect(rect.x, rect.y, rect.width, rect.height + 1)
|
||||||
|
g.translate(0.0, -HIGHLIGHT_OFFSET)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Renders the tag text.
|
||||||
|
*/
|
||||||
|
private fun drawForeground(g: Graphics2D, font: TagFont, point: Point) {
|
||||||
|
val x = point.x
|
||||||
|
val y = point.y + font.baselineDistance
|
||||||
|
|
||||||
|
g.font = font.tagFont
|
||||||
|
g.color = font.foregroundColor1
|
||||||
|
g.drawString(firstChar, x, y)
|
||||||
|
|
||||||
|
if (secondChar.isNotEmpty()) {
|
||||||
|
g.color = font.foregroundColor2
|
||||||
|
g.drawString(secondChar, x + font.tagCharWidth, y)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun alignTag(editor: Editor, cache: EditorOffsetCache, font: TagFont, occupied: List<Rectangle>): Rectangle? {
|
||||||
|
val pos = cache.offsetToXY(editor, offset)
|
||||||
|
val rect = Rectangle(pos.x, pos.y, font.tagCharWidth * length, font.lineHeight)
|
||||||
|
return rect.takeIf { occupied.none(it::intersects) }
|
||||||
|
}
|
||||||
|
}
|
123
src/main/kotlin/org/acejump/view/TextHighlighter.kt
Normal file
123
src/main/kotlin/org/acejump/view/TextHighlighter.kt
Normal file
@@ -0,0 +1,123 @@
|
|||||||
|
package org.acejump.view
|
||||||
|
|
||||||
|
import com.intellij.openapi.editor.Editor
|
||||||
|
import com.intellij.openapi.editor.colors.EditorFontType
|
||||||
|
import com.intellij.openapi.editor.markup.CustomHighlighterRenderer
|
||||||
|
import com.intellij.openapi.editor.markup.HighlighterLayer
|
||||||
|
import com.intellij.openapi.editor.markup.HighlighterTargetArea
|
||||||
|
import com.intellij.openapi.editor.markup.RangeHighlighter
|
||||||
|
import com.intellij.ui.ColorUtil
|
||||||
|
import it.unimi.dsi.fastutil.ints.IntList
|
||||||
|
import org.acejump.boundaries.EditorOffsetCache
|
||||||
|
import org.acejump.config.AceConfig
|
||||||
|
import org.acejump.immutableText
|
||||||
|
import org.acejump.search.SearchQuery
|
||||||
|
import java.awt.Color
|
||||||
|
import java.awt.Graphics
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Renders highlights for search occurrences.
|
||||||
|
*/
|
||||||
|
internal class TextHighlighter {
|
||||||
|
private var previousHighlights = mutableMapOf<Editor, Array<RangeHighlighter>>()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes all current highlights and re-creates them from scratch. Must be called whenever any of the method parameters change.
|
||||||
|
*/
|
||||||
|
fun renderOccurrences(results: Map<Editor, IntList>, query: SearchQuery) {
|
||||||
|
render(results, when (query) {
|
||||||
|
is SearchQuery.RegularExpression -> RegexRenderer
|
||||||
|
else -> SearchedWordRenderer
|
||||||
|
}, query::getHighlightLength)
|
||||||
|
}
|
||||||
|
|
||||||
|
private inline fun render(results: Map<Editor, IntList>, renderer: CustomHighlighterRenderer, getHighlightLength: (CharSequence, Int) -> Int) {
|
||||||
|
for ((editor, offsets) in results) {
|
||||||
|
val highlights = previousHighlights[editor]
|
||||||
|
|
||||||
|
val markup = editor.markupModel
|
||||||
|
val document = editor.document
|
||||||
|
val chars = editor.immutableText
|
||||||
|
|
||||||
|
val modifications = (highlights?.size ?: 0) + offsets.size
|
||||||
|
val enableBulkEditing = modifications > 1000
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (enableBulkEditing) {
|
||||||
|
document.isInBulkUpdate = true
|
||||||
|
}
|
||||||
|
|
||||||
|
highlights?.forEach(markup::removeHighlighter)
|
||||||
|
previousHighlights[editor] = Array(offsets.size) { index ->
|
||||||
|
val start = offsets.getInt(index)
|
||||||
|
val end = start + getHighlightLength(chars, start)
|
||||||
|
|
||||||
|
markup.addRangeHighlighter(start, end, LAYER, null, HighlighterTargetArea.EXACT_RANGE).apply {
|
||||||
|
customRenderer = renderer
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
if (enableBulkEditing) {
|
||||||
|
document.isInBulkUpdate = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (editor in previousHighlights.keys.toList()) {
|
||||||
|
if (!results.containsKey(editor)) {
|
||||||
|
previousHighlights.remove(editor)?.forEach(editor.markupModel::removeHighlighter)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun reset() {
|
||||||
|
previousHighlights.keys.forEach { it.markupModel.removeAllHighlighters() }
|
||||||
|
previousHighlights.clear()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Renders a filled highlight in the background of a searched text occurrence.
|
||||||
|
*/
|
||||||
|
private object SearchedWordRenderer : CustomHighlighterRenderer {
|
||||||
|
override fun paint(editor: Editor, highlighter: RangeHighlighter, g: Graphics) {
|
||||||
|
drawFilled(g, editor, highlighter.startOffset, highlighter.endOffset, AceConfig.searchHighlightColor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Renders a filled highlight in the background of the first highlighted position. Used for regex search queries.
|
||||||
|
*/
|
||||||
|
private object RegexRenderer : CustomHighlighterRenderer {
|
||||||
|
override fun paint(editor: Editor, highlighter: RangeHighlighter, g: Graphics) {
|
||||||
|
drawSingle(g, editor, highlighter.startOffset, AceConfig.searchHighlightColor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private companion object {
|
||||||
|
private const val LAYER = HighlighterLayer.LAST + 1
|
||||||
|
|
||||||
|
private fun drawFilled(g: Graphics, editor: Editor, startOffset: Int, endOffset: Int, color: Color) {
|
||||||
|
val start = EditorOffsetCache.Uncached.offsetToXY(editor, startOffset)
|
||||||
|
val end = EditorOffsetCache.Uncached.offsetToXY(editor, endOffset)
|
||||||
|
|
||||||
|
g.color = ColorUtil.withAlpha(AceConfig.searchHighlightColor, 0.2)
|
||||||
|
g.fillRect(start.x, start.y + 1, end.x - start.x, editor.lineHeight - 1)
|
||||||
|
|
||||||
|
g.color = color
|
||||||
|
g.drawRect(start.x, start.y, end.x - start.x, editor.lineHeight)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun drawSingle(g: Graphics, editor: Editor, offset: Int, color: Color) {
|
||||||
|
val pos = EditorOffsetCache.Uncached.offsetToXY(editor, offset)
|
||||||
|
val char = editor.immutableText.getOrNull(offset)?.takeUnless { it == '\n' || it == '\t' } ?: ' '
|
||||||
|
val font = editor.colorsScheme.getFont(EditorFontType.PLAIN)
|
||||||
|
val lastCharWidth = editor.component.getFontMetrics(font).charWidth(char)
|
||||||
|
|
||||||
|
g.color = ColorUtil.withAlpha(AceConfig.searchHighlightColor, 0.2)
|
||||||
|
g.fillRect(pos.x, pos.y + 1, lastCharWidth, editor.lineHeight - 1)
|
||||||
|
|
||||||
|
g.color = color
|
||||||
|
g.drawRect(pos.x, pos.y, lastCharWidth, editor.lineHeight)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -1,26 +0,0 @@
|
|||||||
charactersAndLayoutHeading=Characters and Layout
|
|
||||||
tagCharsToBeUsedLabel=Allowed characters:
|
|
||||||
keyboardLayoutLabel=Keyboard layout:
|
|
||||||
keyboardDesignLabel=Keyboard design:
|
|
||||||
modesHeading=Modes
|
|
||||||
cycleModeOrderLabel=Cycle order:
|
|
||||||
jumpModeDisabled=(Skip)
|
|
||||||
jumpModeJump=Jump
|
|
||||||
jumpModeJumpEnd=Jump to End
|
|
||||||
jumpModeTarget=Target
|
|
||||||
jumpModeDefine=Definition
|
|
||||||
colorsHeading=Colors
|
|
||||||
jumpModeColorLabel=Jump mode caret background:
|
|
||||||
jumpEndModeColorLabel=Jump to End mode caret background:
|
|
||||||
targetModeColorLabel=Target mode caret background:
|
|
||||||
definitionModeColorLabel=Definition mode caret background:
|
|
||||||
textHighlightColorLabel=Searched text background:
|
|
||||||
tagForegroundColorLabel=Tag foreground:
|
|
||||||
tagBackgroundColorLabel=Tag background:
|
|
||||||
appearanceHeading=Appearance
|
|
||||||
displayQueryLabel=Display search query
|
|
||||||
roundedTagCornersLabel=Rounded tag corners
|
|
||||||
languagesHeading=Language
|
|
||||||
behaviorHeading=Behavior
|
|
||||||
searchWholeFileLabel=Search whole file
|
|
||||||
supportPinyin=Support Pinyin selection
|
|
@@ -1,6 +1,6 @@
|
|||||||
<idea-plugin url="https://github.com/acejump/AceJump">
|
<idea-plugin>
|
||||||
<name>AceJump</name>
|
<name>AceJump</name>
|
||||||
<id>AceJump</id>
|
<id>AceJump-chylex</id>
|
||||||
|
|
||||||
<description><![CDATA[
|
<description><![CDATA[
|
||||||
AceJump allows you to quickly navigate the caret to any position visible in the editor.
|
AceJump allows you to quickly navigate the caret to any position visible in the editor.
|
||||||
@@ -9,6 +9,7 @@
|
|||||||
</description>
|
</description>
|
||||||
|
|
||||||
<depends>com.intellij.modules.platform</depends>
|
<depends>com.intellij.modules.platform</depends>
|
||||||
|
<depends>IdeaVIM</depends>
|
||||||
|
|
||||||
<category>Navigation</category>
|
<category>Navigation</category>
|
||||||
<vendor url="https://github.com/acejump/AceJump">AceJump</vendor>
|
<vendor url="https://github.com/acejump/AceJump">AceJump</vendor>
|
||||||
@@ -18,51 +19,33 @@
|
|||||||
<applicationConfigurable groupId="tools" displayName="AceJump"
|
<applicationConfigurable groupId="tools" displayName="AceJump"
|
||||||
instance="org.acejump.config.AceConfigurable"
|
instance="org.acejump.config.AceConfigurable"
|
||||||
id="preferences.AceConfigurable" dynamic="true"/>
|
id="preferences.AceConfigurable" dynamic="true"/>
|
||||||
</extensions>
|
|
||||||
|
|
||||||
|
<editorActionHandler action="EditorEscape" order="first"
|
||||||
|
implementationClass="org.acejump.action.AceEditorAction$Reset"/>
|
||||||
|
<editorActionHandler action="EditorBackSpace" order="first"
|
||||||
|
implementationClass="org.acejump.action.AceEditorAction$ClearSearch"/>
|
||||||
|
<editorActionHandler action="EditorEnter" order="first, before terminalEnter"
|
||||||
|
implementationClass="org.acejump.action.AceEditorAction$TagImmediately"/>
|
||||||
|
</extensions>
|
||||||
|
|
||||||
<actions>
|
<actions>
|
||||||
<action id="AceAction"
|
<action id="AceVimAction_JumpAllEditors" class="org.acejump.action.AceVimAction$JumpAllEditors" text="AceJump Vim - Jump All Editors" />
|
||||||
class="org.acejump.control.AceAction$ActivateOrCycleMode"
|
<action id="AceVimAction_JumpAllEditors_GoToDeclaration" class="org.acejump.action.AceVimAction$JumpAllEditorsGoToDeclaration" text="AceJump Vim - Jump All Editors - Go To Declaration" />
|
||||||
text="Activate / Cycle AceJump Mode">
|
<action id="AceVimAction_JumpForward" class="org.acejump.action.AceVimAction$JumpForward" text="AceJump Vim - Jump Forward" />
|
||||||
<keyboard-shortcut keymap="Mac OS X" first-keystroke="ctrl SEMICOLON"/>
|
<action id="AceVimAction_JumpBackward" class="org.acejump.action.AceVimAction$JumpBackward" text="AceJump Vim - Jump Backward" />
|
||||||
<keyboard-shortcut keymap="Mac OS X 10.5+" first-keystroke="ctrl SEMICOLON"/>
|
<action id="AceVimAction_JumpTillForward" class="org.acejump.action.AceVimAction$JumpTillForward" text="AceJump Vim - Jump Till Forward" />
|
||||||
<keyboard-shortcut keymap="$default" first-keystroke="ctrl SEMICOLON"/>
|
<action id="AceVimAction_JumpTillBackward" class="org.acejump.action.AceVimAction$JumpTillBackward" text="AceJump Vim - Jump Till Backward" />
|
||||||
</action>
|
<action id="AceVimAction_JumpOnLineForward" class="org.acejump.action.AceVimAction$JumpOnLineForward" text="AceJump Vim - Jump On Line Forward" />
|
||||||
<action id="AceWordStartAction"
|
<action id="AceVimAction_JumpOnLineBackward" class="org.acejump.action.AceVimAction$JumpOnLineBackward" text="AceJump Vim - Jump On Line Backward" />
|
||||||
class="org.acejump.control.AceAction$ToggleJumpMode"
|
<action id="AceVimAction_JumpLineIndentsForward" class="org.acejump.action.AceVimAction$JumpLineIndentsForward" text="AceJump Vim - Jump Line Indents Forward" />
|
||||||
text="Start AceJump in Word Start Mode"/>
|
<action id="AceVimAction_JumpLineIndentsBackward" class="org.acejump.action.AceVimAction$JumpLineIndentsBackward" text="AceJump Vim - Jump Line Indents Backward" />
|
||||||
<action id="AceWordEndAction"
|
<action id="AceVimAction_JumpLWordForward" class="org.acejump.action.AceVimAction$JumpLWordForward" text="AceJump Vim - Jump LWord Forward" />
|
||||||
class="org.acejump.control.AceAction$ToggleJumpEndMode"
|
<action id="AceVimAction_JumpUWordForward" class="org.acejump.action.AceVimAction$JumpUWordForward" text="AceJump Vim - Jump UWord Forward" />
|
||||||
text="Start AceJump in Word End Mode"/>
|
<action id="AceVimAction_JumpLWordBackward" class="org.acejump.action.AceVimAction$JumpLWordBackward" text="AceJump Vim - Jump LWord Backward" />
|
||||||
<action id="AceTargetAction"
|
<action id="AceVimAction_JumpUWordBackward" class="org.acejump.action.AceVimAction$JumpUWordBackward" text="AceJump Vim - Jump UWord Backward" />
|
||||||
class="org.acejump.control.AceAction$ToggleSelectWordMode"
|
<action id="AceVimAction_JumpLWordEndForward" class="org.acejump.action.AceVimAction$JumpLWordEndForward" text="AceJump Vim - Jump LWord End Forward" />
|
||||||
text="Start AceJump in Word Select Mode">
|
<action id="AceVimAction_JumpUWordEndForward" class="org.acejump.action.AceVimAction$JumpUWordEndForward" text="AceJump Vim - Jump UWord End Forward" />
|
||||||
<keyboard-shortcut keymap="Mac OS X" first-keystroke="ctrl alt SEMICOLON"/>
|
<action id="AceVimAction_JumpLWordEndBackward" class="org.acejump.action.AceVimAction$JumpLWordEndBackward" text="AceJump Vim - Jump LWord End Backward" />
|
||||||
<keyboard-shortcut keymap="Mac OS X 10.5+" first-keystroke="ctrl alt SEMICOLON"/>
|
<action id="AceVimAction_JumpUWordEndBackward" class="org.acejump.action.AceVimAction$JumpUWordEndBackward" text="AceJump Vim - Jump UWord End Backward" />
|
||||||
<keyboard-shortcut keymap="$default" first-keystroke="ctrl alt SEMICOLON"/>
|
|
||||||
</action>
|
|
||||||
<action id="AceDeclarationAction"
|
|
||||||
class="org.acejump.control.AceAction$ToggleDefinitionMode"
|
|
||||||
text="Start AceJump in Declaration Mode"/>
|
|
||||||
<action id="AceLineAction"
|
|
||||||
class="org.acejump.control.AceAction$ToggleAllLinesMode"
|
|
||||||
text="Start AceJump in All Lines Mode"
|
|
||||||
description="Targets line markers in AceJump">
|
|
||||||
<keyboard-shortcut keymap="Mac OS X" first-keystroke="ctrl shift SEMICOLON"/>
|
|
||||||
<keyboard-shortcut keymap="Mac OS X 10.5+" first-keystroke="ctrl shift SEMICOLON"/>
|
|
||||||
<keyboard-shortcut keymap="$default" first-keystroke="ctrl shift SEMICOLON"/>
|
|
||||||
</action>
|
|
||||||
<action id="AceWordAction"
|
|
||||||
class="org.acejump.control.AceAction$ToggleAllWordsMode"
|
|
||||||
text="Start AceJump in All Words Mode"/>
|
|
||||||
<action id="AceWordForwardAction"
|
|
||||||
class="org.acejump.control.AceAction$ToggleAllWordsForwardMode"
|
|
||||||
text="Start in All Words After Caret Mode"/>
|
|
||||||
<action id="AceWordBackwardsAction"
|
|
||||||
class="org.acejump.control.AceAction$ToggleAllWordsBackwardsMode"
|
|
||||||
text="Start in All Words Before Caret Mode"/>
|
|
||||||
<action id="AceActOnHighlightedWords"
|
|
||||||
class="org.acejump.control.AceAction$ActOnHighlightedWords"
|
|
||||||
text="Act on Highlighted Words"/>
|
|
||||||
</actions>
|
</actions>
|
||||||
</idea-plugin>
|
</idea-plugin>
|
||||||
|
@@ -1,231 +0,0 @@
|
|||||||
import com.intellij.openapi.actionSystem.AnAction
|
|
||||||
import com.intellij.openapi.actionSystem.IdeActions.*
|
|
||||||
import com.intellij.openapi.components.ServiceManager
|
|
||||||
import com.intellij.openapi.fileTypes.PlainTextFileType
|
|
||||||
import com.intellij.psi.PsiFile
|
|
||||||
import com.intellij.testFramework.fixtures.BasePlatformTestCase
|
|
||||||
import com.intellij.util.ui.UIUtil
|
|
||||||
import org.acejump.config.AceConfig
|
|
||||||
import org.acejump.control.*
|
|
||||||
import org.acejump.label.Tagger
|
|
||||||
import org.acejump.view.Canvas
|
|
||||||
import kotlin.random.Random
|
|
||||||
import kotlin.system.measureTimeMillis
|
|
||||||
import java.io.File
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Functional test cases and end-to-end performance tests.
|
|
||||||
*
|
|
||||||
* TODO: Add more structure to test cases, use test resources to define files.
|
|
||||||
*/
|
|
||||||
|
|
||||||
class AceTest: BasePlatformTestCase() {
|
|
||||||
fun `test that scanner finds all occurrences of single character`() =
|
|
||||||
assertEquals("test test test".search("t"), setOf(0, 3, 5, 8, 10, 13))
|
|
||||||
|
|
||||||
fun `test empty results for an absent query`() =
|
|
||||||
assertEmpty("test test test".search("best"))
|
|
||||||
|
|
||||||
fun `test sticky results on a query with extra characters`() =
|
|
||||||
assertEquals("test test test".search("testz"), setOf(0, 5, 10))
|
|
||||||
|
|
||||||
fun `test a query inside text with some variations`() =
|
|
||||||
assertEquals("abcd dabc cdab".search("cd"), setOf(2, 10))
|
|
||||||
|
|
||||||
fun `test a query containing a space character`() =
|
|
||||||
assertEquals("abcd dabc cdab".search("cd "), setOf(2))
|
|
||||||
|
|
||||||
fun `test a query containing a { character`() =
|
|
||||||
assertEquals("abcd{dabc cdab".search("cd{"), setOf(2))
|
|
||||||
|
|
||||||
fun `test that jumping to first occurrence succeeds`() {
|
|
||||||
"<caret>testing 1234".search("1")
|
|
||||||
|
|
||||||
takeAction(ACTION_EDITOR_ENTER)
|
|
||||||
|
|
||||||
myFixture.checkResult("testing <caret>1234")
|
|
||||||
}
|
|
||||||
|
|
||||||
fun `test that jumping to second occurrence succeeds`() {
|
|
||||||
"<caret>testing 1234".search("ti")
|
|
||||||
|
|
||||||
takeAction(ACTION_EDITOR_ENTER)
|
|
||||||
|
|
||||||
myFixture.checkResult("tes<caret>ting 1234")
|
|
||||||
}
|
|
||||||
|
|
||||||
fun `test that jumping to previous occurrence succeeds`() {
|
|
||||||
"te<caret>sting 1234".search("t")
|
|
||||||
|
|
||||||
takeAction(ACTION_EDITOR_START_NEW_LINE)
|
|
||||||
|
|
||||||
myFixture.checkResult("<caret>testing 1234")
|
|
||||||
}
|
|
||||||
|
|
||||||
fun `test tag selection`() {
|
|
||||||
"<caret>testing 1234".search("g")
|
|
||||||
|
|
||||||
typeAndWaitForResults(Canvas.jumpLocations.first().tag!!)
|
|
||||||
|
|
||||||
myFixture.checkResult("testin<caret>g 1234")
|
|
||||||
}
|
|
||||||
|
|
||||||
fun `test shift selection`() {
|
|
||||||
"<caret>testing 1234".search("4")
|
|
||||||
|
|
||||||
typeAndWaitForResults(Canvas.jumpLocations.first().tag!!.toUpperCase())
|
|
||||||
|
|
||||||
myFixture.checkResult("<selection>testing 123<caret></selection>4")
|
|
||||||
}
|
|
||||||
|
|
||||||
fun `test words before caret action`() {
|
|
||||||
makeEditor("test words <caret> before caret is two")
|
|
||||||
|
|
||||||
takeAction(AceWordBackwardsAction())
|
|
||||||
|
|
||||||
assertEquals(2, Tagger.markers.size)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun `test words after caret action`() {
|
|
||||||
makeEditor("test words <caret> after caret is four")
|
|
||||||
|
|
||||||
takeAction(AceWordForwardAction())
|
|
||||||
|
|
||||||
assertEquals(4, Tagger.markers.size)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun `test word mode`() {
|
|
||||||
makeEditor("test word action")
|
|
||||||
|
|
||||||
takeAction(AceWordAction())
|
|
||||||
|
|
||||||
assertEquals(3, Tagger.markers.size)
|
|
||||||
|
|
||||||
typeAndWaitForResults(Canvas.jumpLocations.toList()[1].tag!!)
|
|
||||||
|
|
||||||
myFixture.checkResult("test <caret>word action")
|
|
||||||
}
|
|
||||||
|
|
||||||
fun `test target mode`() {
|
|
||||||
"<caret>test target action".search("target")
|
|
||||||
|
|
||||||
takeAction(AceTargetAction())
|
|
||||||
typeAndWaitForResults(Canvas.jumpLocations.first().tag!!)
|
|
||||||
|
|
||||||
myFixture.checkResult("test <selection>target<caret></selection> action")
|
|
||||||
}
|
|
||||||
|
|
||||||
fun `test line mode`() {
|
|
||||||
makeEditor(" test\n three\n lines")
|
|
||||||
|
|
||||||
takeAction(AceLineAction())
|
|
||||||
|
|
||||||
assertEquals(9, Tagger.markers.size)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun `test pinyin selection`() {
|
|
||||||
getSettings().supportPinyin = true
|
|
||||||
|
|
||||||
"test 拼音 selection".search("py")
|
|
||||||
|
|
||||||
takeAction(AceTargetAction())
|
|
||||||
typeAndWaitForResults(Canvas.jumpLocations.first().tag!!)
|
|
||||||
|
|
||||||
myFixture.checkResult("test <selection>拼音<caret></selection> selection")
|
|
||||||
}
|
|
||||||
|
|
||||||
fun `test tag latency`(editorText: String) {
|
|
||||||
var time = 0L
|
|
||||||
|
|
||||||
editorText.toCharArray().distinct().filter { !it.isWhitespace() }
|
|
||||||
.forEach { query ->
|
|
||||||
repeat(10) {
|
|
||||||
makeEditor(editorText)
|
|
||||||
|
|
||||||
myFixture.testAction(AceAction())
|
|
||||||
time += measureTimeMillis { typeAndWaitForResults("$query") }
|
|
||||||
assert(Tagger.markers.isNotEmpty()) { "Should be tagged: $query" }
|
|
||||||
resetEditor()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
println("Average time to tag results: ${time / 100}ms")
|
|
||||||
}
|
|
||||||
|
|
||||||
fun `test random text latency`() =
|
|
||||||
`test tag latency`(
|
|
||||||
generateSequence {
|
|
||||||
generateSequence {
|
|
||||||
generateSequence {
|
|
||||||
('a'..'z').random(Random(0))
|
|
||||||
}.take(5).joinToString("")
|
|
||||||
}.take(20).joinToString(" ")
|
|
||||||
}.take(100).joinToString("\n")
|
|
||||||
)
|
|
||||||
|
|
||||||
fun `test lorem ipsum latency`() =
|
|
||||||
`test tag latency`(
|
|
||||||
File(
|
|
||||||
javaClass.classLoader.getResource("lipsum.txt")!!.file
|
|
||||||
).readText()
|
|
||||||
)
|
|
||||||
|
|
||||||
fun getSettings() =
|
|
||||||
ServiceManager.getService(AceConfig::class.java).aceSettings
|
|
||||||
|
|
||||||
// Enforces the results are available in less than 100ms
|
|
||||||
private fun String.search(query: String) =
|
|
||||||
myFixture.run {
|
|
||||||
maybeWarmUp(this@search, query)
|
|
||||||
val queryTime = measureTimeMillis { this@search.executeQuery(query) }
|
|
||||||
// assert(queryTime < 100) { "Query exceeded time limit! ($queryTime ms)" }
|
|
||||||
this@search.replace(Regex("<[^>]*>"), "").assertCorrectNumberOfTags(query)
|
|
||||||
editor.markupModel.allHighlighters.map { it.startOffset }.toSet()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensures that the correct number of locations are tagged
|
|
||||||
private fun String.assertCorrectNumberOfTags(query: String) =
|
|
||||||
assertEquals(split(query.fold("") { prefix, char ->
|
|
||||||
if ((prefix + char) in this) prefix + char else return
|
|
||||||
}).size - 1, myFixture.editor.markupModel.allHighlighters.size)
|
|
||||||
|
|
||||||
private var shouldWarmup = true
|
|
||||||
|
|
||||||
// Should be run exactly once to warm up the JVM
|
|
||||||
private fun maybeWarmUp(text: String, query: String) {
|
|
||||||
if (shouldWarmup) {
|
|
||||||
text.executeQuery(query)
|
|
||||||
takeAction(ACTION_EDITOR_ESCAPE)
|
|
||||||
UIUtil.dispatchAllInvocationEvents()
|
|
||||||
// Now the JVM is warm, never run this method again
|
|
||||||
shouldWarmup = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun makeEditor(contents: String): PsiFile =
|
|
||||||
myFixture.configureByText(PlainTextFileType.INSTANCE, contents)
|
|
||||||
|
|
||||||
fun takeAction(action: String) = myFixture.performEditorAction(action)
|
|
||||||
|
|
||||||
fun takeAction(action: AnAction) = myFixture.testAction(action)
|
|
||||||
|
|
||||||
// Just does a query without enforcing any time limit
|
|
||||||
private fun String.executeQuery(query: String) {
|
|
||||||
myFixture.run {
|
|
||||||
makeEditor(this@executeQuery)
|
|
||||||
testAction(AceAction())
|
|
||||||
typeAndWaitForResults(query)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun resetEditor() {
|
|
||||||
takeAction(ACTION_EDITOR_ESCAPE)
|
|
||||||
UIUtil.dispatchAllInvocationEvents()
|
|
||||||
assertEmpty(myFixture.editor.markupModel.allHighlighters)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun tearDown() = resetEditor().also { super.tearDown() }
|
|
||||||
|
|
||||||
private fun typeAndWaitForResults(string: String) =
|
|
||||||
myFixture.type(string).also { UIUtil.dispatchAllInvocationEvents() }
|
|
||||||
}
|
|
44
src/test/kotlin/LatencyTest.kt
Normal file
44
src/test/kotlin/LatencyTest.kt
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
|
||||||
|
import org.acejump.action.AceVimAction
|
||||||
|
import org.acejump.test.util.BaseTest
|
||||||
|
import java.io.File
|
||||||
|
import kotlin.random.Random
|
||||||
|
import kotlin.system.measureTimeMillis
|
||||||
|
|
||||||
|
class LatencyTest : BaseTest() {
|
||||||
|
|
||||||
|
private fun `test tag latency`(editorText: String) {
|
||||||
|
val chars = editorText.toCharArray().distinct().filter { !it.isWhitespace() }
|
||||||
|
val avg = averageTimeWithWarmup(warmupRuns = 10, timedRuns = 10) {
|
||||||
|
var time = 0L
|
||||||
|
|
||||||
|
for (query in chars) {
|
||||||
|
makeEditor(editorText)
|
||||||
|
myFixture.testAction(AceVimAction.JumpAllEditors())
|
||||||
|
time += measureTimeMillis { typeAndWaitForResults("$query") }
|
||||||
|
// TODO assert(Tagger.markers.isNotEmpty()) { "Should be tagged: $query" }
|
||||||
|
resetEditor()
|
||||||
|
}
|
||||||
|
|
||||||
|
time
|
||||||
|
}
|
||||||
|
|
||||||
|
println("Average time to tag results: ${String.format("%.1f", avg.toDouble() / chars.size)} ms")
|
||||||
|
}
|
||||||
|
|
||||||
|
fun `test random text latency`() = `test tag latency`(
|
||||||
|
generateSequence {
|
||||||
|
generateSequence {
|
||||||
|
generateSequence {
|
||||||
|
('a'..'z').random(Random(0))
|
||||||
|
}.take(5).joinToString("")
|
||||||
|
}.take(20).joinToString(" ")
|
||||||
|
}.take(100).joinToString("\n")
|
||||||
|
)
|
||||||
|
|
||||||
|
fun `test lorem ipsum latency`() = `test tag latency`(
|
||||||
|
File(
|
||||||
|
javaClass.classLoader.getResource("lipsum.txt")!!.file
|
||||||
|
).readText()
|
||||||
|
)
|
||||||
|
}
|
78
src/test/kotlin/org/acejump/test/util/BaseTest.kt
Normal file
78
src/test/kotlin/org/acejump/test/util/BaseTest.kt
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
package org.acejump.test.util
|
||||||
|
|
||||||
|
import com.intellij.openapi.actionSystem.AnAction
|
||||||
|
import com.intellij.openapi.actionSystem.IdeActions
|
||||||
|
import com.intellij.openapi.editor.impl.EditorImpl
|
||||||
|
import com.intellij.openapi.fileTypes.PlainTextFileType
|
||||||
|
import com.intellij.psi.PsiFile
|
||||||
|
import com.intellij.testFramework.fixtures.BasePlatformTestCase
|
||||||
|
import com.intellij.util.ui.UIUtil
|
||||||
|
import org.acejump.action.AceVimAction
|
||||||
|
import org.acejump.session.SessionManager
|
||||||
|
|
||||||
|
abstract class BaseTest : BasePlatformTestCase() {
|
||||||
|
companion object {
|
||||||
|
inline fun averageTimeWithWarmup(warmupRuns: Int, timedRuns: Int, action: () -> Long): Long {
|
||||||
|
repeat(warmupRuns) {
|
||||||
|
action()
|
||||||
|
}
|
||||||
|
|
||||||
|
var time = 0L
|
||||||
|
|
||||||
|
repeat(timedRuns) {
|
||||||
|
time += action()
|
||||||
|
}
|
||||||
|
|
||||||
|
return time / timedRuns
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected val session
|
||||||
|
get() = SessionManager[myFixture.editor]!!
|
||||||
|
|
||||||
|
override fun tearDown() {
|
||||||
|
resetEditor()
|
||||||
|
super.tearDown()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun takeAction(action: String) = myFixture.performEditorAction(action)
|
||||||
|
fun takeAction(action: AnAction) = myFixture.testAction(action)
|
||||||
|
|
||||||
|
fun makeEditor(contents: String): PsiFile {
|
||||||
|
val file = myFixture.configureByText(PlainTextFileType.INSTANCE, contents)
|
||||||
|
(myFixture.editor as EditorImpl).scrollPane.viewport.setSize(1000, 100)
|
||||||
|
return file
|
||||||
|
}
|
||||||
|
|
||||||
|
fun resetEditor() {
|
||||||
|
takeAction(IdeActions.ACTION_EDITOR_ESCAPE)
|
||||||
|
UIUtil.dispatchAllInvocationEvents()
|
||||||
|
assertEmpty(myFixture.editor.markupModel.allHighlighters)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun typeAndWaitForResults(string: String) {
|
||||||
|
myFixture.type(string)
|
||||||
|
UIUtil.dispatchAllInvocationEvents()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun String.executeQuery(query: String) {
|
||||||
|
myFixture.run {
|
||||||
|
makeEditor(this@executeQuery)
|
||||||
|
testAction(AceVimAction.JumpAllEditors())
|
||||||
|
typeAndWaitForResults(query)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun String.search(query: String): Set<Int> {
|
||||||
|
this@search.executeQuery(query)
|
||||||
|
this@search.replace(Regex("<[^>]*>"), "").assertCorrectNumberOfTags(query)
|
||||||
|
return myFixture.editor.markupModel.allHighlighters.map { it.startOffset }.toSet()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun String.assertCorrectNumberOfTags(query: String) {
|
||||||
|
assertEquals(split(query.fold("") { prefix, char ->
|
||||||
|
if ((prefix + char) in this) prefix + char else return
|
||||||
|
}).size - 1, myFixture.editor.markupModel.allHighlighters.size)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user