Skip to content

Commit

Permalink
feat: add code coplilot
Browse files Browse the repository at this point in the history
with comment&generate feature

Log:
Change-Id: I2024bc8eaa6764f025b3a4dee1f20621b2aca349
  • Loading branch information
deepin-mozart committed Nov 7, 2023
1 parent d9c4ca3 commit a6038f4
Show file tree
Hide file tree
Showing 25 changed files with 525 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,10 @@ QRect ScreenRectangleForPoint(QPoint posGlobal)
{
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
const QScreen *screen = QGuiApplication::screenAt(posGlobal);
return screen->availableGeometry();
// added by mozart, to void crash when screen is null.
if (screen)
return screen->availableGeometry();
return {};
#else
const QDesktopWidget *desktop = QApplication::desktop();
return desktop->availableGeometry(posGlobal);
Expand Down
2 changes: 2 additions & 0 deletions src/common/util/eventdefinitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ OPI_OBJECT(editor,
OPI_INTERFACE(replaceText, "text", "target", "repalceType")
OPI_INTERFACE(switchContext, "name")
OPI_INTERFACE(switchWorkspace, "name")
OPI_INTERFACE(contextMenu, "menu")
OPI_INTERFACE(keyPressEvent, "event")
)

OPI_OBJECT(symbol,
Expand Down
34 changes: 32 additions & 2 deletions src/plugins/codeeditor/codeeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "services/window/windowservice.h"
#include "services/language/languageservice.h"
#include "services/editor/editorservice.h"
#include "common/widget/outputpane.h"

#include <QAction>
Expand Down Expand Up @@ -57,13 +58,13 @@ bool CodeEditor::start()
auto &ctx = dpfInstance.serviceContext();
WindowService *windowService = ctx.service<WindowService>(WindowService::name());

TextEditSplitter *editManager = TextEditSplitter::instance();
using namespace std::placeholders;
if (windowService) {
NavEditMainWindow *navEditWindow = NavEditMainWindow::instance();
TextEditSplitter *editManager = TextEditSplitter::instance();
navEditWindow->setWidgetEdit(new AbstractCentral(editManager));
windowService->addCentralNavigation(MWNA_EDIT, new AbstractCentral(navEditWindow));

using namespace std::placeholders;
if (!windowService->addWidgetWorkspace) {
windowService->addWidgetWorkspace = std::bind(&NavEditMainWindow::addWidgetWorkspace, navEditWindow, _1, _2);
}
Expand Down Expand Up @@ -101,6 +102,35 @@ bool CodeEditor::start()
windowService->addContextWidget(tr("&Application Output"), new AbstractWidget(OutputPane::instance()), "Application");
}

QString errStr;
if (!ctx.load(dpfservice::EditorService::name(), &errStr)) {
qCritical() << errStr;
}
EditorService *editorService = dpfGetService(EditorService);
if (editorService) {
if (!editorService->getSelectedText) {
editorService->getSelectedText = std::bind(&TextEditSplitter::getSelectedText, editManager);
}
if (!editorService->getCursorBeforeText) {
editorService->getCursorBeforeText = std::bind(&TextEditSplitter::getCursorBeforeText, editManager);
}
if (!editorService->getCursorAfterText) {
editorService->getCursorAfterText = std::bind(&TextEditSplitter::getCursorAfterText, editManager);
}
if (!editorService->replaceSelectedText) {
editorService->replaceSelectedText = std::bind(&TextEditSplitter::replaceSelectedText, editManager, _1);
}
if (!editorService->showTips) {
editorService->showTips = std::bind(&TextEditSplitter::showTips, editManager, _1);
}
if (!editorService->insertText) {
editorService->insertText = std::bind(&TextEditSplitter::insertText, editManager, _1);
}
if (!editorService->undo) {
editorService->undo = std::bind(&TextEditSplitter::undo, editManager);
}
}

return true;
}

Expand Down
1 change: 1 addition & 0 deletions src/plugins/codeeditor/mainframe/naveditmainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ void NavEditMainWindow::addWidgetWorkspace(const QString &title, AbstractWidget
qDockWidgetWorkspace->setFeatures(QDockWidget::DockWidgetMovable);
qDockWidgetWorkspace->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
addDockWidget(Qt::DockWidgetArea::LeftDockWidgetArea, qDockWidgetWorkspace);
setCorner(Qt::BottomLeftCorner, Qt::DockWidgetArea::LeftDockWidgetArea);
qDockWidgetWorkspace->setWidget(qTabWidgetWorkspace);
}

Expand Down
12 changes: 11 additions & 1 deletion src/plugins/codeeditor/mainframe/texteditkeeper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@

TextEdit *TextEditKeeper::create(const QString &language, QString *err)
{
return instance()->editFactory.create(language, err);
TextEdit *textEdit = instance()->editFactory.create(language, err);
connect(textEdit, &TextEdit::focusChanged, [=](bool focused){
if (focused)
instance()->activeTextEdit = textEdit;
});
return textEdit;
}

void TextEditKeeper::setAnalysedLanguage(const QString &lang)
Expand Down Expand Up @@ -103,3 +108,8 @@ void TextEditKeeper::removeProjectInfo(const dpfservice::ProjectInfo &info)
instance()->proInfo = dpfservice::ProjectInfo();
}
}

