From d2dfec41eea853baf3ce6909e15135d756ca3981 Mon Sep 17 00:00:00 2001 From: nightwing Date: Sat, 26 May 2012 22:03:41 +0400 Subject: [PATCH 1/4] fix highlighting of multiline php strings --- lib/ace/mode/php_highlight_rules.js | 46 ++++++++++++++++------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/lib/ace/mode/php_highlight_rules.js b/lib/ace/mode/php_highlight_rules.js index e25dddc8..424dbfe3 100644 --- a/lib/ace/mode/php_highlight_rules.js +++ b/lib/ace/mode/php_highlight_rules.js @@ -948,18 +948,12 @@ var PhpHighlightRules = function() { token : "string.regexp", regex : "[/](?:(?:\\[(?:\\\\]|[^\\]])+\\])|(?:\\\\/|[^\\]/]))*[/][gimy]*\\s*(?=[).,;]|$)" }, { - token : "string", // single line - regex : '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]' - }, { - token : "string", // multi line string start - regex : '["][\\s\\S]*', + token : "string", // " string start + regex : '"', next : "qqstring" }, { - token : "string", // single line - regex : "['](?:(?:\\\\.)|(?:[^'\\\\]))*?[']" - }, { - token : "string", // multi line string start - regex : "['][\\s\\S]+", + token : "string", // ' string start + regex : "'", next : "qstring" }, { token : "constant.numeric", // hex @@ -1040,33 +1034,45 @@ var PhpHighlightRules = function() { ], "qqstring" : [ { + token : "constant.language.escape", + regex : '\\\\(?:[nrtvef\\\\"$]|[0-7]{1,3}|x[0-9A-Fa-f]{1,2})' + }, { + token : "constant.language.escape", + regex : /\$[\w\d]+(?:\[[\w\d]+\])?/ + }, { + token : "constant.language.escape", + regex : /\$\{[^"\}]+\}?/ // this is wrong but ok for now + }, { token : "string", regex : '"', next : "start" }, { token : "string", - regex : '[^"]+' + regex : '.+?' } ], "qstring" : [ { + token : "constant.language.escape", + regex : "\\\\['\\\\]" + }, { token : "string", regex : "'", next : "start" }, { token : "string", - regex : "[^']+" + regex : ".+?" } ], "htmlcomment" : [ - { - token : "comment", - regex : ".*?-->", - next : "start" - }, { - token : "comment", - regex : ".+" - } + { + token : "comment", + regex : ".*?-->", + next : "start" + }, { + token : "comment", + regex : ".+" + } ], "htmltag" : [ { From c8706415065ae87751d2f58ca1c7f6718f1715e5 Mon Sep 17 00:00:00 2001 From: nightwing Date: Sat, 26 May 2012 22:22:40 +0400 Subject: [PATCH 2/4] improve json syntax highlighting --- lib/ace/mode/json_highlight_rules.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/ace/mode/json_highlight_rules.js b/lib/ace/mode/json_highlight_rules.js index 8e4dbdcf..5a039869 100644 --- a/lib/ace/mode/json_highlight_rules.js +++ b/lib/ace/mode/json_highlight_rules.js @@ -49,8 +49,12 @@ var JsonHighlightRules = function() { this.$rules = { "start" : [ { + token : "variable", // single line + regex : '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]\\s*(?=:)' + }, { token : "string", // single line - regex : '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]' + regex : '"', + next : "string" }, { token : "constant.numeric", // hex regex : "0[xX][0-9a-fA-F]+\\b" @@ -76,6 +80,26 @@ var JsonHighlightRules = function() { token : "text", regex : "\\s+" } + ], + "string" : [ + { + token : "constant.language.escape", + regex : /\\(?:x[0-9a-fA-F]{2}|u[0-9a-fA-F]{4})/ + }, { + token : "string", + regex : '[^"\\\\]+', + merge : true + }, { + token : "string", + regex : '"', + next : "start", + merge : true + }, { + token : "string", + regex : "", + next : "start", + merge : true + } ] }; From 3f3769c80db3876e4a0d4f6dd4a26b4b931b5fd1 Mon Sep 17 00:00:00 2001 From: nightwing Date: Sat, 26 May 2012 23:51:19 +0400 Subject: [PATCH 3/4] add diff mode --- demo/kitchen-sink/demo.js | 2 + demo/kitchen-sink/docs/diff.diff | 69 +++++++++++++++++ lib/ace/mode/diff.js | 91 ++++++++++++++++++++++ lib/ace/mode/diff_highlight_rules.js | 108 +++++++++++++++++++++++++++ 4 files changed, 270 insertions(+) create mode 100644 demo/kitchen-sink/docs/diff.diff create mode 100644 lib/ace/mode/diff.js create mode 100644 lib/ace/mode/diff_highlight_rules.js diff --git a/demo/kitchen-sink/demo.js b/demo/kitchen-sink/demo.js index fcf5732f..845d7804 100644 --- a/demo/kitchen-sink/demo.js +++ b/demo/kitchen-sink/demo.js @@ -101,6 +101,7 @@ var modes = [ new Mode("haxe", "haXe", ["hx"]), new Mode("html", "HTML", ["html", "htm"]), new Mode("java", "Java", ["java"]), + new Mode("diff", "Diff", ["diff", "patch"]), new Mode("javascript", "JavaScript", ["js"]), new Mode("json", "JSON", ["json"]), new Mode("latex", "LaTeX", ["tex"]), @@ -165,6 +166,7 @@ var docs = { "docs/plaintext.txt": {name: "Plain Text", prepare: makeHuge, wrapped: true}, "docs/coffeescript.coffee": "Coffeescript", "docs/json.json": "JSON", + "docs/diff.diff": "Diff", "docs/css.css": "CSS", "docs/scss.scss": "SCSS", "docs/less.less": "LESS", diff --git a/demo/kitchen-sink/docs/diff.diff b/demo/kitchen-sink/docs/diff.diff new file mode 100644 index 00000000..3f58128a --- /dev/null +++ b/demo/kitchen-sink/docs/diff.diff @@ -0,0 +1,69 @@ +diff --git a/lib/ace/edit_session.js b/lib/ace/edit_session.js +index 23fc3fc..ed3b273 100644 +--- a/lib/ace/edit_session.js ++++ b/lib/ace/edit_session.js +@@ -51,6 +51,7 @@ var TextMode = require("./mode/text").Mode; + var Range = require("./range").Range; + var Document = require("./document").Document; + var BackgroundTokenizer = require("./background_tokenizer").BackgroundTokenizer; ++var SearchHighlight = require("./search_highlight").SearchHighlight; + + /** + * class EditSession +@@ -307,6 +308,13 @@ var EditSession = function(text, mode) { + return token; + }; + ++ this.highlight = function(re) { ++ if (!this.$searchHighlight) { ++ var highlight = new SearchHighlight(null, "ace_selected_word", "text"); ++ this.$searchHighlight = this.addDynamicMarker(highlight); ++ } ++ this.$searchHighlight.setRegexp(re); ++ } + /** + * EditSession.setUndoManager(undoManager) + * - undoManager (UndoManager): The new undo manager +@@ -556,7 +564,8 @@ var EditSession = function(text, mode) { + type : type || "line", + renderer: typeof type == "function" ? type : null, + clazz : clazz, +- inFront: !!inFront ++ inFront: !!inFront, ++ id: id + } + + if (inFront) { +diff --git a/lib/ace/editor.js b/lib/ace/editor.js +index 834e603..b27ec73 100644 +--- a/lib/ace/editor.js ++++ b/lib/ace/editor.js +@@ -494,7 +494,7 @@ var Editor = function(renderer, session) { + * Emitted when a selection has changed. + **/ + this.onSelectionChange = function(e) { +- var session = this.getSession(); ++ var session = this.session; + + if (session.$selectionMarker) { + session.removeMarker(session.$selectionMarker); +@@ -509,12 +509,40 @@ var Editor = function(renderer, session) { + this.$updateHighlightActiveLine(); + } + +- var self = this; +- if (this.$highlightSelectedWord && !this.$wordHighlightTimer) +- this.$wordHighlightTimer = setTimeout(function() { +- self.session.$mode.highlightSelection(self); +- self.$wordHighlightTimer = null; +- }, 30, this); ++ var re = this.$highlightSelectedWord && this.$getSelectionHighLightRegexp() + }; +diff --git a/lib/ace/search_highlight.js b/lib/ace/search_highlight.js +new file mode 100644 +index 0000000..b2df779 +--- /dev/null ++++ b/lib/ace/search_highlight.js +@@ -0,0 +1,3 @@ ++new ++empty file \ No newline at end of file diff --git a/lib/ace/mode/diff.js b/lib/ace/mode/diff.js new file mode 100644 index 00000000..9795f79d --- /dev/null +++ b/lib/ace/mode/diff.js @@ -0,0 +1,91 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Ajax.org Code Editor (ACE). + * + * The Initial Developer of the Original Code is + * Ajax.org B.V. + * Portions created by the Initial Developer are Copyright (C) 2010 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +define(function(require, exports, module) { +"use strict"; + +var oop = require("../lib/oop"); +var TextMode = require("./text").Mode; +var Tokenizer = require("../tokenizer").Tokenizer; +var HighlightRules = require("./diff_highlight_rules").DiffHighlightRules; + +var Mode = function() { + this.$tokenizer = new Tokenizer(new HighlightRules().getRules(), "i"); + this.foldingRules = new FoldMode(); +}; +oop.inherits(Mode, TextMode); + +(function() { + +}).call(Mode.prototype); + +exports.Mode = Mode; + + +// folding +var BaseFoldMode = require("./folding/fold_mode").FoldMode; +var Range = require("../range").Range; + +var FoldMode = exports.FoldMode = function() {}; +oop.inherits(FoldMode, BaseFoldMode); + +(function() { + + this.foldingStartMarker = /^(diff|index|@@|\+{3}|\*{5})/; + + this.getFoldWidgetRange = function(session, foldStyle, row) { + var line = session.getLine(row); + var start = {row: row, column: line.length - 2}; + + var regList = ["diff", "index", "\\+{3}", "@@|\\*{5}"]; + for (var i = 1; i <= regList.length; i++) { + var re = RegExp("^(" + regList.slice(0, i).join("|") + ")"); + if (re.test(line)) + break; + } + + for (var l = session.getLength(); ++row < l; ) { + line = session.getLine(row); + if (re.test(line)) + break + } + + return Range.fromPoints(start, {row: row - 1, column: line.length}); + }; + +}).call(FoldMode.prototype); + +}); diff --git a/lib/ace/mode/diff_highlight_rules.js b/lib/ace/mode/diff_highlight_rules.js new file mode 100644 index 00000000..eb38a1d3 --- /dev/null +++ b/lib/ace/mode/diff_highlight_rules.js @@ -0,0 +1,108 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Ajax.org Code Editor (ACE). + * + * The Initial Developer of the Original Code is + * Ajax.org B.V. + * Portions created by the Initial Developer are Copyright (C) 2010 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +define(function(require, exports, module) { +"use strict"; + +var oop = require("../lib/oop"); +var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules; + +var DiffHighlightRules = function() { + // regexp must not have capturing parentheses. Use (?:) instead. + // regexps are ordered -> the first match is used + + this.$rules = { + "start" : [{ + "regex": "^(?:\\*{15}|={67}|-{3}|\\+{3})$", + "token": "punctuation.definition.separator.diff", + "name": "keyword" + }, { //diff.range.unified + "regex": "^(@@)(\\s*.+?\\s*)(@@)(.*)$", + "token": [ + "constant", + "constant.numeric", + "constant", + "comment.doc.tag" + ] + }, { //diff.range.normal + "regex": "^(\\d+)([,\\d]+)(a|d|c)(\\d+)([,\\d]+)(.*)$", + "token": [ + "constant.numeric", + "punctuation.definition.range.diff", + "constant.function", + "constant.numeric", + "punctuation.definition.range.diff", + "invalid" + ], + "name": "meta." + }, { + "regex": "^(?:(\\-{3}|\\+{3}|\\*{3})( .+))$", + "token": [ + "constant.numeric", + "meta.tag" + ] + }, { // added + "regex": "^([!+>])(.*?)(\\s*)$", + "token": [ + "support.constant", + "text", + "invalid" + ], + }, { // removed + "regex": "^([<\\-])(.*?)(\\s*)$", + "token": [ + "support.function", + "string", + "invalid" + ], + }, { + "regex": "^(diff)( .+)$", + "token": ["keyword", "string.regex"] + }, { + "regex": "^Index .+$", + "token": "variable" + }, { + "regex": "^(.*?)(\\s*)$", + "token": ["invisible", "invalid"] + } + ] + }; +}; + +oop.inherits(DiffHighlightRules, TextHighlightRules); + +exports.DiffHighlightRules = DiffHighlightRules; +}); From 6ccfb6e439d785b4e0b0b162be58083ffad4d5c3 Mon Sep 17 00:00:00 2001 From: nightwing Date: Sun, 27 May 2012 14:50:30 +0400 Subject: [PATCH 4/4] tweak diff mode --- lib/ace/mode/diff.js | 37 +------------- lib/ace/mode/diff_highlight_rules.js | 6 +-- lib/ace/mode/folding/diff.js | 76 ++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 38 deletions(-) create mode 100644 lib/ace/mode/folding/diff.js diff --git a/lib/ace/mode/diff.js b/lib/ace/mode/diff.js index 9795f79d..2e383764 100644 --- a/lib/ace/mode/diff.js +++ b/lib/ace/mode/diff.js @@ -41,10 +41,11 @@ var oop = require("../lib/oop"); var TextMode = require("./text").Mode; var Tokenizer = require("../tokenizer").Tokenizer; var HighlightRules = require("./diff_highlight_rules").DiffHighlightRules; +var FoldMode = require("./folding/diff").FoldMode; var Mode = function() { this.$tokenizer = new Tokenizer(new HighlightRules().getRules(), "i"); - this.foldingRules = new FoldMode(); + this.foldingRules = new FoldMode(["diff", "index", "\\+{3}", "@@|\\*{5}"], "i"); }; oop.inherits(Mode, TextMode); @@ -54,38 +55,4 @@ oop.inherits(Mode, TextMode); exports.Mode = Mode; - -// folding -var BaseFoldMode = require("./folding/fold_mode").FoldMode; -var Range = require("../range").Range; - -var FoldMode = exports.FoldMode = function() {}; -oop.inherits(FoldMode, BaseFoldMode); - -(function() { - - this.foldingStartMarker = /^(diff|index|@@|\+{3}|\*{5})/; - - this.getFoldWidgetRange = function(session, foldStyle, row) { - var line = session.getLine(row); - var start = {row: row, column: line.length - 2}; - - var regList = ["diff", "index", "\\+{3}", "@@|\\*{5}"]; - for (var i = 1; i <= regList.length; i++) { - var re = RegExp("^(" + regList.slice(0, i).join("|") + ")"); - if (re.test(line)) - break; - } - - for (var l = session.getLength(); ++row < l; ) { - line = session.getLine(row); - if (re.test(line)) - break - } - - return Range.fromPoints(start, {row: row - 1, column: line.length}); - }; - -}).call(FoldMode.prototype); - }); diff --git a/lib/ace/mode/diff_highlight_rules.js b/lib/ace/mode/diff_highlight_rules.js index eb38a1d3..fbbc282f 100644 --- a/lib/ace/mode/diff_highlight_rules.js +++ b/lib/ace/mode/diff_highlight_rules.js @@ -89,10 +89,10 @@ var DiffHighlightRules = function() { "invalid" ], }, { - "regex": "^(diff)( .+)$", - "token": ["keyword", "string.regex"] + "regex": "^(diff)(\\s+--\\w+)?(.+?)( .+)?$", + "token": ["variable", "variable", "keyword", "variable"] }, { - "regex": "^Index .+$", + "regex": "^Index.+$", "token": "variable" }, { "regex": "^(.*?)(\\s*)$", diff --git a/lib/ace/mode/folding/diff.js b/lib/ace/mode/folding/diff.js new file mode 100644 index 00000000..89bea274 --- /dev/null +++ b/lib/ace/mode/folding/diff.js @@ -0,0 +1,76 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Ajax.org Code Editor (ACE). + * + * The Initial Developer of the Original Code is + * Ajax.org B.V. + * Portions created by the Initial Developer are Copyright (C) 2010 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Fabian Jakobs + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +define(function(require, exports, module) { +"use strict"; + +var oop = require("../../lib/oop"); +var BaseFoldMode = require("./fold_mode").FoldMode; +var Range = require("../../range").Range; + +var FoldMode = exports.FoldMode = function(levels, flag) { + this.regExpList = levels; + this.flag = flag; + this.foldingStartMarker = RegExp("^(" + levels.join("|") + ")", this.flag); +}; +oop.inherits(FoldMode, BaseFoldMode); + +(function() { + this.getFoldWidgetRange = function(session, foldStyle, row) { + var line = session.getLine(row); + var start = {row: row, column: line.length}; + + var regList = this.regExpList; + for (var i = 1; i <= regList.length; i++) { + var re = RegExp("^(" + regList.slice(0, i).join("|") + ")", this.flag); + if (re.test(line)) + break; + } + + for (var l = session.getLength(); ++row < l; ) { + line = session.getLine(row); + if (re.test(line)) + break + } + if (row == start.row + 1) + return; + return Range.fromPoints(start, {row: row - 1, column: line.length}); + }; + +}).call(FoldMode.prototype); + +});