Added lucene mode

This commit is contained in:
Graham Scott 2012-11-16 14:45:39 +01:00
commit 7a378c9ab8
8 changed files with 174 additions and 6 deletions

16
lib/ace/mode/lucene.js Normal file
View file

@ -0,0 +1,16 @@
define(function(require, exports, module) {
'use strict';
var oop = require("../lib/oop");
var TextMode = require("./text").Mode;
var Tokenizer = require("../tokenizer").Tokenizer;
var LuceneHighlightRules = require("./lucene_highlight_rules").LuceneHighlightRules;
var Mode = function() {
this.$tokenizer = new Tokenizer(new LuceneHighlightRules().getRules());
};
oop.inherits(Mode, TextMode);
exports.Mode = Mode;
});

View file

@ -0,0 +1,49 @@
define(function(require, exports, module) {
"use strict";
var oop = require("../lib/oop");
var lang = require("../lib/lang");
var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
var LuceneHighlightRules = function() {
this.$rules = {
"start" : [
{
token : "constant.character.negation",
regex : "[\\-]"
}, {
token : "constant.character.interro",
regex : "[\\?]"
}, {
token : "constant.character.asterisk",
regex : "[\\*]"
}, {
token: 'constant.character.proximity',
regex: '~[0-9]+\\b'
}, {
token : 'keyword.operator',
regex: '(?:AND|OR|NOT)\\b'
}, {
token : "paren.lparen",
regex : "[\\(]"
}, {
token : "paren.rparen",
regex : "[\\)]"
}, {
token : "keyword",
regex : "[\\S]+:"
}, {
token : "string", // " string
regex : '".*?"'
}, {
token : "text",
regex : "\\s+"
}
]
};
};
oop.inherits(LuceneHighlightRules, TextHighlightRules);
exports.LuceneHighlightRules = LuceneHighlightRules;
});

View file

@ -0,0 +1,99 @@
if (typeof process !== "undefined") {
require("amd-loader");
}
define(function(require, exports, module) {
"use strict";
var LuceneMode = require("./lucene").Mode;
var assert = require("../test/assertions");
module.exports = {
name: "Lucene Tokenizer",
setUp : function() {
this.tokenizer = new LuceneMode().getTokenizer();
},
"test: recognises AND as keyword" : function() {
var tokens = this.tokenizer.getLineTokens("AND", "start").tokens;
assert.equal("keyword.operator", tokens[0].type);
},
"test: recognises OR as keyword" : function() {
var tokens = this.tokenizer.getLineTokens("OR", "start").tokens;
assert.equal("keyword.operator", tokens[0].type);
},
"test: recognises NOT as keyword" : function() {
var tokens = this.tokenizer.getLineTokens("NOT", "start").tokens;
assert.equal("keyword.operator", tokens[0].type);
},
'test: recognises "hello this is dog" as string' : function() {
var tokens = this.tokenizer.getLineTokens('"hello this is dog"', "start").tokens;
assert.equal("string", tokens[0].type);
},
'test: recognises -"hello this is dog" as negation with string' : function() {
var tokens = this.tokenizer.getLineTokens('-"hello this is dog"', "start").tokens;
assert.equal("constant.character.negation", tokens[0].type);
assert.equal("string", tokens[1].type);
},
'test: recognises ~100 as text with proximity' : function() {
var tokens = this.tokenizer.getLineTokens('~100', "start").tokens;
assert.equal("constant.character.proximity", tokens[0].type);
},
'test: recognises "hello this is dog"~100 as string with proximity' : function() {
var tokens = this.tokenizer.getLineTokens('"hello this is dog"~100', "start").tokens;
assert.equal("string", tokens[0].type);
assert.equal("constant.character.proximity", tokens[1].type);
},
'test: recognises raw:"hello this is dog" as keyword' : function() {
var tokens = this.tokenizer.getLineTokens('raw:"hello this is dog"', "start").tokens;
assert.equal("keyword", tokens[0].type);
},
'test: recognises raw:foo as"keyword' : function() {
var tokens = this.tokenizer.getLineTokens('raw:foo', "start").tokens;
assert.equal("keyword", tokens[0].type);
},
'test: recognises "(" as opening parenthesis' : function() {
var tokens = this.tokenizer.getLineTokens('(', "start").tokens;
assert.equal("paren.lparen", tokens[0].type);
},
'test: recognises ")" as closing parenthesis' : function() {
var tokens = this.tokenizer.getLineTokens(')', "start").tokens;
assert.equal("paren.rparen", tokens[0].type);
},
'test: recognises foo* as text with asterisk' : function() {
var tokens = this.tokenizer.getLineTokens('foo*', "start").tokens;
assert.equal("text", tokens[0].type);
assert.equal("constant.character.asterisk", tokens[1].type);
},
'test: recognises foo? as text with interro' : function() {
var tokens = this.tokenizer.getLineTokens('foo?', "start").tokens;
assert.equal("text", tokens[0].type);
assert.equal("constant.character.interro", tokens[1].type);
},
'test: recognises single word as text' : function() {
var tokens = this.tokenizer.getLineTokens(' foo', "start").tokens;
assert.equal("text", tokens[0].type);
}
};
});
if (typeof module !== "undefined" && module === require.main) {
require("asyncjs").test.testcase(module.exports).exec();
}

View file

@ -33,6 +33,7 @@ var testNames = [
"ace/mode/javascript_test",
"ace/mode/javascript_highlight_rules_test",
"ace/mode/javascript_worker_test",
"ace/mode/lucene_highlight_rules_test",
"ace/mode/python_test",
"ace/mode/ruby_highlight_rules_test",
"ace/mode/text_test",
@ -60,7 +61,7 @@ for (var i in testNames) {
var nav = document.createElement("div");
nav.innerHTML = html.join("");
nav.style.cssText = "position:absolute;right:0;top:0";
nav.style.cssText = "position:absolute;right:0;top:0";
document.body.appendChild(nav);
if (location.search)
@ -72,7 +73,7 @@ require(testNames, function() {
module.href = x;
return module;
});
async.list(tests)
.expand(function(test) {
return AsyncTest.testcase(test)