Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

activeTextEditor not correct while the extension is activated #38

Closed
aeschli opened this issue Nov 17, 2015 · 3 comments
Closed

activeTextEditor not correct while the extension is activated #38

aeschli opened this issue Nov 17, 2015 · 3 comments
Assignees
Labels
bug Issue identified by VS Code Team member as probable bug
Milestone

Comments

@aeschli
Copy link
Contributor

aeschli commented Nov 17, 2015

  • 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"
    }
}
@aeschli aeschli changed the title activate activeTextEditor not correct while the extension is activated Nov 17, 2015
@alexdima alexdima added the bug Issue identified by VS Code Team member as probable bug label Nov 24, 2015
@alexdima alexdima added this to the Dec 2015 milestone Nov 30, 2015
@alexdima alexdima modified the milestones: Backlog, Dec 2015 Dec 15, 2015
@seanmcbreen
Copy link

I also see this issue in my spell extension - I want to test for the doc type to hide a new status bar option but see the exact same error condition i..e. LanguageId == vs.editor.modes.nullMode

@siegebell
Copy link

I'm also seeing this. When the extension host's workspace is reloaded, all of the unfocused (background tabs) documents in vscode.workspace.textDocuments have their languageId set to "vs.editor.nullMode". Changing tabs and editing the document does not fix this, but manually setting the document to another language and back does.

@alexdima
Copy link
Member

alexdima commented Apr 5, 2017

This has been fixed since a couple of months.

@alexdima alexdima closed this as completed Apr 5, 2017
@alexdima alexdima modified the milestones: April 2017, Backlog Apr 5, 2017
@vscodebot vscodebot bot locked and limited conversation to collaborators Nov 18, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue identified by VS Code Team member as probable bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants