Setting editor text programmatically

I am wondering what would be the best way to modify the contents of an editor programatically (eg, like the way we invoke a snippet) in an extension

[original thread by Gautham Pai]

MonacoWorkspace.applyEdits should be good to modify contents safely

[SimonJohnCastle]

I’m inserting into the active editor from a tree view plugin, here is the basic code.

let editor = theia.window.activeTextEditor
if (editor) {
   if (editor.document.languageId == MY_LANGUAGE_SERVER_ID) {
     text = ............
     editor.insertSnippet(new theia.SnippetString(text));

[Karthik Bhat K]

My solution is below here

import { FileSystem } from "@theia/filesystem/lib/common";

@injectable()
export class someClass implements someOtherClassContribution {
    constructor(
        @inject(FileSystem) private readonly fileSystem: FileSystem
    ) {}

    async someFunction() {
         this.fileSystem.updateContent(this.fileSystem.getFileStat(uri), [
             {
                  range: {
                       start: {
                           line: start_line_number_value, 
                           character: start_character_number_value
                        },
                       end: {
                           line: end_line_number_value,
                           character: end_character_number_value
                       }
                  },
                  text: "sometext that will replace between specified line and characters",
                  rangeLength: length_of_text_to_replace
            }
         ], {
              encoding: 'utf-8'
         });
    }
}

The reason why I like this solution is that I can exactly specify where the edits should happen and how much code should exactly get edited.

The drawback of this is if the program is not written properly to replace texts or the user has made changes to code that you were not expecting then it will cause compile error or build errors in the program

@simonjohncastle What is theia.window.activeTextEditor? Do you program a Theia plugin?

@thebuzzycoder your solution won’t work with dirty editors, dirty editors ignore any changes on the filesystem and override them

[SimonJohnCastle]

Yes, I’m writing a TreeView Plugin where I can double click on items in the Tree or select insert from its context menu to perform a sort of DND insert into the current editor, Unfortunately there is a small bug https://github.com/eclipse-theia/theia/issues/6421 , but it works fine apart from that. I assumed the OP would be trying to do something similar from another plugin or extension

insertSnippet seems to be good then

[Karthik Bhat K]

@anton-kosyakov @simonjohncastle Thanks for the tip. I will try insertSnippet

[Karthik Bhat K]

This is for plugins. Is there something similar for extensions?

@simonjohncastle

EditorManager.activeEditor

[Karthik Bhat K]

Thanks. Turns out I can also use executeEdits

let currentEditor = this.editorManager.currentEditor
if (currentEditor !== undefined) {
    currentEditor.editor.executeEdits(textEdits)
} else {
    console.log('editor is undefined!')
}