Skip to content

Commit

Permalink
Integrate xqlint for semantic highlighting and coding hints. Format X…
Browse files Browse the repository at this point in the history
…Query code command. Enable multiple cursors.
  • Loading branch information
wolfgangmm committed Feb 16, 2013
1 parent e2c698f commit 3c1f913
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 30 deletions.
12 changes: 11 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@
<script src="$shared/resources/scripts/ace/theme-eclipse.js" type="text/javascript" charset="utf-8"/>
<script src="$shared/resources/scripts/ace/mode-javascript.js" type="text/javascript" charset="utf-8"/>
<script src="$shared/resources/scripts/ace/mode-xml.js" type="text/javascript" charset="utf-8"/>
<script src="$shared/resources/scripts/ace/mode-c_cpp.js" type="text/javascript" charset="utf-8"/>
<script src="$shared/resources/scripts/ace/mode-html.js" type="text/javascript" charset="utf-8"/>
<script src="$shared/resources/scripts/ace/mode-css.js" type="text/javascript" charset="utf-8"/>
<script src="$shared/resources/scripts/ace/mode-less.js" type="text/javascript" charset="utf-8"/>

<script type="text/javascript" src="resources/scripts/xqlint.min.js"/>

<!-- Uncompressed Javascript files. Uncomment for development/debugging. -->
<script type="text/javascript" src="src/ace-modes.js"/>
<script type="text/javascript" src="src/util.js"/>
Expand Down Expand Up @@ -168,6 +170,14 @@
</li>
</ul>
</li>
<li id="menu-xquery">
<a href="#">XQuery</a>
<ul>
<li>
<a href="#" id="menu-xquery-format">Format code</a>
</li>
</ul>
</li>
<li>
<a href="#">Help</a>
<ul>
Expand Down
3 changes: 2 additions & 1 deletion keybindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@
"toggleComment": ["Ctrl-Shift-C", "Command-Shift-C"],
"synchronize": ["Alt-Shift-S", "Command-Option-S"],
"preferences": ["Alt-Shift-P", "Ctrl-Command-P"],
"openApp": ["Ctrl-Alt-R", "Command-Option-R"]
"openApp": ["Ctrl-Alt-R", "Command-Option-R"],
"formatCode": ["Ctrl-Shift-P", "Command-Shift-P"]
}
77 changes: 68 additions & 9 deletions src/ace-modes.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,37 +222,41 @@ define("eXide/mode/xquery", function(require, exports, module) {

var oop = require("ace/lib/oop");
var TextMode = require("ace/mode/text").Mode;
var XQueryLexer = require("xqlint/XQueryLexer").XQueryLexer;

var Tokenizer = require("ace/tokenizer").Tokenizer;
var XQueryHighlightRules = require("eXide/mode/xquery_highlight_rules").XQueryHighlightRules;
var XQueryBehaviour = require("eXide/mode/behaviour/xquery").XQueryBehaviour;
var CStyleFoldMode = require("ace/mode/folding/cstyle").FoldMode;
var Range = require("ace/range").Range;

var Mode = function(parent) {
this.$tokenizer = new Tokenizer(new XQueryHighlightRules().getRules());
//this.$tokenizer = new Tokenizer(new XQueryHighlightRules().getRules());
this.$tokenizer = new XQueryLexer();
this.$behaviour = new XQueryBehaviour(parent);
this.foldingRules = new CStyleFoldMode();
};

oop.inherits(Mode, TextMode);

(function() {

this.getNextLineIndent = function(state, line, tab) {
var indent = this.$getIndent(line);
var match = line.match(/\s*(?:then|else|return|[{\(]|<\w+>)\s*$/);
if (match)
indent += tab;
var indent = this.$getIndent(line);
var match = line.match(/\s*(?:then|else|return|[{\(]|<\w+>)\s*$/);
if (match)
indent += tab;
return indent;
};

this.checkOutdent = function(state, line, input) {
if (! /^\s+$/.test(line))
if (! /^\s+$/.test(line))
return false;

return /^\s*[\}\)]/.test(input);
};

this.autoOutdent = function(state, doc, row) {
var line = doc.getLine(row);
var line = doc.getLine(row);
var match = line.match(/^(\s*[\}\)])/);

if (!match) return 0;
Expand Down Expand Up @@ -297,6 +301,61 @@ oop.inherits(Mode, TextMode);
doc.replace(range, outdent ? line.match(re)[1] : "(:" + line + ":)");
}
};

/*this.createWorker = function(session) {
this.$deltas = [];
var worker = new WorkerClient(["ace"], "ace/mode/xquery_worker", "XQueryWorker");
var that = this;
session.getDocument().on('change', function(evt){
that.$deltas.push(evt.data);
});
worker.attachToDocument(session.getDocument());
worker.on("start", function(e) {
that.$deltas = [];
});
worker.on("error", function(e) {
// errors are ignored because they are reported by eXist's compiler
});
worker.on("xqlint", function(e) {
var annotations = [];
for (var i = 0; i < e.data.length; i++) {
if (e.data[i].type !== "error") {
annotations.push({
row: e.data[i].pos.sl,
text: e.data[i].message,
type: e.data[i].type
});
}
}
session.setAnnotations(annotations);
});
worker.on("ok", function(e) {
session.clearAnnotations();
});
worker.on("highlight", function(tokens) {
if(that.$deltas.length > 0) return;
var firstRow = 0;
var lastRow = session.getLength() - 1;
var lines = tokens.data.lines;
var states = tokens.data.states;
session.bgTokenizer.lines = lines;
session.bgTokenizer.states = states;
session.bgTokenizer.fireUpdateEvent(firstRow, lastRow);
});
return worker;
};*/

}).call(Mode.prototype);

exports.Mode = Mode;
Expand Down
10 changes: 9 additions & 1 deletion src/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ eXide.edit.commands = (function () {
return {

init: function (editor) {
commands = editor.editor.commands;
var commands = editor.editor.commands;
$.ajax({
url: "keybindings.js",
dataType: 'json',
Expand Down Expand Up @@ -137,6 +137,14 @@ eXide.edit.commands = (function () {
editor.previousTab();
}
});
commands.addCommand({
name: "formatCode",
bindkey: bindKey(bindings.formatCode),
exec: function(env, args, request) {
$.log("Formatting");
editor.exec("format");
}
});
commands.addCommand({
name: "functionDoc",
bindKey: bindKey(bindings.functionDoc),
Expand Down
7 changes: 3 additions & 4 deletions src/eXide.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ eXide.app = (function() {
var projects;
var preferences;
var templates = {};

var menu;
var hitCount = 0;
var startOffset = 0;
var currentOffset = 0;
Expand All @@ -75,9 +75,9 @@ eXide.app = (function() {
var hasFocus = true;

return {

init: function(afterInitCallback) {
var menu = new eXide.util.Menubar($(".menu"));
menu = new eXide.util.Menubar($(".menu"));
projects = new eXide.edit.Projects();
editor = new eXide.edit.Editor(document.getElementById("editor"), menu);
deploymentEditor = new eXide.edit.PackageEditor(projects);
Expand Down Expand Up @@ -947,7 +947,6 @@ eXide.app = (function() {
menu.click("#menu-edit-preferences", function() {
preferences.show();
}, "preferences");

menu.click("#menu-navigate-definition", function () {
editor.exec("gotoDefinition");
}, "gotoDefinition");
Expand Down
27 changes: 23 additions & 4 deletions src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ eXide.edit.Document = (function() {
return this.$session.getValue();
};

Constr.prototype.setText = function(text) {
this.$session.setValue(text);
};

Constr.prototype.getName = function() {
return this.name;
};
Expand Down Expand Up @@ -185,9 +189,11 @@ eXide.edit.Editor = (function () {
this.editor.setBehavioursEnabled(true);
this.editor.setShowFoldWidgets(true);
this.editor.setFadeFoldWidgets(false);

// enable multiple cursors
require("ace/multi_select").MultiSelect(this.editor);
// register keybindings
eXide.edit.commands.init($this);
this.outline = new eXide.edit.Outline();
this.addEventListener("activate", this.outline, this.outline.updateOutline);
this.addEventListener("validate", this.outline, this.outline.updateOutline);
Expand Down Expand Up @@ -233,7 +239,7 @@ eXide.edit.Editor = (function () {

// register mode helpers
$this.modes = {
"xquery": new eXide.edit.XQueryModeHelper($this),
"xquery": new eXide.edit.XQueryModeHelper($this, menubar),
"xml": new eXide.edit.XMLModeHelper($this),
"html": new eXide.edit.XMLModeHelper($this),
"less": new eXide.edit.LessModeHelper($this),
Expand Down Expand Up @@ -380,7 +386,6 @@ eXide.edit.Editor = (function () {
eXide.util.message("Opening " + resource.path + " readonly!");
else
eXide.util.message("Opening " + resource.path);
$.log("external: %s", externalPath);
var doc = new eXide.edit.Document(resource.name, resource.path, new EditSession(data));
doc.editable = resource.writable;
doc.mime = mime;
Expand Down Expand Up @@ -415,6 +420,11 @@ eXide.edit.Editor = (function () {
$this.editor.setSession(doc.$session);
$this.editor.resize();
$this.editor.focus();

$this.triggerCheck();
if (doc.getModeHelper()) {
doc.getModeHelper().activate();
}
};

Constr.prototype.setMode = function(mode) {
Expand Down Expand Up @@ -643,6 +653,10 @@ eXide.edit.Editor = (function () {
};

Constr.prototype.switchTo = function(doc) {
var helper = this.activeDoc.getModeHelper();
if (helper) {
helper.deactivate();
}
this.editor.setSession(doc.$session);
this.editor.resize();
this.activeDoc = doc;
Expand All @@ -658,6 +672,11 @@ eXide.edit.Editor = (function () {
});
this.updateStatus("");
this.$triggerEvent("activate", [doc]);

helper = doc.getModeHelper();
if (helper) {
helper.activate();
}
};

Constr.prototype.updateTabStatus = function(oldPath, doc) {
Expand Down
6 changes: 6 additions & 0 deletions src/mode-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ eXide.edit.ModeHelper = (function () {

Constr.prototype = {

activate: function() {
},

deactivate: function() {
},

/**
* Add a command which can be invoked dynamically by the editor
*/
Expand Down
1 change: 0 additions & 1 deletion src/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ eXide.util.Preferences = (function () {
};

Constr.prototype.applyPreferences = function () {
$.log("Applying preferences: %o", this.preferences);
var $this = this;
this.editor.setTheme(this.preferences.theme);
this.editor.editor.setShowInvisibles(this.preferences.showInvisibles);
Expand Down
Loading

0 comments on commit 3c1f913

Please sign in to comment.