You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.exportfunctionactivate(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 counterletwordCounter=newWordCounter();letcontroller=newWordCounterController(wordCounter);// Add to a list of disposables which are disposed when this extension is deactivated.context.subscriptions.push(controller);context.subscriptions.push(wordCounter);}classWordCounter{private_statusBarItem: StatusBarItem;publicupdateWordCount(){// Create as needed if(!this._statusBarItem){this._statusBarItem=window.createStatusBarItem(StatusBarAlignment.Left);}// Get the current text editor leteditor=window.activeTextEditor;if(!editor){this._statusBarItem.hide();return;}letdoc=editor.document;console.log('Current doc language '+doc.languageId);// Only update status if an MarkDown file if(doc.languageId==="markdown"){letwordCount=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{letdocContent=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*$/,'');letwordCount=0;if(docContent!=""){wordCount=docContent.split(" ").length;}returnwordCount;}dispose(){this._statusBarItem.dispose();}}classWordCounterController{private_wordCounter: WordCounter;private_disposable: Disposable;constructor(wordCounter: WordCounter){this._wordCounter=wordCounter;this._wordCounter.updateWordCount();// subscribe to selection change and editor activation eventsletsubscriptions: Disposable[]=[];window.onDidChangeActiveTextEditor(this._onEvent,this,subscriptions);// update the counter for the current filethis._wordCounter.updateWordCount();// create a combined disposable from both event subscriptionsthis._disposable=Disposable.from(...subscriptions);}dispose(){this._disposable.dispose();}private_onEvent(){console.log('active editor changed ');this._wordCounter.updateWordCount();}}
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
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.
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.
The text was updated successfully, but these errors were encountered: