Hi,
the first issue appears to be a bug, and unfortunately there doesn't seem to be any easy way to intercept it from TamperMonkey, but it will be fixed in the next version
for gq the following can be a good place to start, but the real gq in vim behaves somewhat differently with the way it handles comments and syntax
let me know if it works for you and if you think we should add it to the ace by default.
vimKeyboard = require("ace/keyboard/vim")
vimKeyboard.handler.defaultKeymap.push({
keys: 'gq',
type: 'operator',
operator: 'hardWrap'
})
vimKeyboard.Vim.defineOperator("hardWrap", function(cm, operatorArgs, ranges, oldAnchor, newHead) {
var anchor = ranges[0].anchor.line;
var head = ranges[0].head.line;
if (anchor < head) {
hardWrap(cm.ace, anchor, head);
} else {
hardWrap(cm.ace, head, anchor);
}
return cm.getCursor()
})
var {Range} = require("ace/range")
editor.setOption("printMarginColumn", 40)
function hardWrap(editor, startRow, endRow) {
var max = editor.getOption("printMarginColumn")
var row = startRow;
while (row < endRow) {
// debugger
var line = editor.session.getLine(row)
console.log(line, row, endRow, session.getLength())
if (line.length > max) {
var space = findSpace(line, max, 5);
if (space) {
editor.session.replace(new Range(row,space.start,row,space.end), "\n")
}
endRow++
} else if (/\S/.test(line)) {
var nextLine = session.getLine(row + 1)
if (nextLine && /\S/.test(nextLine)) {
var trimmedLine = line.replace(/\s+$/, "")
var trimmedNextLine = nextLine.replace(/^\s+/, "")
var mergedLine = trimmedLine + " " + trimmedNextLine;
var space = findSpace(mergedLine, max, 5);
if (space && space.start > trimmedLine.length || mergedLine.length < max) {
var replaceRange = new Range(row,trimmedLine.length,row + 1,nextLine.length - trimmedNextLine.length)
editor.session.replace(replaceRange, " ")
row--
endRow--
}
}
}
row++
}
function findSpace(line, max, min) {
if (line.length < max)
return;
var before = line.slice(0, max)
var after = line.slice(max)
var spaceAfter = /^(?:(\s+)|(\S+)(\s+))/.exec(after)
var spaceBefore = /(?:(\s+)|(\s+)(\S+))$/.exec(before)
var start = 0
var end = 0
if (spaceBefore && !spaceBefore[2]) {
start = max - spaceBefore[1].length
end = max
}
if (spaceAfter && !spaceAfter[2]) {
if (!start)
start = max
end = max + spaceAfter[1].length
}
if (start) {
return {
start: start,
end: end
}
}
if (spaceBefore && spaceBefore[2] && spaceBefore.index > min) {
return {
start: spaceBefore.index,
end: spaceBefore.index + spaceAfter[3].length
}
}
}
}