cleanup emmet extension
This commit is contained in:
parent
c673190bf7
commit
b2612c6039
1 changed files with 51 additions and 31 deletions
|
|
@ -34,15 +34,7 @@ var HashHandler = require("ace/keyboard/hash_handler").HashHandler;
|
||||||
var Editor = require("ace/editor").Editor;
|
var Editor = require("ace/editor").Editor;
|
||||||
var snippetManager = require("ace/snippets").snippetManager;
|
var snippetManager = require("ace/snippets").snippetManager;
|
||||||
var Range = require("ace/range").Range;
|
var Range = require("ace/range").Range;
|
||||||
var emmet;
|
var emmet, emmetPath;
|
||||||
|
|
||||||
Editor.prototype.indexToPosition = function(index) {
|
|
||||||
return this.session.doc.indexToPosition(index);
|
|
||||||
};
|
|
||||||
|
|
||||||
Editor.prototype.positionToIndex = function(pos) {
|
|
||||||
return this.session.doc.positionToIndex(pos);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implementation of {@link IEmmetEditor} interface for Ace
|
* Implementation of {@link IEmmetEditor} interface for Ace
|
||||||
|
|
@ -72,9 +64,10 @@ AceEmmetEditor.prototype = {
|
||||||
getSelectionRange: function() {
|
getSelectionRange: function() {
|
||||||
// TODO should start be caret position instead?
|
// TODO should start be caret position instead?
|
||||||
var range = this.ace.getSelectionRange();
|
var range = this.ace.getSelectionRange();
|
||||||
|
var doc = this.ace.session.doc;
|
||||||
return {
|
return {
|
||||||
start: this.ace.positionToIndex(range.start),
|
start: doc.positionToIndex(range.start),
|
||||||
end: this.ace.positionToIndex(range.end)
|
end: doc.positionToIndex(range.end)
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
@ -91,9 +84,10 @@ AceEmmetEditor.prototype = {
|
||||||
* editor.createSelection(15);
|
* editor.createSelection(15);
|
||||||
*/
|
*/
|
||||||
createSelection: function(start, end) {
|
createSelection: function(start, end) {
|
||||||
|
var doc = this.ace.session.doc;
|
||||||
this.ace.selection.setRange({
|
this.ace.selection.setRange({
|
||||||
start: this.ace.indexToPosition(start),
|
start: doc.indexToPosition(start),
|
||||||
end: this.ace.indexToPosition(end)
|
end: doc.indexToPosition(end)
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
@ -106,9 +100,10 @@ AceEmmetEditor.prototype = {
|
||||||
* alert(range.start + ', ' + range.end);
|
* alert(range.start + ', ' + range.end);
|
||||||
*/
|
*/
|
||||||
getCurrentLineRange: function() {
|
getCurrentLineRange: function() {
|
||||||
var row = this.ace.getCursorPosition().row;
|
var ace = this.ace;
|
||||||
var lineLength = this.ace.session.getLine(row).length;
|
var row = ace.getCursorPosition().row;
|
||||||
var index = this.ace.positionToIndex({row: row, column: 0});
|
var lineLength = ace.session.getLine(row).length;
|
||||||
|
var index = ace.session.doc.positionToIndex({row: row, column: 0});
|
||||||
return {
|
return {
|
||||||
start: index,
|
start: index,
|
||||||
end: index + lineLength
|
end: index + lineLength
|
||||||
|
|
@ -121,7 +116,7 @@ AceEmmetEditor.prototype = {
|
||||||
*/
|
*/
|
||||||
getCaretPos: function(){
|
getCaretPos: function(){
|
||||||
var pos = this.ace.getCursorPosition();
|
var pos = this.ace.getCursorPosition();
|
||||||
return this.ace.positionToIndex(pos);
|
return this.ace.session.doc.positionToIndex(pos);
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -129,7 +124,7 @@ AceEmmetEditor.prototype = {
|
||||||
* @param {Number} index Caret position
|
* @param {Number} index Caret position
|
||||||
*/
|
*/
|
||||||
setCaretPos: function(index){
|
setCaretPos: function(index){
|
||||||
var pos = this.ace.indexToPosition(index);
|
var pos = this.ace.session.doc.indexToPosition(index);
|
||||||
this.ace.selection.moveToPosition(pos);
|
this.ace.selection.moveToPosition(pos);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
@ -169,14 +164,15 @@ AceEmmetEditor.prototype = {
|
||||||
start = 0;
|
start = 0;
|
||||||
|
|
||||||
var editor = this.ace;
|
var editor = this.ace;
|
||||||
var range = Range.fromPoints(editor.indexToPosition(start), editor.indexToPosition(end));
|
var doc = editor.session.doc;
|
||||||
|
var range = Range.fromPoints(doc.indexToPosition(start), doc.indexToPosition(end));
|
||||||
editor.session.remove(range);
|
editor.session.remove(range);
|
||||||
|
|
||||||
range.end = range.start;
|
range.end = range.start;
|
||||||
//editor.selection.setRange(range);
|
//editor.selection.setRange(range);
|
||||||
|
|
||||||
value = this.$updateTabstops(value);
|
value = this.$updateTabstops(value);
|
||||||
snippetManager.insertSnippet(editor, value)
|
snippetManager.insertSnippet(editor, value);
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -292,7 +288,7 @@ AceEmmetEditor.prototype = {
|
||||||
lastZero = range.create(data.start, result);
|
lastZero = range.create(data.start, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return result;
|
||||||
},
|
},
|
||||||
escape: function(ch) {
|
escape: function(ch) {
|
||||||
if (ch == '$') return '\\$';
|
if (ch == '$') return '\\$';
|
||||||
|
|
@ -363,12 +359,17 @@ exports.runEmmetCommand = function(editor) {
|
||||||
}, 0);
|
}, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var pos = editor.selection.lead;
|
||||||
|
var token = editor.session.getTokenAt(pos.row, pos.column);
|
||||||
|
if (token && /\btag\b/.test(token.type))
|
||||||
|
return false;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var result = actions.run(this.action, editorProxy);
|
var result = actions.run(this.action, editorProxy);
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
editor._signal("changeStatus", typeof e == "string" ? e : e.message);
|
editor._signal("changeStatus", typeof e == "string" ? e : e.message);
|
||||||
console.log(e);
|
console.log(e);
|
||||||
result = false
|
result = false;
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
@ -383,21 +384,36 @@ for (var command in keymap) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
exports.updateCommands = function(editor, enabled) {
|
||||||
|
if (enabled) {
|
||||||
|
editor.keyBinding.addKeyboardHandler(exports.commands);
|
||||||
|
} else {
|
||||||
|
editor.keyBinding.removeKeyboardHandler(exports.commands);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.isSupportedMode = function(modeId) {
|
||||||
|
return modeId && /css|less|scss|sass|stylus|html|php|twig/.test(modeId);
|
||||||
|
};
|
||||||
|
|
||||||
var onChangeMode = function(e, target) {
|
var onChangeMode = function(e, target) {
|
||||||
var editor = target;
|
var editor = target;
|
||||||
if (!editor)
|
if (!editor)
|
||||||
return;
|
return;
|
||||||
var modeId = editor.session.$modeId;
|
var enabled = exports.isSupportedMode(editor.session.$modeId);
|
||||||
var enabled = modeId && /css|less|scss|sass|stylus|html|php/.test(modeId);
|
|
||||||
if (e.enableEmmet === false)
|
if (e.enableEmmet === false)
|
||||||
enabled = false;
|
enabled = false;
|
||||||
if (enabled)
|
if (enabled) {
|
||||||
editor.keyBinding.addKeyboardHandler(exports.commands);
|
if (typeof emmetPath == "string") {
|
||||||
else
|
require("ace/config").loadModule(emmetPath, function() {
|
||||||
editor.keyBinding.removeKeyboardHandler(exports.commands);
|
|
||||||
|
});
|
||||||
|
emmetPath = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exports.updateCommands(editor, enabled);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
exports.AceEmmetEditor = AceEmmetEditor;
|
exports.AceEmmetEditor = AceEmmetEditor;
|
||||||
require("ace/config").defineOptions(Editor.prototype, "editor", {
|
require("ace/config").defineOptions(Editor.prototype, "editor", {
|
||||||
enableEmmet: {
|
enableEmmet: {
|
||||||
|
|
@ -409,7 +425,11 @@ require("ace/config").defineOptions(Editor.prototype, "editor", {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
exports.setCore = function(e) {
|
||||||
exports.setCore = function(e) {emmet = e;};
|
if (typeof e == "string")
|
||||||
|
emmetPath = e;
|
||||||
|
else
|
||||||
|
emmet = e;
|
||||||
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue