mirror of
https://github.com/chylex/IntelliJ-IdeaVim.git
synced 2025-03-06 00:32:52 +01:00
Convert common package to kt
This commit is contained in:
parent
622de851fe
commit
eef1b25e9f
src/com/maddyhome/idea/vim
@ -1,76 +1,49 @@
|
||||
package com.maddyhome.idea.vim.common;
|
||||
package com.maddyhome.idea.vim.common
|
||||
|
||||
import com.intellij.application.options.CodeStyle;
|
||||
import com.intellij.openapi.actionSystem.DataContext;
|
||||
import com.intellij.openapi.actionSystem.PlatformDataKeys;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
|
||||
import com.intellij.application.options.CodeStyle
|
||||
import com.intellij.openapi.actionSystem.DataContext
|
||||
import com.intellij.openapi.actionSystem.PlatformDataKeys
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.codeStyle.CommonCodeStyleSettings.IndentOptions
|
||||
|
||||
public class IndentConfig {
|
||||
class IndentConfig private constructor(indentOptions: IndentOptions) {
|
||||
private val indentSize = indentOptions.INDENT_SIZE
|
||||
private val tabSize = indentOptions.TAB_SIZE
|
||||
private val isUseTabs = indentOptions.USE_TAB_CHARACTER
|
||||
|
||||
private final int indentSize;
|
||||
private final int tabSize;
|
||||
private final boolean useTabs;
|
||||
fun getTotalIndent(count: Int): Int = indentSize * count
|
||||
|
||||
private IndentConfig(CommonCodeStyleSettings.IndentOptions indentOptions) {
|
||||
this.indentSize = indentOptions.INDENT_SIZE;
|
||||
this.tabSize = indentOptions.TAB_SIZE;
|
||||
this.useTabs = indentOptions.USE_TAB_CHARACTER;
|
||||
}
|
||||
fun createIndentByCount(count: Int): String = createIndentBySize(getTotalIndent(count))
|
||||
|
||||
public static IndentConfig create(Editor editor) {
|
||||
return create(editor, editor.getProject());
|
||||
}
|
||||
|
||||
public static IndentConfig create(Editor editor, DataContext context) {
|
||||
return create(editor, PlatformDataKeys.PROJECT.getData(context));
|
||||
}
|
||||
|
||||
public static IndentConfig create(Editor editor, Project project) {
|
||||
CommonCodeStyleSettings.IndentOptions indentOptions;
|
||||
|
||||
if(project != null) {
|
||||
indentOptions = CodeStyle.getIndentOptions(project, editor.getDocument());
|
||||
fun createIndentBySize(size: Int): String {
|
||||
val tabCount: Int
|
||||
val spaceCount: Int
|
||||
if (isUseTabs) {
|
||||
tabCount = size / tabSize
|
||||
spaceCount = size % tabSize
|
||||
} else {
|
||||
indentOptions = CodeStyle.getDefaultSettings().getIndentOptions();
|
||||
tabCount = 0
|
||||
spaceCount = size
|
||||
}
|
||||
return "\t".repeat(tabCount) + " ".repeat(spaceCount)
|
||||
}
|
||||
|
||||
companion object {
|
||||
@JvmStatic
|
||||
fun create(editor: Editor, context: DataContext): IndentConfig {
|
||||
return create(editor, PlatformDataKeys.PROJECT.getData(context))
|
||||
}
|
||||
|
||||
return new IndentConfig(indentOptions);
|
||||
}
|
||||
|
||||
public int getIndentSize() {
|
||||
return indentSize;
|
||||
}
|
||||
|
||||
public int getTabSize() {
|
||||
return tabSize;
|
||||
}
|
||||
|
||||
public boolean isUseTabs() {
|
||||
return useTabs;
|
||||
}
|
||||
|
||||
public int getTotalIndent(int count) {
|
||||
return indentSize * count;
|
||||
}
|
||||
|
||||
public String createIndentByCount(int count) {
|
||||
return createIndentBySize(getTotalIndent(count));
|
||||
}
|
||||
|
||||
public String createIndentBySize(int size) {
|
||||
final int tabCount;
|
||||
final int spaceCount;
|
||||
if (useTabs) {
|
||||
tabCount = size / tabSize;
|
||||
spaceCount = size % tabSize;
|
||||
@JvmStatic
|
||||
@JvmOverloads
|
||||
fun create(editor: Editor, project: Project? = editor.project): IndentConfig {
|
||||
val indentOptions = if (project != null) {
|
||||
CodeStyle.getIndentOptions(project, editor.document)
|
||||
} else {
|
||||
CodeStyle.getDefaultSettings().indentOptions
|
||||
}
|
||||
return IndentConfig(indentOptions)
|
||||
}
|
||||
else {
|
||||
tabCount = 0;
|
||||
spaceCount = size;
|
||||
}
|
||||
return StringUtil.repeat("\t", tabCount) + StringUtil.repeat(" ", spaceCount);
|
||||
}
|
||||
}
|
||||
}
|
@ -15,107 +15,61 @@
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.maddyhome.idea.vim.common
|
||||
|
||||
package com.maddyhome.idea.vim.common;
|
||||
import com.intellij.codeInsight.editorActions.TextBlockTransferableData
|
||||
import com.maddyhome.idea.vim.command.SelectionType
|
||||
import com.maddyhome.idea.vim.helper.StringHelper
|
||||
import java.awt.event.KeyEvent
|
||||
import java.util.*
|
||||
import javax.swing.KeyStroke
|
||||
|
||||
import com.intellij.codeInsight.editorActions.TextBlockTransferableData;
|
||||
import com.maddyhome.idea.vim.command.SelectionType;
|
||||
import com.maddyhome.idea.vim.helper.StringHelper;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
class Register {
|
||||
var name: Char
|
||||
val type: SelectionType
|
||||
val keys: MutableList<KeyStroke>
|
||||
val transferableData: MutableList<out TextBlockTransferableData>
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Represents a register.
|
||||
*/
|
||||
public class Register {
|
||||
private char name;
|
||||
@NotNull private final SelectionType type;
|
||||
@NotNull private final List<KeyStroke> keys;
|
||||
@NotNull private List<? extends TextBlockTransferableData> transferableData = new ArrayList<>();
|
||||
|
||||
public Register(char name, @NotNull SelectionType type, @NotNull List<KeyStroke> keys) {
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
this.keys = keys;
|
||||
constructor(name: Char, type: SelectionType, keys: MutableList<KeyStroke>) {
|
||||
this.name = name
|
||||
this.type = type
|
||||
this.keys = keys
|
||||
this.transferableData = mutableListOf()
|
||||
}
|
||||
|
||||
public Register(char name, @NotNull SelectionType type, @NotNull String text, @NotNull List<? extends TextBlockTransferableData> transferableData) {
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
this.keys = StringHelper.stringToKeys(text);
|
||||
this.transferableData = transferableData;
|
||||
constructor(name: Char, type: SelectionType, text: String, transferableData: MutableList<out TextBlockTransferableData>) {
|
||||
this.name = name
|
||||
this.type = type
|
||||
this.keys = StringHelper.stringToKeys(text)
|
||||
this.transferableData = transferableData
|
||||
}
|
||||
|
||||
public void rename(char name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the name the register is assigned to.
|
||||
*/
|
||||
public char getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public List<? extends TextBlockTransferableData> getTransferableData() {
|
||||
return transferableData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the register type.
|
||||
*/
|
||||
@NotNull
|
||||
public SelectionType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the text in the register.
|
||||
*/
|
||||
@Nullable
|
||||
public String getText() {
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
for (KeyStroke key : keys) {
|
||||
final char c = key.getKeyChar();
|
||||
if (c == KeyEvent.CHAR_UNDEFINED) {
|
||||
return null;
|
||||
val text: String?
|
||||
get() {
|
||||
val builder = StringBuilder()
|
||||
for (key in keys) {
|
||||
val c = key.keyChar
|
||||
if (c == KeyEvent.CHAR_UNDEFINED) {
|
||||
return null
|
||||
}
|
||||
builder.append(c)
|
||||
}
|
||||
builder.append(c);
|
||||
return builder.toString()
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the sequence of keys in the register.
|
||||
*/
|
||||
@NotNull
|
||||
public List<KeyStroke> getKeys() {
|
||||
return keys;
|
||||
}
|
||||
|
||||
/**
|
||||
* Append the supplied text to any existing text.
|
||||
*/
|
||||
public void addTextAndResetTransferableData(@NotNull String text) {
|
||||
addKeys(StringHelper.stringToKeys(text));
|
||||
transferableData.clear();
|
||||
fun addTextAndResetTransferableData(text: String) {
|
||||
addKeys(StringHelper.stringToKeys(text))
|
||||
transferableData.clear()
|
||||
}
|
||||
|
||||
public void addKeys(@NotNull List<KeyStroke> keys) {
|
||||
this.keys.addAll(keys);
|
||||
fun addKeys(keys: List<KeyStroke>) {
|
||||
this.keys.addAll(keys)
|
||||
}
|
||||
|
||||
public static class KeySorter implements Comparator<Register> {
|
||||
@Override
|
||||
public int compare(@NotNull Register o1, @NotNull Register o2) {
|
||||
return Character.compare(o1.name, o2.name);
|
||||
}
|
||||
object KeySorter : Comparator<Register> {
|
||||
override fun compare(o1: Register, o2: Register): Int = o1.name.compareTo(o2.name)
|
||||
}
|
||||
}
|
||||
}
|
@ -15,119 +15,93 @@
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.maddyhome.idea.vim.common
|
||||
|
||||
package com.maddyhome.idea.vim.common;
|
||||
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Contract
|
||||
import kotlin.math.max
|
||||
import kotlin.math.min
|
||||
|
||||
/**
|
||||
* Please prefer {@link com.maddyhome.idea.vim.group.visual.VimSelection} for visual selection
|
||||
* Please prefer [com.maddyhome.idea.vim.group.visual.VimSelection] for visual selection
|
||||
*/
|
||||
public class TextRange {
|
||||
@Contract(pure = true)
|
||||
public TextRange(int start, int end) {
|
||||
this(new int[]{start}, new int[]{end});
|
||||
}
|
||||
class TextRange(val startOffsets: IntArray, val endOffsets: IntArray) {
|
||||
constructor(start: Int, end: Int) : this(intArrayOf(start), intArrayOf(end))
|
||||
|
||||
@Contract(pure = true)
|
||||
public TextRange(int[] starts, int[] ends) {
|
||||
this.starts = starts;
|
||||
this.ends = ends;
|
||||
}
|
||||
val isMultiple
|
||||
get() = startOffsets.size > 1
|
||||
|
||||
public boolean isMultiple() {
|
||||
return starts != null && starts.length > 1;
|
||||
}
|
||||
|
||||
public int getMaxLength() {
|
||||
int max = 0;
|
||||
for (int i = 0; i < size(); i++) {
|
||||
max = Math.max(max, getEndOffsets()[i] - getStartOffsets()[i]);
|
||||
val maxLength: Int
|
||||
get() {
|
||||
var max = 0
|
||||
for (i in 0 until size()) {
|
||||
max = max(max, endOffsets[i] - startOffsets[i])
|
||||
}
|
||||
return max
|
||||
}
|
||||
|
||||
return max;
|
||||
}
|
||||
|
||||
public int getSelectionCount() {
|
||||
int res = 0;
|
||||
for (int i = 0; i < size(); i++) {
|
||||
res += getEndOffsets()[i] - getStartOffsets()[i];
|
||||
val selectionCount: Int
|
||||
get() {
|
||||
var res = 0
|
||||
for (i in 0 until size()) {
|
||||
res += endOffsets[i] - startOffsets[i]
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
return res;
|
||||
fun size(): Int = startOffsets.size
|
||||
|
||||
val startOffset: Int
|
||||
get() = startOffsets.first()
|
||||
|
||||
val endOffset: Int
|
||||
get() = endOffsets.last()
|
||||
|
||||
fun normalize(): TextRange {
|
||||
normalizeIndex(0)
|
||||
return this
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return starts.length;
|
||||
}
|
||||
|
||||
public int getStartOffset() {
|
||||
return starts[0];
|
||||
}
|
||||
|
||||
public int getEndOffset() {
|
||||
return ends[ends.length - 1];
|
||||
}
|
||||
|
||||
public int[] getStartOffsets() {
|
||||
return starts;
|
||||
}
|
||||
|
||||
public int[] getEndOffsets() {
|
||||
return ends;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public TextRange normalize() {
|
||||
normalizeIndex(0);
|
||||
return this;
|
||||
}
|
||||
|
||||
private void normalizeIndex(final int index) {
|
||||
if (index < size() && ends[index] < starts[index]) {
|
||||
int t = starts[index];
|
||||
starts[index] = ends[index];
|
||||
ends[index] = t;
|
||||
private fun normalizeIndex(index: Int) {
|
||||
if (index < size() && endOffsets[index] < startOffsets[index]) {
|
||||
val t = startOffsets[index]
|
||||
startOffsets[index] = endOffsets[index]
|
||||
endOffsets[index] = t
|
||||
}
|
||||
}
|
||||
|
||||
@Contract(mutates = "this")
|
||||
public boolean normalize(final int fileSize) {
|
||||
for (int i = 0; i < size(); i++) {
|
||||
normalizeIndex(i);
|
||||
starts[i] = Math.max(0, Math.min(starts[i], fileSize));
|
||||
if (starts[i] == fileSize && fileSize != 0) {
|
||||
return false;
|
||||
fun normalize(fileSize: Int): Boolean {
|
||||
for (i in 0 until size()) {
|
||||
normalizeIndex(i)
|
||||
startOffsets[i] = max(0, min(startOffsets[i], fileSize))
|
||||
if (startOffsets[i] == fileSize && fileSize != 0) {
|
||||
return false
|
||||
}
|
||||
ends[i] = Math.max(0, Math.min(ends[i], fileSize));
|
||||
endOffsets[i] = max(0, min(endOffsets[i], fileSize))
|
||||
}
|
||||
return true;
|
||||
return true
|
||||
}
|
||||
|
||||
public boolean contains(final int offset) {
|
||||
if (isMultiple()) {
|
||||
return false;
|
||||
}
|
||||
return this.getStartOffset() <= offset && offset < this.getEndOffset();
|
||||
}
|
||||
operator fun contains(offset: Int): Boolean = if (isMultiple) false else offset in startOffset until endOffset
|
||||
|
||||
@NotNull
|
||||
public String toString() {
|
||||
final StringBuilder sb = new StringBuilder();
|
||||
sb.append("TextRange");
|
||||
sb.append("{starts=").append(starts == null ? "null" : "");
|
||||
for (int i = 0; starts != null && i < starts.length; ++i) {
|
||||
sb.append(i == 0 ? "" : ", ").append(starts[i]);
|
||||
}
|
||||
sb.append(", ends=").append(ends == null ? "null" : "");
|
||||
for (int i = 0; ends != null && i < ends.length; ++i) {
|
||||
sb.append(i == 0 ? "" : ", ").append(ends[i]);
|
||||
}
|
||||
sb.append('}');
|
||||
return sb.toString();
|
||||
}
|
||||
override fun toString(): String {
|
||||
val sb = StringBuilder()
|
||||
sb.append("TextRange")
|
||||
sb.append("{starts=")
|
||||
|
||||
private final int[] starts;
|
||||
private final int[] ends;
|
||||
}
|
||||
var i = 0
|
||||
while (i < startOffsets.size) {
|
||||
sb.append(if (i == 0) "" else ", ").append(startOffsets[i])
|
||||
++i
|
||||
}
|
||||
|
||||
sb.append(", ends=")
|
||||
i = 0
|
||||
while (i < endOffsets.size) {
|
||||
sb.append(if (i == 0) "" else ", ").append(endOffsets[i])
|
||||
++i
|
||||
}
|
||||
sb.append('}')
|
||||
return sb.toString()
|
||||
}
|
||||
}
|
@ -221,7 +221,7 @@ public class RegisterGroup {
|
||||
for (char d = '8'; d >= '1'; d--) {
|
||||
Register t = registers.get(d);
|
||||
if (t != null) {
|
||||
t.rename((char)(d + 1));
|
||||
t.setName((char)(d + 1));
|
||||
registers.put((char)(d + 1), t);
|
||||
}
|
||||
}
|
||||
@ -365,7 +365,7 @@ public class RegisterGroup {
|
||||
res.add(register);
|
||||
}
|
||||
}
|
||||
res.sort(new Register.KeySorter());
|
||||
res.sort(Register.KeySorter.INSTANCE);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user