How to determine programmatically whether an extension is disabled? - visual-studio-code

Can an extension's enabled/disabled status be determined using the Windows command line or some other method?
If an extension's settings are changed and those changes are recorded in the settings.json file and that extension is then disabled or uninstalled, those changes become 'dimmed out'. Perhaps there is a database entry somewhere that stores the dimmed status of an entry in the settings.json?

From within your own TypeScript extension you can use this:
const testedExtension = vscode.extensions.getExtension("<author of the extension>.<name of the extension>");
if (typeof testedExtension !== "undefined") {
if (testedExtension.isActive) {
vscode.window.showInformationMessage("The tested extension is active (ie enabled). :-)", {modal: true});
}
else {
vscode.window.showInformationMessage("The tested extension is NOT active (ie disabled). :-(", {modal: true});
}
}
else {
vscode.window.showInformationMessage("The tested extension was not found (ie not installed). :-( :-(", {modal: true});
}

Related

Possible to show users of your VSCode extension / color theme notifications on update?

Is it possible to show users of your extension or color theme notifications in Visual Studio Code? For someone who has my color theme or extension installed and is getting updates, I would like to possibly show this person a notification after they update the extension (That could be on launch of VSCode, or right after they go into the market to update & reload the extension and client themselves.)
For example: I think it would be beneficial to me and not invasive if they saw a notification after updating the extension saying "Feedback? Suggestions? Fixes?..on the theme?" OR notifying them of something changed in the theme that may not be favorable. So they can "opt out" of that change if they want (Like an extra set of borders around something or the color change of something.)
Obviously people with all notifications off would not be affected, but I thought an occasional notification after a rare update wouldn't be too bad. I have not been able to find info on if this is possible, and if it was, how to do it. Any info on this is appreciated. And if it is possible, those reading this, whether you've done it or not, would you recommend showing a notification to your theme users in that way?
Thanks :)
Show a notification on bottom-right corner, whenever your extension is updated. You can also control to show it only for major/minor releases.
That's how it looks:
Add below code to extension.ts:
import { window, ExtensionContext, extensions, env, Uri } from "vscode";
const extensionId = "jerrygoyal.shortcut-menu-bar";
// this method is called when your extension is activated
export function activate(context: ExtensionContext) {
showWhatsNew(context); // show notification in case of a major release i.e. 1.0.0 -> 2.0.0
}
// https://stackoverflow.com/a/66303259/3073272
function isMajorUpdate(previousVersion: string, currentVersion: string) {
// rain-check for malformed string
if (previousVersion.indexOf(".") === -1) {
return true;
}
//returns int array [1,1,1] i.e. [major,minor,patch]
var previousVerArr = previousVersion.split(".").map(Number);
var currentVerArr = currentVersion.split(".").map(Number);
if (currentVerArr[0] > previousVerArr[0]) {
return true;
} else {
return false;
}
}
async function showWhatsNew(context: ExtensionContext) {
const previousVersion = context.globalState.get<string>(extensionId);
const currentVersion = extensions.getExtension(extensionId)!.packageJSON
.version;
// store latest version
context.globalState.update(extensionId, currentVersion);
if (
previousVersion === undefined ||
isMajorUpdate(previousVersion, currentVersion)
) {
// show whats new notificatin:
const actions = [{ title: "See how" }];
const result = await window.showInformationMessage(
`Shortcut Menubar v${currentVersion} — Add your own buttons!`,
...actions
);
if (result !== null) {
if (result === actions[0]) {
await env.openExternal(
Uri.parse(
"https://github.com/GorvGoyl/Shortcut-Menu-Bar-VSCode-Extension#create-buttons-with-custom-commands"
)
);
}
}
}
}
You can see this implementation in my VSCode extension repo Shortcut Menu Bar
I think you can register the version during activation event and check for it on each activation. Then you can do whatever you want. For instance GitLens is migrating settings https://github.com/eamodio/vscode-gitlens/blob/master/src/extension.ts#L52 and i'm pretty sure I remember that they were opening a notification (but i have not found immediately in the code)
regards,

Ace editor Detect if Change happen via Autocomplete

editor.getSession().on("change",function(editing){
if (ide.curOp && ide.curOp.command.name){
console.log("change when pressing keys");
}
else {
console.log("Changed when Click on autocomplete list or programically.");
// This change is programmatically but if its via click on autocomplete list or not?
// If its via click on autocomplete I want to save document else want to ignore.
}
});
My comment in code is explaining my question well.
The answer largely depends on what you call "programical", anything editor does is done via api calls so everything is "programical". E.g. if someone adds a <button onclick='editor.setValue("")'> will change caused by it be "programical" or not.
If you want to distinguish api calls made by your code from others, use a boolean variable and set it to true before calling into ace api, and to false after that.
var ignoreChanges = false
editor.session.on("change", function(delta){
if (ignoreChanges) return console.log("ignore changes made by me")
console.log("handle changes made in some other way")
})
function applyChange() {
try {
ignoreChanges = true
// call some editor api here
editor.insert("...")
} finally {
ignoreChanges = false
}
}

