forked from mozilla-b2g/gaia
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1027670 - Improve jump when press enter
- Loading branch information
Raniere Silva
committed
Aug 10, 2014
1 parent
148697c
commit 7a686d6
Showing
1 changed file
with
43 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,6 +103,43 @@ | |
} | ||
} | ||
|
||
function computeJumpLegth(textAfterCursor) { | ||
This comment has been minimized.
Sorry, something went wrong. |
||
var LaTeXJumpRegex = | ||
This comment has been minimized.
Sorry, something went wrong. |
||
/(\\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.
Sorry, something went wrong. |
||
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.
Sorry, something went wrong.
delapuente
|
||
} | ||
} 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{}{}}. | ||
|
@@ -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); | ||
|
@@ -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.
Sorry, something went wrong. |
||
} | ||
|
||
for (var i = 0; i < numberOfBackspacesToEmit; i++) { | ||
|
The name should be
computeJumpLength
. Notice the missingn
inLegth
.