diff --git a/demo/demo.js b/demo/demo.js index 9811e9c6..cf5c9420 100644 --- a/demo/demo.js +++ b/demo/demo.js @@ -73,6 +73,7 @@ var TextMode = require("ace/mode/text").Mode; var GroovyMode = require("ace/mode/groovy").Mode; var ScalaMode = require("ace/mode/scala").Mode; var LatexMode = require("ace/mode/latex").Mode; +var PowershellMode = require("ace/mode/powershell").Mode; var UndoManager = require("ace/undomanager").UndoManager; var vim = require("ace/keyboard/keybinding/vim").Vim; @@ -201,6 +202,10 @@ exports.launch = function(env) { docs.latex.setMode(new LatexMode()); docs.latex.setUndoManager(new UndoManager()); + docs.powershell = new EditSession(require("ace/requirejs/text!demo/docs/powershell.ps1")); + docs.powershell.setMode(new PowershellMode()); + docs.powershell.setUndoManager(new UndoManager()); + // Add a "name" property to all docs for (var doc in docs) { docs[doc].name = doc; @@ -245,7 +250,8 @@ exports.launch = function(env) { csharp: new CSharpMode(), groovy: new GroovyMode(), scala: new ScalaMode(), - latex: new LatexMode() + latex: new LatexMode(), + powershell: new PowershellMode() }; function getMode() { @@ -352,6 +358,9 @@ exports.launch = function(env) { else if (mode instanceof LatexMode) { modeEl.value = "latex"; } + else if (mode instanceof PowershellMode) { + modeEl.value = "powershell"; + } else { modeEl.value = "text"; } diff --git a/demo/docs/powershell.ps1 b/demo/docs/powershell.ps1 new file mode 100644 index 00000000..09bc59ab --- /dev/null +++ b/demo/docs/powershell.ps1 @@ -0,0 +1,5 @@ + +# This is a simple comment +function Hello($name) { + Write-host "Hellot $name" +} diff --git a/kitchen-sink.html b/kitchen-sink.html index 60c5433e..c8d80110 100644 --- a/kitchen-sink.html +++ b/kitchen-sink.html @@ -46,6 +46,7 @@ + @@ -78,6 +79,7 @@ + diff --git a/lib/ace/mode/powershell.js b/lib/ace/mode/powershell.js new file mode 100644 index 00000000..ec2b6463 --- /dev/null +++ b/lib/ace/mode/powershell.js @@ -0,0 +1,56 @@ +define(function(require, exports, module) { + +var oop = require("pilot/oop"); +var TextMode = require("ace/mode/text").Mode; +var Tokenizer = require("ace/tokenizer").Tokenizer; +var PowershellHighlightRules = require("ace/mode/powershell_highlight_rules").PowershellHighlightRules; +var MatchingBraceOutdent = require("ace/mode/matching_brace_outdent").MatchingBraceOutdent; +var CstyleBehaviour = require("ace/mode/behaviour/cstyle").CstyleBehaviour; + +var Mode = function() { + this.$tokenizer = new Tokenizer(new PowershellHighlightRules().getRules()); + this.$outdent = new MatchingBraceOutdent(); + this.$behaviour = new CstyleBehaviour(); +}; +oop.inherits(Mode, TextMode); + +(function() { + + this.getNextLineIndent = function(state, line, tab) { + var indent = this.$getIndent(line); + + var tokenizedLine = this.$tokenizer.getLineTokens(line, state); + var tokens = tokenizedLine.tokens; + var endState = tokenizedLine.state; + + if (tokens.length && tokens[tokens.length-1].type == "comment") { + return indent; + } + + if (state == "start") { + var match = line.match(/^.*[\{\(\[]\s*$/); + if (match) { + indent += tab; + } + } + + return indent; + }; + + this.checkOutdent = function(state, line, input) { + return this.$outdent.checkOutdent(line, input); + }; + + this.autoOutdent = function(state, doc, row) { + this.$outdent.autoOutdent(doc, row); + }; + + + this.createWorker = function(session) { + return null; + }; + +}).call(Mode.prototype); + +exports.Mode = Mode; +}); diff --git a/lib/ace/mode/powershell_highlight_rules.js b/lib/ace/mode/powershell_highlight_rules.js new file mode 100644 index 00000000..2703d8b7 --- /dev/null +++ b/lib/ace/mode/powershell_highlight_rules.js @@ -0,0 +1,93 @@ +define(function(require, exports, module) { + +var oop = require("pilot/oop"); +var lang = require("pilot/lang"); +var DocCommentHighlightRules = require("ace/mode/doc_comment_highlight_rules").DocCommentHighlightRules; +var TextHighlightRules = require("ace/mode/text_highlight_rules").TextHighlightRules; + +var PowershellHighlightRules = function() { + + var keywords = lang.arrayToMap( + ("function|if|else|elseif|switch|while|default|for|do|until|break|continue|foreach|return|filter|in|trap|throw|param|begin|process|end").split("|") + ); + + var buildinConstants = lang.arrayToMap( + ("$Null|$True|$False").split("|") + ); + + + // regexp must not have capturing parentheses. Use (?:) instead. + // regexps are ordered -> the first match is used + + this.$rules = { + "start" : [ + { + token : "comment", + regex : "#.*$" + }, { + token : "string.regexp", + regex : "[/](?:(?:\\[(?:\\\\]|[^\\]])+\\])|(?:\\\\/|[^\\]/]))*[/]\\w*\\s*(?=[).,;]|$)" + }, { + token : "string", // single line + regex : '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]' + }, { + token : "string", // single line + regex : "['](?:(?:\\\\.)|(?:[^'\\\\]))*?[']" + }, { + token : "constant.numeric", // hex + regex : "0[xX][0-9a-fA-F]+\\b" + }, { + token : "constant.numeric", // float + regex : "[+-]?\\d+(?:(?:\\.\\d*)?(?:[eE][+-]?\\d+)?)?\\b" + }, { + token : "constant.language.boolean", + regex : "(?:true|false)\\b" + }, { + token : function(value) { + if (value == "this") + return "variable.language"; + else if (keywords.hasOwnProperty(value)) + return "keyword"; + else if (buildinConstants.hasOwnProperty(value)) + return "constant.language"; + else + return "identifier"; + }, + // TODO: Unicode escape sequences + // TODO: Unicode identifiers + regex : "[a-zA-Z_$][a-zA-Z0-9_$]*\\b" + }, { + token : "keyword.operator", + regex : "eq|ne|ge|gt|lt|le|like|notlike|match|notmatch|replace" + }, { + token : "lparen", + regex : "[[({]" + }, { + token : "rparen", + regex : "[\\])}]" + }, { + token : "text", + regex : "\\s+" + } + ], + "comment" : [ + { + token : "comment", // closing comment + regex : ".*?\\*\\/", + next : "start" + }, { + token : "comment", // comment spanning whole line + merge : true, + regex : ".+" + } + ] + }; + + this.embedRules(DocCommentHighlightRules, "doc-", + [ new DocCommentHighlightRules().getEndRule("start") ]); +}; + +oop.inherits(PowershellHighlightRules, TextHighlightRules); + +exports.PowershellHighlightRules = PowershellHighlightRules; +});