TextEdit *TextEditKeeper::getActiveTextEdit()
{
return instance()->activeTextEdit;
}
10 changes: 7 additions & 3 deletions src/plugins/codeeditor/mainframe/texteditkeeper.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,25 @@
#include <framework/service/qtclassfactory.h>
#include <framework/service/qtclassmanager.h>

class TextEditKeeper final
class TextEditKeeper : public QObject
{
Q_OBJECT

dpf::QtClassFactory<TextEdit> editFactory;
QString analysedLanguage;
QString analysedWorkspace;
QString analysedStorage;
AnalysedData data;
dpfservice::ProjectInfo proInfo;
TextEdit *activeTextEdit = nullptr;
TextEditKeeper(){}

public:
inline static TextEditKeeper *instance(){
static TextEditKeeper ins;
return &ins;
}

public:

template<class Edit>
static bool impl(const QString &language = Edit::implLanguage(), QString *err = nullptr) {
bool result = true;
Expand Down Expand Up @@ -55,6 +57,8 @@ class TextEditKeeper final
static dpfservice::ProjectInfo projectInfo();
static void saveProjectInfo(const dpfservice::ProjectInfo &info);
static void removeProjectInfo(const dpfservice::ProjectInfo &info);

static TextEdit *getActiveTextEdit();
};

#endif // TEXTEDITKEEPER_H
55 changes: 52 additions & 3 deletions src/plugins/codeeditor/textedittabwidget/scintillaeditextern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ void ScintillaEditExtern::replaceRange(Scintilla::Position start,
clearSelections();
setSelectionStart(start);
setSelectionEnd(end);
replaceSel(text.toLatin1());
auto stdString = text.toStdString();
replaceSel(stdString.c_str());
emit replaceed(file(), start, end, text);
}

Expand Down Expand Up @@ -531,7 +532,7 @@ void ScintillaEditExtern::sciUpdateAnnotation()

void ScintillaEditExtern::keyReleaseEvent(QKeyEvent *event)
{
return ScintillaEdit::keyReleaseEvent(event);
ScintillaEdit::keyReleaseEvent(event);
}

void ScintillaEditExtern::keyPressEvent(QKeyEvent *event)
Expand All @@ -541,7 +542,10 @@ void ScintillaEditExtern::keyPressEvent(QKeyEvent *event)
if (isKeyCtrl && isKeyS) {
saveText();
}
return ScintillaEdit::keyPressEvent(event);

editor.keyPressEvent(event->key());

ScintillaEdit::keyPressEvent(event);
}

void ScintillaEditExtern::sciMarginClicked(Scintilla::Position position, Scintilla::KeyMod modifiers, int margin)
Expand All @@ -563,9 +567,54 @@ void ScintillaEditExtern::sciMarginClicked(Scintilla::Position position, Scintil
}
}

QString ScintillaEditExtern::getSelectedText()
{
return getSelText();
}

QString ScintillaEditExtern::getCursorLineText()
{
int lineMaxCharacter = 100;
return getCurLine(lineMaxCharacter);
}

QString ScintillaEditExtern::getCursorBeforeText()
{
int curPosition = static_cast<int>(currentPos());
return textRange(0, curPosition);
}

QString ScintillaEditExtern::getCursorAfterText()
{
int curPosition = static_cast<int>(currentPos());
return textRange(curPosition, static_cast<int>(length()));
}

void ScintillaEditExtern::replaceSelectedRange(const QString &text)
{
replaceRange(selectionStart(), selectionEnd(), text);
}

void ScintillaEditExtern::showTips(const QString &tips)
{
sptr_t pos = currentPos();
auto stdString = tips.toStdString();
callTipShow(pos, stdString.c_str());
}

void ScintillaEditExtern::insertText(const QString &text)
{
auto curPos = currentPos();
auto stdString = text.toStdString();
ScintillaEdit::insertText(curPos, stdString.c_str());
auto cursorNewPos = curPos + static_cast<sptr_t>(stdString.size());
gotoPos(cursorNewPos);
}

