Skip to content

Commit

Permalink
Razor Highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
pmdevers authored and nightwing committed Jan 17, 2016
1 parent 9f19342 commit cb6dd55
Show file tree
Hide file tree
Showing 7 changed files with 312 additions and 0 deletions.
3 changes: 3 additions & 0 deletions demo/kitchen-sink/docs/razor.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@{
Layout = "~/layout"
}
1 change: 1 addition & 0 deletions lib/ace/ext/modelist.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ var supportedModes = {
Protobuf: ["proto"],
Python: ["py"],
R: ["r"],
Razor: ["cshtml"],
RDoc: ["Rd"],
RHTML: ["Rhtml"],
RST: ["rst"],
Expand Down
33 changes: 33 additions & 0 deletions lib/ace/mode/razor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
define(function(require, exports, module) {
"use strict";

var oop = require("../lib/oop");
var HtmlMode = require("./html").Mode;
var RazorHighlightRules = require("./razor_highlight_rules").RazorHighlightRules;
var RazorCompletions = require("./razor_completions").RazorCompletions;
var HtmlCompletions = require("./html_completions").HtmlCompletions;

var Mode = function() {
HtmlMode.call(this);
this.$highlightRules = new RazorHighlightRules();
this.$completer = new RazorCompletions();
this.$htmlCompleter = new HtmlCompletions();
};
oop.inherits(Mode, HtmlMode);

(function() {
this.getCompletions = function(state, session, pos, prefix) {
var razorToken = this.$completer.getCompletions(state, session, pos, prefix);
var htmlToken = this.$htmlCompleter.getCompletions(state, session, pos, prefix);
return razorToken.concat(htmlToken);
};

this.createWorker = function(session) {
return null;
};

this.$id = "ace/mode/razor";
}).call(Mode.prototype);

exports.Mode = Mode;
});
112 changes: 112 additions & 0 deletions lib/ace/mode/razor_completions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/* ***** BEGIN LICENSE BLOCK *****
* Distributed under the BSD license:
*
* Copyright (c) 2010, Ajax.org B.V.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Ajax.org B.V. nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ***** END LICENSE BLOCK ***** */

define(function(require, exports, module) {
"use strict";

var TokenIterator = require("../token_iterator").TokenIterator;

var keywords = [
"abstract", "as", "base", "bool",
"break", "byte", "case", "catch",
"char", "checked", "class", "const",
"continue", "decimal", "default", "delegate",
"do", "double","else","enum",
"event", "explicit", "extern", "false",
"finally", "fixed", "float", "for",
"foreach", "goto", "if", "implicit",
"in", "int", "interface", "internal",
"is", "lock", "long", "namespace",
"new", "null", "object", "operator",
"out", "override", "params", "private",
"protected", "public", "readonly", "ref",
"return", "sbyte", "sealed", "short",
"sizeof", "stackalloc", "static", "string",
"struct", "switch", "this", "throw",
"true", "try", "typeof", "uint",
"ulong", "unchecked", "unsafe", "ushort",
"using", "var", "virtual", "void",
"volatile", "while"];

var shortHands = [
"Html", "Model", "Url", "Layout"
];

var RazorCompletions = function() {

};

(function() {

this.getCompletions = function(state, session, pos, prefix) {

if(state.lastIndexOf("razor-short-start") == -1 && state.lastIndexOf("razor-block-start") == -1)
return [];

var token = session.getTokenAt(pos.row, pos.column);
if (!token)
return [];

if(state.lastIndexOf("razor-short-start") != -1) {
return this.getShortStartCompletions(state, session, pos, prefix);
}

if(state.lastIndexOf("razor-block-start") != -1) {
return this.getKeywordCompletions(state, session, pos, prefix);
}


};

this.getShortStartCompletions = function(state, session, pos, prefix) {
return shortHands.map(function(element){
return {
value: element,
meta: "keyword",
score: Number.MAX_VALUE
};
});
};

this.getKeywordCompletions = function(state, session, pos, prefix) {
return shortHands.concat(keywords).map(function(element){
return {
value: element,
meta: "keyword",
score: Number.MAX_VALUE
};
});
};

}).call(RazorCompletions.prototype);

exports.RazorCompletions = RazorCompletions;

});
152 changes: 152 additions & 0 deletions lib/ace/mode/razor_highlight_rules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
define(function(require, exports, module) {
"use strict";

var oop = require("../lib/oop");
var lang = require("../lib/lang");
var DocCommentHighlightRules = require("./doc_comment_highlight_rules").DocCommentHighlightRules;
var HtmlHighlightRules = require("./html_highlight_rules").HtmlHighlightRules;
var CSharpHighlightRules = require("./csharp_highlight_rules").CSharpHighlightRules;

var blockPrefix = 'razor-block-';
var RazorLangHighlightRules = function() {
CSharpHighlightRules.call(this);

var processPotentialCallback = function(value, stackItem) {
if (typeof stackItem === "function")
return stackItem(value);

return stackItem;
};

var inBraces = 'in-braces';
this.$rules.start.unshift({
regex: '[\\[({]',
onMatch: function(value, state, stack) {
var prefix = /razor-[^\-]+-/.exec(state)[0];

stack.unshift(value);
stack.unshift(prefix + inBraces);
this.next = prefix + inBraces;
return 'paren.lparen';
}
});

var parentCloseMap = {
'{': '}',
'[': ']',
'(': ')'
};

this.$rules[inBraces] = lang.deepCopy(this.$rules.start);
this.$rules[inBraces].unshift({
regex: '[\\])}]',
onMatch: function(value, state, stack) {
var open = stack[1];
if (parentCloseMap[open] !== value)
return 'invalid.illegal';

stack.shift(); // exit in-braces block
stack.shift(); // exit brace marker
this.next = processPotentialCallback(value, stack[0]) || 'start';
return 'paren.rparen';
}
});
};

oop.inherits(RazorLangHighlightRules, CSharpHighlightRules);

var RazorHighlightRules = function() {
HtmlHighlightRules.call(this);

// 'Blocks': @{}, @()

var blockStartRule = {
regex: '@[({]',
onMatch: function(value, state, stack) {
stack.unshift(value);
stack.unshift('razor-block-start');
this.next = 'razor-block-start';
return 'punctuation.block.razor';
}
};

var blockEndMap = {
'@{': '}',
'@(': ')',
};

var blockEndRule = {
regex: '[})]',
onMatch: function(value, state, stack) {
var blockStart = stack[1];
if (blockEndMap[blockStart] !== value)
return 'invalid.illegal';

stack.shift(); // exit razor block
stack.shift(); // remove block type marker
this.next = stack.shift() || 'start';
return 'punctuation.block.razor';
}
};

// Short: @Abc.Cde(Xyz).Ef

var shortStartRule = {
regex: "@(?![{(])",
onMatch: function(value, state, stack) {
stack.unshift("razor-short-start");
this.next = "razor-short-start";
return 'punctuation.short.razor';
}
};

var shortEndRule = {
token: "",
regex: "(?=[^A-Za-z_\\.()\\[\\]])",
next: 'pop'
};

// Control flow: @if, etc

var ifStartRule = {
regex: "@(?=if)",
onMatch: function(value, state, stack) {
stack.unshift(function(value) {
if (value !== '}')
return 'start';

return stack.shift() || 'start';
});
this.next = 'razor-block-start';
return 'punctuation.control.razor';
}
};

// Combined:

var razorStartRules = [
{
token: ["meta.directive.razor", "text", "identifier"],
regex: "^(\\s*@model)(\\s+)(.+)$"
},
blockStartRule,
//ifStartRule,
shortStartRule
];


console.log(this.$rules);
for (var key in this.$rules)
this.$rules[key].unshift.apply(this.$rules[key], razorStartRules);

this.embedRules(RazorLangHighlightRules, "razor-block-", [blockEndRule], ["start"]);
this.embedRules(RazorLangHighlightRules, "razor-short-", [shortEndRule], ["start"]);

this.normalizeRules();
};

oop.inherits(RazorHighlightRules, HtmlHighlightRules);

exports.RazorHighlightRules = RazorHighlightRules;
exports.RazorLangHighlightRules = RazorLangHighlightRules;
});
7 changes: 7 additions & 0 deletions lib/ace/snippets/razor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
define(function(require, exports, module) {
"use strict";

exports.snippetText = require("../requirejs/text!./razor.snippets");
exports.scope = "razor";

});
4 changes: 4 additions & 0 deletions lib/ace/snippets/razor.snippets
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
snippet if
(${1} == ${2}) {
${3}
}

0 comments on commit cb6dd55

Please sign in to comment.