commit 31d5b0988fda838b833a82cae3469ce8c5fc2a39 Author: Buddy Sandidge Date: Wed Feb 19 22:00:58 2014 -0800 Squashed 'vim/bundle/literate-coffeescript/' content from commit 99aa91709 git-subtree-dir: vim/bundle/literate-coffeescript git-subtree-split: 99aa91709201cbb0e9ef0b15eb8c2390b764e8b5 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..9dfbb05 --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2013 Michael Smith + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..787bb5a --- /dev/null +++ b/README.md @@ -0,0 +1,56 @@ +`vim-literate-coffeescript` adds syntax highlighting and indentation support for +[Literate CoffeeScript](http://coffeescript.org/#literate) to the vim text +editor. + +It looks something like this: + +![Preview](test/test.png) + +# Dependencies + +You must have syntax highlighting installed for both +[Markdown](https://github.com/tpope/vim-markdown) and +[CoffeeScript](https://github.com/kchmck/vim-coffee-script) installed to use +this package. + +# Installation + +## Manually + + 1. Clone this repository. + + git clone https://github.com/mintplant/vim-literate-coffeescript.git + + 2. Copy all the vim-specific files into your ~/.vim folder. + + cp -r vim-literate-coffeescript/ftdetect/ ~/.vim/ + cp -r vim-literate-coffeescript/indent/ ~/.vim/ + cp -r vim-literate-coffeescript/syntax/ ~/.vim/ + +## With Pathogen + +### Setup + + 1. Install [Pathogen](http://www.vim.org/scripts/script.php?script_id=2332) + + 2. Change directory to your bundle folder. + + cd ~/.vim/bundle + + 3. Clone this repository. + + git clone https://github.com/mintplant/vim-literate-coffeescript.git + +### Updating + + 1. Change directory to where the repository was cloned. + + cd ~/.vim/bundle/vim-literate-coffeescript/ + + 2. Pull in the latest changes. + + git pull + +# License + +[MIT](LICENSE) diff --git a/ftdetect/vim-literate-coffeescript.vim b/ftdetect/vim-literate-coffeescript.vim new file mode 100644 index 0000000..7f66624 --- /dev/null +++ b/ftdetect/vim-literate-coffeescript.vim @@ -0,0 +1,8 @@ +" Language: Literate CoffeeScript +" Maintainer: Michael Smith +" URL: https://github.com/mintplant/vim-literate-coffeescript +" License: MIT + +autocmd BufNewFile,BufRead *.litcoffee set filetype=litcoffee +autocmd BufNewFile,BufRead *.coffee.md set filetype=litcoffee + diff --git a/indent/litcoffee.vim b/indent/litcoffee.vim new file mode 100644 index 0000000..599cbea --- /dev/null +++ b/indent/litcoffee.vim @@ -0,0 +1,22 @@ +if exists('b:did_indent') + finish +endif + +runtime! indent/coffee.vim + +let b:did_indent = 1 + +setlocal indentexpr=GetLitCoffeeIndent() + +if exists('*GetLitCoffeeIndent') + finish +endif + +function GetLitCoffeeIndent() + if searchpair('^ \|\t', '', '$', 'bWnm') > 0 + return GetCoffeeIndent(v:lnum) + else + return -1 + endif +endfunc + diff --git a/syntax/litcoffee.vim b/syntax/litcoffee.vim new file mode 100644 index 0000000..4028a82 --- /dev/null +++ b/syntax/litcoffee.vim @@ -0,0 +1,20 @@ +" Language: Literate CoffeeScript +" Maintainer: Michael Smith +" URL: https://github.com/mintplant/vim-literate-coffeescript +" License: MIT + +if exists('b:current_syntax') && b:current_syntax == 'litcoffee' + finish +endif + +runtime! syntax/markdown.vim +unlet b:current_syntax + +syn clear markdownCode + +syn include @coffee syntax/coffee.vim + +syn region inlineCoffee start='^ \|\t' end='$' contains=@coffee + +let b:current_syntax = "litcoffee" + diff --git a/test/test.coffee.md b/test/test.coffee.md new file mode 100644 index 0000000..62b99b7 --- /dev/null +++ b/test/test.coffee.md @@ -0,0 +1,117 @@ +The **Scope** class regulates lexical scoping within CoffeeScript. As you +generate code, you create a tree of scopes in the same shape as the nested +function bodies. Each scope knows about the variables declared within it, +and has a reference to its parent enclosing scope. In this way, we know which +variables are new and need to be declared with `var`, and which are shared +with external scopes. + +Import the helpers we plan to use. + + {extend, last} = require './helpers' + + exports.Scope = class Scope + +The `root` is the top-level **Scope** object for a given file. + + @root: null + +Initialize a scope with its parent, for lookups up the chain, +as well as a reference to the **Block** node it belongs to, which is +where it should declare its variables, and a reference to the function that +it belongs to. + + constructor: (@parent, @expressions, @method) -> + @variables = [{name: 'arguments', type: 'arguments'}] + @positions = {} + Scope.root = this unless @parent + +Adds a new variable or overrides an existing one. + + add: (name, type, immediate) -> + return @parent.add name, type, immediate if @shared and not immediate + if Object::hasOwnProperty.call @positions, name + @variables[@positions[name]].type = type + else + @positions[name] = @variables.push({name, type}) - 1 + +When `super` is called, we need to find the name of the current method we're +in, so that we know how to invoke the same method of the parent class. This +can get complicated if super is being called from an inner function. +`namedMethod` will walk up the scope tree until it either finds the first +function object that has a name filled in, or bottoms out. + + namedMethod: -> + return @method if @method.name or !@parent + @parent.namedMethod() + +Look up a variable name in lexical scope, and declare it if it does not +already exist. + + find: (name) -> + return yes if @check name + @add name, 'var' + no + +Reserve a variable name as originating from a function parameter for this +scope. No `var` required for internal references. + + parameter: (name) -> + return if @shared and @parent.check name, yes + @add name, 'param' + +Just check to see if a variable has already been declared, without reserving, +walks up to the root scope. + + check: (name) -> + !!(@type(name) or @parent?.check(name)) + +Generate a temporary variable name at the given index. + + temporary: (name, index) -> + if name.length > 1 + '_' + name + if index > 1 then index - 1 else '' + else + '_' + (index + parseInt name, 36).toString(36).replace /\d/g, 'a' + +Gets the type of a variable. + + type: (name) -> + return v.type for v in @variables when v.name is name + null + +If we need to store an intermediate result, find an available name for a +compiler-generated variable. `_var`, `_var2`, and so on... + + freeVariable: (name, reserve=true) -> + index = 0 + index++ while @check((temp = @temporary name, index)) + @add temp, 'var', yes if reserve + temp + +Ensure that an assignment is made at the top of this scope +(or at the top-level scope, if requested). + + assign: (name, value) -> + @add name, {value, assigned: yes}, yes + @hasAssignments = yes + +Does this scope have any declared variables? + + hasDeclarations: -> + !!@declaredVariables().length + +Return the list of variables first declared in this scope. + + declaredVariables: -> + realVars = [] + tempVars = [] + for v in @variables when v.type is 'var' + (if v.name.charAt(0) is '_' then tempVars else realVars).push v.name + realVars.sort().concat tempVars.sort() + +Return the list of assignments that are supposed to be made at the top +of this scope. + + assignedVariables: -> + "#{v.name} = #{v.type.value}" for v in @variables when v.type.assigned + diff --git a/test/test.litcoffee b/test/test.litcoffee new file mode 100644 index 0000000..62b99b7 --- /dev/null +++ b/test/test.litcoffee @@ -0,0 +1,117 @@ +The **Scope** class regulates lexical scoping within CoffeeScript. As you +generate code, you create a tree of scopes in the same shape as the nested +function bodies. Each scope knows about the variables declared within it, +and has a reference to its parent enclosing scope. In this way, we know which +variables are new and need to be declared with `var`, and which are shared +with external scopes. + +Import the helpers we plan to use. + + {extend, last} = require './helpers' + + exports.Scope = class Scope + +The `root` is the top-level **Scope** object for a given file. + + @root: null + +Initialize a scope with its parent, for lookups up the chain, +as well as a reference to the **Block** node it belongs to, which is +where it should declare its variables, and a reference to the function that +it belongs to. + + constructor: (@parent, @expressions, @method) -> + @variables = [{name: 'arguments', type: 'arguments'}] + @positions = {} + Scope.root = this unless @parent + +Adds a new variable or overrides an existing one. + + add: (name, type, immediate) -> + return @parent.add name, type, immediate if @shared and not immediate + if Object::hasOwnProperty.call @positions, name + @variables[@positions[name]].type = type + else + @positions[name] = @variables.push({name, type}) - 1 + +When `super` is called, we need to find the name of the current method we're +in, so that we know how to invoke the same method of the parent class. This +can get complicated if super is being called from an inner function. +`namedMethod` will walk up the scope tree until it either finds the first +function object that has a name filled in, or bottoms out. + + namedMethod: -> + return @method if @method.name or !@parent + @parent.namedMethod() + +Look up a variable name in lexical scope, and declare it if it does not +already exist. + + find: (name) -> + return yes if @check name + @add name, 'var' + no + +Reserve a variable name as originating from a function parameter for this +scope. No `var` required for internal references. + + parameter: (name) -> + return if @shared and @parent.check name, yes + @add name, 'param' + +Just check to see if a variable has already been declared, without reserving, +walks up to the root scope. + + check: (name) -> + !!(@type(name) or @parent?.check(name)) + +Generate a temporary variable name at the given index. + + temporary: (name, index) -> + if name.length > 1 + '_' + name + if index > 1 then index - 1 else '' + else + '_' + (index + parseInt name, 36).toString(36).replace /\d/g, 'a' + +Gets the type of a variable. + + type: (name) -> + return v.type for v in @variables when v.name is name + null + +If we need to store an intermediate result, find an available name for a +compiler-generated variable. `_var`, `_var2`, and so on... + + freeVariable: (name, reserve=true) -> + index = 0 + index++ while @check((temp = @temporary name, index)) + @add temp, 'var', yes if reserve + temp + +Ensure that an assignment is made at the top of this scope +(or at the top-level scope, if requested). + + assign: (name, value) -> + @add name, {value, assigned: yes}, yes + @hasAssignments = yes + +Does this scope have any declared variables? + + hasDeclarations: -> + !!@declaredVariables().length + +Return the list of variables first declared in this scope. + + declaredVariables: -> + realVars = [] + tempVars = [] + for v in @variables when v.type is 'var' + (if v.name.charAt(0) is '_' then tempVars else realVars).push v.name + realVars.sort().concat tempVars.sort() + +Return the list of assignments that are supposed to be made at the top +of this scope. + + assignedVariables: -> + "#{v.name} = #{v.type.value}" for v in @variables when v.type.assigned + diff --git a/test/test.png b/test/test.png new file mode 100644 index 0000000..e3ad619 Binary files /dev/null and b/test/test.png differ