| 1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include "textedit.h" |
| 6 | #include "style/lspclientkeeper.h" |
| 7 | #include "Document.h" |
| 8 | #include "SciLexer.h" |
| 9 | #include "common/common.h" |
| 10 | #include "framework/framework.h" |
| 11 | |
| 12 | #include <QMouseEvent> |
| 13 | #include <QKeyEvent> |
| 14 | #include <QDir> |
| 15 | #include <QDebug> |
| 16 | #include <QLibrary> |
| 17 | #include <QApplication> |
| 18 | #include <QTemporaryFile> |
| 19 | #include <QJsonDocument> |
| 20 | #include <QJsonObject> |
| 21 | #include <QJsonArray> |
| 22 | #include <QLabel> |
| 23 | #include <QRegularExpression> |
| 24 | #include <QCoreApplication> |
| 25 | #include <QTimer> |
| 26 | |
| 27 | #include <bitset> |
| 28 | #include <iostream> |
| 29 | |
| 30 | class TextEditPrivate |
| 31 | { |
| 32 | friend class TextEdit; |
| 33 | StyleLsp *styleLsp {nullptr}; |
| 34 | StyleSci *styleSci {nullptr}; |
| 35 | }; |
| 36 | |
| 37 | TextEdit::TextEdit(QWidget *parent) |
| 38 | : ScintillaEditExtern (parent) |
| 39 | , d (new TextEditPrivate) |
| 40 | { |
| 41 | QObject::connect(this, &ScintillaEditExtern::textInserted, this, |
| 42 | [=](Scintilla::Position position, |
| 43 | Scintilla::Position length, Scintilla::Position linesAdded, |
| 44 | const QByteArray &text, Scintilla::Position line){ |
| 45 | Q_UNUSED(position) |
| 46 | Q_UNUSED(length) |
| 47 | Q_UNUSED(linesAdded) |
| 48 | Q_UNUSED(text) |
| 49 | Q_UNUSED(line) |
| 50 | emit this->fileChanged(this->file()); |
| 51 | }, Qt::UniqueConnection); |
| 52 | |
| 53 | QObject::connect(this, &ScintillaEditExtern::textDeleted, this, |
| 54 | [=](Scintilla::Position position, |
| 55 | Scintilla::Position length, Scintilla::Position linesAdded, |
| 56 | const QByteArray &text, Scintilla::Position line){ |
| 57 | Q_UNUSED(position) |
| 58 | Q_UNUSED(length) |
| 59 | Q_UNUSED(linesAdded) |
| 60 | Q_UNUSED(text) |
| 61 | Q_UNUSED(line) |
| 62 | emit this->fileChanged(this->file()); |
| 63 | }, Qt::UniqueConnection); |
| 64 | |
| 65 | QObject::connect(this, &ScintillaEditExtern::saved, this, |
| 66 | &TextEdit::fileSaved, Qt::UniqueConnection); |
| 67 | setFocusPolicy(Qt::ClickFocus); |
| 68 | setAcceptDrops(true); |
| 69 | } |
| 70 | |
| 71 | TextEdit::~TextEdit() |
| 72 | { |
| 73 | // emit fileClosed(file()); |
| 74 | } |
| 75 | |
| 76 | void TextEdit::setFile(const QString &filePath) |
| 77 | { |
| 78 | ScintillaEditExtern::setFile(filePath); //顶层设置 |
| 79 | |
| 80 | // 设置正则匹配规则 |
| 81 | if (getStyleSci()) { |
| 82 | getStyleSci()->setLexer(); |
| 83 | getStyleSci()->setStyle(); |
| 84 | getStyleSci()->setMargin(); |
| 85 | getStyleSci()->setKeyWords(); |
| 86 | } |
| 87 | |
| 88 | QString currFileLanguage = fileLanguage(filePath); |
| 89 | if (supportLanguage() != currFileLanguage) { |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | if (getStyleLsp()) { |
| 94 | // 初始化所有lsp client设置 |
| 95 | getStyleLsp()->initLspConnection(); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void TextEdit::dragEnterEvent(QDragEnterEvent *event) |
| 100 | { |
| 101 | if (event->mimeData()->hasUrls()) { |
| 102 | event->acceptProposedAction(); |
| 103 | } else { |
| 104 | event->ignore(); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | void TextEdit::dropEvent(QDropEvent *event) |
| 109 | { |
| 110 | const QMimeData* mimeData = event->mimeData(); |
| 111 | if(mimeData->hasUrls()) { |
| 112 | QList<QUrl>urlList = mimeData->urls(); |
| 113 | QString fileName = urlList.at(0).toLocalFile(); |
| 114 | if(!fileName.isEmpty()) { |
| 115 | editor.openFile(fileName); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |