[Python] Modified the paste effect in script and shell area

When a formula is pasted in a script or in the shell, some symbols are
replaced by their equivalent in python :
x turns into *
^ turns into **
√ turns into sqrt
etc
Change-Id: If6f2a22d4f3c148c2655e0892023b0e28058a9a6
This commit is contained in:
Arthur Camouseigt
2020-06-29 15:33:10 +02:00
committed by Émilie Feral
parent 4b965a0ff6
commit 8f97a332f6
10 changed files with 261 additions and 67 deletions

View File

@@ -89,6 +89,7 @@ escher_src += $(addprefix escher/src/,\
)
tests_src += $(addprefix escher/test/,\
clipboard.cpp \
layout_field.cpp\
)

View File

@@ -3,15 +3,43 @@
#include <escher/text_field.h>
#include <poincare/layout.h>
#include <ion/unicode/utf8_helper.h>
class Clipboard {
public:
static Clipboard * sharedClipboard();
void store(const char * storedText, int length = -1);
const char * storedText() const { return m_textBuffer; }
const char * storedText() { return m_textBuffer; }
void reset();
void enterPython() { replaceCharForPython(true); }
void exitPython() { replaceCharForPython(false); }
static constexpr int k_bufferSize = TextField::maxBufferSize();
private:
char m_textBuffer[TextField::maxBufferSize()];
char m_textBuffer[k_bufferSize];
void replaceCharForPython(bool entersPythonApp);
};
/* The order in which the text pairs are stored is important. Indeed when leaving
* python, the text stored in the buffer is converted into an input for other
* apps. Therefore if we want to convert "3**3" into "3^3", the function must
* look for "**" paterns before "*". Otherwise, we will get "3××3". */
static constexpr int NumberOfPythonTextPairs = 12;
static constexpr UTF8Helper::TextPair PythonTextPairs[NumberOfPythonTextPairs] = {
UTF8Helper::TextPair("√(\x11)", "sqrt(\x11)", true),
UTF8Helper::TextPair("^(\x11)", "exp(\x11)", true),
UTF8Helper::TextPair("log(\x11)", "log10(\x11)", true),
UTF8Helper::TextPair("ln(\x11)", "log(\x11)", true),
UTF8Helper::TextPair("", "e"),
UTF8Helper::TextPair("𝐢", "1j"),
/* Since textPairs are also used to pair events, we need to keep both ^2 and ^
* to get the desired behavior in python when using power or square key*/
UTF8Helper::TextPair("^2", "**2"),
UTF8Helper::TextPair("^", "**"),
UTF8Helper::TextPair("π", "pi"),
UTF8Helper::TextPair("×", "*"),
UTF8Helper::TextPair("·", "*"),
UTF8Helper::TextPair("][", "], ["),
};
#endif

View File

@@ -1,4 +1,5 @@
#include <escher/clipboard.h>
#include <escher/text_field.h>
#include <algorithm>
static Clipboard s_clipboard;
@@ -14,3 +15,7 @@ void Clipboard::store(const char * storedText, int length) {
void Clipboard::reset() {
strlcpy(m_textBuffer, "", 1);
}
void Clipboard::replaceCharForPython(bool entersPythonApp) {
UTF8Helper::tryAndReplacePatternsInStringByPatterns((char *)m_textBuffer, TextField::maxBufferSize(), (UTF8Helper::TextPair *)&PythonTextPairs, NumberOfPythonTextPairs, entersPythonApp);
}

View File

@@ -123,6 +123,7 @@ bool TextField::ContentView::insertTextAtLocation(const char * text, char * loca
assert(m_isEditing);
size_t textLength = textLen < 0 ? strlen(text) : (size_t)textLen;
// TODO when paste fails because of a too big message, create a pop-up
if (m_currentDraftTextLength + textLength >= m_draftTextBufferSize || textLength == 0) {
return false;
}

21
escher/test/clipboard.cpp Normal file
View File

@@ -0,0 +1,21 @@
#include <quiz.h>
#include <string.h>
#include <escher/clipboard.h>
void assert_clipboard_enters_and_exits_python(const char * string, const char * stringResult) {
Clipboard * clipboard = Clipboard::sharedClipboard();
clipboard->store(string);
clipboard->enterPython();
quiz_assert(strcmp(clipboard->storedText(), stringResult) == 0);
clipboard->exitPython();
quiz_assert(strcmp(clipboard->storedText(), string) == 0);
}
QUIZ_CASE(escher_clipboard_enters_and_exits_python) {
assert_clipboard_enters_and_exits_python("4×4", "4*4");
assert_clipboard_enters_and_exits_python("^(ln(4))", "exp(log(4))");
assert_clipboard_enters_and_exits_python("ln(log(ln(π)))^𝐢", "log(log10(log(pi)))**1j");
assert_clipboard_enters_and_exits_python("√(1ᴇ10)", "sqrt(1e10)");
assert_clipboard_enters_and_exits_python("1×𝐢^2", "1*1j**2");
assert_clipboard_enters_and_exits_python("12^(1/4)×(π/6)×(12×π)^(1/4)", "12**(1/4)*(pi/6)*(12*pi)**(1/4)");
}