diff --git a/packages/settings-view/lib/package-keymap-view.js b/packages/settings-view/lib/package-keymap-view.js index 47236b0f8a..1b7a0e8097 100644 --- a/packages/settings-view/lib/package-keymap-view.js +++ b/packages/settings-view/lib/package-keymap-view.js @@ -11,7 +11,6 @@ import KeybindingsPanel from './keybindings-panel' export default class PackageKeymapView { constructor (pack) { this.pack = pack - this.otherPlatformPattern = new RegExp(`\\.platform-(?!${_.escapeRegExp(process.platform)}\\b)`) this.namespace = this.pack.name this.disposables = new CompositeDisposable() etch.initialize(this) @@ -128,8 +127,8 @@ export default class PackageKeymapView { continue } - if (this.otherPlatformPattern.test(selector)) { - continue + if (this.selectorIsNotCompatibleWithPlatform(selector)) { + continue; } const keyBindingRow = document.createElement('tr') @@ -183,4 +182,11 @@ export default class PackageKeymapView { atom.clipboard.write(content) } + + selectorIsNotCompatibleWithPlatform(selector, platform = process.platform) { + const otherPlatformPattern = new RegExp(`\\.platform-(?!${_.escapeRegExp(platform)}\\b)`); + const currentPlatformPattern = new RegExp(`\\.platform-(${_.escapeRegExp(platform)}\\b)`); + + return otherPlatformPattern.test(selector) && !currentPlatformPattern.test(selector); + } } diff --git a/packages/settings-view/spec/package-keymap-view-spec.js b/packages/settings-view/spec/package-keymap-view-spec.js new file mode 100644 index 0000000000..bd02446596 --- /dev/null +++ b/packages/settings-view/spec/package-keymap-view-spec.js @@ -0,0 +1,35 @@ + +const PackageKeymapView = require("../lib/package-keymap-view.js"); +let view; + +describe("PackageKeymapView", () => { + + beforeEach(() => { + // Just prevent this stuff from calling through, it doesn't matter for this test + spyOn(atom.packages, "getLoadedPackage").andReturn({ keymaps: [] }); + + view = new PackageKeymapView({ + name: "test-package" + }); + }); + + it("should say a selector with no platform listed is compatible with the current one", () => { + expect(view.selectorIsNotCompatibleWithPlatform("atom-text-editor", "win32")).toBe(false); + }); + + it("should say a selector with a platform other than the current is not compatible", () => { + expect(view.selectorIsNotCompatibleWithPlatform(".platform-darwin", "linux")).toBe(true); + expect(view.selectorIsNotCompatibleWithPlatform(".platform-win32", "darwin")).toBe(true); + }); + + it("should say a selector with the current platform listed is compatible", () => { + expect(view.selectorIsNotCompatibleWithPlatform(".platform-linux", "linux")).toBe(false); + expect(view.selectorIsNotCompatibleWithPlatform(".platform-win32", "win32")).toBe(false); + expect(view.selectorIsNotCompatibleWithPlatform(".platform-darwin", "darwin")).toBe(false); + }); + + it("should say a selector with the current platform and others listed is compatible", () => { + expect(view.selectorIsNotCompatibleWithPlatform(".platform-linux, .platform-win32", "win32")).toBe(false); + expect(view.selectorIsNotCompatibleWithPlatform(".platform-linux, .platform-win32", "linux")).toBe(false); + }); +});