Start on Snippets extension

Can parse a basic snippets file
This commit is contained in:
David Graham & Nathan Sobo
2012-06-19 17:19:47 -06:00
parent c97d78a31d
commit 4204b27751
4 changed files with 76 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
Snippets = require 'snippets'
Buffer = require 'buffer'
Editor = require 'editor'
_ = require 'underscore'
fdescribe "Snippets extension", ->
[buffer, editor] = []
beforeEach ->
buffer = new Buffer(require.resolve('fixtures/sample.js'))
editor = new Editor({buffer})
describe "when 'tab' is triggered on the editor", ->
describe "when the letters preceding the cursor are registered as a global extension", ->
it "replaces the prefix with the snippet text", ->
Snippets.evalSnippets 'js', """
snippet te "Test snippet description"
this is a test
endsnippet
snippet moo "Moo snippet"
Mooooooo!
endsnippet
"""
editor.insertText("te")
editor.trigger 'tab'
expect(editor.getCursorScreenPosition()).toEqual [0, 2]
expect(buffer.lineForRow(0)).toBe "this is a testvar quicksort = function () {"
ffdescribe "Snippets parser", ->
it "can parse a snippet", ->
snippets = Snippets.snippetsParser.parse """
snippet te "Test snippet description"
this is a test
endsnippet
"""
expect(_.keys(snippets).length).toBe 1
snippet = snippets['te']
expect(snippet.prefix).toBe 'te'
expect(snippet.description).toBe "Test snippet description"
expect(snippet.body).toBe "this is a test"

View File

@@ -0,0 +1 @@
module.exports = require 'extensions/snippets/snippets.coffee'

View File

@@ -0,0 +1,10 @@
fs = require 'fs'
PEG = require 'pegjs'
module.exports =
snippetsByExtension: {}
snippetsParser: PEG.buildParser(fs.read(require.resolve 'extensions/snippets/snippets.pegjs'))
evalSnippets: (extension, text) ->
@snippetsByExtension[extension] = snippetsParser.parse(text)

View File

@@ -0,0 +1,22 @@
snippets = snippets:snippet+ {
var snippetsByPrefix = {};
snippets.forEach(function(snippet) {
snippetsByPrefix[snippet.prefix] = snippet
});
return snippetsByPrefix;
}
snippet = start ws prefix:prefix ws description:string separator body:body end {
return { prefix: prefix, description: description, body: body };
}
separator = [ ]* '\n'
start = 'snippet'
prefix = prefix:[A-Za-z0-9_]+ { return prefix.join(''); }
body = body:bodyCharacter* { return body.join(''); }
bodyCharacter = !end char:[a-z ] { return char; }
end = '\nendsnippet'
string
= ['] body:[^']* ['] { return body.join(''); }
/ ["] body:[^"]* ["] { return body.join(''); }
ws = [ \n]+