CodinGame / monaco-vscode-api

VSCode public API plugged on the monaco editor
MIT License
205 stars 29 forks source link

keybindings, addAction, Command not found #440

Closed Moros1138 closed 1 month ago

Moros1138 commented 1 month ago

I'm sure this is a skill issue but I am struggling hard to find what I'm doing wrong here.

        this.monacoWrapper.getEditor().addAction({
            id: "editor.action.build-and-run",
            label: "Build and Run",
            run: () =>
            {
                  console.log("IT WORKS!");
            }
        });

        await updateUserKeybindings(JSON.stringify([
            {
                "key": "ctrl+enter",
                "command": "editor.action.build-and-run",
                "when": "editorFocus"
            }
        ]));

F1 + Typing "Build and Run" finds it and runs it no problem

Typing Ctrl+Enter

Screenshot_20240602_033923

CGNonofr commented 1 month ago

Internally, addAction generates a random action is by prefixing the provided id. So it can't be referenced by the provided id directly.

The simplets way is to register your action via a VSCode extension

Moros1138 commented 1 month ago

I decided that magic numbers for the keybindings directly in the addAction function were gonna be my friends.

making an entire extension to run a trivial bit of code is beyond overkill for my project. Thanks for the response.

CGNonofr commented 1 month ago

The main point of this project is to be able to use the VSCode extension api, so out of curiosity, why are you using that project for?

Moros1138 commented 1 month ago

All of the examples I saw demonstrating the use of the TypeFox language client uses this project. I'm just following the examples. To directly answer the question: because I'm not presently aware of a viable alternative to get me

Beyond these features, I have absolutely no need for the rest of VS code. I gave Code Mirror's language server integration a shot and while it's functional, it's not nearly as polished an experience as this project provides.

I spent a lot of time going through monaco main repo. Looking in the issues for people who wanted what I wanted and every time someone asked about C/C++, they were told it wasn't supported directly and referred them to TypeFox, TypeFox refers to this project in pretty much every example they have in their mono repo.

CGNonofr commented 1 month ago

I see!

The language client npm module relies on the VSCode extension api to register its language features, so you already have everything needed to register your extension action/keybinding.

making an entire extension to run a trivial bit of code is beyond overkill for my project.

It may seems overkill, but this project goal is to make it as easy as possible

It can be done programmatically, there's no need to package it, something like:

import { ExtensionHostKind, registerExtension } from 'vscode/extensions'

const { getApi } = registerExtension({
  publisher: 'hello',
  name: 'world',
  version: '1.0.0',
  engines: {
    vscode: '*'
  },
  contributes: {
    commands: [{
      command: 'editor.action.build-and-run',
      title: 'Build and Run'
    }],
    keybindings: [{
      command: 'editor.action.build-and-run',
      key: 'ctrl+enter',
      when: 'editorFocus'
    }]
  },
}, ExtensionHostKind.LocalProcess)
getApi().then(api => {
  api.commands.registerTextEditorCommand('editor.action.build-and-run', (editor) => {
    console.log("IT WORKS!", editor);
  })
})
Moros1138 commented 1 month ago

Thank you for that. That doesn't look too bad.I will adapt that, instead of relying on numbers that might break later on. I'll close with a meme I made for my friends when I shown what I was doing. I hope it's worth a chuckle.

Screenshot_20240602_191549