SkPlainTextEditor: from experimental to modules

Change-Id: I8896283ee3a57af926a43f6647e27059d52dd7a8
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/237146
Reviewed-by: Hal Canary <halcanary@google.com>
Commit-Queue: Hal Canary <halcanary@google.com>
diff --git a/BUILD.gn b/BUILD.gn
index a59e3fb..61baf01 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -2616,7 +2616,7 @@
     test_app("editor") {
       is_shared_library = is_android
       deps = [
-        "experimental/editor:editor_app",
+        "modules/skplaintexteditor:editor_app",
       ]
     }
   }
diff --git a/infra/bots/compile.isolate b/infra/bots/compile.isolate
index bd3108e..d8fe2dc 100644
--- a/infra/bots/compile.isolate
+++ b/infra/bots/compile.isolate
@@ -17,7 +17,14 @@
       '../../dm',
       '../../docs/examples',
       '../../example',
-      '../../experimental',
+      '../../experimental/Networking/SkSockets.cpp',
+      '../../experimental/Networking/SkSockets.h',
+      '../../experimental/c-api-example/skia-c-example.c',
+      '../../experimental/ffmpeg',
+      '../../experimental/svg/model',
+      '../../experimental/tools/coreGraphicsPdf2png.cpp',
+      '../../experimental/wasm-skp-debugger/debugger_bindings.cpp',
+      '../../experimental/xform',
       '../../fuzz',
       '../../gm',
       '../../gn',
