PolycodeTextEditor.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. Copyright (C) 2012 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "PolycodeTextEditor.h"
  20. PolycodeSyntaxHighlighter::PolycodeSyntaxHighlighter(String extension) {
  21. colorScheme[0] = Color(0.0, 0.0, 0.0, 1.0);
  22. colorScheme[1] = Color(0.0, 0.53, 0.0, 1.0);
  23. colorScheme[2] = Color(0.79, 0.0, 0.63, 1.0);
  24. colorScheme[3] = Color(126.0/255.0, 73.0/255.0, 42.0/255.0, 1.0);
  25. colorScheme[4] = Color(227.0/255.0, 11.0/255.0, 0.0/255.0, 1.0);
  26. colorScheme[5] = Color(39.0/255.0, 90.0/255.0, 94.0/255.0, 1.0);
  27. colorScheme[6] = Color(56.0/255.0, 0.0/255.0, 218.0/255.0, 1.0);
  28. // String separators = " ;()\t\n=+-/\\'\"";
  29. // String keywords = "true,false,";
  30. separators = String(" ; ( ) \t \n = + - / \\ ' \"").split(" ");
  31. separators.push_back(" ");
  32. keywords = String("true false class self break do end else elseif function if local nil not or repeat return then until while").split(" ");
  33. }
  34. PolycodeSyntaxHighlighter::~PolycodeSyntaxHighlighter() {
  35. }
  36. bool PolycodeSyntaxHighlighter::contains(String part, std::vector<String> list) {
  37. for(int i=0; i < list.size(); i++) {
  38. if(list[i] == part)
  39. return true;
  40. }
  41. return false;
  42. }
  43. std::vector<SyntaxHighlightToken> PolycodeSyntaxHighlighter::parseText(String text) {
  44. return parseLua(text);
  45. }
  46. std::vector<SyntaxHighlightToken> PolycodeSyntaxHighlighter::parseLua(String text) {
  47. std::vector<SyntaxHighlightToken> tokens;
  48. const int MODE_GENERAL = 0;
  49. const int MODE_COMMENT = 1;
  50. const int MODE_STRING = 2;
  51. const int MODE_METHOD = 3;
  52. const int MODE_KEYWORD = 4;
  53. int mode = MODE_GENERAL;
  54. bool isComment = false;
  55. String line = "";
  56. char lastSeparator = ' ';
  57. for(int i=0; i < text.length(); i++) {
  58. char ch = text[i];
  59. if(contains(String(ch), separators)) {
  60. unsigned int type = mode;
  61. unsigned int ch_type = mode;
  62. if(ch == '\"')
  63. ch_type = MODE_STRING;
  64. if(mode != MODE_STRING && ch == '(') {
  65. type = MODE_METHOD;
  66. }
  67. if(mode != MODE_STRING) {
  68. if(contains(line, keywords)) {
  69. type = MODE_KEYWORD;
  70. }
  71. }
  72. if(isComment) {
  73. type = MODE_COMMENT;
  74. ch_type = MODE_COMMENT;
  75. }
  76. if(line != "")
  77. tokens.push_back(SyntaxHighlightToken(line, type));
  78. tokens.push_back(SyntaxHighlightToken(String(ch), ch_type));
  79. if(ch == '-' && lastSeparator == '-' && mode != MODE_STRING) {
  80. isComment = true;
  81. tokens[tokens.size()-1].type = MODE_COMMENT;
  82. tokens[tokens.size()-2].type = MODE_COMMENT;
  83. }
  84. if(ch == '\n' )
  85. isComment = false;
  86. if(ch == '\"') {
  87. if(mode == MODE_STRING) {
  88. mode = MODE_GENERAL;
  89. } else {
  90. mode = MODE_STRING;
  91. }
  92. }
  93. line = "";
  94. lastSeparator = ch;
  95. } else {
  96. line += String(ch);
  97. }
  98. }
  99. for(int i=0; i < tokens.size(); i++) {
  100. switch(tokens[i].type) {
  101. case MODE_STRING:
  102. tokens[i].color = colorScheme[4];
  103. break;
  104. case MODE_COMMENT:
  105. tokens[i].color = colorScheme[1];
  106. break;
  107. case MODE_METHOD:
  108. tokens[i].color = colorScheme[3];
  109. break;
  110. case MODE_KEYWORD:
  111. tokens[i].color = colorScheme[2];
  112. break;
  113. default:
  114. tokens[i].color = colorScheme[0];
  115. break;
  116. }
  117. // printf("%s(%d)", tokens[i].text.c_str(), tokens[i].type);
  118. }
  119. return tokens;
  120. }
  121. PolycodeTextEditor::PolycodeTextEditor() : PolycodeEditor(true){
  122. }
  123. PolycodeTextEditor::~PolycodeTextEditor() {
  124. if(syntaxHighligher)
  125. delete syntaxHighligher;
  126. }
  127. bool PolycodeTextEditor::openFile(OSFileEntry filePath) {
  128. textInput = new UITextInput(true, 100, 100);
  129. addChild(textInput);
  130. syntaxHighligher = NULL;
  131. if(filePath.extension == "lua") {
  132. syntaxHighligher = new PolycodeSyntaxHighlighter(filePath.extension);
  133. textInput->setSyntaxHighlighter(syntaxHighligher);
  134. }
  135. Data *data = new Data();
  136. data->loadFromFile(filePath.fullPath);
  137. textInput->insertText(data->getAsString(String::ENCODING_UTF8));
  138. delete data;
  139. PolycodeEditor::openFile(filePath);
  140. return true;
  141. }
  142. void PolycodeTextEditor::saveFile() {
  143. Data *data = new Data();
  144. data->setFromString(textInput->getText(), String::ENCODING_UTF8);
  145. data->saveToFile(filePath);
  146. delete data;
  147. }
  148. void PolycodeTextEditor::Resize(int x, int y) {
  149. textInput->Resize(x,y);
  150. PolycodeEditor::Resize(x,y);
  151. }