From 379a4a4d8d8d8bcbc529c92509f05d3347440fb4 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 31 Jan 2013 12:01:02 -0800 Subject: [PATCH] Only auto-load .cson and .json keymap files This now prevents files such as .DS_Store from being loaded as a keymap where previously all paths were loaded regardless of extension. Closes #205 --- spec/app/atom-spec.coffee | 5 ++++- .../package-with-module/keymaps/keymap-3.cjson | 2 ++ spec/stdlib/fs-spec.coffee | 5 +++++ src/app/atom-package.coffee | 9 +++------ src/app/keymap.coffee | 2 +- src/stdlib/fs.coffee | 13 +++++++++---- 6 files changed, 24 insertions(+), 12 deletions(-) create mode 100644 spec/fixtures/packages/package-with-module/keymaps/keymap-3.cjson diff --git a/spec/app/atom-spec.coffee b/spec/app/atom-spec.coffee index a02c84d97..73474a2a1 100644 --- a/spec/app/atom-spec.coffee +++ b/spec/app/atom-spec.coffee @@ -26,17 +26,20 @@ describe "the `atom` global", -> describe "keymap loading", -> describe "when package.json does not contain a 'keymaps' manifest", -> - it "loads all keymaps in the directory", -> + it "loads all the .cson/.json files in the keymaps directory", -> element1 = $$ -> @div class: 'test-1' element2 = $$ -> @div class: 'test-2' + element3 = $$ -> @div class: 'test-3' expect(keymap.bindingsForElement(element1)['ctrl-z']).toBeUndefined() expect(keymap.bindingsForElement(element2)['ctrl-z']).toBeUndefined() + expect(keymap.bindingsForElement(element3)['ctrl-z']).toBeUndefined() atom.loadPackage("package-with-module") expect(keymap.bindingsForElement(element1)['ctrl-z']).toBe "test-1" expect(keymap.bindingsForElement(element2)['ctrl-z']).toBe "test-2" + expect(keymap.bindingsForElement(element3)['ctrl-z']).toBeUndefined() describe "when package.json contains a 'keymaps' manifest", -> it "loads only the keymaps specified by the manifest, in the specified order", -> diff --git a/spec/fixtures/packages/package-with-module/keymaps/keymap-3.cjson b/spec/fixtures/packages/package-with-module/keymaps/keymap-3.cjson new file mode 100644 index 000000000..3c95c85a5 --- /dev/null +++ b/spec/fixtures/packages/package-with-module/keymaps/keymap-3.cjson @@ -0,0 +1,2 @@ +".test-3": + "ctrl-z": "test-3" diff --git a/spec/stdlib/fs-spec.coffee b/spec/stdlib/fs-spec.coffee index ffcdaf31b..b0a6fae5f 100644 --- a/spec/stdlib/fs-spec.coffee +++ b/spec/stdlib/fs-spec.coffee @@ -128,3 +128,8 @@ describe "fs", -> describe ".md5ForPath(path)", -> it "returns the MD5 hash of the file at the given path", -> expect(fs.md5ForPath(require.resolve('fixtures/sample.js'))).toBe 'dd38087d0d7e3e4802a6d3f9b9745f2b' + + describe ".list(path, extensions)", -> + it "returns the paths with the specified extensions", -> + path = require.resolve('fixtures/css.css') + expect(fs.list(require.resolve('fixtures'), ['.css'])).toEqual [path] diff --git a/src/app/atom-package.coffee b/src/app/atom-package.coffee index bb1ce071b..15a4e465a 100644 --- a/src/app/atom-package.coffee +++ b/src/app/atom-package.coffee @@ -26,15 +26,12 @@ class AtomPackage extends Package @metadata = fs.readObject(metadataPath) loadKeymaps: -> - for keymapPath in @getKeymapPaths() - keymap.load(keymapPath) - - getKeymapPaths: -> if keymaps = @metadata?.keymaps - keymaps.map (relativePath) => + keymaps = keymaps.map (relativePath) => fs.resolve(@keymapsDirPath, relativePath, ['cson', 'json', '']) + keymap.load(keymapPath) for keymapPath in keymaps else - fs.list(@keymapsDirPath) + keymap.loadDirectory(@keymapsDirPath) loadStylesheets: -> for stylesheetPath in @getStylesheetPaths() diff --git a/src/app/keymap.coffee b/src/app/keymap.coffee index 1918a429d..97cf4303b 100644 --- a/src/app/keymap.coffee +++ b/src/app/keymap.coffee @@ -35,7 +35,7 @@ class Keymap @loadDirectory(fs.join(config.configDirPath, 'keymaps')) loadDirectory: (directoryPath) -> - @load(filePath) for filePath in fs.list(directoryPath) + @load(filePath) for filePath in fs.list(directoryPath, ['.cson', '.json']) load: (path) -> @add(fs.readObject(path)) diff --git a/src/stdlib/fs.coffee b/src/stdlib/fs.coffee index a9eb755da..fcf8904a9 100644 --- a/src/stdlib/fs.coffee +++ b/src/stdlib/fs.coffee @@ -59,11 +59,16 @@ module.exports = # Returns an array with all the names of files contained # in the directory path. - list: (rootPath) -> + list: (rootPath, extensions) -> paths = [] - onPath = (path) => - paths.push(@join(rootPath, path)) - false + if extensions + onPath = (path) => + paths.push(@join(rootPath, path)) if _.contains(extensions, @extension(path)) + false + else + onPath = (path) => + paths.push(@join(rootPath, path)) + false @traverseTree(rootPath, onPath, onPath) paths