Skip to content

Commit

Permalink
Fixes the seq editor repeat arg issue and a typescript not registered… (
Browse files Browse the repository at this point in the history
#1142)

* Fixes the seq editor repeat arg issue and a typescript not registered error we were seeing

* Use the dispatched editor rather than creating a new one
  • Loading branch information
cohansen authored Mar 6, 2024
1 parent 4bbc97f commit 296abf4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 54 deletions.
39 changes: 28 additions & 11 deletions src/components/sequencing/SequenceEditor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
let commandDictionaryJson: AmpcsCommandDictionary | null = null;
let commandDictionaryTsFiles: TypeScriptFile[] = [];
let monaco: Monaco;
let editor: Editor.IStandaloneCodeEditor;
let sequenceEditorModel: Editor.ITextModel;
$: effects
Expand Down Expand Up @@ -65,6 +66,10 @@
a.click();
}
function editorCreated(event: CustomEvent<Editor.IStandaloneCodeEditor>) {
editor = event.detail;
}
function workerFullyLoaded(
event: CustomEvent<{ model: Editor.ITextModel; worker: languages.typescript.TypeScriptWorker }>,
) {
Expand All @@ -82,19 +87,30 @@
* Used to update the custom worker to use the selected command dictionary.
*/
async function workerUpdateModel() {
try {
const tsWorker = await monaco.languages.typescript.getTypeScriptWorker();
const worker = await tsWorker();
if (commandDictionaryJson && sequenceEditorModel) {
worker.updateModelConfig({
command_dict_str: JSON.stringify(commandDictionaryJson ?? {}),
model_id: sequenceEditorModel.id,
should_inject: true,
if (editor) {
try {
/**
* We don't have a way to check if the editor is initialized or not so it was throwing a "typescript not registered"
* error here. This is a hacky workaround to see if the editor is ready and we can load the typescript worker.
* :woozy:
*
* https://github.com/microsoft/monaco-editor/issues/115
*/
editor.onDidScrollChange(async () => {
const tsWorker = await monaco.languages.typescript.getTypeScriptWorker();
const worker = await tsWorker();
if (commandDictionaryJson && sequenceEditorModel) {
worker.updateModelConfig({
command_dict_str: JSON.stringify(commandDictionaryJson ?? {}),
model_id: sequenceEditorModel.id,
should_inject: true,
});
}
});
} catch (reason) {
console.log('Failed to pass the command dictionary to the custom worker.', reason);
}
} catch (reason) {
console.log('Failed to pass the command dictionary to the custom worker.', reason);
}
}
</script>
Expand Down Expand Up @@ -124,6 +140,7 @@
tabSize={2}
value={sequenceDefinition}
on:didChangeModelContent
on:editor={editorCreated}
on:fullyLoaded={workerFullyLoaded}
/>
</svelte:fragment>
Expand Down
3 changes: 3 additions & 0 deletions src/components/ui/MonacoEditor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
const dispatch = createEventDispatcher<{
didChangeModelContent: { e: Editor.IModelContentChangedEvent; value: string };
editor: Editor.IStandaloneCodeEditor;
fullyLoaded: { model: Editor.ITextModel; worker: TypeScriptWorker };
}>();
Expand Down Expand Up @@ -102,6 +103,8 @@
monaco = await import('monaco-editor');
editor = monaco.editor.create(div, options, override);
dispatch('editor', editor);
if (language && language === 'typescript') {
monaco.languages.typescript.typescriptDefaults.setWorkerOptions({
customWorkerPath: `${base}/customTS.worker.js`,
Expand Down
10 changes: 0 additions & 10 deletions src/workers/customCodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,4 @@ Suggestion: ${balancedTime}`,
UncaughtArgumentType: (): ErrorCode => {
return { id: CustomErrorCodes.Type.UNCAUGHT_ARG, message: `Error: Command Argument Type Mismatch` };
},
/**
* uncheckable warning code and message.
*/
UncheckableArgumentType: (): ErrorCode => {
return {
id: CustomErrorCodes.Type.UNCHECK_ARG,
message: `Warning: Not able to evaluate and validate identifier.
Suggestion: Use literals when you can`,
};
},
};
33 changes: 0 additions & 33 deletions src/workers/workerHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,29 +187,6 @@ export function findNextChildOfKind(
.find(child => child !== undefined);
}

/**
* Checks if a TypeScript AST node is a literal (string, number, boolean).
*
* @param {ts.Node} node - The TypeScript AST node to check.
* @returns {boolean} `true` if the node is a literal (string, number, boolean), `false` otherwise.
*
* @example
* const node = ... // some TypeScript AST node
* const result = isLiteral(node);
* console.log(result); // Outputs: true or false
*/
function isLiteral(node: tsc.Node): boolean {
switch (node.kind) {
case tsc.SyntaxKind.StringLiteral:
case tsc.SyntaxKind.NumericLiteral:
case tsc.SyntaxKind.TrueKeyword:
case tsc.SyntaxKind.FalseKeyword:
return true;
default:
return false;
}
}

/**
* Removes quotation marks from the input string.
*
Expand Down Expand Up @@ -424,16 +401,6 @@ export function validateArguments(
return false;
}

// ignore identifier as we need literals to do the checks
if (!isLiteral(argumentNode)) {
return makeDiagnostic(
CustomErrorCodes.UncheckableArgumentType(),
sourceFile,
argumentNode,
tsc.DiagnosticCategory.Warning,
);
}

let validation_resp: ValidationReturn;

switch (expected_argument.arg_type) {
Expand Down

0 comments on commit 296abf4

Please sign in to comment.