Load snippets from any atom package with a snippets directory

This commit is contained in:
Corey Johnson & Nathan Sobo
2013-01-07 14:26:27 -07:00
parent 6efe533650
commit f008ff52e8
6 changed files with 34 additions and 2 deletions

View File

@@ -17,8 +17,14 @@
beforeEach ->
$1
"""
"After each":
prefix: "af"
body: """
afterEach ->
$1
"""
"Expectation":
prefix: "be"
prefix: "ex"
body: "expect($1).to$2"
"Console log":
prefix: "log"

View File

@@ -0,0 +1,4 @@
".test":
"Test Snippet":
prefix: "test"
body: "testing 123"

View File

@@ -13,7 +13,8 @@ TokenizedBuffer = require 'tokenized-buffer'
fs = require 'fs'
require 'window'
requireStylesheet "jasmine.css"
require.paths.unshift(require.resolve('fixtures/packages'))
fixturePackagesPath = require.resolve('fixtures/packages')
require.paths.unshift(fixturePackagesPath)
[bindingSetsToRestore, bindingSetsByFirstKeystrokeToRestore] = []
# Load TextMate bundles, which specs rely on (but not other packages)
@@ -29,6 +30,7 @@ beforeEach ->
# reset config before each spec; don't load or save from/to `config.json`
window.config = new Config()
config.packageDirPaths.unshift(fixturePackagesPath)
spyOn(config, 'load')
spyOn(config, 'save')
config.set "editor.fontSize", 16

View File

@@ -1,4 +1,5 @@
Snippets = require 'snippets'
Snippet = require 'snippets/src/snippet'
RootView = require 'root-view'
Buffer = require 'buffer'
Editor = require 'editor'
@@ -197,6 +198,10 @@ describe "Snippets extension", ->
anotherEditor.trigger keydownEvent('tab', target: anotherEditor[0])
expect(anotherEditor.getSelectedBufferRange()).toEqual [[1, 6], [1, 36]]
describe "snippet loading", ->
it "loads snippets from all packages with a snippets directory", ->
expect(syntax.getProperty(['.test'], 'snippets.test')?.constructor).toBe Snippet
describe "Snippets parser", ->
it "breaks a snippet body into lines, with each line containing tab stops at the appropriate position", ->
bodyTree = Snippets.parser.parse """

View File

@@ -0,0 +1,11 @@
AtomPackage = require 'atom-package'
TextMatePackage = require 'text-mate-package'
fs = require 'fs'
AtomPackage.prototype.loadSnippets = ->
snippetsDirPath = fs.join(@path, 'snippets')
if fs.exists(snippetsDirPath)
for snippetsPath in fs.list(snippetsDirPath)
snippets.load(snippetsPath)
TextMatePackage.prototype.loadSnippets = ->

View File

@@ -3,6 +3,7 @@ PEG = require 'pegjs'
_ = require 'underscore'
SnippetExpansion = require 'snippets/src/snippet-expansion'
Snippet = require './snippet'
require './package-extensions'
module.exports =
snippetsByExtension: {}
@@ -15,6 +16,9 @@ module.exports =
@rootView.on 'editor:attached', (e, editor) => @enableSnippetsInEditor(editor)
loadAll: ->
for pack in atom.getPackages()
pack.loadSnippets()
for snippetsPath in fs.list(@userSnippetsDir)
@load(snippetsPath)