Skip to content

Commit

Permalink
Merge pull request #736 from SwiftFiddle/refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
kishikawakatsumi authored Dec 23, 2023
2 parents 30bb097 + 3ff66b4 commit ee744eb
Show file tree
Hide file tree
Showing 11 changed files with 252 additions and 333 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4
uses: actions/checkout@v4

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4
- uses: actions/checkout@v4

- name: Update Package.swift.json
run: |
Expand Down Expand Up @@ -66,7 +66,7 @@ jobs:
runs-on: ubuntu-latest
needs: build
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4
- uses: actions/checkout@v4

- uses: azure/setup-kubectl@v3
- uses: azure/login@v1
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/spm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
run:
runs-on: macos-latest
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4
- uses: actions/checkout@v4
- name: Build
run: |
set -ex
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4
- uses: actions/checkout@v4
- name: Build
run: |
set -ex
Expand Down
9 changes: 0 additions & 9 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,6 @@
"version": "1.2.0"
}
},
{
"package": "swift-backtrace",
"repositoryURL": "https://github.com/swift-server/swift-backtrace.git",
"state": {
"branch": null,
"revision": "80746bdd0ac8a7d83aad5d89dac3cbf15de652e6",
"version": "1.3.4"
}
},
{
"package": "swift-collections",
"repositoryURL": "https://github.com/apple/swift-collections.git",
Expand Down
170 changes: 12 additions & 158 deletions Public/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ export class App {
reader.readAsText(files[0], "UTF-8");
}

run() {
async run() {
if (runButton.classList.contains("disabled")) {
return;
}
Expand All @@ -313,16 +313,6 @@ export class App {

this.editor.clearMarkers();

this.terminal.saveCursorPosition();
this.terminal.switchAlternateBuffer();
this.terminal.moveCursorTo(0, 0);
this.terminal.hideCursor();

const altBuffer = [];
const cancelToken = this.terminal.showSpinner("Running", () => {
return altBuffer.filter(Boolean);
});

const params = {
toolchain_version: this.versionPicker.selected,
code: this.editor.getValue(),
Expand All @@ -340,10 +330,6 @@ export class App {
}

const runner = new Runner(this.terminal);
runner.onmessage = (message) => {
altBuffer.length = 0;
altBuffer.push(...this.parseMessage(message));
};

let stopRunner;
if (stopButton) {
Expand All @@ -354,154 +340,22 @@ export class App {
stopButton.addEventListener("click", stopRunner);
}

runner.run(params, (buffer, stderr, error, isCancel) => {
runButton.classList.remove("disabled");
if (stopButton) {
stopButton.classList.add("disabled");
}

document.getElementById("run-button-icon").classList.remove("d-none");
document.getElementById("run-button-spinner").classList.add("d-none");

this.terminal.hideSpinner(cancelToken);
this.terminal.switchNormalBuffer();
this.terminal.showCursor();
this.terminal.restoreCursorPosition();
this.terminal.reset();

if (isCancel) {
buffer = altBuffer.map((b) => `${b.text}\n`);
}

this.history.forEach((line) => {
const regex =
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g;
const plainText = line.replace(regex, "");
this.terminal.write(`\x1b[38;2;120;124;130m${plainText}\x1b[0m`); // #787C82
});
this.history.push(...buffer);

buffer.forEach((line) => {
this.terminal.write(line);
});

const markers = this.parseErrorMessage(stderr);
this.editor.updateMarkers(markers);
this.editor.focus();

if (stopButton) {
stopButton.removeEventListener("click", stopRunner);
}
});
}

parseMessage(message) {
const lines = [];

const data = JSON.parse(message);
const version = data.version;
const stderr = data.errors;
const stdout = data.output;
const markers = await runner.run(params);

if (version) {
lines.push(
...version
.split("\n")
.filter(Boolean)
.map((line) => {
return {
text: `\x1b[38;2;127;168;183m${line}\x1b[0m`, // #7FA8B7
numberOfLines: Math.ceil(line.length / this.terminal.cols),
};
})
);
}
if (stderr) {
lines.push(
...stderr
.split("\n")
.filter(Boolean)
.map((line) => {
return {
text: `\x1b[2m\x1b[37m${line}\x1b[0m`,
numberOfLines: Math.ceil(line.length / this.terminal.cols),
};
})
);
}
if (stdout) {
lines.push(
...stdout
.split("\n")
.filter(Boolean)
.map((line) => {
return {
text: `\x1b[37m${line}\x1b[0m`,
numberOfLines: Math.ceil(line.length / this.terminal.cols),
};
})
);
runButton.classList.remove("disabled");
if (stopButton) {
stopButton.classList.add("disabled");
}

return lines;
}
document.getElementById("run-button-icon").classList.remove("d-none");
document.getElementById("run-button-spinner").classList.add("d-none");

parseErrorMessage(message) {
const matches = message
.replace(
// Remove all ANSI colors/styles from strings
// https://stackoverflow.com/a/29497680/1733883
// https://github.com/chalk/ansi-regex/blob/main/index.js#L3
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g,
""
)
.matchAll(
/(?:\/main\.swift|<stdin>):(\d+):(\d+): (error|warning|note): ([\s\S]*?)\n*(?=(?:\/|$))/gi
);
return [...matches].map((match) => {
const row = +match[1];
let column = +match[2];
const text = match[4];
const type = match[3];
let severity;
switch (type) {
case "warning":
severity = 4; // monaco.MarkerSeverity.Warning;
break;
case "error":
severity = 8; // monaco.MarkerSeverity.Error;
break;
default: // monaco.MarkerSeverity.Info;
severity = 2;
break;
}

let length;
if (text.match(/~+\^~+/)) {
// ~~~^~~~
length = text.match(/~+\^~+/)[0].length;
column -= text.match(/~+\^/)[0].length - 1;
} else if (text.match(/\^~+/)) {
// ^~~~
length = text.match(/\^~+/)[0].length;
} else if (text.match(/~+\^/)) {
// ~~~^
length = text.match(/~+\^/)[0].length;
column -= length - 1;
} else if (text.match(/\^/)) {
// ^
length = 1;
}
this.editor.updateMarkers(markers);
this.editor.focus();

return {
startLineNumber: row,
startColumn: column,
endLineNumber: row,
endColumn: column + length,
message: text,
severity: severity,
};
});
if (stopButton) {
stopButton.removeEventListener("click", stopRunner);
}
}

saveEditState() {
Expand Down
10 changes: 1 addition & 9 deletions Public/js/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export class Console {
this.terminal.write("\x9B?47h");
}

showSpinner(message, progress) {
showSpinner(message) {
const self = this;
const startTime = performance.now();
const interval = 200;
Expand All @@ -149,17 +149,9 @@ export class Console {
spins++;
}

let numberOfLines = 0;
updateSpinner(message);
return setInterval(() => {
this.eraseLine();
const lines = progress();
this.eraseLines(numberOfLines);
numberOfLines = 0;
lines.forEach((buffer) => {
numberOfLines += buffer.numberOfLines;
this.terminal.writeln(buffer.text);
});
updateSpinner(message);
}, interval);
}
Expand Down
Loading

0 comments on commit ee744eb

Please sign in to comment.