From ea9a0dda835fecdd358dddcaec417af9b3a05cb5 Mon Sep 17 00:00:00 2001 From: Shane Kilkelly Date: Thu, 25 May 2017 09:33:15 +0100 Subject: [PATCH] More robust change detection for autocomplete. - accurately detect when labels are involved in 'remove' event --- .../auto-complete/AutoCompleteManager.coffee | 40 +++++++++++-------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/services/web/public/coffee/ide/editor/directives/aceEditor/auto-complete/AutoCompleteManager.coffee b/services/web/public/coffee/ide/editor/directives/aceEditor/auto-complete/AutoCompleteManager.coffee index 6b95bc4cc4..6011623cd4 100644 --- a/services/web/public/coffee/ide/editor/directives/aceEditor/auto-complete/AutoCompleteManager.coffee +++ b/services/web/public/coffee/ide/editor/directives/aceEditor/auto-complete/AutoCompleteManager.coffee @@ -121,27 +121,33 @@ define [ onChange: (change) -> cursorPosition = @editor.getCursorPosition() end = change.end + range = new Range(end.row, 0, end.row, end.column) + lineUpToCursor = @editor.getSession().getTextRange(range) + commandFragment = getLastCommandFragment(lineUpToCursor) + + # Check if user has backspaced/deleted a highlighted region of text + # and see if that contains a `\label{}` + if change.action == 'remove' + if _.any(change.lines, (line) -> line.match(/\\label\{[^\}\n\\]{0,80}\}/)) + @labelsManager.scheduleLoadLabelsFromOpenDoc() + if commandFragment? and commandFragment.length > 2 + if commandFragment.startsWith('\\label{') + @labelsManager.scheduleLoadLabelsFromOpenDoc() + # Check that this change was made by us, not a collaborator # (Cursor is still one place behind) # NOTE: this is also the case when a user backspaces over a highlighted region - if end.row == cursorPosition.row and end.column == cursorPosition.column + 1 - if change.action == "insert" - range = new Range(end.row, 0, end.row, end.column) - lineUpToCursor = @editor.getSession().getTextRange(range) - commandFragment = getLastCommandFragment(lineUpToCursor) - - if commandFragment? and commandFragment.length > 2 - if commandFragment.startsWith('\\label{') - @labelsManager.scheduleLoadLabelsFromOpenDoc() - setTimeout () => - @editor.execCommand("startAutocomplete") - , 0 - else - # Check if user has backspaced/deleted a highlighted region of text - # and see if that contains a `\label{}` - if change.action == 'remove' - if _.any(change.lines, (line) -> line.match(/\\label{.*}/)) + if ( + change.action == "insert" and + end.row == cursorPosition.row and + end.column == cursorPosition.column + 1 + ) + if commandFragment? and commandFragment.length > 2 + if commandFragment.startsWith('\\label{') @labelsManager.scheduleLoadLabelsFromOpenDoc() + setTimeout () => + @editor.execCommand("startAutocomplete") + , 0 monkeyPatchAutocomplete: () -> Autocomplete = ace.require("ace/autocomplete").Autocomplete