Skip to content

Commit

Permalink
Fix behaviour (hopefully) for the new [TextBuffer.onDidChange](atom…
Browse files Browse the repository at this point in the history
  • Loading branch information
Evpok committed Nov 7, 2017
1 parent 2c807fb commit 43aa396
Showing 1 changed file with 29 additions and 25 deletions.
54 changes: 29 additions & 25 deletions lib/environment_autocompletion/env_completer.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,8 @@ module.exports = {
// Environment completion
this.disposables.add(
// Observe text buffer changes
this.editor.getBuffer().onDidChange((event) => {
// Get out early if we are not breaking line to avoid dragging typing
if (!event.newText.endsWith('\n') && !event.newText.endsWith('\r')){return}

const buff = this.editor.getBuffer()
const currow = event.newRange.end.row
const prevline = buff.lineForRow(currow-1)
// Get out early if there are no environments beginnings too
if (!closer.has_beginners(prevline)){return}

// Real stuff happens here
const before = buff.getTextInRange(new atom.Range([0, 0], [currow-1, 0]))
const after = buff.getTextInRange(new atom.Range([currow+1, 0], buff.getEndPosition()))
const close = closer.to_close(prevline, before, after)
// Get out if there is nothing to close
if (!close.length){return}

// Elsewise, do the magic
buff.insert([currow, buff.lineLengthForRow(currow)], '\n'+closer.closers(close))
this.editor.moveUp()
this.editor.setIndentationForBufferRow(currow+1, this.editor.indentationForBufferRow(currow-1))
// Optional `\item` addition
if (this.addItemToListEnvironments){
buff.insert([currow, 0], closer.additions(close[0]))
}
this.editor.getBuffer().onDidChangeText((event) => {
event.changes.forEach((e, i, a) => {this.complete(e)})
})
)

Expand All @@ -51,6 +28,33 @@ module.exports = {
)
}

complete(change){
// Get out early if we are not breaking line to avoid dragging typing
if (!change.newText.startsWith('\n') && !change.newText.startsWith('\r')){return}

const buff = this.editor.getBuffer()
const currow = change.newRange.end.row
const prevline = buff.lineForRow(currow-1)
// Get out early if there are no environments beginnings too
if (!closer.has_beginners(prevline)){return}

// Real stuff happens here
const before = buff.getTextInRange(new atom.Range([0, 0], [currow-1, 0]))
const after = buff.getTextInRange(new atom.Range([currow+1, 0], buff.getEndPosition()))
const close = closer.to_close(prevline, before, after)
// Get out if there is nothing to close
if (!close.length){return}

// Elsewise, do the magic
buff.insert([currow, buff.lineLengthForRow(currow)], '\n'+closer.closers(close))
this.editor.moveUp()
this.editor.setIndentationForBufferRow(currow+1, this.editor.indentationForBufferRow(currow-1))
// Optional `\item` addition
if (this.addItemToListEnvironments){
buff.insert([currow, 0], closer.additions(close[0]))
}
}

destroy(){
this.disposables.dispose()
this.on_destroyed()
Expand Down

0 comments on commit 43aa396

Please sign in to comment.