Skip to content

Commit 3f26766

Browse files
committed
feat: print 多参数和 promise 支持;修复终端主题设置;
1 parent 098f08e commit 3f26766

16 files changed

+2008
-915
lines changed

CMakeLists.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,9 @@ set(CONTROL_SRC
210210
src/control/gotolinewidget.h
211211
src/control/gotolinewidget.cpp
212212
src/control/codeeditcontrolwidget.h
213-
src/control/codeeditcontrolwidget.cpp)
213+
src/control/codeeditcontrolwidget.cpp
214+
src/control/scrollablelabel.h
215+
src/control/scrollablelabel.cpp)
214216

215217
set(CLASS_SRC
216218
src/class/logger.cpp
@@ -356,7 +358,8 @@ set(SCRIPT_ADDON_SRC
356358
src/scriptaddon/scriptjson.h
357359
src/scriptaddon/scriptjson.cpp
358360
src/scriptaddon/scriptfile.cpp
359-
src/scriptaddon/scriptfile.h)
361+
src/scriptaddon/scriptfile.h
362+
src/scriptaddon/aspromise.hpp)
360363

361364
# localization support
362365
file(

lang/zh_CN/winghex_zh_CN.ts

Lines changed: 450 additions & 450 deletions
Large diffs are not rendered by default.

lang/zh_TW/winghex_zh_TW.ts

Lines changed: 450 additions & 450 deletions
Large diffs are not rendered by default.

src/class/aspreprocesser.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "aspreprocesser.h"
1919
#include "class/qascodeparser.h"
2020
#include "class/scriptmachine.h"
21+
#include "scriptaddon/aspromise.hpp"
2122

2223
#include <QDir>
2324
#include <QFileInfo>
@@ -28,7 +29,8 @@ Q_GLOBAL_STATIC_WITH_ARGS(
2829
({"__AS_ARRAY__", "__AS_ANY__", "__AS_GRID__", "__AS_HANDLE__",
2930
"__AS_MATH__", "__AS_WEAKREF__", "__AS_COROUTINE__", "__WING_FILE__",
3031
"__WING_STRING__", "__WING_COLOR__", "__WING_JSON__", "__WING_REGEX__",
31-
"__WING_DICTIONARY__"}));
32+
"__WING_DICTIONARY__", "__WING_PRINT_VAR__", "__WING_PRINT_LN__",
33+
"__AS_PROMISE__"}));
3234

3335
AsPreprocesser::AsPreprocesser(asIScriptEngine *engine) : engine(engine) {
3436
Q_ASSERT(engine);
@@ -656,6 +658,12 @@ int AsPreprocesser::processScriptSection(const QByteArray &script,
656658
}
657659
}
658660

661+
// asPromise co_await keyword expansion
662+
size_t len = modifiedScript.size();
663+
auto data = AsGeneratePromiseEntrypoints(modifiedScript.data(), &len);
664+
modifiedScript = QByteArray(data, len);
665+
asFreeMem(data);
666+
659667
// Build the actual script
660668
engine->SetEngineProperty(asEP_COPY_SCRIPT_SECTIONS, true);
661669

src/class/scriptmachine.cpp

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "class/qascodeparser.h"
3737
#include "class/settingmanager.h"
3838
#include "define.h"
39+
#include "scriptaddon/aspromise.hpp"
3940
#include "scriptaddon/scriptcolor.h"
4041
#include "scriptaddon/scriptfile.h"
4142
#include "scriptaddon/scriptjson.h"
@@ -142,6 +143,12 @@ bool ScriptMachine::isRunning(ConsoleMode mode) const {
142143
return _ctx.value(mode) != nullptr;
143144
}
144145

