Skip to content

activeTextEditor not correct while the extension is activated #38

@aeschli

Description

@aeschli
  • create the following extension (example for our doc)
  • run the extension. In a workspace create a md file, open it and check that you get the counter in the status bar
  • now reload the workspace while the md file is open. Observer that the md file opens, but the counter is not shown

The extension activates on "onLanguage:markdown".
When it starts, it prints the current active mode: vs.editor.modes.nullMode
It registers for active editor change event, but doesn't get any events that tells it that a markdown file has opened.

import {window, commands, Disposable, ExtensionContext, StatusBarAlignment, StatusBarItem, TextDocument} from 'vscode';

// This method is called when your extension is activated. Activation is
// controlled by the activation events defined in package.json.
export function activate(context: ExtensionContext) {

    // Use the console to output diagnostic information (console.log) and errors (console.error).
    // This line of code will only be executed once when your extension is activated.
    console.log('Congratulations, your extension "WordCount" is now active!');

    // create a new word counter
    let wordCounter = new WordCounter();
    let controller = new WordCounterController(wordCounter);

    // Add to a list of disposables which are disposed when this extension is deactivated.
    context.subscriptions.push(controller);
    context.subscriptions.push(wordCounter);
}

class WordCounter {

    private _statusBarItem: StatusBarItem;

    public updateWordCount() {

        // Create as needed 
        if (!this._statusBarItem) { 
            this._statusBarItem = window.createStatusBarItem(StatusBarAlignment.Left); 
        }  

        // Get the current text editor 
        let editor = window.activeTextEditor; 
        if (!editor) { 
            this._statusBarItem.hide(); 
            return; 
        } 

         let doc = editor.document; 

        console.log('Current doc language ' + doc.languageId);

        // Only update status if an MarkDown file 
        if (doc.languageId === "markdown") { 
            let wordCount = this._getWordCount(doc); 

            // Update the status bar 
            this._statusBarItem.text = wordCount !== 1 ? `${wordCount} Words` : '1 Word'; 
            this._statusBarItem.show(); 
        } else { 
            this._statusBarItem.hide(); 
        } 
    } 

    public _getWordCount(doc: TextDocument): number { 

        let docContent = doc.getText(); 

        // Parse out unwanted whitespace so the split is accurate 
        docContent = docContent.replace(/(< ([^>]+)<)/g, '').replace(/\s+/g, ' '); 
        docContent = docContent.replace(/^\s\s*/, '').replace(/\s\s*$/, ''); 
        let wordCount = 0; 
        if (docContent != "") { 
            wordCount = docContent.split(" ").length; 
        } 

        return wordCount; 
    } 

    dispose() {
        this._statusBarItem.dispose();
    }
}

class WordCounterController {

    private _wordCounter: WordCounter;
    private _disposable: Disposable;

    constructor(wordCounter: WordCounter) {
        this._wordCounter = wordCounter;
        this._wordCounter.updateWordCount();

        // subscribe to selection change and editor activation events
        let subscriptions: Disposable[] = [];
        window.onDidChangeActiveTextEditor(this._onEvent, this, subscriptions);

        // update the counter for the current file
        this._wordCounter.updateWordCount();        

        // create a combined disposable from both event subscriptions
        this._disposable = Disposable.from(...subscriptions);
    }

    dispose() {
        this._disposable.dispose();
    }

    private _onEvent() {
        console.log('active editor changed ');
        this._wordCounter.updateWordCount();
    }
}
{
    "name": "WordCount",
    "description": "Count the words",
    "version": "0.0.1",
    "engines": {
        "vscode": "^0.10.1"
    },
    "categories": [
        "Other"
    ],
    "activationEvents": [
        "onLanguage:markdown"
    ],
    "main": "./out/src/extension",
    "scripts": {
        "vscode:prepublish": "node ./node_modules/vscode/bin/compile",
        "compile": "node ./node_modules/vscode/bin/compile -watch -p ./"
    },
    "devDependencies": {
        "typescript": "^1.6.2",
        "vscode": "0.10.x"
    }
}

Metadata

Metadata

Assignees

Labels

bugIssue identified by VS Code Team member as probable bug

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions