Skip to content

Commit

Permalink
Bug 1027670 - Improve jump when press enter
Browse files Browse the repository at this point in the history
  • Loading branch information
Raniere Silva committed Aug 10, 2014
1 parent 148697c commit 7a686d6
Showing 1 changed file with 43 additions and 24 deletions.
67 changes: 43 additions & 24 deletions apps/keyboard/js/imes/latex/latex.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,43 @@
}
}

function computeJumpLegth(textAfterCursor) {

This comment has been minimized.

Copy link
@delapuente

delapuente Aug 11, 2014

The name should be computeJumpLength. Notice the missing n in Legth.

var LaTeXJumpRegex =

This comment has been minimized.

Copy link
@delapuente

delapuente Aug 11, 2014

Use camelCase and explain the regular expression.

/(\\r(ight(\)|\]|\\\})|angle|ang|ceil|floor)|\\end\{\w+\}|\]|\\?\}|\\?\||\$)/;

var LaTeXJumpMatch = textAfterCursor.match(LaTeXJumpRegex);
if (LaTeXJumpMatch) {
if (LaTeXJumpMatch[0].startsWith('\\r') ||
LaTeXJumpMatch[0].startsWith('\\end')) {
// If match one delimiter, jump to the end of it.

This comment has been minimized.

Copy link
@delapuente

delapuente Aug 11, 2014

Reword: // If matching one delimiter, jump to the end of it.

return LaTeXJumpMatch.index + LaTeXJumpMatch[0].length;
} else {
// If LaTeXJumpMatch start at the begin of textAfterCursor we need
// to do something otherwise the cursor will not move.
if (LaTeXJumpMatch.index === 0) {
if (textAfterCursor.length === LaTeXJumpMatch[0].length) {
// If the match is the text after cursor we move to the end of it.
return LaTeXJumpMatch[0].length;
} else if (textAfterCursor[LaTeXJumpMatch[0].length] === '\\') {
// If the next char after the match is '\' we move before it.
return LaTeXJumpMatch[0].length;
} else if (textAfterCursor[LaTeXJumpMatch[0].length] === ' ') {
// If the next char after the match is ' ' we move after it.
return LaTeXJumpMatch[0].length + 1;
} else {
// Otherwise we need to search for another match.
return LaTeXJumpMatch[0].length +
computeJumpLegth(textAfterCursor.slice(LaTeXJumpMatch[0].length));

This comment has been minimized.

Copy link
@delapuente

delapuente Aug 11, 2014

Great! Now it can be understood very well. Simply consider the use of exec() method.

}
} else {
return LaTeXJumpMatch.index;
}
}
} else {
return 0;
}
}

/*
* Return key is used to jump inside and outside LaTeX commands and
* environment. Jump inside is need because of \underset{}{\overset{}{}}.
Expand All @@ -111,36 +148,18 @@
*
*/
function handleReturn() {
var LaTeXCommandEndRegex = /[}\]\|\$]/;
// Use 72 for the threshold because it was the suggested value for long text
// at PEP8 line size.
var jumpThreshold = 72;

var jumpLength, jumpLengthAfterMatch;
var textAfterCursor = keyboard.app.inputContext.textAfterCursor;
// Avoid jump to next paragraph
textAfterCursor = textAfterCursor.split('\n')[0];
// Allow only small jumps
textAfterCursor = textAfterCursor.slice(0, jumpThreshold);
var matchLaTeXCommandEnd = textAfterCursor.match(LaTeXCommandEndRegex);
if (matchLaTeXCommandEnd) {
// If matchLaTeXCommandEnd start at the begin of textAfterCursor we need
// to do something otherwise the cursor will not move.
if (matchLaTeXCommandEnd.index === 0) {
// If the is only one char after the cursor we move after it. Otherwise
// we move to the next match.
if (textAfterCursor.length === 1) {
jumpLengthAfterMatch = 1;
} else {
textAfterCursor = textAfterCursor.slice(1);
matchLaTeXCommandEnd = textAfterCursor.match(LaTeXCommandEndRegex);
jumpLengthAfterMatch = 1 + matchLaTeXCommandEnd.index;
}
} else {
jumpLengthAfterMatch = matchLaTeXCommandEnd.index;
}
jumpLength = keyboard.app.inputContext.selectionStart +
jumpLengthAfterMatch;

var jumpLength = computeJumpLegth(textAfterCursor);
if (jumpLength > 0) {
jumpLength += keyboard.app.inputContext.selectionStart;
keyboard.app.inputContext.setSelectionRange(jumpLength, 0);
} else {
keyboard.sendKey(RETURN);
Expand All @@ -153,13 +172,13 @@
*
*/
function handleBackspace() {
var lastWordRegex = /\\(\w+|\|)$/;
var lastWordRegex = /\\(\w+|\||\{)$/;
var numberOfBackspacesToEmit = 1;
var wordsBeforeCursor = keyboard.app.inputContext.textBeforeCursor;

var lastWordMatch = wordsBeforeCursor.match(lastWordRegex);
if (lastWordMatch) {
numberOfBackspacesToEmit = lastWordMatch[lastWordMatch.length - 1].length;
numberOfBackspacesToEmit = lastWordMatch[0].length;

This comment has been minimized.

Copy link
@delapuente

delapuente Aug 11, 2014

Correct!

}

for (var i = 0; i < numberOfBackspacesToEmit; i++) {
Expand Down

0 comments on commit 7a686d6

Please sign in to comment.