mirror of
https://github.com/atom/atom.git
synced 2026-04-06 03:02:13 -04:00
Start on Snippets extension
Can parse a basic snippets file
This commit is contained in:
43
spec/extensions/snippets-spec.coffee
Normal file
43
spec/extensions/snippets-spec.coffee
Normal 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"
|
||||
1
src/extensions/snippets/index.coffee
Normal file
1
src/extensions/snippets/index.coffee
Normal file
@@ -0,0 +1 @@
|
||||
module.exports = require 'extensions/snippets/snippets.coffee'
|
||||
10
src/extensions/snippets/snippets.coffee
Normal file
10
src/extensions/snippets/snippets.coffee
Normal 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)
|
||||
|
||||
22
src/extensions/snippets/snippets.pegjs
Normal file
22
src/extensions/snippets/snippets.pegjs
Normal 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]+
|
||||
Reference in New Issue
Block a user