Change content before preview in TinyMCE 4

In TinyMCE 4, I want to change displayed contents before they are showed on preview popup windows. This change must not affect current contents in editor. Can we do that?
If it can't, I want to catch close event of preview windows. How to do that?
TinyMCE allows us to register callbacks via the property init_instance_callback
By using the BeforeExecCommand event, oddly not in current documentation, you can make changes prior to a command being executed. We can similarly use the ExecCommand event to make changes after the command is executed. The Preview Plugin triggers the mcePreview command. You can view the Editor command Identifiers here.
Putting that together you can add the following when initializing your TinyMCE. This will show "changed content" in the preview without making visible changes to the content within TinyMCE.
var preProssesInnerHtml;
tinymce.init({
//other things...
init_instance_callback: function (editor) {
editor.on('BeforeExecCommand', function (e) {
if (e.command == "mcePreview") {
//store content prior to changing.
preProssesInnerHtml = editor.getContent();
editor.setContent("changed content");
}
});
editor.on("ExecCommand", function (e) {
if (e.command == "mcePreview") {
//Restore initial content.
editor.setContent(preProssesInnerHtml);
}
});
}
}

Set preferences in the user branch and unset them on uninstall

I created a firefox add-on with the following lib/main.js:
const {Cc,Ci} = require("chrome");
var pref = Cc["#mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
pref.setIntPref("network.http.response.timeout", 3600*24);
It wasn't accepted with the following reason:
Add-ons which change critical settings must revert the changes when disabled or uninstalled. You should also make the changes in the default, rather than the user, branch.
You need to call getDefaultBranch("") on the preferences service, and call the preference methods on the returned object rather than on the preference service directly.
To revert a preference back to the default, set by setIntPref(), I found out that I have to do this on uninstall:
pref.clearUserPref("network.http.response.timeout")
This command works fine If I call it in another test-addon. I only have to find out How to implement a command, so it is executed when the firefox-addon is uninstalled?
So how do I have to understand these comments? How do I set the preferences in a "user branch"?
Here's how I did it just now:
function clearPrefBranch(aPrefBranchName) {
var defaultBranch = Services.prefs.getDefaultBranch(null);
defaultBranch.deleteBranch(aPrefBranchName);
}
Then, just call clearPrefBranch with an argument of extensions.mypluginname (assuming you used the naming convention, and you should be able to delete all of your extension's installed preferences.
EDIT:
The code I used inside of my main.js file:
const {Cc,Ci,Cm,Cr,Cu} = require("chrome");
Cu.import("resource://gre/modules/Services.jsm");
exports.onUnload = function(aOptions, aCallbacks) {
MyPlugin.shutdown();
};
function clearPrefBranch(aPrefBranchName) {
var defaultBranch = Services.prefs.getDefaultBranch(null);
defaultBranch.deleteBranch(aPrefBranchName);
}
var MyPlugin = {
shutdown: function() {
prefLoader.clearPrefBranch('extensions.oopstab');
}
};
I solved the second part (uninstall) like this, that in my main.js I added this code at the end:
exports.onUnload = function(reason) {
//called when add-on is
// uninstalled
// disabled
// shutdown
// upgraded
// downgraded
pref.clearUserPref("network.http.response.timeout");
};
That worked on disabling and uninstalling the add-on.

JsTree with dnd plugin, always copy

I have 2 trees using jsTree and dnd plugin.
I want that each drag operation to be a copy instead of a move.
There is a "copy_modifier" which works Ok when pressing a modifier key, but I want copy to be the default behavior without the modifier.
Any ideas?
Thanks,
Adrian
Found a solution on http://groups.google.com/group/jstree
I added the following section when configuring jsTree:
"crrm": {
"move": { "always_copy": "multitree" }
}
Hope this helps,
Adrian
another solution for the new version. it works, but not fully tested.
"core": {
"check_callback": function (operation, node, node_parent, node_position, more) {
if (more) {
if (more.is_multi) {
more.origin.settings.dnd.always_copy = true;
} else {
more.origin.settings.dnd.always_copy = false;
}
}
return true;
}
}
Adrian's solution won't work with the new versions.
There's that dnd plugins always copy flag
dnd.always_copy
Setting this flag will make all drag and drops copy operations instead of move. But if you are looking for a solution where you need internal tree elements to be moved on dnd but inter tree dnds to be copies than here's a hack:
Keep a global variable flag on your page
Handle copy_node.jstree events and update your global flag from
data.is_multi (data is the second arg of the event function)
Implement check_callback function and if operation is delete_node and your flag is set unset your flag and return false, preventing deletion from the dnd.