javascript-code-editor.ts

46 lines | 2.108 kB Blame History Raw Download
import { CodeEditor } from "./code-editor";
import * as monaco from 'monaco-editor';
import initScript from '!!raw-loader!./injects/editor-init.js';
import spiralBoxesSolutionScript from '!!raw-loader!./injects/spiral-boxes-solution.js';
import declarations from '!!raw-loader!./injects/declarations.d.ts';

export class JavascriptCodeEditor extends CodeEditor {
    setInitInput(): string {
        return initScript;
    }

    setInitSettings(): void {
        monaco.languages.typescript.javascriptDefaults.addExtraLib(declarations, 'ts:filename/declarations.d.ts');

        monaco.languages.registerCompletionItemProvider("javascript", {
            provideCompletionItems: (model, position, context, token) => {
                return {
                    suggestions: [
                        {
                            label: 'for',
                            kind: monaco.languages.CompletionItemKind.Snippet,
                            insertText: 'for (let i = 0; i < ${1:array}.length; i++) {\n\t$0\n}',
                            range: <any>null,
                            insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet
                        },
                        {
                            label: 'forr',
                            kind: monaco.languages.CompletionItemKind.Snippet,
                            insertText: 'for (var i = ${1:array}.length - 1; i >= 0; i--) {\n\t$0\n}',
                            range: <any>null,
                            insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet
                        },
                        {
                            label: 'spiralSolutionSnippet',
                            detail: "",
                            kind: monaco.languages.CompletionItemKind.Snippet,
                            insertText: spiralBoxesSolutionScript,
                            range: <any>null,
                            insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet
                        }
                    ]
                }
            }
        });
    }
}