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

Snippet token for inserting selection (closes #2059) #2081

Closed
wants to merge 3 commits into from
Closed
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 @@ -348,6 +348,9 @@ private Pair<byte[], Integer> getTemplateContent(final Spinner templateSpinner,
final int startingIndex = t.indexOf(HighlightingEditor.PLACE_CURSOR_HERE_TOKEN);
t = t.replace(HighlightingEditor.PLACE_CURSOR_HERE_TOKEN, "");

// Has no utility in a new file
t = t.replace(HighlightingEditor.INSERT_SELECTION_HERE_TOKEN, "");

final byte[] bytes;
if (encrypt && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
final char[] pass = ApplicationObject.settings().getDefaultPassword();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class HighlightingEditor extends AppCompatEditText {
final static float HIGHLIGHT_REGION_SIZE = 0.75f; // Minimum extra screens to highlight (should be > 0.5 to cover screen)

public final static String PLACE_CURSOR_HERE_TOKEN = "%%PLACE_CURSOR_HERE%%";
public final static String INSERT_SELECTION_HERE_TOKEN = "%%INSERT_SELECTION_HERE%%";

private boolean _accessibilityEnabled = true;
private final boolean _isSpellingRedUnderline;
Expand Down Expand Up @@ -190,6 +191,11 @@ private int[] hlRegion(final Rect rect) {
}
}

@Override
public boolean bringPointIntoView(int i) {
return super.bringPointIntoView(i);
}
Comment on lines 193 to +197
Copy link
Owner

Choose a reason for hiding this comment

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

Is there a reason for this? As far I see it's a override that just calls the original implementation


private int rowStart(final int y) {
final Layout layout = getLayout();
final int line = layout.getLineForVertical(y);
Expand Down Expand Up @@ -360,13 +366,30 @@ public void simulateKeyPress(int keyEvent_KEYCODE_SOMETHING) {
public void insertOrReplaceTextOnCursor(final String newText) {
final Editable edit = getText();
if (edit != null && newText != null) {
final int newCursorPos = newText.indexOf(PLACE_CURSOR_HERE_TOKEN);
final String finalText = newText.replace(PLACE_CURSOR_HERE_TOKEN, "");

// TODO - should consider moving any snippet specific logic out of here
// Fill in any instances of selection
final int[] sel = TextViewUtils.getSelection(this);
final CharSequence selected = TextViewUtils.toString(edit, sel[0], sel[1]);
String expanded = newText.replace(INSERT_SELECTION_HERE_TOKEN, selected);

// Determine where to place the cursor
final int newCursorPos = expanded.indexOf(PLACE_CURSOR_HERE_TOKEN);
final String finalText = expanded.replace(PLACE_CURSOR_HERE_TOKEN, "");

sel[0] = Math.max(sel[0], 0);

// Needed to prevent selection of whole of inserted text after replace
// if we want a cursor position instead
if (newCursorPos >= 0) {
setSelection(sel[0]);
}

withAutoFormatDisabled(() -> edit.replace(sel[0], sel[1], finalText));

if (newCursorPos >= 0) {
setSelection(sel[0] + newCursorPos);
TextViewUtils.showSelection(this);
}
}
}
Expand Down
Loading