monaco-editor icon indicating copy to clipboard operation
monaco-editor copied to clipboard

[Bug] inlineEdit.trigger action not working as expected

Open yug2005 opened this issue 7 months ago • 2 comments

Reproducible in vscode.dev or in VS Code Desktop?

  • [x] Not reproducible in vscode.dev or VS Code Desktop

Reproducible in the monaco editor playground?

Monaco Editor Playground Link

https://microsoft.github.io/monaco-editor/playground.html?source=v0.52.2#XQAAAALPBAAAAAAAAABBqQkHQ5NjdMjwa-jY7SIQ9S7DNlzs5W-mwj0fe1ZCDRFc9ws9XQE0SJE1jc2VKxhaLFIw9vEWSxW3yscw7M2LAc4m7jWi3uGQetsfyL9Rw59E2COVxnxnvCRAg0kwCdQb8ogwu-POzKkkdtD5TzWjqVrEVwEMSpGc43m1G4E_3xsiuf3S4-OZgeIvwsKN0nE2W8eOTUl8_QtBLL1Z0xGP3vXvPEHt7hIFgxMCxMGY69x15CB0GVkSHiiihL0y_1YjXmgaT2QNdBJ5zDh0geQXWUCCCqXaa6QinRJckhA8Dz0a7L1Xc9guVk5LLib4E8eDTDpuEE5uXKTOHEIQ0qRcFeWs70AOZAV_VV2aeqPNhLA0bC2taNREup__RQG1viB9ypLGAgU74eEAAQJK4NQkYEYR7XyUvlAMBcEr-pZG0oRQRimXk7mF_wglLYnSkpQGy73LYppYV4oJirgJ8JgIwp28DBN95nOOBlPmac2jbLthpbvp5ekMqDoTR4cY1NXzkPTHQfzpu3-op3bocElIf-3RWxp5Kve7b-vFoGaCzv5GFtoU5Vbkr3KsgfmqYJVvynhTlMdYZrsJYA1hwW53qOTw2SH6OcCjyYwwQE2BFFX7K8rpHRNnxbihTG3zrMb6uZ_6rGkhyRe0kMgPzc0SPPIJkrvL660dTmKJzk0dJ4zNajjI7bwjOupk6FGbhYUaGD1564iMs41U1E4JLH69-yiCvQ_45tK7ciBjThCPYIrDdDWdzbvVJfrC6VC9KYgim25VfSCaAFbtNZR2nnVoTTTXp6ApfYKZd96fwkvoDB17ByAGBF8rxL9FZTnqjiZjJyL30g4Inf7nRtI

Monaco Editor Playground Code

const value = /* set from `myEditor.getModel()`: */ `// This is some existing text `;

// Hover on each property to see its docs!
const myEditor = monaco.editor.create(document.getElementById("container"), {
	value,
	language: "javascript",
	automaticLayout: true,
});

monaco.languages.registerInlineEditProvider("javascript", {
	provideInlineEdit: async (model, context, token) => {
		console.log("provideInlineEdit called with ", context);
		const position = myEditor.getPosition();
		return Promise.resolve({
			text: "insert text",
			range: {
                                startLineNumber: position.lineNumber,
                                endLineNumber: position.lineNumber,
                                startColumn: position.column,
                                endColumn: position.column,
			}
		})
	},
	freeInlineEdit: (args) => {}
});

myEditor.updateOptions({
	experimentalInlineEdit: {
		enabled: true,
		showToolbar: "always"
	}
});

myEditor.onDidChangeCursorPosition(() => {
	console.log("Cursor position changed: ", myEditor.getPosition());
	console.log("Triggering inline edit");
	myEditor.trigger("", "editor.action.inlineEdit.trigger", {});
	myEditor.trigger("", "editor.action.inlineEdits.trigger", {});
});

Reproduction Steps

Try moving the cursor in the monaco editor (in the playground) and even though the triggers are being called on every cursor change, the provideInlineEdit function does not get called.

I am trying the triggers: "editor.action.InlineEdit.trigger" and "editor.action.InlineEdits.trigger".

Actual (Problematic) Behavior

The triggers: "editor.action.InlineEdit.trigger" and "editor.action.InlineEdits.trigger" are being called on the cursor change event but it is invoking the provideInlineEdit function defined by the provider on every cursor change.

Expected Behavior

The provideInlineEdit function should get triggered every time the triggers: "editor.action.InlineEdit.trigger" and "editor.action.InlineEdits.trigger" are invoked.

Additional Context

I am trying to implement AI inline edits using this functionality but it is not working as expected. I believe that the inline edit feature is still in the experimental stage.

yug2005 avatar Apr 02 '25 23:04 yug2005

This might help you — it worked for me using monaco.languages.registerInlineEditProvider with a simple logic. Just make sure the experimental options are enabled in your editor config, like this:

myEditor.updateOptions({
  inlineSuggest: {
    enabled: true,
    showToolbar: 'onHover',
    mode: 'subword',
    suppressSuggestions: false,
  },
  experimentalInlineEdit: {
    enabled: true,
    showToolbar: 'always',
  }
});

And this is the provider that worked in my case:

monaco.languages.registerInlineEditProvider('ppl', {
  provideInlineEdit(model, context, token) {
    const edits: monaco.languages.IInlineEdit[] = [];

    const totalLines = model.getLineCount();
    for (let lineNumber = 1; lineNumber <= totalLines; lineNumber++) {
      const lineContent = model.getLineContent(lineNumber);
      const commentIndex = lineContent.indexOf('//');
      const codePart = commentIndex === -1 ? lineContent : lineContent.substring(0, commentIndex);

      if (codePart.trimStart().startsWith('PRINT(') && !codePart.trimEnd().endsWith(';')) {
        edits.push({
          range: new monaco.Range(lineNumber, codePart.length + 1, lineNumber, codePart.length + 1),
          text: ';',
        });
      }
    }

    return edits.length > 0 ? edits[0] : null;
  },

  freeInlineEdit(edit) {
    console.log('Inline edit released:', edit);
  },
});

Also, you don’t need to force position — it's not included in the context, and I didn’t need it to make this work. Just use the model to analyze and apply changes.

Hope it helps — it worked for me!

Image

hpvcodeDev avatar Apr 10 '25 18:04 hpvcodeDev

That does not work for what I am trying to accomplish. The behaviour I want is whenever the cursor changes, there a new inline edit suggested. I am trying to implement this by calling the trigger editor.action.inlineEdit.trigger in onDidChangeCursorPosition so that the provideInlineEdit is called when the cursor changes positions. The problem is that the trigger just does not work as it is supposed to.

yug2005 avatar May 08 '25 00:05 yug2005