146+
#define INS_1 "const ?&in = null"
147+
#define INS_2 INS_1 ", " INS_1
148+
#define INS_4 INS_2 ", " INS_2
149+
#define INS_8 INS_4 ", " INS_4
150+
#define INS_16 INS_8 ", " INS_8
151+
145152
bool ScriptMachine::configureEngine() {
146153
if (_engine == nullptr) {
147154
return false;
@@ -211,9 +218,17 @@ bool ScriptMachine::configureEngine() {
211218
q_check_ptr(_engine->GetTypeInfoByName("color"));
212219

213220
// Register a couple of extra functions for the scripts
214-
r = _engine->RegisterGlobalFunction("void print(? &in obj)",
215-
asMETHOD(ScriptMachine, print),
216-
asCALL_THISCALL_ASGLOBAL, this);
221+
r = _engine->RegisterGlobalFunction(
222+
"void print(const ? &in obj, const ? &in = null," INS_16 ")",
223+
asFUNCTION(print), asCALL_GENERIC);
224+
Q_ASSERT(r >= 0);
225+
if (r < 0) {
226+
return false;
227+
}
228+
229+
r = _engine->RegisterGlobalFunction(
230+
"void println(const ? &in obj, const ? &in = null," INS_16 ")",
231+
asFUNCTION(println), asCALL_GENERIC);
217232
Q_ASSERT(r >= 0);
218233
if (r < 0) {
219234
return false;
@@ -341,18 +356,56 @@ void ScriptMachine::exceptionCallback(asIScriptContext *context) {
341356
}
342357
}
343358

344-
void ScriptMachine::print(void *ref, int typeId) {
359+
void ScriptMachine::print(asIScriptGeneric *args) {
345360
auto context = asGetActiveContext();
346361
if (context) {
347362
ConsoleMode mode = ConsoleMode(reinterpret_cast<asPWORD>(
348363
context->GetUserData(AsUserDataType::UserData_ContextMode)));
349364

365+
auto &m = ScriptMachine::instance();
366+
350367
MessageInfo info;
351368
info.mode = mode;
352369
info.type = MessageType::Print;
353-
info.message = _debugger->toString(ref, typeId, _engine);
354370

355-
outputMessage(info);
371+
for (int i = 0; i < args->GetArgCount(); ++i) {
372+
void *ref = args->GetArgAddress(i);
373+
int typeId = args->GetArgTypeId(i);
374+
375+
if (typeId) {
376+
info.message.append(
377+
m.debugger()->toString(ref, typeId, m.engine()));
378+
}
379+
}
380+
381+
m.outputMessage(info);
382+
}
383+
}
384+
385+
void ScriptMachine::println(asIScriptGeneric *args) {
386+
auto context = asGetActiveContext();
387+
if (context) {
388+
ConsoleMode mode = ConsoleMode(reinterpret_cast<asPWORD>(
389+
context->GetUserData(AsUserDataType::UserData_ContextMode)));
390+
391+
auto &m = ScriptMachine::instance();
392+
393+
MessageInfo info;
394+
info.mode = mode;
395+
info.type = MessageType::Print;
396+
397+
for (int i = 0; i < args->GetArgCount(); ++i) {
398+
void *ref = args->GetArgAddress(i);
399+
int typeId = args->GetArgTypeId(i);
400+
401+
if (typeId) {
402+
info.message
403+
.append(m.debugger()->toString(ref, typeId, m.engine()))
404+
.append('\n');
405+
}
406+
}
407+
408+
m.outputMessage(info);
356409
}
357410
}
358411

@@ -1963,6 +2016,7 @@ void ScriptMachine::registerEngineAddon(asIScriptEngine *engine) {
19632016
RegisterScriptFile(engine);
19642017
registerExceptionRoutines(engine);
19652018
registerEngineAssert(engine);
2019+
AsDirectPromise::Register(engine);
19662020
}
19672021

19682022
void ScriptMachine::registerEngineAssert(asIScriptEngine *engine) {

src/class/scriptmachine.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,9 @@ public slots:
160160
QString getCallStack(asIScriptContext *context);
161161

162162
private:
163-
void print(void *ref, int typeId);
163+
static void print(asIScriptGeneric *args);
164+
static void println(asIScriptGeneric *args);
165+
164166
QString getInput();
165167

166168
bool isType(asITypeInfo *tinfo, RegisteredType type);

src/control/scriptingconsole.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ void ScriptingConsole::applyScriptSettings() {
225225
auto dfont = QFont(set.consoleFontFamily());
226226
dfont.setPointSize(set.consoleFontSize());
227227

228-
auto thname = set.editorTheme();
228+
auto thname = set.consoleTheme();
229229
if (thname.isEmpty()) {
230230
switch (SkinManager::instance().currentTheme()) {
231231
case SkinManager::Theme::Dark:

src/control/scriptingconsolebase.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
/*==============================================================================
2+
** Copyright (C) 2024-2027 WingSummer
3+
**
4+
** This program is free software: you can redistribute it and/or modify it under
5+
** the terms of the GNU Affero General Public License as published by the Free
6+
** Software Foundation, version 3.
7+
**
8+
** This program is distributed in the hope that it will be useful, but WITHOUT
9+
** ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10+
** FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
11+
** details.
12+
**
13+
** You should have received a copy of the GNU Affero General Public License
14+
** along with this program. If not, see <https://www.gnu.org/licenses/>.
15+
** =============================================================================
16+
*/
17+
118
#include "scriptingconsolebase.h"
219
#include "class/wingconsolehighligher.h"
320
#include "wingsyntaxhighlighter.h"

src/control/scriptingconsolebase.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
/*==============================================================================
2+
** Copyright (C) 2024-2027 WingSummer
3+
**
4+
** This program is free software: you can redistribute it and/or modify it under
5+
** the terms of the GNU Affero General Public License as published by the Free
6+
** Software Foundation, version 3.
7+
**
8+
** This program is distributed in the hope that it will be useful, but WITHOUT
9+
** ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10+
** FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
11+
** details.
12+
**
13+
** You should have received a copy of the GNU Affero General Public License
14+
** along with this program. If not, see <https://www.gnu.org/licenses/>.
15+
** =============================================================================
16+
*/
17+
118
#ifndef SCRIPTINGCONSOLEBASE_H
219
#define SCRIPTINGCONSOLEBASE_H
320

src/control/scrollablelabel.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*==============================================================================
2+
** Copyright (C) 2024-2027 WingSummer
3+
**
4+
** This program is free software: you can redistribute it and/or modify it under
5+
** the terms of the GNU Affero General Public License as published by the Free
6+
** Software Foundation, version 3.
7+
**
8+
** This program is distributed in the hope that it will be useful, but WITHOUT
9+
** ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10+
** FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
11+
** details.
12+
**
13+
** You should have received a copy of the GNU Affero General Public License
14+
** along with this program. If not, see <https://www.gnu.org/licenses/>.
15+
** =============================================================================
16+
*/
17+
18+
#include "scrollablelabel.h"
19+
20+
#include <QPainter>
21+
#include <QScrollBar>
22+
#include <QWheelEvent>
23+
24+
ScrollableLabel::ScrollableLabel(QWidget *parent) : QScrollArea(parent) {
25+
setupUI();
26+
}
27+
28+
void ScrollableLabel::setText(const QString &text) {
29+
label.setText(text);
30+
updateLabelSize();
31+
}
32+
33+
QSize ScrollableLabel::sizeHint() const { return label.sizeHint(); }
34+
35+
QSize ScrollableLabel::minimumSizeHint() const {
36+
return label.minimumSizeHint();
37+
}
38+
39+
void ScrollableLabel::wheelEvent(QWheelEvent *event) {
40+
if (shouldScroll()) {
41+
QPoint delta = event->angleDelta();
42+
if (!delta.isNull()) {
43+
horizontalScrollBar()->setValue(horizontalScrollBar()->value() -
44+
delta.y());
45+
event->accept();
46+
return;
47+
}
48+
}
49+
event->ignore();
50+
}
51+
52+
void ScrollableLabel::resizeEvent(QResizeEvent *event) {
53+
QScrollArea::resizeEvent(event);
54+
updateLabelSize();
55+
}
56+
57+
void ScrollableLabel::setupUI() {
58+
setWidgetResizable(false);
59+
setFrameShape(QFrame::NoFrame);
60+
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
61+
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
62+
63+
label.setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
64+
label.setWordWrap(false);
65+
label.setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
66+
67+
setWidget(&label);
68+
}
69+
70+
void ScrollableLabel::updateLabelSize() {
71+
QFontMetrics fm(label.font());
72+
const int textWidth = fm.horizontalAdvance(label.text());
73+
const int textHeight = fm.height();
74+
75+
if (textWidth > width()) {
76+
label.setFixedSize(textWidth, textHeight);
77+
} else {
78+
label.setFixedSize(width(), textHeight);
79+
}
80+
}
81+
82+
bool ScrollableLabel::shouldScroll() const {
83+
return label.width() > viewport()->width();
84+
}

0 commit comments

Comments
 (0)