Skip to content

Commit

Permalink
new highlighter format
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelEinbinder committed Jan 8, 2024
1 parent d943cb1 commit 027af82
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
7 changes: 4 additions & 3 deletions slug/apps/kang/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ if (parsedArgs.values['test-highlight']) {
absolutePath,
relativePath,
newFile,
hasLanguageModel: !!doHighlight,
});
// TODO thread stdin in case it had some data in it before we went to web mode
// send a secret message and wait for it to come back.
Expand All @@ -116,16 +117,16 @@ function setupHighlight() {
terminal: false,
});
rl.on('line', line => {
const {id, tokens, error} = JSON.parse(line);
const {id, result, error} = JSON.parse(line);
if (error)
higlightCallbacks.get(id).reject(new Error(error));
else
higlightCallbacks.get(id).resolve(tokens);
higlightCallbacks.get(id).resolve(result);
});
process.on('beforeExit', () => {
commandChild.kill();
});
return (content, id) => {
commandChild.stdin.write(JSON.stringify({content, id}) + '\n');
commandChild.stdin.write(JSON.stringify({method: 'highlight', params: {content}, id}) + '\n');
};
}
2 changes: 1 addition & 1 deletion slug/apps/kang/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ const rpc = RPC(transport, {
}
});
window['editorForTest'] = editor;
updateEditorMode(true);
updateEditorMode(!params.hasLanguageModel);
editorContainer.append(editor.element);
editor.layout();
lastSavedVersion = editor.value;
Expand Down
6 changes: 4 additions & 2 deletions slug/editor/js/highlighter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class Highlighter extends Emitter<{
'highlight': {from: number, to: number};
}> {
private _selectionColors: {color?: string, background?: string};
private _mode: Mode<any>;
private _mode: Mode<any> | null;
private _lineInfo = new WeakMap<import('./model').Line, {state: any, tokens: Array<Token>}>();
private _currentLineNumber = 0;
private _requestLineNumber = 0;
Expand Down Expand Up @@ -176,6 +176,8 @@ export class Highlighter extends Emitter<{
}

indentation(lineNumber: number): number {
if (!this._mode)
return 0;
const line = this._model.line(lineNumber);
if (!this._lineInfo.has(line))
return 0;
Expand Down Expand Up @@ -265,7 +267,7 @@ export class Highlighter extends Emitter<{
}

hoverForLocation(loc: Loc): { content: string | Node, reposition: Loc } | null {
if (!this._mode.hover)
if (!this._mode?.hover)
return null;
const line = this._model.line(loc.line);
if (!line)
Expand Down
10 changes: 8 additions & 2 deletions slug/sdk/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,14 @@ class Transport {
this._sawSecret = true;
}
setImmediate(() => {
if (this.onmessage)
this.onmessage.call(null, JSON.parse(message));
let parsed;
try {
parsed = JSON.parse(message);
} catch {
console.error(JSON.stringify(message));
throw new Error('failed to parse json');
}
this.onmessage.call(null, parsed);
});
};
sendMessage(this._pendingMessage + buffer.toString(undefined, 0, end));
Expand Down

0 comments on commit 027af82

Please sign in to comment.