diff --git a/spec/package-manager-spec.coffee b/spec/package-manager-spec.coffee index 8b362b462..b3ed9d26c 100644 --- a/spec/package-manager-spec.coffee +++ b/spec/package-manager-spec.coffee @@ -438,6 +438,38 @@ describe "PackageManager", -> runs -> expect(atom.packages.isPackageActive("package-with-empty-keymap")).toBe true + describe "when the package's keymaps have been disabled", -> + it "does not add the keymaps", -> + element1 = $$ -> @div class: 'test-1' + + expect(atom.keymaps.findKeyBindings(keystrokes: 'ctrl-z', target: element1[0])).toHaveLength 0 + + atom.config.set("core.disabledKeymaps", ["package-with-keymaps-manifest"]) + + waitsForPromise -> + atom.packages.activatePackage("package-with-keymaps-manifest") + + + runs -> + expect(atom.keymaps.findKeyBindings(keystrokes: 'ctrl-z', target: element1[0])).toHaveLength 0 + + describe "when the package's keymaps are disabled at activation and re-enabled later", -> + it "re-adds the keymaps", -> + element1 = $$ -> @div class: 'test-1' + + expect(atom.keymaps.findKeyBindings(keystrokes: 'ctrl-z', target: element1[0])).toHaveLength 0 + + atom.config.set("core.disabledKeymaps", ["package-with-keymaps-manifest"]) + + waitsForPromise -> + atom.packages.activatePackage("package-with-keymaps-manifest") + + runs -> + atom.config.set("core.disabledKeymaps", []) + expect(atom.keymaps.findKeyBindings(keystrokes: 'ctrl-z', target: element1[0])[0].command).toBe 'keymap-1' + + + describe "menu loading", -> beforeEach -> atom.contextMenu.definitions = [] diff --git a/src/package.coffee b/src/package.coffee index a287a2a98..599150080 100644 --- a/src/package.coffee +++ b/src/package.coffee @@ -200,7 +200,13 @@ class Package activateResources: -> @activationDisposables = new CompositeDisposable - @activationDisposables.add(atom.keymaps.add(keymapPath, map)) for [keymapPath, map] in @keymaps + + @activationDisposables.add atom.config.observe "core.disabledKeymaps", {}, (map) => + value = not _.include(map ? [], @name) + if value + @activateKeymaps() + else if not value + @deactivateKeymaps() for [menuPath, map] in @menus when map['context-menu']? try @@ -232,6 +238,31 @@ class Package settings.activate() for settings in @settings @settingsActivated = true + activateKeymaps: -> + return if @keymapActivated + + @keymapDisposables = new CompositeDisposable() + + @keymapDisposables.add(atom.keymaps.add(keymapPath, map)) for [keymapPath, map] in @keymaps + atom.menu.update() + + @keymapActivated = true + + deactivateKeymaps: -> + return if not @keymapActivated + + @keymapDisposables?.dispose() + atom.menu.update() + + @keymapActivated = false + + hasKeymaps: -> + for [path, map] in @keymaps + if map.length > 0 + return true + + false + activateServices: -> for name, {versions} of @metadata.providedServices servicesByVersion = {} @@ -399,6 +430,7 @@ class Package settings.deactivate() for settings in @settings @stylesheetDisposables?.dispose() @activationDisposables?.dispose() + @keymapDisposables?.dispose() @stylesheetsActivated = false @grammarsActivated = false @settingsActivated = false