From 4be8a483e2f9d657772922a0b46d8759ed3970c4 Mon Sep 17 00:00:00 2001 From: Possseidon Date: Tue, 20 Sep 2022 16:36:37 +0200 Subject: [PATCH] Start implementation of clipboard. --- code/Editor.lua | 19 ++++++++++++++++--- code/clipboard.lua | 23 +++++++++++++++++++++++ code/update.lua | 1 + 3 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 code/clipboard.lua diff --git a/code/Editor.lua b/code/Editor.lua index ea9f697..d2a4f64 100644 --- a/code/Editor.lua +++ b/code/Editor.lua @@ -1,4 +1,5 @@ local lexLua = require "code.lexers.lexLua" +local clipboard = require "code.clipboard" local Highlighter = require "code.Highlighter" ---@class Point @@ -686,17 +687,29 @@ end ---TODO function Editor:cut() - -- TODO + self:copy() end ---TODO function Editor:copy() - -- TODO + local selectedText = self:selectedText() + if selectedText then + clipboard.set(selectedText, false) + else + -- local x, y = + -- clipboard.set(self:(), true) + end end ---TODO function Editor:paste() - -- TODO + local text, fullLine = clipboard.get() + if fullLine then + local x, y = self:getCursor() + self:replaceLines(y, y - 1, text, x, y) -- TODO: Where to put cursor? Same x next line? + else + self:insert(text) + end end ---TODO diff --git a/code/clipboard.lua b/code/clipboard.lua new file mode 100644 index 0000000..941b0b5 --- /dev/null +++ b/code/clipboard.lua @@ -0,0 +1,23 @@ +local clipboard = {} + +local filename = ".code-clipboard" + +---Returns the current content of the clipboard. +---@return string text, boolean fullLine +function clipboard.get() + local file = fs.open(filename, "rb") + local data = file.readAll() + file.close() + return data:sub(2), data:sub(1, 1) == "\1" +end + +---Sets the content of the clipboard with the given values. +---@param text string +---@param fullLine boolean? +function clipboard.set(text, fullLine) + local file = fs.open(filename, "wb") + file.write((fullLine and "\1" or "\0") .. text) + file.close() +end + +return clipboard diff --git a/code/update.lua b/code/update.lua index 5d10dc0..3d8bbdd 100644 --- a/code/update.lua +++ b/code/update.lua @@ -21,6 +21,7 @@ local githubPossseidon = "https://raw.githubusercontent.com/Possseidon/" addRequests(githubPossseidon .. "cc-code/main", "", { "code/highlighter/vscode.lua", "code/class.lua", + "code/clipboard.lua", "code/Code.lua", "code/Editor.lua", "code/Highlighter.lua",