-
-
Notifications
You must be signed in to change notification settings - Fork 195
Expand file tree
/
Copy pathtestBuilder.js
More file actions
181 lines (167 loc) · 7.19 KB
/
Copy pathtestBuilder.js
File metadata and controls
181 lines (167 loc) · 7.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
/*globals path*/
define(function (require, exports, module) {
const AppInit = brackets.getModule("utils/AppInit"),
DocumentManager = brackets.getModule("document/DocumentManager"),
EditorManager = brackets.getModule("editor/EditorManager"),
FileSystem = brackets.getModule("filesystem/FileSystem"),
Editor = brackets.getModule("editor/Editor"),
Dialogs = brackets.getModule("widgets/Dialogs"),
CommandManager = brackets.getModule("command/CommandManager"),
Commands = brackets.getModule("command/Commands"),
WorkspaceManager = brackets.getModule("view/WorkspaceManager"),
MacroRunner = require("./MacroRunner");
const BUILD_SCRATCH_FILE = path.join(brackets.app.getApplicationSupportDirectory(), "testBuilder.js");
let builderPanel, $panel, builderEditor;
function toggleTestBuilder() {
if(!$panel){
$panel = $(panelHTML);
builderPanel = WorkspaceManager.createBottomPanel("phcode-test-builder-panel", $panel, 100, "Test Builder");
builderPanel.hide();
_setupPanel().then(()=>{
builderPanel.setVisible(!builderPanel.isVisible());
});
return;
}
builderPanel.setVisible(!builderPanel.isVisible());
}
const panelHTML = `
<div id="test-builder-panel-phcode" class="bottom-panel vert-resizable top-resizer">
<div class="toolbar" style="display: flex; justify-content: space-between; align-items: center; padding: 5px 8px;">
<div style="display: flex; gap: 4px;">
<button class="btn btn-mini no-focus save-test-builder">Save</button>
<button class="btn btn-mini primary no-focus run-test-builder">Run</button>
<button class="btn btn-mini no-focus run-selected">Run Selected</button>
</div>
<div style="display: flex; gap: 4px;">
<button class="btn btn-mini no-focus mark-validate" title="Validate marks at cursor">Marks</button>
<button class="btn btn-mini no-focus cursor-locate">cursor</button>
<button class="btn btn-mini no-focus text-validate" title="validate text">Text</button>
</div>
</div>
<div style="display: flex; height: 100%; overflow: scroll;">
<div class="test_builder-editor" style="width: 100%; height: 100%;"></div>
</div>
</div>`;
function saveFile() {
return new Promise((resolve, reject) => {
CommandManager.execute(Commands.FILE_SAVE,
{doc: builderEditor.document})
.done(resolve)
.fail(function (openErr) {
console.error("error saving test builder file: ", BUILD_SCRATCH_FILE, openErr);
reject();
});
});
}
async function runTests(macroText) {
saveFile();
const errors = await MacroRunner.runMacro(macroText || builderEditor.document.getText());
if(errors.length) {
let errorHTML = "";
for (let error of errors) {
errorHTML += `${error.errorText}<br>`;
}
Dialogs.showErrorDialog("Error running macro: ", errorHTML);
}
}
function runSelection() {
return runTests(builderEditor.getSelectedText());
}
function _locateCursor() {
const editor = EditorManager.getActiveEditor();
if(!editor) {
return;
}
const formattedSelections = MacroRunner.computeCursors(editor, true);
builderEditor.replaceRange(`\n__PR.setCursors([${formattedSelections.join(", ")}]);`,
builderEditor.getCursorPos());
editor.focus();
}
function _validateText() {
const editor = EditorManager.getActiveEditor();
if(!editor) {
return;
}
const selection = editor.getSelection();
const start = selection.start, end = selection.end;
const selectionText = `${start.line+1}:${start.ch+1}-${end.line+1}:${end.ch+1}`;
let quotedString = editor.getSelectedText().replaceAll("\n", "\\n");
builderEditor.replaceRange(`\n__PR.validateText(\`${quotedString}\`, "${selectionText}");`,
builderEditor.getCursorPos());
editor.focus();
}
function _validateMarks(){
const editor = EditorManager.getActiveEditor();
if(!editor) {
return;
}
const marks = editor.findMarksAt(editor.getCursorPos()).filter(mark => mark.markType);
const markTypeMap = {};
for(let mark of marks){
if(!markTypeMap[mark.markType]){
markTypeMap[mark.markType] = [];
}
const loc = mark.find();
markTypeMap[mark.markType].push(`"${loc.from.line+1}:${loc.from.ch+1}-${loc.to.line+1}:${loc.to.ch+1}"`);
}
for(let markType of Object.keys(markTypeMap)) {
const selections = markTypeMap[markType];
builderEditor.replaceRange(`\n__PR.validateMarks("${markType}", [${selections.join(", ")}]);`,
builderEditor.getCursorPos());
}
}
async function _setupPanel() {
let file = FileSystem.getFileForPath(BUILD_SCRATCH_FILE);
let isExists = await file.existsAsync();
if(!isExists) {
await new Promise(resolve => {
file.write("", {blind: true}, resolve);
});
}
DocumentManager.getDocumentForPath(BUILD_SCRATCH_FILE).done(function (doc) {
const _$editor = $panel.find(".test_builder-editor");
builderEditor = new Editor.Editor(doc, false, _$editor, null, {});
builderEditor.updateLayout();
});
new ResizeObserver(()=>{
builderEditor && builderEditor.updateLayout();
}).observe($panel[0]);
$panel.find(".save-test-builder").click(saveFile);
$panel.find(".run-test-builder").click(()=>{
runTests();
});
$panel.find(".run-selected").click(runSelection);
$panel.find(".cursor-locate").click(_locateCursor);
$panel.find(".text-validate").click(_validateText);
$panel.find(".mark-validate").click(_validateMarks);
}
AppInit.appReady(function () {
if(Phoenix.isTestWindow) {
return;
}
$panel = $(panelHTML);
builderPanel = WorkspaceManager.createBottomPanel("phcode-test-builder-panel", $panel, 100, "Test Builder");
builderPanel.hide();
_setupPanel();
});
exports.toggleTestBuilder = toggleTestBuilder;
});