1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2026-03-12 04:41:54 +00:00

(re)format editors.js

This commit is contained in:
Fred Sauer
2026-02-14 19:05:27 -08:00
parent 4e79b95c56
commit 767de4834f

View File

@@ -8,13 +8,13 @@ import { hex, rpad } from "../../common/util";
declare var CodeMirror;
// helper function for editor
function jumpToLine(ed, i:number) {
var t = ed.charCoords({line: i, ch: 0}, "local").top;
function jumpToLine(ed, i: number) {
var t = ed.charCoords({ line: i, ch: 0 }, "local").top;
var middleHeight = ed.getScrollerElement().offsetHeight / 2;
ed.scrollTo(null, t - middleHeight - 5);
}
function createTextSpan(text:string, className:string) : HTMLElement {
function createTextSpan(text: string, className: string): HTMLElement {
var span = document.createElement("span");
span.setAttribute("class", className);
span.appendChild(document.createTextNode(text));
@@ -47,17 +47,17 @@ export var textMapFunctions = {
};
export class SourceEditor implements ProjectView {
constructor(path:string, mode:string) {
constructor(path: string, mode: string) {
this.path = path;
this.mode = mode;
}
path : string;
mode : string;
path: string;
mode: string;
editor;
updateTimer = null;
dirtylisting = true;
sourcefile : SourceFile;
currentDebugLine : SourceLocation;
sourcefile: SourceFile;
currentDebugLine: SourceLocation;
markCurrentPC; // TextMarker
markHighlight; // TextMarker
errormsgs = [];
@@ -66,16 +66,16 @@ export class SourceEditor implements ProjectView {
inspectWidget;
refreshDelayMsec = 300;
createDiv(parent:HTMLElement) {
createDiv(parent: HTMLElement) {
var div = document.createElement('div');
div.setAttribute("class", "editor");
parent.appendChild(div);
var text = current_project.getFile(this.path) as string;
var asmOverride = text && this.mode=='verilog' && /__asm\b([\s\S]+?)\b__endasm\b/.test(text);
var asmOverride = text && this.mode == 'verilog' && /__asm\b([\s\S]+?)\b__endasm\b/.test(text);
this.newEditor(div, asmOverride);
if (text) {
this.setText(text); // TODO: this calls setCode() and builds... it shouldn't
this.editor.setSelection({line:0,ch:0}, {line:0,ch:0}, {scroll:true}); // move cursor to start
this.editor.setSelection({ line: 0, ch: 0 }, { line: 0, ch: 0 }, { scroll: true }); // move cursor to start
}
this.setupEditor();
if (current_project.getToolForFilename(this.path).startsWith("remote:")) {
@@ -90,7 +90,7 @@ export class SourceEditor implements ProjectView {
}
}
newEditor(parent:HTMLElement, isAsmOverride?:boolean) {
newEditor(parent: HTMLElement, isAsmOverride?: boolean) {
var modedef = MODEDEFS[this.mode] || MODEDEFS.default;
var isAsm = isAsmOverride || modedef.isAsm;
var lineWrap = !!modedef.lineWrap;
@@ -116,7 +116,7 @@ export class SourceEditor implements ProjectView {
editorChanged() {
clearTimeout(this.updateTimer);
this.updateTimer = setTimeout( () => {
this.updateTimer = setTimeout(() => {
current_project.updateFile(this.path, this.editor.getValue());
}, this.refreshDelayMsec);
if (this.markHighlight) {
@@ -149,7 +149,7 @@ export class SourceEditor implements ProjectView {
inspectUnderCursor() {
var start = this.editor.getCursor(true);
var end = this.editor.getCursor(false);
if (start.line == end.line && start.ch < end.ch && end.ch-start.ch < 80) {
if (start.line == end.line && start.ch < end.ch && end.ch - start.ch < 80) {
var name = this.editor.getSelection();
this.inspect(name);
} else {
@@ -157,7 +157,7 @@ export class SourceEditor implements ProjectView {
}
}
inspect(ident : string) : void {
inspect(ident: string): void {
var result;
if (platform.inspect) {
result = platform.inspect(ident);
@@ -169,12 +169,12 @@ export class SourceEditor implements ProjectView {
if (result) {
var infospan = createTextSpan(result, "tooltipinfoline");
var line = this.editor.getCursor().line;
this.inspectWidget = this.editor.addLineWidget(line, infospan, {above:false});
this.inspectWidget = this.editor.addLineWidget(line, infospan, { above: false });
}
}
setText(text:string) {
var i,j;
setText(text: string) {
var i, j;
var oldtext = this.editor.getValue();
if (oldtext != text) {
this.editor.setValue(text);
@@ -192,60 +192,60 @@ export class SourceEditor implements ProjectView {
}
}
insertText(text:string) {
insertText(text: string) {
var cur = this.editor.getCursor();
this.editor.replaceRange(text, cur, cur);
}
highlightLines(start:number, end:number) {
highlightLines(start: number, end: number) {
//this.editor.setSelection({line:start, ch:0}, {line:end, ch:0});
var cls = 'hilite-span'
var markOpts = {className:cls, inclusiveLeft:true};
this.markHighlight = this.editor.markText({line:start,ch:0}, {line:end,ch:0}, markOpts);
this.editor.scrollIntoView({from:{line:start,ch:0}, to:{line:end,ch:0}});
var markOpts = { className: cls, inclusiveLeft: true };
this.markHighlight = this.editor.markText({ line: start, ch: 0 }, { line: end, ch: 0 }, markOpts);
this.editor.scrollIntoView({ from: { line: start, ch: 0 }, to: { line: end, ch: 0 } });
}
replaceSelection(start:number, end:number, text:string) {
replaceSelection(start: number, end: number, text: string) {
this.editor.setSelection(this.editor.posFromIndex(start), this.editor.posFromIndex(end));
this.editor.replaceSelection(text);
}
getValue() : string {
getValue(): string {
return this.editor.getValue();
}
getPath() : string { return this.path; }
getPath(): string { return this.path; }
addError(info: WorkerError) {
// only mark errors with this filename, or without any filename
if (!info.path || this.path.endsWith(info.path)) {
var numLines = this.editor.lineCount();
var line = info.line-1;
var line = info.line - 1;
if (isNaN(line) || line < 0 || line >= numLines) line = 0;
this.addErrorMarker(line, info.msg);
if (info.start != null) {
var markOpts = {className:"mark-error", inclusiveLeft:true};
var start = {line:line, ch:info.end?info.start:info.start-1};
var end = {line:line, ch:info.end?info.end:info.start};
var markOpts = { className: "mark-error", inclusiveLeft: true };
var start = { line: line, ch: info.end ? info.start : info.start - 1 };
var end = { line: line, ch: info.end ? info.end : info.start };
var mark = this.editor.markText(start, end, markOpts);
this.errormarks.push(mark);
}
}
}
addErrorMarker(line:number, msg:string) {
addErrorMarker(line: number, msg: string) {
var div = document.createElement("div");
div.setAttribute("class", "tooltipbox tooltiperror");
div.appendChild(document.createTextNode("\u24cd"));
this.editor.setGutterMarker(line, "gutter-info", div);
this.errormsgs.push({line:line, msg:msg});
this.errormsgs.push({ line: line, msg: msg });
// expand line widgets when mousing over errors
$(div).mouseover((e) => {
this.expandErrors();
});
}
addErrorLine(line:number, msg:string) {
addErrorLine(line: number, msg: string) {
var errspan = createTextSpan(msg, "tooltiperrorline");
this.errorwidgets.push(this.editor.addLineWidget(line, errspan));
}
@@ -257,7 +257,7 @@ export class SourceEditor implements ProjectView {
}
}
markErrors(errors:WorkerError[]) {
markErrors(errors: WorkerError[]) {
// TODO: move cursor to error line if offscreen?
this.clearErrors();
errors = errors.slice(0, MAX_ERRORS);
@@ -275,7 +275,7 @@ export class SourceEditor implements ProjectView {
while (this.errormarks.length) this.errormarks.shift().clear();
}
getSourceFile() : SourceFile { return this.sourcefile; }
getSourceFile(): SourceFile { return this.sourcefile; }
updateListing() {
// update editor annotations
@@ -288,21 +288,21 @@ export class SourceEditor implements ProjectView {
for (var info of lstlines) {
//if (info.path && info.path != this.path) continue;
if (info.offset >= 0) {
this.setGutter("gutter-offset", info.line-1, hex(info.offset&0xffff,4));
this.setGutter("gutter-offset", info.line - 1, hex(info.offset & 0xffff, 4));
}
if (info.insns) {
var insnstr = info.insns.length > 9 ? ("...") : info.insns;
this.setGutter("gutter-bytes", info.line-1, insnstr);
this.setGutter("gutter-bytes", info.line - 1, insnstr);
if (info.iscode) {
// TODO: labels trick this part?
if (info.cycles) {
this.setGutter("gutter-clock", info.line-1, info.cycles+"");
this.setGutter("gutter-clock", info.line - 1, info.cycles + "");
} else if (platform.getOpcodeMetadata) {
var opcode = parseInt(info.insns.split(" ")[0], 16);
var meta = platform.getOpcodeMetadata(opcode, info.offset);
if (meta && meta.minCycles) {
var clockstr = meta.minCycles+"";
this.setGutter("gutter-clock", info.line-1, clockstr);
var clockstr = meta.minCycles + "";
this.setGutter("gutter-clock", info.line - 1, clockstr);
}
}
}
@@ -310,7 +310,7 @@ export class SourceEditor implements ProjectView {
}
}
setGutter(type:string, line:number, text:string) {
setGutter(type: string, line: number, text: string) {
var lineinfo = this.editor.lineInfo(line);
if (lineinfo && lineinfo.gutterMarkers && lineinfo.gutterMarkers[type]) {
// do not replace existing marker
@@ -320,11 +320,11 @@ export class SourceEditor implements ProjectView {
}
}
setGutterBytes(line:number, s:string) {
this.setGutter("gutter-bytes", line-1, s);
setGutterBytes(line: number, s: string) {
this.setGutter("gutter-bytes", line - 1, s);
}
setTimingResult(result:CodeAnalyzer) : void {
setTimingResult(result: CodeAnalyzer): void {
this.editor.clearGutter("gutter-bytes");
if (this.sourcefile == null) return;
// show the lines
@@ -333,7 +333,7 @@ export class SourceEditor implements ProjectView {
let clocks = result.pc2clockrange[pc];
var minclocks = clocks && clocks.minclocks;
var maxclocks = clocks && clocks.maxclocks;
if (minclocks>=0 && maxclocks>=0) {
if (minclocks >= 0 && maxclocks >= 0) {
var s;
if (maxclocks == minclocks)
s = minclocks + "";
@@ -346,34 +346,34 @@ export class SourceEditor implements ProjectView {
}
}
setCurrentLine(line:SourceLocation, moveCursor:boolean) {
setCurrentLine(line: SourceLocation, moveCursor: boolean) {
var blocked = platform.isBlocked && platform.isBlocked();
var addCurrentMarker = (line:SourceLocation) => {
var addCurrentMarker = (line: SourceLocation) => {
var div = document.createElement("div");
var cls = blocked ? 'currentpc-marker-blocked' : 'currentpc-marker';
div.classList.add(cls);
div.appendChild(document.createTextNode("\u25b6"));
this.editor.setGutterMarker(line.line-1, "gutter-info", div);
this.editor.setGutterMarker(line.line - 1, "gutter-info", div);
}
this.clearCurrentLine(moveCursor);
if (line) {
addCurrentMarker(line);
if (moveCursor) {
this.editor.setCursor({line:line.line-1,ch:line.start||0}, {scroll:true});
this.editor.setCursor({ line: line.line - 1, ch: line.start || 0 }, { scroll: true });
}
var cls = blocked ? 'currentpc-span-blocked' : 'currentpc-span';
var markOpts = {className:cls, inclusiveLeft:true};
var markOpts = { className: cls, inclusiveLeft: true };
if (line.start || line.end)
this.markCurrentPC = this.editor.markText({line:line.line-1,ch:line.start}, {line:line.line-1,ch:line.end||line.start+1}, markOpts);
this.markCurrentPC = this.editor.markText({ line: line.line - 1, ch: line.start }, { line: line.line - 1, ch: line.end || line.start + 1 }, markOpts);
else
this.markCurrentPC = this.editor.markText({line:line.line-1,ch:0}, {line:line.line,ch:0}, markOpts);
this.markCurrentPC = this.editor.markText({ line: line.line - 1, ch: 0 }, { line: line.line, ch: 0 }, markOpts);
this.currentDebugLine = line;
}
}
clearCurrentLine(moveCursor:boolean) {
clearCurrentLine(moveCursor: boolean) {
if (this.currentDebugLine) {
this.editor.clearGutter("gutter-info");
if (moveCursor) this.editor.setSelection(this.editor.getCursor());
@@ -385,7 +385,7 @@ export class SourceEditor implements ProjectView {
}
}
getActiveLine() : SourceLocation {
getActiveLine(): SourceLocation {
if (this.sourcefile) {
var cpustate = lastDebugState && lastDebugState.c;
if (!cpustate && platform.getCPUState && !platform.isRunning())
@@ -398,7 +398,7 @@ export class SourceEditor implements ProjectView {
}
}
refreshDebugState(moveCursor:boolean) {
refreshDebugState(moveCursor: boolean) {
// TODO: only if line changed
// TODO: remove after compilation
this.clearCurrentLine(moveCursor);
@@ -424,20 +424,20 @@ export class SourceEditor implements ProjectView {
this.refreshListing();
this.refreshDebugState(moveCursor);
}
tick() {
this.refreshDebugState(false);
}
getLine(line : number) {
return this.editor.getLine(line-1);
getLine(line: number) {
return this.editor.getLine(line - 1);
}
getCurrentLine() : number {
return this.editor.getCursor().line+1;
getCurrentLine(): number {
return this.editor.getCursor().line + 1;
}
getCursorPC() : number {
getCursorPC(): number {
var line = this.getCurrentLine();
while (this.sourcefile && line >= 0) {
var pc = this.sourcefile.line2offset[line];
@@ -454,7 +454,7 @@ export class SourceEditor implements ProjectView {
toggleBreakpoint(lineno: number) {
// TODO: we have to always start at beginning of frame
if (this.sourcefile != null) {
var targetPC = this.sourcefile.line2offset[lineno+1];
var targetPC = this.sourcefile.line2offset[lineno + 1];
/*
var bpid = "pc" + targetPC;
if (platform.hasBreakpoint(bpid)) {
@@ -479,7 +479,7 @@ export class DisassemblerView implements ProjectView {
getDisasmView() { return this.disasmview; }
createDiv(parent : HTMLElement) {
createDiv(parent: HTMLElement) {
var div = document.createElement('div');
div.setAttribute("class", "editor");
parent.appendChild(div);
@@ -487,7 +487,7 @@ export class DisassemblerView implements ProjectView {
return div;
}
newEditor(parent : HTMLElement) {
newEditor(parent: HTMLElement) {
this.disasmview = CodeMirror(parent, {
mode: 'z80', // TODO: pick correct one
theme: 'cobalt',
@@ -524,17 +524,17 @@ export class DisassemblerView implements ProjectView {
*/
let bytes = "";
let comment = "";
for (let i=0; i<disasm.nbytes; i++)
bytes += hex(platform.readAddress(a+i));
for (let i = 0; i < disasm.nbytes; i++)
bytes += hex(platform.readAddress(a + i));
while (bytes.length < 14)
bytes += ' ';
let dstr = disasm.line;
if (addr2symbol && disasm.isaddr) { // TODO: move out
dstr = dstr.replace(/([^#])[$]([0-9A-F]+)/, (substr:string, ...args:any[]):string => {
dstr = dstr.replace(/([^#])[$]([0-9A-F]+)/, (substr: string, ...args: any[]): string => {
let addr = parseInt(args[1], 16);
let sym = addr2symbol[addr];
if (sym) return (args[0] + sym);
sym = addr2symbol[addr-1];
sym = addr2symbol[addr - 1];
if (sym) return (args[0] + sym + "+1");
return substr;
});
@@ -545,7 +545,7 @@ export class DisassemblerView implements ProjectView {
comment = "; " + sym;
}
}
let dline = hex(a, 4) + "\t" + rpad(bytes,14) + "\t" + rpad(dstr,30) + comment + "\n";
let dline = hex(a, 4) + "\t" + rpad(bytes, 14) + "\t" + rpad(dstr, 30) + comment + "\n";
s += dline;
if (a == pc) selline = curline;
curline++;
@@ -553,16 +553,16 @@ export class DisassemblerView implements ProjectView {
}
return s;
}
var startpc = pc < 0 ? pc-disasmWindow : Math.max(0, pc-disasmWindow); // for 32-bit PCs w/ hi bit set
let text = disassemble(startpc, pc-startpc) + disassemble(pc, disasmWindow);
var startpc = pc < 0 ? pc - disasmWindow : Math.max(0, pc - disasmWindow); // for 32-bit PCs w/ hi bit set
let text = disassemble(startpc, pc - startpc) + disassemble(pc, disasmWindow);
this.disasmview.setValue(text);
if (moveCursor) {
if (moveCursor) {
this.disasmview.setCursor(selline, 0);
}
jumpToLine(this.disasmview, selline);
}
getCursorPC() : number {
getCursorPC(): number {
var line = this.disasmview.getCursor().line;
if (line >= 0) {
var toks = this.disasmview.getLine(line).trim().split(/\s+/);
@@ -578,10 +578,10 @@ export class DisassemblerView implements ProjectView {
///
export class ListingView extends DisassemblerView implements ProjectView {
assemblyfile : SourceFile;
path : string;
assemblyfile: SourceFile;
path: string;
constructor(lstfn : string) {
constructor(lstfn: string) {
super();
this.path = lstfn;
}
@@ -589,7 +589,7 @@ export class ListingView extends DisassemblerView implements ProjectView {
refreshListing() {
// lookup corresponding assemblyfile for this file, using listing
var lst = current_project.getListingForFile(this.path);
// TODO?
// TODO?
this.assemblyfile = lst && (lst.assemblyfile || lst.sourcefile);
}
@@ -610,9 +610,9 @@ export class ListingView extends DisassemblerView implements ProjectView {
if (res) {
// set cursor while debugging
if (moveCursor) {
disasmview.setCursor(res.line-1, 0);
disasmview.setCursor(res.line - 1, 0);
}
jumpToLine(disasmview, res.line-1);
jumpToLine(disasmview, res.line - 1);
}
}
}