diff --git a/experimental/editor/BUILD.gn b/modules/skplaintexteditor/BUILD.gn
similarity index 76%
rename from experimental/editor/BUILD.gn
rename to modules/skplaintexteditor/BUILD.gn
index aeac20e..1442477 100644
--- a/experimental/editor/BUILD.gn
+++ b/modules/skplaintexteditor/BUILD.gn
@@ -8,13 +8,13 @@
   source_set("editor_lib") {
     include_dirs = [ "../.." ]
     public = [
-      "editor.h",
-      "stringslice.h",
-      "stringview.h",
+      "include/editor.h",
+      "include/stringslice.h",
+      "include/stringview.h",
     ]
     sources = [
-      "editor.cpp",
-      "stringslice.cpp",
+      "src/editor.cpp",
+      "src/stringslice.cpp",
     ]
     public_deps = [
       "../..:skia",
@@ -27,10 +27,10 @@
   source_set("shape") {
     include_dirs = [ "../.." ]
     public = [
-      "shape.h",
+      "src/shape.h",
     ]
     sources = [
-      "shape.cpp",
+      "src/shape.cpp",
     ]
     public_deps = [
       "../..:skia",
@@ -44,10 +44,10 @@
   source_set("word_boundaries") {
     include_dirs = [ "../.." ]
     public = [
-      "word_boundaries.h",
+      "src/word_boundaries.h",
     ]
     sources = [
-      "word_boundaries.cpp",
+      "src/word_boundaries.cpp",
     ]
     deps = [
       "../../third_party/icu",
@@ -57,7 +57,7 @@
   source_set("editor_app") {
     testonly = true
     sources = [
-      "editor_application.cpp",
+      "app/editor_application.cpp",
     ]
     public_deps = [
       "../..:sk_app",
diff --git a/experimental/editor/README.md b/modules/skplaintexteditor/README.md
similarity index 100%
rename from experimental/editor/README.md
rename to modules/skplaintexteditor/README.md
diff --git a/experimental/editor/editor_application.cpp b/modules/skplaintexteditor/app/editor_application.cpp
similarity index 82%
rename from experimental/editor/editor_application.cpp
rename to modules/skplaintexteditor/app/editor_application.cpp
index 30c5505..3a49d55 100644
--- a/experimental/editor/editor_application.cpp
+++ b/modules/skplaintexteditor/app/editor_application.cpp
@@ -1,7 +1,7 @@
 // Copyright 2019 Google LLC.
 // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
 
-// [Work In Progress] Proof of principle of a text editor written with Skia & SkShaper.
+// Proof of principle of a text editor written with Skia & SkShaper.
 // https://bugs.skia.org/9020
 
 #include "include/core/SkCanvas.h"
@@ -12,13 +12,16 @@
 #include "tools/sk_app/Application.h"
 #include "tools/sk_app/Window.h"
 
-#include "experimental/editor/editor.h"
+#include "modules/skplaintexteditor/include/editor.h"
 
 #include "third_party/icu/SkLoadICU.h"
 
 #include <fstream>
 #include <memory>
 
+using SkPlainTextEditor::Editor;
+using SkPlainTextEditor::StringView;
+
 #ifdef SK_EDITOR_DEBUG_OUT
 static const char* key_name(sk_app::Window::Key k) {
     switch (k) {
@@ -55,15 +58,15 @@
 }
 #endif  // SK_EDITOR_DEBUG_OUT
 
-static editor::Editor::Movement convert(sk_app::Window::Key key) {
+static Editor::Movement convert(sk_app::Window::Key key) {
     switch (key) {
-        case sk_app::Window::Key::kLeft:  return editor::Editor::Movement::kLeft;
-        case sk_app::Window::Key::kRight: return editor::Editor::Movement::kRight;
-        case sk_app::Window::Key::kUp:    return editor::Editor::Movement::kUp;
-        case sk_app::Window::Key::kDown:  return editor::Editor::Movement::kDown;
-        case sk_app::Window::Key::kHome:  return editor::Editor::Movement::kHome;
-        case sk_app::Window::Key::kEnd:   return editor::Editor::Movement::kEnd;
-        default: return editor::Editor::Movement::kNowhere;
+        case sk_app::Window::Key::kLeft:  return Editor::Movement::kLeft;
+        case sk_app::Window::Key::kRight: return Editor::Movement::kRight;
+        case sk_app::Window::Key::kUp:    return Editor::Movement::kUp;
+        case sk_app::Window::Key::kDown:  return Editor::Movement::kDown;
+        case sk_app::Window::Key::kHome:  return Editor::Movement::kHome;
+        case sk_app::Window::Key::kEnd:   return Editor::Movement::kEnd;
+        default: return Editor::Movement::kNowhere;
     }
 }
 namespace {
@@ -80,9 +83,9 @@
     sk_app::Window* fParent = nullptr;
     // TODO(halcanary): implement a cross-platform clipboard interface.
     std::vector<char> fClipboard;
-    editor::Editor fEditor;
-    editor::Editor::TextPosition fTextPos{0, 0};
-    editor::Editor::TextPosition fMarkPos;
+    Editor fEditor;
+    Editor::TextPosition fTextPos{0, 0};
+    Editor::TextPosition fMarkPos;
     int fPos = 0;  // window pixel position in file
     int fWidth = 0;  // window width
     int fHeight = 0;  // window height
@@ -94,7 +97,7 @@
     void loadFile(const char* path) {
         if (sk_sp<SkData> data = SkData::MakeFromFileName(path)) {
             fPath = path;
-            fEditor.insert(editor::Editor::TextPosition{0, 0},
+            fEditor.insert(Editor::TextPosition{0, 0},
                            (const char*)data->data(), data->size());
         } else {
             fPath  = "output.txt";
@@ -106,12 +109,12 @@
         SkAutoCanvasRestore acr(canvas, true);
         canvas->clipRect({0, 0, (float)fWidth, (float)fHeight});
         canvas->translate(fMargin, (float)(fMargin - fPos));
-        editor::Editor::PaintOpts options;
+        Editor::PaintOpts options;
         options.fCursor = fTextPos;
         options.fCursorColor = {1, 0, 0, fBlink ? 0.0f : 1.0f};
         options.fBackgroundColor = SkColor4f{0.8f, 0.8f, 0.8f, 1};
         options.fCursorColor = {1, 0, 0, fBlink ? 0.0f : 1.0f};
-        if (fMarkPos != editor::Editor::TextPosition()) {
+        if (fMarkPos != Editor::TextPosition()) {
             options.fSelectionBegin = fMarkPos;
             options.fSelectionEnd = fTextPos;
         }
@@ -181,7 +184,7 @@
                 #ifdef SK_EDITOR_DEBUG_OUT
                 SkDebugf("insert: %X'%c'\n", (unsigned)c, ch);
                 #endif  // SK_EDITOR_DEBUG_OUT
-                return this->moveCursor(editor::Editor::Movement::kRight);
+                return this->moveCursor(Editor::Movement::kRight);
             }
         }
         static constexpr ModifierKey kCommandOrControl = ModifierKey::kCommand |
@@ -189,7 +192,7 @@
         if (Any(modi & kCommandOrControl) && !Any(modi & ~kCommandOrControl)) {
             switch (c) {
                 case 'p':
-                    for (editor::StringView str : fEditor.text()) {
+                    for (StringView str : fEditor.text()) {
                         SkDebugf(">>  '%.*s'\n", str.size, str.data);
                     }
                     return true;
@@ -201,31 +204,49 @@
                             if (i != 0) {
                                 out << '\n';
                             }
-                            editor::StringView str = fEditor.line(i);
+                            StringView str = fEditor.line(i);
                             out.write(str.data, str.size);
                         }
                     }
                     return true;
                 case 'c':
-                    if (fMarkPos != editor::Editor::TextPosition()) {
+                    if (fMarkPos != Editor::TextPosition()) {
                         fClipboard.resize(fEditor.copy(fMarkPos, fTextPos, nullptr));
                         fEditor.copy(fMarkPos, fTextPos, fClipboard.data());
                         return true;
                     }
+                    return false;
                 case 'x':
-                    if (fMarkPos != editor::Editor::TextPosition()) {
+                    if (fMarkPos != Editor::TextPosition()) {
                         fClipboard.resize(fEditor.copy(fMarkPos, fTextPos, nullptr));
                         fEditor.copy(fMarkPos, fTextPos, fClipboard.data());
                         (void)this->move(fEditor.remove(fMarkPos, fTextPos), false);
                         this->inval();
                         return true;
                     }
+                    return false;
                 case 'v':
                     if (fClipboard.size()) {
                         fEditor.insert(fTextPos, fClipboard.data(), fClipboard.size());
                         this->inval();
                         return true;
                     }
+                    return false;
+                case '=':
+                case '+':
+                    {
+                        float s = fEditor.font().getSize() + 1;
+                        fEditor.setFont(fEditor.font().makeWithSize(s));
+                    }
+                    return true;
+                case '-':
+                case '_':
+                    {
+                        float s = fEditor.font().getSize() - 1;
+                        if (s > 0) {
+                            fEditor.setFont(fEditor.font().makeWithSize(s));
+                        }
+                    }
             }
         }
         #ifdef SK_EDITOR_DEBUG_OUT
@@ -234,19 +255,19 @@
         return false;
     }
 
-    bool moveCursor(editor::Editor::Movement m, bool shift = false) {
+    bool moveCursor(Editor::Movement m, bool shift = false) {
         return this->move(fEditor.move(m, fTextPos), shift);
     }
 
-    bool move(editor::Editor::TextPosition pos, bool shift) {
-        if (pos == fTextPos || pos == editor::Editor::TextPosition()) {
+    bool move(Editor::TextPosition pos, bool shift) {
+        if (pos == fTextPos || pos == Editor::TextPosition()) {
             if (!shift) {
-                fMarkPos = editor::Editor::TextPosition();
+                fMarkPos = Editor::TextPosition();
             }
             return false;
         }
         if (shift != fShiftDown) {
-            fMarkPos = shift ? fTextPos : editor::Editor::TextPosition();
+            fMarkPos = shift ? fTextPos : Editor::TextPosition();
             fShiftDown = shift;
         }
         fTextPos = pos;
@@ -289,19 +310,19 @@
                 case sk_app::Window::Key::kEnd:
                     return this->moveCursor(convert(key), shift);
                 case sk_app::Window::Key::kDelete:
-                    if (fMarkPos != editor::Editor::TextPosition()) {
+                    if (fMarkPos != Editor::TextPosition()) {
                         (void)this->move(fEditor.remove(fMarkPos, fTextPos), false);
                     } else {
-                        auto pos = fEditor.move(editor::Editor::Movement::kRight, fTextPos);
+                        auto pos = fEditor.move(Editor::Movement::kRight, fTextPos);
                         (void)this->move(fEditor.remove(fTextPos, pos), false);
                     }
                     this->inval();
                     return true;
                 case sk_app::Window::Key::kBack:
-                    if (fMarkPos != editor::Editor::TextPosition()) {
+                    if (fMarkPos != Editor::TextPosition()) {
                         (void)this->move(fEditor.remove(fMarkPos, fTextPos), false);
                     } else {
-                        auto pos = fEditor.move(editor::Editor::Movement::kLeft, fTextPos);
+                        auto pos = fEditor.move(Editor::Movement::kLeft, fTextPos);
                         (void)this->move(fEditor.remove(fTextPos, pos), false);
                     }
                     this->inval();
@@ -314,9 +335,9 @@
         } else if (skstd::Any(ctrlAltCmd & (ModifierKey::kControl | ModifierKey::kCommand))) {
             switch (key) {
                 case sk_app::Window::Key::kLeft:
-                    return this->moveCursor(editor::Editor::Movement::kWordLeft, shift);
+                    return this->moveCursor(Editor::Movement::kWordLeft, shift);
                 case sk_app::Window::Key::kRight:
-                    return this->moveCursor(editor::Editor::Movement::kWordRight, shift);
+                    return this->moveCursor(Editor::Movement::kWordRight, shift);
                 default:
                     break;
             }
@@ -355,7 +376,7 @@
         fWindow->pushLayer(&fLayer);
         fWindow->setTitle(SkStringPrintf("Editor: \"%s\"", fLayer.fPath.c_str()).c_str());
         fLayer.onResize(fWindow->width(), fWindow->height());
-        fLayer.fEditor.paint(nullptr, editor::Editor::PaintOpts());
+        fLayer.fEditor.paint(nullptr, Editor::PaintOpts());
 
         fWindow->show();
         return true;
diff --git a/experimental/editor/editor.h b/modules/skplaintexteditor/include/editor.h
similarity index 84%
rename from experimental/editor/editor.h
rename to modules/skplaintexteditor/include/editor.h
index 565fa6e..b87d536 100644
--- a/experimental/editor/editor.h
+++ b/modules/skplaintexteditor/include/editor.h
@@ -3,8 +3,8 @@
 #ifndef editor_DEFINED
 #define editor_DEFINED
 
-#include "experimental/editor/stringslice.h"
-#include "experimental/editor/stringview.h"
+#include "modules/skplaintexteditor/include/stringslice.h"
+#include "modules/skplaintexteditor/include/stringview.h"
 
 #include "include/core/SkColor.h"
 #include "include/core/SkFont.h"
@@ -19,9 +19,7 @@
 class SkCanvas;
 class SkShaper;
 
-// TODO: modulize this; editor::Editor becomes SkEditor ?
-
-namespace editor {
+namespace SkPlainTextEditor {
 
 class Editor {
     struct TextLine;
@@ -49,7 +47,7 @@
     };
     // Loop over all the lines of text.  The lines are not '\0'- or '\n'-terminated.
     // For example, to dump the entire file to standard output:
-    //     for (editor::StringView str : editor.text()) {
+    //     for (SkPlainTextEditor::StringView str : editor.text()) {
     //         std::cout.write(str.data, str.size) << '\n';
     //     }
     Text text() const { return Text{fLines}; }
@@ -125,17 +123,17 @@
     void markDirty(TextLine*);
     void reshapeAll();
 };
-}  // namespace editor
+}  // namespace SkPlainTextEditor
 
-static inline bool operator==(const editor::Editor::TextPosition& u,
-                              const editor::Editor::TextPosition& v) {
+static inline bool operator==(const SkPlainTextEditor::Editor::TextPosition& u,
+                              const SkPlainTextEditor::Editor::TextPosition& v) {
     return u.fParagraphIndex == v.fParagraphIndex && u.fTextByteIndex == v.fTextByteIndex;
 }
-static inline bool operator!=(const editor::Editor::TextPosition& u,
-                              const editor::Editor::TextPosition& v) { return !(u == v); }
+static inline bool operator!=(const SkPlainTextEditor::Editor::TextPosition& u,
+                              const SkPlainTextEditor::Editor::TextPosition& v) { return !(u == v); }
 
-static inline bool operator<(const editor::Editor::TextPosition& u,
-                             const editor::Editor::TextPosition& v) {
+static inline bool operator<(const SkPlainTextEditor::Editor::TextPosition& u,
+                             const SkPlainTextEditor::Editor::TextPosition& v) {
     return u.fParagraphIndex < v.fParagraphIndex ||
            (u.fParagraphIndex == v.fParagraphIndex && u.fTextByteIndex < v.fTextByteIndex);
 }
diff --git a/experimental/editor/stringslice.h b/modules/skplaintexteditor/include/stringslice.h
similarity index 87%
rename from experimental/editor/stringslice.h
rename to modules/skplaintexteditor/include/stringslice.h
index 12b0718..00619af 100644
--- a/experimental/editor/stringslice.h
+++ b/modules/skplaintexteditor/include/stringslice.h
@@ -3,12 +3,12 @@
 #ifndef stringslice_DEFINED
 #define stringslice_DEFINED
 
-#include "experimental/editor/stringview.h"
+#include "modules/skplaintexteditor/include/stringview.h"
 
 #include <memory>
 #include <cstddef>
 
-namespace editor {
+namespace SkPlainTextEditor {
 // A lightweight modifiable string class.
 class StringSlice {
 public:
@@ -25,7 +25,7 @@
     const char* begin() const { return fPtr.get(); }
     const char* end() const { return fPtr ? fPtr.get() + fLength : nullptr; }
     std::size_t size() const { return fLength; }
-    editor::StringView view() const { return {fPtr.get(), fLength}; }
+    SkPlainTextEditor::StringView view() const { return {fPtr.get(), fLength}; }
 
     // mutation:
     void insert(std::size_t offset, const char* text, std::size_t length);
@@ -42,5 +42,5 @@
     std::size_t fCapacity = 0;
     void realloc(std::size_t);
 };
-}  // namespace editor;
+}  // namespace SkPlainTextEditor;
 #endif  // stringslice_DEFINED
diff --git a/experimental/editor/stringview.h b/modules/skplaintexteditor/include/stringview.h
similarity index 91%
rename from experimental/editor/stringview.h
rename to modules/skplaintexteditor/include/stringview.h
index 2e9a0eb..a9effa5 100644
--- a/experimental/editor/stringview.h
+++ b/modules/skplaintexteditor/include/stringview.h
@@ -5,7 +5,7 @@
 
 #include <cstddef>
 
-namespace editor {
+namespace SkPlainTextEditor {
 
 template <typename T>
 struct Span {
diff --git a/experimental/editor/editor.cpp b/modules/skplaintexteditor/src/editor.cpp
similarity index 98%
rename from experimental/editor/editor.cpp
rename to modules/skplaintexteditor/src/editor.cpp
index a7c0833..4b66d80 100644
--- a/experimental/editor/editor.cpp
+++ b/modules/skplaintexteditor/src/editor.cpp
@@ -1,18 +1,18 @@
 // Copyright 2019 Google LLC.
 // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
 
-#include "experimental/editor/editor.h"
+#include "modules/skplaintexteditor/include/editor.h"
 
 #include "include/core/SkCanvas.h"
 #include "include/core/SkExecutor.h"
 #include "include/core/SkPath.h"
 #include "src/utils/SkUTF.h"
 
-#include "experimental/editor/shape.h"
+#include "modules/skplaintexteditor/src/shape.h"
 
 #include <algorithm>
 
-using namespace editor;
+using namespace SkPlainTextEditor;
 
 static inline SkRect offset(SkRect r, SkIPoint p) {
     return r.makeOffset((float)p.x(), (float)p.y());
diff --git a/experimental/editor/shape.cpp b/modules/skplaintexteditor/src/shape.cpp
similarity index 97%
rename from experimental/editor/shape.cpp
rename to modules/skplaintexteditor/src/shape.cpp
index d82f7c6..9acba0d 100644
--- a/experimental/editor/shape.cpp
+++ b/modules/skplaintexteditor/src/shape.cpp
@@ -1,9 +1,8 @@
 // Copyright 2019 Google LLC.
 // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
 
-#include "experimental/editor/shape.h"
+#include "modules/skplaintexteditor/src/shape.h"
 
-#include "experimental/editor/word_boundaries.h"
 #include "include/core/SkFont.h"
 #include "include/core/SkFontMetrics.h"
 #include "include/core/SkPoint.h"
@@ -13,6 +12,7 @@
 #include "include/core/SkTextBlob.h"
 #include "include/core/SkTypes.h"
 #include "include/private/SkTFitsIn.h"
+#include "modules/skplaintexteditor/src/word_boundaries.h"
 #include "modules/skshaper/include/SkShaper.h"
 #include "src/core/SkTextBlobPriv.h"
 #include "src/utils/SkUTF.h"
@@ -21,7 +21,7 @@
 #include <string.h>
 
 
-using namespace editor;
+using namespace SkPlainTextEditor;
 
 namespace {
 class RunHandler final : public SkShaper::RunHandler {
@@ -257,7 +257,7 @@
     }
 }
 
-ShapeResult editor::Shape(const char* utf8Text,
+ShapeResult SkPlainTextEditor::Shape(const char* utf8Text,
                           size_t textByteLen,
                           const SkFont& font,
                           const char* locale,
diff --git a/experimental/editor/shape.h b/modules/skplaintexteditor/src/shape.h
similarity index 94%
rename from experimental/editor/shape.h
rename to modules/skplaintexteditor/src/shape.h
index 8a74c81..c5585eb 100644
--- a/experimental/editor/shape.h
+++ b/modules/skplaintexteditor/src/shape.h
@@ -7,7 +7,7 @@
 #include <cstddef>
 #include <vector>
 
-namespace editor {
+namespace SkPlainTextEditor {
 
 struct ShapeResult {
     sk_sp<SkTextBlob> blob;
diff --git a/experimental/editor/stringslice.cpp b/modules/skplaintexteditor/src/stringslice.cpp
similarity index 95%
rename from experimental/editor/stringslice.cpp
rename to modules/skplaintexteditor/src/stringslice.cpp
index fc78a35..3f7da00 100644
--- a/experimental/editor/stringslice.cpp
+++ b/modules/skplaintexteditor/src/stringslice.cpp
@@ -1,14 +1,14 @@
 // Copyright 2019 Google LLC.
 // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
 
-#include "experimental/editor/stringslice.h"
+#include "modules/skplaintexteditor/include/stringslice.h"
 
 #include <algorithm>
 #include <cassert>
 #include <cstdlib>
 #include <cstring>
 
-using namespace editor;
+using namespace SkPlainTextEditor;
 
 void StringSlice::FreeWrapper::operator()(void* t) { std::free(t); }
 
diff --git a/experimental/editor/word_boundaries.cpp b/modules/skplaintexteditor/src/word_boundaries.cpp
similarity index 95%
rename from experimental/editor/word_boundaries.cpp
rename to modules/skplaintexteditor/src/word_boundaries.cpp
index c0a37cf..17c8cd3 100644
--- a/experimental/editor/word_boundaries.cpp
+++ b/modules/skplaintexteditor/src/word_boundaries.cpp
@@ -1,7 +1,7 @@
 // Copyright 2019 Google LLC.
 // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
 
-#include "experimental/editor/word_boundaries.h"
+#include "modules/skplaintexteditor/src/word_boundaries.h"
 
 #include <unicode/brkiter.h>
 #include <unicode/unistr.h>
diff --git a/experimental/editor/word_boundaries.h b/modules/skplaintexteditor/src/word_boundaries.h
similarity index 100%
rename from experimental/editor/word_boundaries.h
rename to modules/skplaintexteditor/src/word_boundaries.h