void ScintillaEditExtern::focusInEvent(QFocusEvent *event)
{
focusChanged(true);

return ScintillaEdit::focusInEvent(event);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ class ScintillaEditExtern : public ScintillaEdit
void find(const QString &srcText, int operateType);
void replace(const QString &srcText, const QString &destText, int operateType);

// Mozart added.
QString getSelectedText();
QString getCursorLineText();
QString getCursorBeforeText();
QString getCursorAfterText();
void replaceSelectedRange(const QString &text);
void showTips(const QString &tips);
void insertText(const QString &text);

signals:
void hovered(Scintilla::Position position);
void hoverCleaned(Scintilla::Position position);
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/codeeditor/textedittabwidget/style/stylelsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,9 @@ void StyleLsp::sciSelectionMenu(QContextMenuEvent *event)
}
});

// notify other plugin to add action.
editor.contextMenu(QVariant::fromValue(&contextMenu));

contextMenu.move(showPos);
contextMenu.exec();
}
Expand Down
6 changes: 6 additions & 0 deletions src/plugins/codeeditor/textedittabwidget/textedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "SciLexer.h"
#include "common/common.h"
#include "framework/framework.h"
#include "services/editor/editorservice.h"

#include <QMouseEvent>
#include <QKeyEvent>
Expand All @@ -34,6 +35,7 @@ class TextEditPrivate
StyleSci *styleSci {nullptr};
};

using namespace dpfservice;
TextEdit::TextEdit(QWidget *parent)
: ScintillaEditExtern (parent)
, d (new TextEditPrivate)
Expand All @@ -48,6 +50,10 @@ TextEdit::TextEdit(QWidget *parent)
Q_UNUSED(text)
Q_UNUSED(line)
emit this->fileChanged(this->file());
auto service = dpfGetService(EditorService);
if (service) {
emit service->fileChanged();
}
}, Qt::UniqueConnection);

QObject::connect(this, &ScintillaEditExtern::textDeleted, this,
Expand Down
64 changes: 64 additions & 0 deletions src/plugins/codeeditor/textedittabwidget/texteditsplitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "texteditsplitter.h"
#include "transceiver/codeeditorreceiver.h"
#include "mainframe/texteditkeeper.h"

#include <QBoxLayout>
#include <QDebug>
Expand Down Expand Up @@ -52,6 +53,69 @@ TextEditSplitter::TextEditSplitter(QWidget *parent)

}

QString TextEditSplitter::getSelectedText()
{
auto edit = TextEditKeeper::instance()->getActiveTextEdit();
if (!edit)
return "";

return edit->getSelectedText();
}

QString TextEditSplitter::getCursorBeforeText()
{
auto edit = TextEditKeeper::instance()->getActiveTextEdit();
if (!edit)
return "";

return edit->getCursorBeforeText();
}

QString TextEditSplitter::getCursorAfterText()
{
auto edit = TextEditKeeper::instance()->getActiveTextEdit();
if (!edit)
return "";

return edit->getCursorAfterText();
}

void TextEditSplitter::replaceSelectedText(const QString &text)
{
auto edit = TextEditKeeper::instance()->getActiveTextEdit();
if (!edit)
return;

edit->replaceSelectedRange(text);
}

void TextEditSplitter::showTips(const QString &tips)
{
auto edit = TextEditKeeper::instance()->getActiveTextEdit();
if (!edit)
return;

edit->showTips(tips);
}

void TextEditSplitter::insertText(const QString &text)
{
auto edit = TextEditKeeper::instance()->getActiveTextEdit();
if (!edit)
return;

edit->insertText(text);
}

void TextEditSplitter::undo()
{
auto edit = TextEditKeeper::instance()->getActiveTextEdit();
if (!edit)
return;

edit->undo();
}

TextEditSplitter::~TextEditSplitter()
{

Expand Down
8 changes: 8 additions & 0 deletions src/plugins/codeeditor/textedittabwidget/texteditsplitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ class TextEditSplitter : public QWidget
explicit TextEditSplitter(QWidget *parent = nullptr);
QSplitter *getSplitter() const;

QString getSelectedText();
QString getCursorBeforeText();
QString getCursorAfterText();
void replaceSelectedText(const QString &text);
void showTips(const QString &tips);
void insertText(const QString &text);
void undo();

public slots:
void doSplit(Qt::Orientation orientation, const newlsp::ProjectKey &key, const QString &file);
void doClose();
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/codeeditor/textedittabwidget/textedittabbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "textedittabbar.h"
#include "common/common.h"
#include "services/editor/editorservice.h"

#include <QFileInfo>
#include <QDebug>
#include <QMessageBox>
Expand Down Expand Up @@ -145,6 +147,7 @@ void TextEditTabBar::doFileChanged(const QString &file)

d->tab->setTabText(index , "*" + d->tab->tabText(index));
qInfo() << d->tab->tabText(index);

}

void TextEditTabBar::doFileSaved(const QString &file)
Expand Down
Loading

0 comments on commit a6038f4

Please sign in to comment.