Skip to content

Commit

Permalink
fix #50
Browse files Browse the repository at this point in the history
  • Loading branch information
yydcdut committed Jun 26, 2018
1 parent 1cf5cea commit 5922fb2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,22 @@ public void onTextChanged(CharSequence s, int start, int before, int after) {
if (after == 0) {
return;
}
String addString = s.subSequence(start, (start + after) > s.length() ? s.length() : (start + after)).toString();
String addString = s.subSequence(Utils.safePosition(start, s), Utils.safePosition(start + after, s)).toString();
String beforeString = null;
String afterString = null;
if (start + 1 <= s.length()) {
afterString = s.subSequence(start, start + 1).toString();
afterString = s.subSequence(Utils.safePosition(start, s), Utils.safePosition(start + 1, s)).toString();
}
if (start > 0) {
beforeString = s.subSequence(start - 1, start).toString();
beforeString = s.subSequence(Utils.safePosition(start - 1, s), Utils.safePosition(start, s)).toString();
}
//``` --> `1``(``1`)(```1)(1```)
if (SyntaxUtils.isNeedFormat(SyntaxKey.KEY_CODE_BLOCK_SINGLE, addString, beforeString, afterString)) {
format((Editable) s, start);
}
}


private void format(Editable editable, int start) {
Utils.removeSpans(editable, start, MDCodeBlockSpan.class);
Syntax syntax = EditFactory.create().getCodeBlockSyntax(mMarkdownConfiguration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,4 +321,14 @@ public static char getChar(char[] array, int index) {
return array[index];
}

/**
* get position safely
*
* @param position the position
* @param s the CharSequence
* @return the safe position
*/
public static int safePosition(int position, CharSequence s) {
return position > s.length() ? s.length() : (position < 0 ? 0 : position);
}
}

0 comments on commit 5922fb2

Please sign in to comment.