Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved indentation system #168

Merged
merged 1 commit into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public void addDefaultKeyBindings() {

addKeyBinding("ENTER", INSERT_BREAK);
addKeyBinding("TAB", INSERT_TAB);
addKeyBinding("S+TAB", DELETE_TAB);

addKeyBinding("INSERT", OVERWRITE);
addKeyBinding("C+\\", TOGGLE_RECT);
Expand Down
90 changes: 89 additions & 1 deletion src/rars/venus/editors/jeditsyntax/InputHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public abstract class InputHandler extends KeyAdapter {
public static final ActionListener SELECT_DOC_END = new document_end(true);
public static final ActionListener INSERT_BREAK = new insert_break();
public static final ActionListener INSERT_TAB = new insert_tab();
public static final ActionListener DELETE_TAB = new delete_tab();
public static final ActionListener HOME = new home(false);
public static final ActionListener DOCUMENT_HOME = new document_home(false);
public static final ActionListener SELECT_HOME = new home(true);
Expand Down Expand Up @@ -100,6 +101,7 @@ public abstract class InputHandler extends KeyAdapter {
actions.put("select-doc-end", SELECT_DOC_END);
actions.put("insert-break", INSERT_BREAK);
actions.put("insert-tab", INSERT_TAB);
actions.put("delete-tab", DELETE_TAB);
actions.put("home", HOME);
actions.put("select-home", SELECT_HOME);
actions.put("document-home", DOCUMENT_HOME);
Expand Down Expand Up @@ -672,7 +674,93 @@ public void actionPerformed(ActionEvent evt) {
return;
}

textArea.overwriteSetSelectedText("\t");
int startOffset = textArea.getSelectionStart();
int startLine = textArea.getSelectionStartLine();
int startLineOffset = textArea.getLineStartOffset(startLine);
int endOffset = textArea.getSelectionEnd();
int endLine = textArea.getSelectionEndLine();
int endLineOffset = textArea.getLineEndOffset(endLine);

if (startLineOffset != endLineOffset && startLine != endLine) {
String text = textArea.getText();
String selected = text.substring(startLineOffset, endLineOffset - 1);
String prefixed = TextUtilities.addLinePrefixes(selected, "\t");

try {
textArea.document.replace(startLineOffset, endLineOffset - startLineOffset - 1, prefixed, null);
} catch (BadLocationException bl) {
bl.printStackTrace();
}
textArea.select(startOffset + 1, endOffset + (prefixed.length() - selected.length()));
} else {
textArea.overwriteSetSelectedText("\t");
}
}
}

public static class delete_tab implements ActionListener {
public void actionPerformed(ActionEvent evt) {
JEditTextArea textArea = getTextArea(evt);

if (!textArea.isEditable()) {
textArea.getToolkit().beep();
return;
}

int startOffset = textArea.getSelectionStart();
int startLine = textArea.getSelectionStartLine();
int startLineOffset = textArea.getLineStartOffset(startLine);
int endOffset = textArea.getSelectionEnd();
int endLine = textArea.getSelectionEndLine();
int endLineOffset = textArea.getLineEndOffset(endLine);

if (startOffset != endOffset) {
String text = textArea.getText();
String selected = text.substring(startLineOffset, endLineOffset - 1);
String stripped = TextUtilities.deleteLinePrefixes(selected, "\t");

if (selected.equals(stripped)) {
textArea.getToolkit().beep();
return;
}

try {
textArea.document.replace(startLineOffset, endLineOffset - startLineOffset - 1, stripped, null);
} catch (BadLocationException bl) {
bl.printStackTrace();
}

textArea.select(startOffset - 1 < 0 ? 0 : startOffset - 1, endOffset + (stripped.length() - selected.length()));
} else {
int caretOffset = textArea.getCaretPosition();
int caretLine = textArea.getCaretLine();
int caretLineOffset = textArea.getLineStartOffset(caretLine);

if (caretOffset == 0) {
textArea.getToolkit().beep();
return;
}
try {
String lineText = textArea.getLineText(caretLine);
int tabLineIndex = -1;
for (int i = 0; i < lineText.length(); i++) {
if (lineText.charAt(i) != '\t') {
break;
}
tabLineIndex = i;
}

if (tabLineIndex == -1) {
textArea.getToolkit().beep();
return;
}

int tabIndex = caretLineOffset + tabLineIndex;
textArea.getDocument().remove(tabIndex, 1);
} catch (BadLocationException bl) {
bl.printStackTrace();
}
}
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/rars/venus/editors/jeditsyntax/JEditTextArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,17 @@ public JEditTextArea(TextAreaDefaults defaults, JComponent lineNumbers) {
caretTimer.setDelay(caretBlinkRate);

// Intercept keystrokes before focus manager gets them. If in editing window,
// pass TAB keystrokes on to the key processor instead of letting focus
// pass (SHIFT) TAB keystrokes on to the key processor instead of letting focus
// manager use them for focus traversal.
// One can also accomplish this using: setFocusTraversalKeysEnabled(false);
// but that seems heavy-handed.
// DPS 12May2010
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(
new KeyEventDispatcher() {
public boolean dispatchKeyEvent(KeyEvent e) {
if (JEditTextArea.this.isFocusOwner() && e.getKeyCode() == KeyEvent.VK_TAB && e.getModifiers() == 0) {
int modifiers = e.getModifiers();
if (JEditTextArea.this.isFocusOwner() && e.getKeyCode() == KeyEvent.VK_TAB
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be overcomplicating the matter a bit, but if simplified to JEditTextArea.this.isFocusOwner() && e.getKeyCode() == KeyEvent.VK_TAB CTRL + TAB results in a stack overflow for some reason.

&& (modifiers == 0 || (modifiers & InputEvent.SHIFT_MASK) != 0)) {
processKeyEvent(e);
return true;
} else {
Expand Down
32 changes: 32 additions & 0 deletions src/rars/venus/editors/jeditsyntax/TextUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,36 @@ public static int findWordEnd(String line, int pos, String noWordSep) {
}
return wordEnd;
}

/**
* Prefix all lines with the specified prefix.
*
* @param text The text
* @param prefix The prefix
*/
public static String addLinePrefixes(String text, String prefix) {
String[] lines = text.split("\n", -1);

for (int i = 0; i < lines.length; i++) {
lines[i] = prefix + lines[i];
}

return String.join("\n", lines);
}

/**
* Delete all lines of the specified prefix.
*
* @param text The text
* @param prefix The prefix
*/
public static String deleteLinePrefixes(String text, String prefix) {
String[] lines = text.split("\n", -1);

for (int i = 0; i < lines.length; i++) {
lines[i] = lines[i].replaceFirst(prefix, "");
}

return String.join("\n", lines);
}
}