手动更改 ace 行号
Manually change ace line numbers
我将 ace 编辑器用于多种用途,但一个用例是渲染差异。
渲染 diff 时,您会看到常规线条、绿线和红线,与您在 git 中看到的相同。我已经完成了所有这些工作,但现在我希望能够更改行号,以便行号仅出现在先前和插入的行上,我不希望行号出现在已删除的行上。
我在 ace API 中找不到任何东西可以做到这一点,有人知道是否有简单的方法吗?切换到 CodeMirror 可能最简单,因为我看到他们有这个:
lineNumberFormatter: function(line: integer) → string
我可以手动更改 DOM 但我担心的是:
- 那会很密集吗?
- 这是个坏主意吗,ACE 稍后会自行刷新它吗?
ace 中 lineNumberFormatter
的等价物是 gutterRenderer
editor.session.gutterRenderer = {
getWidth: function(session, lastLineText, config) {
return lastLineText.length * config.characterWidth //desired gutter width in pixels
},
getText: function(session, row) {
return row.toString(36) // any string
}
}
editor.renderer.updateFull()
另一种选择是重新定义类似于https://github.com/c9/core/blob/master/plugins/c9.ide.scm/diff/unified.js#L185
的editor.renderer.$gutterLayer.update
方法
我将 ace 编辑器用于多种用途,但一个用例是渲染差异。
渲染 diff 时,您会看到常规线条、绿线和红线,与您在 git 中看到的相同。我已经完成了所有这些工作,但现在我希望能够更改行号,以便行号仅出现在先前和插入的行上,我不希望行号出现在已删除的行上。
我在 ace API 中找不到任何东西可以做到这一点,有人知道是否有简单的方法吗?切换到 CodeMirror 可能最简单,因为我看到他们有这个:
lineNumberFormatter: function(line: integer) → string
我可以手动更改 DOM 但我担心的是:
- 那会很密集吗?
- 这是个坏主意吗,ACE 稍后会自行刷新它吗?
ace 中 lineNumberFormatter
的等价物是 gutterRenderer
editor.session.gutterRenderer = {
getWidth: function(session, lastLineText, config) {
return lastLineText.length * config.characterWidth //desired gutter width in pixels
},
getText: function(session, row) {
return row.toString(36) // any string
}
}
editor.renderer.updateFull()
另一种选择是重新定义类似于https://github.com/c9/core/blob/master/plugins/c9.ide.scm/diff/unified.js#L185
的editor.renderer.$gutterLayer.update
方法