microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
164.21k stars 29.29k forks source link

Monaco API request : CommonEditorRegistry #7250

Closed basarat closed 8 years ago

basarat commented 8 years ago

I want to integrate monaco into my own global command pallete in alm. I've done that with this code:

https://github.com/alm-tools/alm/blob/c49ca862f6c90b9ca2351fc47f92027494aea513/src/app/commands/monacoActionLoader.ts

import * as types from "../../common/types";
/**
 * This creates a dummy monaco editor just to get its actions
 */
export function getActions(): types.MonacoActionInformation[] {
    const elt = document.createElement('div');
    const editor = monaco.editor.create(elt,{},[]);

    // WARNING: This isn't documented or well exposed
    // but this is what `quickCommand.ts` (the built in command pallet) uses
    // It uses its on (injected) version but fortunately its on the editor as well
    const keybindingService = (editor as any)._keybindingService;
    // console.log(keybindingService); // DEBUG

    const actions = editor.getActions();

    let result = actions.map(a=>{
        // can have multiple (but in my experience they only had singles or 0)
        let keyboards = keybindingService.lookupKeybindings(a.id).map(k => keybindingService.getLabelFor(k));
        let keyboard = keyboards[0];

        // if there was any keyboard
        // map to it look nice for us
        // We don't actually need to use these bindings other than the display as trigger is done by `id`.
        let kbd = null;
        if (keyboard) {
            kbd = keyboard
                .replace('UpArrow','Up')
                .replace('DownArrow','Down')
                .replace('LeftArrow','Left')
                .replace('RightArrow','Right')
                ;
        }

        return {
            label: a.label,
            id: a.id,
            kbd: kbd
        }
    });

    editor.dispose();
    elt.remove();
    return result;
}

As you can see I've used hacks to get access to actions as well get access to _keybindingService. Would be great to get the

Thanks for all your great work. Looking forward to using the editor :rose:

/cc @alexandrudima :heart:

basarat commented 8 years ago

This can be closed as I have a workflow for easily exposing whatever internal I want as long as its shipped as a part of monaco :rose: