Detecting text input to VSCode activeTextEditor

Viewed 1586

I'm working on a VScode extension for my school open source project. I was wondering there was a way to detect text input to the activeTextEditor window?

For example if someone were to paste a string in, could I grab detect that string similar to an OnInput in JavaScript? A setup would be spell checking or doing a replacement for commands, similar to Visual Studios' intellisense you type prop +tab +tab it auto generates code.

Let me know if you've heard of something that might work. Thanks

1 Answers

The api you are looking for is vscode.workspace.onDidChangeTextDocument. This event is fired whenever a document that VS Code knows about changes. It is fired with a TextDocumentChangeEvent object which includes all the text changes:

import * as vscode from 'vscode'


export function activate() {
    vscode.workspace.onDidChangeTextDocument(changeEvent => {
        console.log(`Did change: ${changeEvent.document.uri}`);

        for (const change of changeEvent.contentChanges) {
             console.log(change.range); // range of text being replaced
             console.log(change.text); // text replacement
        }
   });
} 

If you only care about changes to the active editor's text, just check to see if changeEvent.document matches the active editor's document.

Related